Orionjs MongoDB has built-in support for typed IDs with automatic prefixing:
Copy
import {typedId} from '@orion-js/mongodb'import {schemaWithName, InferSchemaType} from '@orion-js/schema'export const ProductSchema = schemaWithName('ProductSchema', { _id: {type: typedId('product')}, // Will create IDs like 'product-abc123' name: {type: String}, price: {type: Number}})export type ProductType = InferSchemaType<typeof ProductSchema>// The type for _id will be `product-${string}`
All MongoDB collection operations are typed based on your schema:
Copy
import {createCollection} from '@orion-js/mongodb'import {OptionalId} from '@orion-js/mongodb'// OptionalId makes the _id field optional for insert operationsconst newUser: OptionalId<UserType> = { name: 'John Doe', email: 'john@example.com', createdAt: new Date()}// Insert with type safetyconst userId = await Users.insertOne(newUser)// Update with type checkingawait Users.updateOne( {_id: userId}, {$set: {name: 'John Smith'}})// Find with type safetyconst user = await Users.findOne({_id: userId})console.log(user.name) // TypeScript knows the structure
When you create a collection with a schema, Orionjs automatically:
Creates a MongoDB validation schema based on your Orionjs schema
Applies validation on all insert and update operations
Enforces types and constraints defined in your schema
Copy
// This will throw a validation errortry { await Users.insertOne({ name: 123, // Type error: should be string email: 'invalid-email' // Validation error if email type is used })} catch (error) { console.error('Validation failed:', error)}