@farming-labs/orm 0.0.9 → 0.0.11
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.
- package/dist/index.cjs +290 -15
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +46 -19
- package/dist/index.d.ts +46 -19
- package/dist/index.js +280 -14
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,7 @@
|
|
|
1
|
-
type ScalarKind = "id" | "string" | "boolean" | "datetime";
|
|
1
|
+
type ScalarKind = "id" | "string" | "boolean" | "datetime" | "integer" | "json";
|
|
2
|
+
type JsonValue = null | string | number | boolean | {
|
|
3
|
+
[key: string]: JsonValue;
|
|
4
|
+
} | JsonValue[];
|
|
2
5
|
type FieldReference = `${string}.${string}`;
|
|
3
6
|
type FieldConfig<Kind extends ScalarKind = ScalarKind, Nullable extends boolean = boolean> = {
|
|
4
7
|
kind: Kind;
|
|
@@ -10,25 +13,33 @@ type FieldConfig<Kind extends ScalarKind = ScalarKind, Nullable extends boolean
|
|
|
10
13
|
references?: FieldReference;
|
|
11
14
|
description?: string;
|
|
12
15
|
};
|
|
13
|
-
type
|
|
14
|
-
|
|
16
|
+
type ScalarValue<Kind extends ScalarKind> = Kind extends "id" ? string : Kind extends "string" ? string : Kind extends "boolean" ? boolean : Kind extends "datetime" ? Date : Kind extends "integer" ? number : JsonValue;
|
|
17
|
+
type AnyFieldBuilder = FieldBuilder<ScalarKind, boolean, ScalarValue<ScalarKind>>;
|
|
18
|
+
declare class FieldBuilder<Kind extends ScalarKind, Nullable extends boolean = false, Value = ScalarValue<Kind>> {
|
|
15
19
|
readonly config: FieldConfig<Kind, Nullable>;
|
|
16
20
|
readonly _tag = "field";
|
|
21
|
+
readonly __kind?: Kind;
|
|
22
|
+
readonly __nullable?: Nullable;
|
|
23
|
+
readonly __value?: Value;
|
|
17
24
|
constructor(config: FieldConfig<Kind, Nullable>);
|
|
18
|
-
unique(): FieldBuilder<Kind, Nullable
|
|
19
|
-
nullable(): FieldBuilder<Kind, true>;
|
|
20
|
-
default(value: unknown): FieldBuilder<Kind, Nullable
|
|
21
|
-
defaultNow(): FieldBuilder<Kind, Nullable
|
|
22
|
-
references(reference: FieldReference): FieldBuilder<Kind, Nullable
|
|
23
|
-
map(name: string): FieldBuilder<Kind, Nullable
|
|
24
|
-
describe(description: string): FieldBuilder<Kind, Nullable
|
|
25
|
+
unique(): FieldBuilder<Kind, Nullable, ScalarValue<Kind>>;
|
|
26
|
+
nullable(): FieldBuilder<Kind, true, Value>;
|
|
27
|
+
default(value: unknown): FieldBuilder<Kind, Nullable, ScalarValue<Kind>>;
|
|
28
|
+
defaultNow(): FieldBuilder<Kind, Nullable, ScalarValue<Kind>>;
|
|
29
|
+
references(reference: FieldReference): FieldBuilder<Kind, Nullable, ScalarValue<Kind>>;
|
|
30
|
+
map(name: string): FieldBuilder<Kind, Nullable, ScalarValue<Kind>>;
|
|
31
|
+
describe(description: string): FieldBuilder<Kind, Nullable, ScalarValue<Kind>>;
|
|
25
32
|
}
|
|
26
|
-
type
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
declare function
|
|
31
|
-
declare function
|
|
33
|
+
type FieldOutput<TField> = TField extends {
|
|
34
|
+
__nullable?: infer Nullable;
|
|
35
|
+
__value?: infer Value;
|
|
36
|
+
} ? Nullable extends true ? Value | null : Value : never;
|
|
37
|
+
declare function id(): FieldBuilder<"id", false, string>;
|
|
38
|
+
declare function string(): FieldBuilder<"string", false, string>;
|
|
39
|
+
declare function boolean(): FieldBuilder<"boolean", false, boolean>;
|
|
40
|
+
declare function datetime(): FieldBuilder<"datetime", false, Date>;
|
|
41
|
+
declare function integer(): FieldBuilder<"integer", false, number>;
|
|
42
|
+
declare function json<TValue extends JsonValue = JsonValue>(): FieldBuilder<"json", false, TValue>;
|
|
32
43
|
|
|
33
44
|
type RelationKind = "belongsTo" | "hasOne" | "hasMany" | "manyToMany";
|
|
34
45
|
type RelationDefinition<Target extends string = string, Kind extends RelationKind = RelationKind> = Kind extends "manyToMany" ? {
|
|
@@ -121,7 +132,6 @@ type RelationForName<TSchema, TName extends ModelName<TSchema>, TRelationName ex
|
|
|
121
132
|
type RelationTarget<TSchema, TName extends ModelName<TSchema>, TRelationName extends RelationName<TSchema, TName>> = RelationForName<TSchema, TName, TRelationName> extends RelationDefinition<infer Target, any> ? Extract<Target, ModelName<TSchema>> : never;
|
|
122
133
|
|
|
123
134
|
type Direction = "asc" | "desc";
|
|
124
|
-
type Primitive = string | number | boolean | Date | null;
|
|
125
135
|
type Comparable = string | number | Date;
|
|
126
136
|
type ValueFilter<T> = T extends string ? T | {
|
|
127
137
|
eq?: T;
|
|
@@ -141,7 +151,7 @@ type ValueFilter<T> = T extends string ? T | {
|
|
|
141
151
|
in?: T[];
|
|
142
152
|
not?: T;
|
|
143
153
|
};
|
|
144
|
-
type Where<TRecord extends Record<string,
|
|
154
|
+
type Where<TRecord extends Record<string, unknown>> = {
|
|
145
155
|
[K in keyof TRecord]?: ValueFilter<TRecord[K]>;
|
|
146
156
|
} & {
|
|
147
157
|
AND?: Array<Where<TRecord>>;
|
|
@@ -309,9 +319,26 @@ type ManifestModel = {
|
|
|
309
319
|
type SchemaManifest = {
|
|
310
320
|
models: Record<string, ManifestModel>;
|
|
311
321
|
};
|
|
322
|
+
type ManifestUniqueLookup = {
|
|
323
|
+
kind: "id" | "field" | "constraint";
|
|
324
|
+
fields: ManifestField[];
|
|
325
|
+
values: Record<string, unknown>;
|
|
326
|
+
constraint?: ManifestConstraint;
|
|
327
|
+
};
|
|
328
|
+
declare function isOperatorFilterObject(value: unknown): value is Record<string, unknown>;
|
|
329
|
+
declare function equalValues(left: unknown, right: unknown): boolean;
|
|
330
|
+
declare function requireUniqueLookup(model: ManifestModel, where: Record<string, unknown>, operation: string): ManifestUniqueLookup;
|
|
331
|
+
declare function resolveRowIdentityLookup(model: ManifestModel, row: Record<string, unknown>): ManifestUniqueLookup;
|
|
332
|
+
declare function toUniqueLookupWhere(lookup: ManifestUniqueLookup): {
|
|
333
|
+
[k: string]: unknown;
|
|
334
|
+
};
|
|
335
|
+
declare function mergeUniqueLookupCreateData(model: ManifestModel, createData: Partial<Record<string, unknown>>, lookup: ManifestUniqueLookup, operation: string): {
|
|
336
|
+
[x: string]: unknown;
|
|
337
|
+
};
|
|
338
|
+
declare function validateUniqueLookupUpdateData(model: ManifestModel, updateData: Partial<Record<string, unknown>>, lookup: ManifestUniqueLookup, operation: string): void;
|
|
312
339
|
declare function createManifest<TSchema extends SchemaDefinition<Record<string, AnyModelDefinition>>>(schema: TSchema): SchemaManifest;
|
|
313
340
|
|
|
314
341
|
type MemoryStore<TSchema extends SchemaDefinition<any>> = Partial<Record<ModelName<TSchema>, Array<Record<string, unknown>>>>;
|
|
315
342
|
declare function createMemoryDriver<TSchema extends SchemaDefinition<any>>(seed?: MemoryStore<TSchema>): OrmDriver<TSchema>;
|
|
316
343
|
|
|
317
|
-
export { type AnyFieldBuilder, type AnyModelDefinition, type AnyRelation, type BatchTask, type ConstraintFieldName, type ConstraintFieldSet, type CountArgs, type CreateArgs, type CreateManyArgs, type DeleteArgs, type DeleteManyArgs, type DrizzleGenerationOptions, FieldBuilder, type FieldConfig, type FieldMap, type FieldOutput, type FieldReference, type FindFirstArgs, type FindManyArgs, type FindOneArgs, type FindUniqueArgs, type ManifestConstraint, type ManifestField, type ManifestModel, type ModelClient, type ModelConstraints, type ModelDefinition, type ModelFields, type ModelForName, type ModelName, type ModelRelations, type OrmClient, type OrmDriver, type PrismaGenerationOptions, type RelationDefinition, type RelationForName, type RelationKind, type RelationMap, type RelationName, type RelationTarget, type ScalarKind, type ScalarRecord, type ScalarValue, type SchemaDefinition, type SchemaManifest, type SchemaModels, type SelectShape, type SelectedRecord, type SqlGenerationOptions, type UpdateArgs, type UpdateManyArgs, type UpsertArgs, type Where, belongsTo, boolean, createManifest, createMemoryDriver, createOrm, datetime, defineSchema, hasMany, hasOne, id, manyToMany, model, renderDrizzleSchema, renderPrismaSchema, renderSafeSql, replaceGeneratedBlock, string };
|
|
344
|
+
export { type AnyFieldBuilder, type AnyModelDefinition, type AnyRelation, type BatchTask, type ConstraintFieldName, type ConstraintFieldSet, type CountArgs, type CreateArgs, type CreateManyArgs, type DeleteArgs, type DeleteManyArgs, type DrizzleGenerationOptions, FieldBuilder, type FieldConfig, type FieldMap, type FieldOutput, type FieldReference, type FindFirstArgs, type FindManyArgs, type FindOneArgs, type FindUniqueArgs, type JsonValue, type ManifestConstraint, type ManifestField, type ManifestModel, type ManifestUniqueLookup, type ModelClient, type ModelConstraints, type ModelDefinition, type ModelFields, type ModelForName, type ModelName, type ModelRelations, type OrmClient, type OrmDriver, type PrismaGenerationOptions, type RelationDefinition, type RelationForName, type RelationKind, type RelationMap, type RelationName, type RelationTarget, type ScalarKind, type ScalarRecord, type ScalarValue, type SchemaDefinition, type SchemaManifest, type SchemaModels, type SelectShape, type SelectedRecord, type SqlGenerationOptions, type UpdateArgs, type UpdateManyArgs, type UpsertArgs, type Where, belongsTo, boolean, createManifest, createMemoryDriver, createOrm, datetime, defineSchema, equalValues, hasMany, hasOne, id, integer, isOperatorFilterObject, json, manyToMany, mergeUniqueLookupCreateData, model, renderDrizzleSchema, renderPrismaSchema, renderSafeSql, replaceGeneratedBlock, requireUniqueLookup, resolveRowIdentityLookup, string, toUniqueLookupWhere, validateUniqueLookupUpdateData };
|
package/dist/index.js
CHANGED
|
@@ -70,6 +70,9 @@ var FieldBuilder = class {
|
|
|
70
70
|
this.config = config;
|
|
71
71
|
}
|
|
72
72
|
_tag = "field";
|
|
73
|
+
__kind;
|
|
74
|
+
__nullable;
|
|
75
|
+
__value;
|
|
73
76
|
unique() {
|
|
74
77
|
return cloneField({
|
|
75
78
|
...this.config,
|
|
@@ -142,8 +145,189 @@ function datetime() {
|
|
|
142
145
|
unique: false
|
|
143
146
|
});
|
|
144
147
|
}
|
|
148
|
+
function integer() {
|
|
149
|
+
return new FieldBuilder({
|
|
150
|
+
kind: "integer",
|
|
151
|
+
nullable: false,
|
|
152
|
+
unique: false
|
|
153
|
+
});
|
|
154
|
+
}
|
|
155
|
+
function json() {
|
|
156
|
+
return new FieldBuilder({
|
|
157
|
+
kind: "json",
|
|
158
|
+
nullable: false,
|
|
159
|
+
unique: false
|
|
160
|
+
});
|
|
161
|
+
}
|
|
145
162
|
|
|
146
163
|
// src/manifest.ts
|
|
164
|
+
import { isDeepStrictEqual } from "util";
|
|
165
|
+
var filterOperatorKeys = /* @__PURE__ */ new Set(["eq", "contains", "in", "not", "gt", "gte", "lt", "lte"]);
|
|
166
|
+
function isFilterObject(value) {
|
|
167
|
+
return !!value && typeof value === "object" && !(value instanceof Date) && !Array.isArray(value);
|
|
168
|
+
}
|
|
169
|
+
function isOperatorFilterObject(value) {
|
|
170
|
+
if (!isFilterObject(value)) {
|
|
171
|
+
return false;
|
|
172
|
+
}
|
|
173
|
+
const keys = Object.keys(value);
|
|
174
|
+
return keys.length > 0 && keys.every((key) => filterOperatorKeys.has(key));
|
|
175
|
+
}
|
|
176
|
+
function equalValues(left, right) {
|
|
177
|
+
return isDeepStrictEqual(left, right);
|
|
178
|
+
}
|
|
179
|
+
function extractEqualityValue(filter) {
|
|
180
|
+
if (!isOperatorFilterObject(filter)) {
|
|
181
|
+
return {
|
|
182
|
+
supported: true,
|
|
183
|
+
value: filter
|
|
184
|
+
};
|
|
185
|
+
}
|
|
186
|
+
const keys = Object.keys(filter);
|
|
187
|
+
if (keys.length === 1 && "eq" in filter) {
|
|
188
|
+
return {
|
|
189
|
+
supported: true,
|
|
190
|
+
value: filter.eq
|
|
191
|
+
};
|
|
192
|
+
}
|
|
193
|
+
return {
|
|
194
|
+
supported: false,
|
|
195
|
+
value: void 0
|
|
196
|
+
};
|
|
197
|
+
}
|
|
198
|
+
function requireEqualityValues(model2, where, operation) {
|
|
199
|
+
const keys = Object.keys(where).filter((key) => key !== "AND" && key !== "OR" && key !== "NOT");
|
|
200
|
+
if ("AND" in where || "OR" in where || "NOT" in where || keys.length === 0) {
|
|
201
|
+
throw new Error(
|
|
202
|
+
`${operation} on model "${model2.name}" requires a unique equality filter in "where".`
|
|
203
|
+
);
|
|
204
|
+
}
|
|
205
|
+
const values = {};
|
|
206
|
+
for (const fieldName of keys) {
|
|
207
|
+
const field = model2.fields[fieldName];
|
|
208
|
+
if (!field) {
|
|
209
|
+
throw new Error(`Unknown field "${fieldName}" on model "${model2.name}".`);
|
|
210
|
+
}
|
|
211
|
+
const { supported, value } = extractEqualityValue(where[fieldName]);
|
|
212
|
+
if (!supported || value === void 0 || value === null) {
|
|
213
|
+
throw new Error(
|
|
214
|
+
`${operation} on model "${model2.name}" requires the "where" field "${fieldName}" to use a single non-null equality value.`
|
|
215
|
+
);
|
|
216
|
+
}
|
|
217
|
+
values[fieldName] = value;
|
|
218
|
+
}
|
|
219
|
+
return values;
|
|
220
|
+
}
|
|
221
|
+
function sameFieldSet(left, right) {
|
|
222
|
+
return left.length === right.length && left.every((fieldName) => right.includes(fieldName));
|
|
223
|
+
}
|
|
224
|
+
function requireUniqueLookup(model2, where, operation) {
|
|
225
|
+
const values = requireEqualityValues(model2, where, operation);
|
|
226
|
+
const keys = Object.keys(values);
|
|
227
|
+
if (keys.length === 1) {
|
|
228
|
+
const field = model2.fields[keys[0]];
|
|
229
|
+
if (field.kind === "id") {
|
|
230
|
+
return {
|
|
231
|
+
kind: "id",
|
|
232
|
+
fields: [field],
|
|
233
|
+
values
|
|
234
|
+
};
|
|
235
|
+
}
|
|
236
|
+
if (field.unique) {
|
|
237
|
+
return {
|
|
238
|
+
kind: "field",
|
|
239
|
+
fields: [field],
|
|
240
|
+
values
|
|
241
|
+
};
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
const constraint = model2.constraints.unique.find(
|
|
245
|
+
(candidate) => sameFieldSet([...candidate.fields], keys)
|
|
246
|
+
);
|
|
247
|
+
if (!constraint) {
|
|
248
|
+
throw new Error(
|
|
249
|
+
`${operation} on model "${model2.name}" requires the "where" clause to match an id field, unique field, or declared unique constraint using equality values only.`
|
|
250
|
+
);
|
|
251
|
+
}
|
|
252
|
+
return {
|
|
253
|
+
kind: "constraint",
|
|
254
|
+
fields: constraint.fields.map((fieldName) => model2.fields[fieldName]),
|
|
255
|
+
values: Object.fromEntries(
|
|
256
|
+
constraint.fields.map((fieldName) => [fieldName, values[fieldName]])
|
|
257
|
+
),
|
|
258
|
+
constraint
|
|
259
|
+
};
|
|
260
|
+
}
|
|
261
|
+
function resolveRowIdentityLookup(model2, row) {
|
|
262
|
+
const idField = model2.fields.id;
|
|
263
|
+
if (idField && row[idField.name] !== void 0 && row[idField.name] !== null) {
|
|
264
|
+
return {
|
|
265
|
+
kind: "id",
|
|
266
|
+
fields: [idField],
|
|
267
|
+
values: {
|
|
268
|
+
[idField.name]: row[idField.name]
|
|
269
|
+
}
|
|
270
|
+
};
|
|
271
|
+
}
|
|
272
|
+
const uniqueField = Object.values(model2.fields).find(
|
|
273
|
+
(field) => field.unique && row[field.name] !== void 0 && row[field.name] !== null
|
|
274
|
+
);
|
|
275
|
+
if (uniqueField) {
|
|
276
|
+
return {
|
|
277
|
+
kind: "field",
|
|
278
|
+
fields: [uniqueField],
|
|
279
|
+
values: {
|
|
280
|
+
[uniqueField.name]: row[uniqueField.name]
|
|
281
|
+
}
|
|
282
|
+
};
|
|
283
|
+
}
|
|
284
|
+
for (const constraint of model2.constraints.unique) {
|
|
285
|
+
if (constraint.fields.every(
|
|
286
|
+
(fieldName) => row[fieldName] !== void 0 && row[fieldName] !== null
|
|
287
|
+
)) {
|
|
288
|
+
return {
|
|
289
|
+
kind: "constraint",
|
|
290
|
+
fields: constraint.fields.map((fieldName) => model2.fields[fieldName]),
|
|
291
|
+
values: Object.fromEntries(
|
|
292
|
+
constraint.fields.map((fieldName) => [fieldName, row[fieldName]])
|
|
293
|
+
),
|
|
294
|
+
constraint
|
|
295
|
+
};
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
throw new Error(
|
|
299
|
+
`Model "${model2.name}" requires an "id" field, unique field, or declared unique constraint with non-null values for identity lookups.`
|
|
300
|
+
);
|
|
301
|
+
}
|
|
302
|
+
function toUniqueLookupWhere(lookup) {
|
|
303
|
+
return Object.fromEntries(lookup.fields.map((field) => [field.name, lookup.values[field.name]]));
|
|
304
|
+
}
|
|
305
|
+
function mergeUniqueLookupCreateData(model2, createData, lookup, operation) {
|
|
306
|
+
const output = {
|
|
307
|
+
...createData
|
|
308
|
+
};
|
|
309
|
+
for (const field of lookup.fields) {
|
|
310
|
+
const currentValue = output[field.name];
|
|
311
|
+
const expectedValue = lookup.values[field.name];
|
|
312
|
+
if (currentValue !== void 0 && !equalValues(currentValue, expectedValue)) {
|
|
313
|
+
throw new Error(
|
|
314
|
+
`${operation} on model "${model2.name}" requires create.${field.name} to match where.${field.name}.`
|
|
315
|
+
);
|
|
316
|
+
}
|
|
317
|
+
output[field.name] = currentValue ?? expectedValue;
|
|
318
|
+
}
|
|
319
|
+
return output;
|
|
320
|
+
}
|
|
321
|
+
function validateUniqueLookupUpdateData(model2, updateData, lookup, operation) {
|
|
322
|
+
for (const field of lookup.fields) {
|
|
323
|
+
const nextValue = updateData[field.name];
|
|
324
|
+
if (nextValue !== void 0 && !equalValues(nextValue, lookup.values[field.name])) {
|
|
325
|
+
throw new Error(
|
|
326
|
+
`${operation} on model "${model2.name}" cannot change the conflict field "${field.name}".`
|
|
327
|
+
);
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
}
|
|
147
331
|
function createConstraintName(table, columns, suffix) {
|
|
148
332
|
const base = [table, ...columns].join("_").replace(/[^a-zA-Z0-9_]+/g, "_").replace(/_+/g, "_").replace(/^_+|_+$/g, "").toLowerCase();
|
|
149
333
|
return `${base}_${suffix}`;
|
|
@@ -254,6 +438,10 @@ function prismaType(field) {
|
|
|
254
438
|
return "Boolean";
|
|
255
439
|
case "datetime":
|
|
256
440
|
return "DateTime";
|
|
441
|
+
case "integer":
|
|
442
|
+
return "Int";
|
|
443
|
+
case "json":
|
|
444
|
+
return "Json";
|
|
257
445
|
}
|
|
258
446
|
}
|
|
259
447
|
function drizzleConstraintProperty(constraint) {
|
|
@@ -274,6 +462,12 @@ function drizzleImports(dialect, manifest) {
|
|
|
274
462
|
const needsDate = models.some(
|
|
275
463
|
(model2) => Object.values(model2.fields).some((field) => field.kind === "datetime")
|
|
276
464
|
);
|
|
465
|
+
const needsInteger = models.some(
|
|
466
|
+
(model2) => Object.values(model2.fields).some((field) => field.kind === "integer")
|
|
467
|
+
);
|
|
468
|
+
const needsJson = models.some(
|
|
469
|
+
(model2) => Object.values(model2.fields).some((field) => field.kind === "json")
|
|
470
|
+
);
|
|
277
471
|
const needsIndexes = models.some(
|
|
278
472
|
(model2) => model2.constraints.indexes.length || model2.constraints.unique.length
|
|
279
473
|
);
|
|
@@ -282,7 +476,9 @@ function drizzleImports(dialect, manifest) {
|
|
|
282
476
|
"pgTable",
|
|
283
477
|
"text",
|
|
284
478
|
needsBoolean ? "boolean" : null,
|
|
479
|
+
needsInteger ? "integer" : null,
|
|
285
480
|
needsDate ? "timestamp" : null,
|
|
481
|
+
needsJson ? "jsonb" : null,
|
|
286
482
|
needsIndexes ? "index" : null,
|
|
287
483
|
needsIndexes ? "uniqueIndex" : null
|
|
288
484
|
].filter(Boolean);
|
|
@@ -293,7 +489,9 @@ function drizzleImports(dialect, manifest) {
|
|
|
293
489
|
"varchar",
|
|
294
490
|
"text",
|
|
295
491
|
needsBoolean ? "boolean" : null,
|
|
492
|
+
needsInteger ? "int" : null,
|
|
296
493
|
needsDate ? "datetime" : null,
|
|
494
|
+
needsJson ? "json" : null,
|
|
297
495
|
needsIndexes ? "index" : null,
|
|
298
496
|
needsIndexes ? "uniqueIndex" : null
|
|
299
497
|
].filter(Boolean);
|
|
@@ -307,6 +505,10 @@ function drizzleImports(dialect, manifest) {
|
|
|
307
505
|
].filter(Boolean);
|
|
308
506
|
}
|
|
309
507
|
function drizzleColumn(field, dialect, options = {}) {
|
|
508
|
+
const renderDefault = () => {
|
|
509
|
+
if (field.defaultValue === void 0 || field.kind === "json") return "";
|
|
510
|
+
return `.default(${JSON.stringify(field.defaultValue)})`;
|
|
511
|
+
};
|
|
310
512
|
if (field.kind === "id") {
|
|
311
513
|
if (dialect === "mysql") {
|
|
312
514
|
return `varchar("${field.column}", { length: 191 }).primaryKey()`;
|
|
@@ -316,15 +518,30 @@ function drizzleColumn(field, dialect, options = {}) {
|
|
|
316
518
|
if (field.kind === "string") {
|
|
317
519
|
if (dialect === "mysql") {
|
|
318
520
|
const base = field.unique || field.references || options.indexed ? `varchar("${field.column}", { length: 191 })` : `text("${field.column}")`;
|
|
319
|
-
return `${base}${field.nullable ? "" : ".notNull()"}${field.unique ? ".unique()" : ""}${
|
|
521
|
+
return `${base}${field.nullable ? "" : ".notNull()"}${field.unique ? ".unique()" : ""}${renderDefault()}`;
|
|
320
522
|
}
|
|
321
|
-
return `text("${field.column}")${field.nullable ? "" : ".notNull()"}${field.unique ? ".unique()" : ""}${
|
|
523
|
+
return `text("${field.column}")${field.nullable ? "" : ".notNull()"}${field.unique ? ".unique()" : ""}${renderDefault()}`;
|
|
322
524
|
}
|
|
323
525
|
if (field.kind === "boolean") {
|
|
324
526
|
if (dialect === "sqlite") {
|
|
325
|
-
return `integer("${field.column}", { mode: "boolean" })${field.nullable ? "" : ".notNull()"}${
|
|
527
|
+
return `integer("${field.column}", { mode: "boolean" })${field.nullable ? "" : ".notNull()"}${renderDefault()}`;
|
|
326
528
|
}
|
|
327
|
-
return `boolean("${field.column}")${field.nullable ? "" : ".notNull()"}${
|
|
529
|
+
return `boolean("${field.column}")${field.nullable ? "" : ".notNull()"}${renderDefault()}`;
|
|
530
|
+
}
|
|
531
|
+
if (field.kind === "integer") {
|
|
532
|
+
if (dialect === "mysql") {
|
|
533
|
+
return `int("${field.column}")${field.nullable ? "" : ".notNull()"}${renderDefault()}`;
|
|
534
|
+
}
|
|
535
|
+
return `integer("${field.column}")${field.nullable ? "" : ".notNull()"}${renderDefault()}`;
|
|
536
|
+
}
|
|
537
|
+
if (field.kind === "json") {
|
|
538
|
+
if (dialect === "pg") {
|
|
539
|
+
return `jsonb("${field.column}")${field.nullable ? "" : ".notNull()"}`;
|
|
540
|
+
}
|
|
541
|
+
if (dialect === "mysql") {
|
|
542
|
+
return `json("${field.column}")${field.nullable ? "" : ".notNull()"}`;
|
|
543
|
+
}
|
|
544
|
+
return `text("${field.column}", { mode: "json" })${field.nullable ? "" : ".notNull()"}`;
|
|
328
545
|
}
|
|
329
546
|
if (dialect === "mysql") {
|
|
330
547
|
return `datetime("${field.column}", { mode: "date" })${field.nullable ? "" : ".notNull()"}`;
|
|
@@ -344,6 +561,14 @@ function sqlType(field, dialect, options = {}) {
|
|
|
344
561
|
if (field.kind === "boolean") {
|
|
345
562
|
return dialect === "sqlite" ? "integer" : "boolean";
|
|
346
563
|
}
|
|
564
|
+
if (field.kind === "integer") {
|
|
565
|
+
return "integer";
|
|
566
|
+
}
|
|
567
|
+
if (field.kind === "json") {
|
|
568
|
+
if (dialect === "postgres") return "jsonb";
|
|
569
|
+
if (dialect === "mysql") return "json";
|
|
570
|
+
return "text";
|
|
571
|
+
}
|
|
347
572
|
if (dialect === "mysql") {
|
|
348
573
|
return "datetime";
|
|
349
574
|
}
|
|
@@ -397,7 +622,7 @@ function renderPrismaSchema(schema, options = {}) {
|
|
|
397
622
|
if (field.kind === "id") modifiers.push("@id");
|
|
398
623
|
if (field.generated === "id") modifiers.push("@default(cuid())");
|
|
399
624
|
if (field.generated === "now") modifiers.push("@default(now())");
|
|
400
|
-
if (field.defaultValue !== void 0 && field.generated === void 0) {
|
|
625
|
+
if (field.defaultValue !== void 0 && field.generated === void 0 && field.kind !== "json") {
|
|
401
626
|
modifiers.push(
|
|
402
627
|
typeof field.defaultValue === "string" ? `@default("${field.defaultValue}")` : `@default(${String(field.defaultValue)})`
|
|
403
628
|
);
|
|
@@ -555,7 +780,7 @@ function renderSafeSql(schema, options) {
|
|
|
555
780
|
if (field.kind === "id") parts.push("primary key");
|
|
556
781
|
if (!field.nullable) parts.push("not null");
|
|
557
782
|
if (field.unique && field.kind !== "id") parts.push("unique");
|
|
558
|
-
if (field.defaultValue !== void 0) {
|
|
783
|
+
if (field.defaultValue !== void 0 && field.kind !== "json") {
|
|
559
784
|
parts.push(
|
|
560
785
|
`default ${typeof field.defaultValue === "string" ? `'${field.defaultValue}'` : String(field.defaultValue)}`
|
|
561
786
|
);
|
|
@@ -606,17 +831,24 @@ ${block}
|
|
|
606
831
|
|
|
607
832
|
// src/memory.ts
|
|
608
833
|
import { randomUUID } from "crypto";
|
|
609
|
-
var
|
|
834
|
+
var manifestCache = /* @__PURE__ */ new WeakMap();
|
|
835
|
+
function getManifest(schema) {
|
|
836
|
+
const cached = manifestCache.get(schema);
|
|
837
|
+
if (cached) return cached;
|
|
838
|
+
const next = createManifest(schema);
|
|
839
|
+
manifestCache.set(schema, next);
|
|
840
|
+
return next;
|
|
841
|
+
}
|
|
610
842
|
function evaluateFilter(value, filter) {
|
|
611
|
-
if (
|
|
612
|
-
return value
|
|
843
|
+
if (!isOperatorFilterObject(filter)) {
|
|
844
|
+
return equalValues(value, filter);
|
|
613
845
|
}
|
|
614
846
|
const record = filter;
|
|
615
|
-
if ("eq" in record && value
|
|
616
|
-
if ("not" in record && value
|
|
847
|
+
if ("eq" in record && !equalValues(value, record.eq)) return false;
|
|
848
|
+
if ("not" in record && equalValues(value, record.not)) return false;
|
|
617
849
|
if ("in" in record) {
|
|
618
850
|
const values = Array.isArray(record.in) ? record.in : [];
|
|
619
|
-
if (!values.
|
|
851
|
+
if (!values.some((candidate) => equalValues(candidate, value))) return false;
|
|
620
852
|
}
|
|
621
853
|
if ("contains" in record) {
|
|
622
854
|
if (typeof value !== "string" || typeof record.contains !== "string") return false;
|
|
@@ -799,6 +1031,11 @@ function createMemoryDriver(seed) {
|
|
|
799
1031
|
return projectRow(schema, model2, row, args.select);
|
|
800
1032
|
},
|
|
801
1033
|
async findUnique(schema, model2, args) {
|
|
1034
|
+
requireUniqueLookup(
|
|
1035
|
+
getManifest(schema).models[model2],
|
|
1036
|
+
args.where,
|
|
1037
|
+
"FindUnique"
|
|
1038
|
+
);
|
|
802
1039
|
const row = applyQuery(getRows(model2), args)[0];
|
|
803
1040
|
if (!row) return null;
|
|
804
1041
|
return projectRow(schema, model2, row, args.select);
|
|
@@ -830,12 +1067,32 @@ function createMemoryDriver(seed) {
|
|
|
830
1067
|
return rows.length;
|
|
831
1068
|
},
|
|
832
1069
|
async upsert(schema, model2, args) {
|
|
1070
|
+
const lookup = requireUniqueLookup(
|
|
1071
|
+
getManifest(schema).models[model2],
|
|
1072
|
+
args.where,
|
|
1073
|
+
"Upsert"
|
|
1074
|
+
);
|
|
1075
|
+
validateUniqueLookupUpdateData(
|
|
1076
|
+
getManifest(schema).models[model2],
|
|
1077
|
+
args.update,
|
|
1078
|
+
lookup,
|
|
1079
|
+
"Upsert"
|
|
1080
|
+
);
|
|
833
1081
|
const row = getRows(model2).find((item) => matchesWhere(item, args.where));
|
|
834
1082
|
if (row) {
|
|
835
1083
|
Object.assign(row, args.update);
|
|
836
1084
|
return projectRow(schema, model2, row, args.select);
|
|
837
1085
|
}
|
|
838
|
-
const created = buildRow(
|
|
1086
|
+
const created = buildRow(
|
|
1087
|
+
schema,
|
|
1088
|
+
model2,
|
|
1089
|
+
mergeUniqueLookupCreateData(
|
|
1090
|
+
getManifest(schema).models[model2],
|
|
1091
|
+
args.create,
|
|
1092
|
+
lookup,
|
|
1093
|
+
"Upsert"
|
|
1094
|
+
)
|
|
1095
|
+
);
|
|
839
1096
|
getRows(model2).push(created);
|
|
840
1097
|
return projectRow(schema, model2, created, args.select);
|
|
841
1098
|
},
|
|
@@ -926,15 +1183,24 @@ export {
|
|
|
926
1183
|
createOrm,
|
|
927
1184
|
datetime,
|
|
928
1185
|
defineSchema,
|
|
1186
|
+
equalValues,
|
|
929
1187
|
hasMany,
|
|
930
1188
|
hasOne,
|
|
931
1189
|
id,
|
|
1190
|
+
integer,
|
|
1191
|
+
isOperatorFilterObject,
|
|
1192
|
+
json,
|
|
932
1193
|
manyToMany,
|
|
1194
|
+
mergeUniqueLookupCreateData,
|
|
933
1195
|
model,
|
|
934
1196
|
renderDrizzleSchema,
|
|
935
1197
|
renderPrismaSchema,
|
|
936
1198
|
renderSafeSql,
|
|
937
1199
|
replaceGeneratedBlock,
|
|
938
|
-
|
|
1200
|
+
requireUniqueLookup,
|
|
1201
|
+
resolveRowIdentityLookup,
|
|
1202
|
+
string,
|
|
1203
|
+
toUniqueLookupWhere,
|
|
1204
|
+
validateUniqueLookupUpdateData
|
|
939
1205
|
};
|
|
940
1206
|
//# sourceMappingURL=index.js.map
|