@farming-labs/orm 0.0.10 → 0.0.12
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 +92 -21
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +31 -19
- package/dist/index.d.ts +31 -19
- package/dist/index.js +88 -21
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -28,9 +28,13 @@ __export(index_exports, {
|
|
|
28
28
|
createOrm: () => createOrm,
|
|
29
29
|
datetime: () => datetime,
|
|
30
30
|
defineSchema: () => defineSchema,
|
|
31
|
+
equalValues: () => equalValues,
|
|
31
32
|
hasMany: () => hasMany,
|
|
32
33
|
hasOne: () => hasOne,
|
|
33
34
|
id: () => id,
|
|
35
|
+
integer: () => integer,
|
|
36
|
+
isOperatorFilterObject: () => isOperatorFilterObject,
|
|
37
|
+
json: () => json,
|
|
34
38
|
manyToMany: () => manyToMany,
|
|
35
39
|
mergeUniqueLookupCreateData: () => mergeUniqueLookupCreateData,
|
|
36
40
|
model: () => model,
|
|
@@ -118,6 +122,9 @@ var FieldBuilder = class {
|
|
|
118
122
|
this.config = config;
|
|
119
123
|
}
|
|
120
124
|
_tag = "field";
|
|
125
|
+
__kind;
|
|
126
|
+
__nullable;
|
|
127
|
+
__value;
|
|
121
128
|
unique() {
|
|
122
129
|
return cloneField({
|
|
123
130
|
...this.config,
|
|
@@ -190,19 +197,39 @@ function datetime() {
|
|
|
190
197
|
unique: false
|
|
191
198
|
});
|
|
192
199
|
}
|
|
200
|
+
function integer() {
|
|
201
|
+
return new FieldBuilder({
|
|
202
|
+
kind: "integer",
|
|
203
|
+
nullable: false,
|
|
204
|
+
unique: false
|
|
205
|
+
});
|
|
206
|
+
}
|
|
207
|
+
function json() {
|
|
208
|
+
return new FieldBuilder({
|
|
209
|
+
kind: "json",
|
|
210
|
+
nullable: false,
|
|
211
|
+
unique: false
|
|
212
|
+
});
|
|
213
|
+
}
|
|
193
214
|
|
|
194
215
|
// src/manifest.ts
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
return left.getTime() === right.getTime();
|
|
198
|
-
}
|
|
199
|
-
return Object.is(left, right);
|
|
200
|
-
}
|
|
216
|
+
var import_node_util = require("util");
|
|
217
|
+
var filterOperatorKeys = /* @__PURE__ */ new Set(["eq", "contains", "in", "not", "gt", "gte", "lt", "lte"]);
|
|
201
218
|
function isFilterObject(value) {
|
|
202
219
|
return !!value && typeof value === "object" && !(value instanceof Date) && !Array.isArray(value);
|
|
203
220
|
}
|
|
221
|
+
function isOperatorFilterObject(value) {
|
|
222
|
+
if (!isFilterObject(value)) {
|
|
223
|
+
return false;
|
|
224
|
+
}
|
|
225
|
+
const keys = Object.keys(value);
|
|
226
|
+
return keys.length > 0 && keys.every((key) => filterOperatorKeys.has(key));
|
|
227
|
+
}
|
|
228
|
+
function equalValues(left, right) {
|
|
229
|
+
return (0, import_node_util.isDeepStrictEqual)(left, right);
|
|
230
|
+
}
|
|
204
231
|
function extractEqualityValue(filter) {
|
|
205
|
-
if (!
|
|
232
|
+
if (!isOperatorFilterObject(filter)) {
|
|
206
233
|
return {
|
|
207
234
|
supported: true,
|
|
208
235
|
value: filter
|
|
@@ -334,7 +361,7 @@ function mergeUniqueLookupCreateData(model2, createData, lookup, operation) {
|
|
|
334
361
|
for (const field of lookup.fields) {
|
|
335
362
|
const currentValue = output[field.name];
|
|
336
363
|
const expectedValue = lookup.values[field.name];
|
|
337
|
-
if (currentValue !== void 0 && !
|
|
364
|
+
if (currentValue !== void 0 && !equalValues(currentValue, expectedValue)) {
|
|
338
365
|
throw new Error(
|
|
339
366
|
`${operation} on model "${model2.name}" requires create.${field.name} to match where.${field.name}.`
|
|
340
367
|
);
|
|
@@ -346,7 +373,7 @@ function mergeUniqueLookupCreateData(model2, createData, lookup, operation) {
|
|
|
346
373
|
function validateUniqueLookupUpdateData(model2, updateData, lookup, operation) {
|
|
347
374
|
for (const field of lookup.fields) {
|
|
348
375
|
const nextValue = updateData[field.name];
|
|
349
|
-
if (nextValue !== void 0 && !
|
|
376
|
+
if (nextValue !== void 0 && !equalValues(nextValue, lookup.values[field.name])) {
|
|
350
377
|
throw new Error(
|
|
351
378
|
`${operation} on model "${model2.name}" cannot change the conflict field "${field.name}".`
|
|
352
379
|
);
|
|
@@ -463,6 +490,10 @@ function prismaType(field) {
|
|
|
463
490
|
return "Boolean";
|
|
464
491
|
case "datetime":
|
|
465
492
|
return "DateTime";
|
|
493
|
+
case "integer":
|
|
494
|
+
return "Int";
|
|
495
|
+
case "json":
|
|
496
|
+
return "Json";
|
|
466
497
|
}
|
|
467
498
|
}
|
|
468
499
|
function drizzleConstraintProperty(constraint) {
|
|
@@ -483,6 +514,12 @@ function drizzleImports(dialect, manifest) {
|
|
|
483
514
|
const needsDate = models.some(
|
|
484
515
|
(model2) => Object.values(model2.fields).some((field) => field.kind === "datetime")
|
|
485
516
|
);
|
|
517
|
+
const needsInteger = models.some(
|
|
518
|
+
(model2) => Object.values(model2.fields).some((field) => field.kind === "integer")
|
|
519
|
+
);
|
|
520
|
+
const needsJson = models.some(
|
|
521
|
+
(model2) => Object.values(model2.fields).some((field) => field.kind === "json")
|
|
522
|
+
);
|
|
486
523
|
const needsIndexes = models.some(
|
|
487
524
|
(model2) => model2.constraints.indexes.length || model2.constraints.unique.length
|
|
488
525
|
);
|
|
@@ -491,7 +528,9 @@ function drizzleImports(dialect, manifest) {
|
|
|
491
528
|
"pgTable",
|
|
492
529
|
"text",
|
|
493
530
|
needsBoolean ? "boolean" : null,
|
|
531
|
+
needsInteger ? "integer" : null,
|
|
494
532
|
needsDate ? "timestamp" : null,
|
|
533
|
+
needsJson ? "jsonb" : null,
|
|
495
534
|
needsIndexes ? "index" : null,
|
|
496
535
|
needsIndexes ? "uniqueIndex" : null
|
|
497
536
|
].filter(Boolean);
|
|
@@ -502,7 +541,9 @@ function drizzleImports(dialect, manifest) {
|
|
|
502
541
|
"varchar",
|
|
503
542
|
"text",
|
|
504
543
|
needsBoolean ? "boolean" : null,
|
|
544
|
+
needsInteger ? "int" : null,
|
|
505
545
|
needsDate ? "datetime" : null,
|
|
546
|
+
needsJson ? "json" : null,
|
|
506
547
|
needsIndexes ? "index" : null,
|
|
507
548
|
needsIndexes ? "uniqueIndex" : null
|
|
508
549
|
].filter(Boolean);
|
|
@@ -516,6 +557,10 @@ function drizzleImports(dialect, manifest) {
|
|
|
516
557
|
].filter(Boolean);
|
|
517
558
|
}
|
|
518
559
|
function drizzleColumn(field, dialect, options = {}) {
|
|
560
|
+
const renderDefault = () => {
|
|
561
|
+
if (field.defaultValue === void 0 || field.kind === "json") return "";
|
|
562
|
+
return `.default(${JSON.stringify(field.defaultValue)})`;
|
|
563
|
+
};
|
|
519
564
|
if (field.kind === "id") {
|
|
520
565
|
if (dialect === "mysql") {
|
|
521
566
|
return `varchar("${field.column}", { length: 191 }).primaryKey()`;
|
|
@@ -525,15 +570,30 @@ function drizzleColumn(field, dialect, options = {}) {
|
|
|
525
570
|
if (field.kind === "string") {
|
|
526
571
|
if (dialect === "mysql") {
|
|
527
572
|
const base = field.unique || field.references || options.indexed ? `varchar("${field.column}", { length: 191 })` : `text("${field.column}")`;
|
|
528
|
-
return `${base}${field.nullable ? "" : ".notNull()"}${field.unique ? ".unique()" : ""}${
|
|
573
|
+
return `${base}${field.nullable ? "" : ".notNull()"}${field.unique ? ".unique()" : ""}${renderDefault()}`;
|
|
529
574
|
}
|
|
530
|
-
return `text("${field.column}")${field.nullable ? "" : ".notNull()"}${field.unique ? ".unique()" : ""}${
|
|
575
|
+
return `text("${field.column}")${field.nullable ? "" : ".notNull()"}${field.unique ? ".unique()" : ""}${renderDefault()}`;
|
|
531
576
|
}
|
|
532
577
|
if (field.kind === "boolean") {
|
|
533
578
|
if (dialect === "sqlite") {
|
|
534
|
-
return `integer("${field.column}", { mode: "boolean" })${field.nullable ? "" : ".notNull()"}${
|
|
579
|
+
return `integer("${field.column}", { mode: "boolean" })${field.nullable ? "" : ".notNull()"}${renderDefault()}`;
|
|
580
|
+
}
|
|
581
|
+
return `boolean("${field.column}")${field.nullable ? "" : ".notNull()"}${renderDefault()}`;
|
|
582
|
+
}
|
|
583
|
+
if (field.kind === "integer") {
|
|
584
|
+
if (dialect === "mysql") {
|
|
585
|
+
return `int("${field.column}")${field.nullable ? "" : ".notNull()"}${renderDefault()}`;
|
|
535
586
|
}
|
|
536
|
-
return `
|
|
587
|
+
return `integer("${field.column}")${field.nullable ? "" : ".notNull()"}${renderDefault()}`;
|
|
588
|
+
}
|
|
589
|
+
if (field.kind === "json") {
|
|
590
|
+
if (dialect === "pg") {
|
|
591
|
+
return `jsonb("${field.column}")${field.nullable ? "" : ".notNull()"}`;
|
|
592
|
+
}
|
|
593
|
+
if (dialect === "mysql") {
|
|
594
|
+
return `json("${field.column}")${field.nullable ? "" : ".notNull()"}`;
|
|
595
|
+
}
|
|
596
|
+
return `text("${field.column}", { mode: "json" })${field.nullable ? "" : ".notNull()"}`;
|
|
537
597
|
}
|
|
538
598
|
if (dialect === "mysql") {
|
|
539
599
|
return `datetime("${field.column}", { mode: "date" })${field.nullable ? "" : ".notNull()"}`;
|
|
@@ -553,6 +613,14 @@ function sqlType(field, dialect, options = {}) {
|
|
|
553
613
|
if (field.kind === "boolean") {
|
|
554
614
|
return dialect === "sqlite" ? "integer" : "boolean";
|
|
555
615
|
}
|
|
616
|
+
if (field.kind === "integer") {
|
|
617
|
+
return "integer";
|
|
618
|
+
}
|
|
619
|
+
if (field.kind === "json") {
|
|
620
|
+
if (dialect === "postgres") return "jsonb";
|
|
621
|
+
if (dialect === "mysql") return "json";
|
|
622
|
+
return "text";
|
|
623
|
+
}
|
|
556
624
|
if (dialect === "mysql") {
|
|
557
625
|
return "datetime";
|
|
558
626
|
}
|
|
@@ -606,7 +674,7 @@ function renderPrismaSchema(schema, options = {}) {
|
|
|
606
674
|
if (field.kind === "id") modifiers.push("@id");
|
|
607
675
|
if (field.generated === "id") modifiers.push("@default(cuid())");
|
|
608
676
|
if (field.generated === "now") modifiers.push("@default(now())");
|
|
609
|
-
if (field.defaultValue !== void 0 && field.generated === void 0) {
|
|
677
|
+
if (field.defaultValue !== void 0 && field.generated === void 0 && field.kind !== "json") {
|
|
610
678
|
modifiers.push(
|
|
611
679
|
typeof field.defaultValue === "string" ? `@default("${field.defaultValue}")` : `@default(${String(field.defaultValue)})`
|
|
612
680
|
);
|
|
@@ -764,7 +832,7 @@ function renderSafeSql(schema, options) {
|
|
|
764
832
|
if (field.kind === "id") parts.push("primary key");
|
|
765
833
|
if (!field.nullable) parts.push("not null");
|
|
766
834
|
if (field.unique && field.kind !== "id") parts.push("unique");
|
|
767
|
-
if (field.defaultValue !== void 0) {
|
|
835
|
+
if (field.defaultValue !== void 0 && field.kind !== "json") {
|
|
768
836
|
parts.push(
|
|
769
837
|
`default ${typeof field.defaultValue === "string" ? `'${field.defaultValue}'` : String(field.defaultValue)}`
|
|
770
838
|
);
|
|
@@ -815,7 +883,6 @@ ${block}
|
|
|
815
883
|
|
|
816
884
|
// src/memory.ts
|
|
817
885
|
var import_node_crypto = require("crypto");
|
|
818
|
-
var isDate = (value) => value instanceof Date;
|
|
819
886
|
var manifestCache = /* @__PURE__ */ new WeakMap();
|
|
820
887
|
function getManifest(schema) {
|
|
821
888
|
const cached = manifestCache.get(schema);
|
|
@@ -825,15 +892,15 @@ function getManifest(schema) {
|
|
|
825
892
|
return next;
|
|
826
893
|
}
|
|
827
894
|
function evaluateFilter(value, filter) {
|
|
828
|
-
if (
|
|
829
|
-
return value
|
|
895
|
+
if (!isOperatorFilterObject(filter)) {
|
|
896
|
+
return equalValues(value, filter);
|
|
830
897
|
}
|
|
831
898
|
const record = filter;
|
|
832
|
-
if ("eq" in record && value
|
|
833
|
-
if ("not" in record && value
|
|
899
|
+
if ("eq" in record && !equalValues(value, record.eq)) return false;
|
|
900
|
+
if ("not" in record && equalValues(value, record.not)) return false;
|
|
834
901
|
if ("in" in record) {
|
|
835
902
|
const values = Array.isArray(record.in) ? record.in : [];
|
|
836
|
-
if (!values.
|
|
903
|
+
if (!values.some((candidate) => equalValues(candidate, value))) return false;
|
|
837
904
|
}
|
|
838
905
|
if ("contains" in record) {
|
|
839
906
|
if (typeof value !== "string" || typeof record.contains !== "string") return false;
|
|
@@ -1169,9 +1236,13 @@ function defineSchema(models) {
|
|
|
1169
1236
|
createOrm,
|
|
1170
1237
|
datetime,
|
|
1171
1238
|
defineSchema,
|
|
1239
|
+
equalValues,
|
|
1172
1240
|
hasMany,
|
|
1173
1241
|
hasOne,
|
|
1174
1242
|
id,
|
|
1243
|
+
integer,
|
|
1244
|
+
isOperatorFilterObject,
|
|
1245
|
+
json,
|
|
1175
1246
|
manyToMany,
|
|
1176
1247
|
mergeUniqueLookupCreateData,
|
|
1177
1248
|
model,
|