import {schemaWithName, InferSchemaType, createEnum} from '@orion-js/schema'
import {typedId} from '@orion-js/mongodb'
// Create a type-safe enum for product status
export const ProductStatusEnum = createEnum('ProductStatusEnum', [
'draft',
'active',
'discontinued'
] as const)
// Create a typed ID for products
export const typedProductId = typedId('prd')
export type ProductId = typeof typedProductId.__tsFieldType
export const ProductSchema = schemaWithName('Product', {
_id: {type: typedProductId},
name: {type: String, label: 'Product Name', min: 3, max: 100},
description: {type: String, optional: true},
status: {type: ProductStatusEnum, defaultValue: 'draft'},
price: {
type: Number,
validate: (value) => {
if (value < 0) return 'priceMustBePositive'
}
},
createdAt: {type: Date, defaultValue: () => new Date()}
})
// Infer the TypeScript type from your schema
export type Product = InferSchemaType<typeof ProductSchema>
// The type will be inferred as:
// type Product = {
// _id: `prd-${string}`;
// name: string;
// description?: string;
// status: "draft" | "active" | "discontinued";
// price: number;
// createdAt: Date;
// }