@prisma-next/contract-authoring 0.3.0-dev.7 → 0.3.0-dev.71

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,5 +1,82 @@
1
- import type { ColumnBuilderState, ColumnTypeDescriptor, TableBuilderState } from './builder-state';
1
+ import type { ColumnDefault, ExecutionMutationDefaultValue } from '@prisma-next/contract/types';
2
+ import { ifDefined } from '@prisma-next/utils/defined';
3
+ import type {
4
+ ColumnBuilderState,
5
+ ColumnTypeDescriptor,
6
+ ForeignKeyDef,
7
+ ForeignKeyOptions,
8
+ IndexDef,
9
+ TableBuilderState,
10
+ UniqueConstraintDef,
11
+ } from './builder-state';
2
12
 
13
+ /**
14
+ * Column options for nullable columns.
15
+ */
16
+ interface NullableColumnOptions<Descriptor extends ColumnTypeDescriptor> {
17
+ type: Descriptor;
18
+ nullable: true;
19
+ typeParams?: Record<string, unknown>;
20
+ default?: ColumnDefault;
21
+ }
22
+
23
+ /**
24
+ * Column options for non-nullable columns.
25
+ * Non-nullable columns can optionally have a default value.
26
+ */
27
+ interface NonNullableColumnOptions<Descriptor extends ColumnTypeDescriptor> {
28
+ type: Descriptor;
29
+ nullable?: false;
30
+ typeParams?: Record<string, unknown>;
31
+ default?: ColumnDefault;
32
+ }
33
+
34
+ type GeneratedColumnOptions<Descriptor extends ColumnTypeDescriptor> = Omit<
35
+ NonNullableColumnOptions<Descriptor>,
36
+ 'default' | 'nullable'
37
+ > & {
38
+ /**
39
+ * Generated columns are always non-nullable and use mutation-time defaults
40
+ * that the runtime injects when the column is omitted from insert input.
41
+ */
42
+ nullable?: false;
43
+ generated: ExecutionMutationDefaultValue;
44
+ };
45
+
46
+ /** Column options for any column nullability. */
47
+ type ColumnOptions<Descriptor extends ColumnTypeDescriptor> =
48
+ | NullableColumnOptions<Descriptor>
49
+ | NonNullableColumnOptions<Descriptor>;
50
+
51
+ type NullableFromOptions<TOptions> = TOptions extends { nullable: true } ? true : false;
52
+
53
+ interface TableBuilderInternalState<
54
+ Name extends string,
55
+ Columns extends Record<string, ColumnBuilderState<string, boolean, string>>,
56
+ PrimaryKey extends readonly string[] | undefined,
57
+ > {
58
+ readonly name: Name;
59
+ readonly columns: Columns;
60
+ readonly primaryKey: PrimaryKey;
61
+ readonly primaryKeyName: string | undefined;
62
+ readonly uniques: readonly UniqueConstraintDef[];
63
+ readonly indexes: readonly IndexDef[];
64
+ readonly foreignKeys: readonly ForeignKeyDef[];
65
+ }
66
+
67
+ /**
68
+ * Creates a new table builder with the given name.
69
+ * This is the preferred way to create a TableBuilder - it ensures
70
+ * type parameters are inferred correctly without unsafe casts.
71
+ */
72
+ export function createTable<Name extends string>(name: Name): TableBuilder<Name> {
73
+ return new TableBuilder(name, {}, undefined, undefined, [], [], []);
74
+ }
75
+
76
+ /**
77
+ * Builder for defining table structure with type-safe chaining.
78
+ * Use `createTable(name)` to create instances.
79
+ */
3
80
  export class TableBuilder<
4
81
  Name extends string,
5
82
  Columns extends Record<string, ColumnBuilderState<string, boolean, string>> = Record<
@@ -8,76 +85,202 @@ export class TableBuilder<
8
85
  >,
9
86
  PrimaryKey extends readonly string[] | undefined = undefined,
