import type { Expand, IdField, SystemFields as NonIdSystemFields, } from "convex/server"; import * as Schema from "effect/Schema"; import * as GenericId from "./GenericId"; type SystemFieldsSchema = Schema.Struct<{ _id: Schema.Schema< GenericId.GenericId, GenericId.GenericId, never >; _creationTime: typeof Schema.Number; }>; /** * Produces a schema for Convex system fields. */ export const SystemFields = ( tableName: TableName, ): SystemFieldsSchema => Schema.Struct({ _id: GenericId.GenericId(tableName), _creationTime: Schema.Number, }); /** * Extend a table schema with Convex system fields. */ export const extendWithSystemFields = < TableName extends string, TableSchema extends Schema.Schema.AnyNoContext, >( tableName: TableName, schema: TableSchema, ): ExtendWithSystemFields => Schema.extend(SystemFields(tableName), schema); /** * Extend a table schema with Convex system fields at the type level. */ export type ExtendWithSystemFields< TableName extends string, TableSchema extends Schema.Schema.AnyNoContext, > = Schema.extend, TableSchema>; export type WithSystemFields = Expand< Readonly> & Readonly & Document >;