@prisma-next/contract-authoring 0.3.0-pr.88.6 → 0.3.0-pr.89.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1 +1 @@
1
- {"version":3,"file":"contract-builder.d.ts","sourceRoot":"","sources":["../src/contract-builder.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,4CAA4C,CAAC;AAChF,OAAO,KAAK,EACV,kBAAkB,EAClB,oBAAoB,EACpB,iBAAiB,EACjB,kBAAkB,EAClB,iBAAiB,EAClB,MAAM,iBAAiB,CAAC;AACzB,OAAO,EAAE,YAAY,EAAE,MAAM,iBAAiB,CAAC;AAC/C,OAAO,EAAE,YAAY,EAAE,MAAM,iBAAiB,CAAC;AAE/C,qBAAa,eAAe,CAC1B,MAAM,SAAS,MAAM,GAAG,SAAS,GAAG,SAAS,EAC7C,MAAM,SAAS,MAAM,CACnB,MAAM,EACN,iBAAiB,CACf,MAAM,EACN,MAAM,CAAC,MAAM,EAAE,kBAAkB,CAAC,MAAM,EAAE,OAAO,EAAE,MAAM,CAAC,CAAC,EAC3D,SAAS,MAAM,EAAE,GAAG,SAAS,CAC9B,CACF,GAAG,MAAM,CAAC,KAAK,EAAE,KAAK,CAAC,EACxB,MAAM,SAAS,MAAM,CACnB,MAAM,EACN,iBAAiB,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,kBAAkB,CAAC,CAAC,CAC9F,GAAG,MAAM,CAAC,KAAK,EAAE,KAAK,CAAC,EACxB,QAAQ,SAAS,MAAM,GAAG,SAAS,GAAG,SAAS,EAC/C,cAAc,SAAS,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,SAAS,GAAG,SAAS,EACtE,YAAY,SAAS,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,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CAEhD"}
package/dist/index.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  export type { ColumnBuilder, ColumnBuilderState, ColumnTypeDescriptor, ContractBuilderState, ForeignKeyDef, IndexDef, ModelBuilderState, RelationDefinition, TableBuilderState, UniqueConstraintDef, } from './builder-state';
2
2
  export { ContractBuilder, defineContract } from './contract-builder';
3
3
  export { ModelBuilder } from './model-builder';
4
- export { TableBuilder } from './table-builder';
4
+ export { createTable, TableBuilder } from './table-builder';
5
5
  export type { BuildModelFields, BuildModels, BuildRelations, BuildStorage, BuildStorageColumn, BuildStorageTables, ExtractColumns, ExtractModelFields, ExtractModelRelations, ExtractPrimaryKey, Mutable, } from './types';
6
6
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,YAAY,EACV,aAAa,EACb,kBAAkB,EAClB,oBAAoB,EACpB,oBAAoB,EACpB,aAAa,EACb,QAAQ,EACR,iBAAiB,EACjB,kBAAkB,EAClB,iBAAiB,EACjB,mBAAmB,GACpB,MAAM,iBAAiB,CAAC;AAEzB,OAAO,EAAE,eAAe,EAAE,cAAc,EAAE,MAAM,oBAAoB,CAAC;AACrE,OAAO,EAAE,YAAY,EAAE,MAAM,iBAAiB,CAAC;AAC/C,OAAO,EAAE,YAAY,EAAE,MAAM,iBAAiB,CAAC;AAE/C,YAAY,EACV,gBAAgB,EAChB,WAAW,EACX,cAAc,EACd,YAAY,EACZ,kBAAkB,EAClB,kBAAkB,EAClB,cAAc,EACd,kBAAkB,EAClB,qBAAqB,EACrB,iBAAiB,EACjB,OAAO,GACR,MAAM,SAAS,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,YAAY,EACV,aAAa,EACb,kBAAkB,EAClB,oBAAoB,EACpB,oBAAoB,EACpB,aAAa,EACb,QAAQ,EACR,iBAAiB,EACjB,kBAAkB,EAClB,iBAAiB,EACjB,mBAAmB,GACpB,MAAM,iBAAiB,CAAC;AAEzB,OAAO,EAAE,eAAe,EAAE,cAAc,EAAE,MAAM,oBAAoB,CAAC;AACrE,OAAO,EAAE,YAAY,EAAE,MAAM,iBAAiB,CAAC;AAC/C,OAAO,EAAE,WAAW,EAAE,YAAY,EAAE,MAAM,iBAAiB,CAAC;AAE5D,YAAY,EACV,gBAAgB,EAChB,WAAW,EACX,cAAc,EACd,YAAY,EACZ,kBAAkB,EAClB,kBAAkB,EAClB,cAAc,EACd,kBAAkB,EAClB,qBAAqB,EACrB,iBAAiB,EACjB,OAAO,GACR,MAAM,SAAS,CAAC"}
package/dist/index.js CHANGED
@@ -74,17 +74,21 @@ var ModelBuilder = class _ModelBuilder {
74
74
  };
75
75
 
76
76
  // src/table-builder.ts
77
+ function createTable(name) {
78
+ return new TableBuilder(name, {}, void 0, void 0, [], [], []);
79
+ }
77
80
  var TableBuilder = class _TableBuilder {
78
81
  _state;
79
- constructor(options) {
82
+ /** @internal Use createTable() instead */
83
+ constructor(name, columns, primaryKey, primaryKeyName, uniques, indexes, foreignKeys) {
80
84
  this._state = {
81
- name: options.name,
82
- columns: options.columns ?? {},
83
- primaryKey: options.primaryKey,
84
- primaryKeyName: options.primaryKeyName,
85
- uniques: options.uniques ?? [],
86
- indexes: options.indexes ?? [],
87
- foreignKeys: options.foreignKeys ?? []
85
+ name,
86
+ columns,
87
+ primaryKey,
88
+ primaryKeyName,
89
+ uniques,
90
+ indexes,
91
+ foreignKeys
88
92
  };
89
93
  }
90
94
  get _name() {
@@ -105,38 +109,63 @@ var TableBuilder = class _TableBuilder {
105
109
  type: codecId,
106
110
  nativeType
107
111
  };
108
- return new _TableBuilder({
109
- ...this._state,
110
- columns: { ...this._columns, [name]: columnState }
111
- });
112
+ const newColumns = { ...this._columns, [name]: columnState };
113
+ return new _TableBuilder(
114
+ this._state.name,
115
+ newColumns,
116
+ this._state.primaryKey,
117
+ this._state.primaryKeyName,
118
+ this._state.uniques,
119
+ this._state.indexes,
120
+ this._state.foreignKeys
121
+ );
112
122
  }
113
123
  primaryKey(columns, name) {
114
- return new _TableBuilder({
115
- ...this._state,
116
- primaryKey: columns,
117
- primaryKeyName: name
118
- });
124
+ return new _TableBuilder(
125
+ this._state.name,
126
+ this._state.columns,
127
+ columns,
128
+ name,
129
+ this._state.uniques,
130
+ this._state.indexes,
131
+ this._state.foreignKeys
132
+ );
119
133
  }
120
134
  unique(columns, name) {
121
135
  const constraint = name ? { columns, name } : { columns };
122
- return new _TableBuilder({
123
- ...this._state,
124
- uniques: [...this._state.uniques, constraint]
125
- });
136
+ return new _TableBuilder(
137
+ this._state.name,
138
+ this._state.columns,
139
+ this._state.primaryKey,
140
+ this._state.primaryKeyName,
141
+ [...this._state.uniques, constraint],
142
+ this._state.indexes,
143
+ this._state.foreignKeys
144
+ );
126
145
  }
127
146
  index(columns, name) {
128
147
  const indexDef = name ? { columns, name } : { columns };
129
- return new _TableBuilder({
130
- ...this._state,
131
- indexes: [...this._state.indexes, indexDef]
132
- });
148
+ return new _TableBuilder(
149
+ this._state.name,
150
+ this._state.columns,
151
+ this._state.primaryKey,
152
+ this._state.primaryKeyName,
153
+ this._state.uniques,
154
+ [...this._state.indexes, indexDef],
155
+ this._state.foreignKeys
156
+ );
133
157
  }
134
158
  foreignKey(columns, references, name) {
135
159
  const fkDef = name ? { columns, references, name } : { columns, references };
136
- return new _TableBuilder({
137
- ...this._state,
138
- foreignKeys: [...this._state.foreignKeys, fkDef]
139
- });
160
+ return new _TableBuilder(
161
+ this._state.name,
162
+ this._state.columns,
163
+ this._state.primaryKey,
164
+ this._state.primaryKeyName,
165
+ this._state.uniques,
166
+ this._state.indexes,
167
+ [...this._state.foreignKeys, fkDef]
168
+ );
140
169
  }
141
170
  build() {
142
171
  return {
@@ -173,7 +202,7 @@ var ContractBuilder = class _ContractBuilder {
173
202
  });
174
203
  }
175
204
  table(name, callback) {
176
- const tableBuilder = new TableBuilder({ name });
205
+ const tableBuilder = createTable(name);
177
206
  const result = callback(tableBuilder);
178
207
  const finalBuilder = result instanceof TableBuilder ? result : tableBuilder;
179
208
  const tableState = finalBuilder.build();
@@ -206,6 +235,7 @@ export {
206
235
  ContractBuilder,
207
236
  ModelBuilder,
208
237
  TableBuilder,
238
+ createTable,
209
239
  defineContract
210
240
  };
211
241
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/model-builder.ts","../src/table-builder.ts","../src/contract-builder.ts"],"sourcesContent":["import type { ModelBuilderState, RelationDefinition } from './builder-state';\n\nexport class ModelBuilder<\n Name extends string,\n Table extends string,\n Fields extends Record<string, string> = Record<never, never>,\n Relations extends Record<string, RelationDefinition> = Record<never, never>,\n> {\n private readonly _name: Name;\n private readonly _table: Table;\n private readonly _fields: Fields;\n private readonly _relations: Relations;\n\n constructor(\n name: Name,\n table: Table,\n fields: Fields = {} as Fields,\n relations: Relations = {} as Relations,\n ) {\n this._name = name;\n this._table = table;\n this._fields = fields;\n this._relations = relations;\n }\n\n field<FieldName extends string, ColumnName extends string>(\n fieldName: FieldName,\n columnName: ColumnName,\n ): ModelBuilder<Name, Table, Fields & Record<FieldName, ColumnName>, Relations> {\n return new ModelBuilder(\n this._name,\n this._table,\n {\n ...this._fields,\n [fieldName]: columnName,\n } as Fields & Record<FieldName, ColumnName>,\n this._relations,\n );\n }\n\n relation<RelationName extends string, ToModel extends string, ToTable extends string>(\n name: RelationName,\n options: {\n toModel: ToModel;\n toTable: ToTable;\n cardinality: '1:1' | '1:N' | 'N:1';\n on: {\n parentTable: Table;\n parentColumns: readonly string[];\n childTable: ToTable;\n childColumns: readonly string[];\n };\n },\n ): ModelBuilder<Name, Table, Fields, Relations & Record<RelationName, RelationDefinition>>;\n relation<\n RelationName extends string,\n ToModel extends string,\n ToTable extends string,\n JunctionTable extends string,\n >(\n name: RelationName,\n options: {\n toModel: ToModel;\n toTable: ToTable;\n cardinality: 'N:M';\n through: {\n table: JunctionTable;\n parentColumns: readonly string[];\n childColumns: readonly string[];\n };\n on: {\n parentTable: Table;\n parentColumns: readonly string[];\n childTable: JunctionTable;\n childColumns: readonly string[];\n };\n },\n ): ModelBuilder<Name, Table, Fields, Relations & Record<RelationName, RelationDefinition>>;\n relation<\n RelationName extends string,\n ToModel extends string,\n ToTable extends string,\n JunctionTable extends string = never,\n >(\n name: RelationName,\n options: {\n toModel: ToModel;\n toTable: ToTable;\n cardinality: '1:1' | '1:N' | 'N:1' | 'N:M';\n through?: {\n table: JunctionTable;\n parentColumns: readonly string[];\n childColumns: readonly string[];\n };\n on: {\n parentTable: Table;\n parentColumns: readonly string[];\n childTable: ToTable | JunctionTable;\n childColumns: readonly string[];\n };\n },\n ): ModelBuilder<Name, Table, Fields, Relations & Record<RelationName, RelationDefinition>> {\n // Validate parentTable matches model's table\n if (options.on.parentTable !== this._table) {\n throw new Error(\n `Relation \"${name}\" parentTable \"${options.on.parentTable}\" does not match model table \"${this._table}\"`,\n );\n }\n\n // Validate childTable matches toTable (for non-N:M) or through.table (for N:M)\n if (options.cardinality === 'N:M') {\n if (!options.through) {\n throw new Error(`Relation \"${name}\" with cardinality \"N:M\" requires through field`);\n }\n if (options.on.childTable !== options.through.table) {\n throw new Error(\n `Relation \"${name}\" childTable \"${options.on.childTable}\" does not match through.table \"${options.through.table}\"`,\n );\n }\n } else {\n if (options.on.childTable !== options.toTable) {\n throw new Error(\n `Relation \"${name}\" childTable \"${options.on.childTable}\" does not match toTable \"${options.toTable}\"`,\n );\n }\n }\n\n const relationDef: RelationDefinition = {\n to: options.toModel,\n cardinality: options.cardinality,\n on: {\n parentCols: options.on.parentColumns,\n childCols: options.on.childColumns,\n },\n ...(options.through\n ? {\n through: {\n table: options.through.table,\n parentCols: options.through.parentColumns,\n childCols: options.through.childColumns,\n },\n }\n : undefined),\n };\n\n return new ModelBuilder(this._name, this._table, this._fields, {\n ...this._relations,\n [name]: relationDef,\n } as Relations & Record<RelationName, RelationDefinition>);\n }\n\n build(): ModelBuilderState<Name, Table, Fields, Relations> {\n return {\n name: this._name,\n table: this._table,\n fields: this._fields,\n relations: this._relations,\n };\n }\n}\n","import type {\n ColumnBuilderState,\n ColumnTypeDescriptor,\n ForeignKeyDef,\n IndexDef,\n TableBuilderState,\n UniqueConstraintDef,\n} from './builder-state';\n\ninterface TableBuilderInternalState<\n Name extends string,\n Columns extends Record<string, ColumnBuilderState<string, boolean, string>>,\n PrimaryKey extends readonly string[] | undefined,\n> {\n readonly name: Name;\n readonly columns: Columns;\n readonly primaryKey: PrimaryKey;\n readonly primaryKeyName: string | undefined;\n readonly uniques: readonly UniqueConstraintDef[];\n readonly indexes: readonly IndexDef[];\n readonly foreignKeys: readonly ForeignKeyDef[];\n}\n\ntype TableBuilderOptions<\n Name extends string,\n Columns extends Record<string, ColumnBuilderState<string, boolean, string>>,\n PrimaryKey extends readonly string[] | undefined,\n> = Partial<Omit<TableBuilderInternalState<Name, Columns, PrimaryKey>, 'name'>> & { name: Name };\n\nexport class TableBuilder<\n Name extends string,\n Columns extends Record<string, ColumnBuilderState<string, boolean, string>> = Record<\n never,\n ColumnBuilderState<string, boolean, string>\n >,\n PrimaryKey extends readonly string[] | undefined = undefined,\n> {\n private readonly _state: TableBuilderInternalState<Name, Columns, PrimaryKey>;\n\n constructor(options: TableBuilderOptions<Name, Columns, PrimaryKey>) {\n this._state = {\n name: options.name,\n columns: (options.columns ?? {}) as Columns,\n primaryKey: options.primaryKey as PrimaryKey,\n primaryKeyName: options.primaryKeyName,\n uniques: options.uniques ?? [],\n indexes: options.indexes ?? [],\n foreignKeys: options.foreignKeys ?? [],\n };\n }\n\n private get _name(): Name {\n return this._state.name;\n }\n\n private get _columns(): Columns {\n return this._state.columns;\n }\n\n private get _primaryKey(): PrimaryKey {\n return this._state.primaryKey;\n }\n\n column<\n ColName extends string,\n Descriptor extends ColumnTypeDescriptor,\n Nullable extends boolean | undefined = undefined,\n >(\n name: ColName,\n options: {\n type: Descriptor;\n nullable?: Nullable;\n },\n ): TableBuilder<\n Name,\n Columns &\n Record<\n ColName,\n ColumnBuilderState<ColName, Nullable extends true ? true : false, Descriptor['codecId']>\n >,\n PrimaryKey\n > {\n const nullable = (options.nullable ?? false) as Nullable extends true ? true : false;\n const { codecId, nativeType } = options.type;\n\n const columnState = {\n name,\n nullable,\n type: codecId,\n nativeType,\n } as ColumnBuilderState<ColName, Nullable extends true ? true : false, Descriptor['codecId']>;\n return new TableBuilder({\n ...this._state,\n columns: { ...this._columns, [name]: columnState } as Columns &\n Record<\n ColName,\n ColumnBuilderState<ColName, Nullable extends true ? true : false, Descriptor['codecId']>\n >,\n });\n }\n\n primaryKey<PK extends readonly string[]>(\n columns: PK,\n name?: string,\n ): TableBuilder<Name, Columns, PK> {\n return new TableBuilder({\n ...this._state,\n primaryKey: columns,\n primaryKeyName: name,\n });\n }\n\n unique(columns: readonly string[], name?: string): TableBuilder<Name, Columns, PrimaryKey> {\n const constraint: UniqueConstraintDef = name ? { columns, name } : { columns };\n return new TableBuilder({\n ...this._state,\n uniques: [...this._state.uniques, constraint],\n });\n }\n\n index(columns: readonly string[], name?: string): TableBuilder<Name, Columns, PrimaryKey> {\n const indexDef: IndexDef = name ? { columns, name } : { columns };\n return new TableBuilder({\n ...this._state,\n indexes: [...this._state.indexes, indexDef],\n });\n }\n\n foreignKey(\n columns: readonly string[],\n references: { table: string; columns: readonly string[] },\n name?: string,\n ): TableBuilder<Name, Columns, PrimaryKey> {\n const fkDef: ForeignKeyDef = name ? { columns, references, name } : { columns, references };\n return new TableBuilder({\n ...this._state,\n foreignKeys: [...this._state.foreignKeys, fkDef],\n });\n }\n\n build(): TableBuilderState<Name, Columns, PrimaryKey> {\n return {\n name: this._name,\n columns: this._columns,\n ...(this._primaryKey !== undefined ? { primaryKey: this._primaryKey } : {}),\n ...(this._state.primaryKeyName !== undefined\n ? { primaryKeyName: this._state.primaryKeyName }\n : {}),\n uniques: this._state.uniques,\n indexes: this._state.indexes,\n foreignKeys: this._state.foreignKeys,\n } as TableBuilderState<Name, Columns, PrimaryKey>;\n }\n}\n","import type { TargetPackRef } from '@prisma-next/contract/framework-components';\nimport type {\n ColumnBuilderState,\n ContractBuilderState,\n ModelBuilderState,\n RelationDefinition,\n TableBuilderState,\n} from './builder-state';\nimport { ModelBuilder } from './model-builder';\nimport { TableBuilder } from './table-builder';\n\nexport class ContractBuilder<\n Target extends string | undefined = undefined,\n Tables extends Record<\n string,\n TableBuilderState<\n string,\n Record<string, ColumnBuilderState<string, boolean, string>>,\n readonly string[] | undefined\n >\n > = Record<never, never>,\n Models extends Record<\n string,\n ModelBuilderState<string, string, Record<string, string>, Record<string, RelationDefinition>>\n > = Record<never, never>,\n CoreHash extends string | undefined = undefined,\n ExtensionPacks extends Record<string, unknown> | undefined = undefined,\n Capabilities extends Record<string, Record<string, boolean>> | undefined = undefined,\n> {\n protected readonly state: ContractBuilderState<\n Target,\n Tables,\n Models,\n CoreHash,\n ExtensionPacks,\n Capabilities\n >;\n\n constructor(\n state?: ContractBuilderState<Target, Tables, Models, CoreHash, ExtensionPacks, Capabilities>,\n ) {\n this.state =\n state ??\n ({\n tables: {},\n models: {},\n } as ContractBuilderState<Target, Tables, Models, CoreHash, ExtensionPacks, Capabilities>);\n }\n\n target<T extends string>(\n packRef: TargetPackRef<string, T>,\n ): ContractBuilder<T, Tables, Models, CoreHash, ExtensionPacks, Capabilities> {\n return new ContractBuilder<T, Tables, Models, CoreHash, ExtensionPacks, Capabilities>({\n ...this.state,\n target: packRef.targetId,\n });\n }\n\n capabilities<C extends Record<string, Record<string, boolean>>>(\n capabilities: C,\n ): ContractBuilder<Target, Tables, Models, CoreHash, ExtensionPacks, C> {\n return new ContractBuilder<Target, Tables, Models, CoreHash, ExtensionPacks, C>({\n ...this.state,\n capabilities,\n });\n }\n\n table<\n TableName extends string,\n T extends TableBuilder<\n TableName,\n Record<string, ColumnBuilderState<string, boolean, string>>,\n readonly string[] | undefined\n >,\n >(\n name: TableName,\n callback: (t: TableBuilder<TableName>) => T | undefined,\n ): ContractBuilder<\n Target,\n Tables & Record<TableName, ReturnType<T['build']>>,\n Models,\n CoreHash,\n ExtensionPacks,\n Capabilities\n > {\n const tableBuilder = new TableBuilder<TableName>({ name });\n const result = callback(tableBuilder);\n const finalBuilder = result instanceof TableBuilder ? result : tableBuilder;\n const tableState = finalBuilder.build();\n\n return new ContractBuilder<\n Target,\n Tables & Record<TableName, ReturnType<T['build']>>,\n Models,\n CoreHash,\n ExtensionPacks,\n Capabilities\n >({\n ...this.state,\n tables: { ...this.state.tables, [name]: tableState } as Tables &\n Record<TableName, ReturnType<T['build']>>,\n });\n }\n\n model<\n ModelName extends string,\n TableName extends string,\n M extends ModelBuilder<\n ModelName,\n TableName,\n Record<string, string>,\n Record<string, RelationDefinition>\n >,\n >(\n name: ModelName,\n table: TableName,\n callback: (\n m: ModelBuilder<ModelName, TableName, Record<string, string>, Record<never, never>>,\n ) => M | undefined,\n ): ContractBuilder<\n Target,\n Tables,\n Models & Record<ModelName, ReturnType<M['build']>>,\n CoreHash,\n ExtensionPacks,\n Capabilities\n > {\n const modelBuilder = new ModelBuilder<ModelName, TableName>(name, table);\n const result = callback(modelBuilder);\n const finalBuilder = result instanceof ModelBuilder ? result : modelBuilder;\n const modelState = finalBuilder.build();\n\n return new ContractBuilder<\n Target,\n Tables,\n Models & Record<ModelName, ReturnType<M['build']>>,\n CoreHash,\n ExtensionPacks,\n Capabilities\n >({\n ...this.state,\n models: { ...this.state.models, [name]: modelState } as Models &\n Record<ModelName, ReturnType<M['build']>>,\n });\n }\n\n coreHash<H extends string>(\n hash: H,\n ): ContractBuilder<Target, Tables, Models, H, ExtensionPacks, Capabilities> {\n return new ContractBuilder<Target, Tables, Models, H, ExtensionPacks, Capabilities>({\n ...this.state,\n coreHash: hash,\n });\n }\n}\n\nexport function defineContract(): ContractBuilder {\n return new ContractBuilder();\n}\n"],"mappings":";AAEO,IAAM,eAAN,MAAM,cAKX;AAAA,EACiB;AAAA,EACA;AAAA,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AO,IAAI,gBAAgB;AAC7B;","names":[]}
1
+ {"version":3,"sources":["../src/model-builder.ts","../src/table-builder.ts","../src/contract-builder.ts"],"sourcesContent":["import type { ModelBuilderState, RelationDefinition } from './builder-state';\n\nexport class ModelBuilder<\n Name extends string,\n Table extends string,\n Fields extends Record<string, string> = Record<never, never>,\n Relations extends Record<string, RelationDefinition> = Record<never, never>,\n> {\n private readonly _name: Name;\n private readonly _table: Table;\n private readonly _fields: Fields;\n private readonly _relations: Relations;\n\n constructor(\n name: Name,\n table: Table,\n fields: Fields = {} as Fields,\n relations: Relations = {} as Relations,\n ) {\n this._name = name;\n this._table = table;\n this._fields = fields;\n this._relations = relations;\n }\n\n field<FieldName extends string, ColumnName extends string>(\n fieldName: FieldName,\n columnName: ColumnName,\n ): ModelBuilder<Name, Table, Fields & Record<FieldName, ColumnName>, Relations> {\n return new ModelBuilder(\n this._name,\n this._table,\n {\n ...this._fields,\n [fieldName]: columnName,\n } as Fields & Record<FieldName, ColumnName>,\n this._relations,\n );\n }\n\n relation<RelationName extends string, ToModel extends string, ToTable extends string>(\n name: RelationName,\n options: {\n toModel: ToModel;\n toTable: ToTable;\n cardinality: '1:1' | '1:N' | 'N:1';\n on: {\n parentTable: Table;\n parentColumns: readonly string[];\n childTable: ToTable;\n childColumns: readonly string[];\n };\n },\n ): ModelBuilder<Name, Table, Fields, Relations & Record<RelationName, RelationDefinition>>;\n relation<\n RelationName extends string,\n ToModel extends string,\n ToTable extends string,\n JunctionTable extends string,\n >(\n name: RelationName,\n options: {\n toModel: ToModel;\n toTable: ToTable;\n cardinality: 'N:M';\n through: {\n table: JunctionTable;\n parentColumns: readonly string[];\n childColumns: readonly string[];\n };\n on: {\n parentTable: Table;\n parentColumns: readonly string[];\n childTable: JunctionTable;\n childColumns: readonly string[];\n };\n },\n ): ModelBuilder<Name, Table, Fields, Relations & Record<RelationName, RelationDefinition>>;\n relation<\n RelationName extends string,\n ToModel extends string,\n ToTable extends string,\n JunctionTable extends string = never,\n >(\n name: RelationName,\n options: {\n toModel: ToModel;\n toTable: ToTable;\n cardinality: '1:1' | '1:N' | 'N:1' | 'N:M';\n through?: {\n table: JunctionTable;\n parentColumns: readonly string[];\n childColumns: readonly string[];\n };\n on: {\n parentTable: Table;\n parentColumns: readonly string[];\n childTable: ToTable | JunctionTable;\n childColumns: readonly string[];\n };\n },\n ): ModelBuilder<Name, Table, Fields, Relations & Record<RelationName, RelationDefinition>> {\n // Validate parentTable matches model's table\n if (options.on.parentTable !== this._table) {\n throw new Error(\n `Relation \"${name}\" parentTable \"${options.on.parentTable}\" does not match model table \"${this._table}\"`,\n );\n }\n\n // Validate childTable matches toTable (for non-N:M) or through.table (for N:M)\n if (options.cardinality === 'N:M') {\n if (!options.through) {\n throw new Error(`Relation \"${name}\" with cardinality \"N:M\" requires through field`);\n }\n if (options.on.childTable !== options.through.table) {\n throw new Error(\n `Relation \"${name}\" childTable \"${options.on.childTable}\" does not match through.table \"${options.through.table}\"`,\n );\n }\n } else {\n if (options.on.childTable !== options.toTable) {\n throw new Error(\n `Relation \"${name}\" childTable \"${options.on.childTable}\" does not match toTable \"${options.toTable}\"`,\n );\n }\n }\n\n const relationDef: RelationDefinition = {\n to: options.toModel,\n cardinality: options.cardinality,\n on: {\n parentCols: options.on.parentColumns,\n childCols: options.on.childColumns,\n },\n ...(options.through\n ? {\n through: {\n table: options.through.table,\n parentCols: options.through.parentColumns,\n childCols: options.through.childColumns,\n },\n }\n : undefined),\n };\n\n return new ModelBuilder(this._name, this._table, this._fields, {\n ...this._relations,\n [name]: relationDef,\n } as Relations & Record<RelationName, RelationDefinition>);\n }\n\n build(): ModelBuilderState<Name, Table, Fields, Relations> {\n return {\n name: this._name,\n table: this._table,\n fields: this._fields,\n relations: this._relations,\n };\n }\n}\n","import type {\n ColumnBuilderState,\n ColumnTypeDescriptor,\n ForeignKeyDef,\n IndexDef,\n TableBuilderState,\n UniqueConstraintDef,\n} from './builder-state';\n\ninterface TableBuilderInternalState<\n Name extends string,\n Columns extends Record<string, ColumnBuilderState<string, boolean, string>>,\n PrimaryKey extends readonly string[] | undefined,\n> {\n readonly name: Name;\n readonly columns: Columns;\n readonly primaryKey: PrimaryKey;\n readonly primaryKeyName: string | undefined;\n readonly uniques: readonly UniqueConstraintDef[];\n readonly indexes: readonly IndexDef[];\n readonly foreignKeys: readonly ForeignKeyDef[];\n}\n\n/**\n * Creates a new table builder with the given name.\n * This is the preferred way to create a TableBuilder - it ensures\n * type parameters are inferred correctly without unsafe casts.\n */\nexport function createTable<Name extends string>(name: Name): TableBuilder<Name> {\n return new TableBuilder(name, {}, undefined, undefined, [], [], []);\n}\n\n/**\n * Builder for defining table structure with type-safe chaining.\n * Use `createTable(name)` to create instances.\n */\nexport class TableBuilder<\n Name extends string,\n Columns extends Record<string, ColumnBuilderState<string, boolean, string>> = Record<\n never,\n ColumnBuilderState<string, boolean, string>\n >,\n PrimaryKey extends readonly string[] | undefined = undefined,\n> {\n private readonly _state: TableBuilderInternalState<Name, Columns, PrimaryKey>;\n\n /** @internal Use createTable() instead */\n constructor(\n name: Name,\n columns: Columns,\n primaryKey: PrimaryKey,\n primaryKeyName: string | undefined,\n uniques: readonly UniqueConstraintDef[],\n indexes: readonly IndexDef[],\n foreignKeys: readonly ForeignKeyDef[],\n ) {\n this._state = {\n name,\n columns,\n primaryKey,\n primaryKeyName,\n uniques,\n indexes,\n foreignKeys,\n };\n }\n\n private get _name(): Name {\n return this._state.name;\n }\n\n private get _columns(): Columns {\n return this._state.columns;\n }\n\n private get _primaryKey(): PrimaryKey {\n return this._state.primaryKey;\n }\n\n column<\n ColName extends string,\n Descriptor extends ColumnTypeDescriptor,\n Nullable extends boolean | undefined = undefined,\n >(\n name: ColName,\n options: {\n type: Descriptor;\n nullable?: Nullable;\n },\n ): TableBuilder<\n Name,\n Columns &\n Record<\n ColName,\n ColumnBuilderState<ColName, Nullable extends true ? true : false, Descriptor['codecId']>\n >,\n PrimaryKey\n > {\n const nullable = (options.nullable ?? false) as Nullable extends true ? true : false;\n const { codecId, nativeType } = options.type;\n\n const columnState = {\n name,\n nullable,\n type: codecId,\n nativeType,\n } as ColumnBuilderState<ColName, Nullable extends true ? true : false, Descriptor['codecId']>;\n const newColumns = { ...this._columns, [name]: columnState } as Columns &\n Record<\n ColName,\n ColumnBuilderState<ColName, Nullable extends true ? true : false, Descriptor['codecId']>\n >;\n return new TableBuilder(\n this._state.name,\n newColumns,\n this._state.primaryKey,\n this._state.primaryKeyName,\n this._state.uniques,\n this._state.indexes,\n this._state.foreignKeys,\n );\n }\n\n primaryKey<PK extends readonly string[]>(\n columns: PK,\n name?: string,\n ): TableBuilder<Name, Columns, PK> {\n return new TableBuilder(\n this._state.name,\n this._state.columns,\n columns,\n name,\n this._state.uniques,\n this._state.indexes,\n this._state.foreignKeys,\n );\n }\n\n unique(columns: readonly string[], name?: string): TableBuilder<Name, Columns, PrimaryKey> {\n const constraint: UniqueConstraintDef = name ? { columns, name } : { columns };\n return new TableBuilder(\n this._state.name,\n this._state.columns,\n this._state.primaryKey,\n this._state.primaryKeyName,\n [...this._state.uniques, constraint],\n this._state.indexes,\n this._state.foreignKeys,\n );\n }\n\n index(columns: readonly string[], name?: string): TableBuilder<Name, Columns, PrimaryKey> {\n const indexDef: IndexDef = name ? { columns, name } : { columns };\n return new TableBuilder(\n this._state.name,\n this._state.columns,\n this._state.primaryKey,\n this._state.primaryKeyName,\n this._state.uniques,\n [...this._state.indexes, indexDef],\n this._state.foreignKeys,\n );\n }\n\n foreignKey(\n columns: readonly string[],\n references: { table: string; columns: readonly string[] },\n name?: string,\n ): TableBuilder<Name, Columns, PrimaryKey> {\n const fkDef: ForeignKeyDef = name ? { columns, references, name } : { columns, references };\n return new TableBuilder(\n this._state.name,\n this._state.columns,\n this._state.primaryKey,\n this._state.primaryKeyName,\n this._state.uniques,\n this._state.indexes,\n [...this._state.foreignKeys, fkDef],\n );\n }\n\n build(): TableBuilderState<Name, Columns, PrimaryKey> {\n return {\n name: this._name,\n columns: this._columns,\n ...(this._primaryKey !== undefined ? { primaryKey: this._primaryKey } : {}),\n ...(this._state.primaryKeyName !== undefined\n ? { primaryKeyName: this._state.primaryKeyName }\n : {}),\n uniques: this._state.uniques,\n indexes: this._state.indexes,\n foreignKeys: this._state.foreignKeys,\n } as TableBuilderState<Name, Columns, PrimaryKey>;\n }\n}\n","import type { TargetPackRef } from '@prisma-next/contract/framework-components';\nimport type {\n ColumnBuilderState,\n ContractBuilderState,\n ModelBuilderState,\n RelationDefinition,\n TableBuilderState,\n} from './builder-state';\nimport { ModelBuilder } from './model-builder';\nimport { createTable, TableBuilder } from './table-builder';\n\nexport class ContractBuilder<\n Target extends string | undefined = undefined,\n Tables extends Record<\n string,\n TableBuilderState<\n string,\n Record<string, ColumnBuilderState<string, boolean, string>>,\n readonly string[] | undefined\n >\n > = Record<never, never>,\n Models extends Record<\n string,\n ModelBuilderState<string, string, Record<string, string>, Record<string, RelationDefinition>>\n > = Record<never, never>,\n CoreHash extends string | undefined = undefined,\n ExtensionPacks extends Record<string, unknown> | undefined = undefined,\n Capabilities extends Record<string, Record<string, boolean>> | undefined = undefined,\n> {\n protected readonly state: ContractBuilderState<\n Target,\n Tables,\n Models,\n CoreHash,\n ExtensionPacks,\n Capabilities\n >;\n\n constructor(\n state?: ContractBuilderState<Target, Tables, Models, CoreHash, ExtensionPacks, Capabilities>,\n ) {\n this.state =\n state ??\n ({\n tables: {},\n models: {},\n } as ContractBuilderState<Target, Tables, Models, CoreHash, ExtensionPacks, Capabilities>);\n }\n\n target<T extends string>(\n packRef: TargetPackRef<string, T>,\n ): ContractBuilder<T, Tables, Models, CoreHash, ExtensionPacks, Capabilities> {\n return new ContractBuilder<T, Tables, Models, CoreHash, ExtensionPacks, Capabilities>({\n ...this.state,\n target: packRef.targetId,\n });\n }\n\n capabilities<C extends Record<string, Record<string, boolean>>>(\n capabilities: C,\n ): ContractBuilder<Target, Tables, Models, CoreHash, ExtensionPacks, C> {\n return new ContractBuilder<Target, Tables, Models, CoreHash, ExtensionPacks, C>({\n ...this.state,\n capabilities,\n });\n }\n\n table<\n TableName extends string,\n T extends TableBuilder<\n TableName,\n Record<string, ColumnBuilderState<string, boolean, string>>,\n readonly string[] | undefined\n >,\n >(\n name: TableName,\n callback: (t: TableBuilder<TableName>) => T | undefined,\n ): ContractBuilder<\n Target,\n Tables & Record<TableName, ReturnType<T['build']>>,\n Models,\n CoreHash,\n ExtensionPacks,\n Capabilities\n > {\n const tableBuilder = createTable(name);\n const result = callback(tableBuilder);\n const finalBuilder = result instanceof TableBuilder ? result : tableBuilder;\n const tableState = finalBuilder.build();\n\n return new ContractBuilder<\n Target,\n Tables & Record<TableName, ReturnType<T['build']>>,\n Models,\n CoreHash,\n ExtensionPacks,\n Capabilities\n >({\n ...this.state,\n tables: { ...this.state.tables, [name]: tableState } as Tables &\n Record<TableName, ReturnType<T['build']>>,\n });\n }\n\n model<\n ModelName extends string,\n TableName extends string,\n M extends ModelBuilder<\n ModelName,\n TableName,\n Record<string, string>,\n Record<string, RelationDefinition>\n >,\n >(\n name: ModelName,\n table: TableName,\n callback: (\n m: ModelBuilder<ModelName, TableName, Record<string, string>, Record<never, never>>,\n ) => M | undefined,\n ): ContractBuilder<\n Target,\n Tables,\n Models & Record<ModelName, ReturnType<M['build']>>,\n CoreHash,\n ExtensionPacks,\n Capabilities\n > {\n const modelBuilder = new ModelBuilder<ModelName, TableName>(name, table);\n const result = callback(modelBuilder);\n const finalBuilder = result instanceof ModelBuilder ? result : modelBuilder;\n const modelState = finalBuilder.build();\n\n return new ContractBuilder<\n Target,\n Tables,\n Models & Record<ModelName, ReturnType<M['build']>>,\n CoreHash,\n ExtensionPacks,\n Capabilities\n >({\n ...this.state,\n models: { ...this.state.models, [name]: modelState } as Models &\n Record<ModelName, ReturnType<M['build']>>,\n });\n }\n\n coreHash<H extends string>(\n hash: H,\n ): ContractBuilder<Target, Tables, Models, H, ExtensionPacks, Capabilities> {\n return new ContractBuilder<Target, Tables, Models, H, ExtensionPacks, Capabilities>({\n ...this.state,\n coreHash: hash,\n });\n }\n}\n\nexport function defineContract(): ContractBuilder {\n return new ContractBuilder();\n}\n"],"mappings":";AAEO,IAAM,eAAN,MAAM,cAKX;AAAA,EACiB;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EAEjB,YACE,MACA,OACA,SAAiB,CAAC,GAClB,YAAuB,CAAC,GACxB;AACA,SAAK,QAAQ;AACb,SAAK,SAAS;AACd,SAAK,UAAU;AACf,SAAK,aAAa;AAAA,EACpB;AAAA,EAEA,MACE,WACA,YAC8E;AAC9E,WAAO,IAAI;AAAA,MACT,KAAK;AAAA,MACL,KAAK;AAAA,MACL;AAAA,QACE,GAAG,KAAK;AAAA,QACR,CAAC,SAAS,GAAG;AAAA,MACf;AAAA,MACA,KAAK;AAAA,IACP;AAAA,EACF;AAAA,EAwCA,SAME,MACA,SAgByF;AAEzF,QAAI,QAAQ,GAAG,gBAAgB,KAAK,QAAQ;AAC1C,YAAM,IAAI;AAAA,QACR,aAAa,IAAI,kBAAkB,QAAQ,GAAG,WAAW,iCAAiC,KAAK,MAAM;AAAA,MACvG;AAAA,IACF;AAGA,QAAI,QAAQ,gBAAgB,OAAO;AACjC,UAAI,CAAC,QAAQ,SAAS;AACpB,cAAM,IAAI,MAAM,aAAa,IAAI,iDAAiD;AAAA,MACpF;AACA,UAAI,QAAQ,GAAG,eAAe,QAAQ,QAAQ,OAAO;AACnD,cAAM,IAAI;AAAA,UACR,aAAa,IAAI,iBAAiB,QAAQ,GAAG,UAAU,mCAAmC,QAAQ,QAAQ,KAAK;AAAA,QACjH;AAAA,MACF;AAAA,IACF,OAAO;AACL,UAAI,QAAQ,GAAG,eAAe,QAAQ,SAAS;AAC7C,cAAM,IAAI;AAAA,UACR,aAAa,IAAI,iBAAiB,QAAQ,GAAG,UAAU,6BAA6B,QAAQ,OAAO;AAAA,QACrG;AAAA,MACF;AAAA,IACF;AAEA,UAAM,cAAkC;AAAA,MACtC,IAAI,QAAQ;AAAA,MACZ,a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AI,iBAOT;AAAA,MACA,GAAG,KAAK;AAAA,MACR,QAAQ,EAAE,GAAG,KAAK,MAAM,QAAQ,CAAC,IAAI,GAAG,WAAW;AAAA,IAErD,CAAC;AAAA,EACH;AAAA,EAEA,MAUE,MACA,OACA,UAUA;AACA,UAAM,eAAe,IAAI,aAAmC,MAAM,KAAK;AACvE,UAAM,SAAS,SAAS,YAAY;AACpC,UAAM,eAAe,kBAAkB,eAAe,SAAS;AAC/D,UAAM,aAAa,aAAa,MAAM;AAEtC,WAAO,IAAI,iBAOT;AAAA,MACA,GAAG,KAAK;AAAA,MACR,QAAQ,EAAE,GAAG,KAAK,MAAM,QAAQ,CAAC,IAAI,GAAG,WAAW;AAAA,IAErD,CAAC;AAAA,EACH;AAAA,EAEA,SACE,MAC0E;AAC1E,WAAO,IAAI,iBAAyE;AAAA,MAClF,GAAG,KAAK;AAAA,MACR,UAAU;AAAA,IACZ,CAAC;AAAA,EACH;AACF;AAEO,SAAS,iBAAkC;AAChD,SAAO,IAAI,gBAAgB;AAC7B;","names":[]}
@@ -1,19 +1,18 @@
1
1
  import type { ColumnBuilderState, ColumnTypeDescriptor, ForeignKeyDef, IndexDef, TableBuilderState, UniqueConstraintDef } from './builder-state';
2
- interface TableBuilderInternalState<Name extends string, Columns extends Record<string, ColumnBuilderState<string, boolean, string>>, PrimaryKey extends readonly string[] | undefined> {
3
- readonly name: Name;
4
- readonly columns: Columns;
5
- readonly primaryKey: PrimaryKey;
6
- readonly primaryKeyName: string | undefined;
7
- readonly uniques: readonly UniqueConstraintDef[];
8
- readonly indexes: readonly IndexDef[];
9
- readonly foreignKeys: readonly ForeignKeyDef[];
10
- }
11
- type TableBuilderOptions<Name extends string, Columns extends Record<string, ColumnBuilderState<string, boolean, string>>, PrimaryKey extends readonly string[] | undefined> = Partial<Omit<TableBuilderInternalState<Name, Columns, PrimaryKey>, 'name'>> & {
12
- name: Name;
13
- };
2
+ /**
3
+ * Creates a new table builder with the given name.
4
+ * This is the preferred way to create a TableBuilder - it ensures
5
+ * type parameters are inferred correctly without unsafe casts.
6
+ */
7
+ export declare function createTable<Name extends string>(name: Name): TableBuilder<Name>;
8
+ /**
9
+ * Builder for defining table structure with type-safe chaining.
10
+ * Use `createTable(name)` to create instances.
11
+ */
14
12
  export declare class TableBuilder<Name extends string, Columns extends Record<string, ColumnBuilderState<string, boolean, string>> = Record<never, ColumnBuilderState<string, boolean, string>>, PrimaryKey extends readonly string[] | undefined = undefined> {
15
13
  private readonly _state;
16
- constructor(options: TableBuilderOptions<Name, Columns, PrimaryKey>);
14
+ /** @internal Use createTable() instead */
15
+ constructor(name: Name, columns: Columns, primaryKey: PrimaryKey, primaryKeyName: string | undefined, uniques: readonly UniqueConstraintDef[], indexes: readonly IndexDef[], foreignKeys: readonly ForeignKeyDef[]);
17
16
  private get _name();
18
17
  private get _columns();
19
18
  private get _primaryKey();
@@ -30,5 +29,4 @@ export declare class TableBuilder<Name extends string, Columns extends Record<st
30
29
  }, name?: string): TableBuilder<Name, Columns, PrimaryKey>;
31
30
  build(): TableBuilderState<Name, Columns, PrimaryKey>;
32
31
  }
33
- export {};
34
32
  //# sourceMappingURL=table-builder.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"table-builder.d.ts","sourceRoot":"","sources":["../src/table-builder.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,kBAAkB,EAClB,oBAAoB,EACpB,aAAa,EACb,QAAQ,EACR,iBAAiB,EACjB,mBAAmB,EACpB,MAAM,iBAAiB,CAAC;AAEzB,UAAU,yBAAyB,CACjC,IAAI,SAAS,MAAM,EACnB,OAAO,SAAS,MAAM,CAAC,MAAM,EAAE,kBAAkB,CAAC,MAAM,EAAE,OAAO,EAAE,MAAM,CAAC,CAAC,EAC3E,UAAU,SAAS,SAAS,MAAM,EAAE,GAAG,SAAS;IAEhD,QAAQ,CAAC,IAAI,EAAE,IAAI,CAAC;IACpB,QAAQ,CAAC,OAAO,EAAE,OAAO,CAAC;IAC1B,QAAQ,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AAS,MAAM,EAAE,EACrC,OAAO,EAAE,EAAE,EACX,IAAI,CAAC,EAAE,MAAM,GACZ,YAAY,CAAC,IAAI,EAAE,OAAO,EAAE,EAAE,CAAC;IAYlC,MAAM,CAAC,OAAO,EAAE,SAAS,MAAM,EAAE,EAAE,IAAI,CAAC,EAAE,MAAM,GAAG,YAAY,CAAC,IAAI,EAAE,OAAO,EAAE,UAAU,CAAC;IAa1F,KAAK,CAAC,OAAO,EAAE,SAAS,MAAM,EAAE,EAAE,IAAI,CAAC,EAAE,MAAM,GAAG,YAAY,CAAC,IAAI,EAAE,OAAO,EAAE,UAAU,CAAC;IAazF,UAAU,CACR,OAAO,EAAE,SAAS,MAAM,EAAE,EAC1B,UAAU,EAAE;QAAE,KAAK,EAAE,MAAM,CAAC;QAAC,OAAO,EAAE,SAAS,MAAM,EAAE,CAAA;KAAE,EACzD,IAAI,CAAC,EAAE,MAAM,GACZ,YAAY,CAAC,IAAI,EAAE,OAAO,EAAE,UAAU,CAAC;IAa1C,KAAK,IAAI,iBAAiB,CAAC,IAAI,EAAE,OAAO,EAAE,UAAU,CAAC;CAatD"}
package/package.json CHANGED
@@ -1,12 +1,12 @@
1
1
  {
2
2
  "name": "@prisma-next/contract-authoring",
3
- "version": "0.3.0-pr.88.6",
3
+ "version": "0.3.0-pr.89.1",
4
4
  "type": "module",
5
5
  "sideEffects": false,
6
6
  "description": "Target-agnostic contract authoring builder core for Prisma Next",
7
7
  "dependencies": {
8
8
  "ts-toolbelt": "^9.6.0",
9
- "@prisma-next/contract": "0.3.0-pr.88.6"
9
+ "@prisma-next/contract": "0.3.0-pr.89.1"
10
10
  },
11
11
  "devDependencies": {
12
12
  "@vitest/coverage-v8": "4.0.16",
@@ -7,7 +7,7 @@ import type {
7
7
  TableBuilderState,
8
8
  } from './builder-state';
9
9
  import { ModelBuilder } from './model-builder';
10
- import { TableBuilder } from './table-builder';
10
+ import { createTable, TableBuilder } from './table-builder';
11
11
 
12
12
  export class ContractBuilder<
13
13
  Target extends string | undefined = undefined,
@@ -83,7 +83,7 @@ export class ContractBuilder<
83
83
  ExtensionPacks,
84
84
  Capabilities
85
85
  > {
86
- const tableBuilder = new TableBuilder<TableName>({ name });
86
+ const tableBuilder = createTable(name);
87
87
  const result = callback(tableBuilder);
88
88
  const finalBuilder = result instanceof TableBuilder ? result : tableBuilder;
89
89
  const tableState = finalBuilder.build();
package/src/index.ts CHANGED
@@ -13,7 +13,7 @@ export type {
13
13
 
14
14
  export { ContractBuilder, defineContract } from './contract-builder';
15
15
  export { ModelBuilder } from './model-builder';
16
- export { TableBuilder } from './table-builder';
16
+ export { createTable, TableBuilder } from './table-builder';
17
17
 
18
18
  export type {
19
19
  BuildModelFields,
@@ -21,12 +21,19 @@ interface TableBuilderInternalState<
21
21
  readonly foreignKeys: readonly ForeignKeyDef[];
22
22
  }
23
23
 
24
- type TableBuilderOptions<
25
- Name extends string,
26
- Columns extends Record<string, ColumnBuilderState<string, boolean, string>>,
27
- PrimaryKey extends readonly string[] | undefined,
28
- > = Partial<Omit<TableBuilderInternalState<Name, Columns, PrimaryKey>, 'name'>> & { name: Name };
24
+ /**
25
+ * Creates a new table builder with the given name.
26
+ * This is the preferred way to create a TableBuilder - it ensures
27
+ * type parameters are inferred correctly without unsafe casts.
28
+ */
29
+ export function createTable<Name extends string>(name: Name): TableBuilder<Name> {
30
+ return new TableBuilder(name, {}, undefined, undefined, [], [], []);
31
+ }
29
32
 
33
+ /**
34
+ * Builder for defining table structure with type-safe chaining.
35
+ * Use `createTable(name)` to create instances.
36
+ */
30
37
  export class TableBuilder<
31
38
  Name extends string,
32
39
  Columns extends Record<string, ColumnBuilderState<string, boolean, string>> = Record<
@@ -37,15 +44,24 @@ export class TableBuilder<
37
44
  > {
38
45
  private readonly _state: TableBuilderInternalState<Name, Columns, PrimaryKey>;
39
46
 
40
- constructor(options: TableBuilderOptions<Name, Columns, PrimaryKey>) {
47
+ /** @internal Use createTable() instead */
48
+ constructor(
49
+ name: Name,
50
+ columns: Columns,
51
+ primaryKey: PrimaryKey,
52
+ primaryKeyName: string | undefined,
53
+ uniques: readonly UniqueConstraintDef[],
54
+ indexes: readonly IndexDef[],
55
+ foreignKeys: readonly ForeignKeyDef[],
56
+ ) {
41
57
  this._state = {
42
- name: options.name,
43
- columns: (options.columns ?? {}) as Columns,
44
- primaryKey: options.primaryKey as PrimaryKey,
45
- primaryKeyName: options.primaryKeyName,
46
- uniques: options.uniques ?? [],
47
- indexes: options.indexes ?? [],
48
- foreignKeys: options.foreignKeys ?? [],
58
+ name,
59
+ columns,
60
+ primaryKey,
61
+ primaryKeyName,
62
+ uniques,
63
+ indexes,
64
+ foreignKeys,
49
65
  };
50
66
  }
51
67
 
@@ -89,41 +105,61 @@ export class TableBuilder<
89
105
  type: codecId,
90
106
  nativeType,
91
107
  } as ColumnBuilderState<ColName, Nullable extends true ? true : false, Descriptor['codecId']>;
92
- return new TableBuilder({
93
- ...this._state,
94
- columns: { ...this._columns, [name]: columnState } as Columns &
95
- Record<
96
- ColName,
97
- ColumnBuilderState<ColName, Nullable extends true ? true : false, Descriptor['codecId']>
98
- >,
99
- });
108
+ const newColumns = { ...this._columns, [name]: columnState } as Columns &
109
+ Record<
110
+ ColName,
111
+ ColumnBuilderState<ColName, Nullable extends true ? true : false, Descriptor['codecId']>
112
+ >;
113
+ return new TableBuilder(
114
+ this._state.name,
115
+ newColumns,
116
+ this._state.primaryKey,
117
+ this._state.primaryKeyName,
118
+ this._state.uniques,
119
+ this._state.indexes,
120
+ this._state.foreignKeys,
121
+ );
100
122
  }
101
123
 
102
124
  primaryKey<PK extends readonly string[]>(
103
125
  columns: PK,
104
126
  name?: string,
105
127
  ): TableBuilder<Name, Columns, PK> {
106
- return new TableBuilder({
107
- ...this._state,
108
- primaryKey: columns,
109
- primaryKeyName: name,
110
- });
128
+ return new TableBuilder(
129
+ this._state.name,
130
+ this._state.columns,
131
+ columns,
132
+ name,
133
+ this._state.uniques,
134
+ this._state.indexes,
135
+ this._state.foreignKeys,
136
+ );
111
137
  }
112
138
 
113
139
  unique(columns: readonly string[], name?: string): TableBuilder<Name, Columns, PrimaryKey> {
114
140
  const constraint: UniqueConstraintDef = name ? { columns, name } : { columns };
115
- return new TableBuilder({
116
- ...this._state,
117
- uniques: [...this._state.uniques, constraint],
118
- });
141
+ return new TableBuilder(
142
+ this._state.name,
143
+ this._state.columns,
144
+ this._state.primaryKey,
145
+ this._state.primaryKeyName,
146
+ [...this._state.uniques, constraint],
147
+ this._state.indexes,
148
+ this._state.foreignKeys,
149
+ );
119
150
  }
120
151
 
121
152
  index(columns: readonly string[], name?: string): TableBuilder<Name, Columns, PrimaryKey> {
122
153
  const indexDef: IndexDef = name ? { columns, name } : { columns };
123
- return new TableBuilder({
124
- ...this._state,
125
- indexes: [...this._state.indexes, indexDef],
126
- });
154
+ return new TableBuilder(
155
+ this._state.name,
156
+ this._state.columns,
157
+ this._state.primaryKey,
158
+ this._state.primaryKeyName,
159
+ this._state.uniques,
160
+ [...this._state.indexes, indexDef],
161
+ this._state.foreignKeys,
162
+ );
127
163
  }
128
164
 
129
165
  foreignKey(
@@ -132,10 +168,15 @@ export class TableBuilder<
132
168
  name?: string,
133
169
  ): TableBuilder<Name, Columns, PrimaryKey> {
134
170
  const fkDef: ForeignKeyDef = name ? { columns, references, name } : { columns, references };
135
- return new TableBuilder({
136
- ...this._state,
137
- foreignKeys: [...this._state.foreignKeys, fkDef],
138
- });
171
+ return new TableBuilder(
172
+ this._state.name,
173
+ this._state.columns,
174
+ this._state.primaryKey,
175
+ this._state.primaryKeyName,
176
+ this._state.uniques,
177
+ this._state.indexes,
178
+ [...this._state.foreignKeys, fkDef],
179
+ );
139
180
  }
140
181
 
141
182
  build(): TableBuilderState<Name, Columns, PrimaryKey> {