10
87
  > {
11
- private readonly _name: Name;
12
- private readonly _columns: Columns;
13
- private readonly _primaryKey: PrimaryKey;
14
-
15
- constructor(name: Name, columns: Columns = {} as Columns, primaryKey?: PrimaryKey) {
16
- this._name = name;
17
- this._columns = columns;
18
- this._primaryKey = primaryKey as PrimaryKey;
88
+ private readonly _state: TableBuilderInternalState<Name, Columns, PrimaryKey>;
89
+
90
+ /** @internal Use createTable() instead */
91
+ constructor(
92
+ name: Name,
93
+ columns: Columns,
94
+ primaryKey: PrimaryKey,
95
+ primaryKeyName: string | undefined,
96
+ uniques: readonly UniqueConstraintDef[],
97
+ indexes: readonly IndexDef[],
98
+ foreignKeys: readonly ForeignKeyDef[],
99
+ ) {
100
+ this._state = {
101
+ name,
102
+ columns,
103
+ primaryKey,
104
+ primaryKeyName,
105
+ uniques,
106
+ indexes,
107
+ foreignKeys,
108
+ };
109
+ }
110
+
111
+ private get _name(): Name {
112
+ return this._state.name;
113
+ }
114
+
115
+ private get _columns(): Columns {
116
+ return this._state.columns;
117
+ }
118
+
119
+ private get _primaryKey(): PrimaryKey {
120
+ return this._state.primaryKey;
121
+ }
122
+
123
+ /** Add a nullable column to the table. */
124
+ column<ColName extends string, Descriptor extends ColumnTypeDescriptor>(
125
+ name: ColName,
126
+ options: NullableColumnOptions<Descriptor>,
127
+ ): TableBuilder<
128
+ Name,
129
+ Columns & Record<ColName, ColumnBuilderState<ColName, true, Descriptor['codecId']>>,
130
+ PrimaryKey
131
+ >;
132
+
133
+ /**
134
+ * Add a non-nullable column to the table.
135
+ * Non-nullable columns can optionally have a default value.
136
+ */
137
+ column<ColName extends string, Descriptor extends ColumnTypeDescriptor>(
138
+ name: ColName,
139
+ options: NonNullableColumnOptions<Descriptor>,
140
+ ): TableBuilder<
141
+ Name,
142
+ Columns & Record<ColName, ColumnBuilderState<ColName, false, Descriptor['codecId']>>,
143
+ PrimaryKey
144
+ >;
145
+
146
+ /**
147
+ * Implementation of the column method.
148
+ */
149
+ column<ColName extends string, Descriptor extends ColumnTypeDescriptor>(
150
+ name: ColName,
151
+ options: ColumnOptions<Descriptor>,
152
+ ): TableBuilder<
153
+ Name,
154
+ Columns & Record<ColName, ColumnBuilderState<ColName, boolean, Descriptor['codecId']>>,
155
+ PrimaryKey
156
+ > {
157
+ return this.columnInternal(name, options);
158
+ }
159
+
160
+ generated<ColName extends string, Descriptor extends ColumnTypeDescriptor>(
161
+ name: ColName,
162
+ options: GeneratedColumnOptions<Descriptor>,
163
+ ): TableBuilder<
164
+ Name,
165
+ Columns & Record<ColName, ColumnBuilderState<ColName, false, Descriptor['codecId']>>,
166
+ PrimaryKey
167
+ > {
168
+ const { generated, ...columnOptions } = options;
169
+ return this.columnInternal(name, columnOptions, generated);
19
170
  }
20
171
 
21
- column<
172
+ private columnInternal<
22
173
  ColName extends string,
23
174
  Descriptor extends ColumnTypeDescriptor,
24
- Nullable extends boolean | undefined = undefined,
175
+ Options extends ColumnOptions<Descriptor>,
25
176
  >(
26
177
  name: ColName,
27
- options: {
28
- type: Descriptor;
29
- nullable?: Nullable;
30
- },
178
+ options: Options,
179
+ executionDefault?: ExecutionMutationDefaultValue,
31
180
  ): TableBuilder<
32
181
  Name,
33
182
  Columns &
34
183
  Record<
35
184
  ColName,
36
- ColumnBuilderState<ColName, Nullable extends true ? true : false, Descriptor['codecId']>
185
+ ColumnBuilderState<ColName, NullableFromOptions<Options>, Descriptor['codecId']>
37
186
  >,
38
187
  PrimaryKey
39
188
  > {
40
- const nullable = (options.nullable ?? false) as Nullable extends true ? true : false;
41
- const { codecId, nativeType } = options.type;
189
+ const nullable = options.nullable ?? false;
190
+ const { codecId, nativeType, typeParams: descriptorTypeParams, typeRef } = options.type;
191
+ const typeParams = options.typeParams ?? descriptorTypeParams;
42
192
 
43
193
  const columnState = {
44
194
  name,
45
195
  nullable,
46
196
  type: codecId,
47
197
  nativeType,
48
- } as ColumnBuilderState<ColName, Nullable extends true ? true : false, Descriptor['codecId']>;
198
+ ...ifDefined('typeParams', typeParams),
199
+ ...ifDefined('typeRef', typeRef),
200
+ ...ifDefined('default', 'default' in options ? options.default : undefined),
201
+ ...ifDefined('executionDefault', executionDefault),
202
+ } as ColumnBuilderState<ColName, NullableFromOptions<Options>, Descriptor['codecId']>;
203
+ const newColumns = { ...this._columns, [name]: columnState } as Columns &
204
+ Record<
205
+ ColName,
206
+ ColumnBuilderState<ColName, NullableFromOptions<Options>, Descriptor['codecId']>
207
+ >;
49
208
  return new TableBuilder(
50
- this._name,
51
- { ...this._columns, [name]: columnState } as Columns &
52
- Record<
53
- ColName,
54
- ColumnBuilderState<ColName, Nullable extends true ? true : false, Descriptor['codecId']>
55
- >,
56
- this._primaryKey,
209
+ this._state.name,
210
+ newColumns,
211
+ this._state.primaryKey,
212
+ this._state.primaryKeyName,
213
+ this._state.uniques,
214
+ this._state.indexes,
215
+ this._state.foreignKeys,
57
216
  );
58
217
  }
59
218
 
60
219
  primaryKey<PK extends readonly string[]>(
61
220
  columns: PK,
62
- _name?: string,
221
+ name?: string,
63
222
  ): TableBuilder<Name, Columns, PK> {
64
- return new TableBuilder(this._name, this._columns, columns);
223
+ return new TableBuilder(
224
+ this._state.name,
225
+ this._state.columns,
226
+ columns,
227
+ name,
228
+ this._state.uniques,
229
+ this._state.indexes,
230
+ this._state.foreignKeys,
231
+ );
65
232
  }
66
233
 
67
- unique(_columns: readonly string[], _name?: string): TableBuilder<Name, Columns, PrimaryKey> {
68
- return this;
234
+ unique(columns: readonly string[], name?: string): TableBuilder<Name, Columns, PrimaryKey> {
235
+ const constraint: UniqueConstraintDef = name ? { columns, name } : { columns };
236
+ return new TableBuilder(
237
+ this._state.name,
238
+ this._state.columns,
239
+ this._state.primaryKey,
240
+ this._state.primaryKeyName,
241
+ [...this._state.uniques, constraint],
242
+ this._state.indexes,
243
+ this._state.foreignKeys,
244
+ );
69
245
  }
70
246
 
71
- index(_columns: readonly string[], _name?: string): TableBuilder<Name, Columns, PrimaryKey> {
72
- return this;
247
+ index(columns: readonly string[], name?: string): TableBuilder<Name, Columns, PrimaryKey> {
248
+ const indexDef: IndexDef = name ? { columns, name } : { columns };
249
+ return new TableBuilder(
250
+ this._state.name,
251
+ this._state.columns,
252
+ this._state.primaryKey,
253
+ this._state.primaryKeyName,
254
+ this._state.uniques,
255
+ [...this._state.indexes, indexDef],
256
+ this._state.foreignKeys,
257
+ );
73
258
  }
74
259
 
75
260
  foreignKey(
76
- _columns: readonly string[],
77
- _references: { table: string; columns: readonly string[] },
78
- _name?: string,
261
+ columns: readonly string[],
262
+ references: { table: string; columns: readonly string[] },
263
+ opts?: string | (ForeignKeyOptions & { constraint?: boolean; index?: boolean }),
79
264
  ): TableBuilder<Name, Columns, PrimaryKey> {
80
- return this;
265
+ const resolved = typeof opts === 'string' ? { name: opts } : opts;
266
+ const fkDef: ForeignKeyDef = {
267
+ columns,
268
+ references,
269
+ ...ifDefined('name', resolved?.name),
270
+ ...ifDefined('onDelete', resolved?.onDelete),
271
+ ...ifDefined('onUpdate', resolved?.onUpdate),
272
+ ...ifDefined('constraint', resolved?.constraint),
273
+ ...ifDefined('index', resolved?.index),
274
+ };
275
+ return new TableBuilder(
276
+ this._state.name,
277
+ this._state.columns,
278
+ this._state.primaryKey,
279
+ this._state.primaryKeyName,
280
+ this._state.uniques,
281
+ this._state.indexes,
282
+ [...this._state.foreignKeys, fkDef],
283
+ );
81
284
  }
82
285
 
83
286
  build(): TableBuilderState<Name, Columns, PrimaryKey> {
@@ -85,6 +288,12 @@ export class TableBuilder<
85
288
  name: this._name,
86
289
  columns: this._columns,
87
290
  ...(this._primaryKey !== undefined ? { primaryKey: this._primaryKey } : {}),
291
+ ...(this._state.primaryKeyName !== undefined
292
+ ? { primaryKeyName: this._state.primaryKeyName }
293
+ : {}),
294
+ uniques: this._state.uniques,
295
+ indexes: this._state.indexes,
296
+ foreignKeys: this._state.foreignKeys,
88
297
  } as TableBuilderState<Name, Columns, PrimaryKey>;
89
298
  }
90
299
  }
package/src/types.ts CHANGED
@@ -1,3 +1,4 @@
1
+ import type { ColumnDefault } from '@prisma-next/contract/types';
1
2
  import type {
2
3
  ColumnBuilderState,
3
4
  ModelBuilderState,
@@ -9,6 +10,9 @@ export type BuildStorageColumn<Nullable extends boolean, Type extends string> =
9
10
  readonly nativeType: string;
10
11
  readonly codecId: Type;
11
12
  readonly nullable: Nullable;
13
+ readonly typeParams?: Record<string, unknown>;
14
+ readonly typeRef?: string;
15
+ readonly default?: ColumnDefault;
12
16
  };
13
17
 
14
18
  export type ExtractColumns<
@@ -25,13 +29,14 @@ export type ExtractPrimaryKey<
25
29
  Record<string, ColumnBuilderState<string, boolean, string>>,
26
30
  readonly string[] | undefined
27
31
  >,
28
- > = T extends TableBuilderState<
29
- string,
30
- Record<string, ColumnBuilderState<string, boolean, string>>,
31
- infer PK
32
- >
33
- ? PK
34
- : never;
32
+ > =
33
+ T extends TableBuilderState<
34
+ string,
35
+ Record<string, ColumnBuilderState<string, boolean, string>>,
36
+ infer PK
37
+ >
38
+ ? PK
39
+ : never;
35
40
 
36
41
  export type BuildStorage<
37
42
  Tables extends Record<
@@ -92,9 +97,10 @@ export type ExtractModelFields<
92
97
  Record<string, string>,
93
98
  Record<string, RelationDefinition>
94
99
  >,
95
- > = T extends ModelBuilderState<string, string, infer F, Record<string, RelationDefinition>>
96
- ? F
97
- : never;
100
+ > =
101
+ T extends ModelBuilderState<string, string, infer F, Record<string, RelationDefinition>>
102
+ ? F
103
+ : never;
98
104
 
99
105
  export type ExtractModelRelations<
100
106
  T extends ModelBuilderState<
@@ -1,60 +0,0 @@
1
- /**
2
- * Column type descriptor containing both codec ID and native type.
3
- * Used when defining columns with descriptor objects instead of string IDs.
4
- */
5
- export type ColumnTypeDescriptor = {
6
- readonly codecId: string;
7
- readonly nativeType: string;
8
- };
9
- export interface ColumnBuilderState<Name extends string, Nullable extends boolean, Type extends string> {
10
- readonly name: Name;
11
- readonly nullable: Nullable;
12
- readonly type: Type;
13
- readonly nativeType: string;
14
- }
15
- export interface TableBuilderState<Name extends string, Columns extends Record<string, ColumnBuilderState<string, boolean, string>>, PrimaryKey extends readonly string[] | undefined> {
16
- readonly name: Name;
17
- readonly columns: Columns;
18
- readonly primaryKey?: PrimaryKey;
19
- }
20
- export type RelationDefinition = {
21
- readonly to: string;
22
- readonly cardinality: '1:1' | '1:N' | 'N:1' | 'N:M';
23
- readonly on: {
24
- readonly parentCols: readonly string[];
25
- readonly childCols: readonly string[];
26
- };
27
- readonly through?: {
28
- readonly table: string;
29
- readonly parentCols: readonly string[];
30
- readonly childCols: readonly string[];
31
- };
32
- };
33
- export interface ModelBuilderState<Name extends string, Table extends string, Fields extends Record<string, string>, Relations extends Record<string, RelationDefinition>> {
34
- readonly name: Name;
35
- readonly table: Table;
36
- readonly fields: Fields;
37
- readonly relations: Relations;
38
- }
39
- export interface ContractBuilderState<Target extends string | undefined = string | undefined, Tables extends Record<string, TableBuilderState<string, Record<string, ColumnBuilderState<string, boolean, string>>, readonly string[] | undefined>> = Record<never, TableBuilderState<string, Record<string, ColumnBuilderState<string, boolean, string>>, readonly string[] | undefined>>, Models extends Record<string, ModelBuilderState<string, string, Record<string, string>, Record<string, RelationDefinition>>> = Record<never, ModelBuilderState<string, string, Record<string, string>, Record<string, RelationDefinition>>>, CoreHash extends string | undefined = string | undefined, ExtensionPacks extends Record<string, unknown> | undefined = undefined, Capabilities extends Record<string, Record<string, boolean>> | undefined = undefined> {
40
- readonly target?: Target;
41
- readonly tables: Tables;
42
- readonly models: Models;
43
- readonly coreHash?: CoreHash;
44
- readonly extensionPacks?: ExtensionPacks;
45
- readonly capabilities?: Capabilities;
46
- /**
47
- * Array of extension pack namespace identifiers (e.g., ['pgvector', 'postgis']).
48
- * Populated when extension packs are registered during contract building.
49
- * Used to track which extension packs are included in the contract.
50
- * Can be undefined or empty if no extension packs are registered.
51
- * Namespace format matches the extension pack ID (e.g., 'pgvector', not 'pgvector@1.0.0').
52
- */
53
- readonly extensionNamespaces?: readonly string[];
54
- }
55
- export interface ColumnBuilder<Name extends string, Nullable extends boolean, Type extends string> {
56
- nullable<Value extends boolean>(value?: Value): ColumnBuilder<Name, Value, Type>;
57
- type<Id extends string>(id: Id): ColumnBuilder<Name, Nullable, Id>;
58
- build(): ColumnBuilderState<Name, Nullable, Type>;
59
- }
60
- //# sourceMappingURL=builder-state.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"builder-state.d.ts","sourceRoot":"","sources":["../src/builder-state.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,MAAM,MAAM,oBAAoB,GAAG;IACjC,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;IACzB,QAAQ,CAAC,UAAU,EAAE,MAAM,CAAC;CAC7B,CAAC;AAEF,MAAM,WAAW,kBAAkB,CACjC,IAAI,SAAS,MAAM,EACnB,QAAQ,SAAS,OAAO,EACxB,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AG,SAAS;IAEpF,QAAQ,CAAC,MAAM,CAAC,EAAE,MAAM,CAAC;IACzB,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IACxB,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IACxB,QAAQ,CAAC,QAAQ,CAAC,EAAE,QAAQ,CAAC;IAC7B,QAAQ,CAAC,cAAc,CAAC,EAAE,cAAc,CAAC;IACzC,QAAQ,CAAC,YAAY,CAAC,EAAE,YAAY,CAAC;IACrC;;;;;;OAMG;IACH,QAAQ,CAAC,mBAAmB,CAAC,EAAE,SAAS,MAAM,EAAE,CAAC;CAClD;AAED,MAAM,WAAW,aAAa,CAAC,IAAI,SAAS,MAAM,EAAE,QAAQ,SAAS,OAAO,EAAE,IAAI,SAAS,MAAM;IAC/F,QAAQ,CAAC,KAAK,SAAS,OAAO,EAAE,KAAK,CAAC,EAAE,KAAK,GAAG,aAAa,CAAC,IAAI,EAAE,KAAK,EAAE,IAAI,CAAC,CAAC;IACjF,IAAI,CAAC,EAAE,SAAS,MAAM,EAAE,EAAE,EAAE,EAAE,GAAG,aAAa,CAAC,IAAI,EAAE,QAAQ,EAAE,EAAE,CAAC,CAAC;IACnE,KAAK,IAAI,kBAAkB,CAAC,IAAI,EAAE,QAAQ,EAAE,IAAI,CAAC,CAAC;CACnD"}
@@ -1,15 +0,0 @@
1
- import type { TargetPackRef } from '@prisma-next/contract/framework-components';
2
- import type { ColumnBuilderState, ContractBuilderState, ModelBuilderState, RelationDefinition, TableBuilderState } from './builder-state';
3
- import { ModelBuilder } from './model-builder';
4
- import { TableBuilder } from './table-builder';
5
- export declare class ContractBuilder<Target extends string | undefined = undefined, Tables extends Record<string, TableBuilderState<string, Record<string, ColumnBuilderState<string, boolean, string>>, readonly string[] | undefined>> = Record<never, never>, Models extends Record<string, ModelBuilderState<string, string, Record<string, string>, Record<string, RelationDefinition>>> = Record<never, never>, CoreHash extends string | undefined = undefined, ExtensionPacks extends Record<string, unknown> | undefined = undefined, Capabilities extends Record<string, Record<string, boolean>> | undefined = undefined> {
6
- protected readonly state: ContractBuilderState<Target, Tables, Models, CoreHash, ExtensionPacks, Capabilities>;
7
- constructor(state?: ContractBuilderState<Target, Tables, Models, CoreHash, ExtensionPacks, Capabilities>);
8
- target<T extends string>(packRef: TargetPackRef<string, T>): ContractBuilder<T, Tables, Models, CoreHash, ExtensionPacks, Capabilities>;
9
- capabilities<C extends Record<string, Record<string, boolean>>>(capabilities: C): ContractBuilder<Target, Tables, Models, CoreHash, ExtensionPacks, C>;
10
- table<TableName extends string, T extends TableBuilder<TableName, Record<string, ColumnBuilderState<string, boolean, string>>, readonly string[] | undefined>>(name: TableName, callback: (t: TableBuilder<TableName>) => T | undefined): ContractBuilder<Target, Tables & Record<TableName, ReturnType<T['build']>>, Models, CoreHash, ExtensionPacks, Capabilities>;
11
- model<ModelName extends string, TableName extends string, M extends ModelBuilder<ModelName, TableName, Record<string, string>, Record<string, RelationDefinition>>>(name: ModelName, table: TableName, callback: (m: ModelBuilder<ModelName, TableName, Record<string, string>, Record<never, never>>) => M | undefined): ContractBuilder<Target, Tables, Models & Record<ModelName, ReturnType<M['build']>>, CoreHash, ExtensionPacks, Capabilities>;
12
- coreHash<H extends string>(hash: H): ContractBuilder<Target, Tables, Models, H, ExtensionPacks, Capabilities>;
13
- }
14
- export declare function defineContract(): ContractBuilder;
15
- //# sourceMappingURL=contract-builder.d.ts.map
@@ -1 +0,0 @@
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,CA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b;IAoBD,QAAQ,CAAC,CAAC,SAAS,MAAM,EACvB,IAAI,EAAE,CAAC,GACN,eAAe,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,CAAC,EAAE,cAAc,EAAE,YAAY,CAAC;CAM5E;AAED,wBAAgB,cAAc,IAAI,eAAe,CAEhD"}
package/dist/index.d.ts DELETED
@@ -1,6 +0,0 @@
1
- export type { ColumnBuilder, ColumnBuilderState, ColumnTypeDescriptor, ContractBuilderState, ModelBuilderState, RelationDefinition, TableBuilderState, } from './builder-state';
2
- export { ContractBuilder, defineContract } from './contract-builder';
3
- export { ModelBuilder } from './model-builder';
4
- export { TableBuilder } from './table-builder';
5
- export type { BuildModelFields, BuildModels, BuildRelations, BuildStorage, BuildStorageColumn, BuildStorageTables, ExtractColumns, ExtractModelFields, ExtractModelRelations, ExtractPrimaryKey, Mutable, } from './types';
6
- //# sourceMappingURL=index.d.ts.map
@@ -1 +0,0 @@
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,iBAAiB,EACjB,kBAAkB,EAClB,iBAAiB,GAClB,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"}
package/dist/index.js DELETED
@@ -1,179 +0,0 @@
1
- // src/model-builder.ts
2
- var ModelBuilder = class _ModelBuilder {
3
- _name;
4
- _table;
5
- _fields;
6
- _relations;
7
- constructor(name, table, fields = {}, relations = {}) {
8
- this._name = name;
9
- this._table = table;
10
- this._fields = fields;
11
- this._relations = relations;
12
- }
13
- field(fieldName, columnName) {
14
- return new _ModelBuilder(
15
- this._name,
16
- this._table,
17
- {
18
- ...this._fields,
19
- [fieldName]: columnName
20
- },
21
- this._relations
22
- );
23
- }
24
- relation(name, options) {
25
- if (options.on.parentTable !== this._table) {
26
- throw new Error(
27
- `Relation "${name}" parentTable "${options.on.parentTable}" does not match model table "${this._table}"`
28
- );
29
- }
30
- if (options.cardinality === "N:M") {
31
- if (!options.through) {
32
- throw new Error(`Relation "${name}" with cardinality "N:M" requires through field`);
33
- }
34
- if (options.on.childTable !== options.through.table) {
35
- throw new Error(
36
- `Relation "${name}" childTable "${options.on.childTable}" does not match through.table "${options.through.table}"`
37
- );
38
- }
39
- } else {
40
- if (options.on.childTable !== options.toTable) {
41
- throw new Error(
42
- `Relation "${name}" childTable "${options.on.childTable}" does not match toTable "${options.toTable}"`
43
- );
44
- }
45
- }
46
- const relationDef = {
47
- to: options.toModel,
48
- cardinality: options.cardinality,
49
- on: {
50
- parentCols: options.on.parentColumns,
51
- childCols: options.on.childColumns
52
- },
53
- ...options.through ? {
54
- through: {
55
- table: options.through.table,
56
- parentCols: options.through.parentColumns,
57
- childCols: options.through.childColumns
58
- }
59
- } : void 0
60
- };
61
- return new _ModelBuilder(this._name, this._table, this._fields, {
62
- ...this._relations,
63
- [name]: relationDef
64
- });
65
- }
66
- build() {
67
- return {
68
- name: this._name,
69
- table: this._table,
70
- fields: this._fields,
71
- relations: this._relations
72
- };
73
- }
74
- };
75
-
76
- // src/table-builder.ts
77
- var TableBuilder = class _TableBuilder {
78
- _name;
79
- _columns;
80
- _primaryKey;
81
- constructor(name, columns = {}, primaryKey) {
82
- this._name = name;
83
- this._columns = columns;
84
- this._primaryKey = primaryKey;
85
- }
86
- column(name, options) {
87
- const nullable = options.nullable ?? false;
88
- const { codecId, nativeType } = options.type;
89
- const columnState = {
90
- name,
91
- nullable,
92
- type: codecId,
93
- nativeType
94
- };
95
- return new _TableBuilder(
96
- this._name,
97
- { ...this._columns, [name]: columnState },
98
- this._primaryKey
99
- );
100
- }
101
- primaryKey(columns, _name) {
102
- return new _TableBuilder(this._name, this._columns, columns);
103
- }
104
- unique(_columns, _name) {
105
- return this;
106
- }
107
- index(_columns, _name) {
108
- return this;
109
- }
110
- foreignKey(_columns, _references, _name) {
111
- return this;
112
- }
113
- build() {
114
- return {
115
- name: this._name,
116
- columns: this._columns,
117
- ...this._primaryKey !== void 0 ? { primaryKey: this._primaryKey } : {}
118
- };
119
- }
120
- };
121
-
122
- // src/contract-builder.ts
123
- var ContractBuilder = class _ContractBuilder {
124
- state;
125
- constructor(state) {
126
- this.state = state ?? {
127
- tables: {},
128
- models: {}
129
- };
130
- }
131
- target(packRef) {
132
- return new _ContractBuilder({
133
- ...this.state,
134
- target: packRef.targetId
135
- });
136
- }
137
- capabilities(capabilities) {
138
- return new _ContractBuilder({
139
- ...this.state,
140
- capabilities
141
- });
142
- }
143
- table(name, callback) {
144
- const tableBuilder = new TableBuilder(name);
145
- const result = callback(tableBuilder);
146
- const finalBuilder = result instanceof TableBuilder ? result : tableBuilder;
147
- const tableState = finalBuilder.build();
148
- return new _ContractBuilder({
149
- ...this.state,
150
- tables: { ...this.state.tables, [name]: tableState }
151
- });
152
- }
153
- model(name, table, callback) {
154
- const modelBuilder = new ModelBuilder(name, table);
155
- const result = callback(modelBuilder);
156
- const finalBuilder = result instanceof ModelBuilder ? result : modelBuilder;
157
- const modelState = finalBuilder.build();
158
- return new _ContractBuilder({
159
- ...this.state,
160
- models: { ...this.state.models, [name]: modelState }
161
- });
162
- }
163
- coreHash(hash) {
164
- return new _ContractBuilder({
165
- ...this.state,
166
- coreHash: hash
167
- });
168
- }
169
- };
170
- function defineContract() {
171
- return new ContractBuilder();
172
- }
173
- export {
174
- ContractBuilder,
175
- ModelBuilder,
176
- TableBuilder,
177
- defineContract
178
- };
179
- //# sourceMappingURL=index.js.map