hekireki 0.52.0 → 0.52.2
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/README.md +8 -0
- package/dist/bin/activerecord.js +1 -1
- package/dist/bin/ajv.js +1 -1
- package/dist/bin/arktype.js +1 -1
- package/dist/bin/atlas.js +1 -1
- package/dist/bin/django.js +1 -1
- package/dist/bin/docs.js +1 -1
- package/dist/bin/drizzle.js +1 -1
- package/dist/bin/ecto.js +1 -1
- package/dist/bin/efcore.js +1 -1
- package/dist/bin/effect.js +1 -1
- package/dist/bin/eloquent.js +1 -1
- package/dist/bin/er.js +1 -1
- package/dist/bin/exposed.js +1 -1
- package/dist/bin/gorm.js +1 -1
- package/dist/bin/hekireki.js +5 -5
- package/dist/bin/kysely.js +1 -1
- package/dist/bin/openapi.js +1 -1
- package/dist/bin/pydantic.js +1 -1
- package/dist/bin/sea-orm.js +1 -1
- package/dist/bin/seed.js +1 -1
- package/dist/bin/sqlalchemy.js +1 -1
- package/dist/bin/typebox.js +1 -1
- package/dist/bin/valibot.js +1 -1
- package/dist/bin/zod-openapi-hono.js +1 -1
- package/dist/bin/zod.js +1 -1
- package/dist/{bin-BcqPhgiX.js → bin-i7iQQ-V-.js} +3295 -2428
- package/dist/case-D3JCqTLI.js +54 -0
- package/dist/{discover-BKlX-LLS.js → discover-CLLycWcR.js} +3 -3
- package/dist/{emit-BSQAIFiF.js → emit-DsCT5gLu.js} +5 -2
- package/dist/{errors-fAN1Og-7.js → errors-CRIxtr-P.js} +88 -26
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/dist/{migrate-BS6d_8aA.js → migrate-1xVrRUlb.js} +77 -21
- package/dist/{native-engine-DcZUEqO8.js → native-engine-DeQEnJxi.js} +145 -30
- package/dist/{plan-BMypogKp.js → plan-8rsBBJcs.js} +1294 -768
- package/dist/{run-BYq7BEVd.js → run-DbZYkHVI.js} +235 -128
- package/dist/{schema-B7beUJic.js → schema-CyJTUCF1.js} +54 -182
- package/dist/{sql-BwL2BncN.js → sql-LMMCiAlf.js} +48 -19
- package/dist/{start-s1j4PqUM.js → start-Ba8wQAD1.js} +1304 -929
- package/dist/studio/assets/client-8R97eV42.js +2 -0
- package/dist/studio/assets/{confirm-dialog-CKCkchiq.js → confirm-dialog-BfMzwGyk.js} +1 -1
- package/dist/studio/assets/docs-C7FtC4KG.js +1 -0
- package/dist/studio/assets/enums._name-DLAiEdMc.js +1 -0
- package/dist/studio/assets/{field-icon-C1wSRFKO.js → field-icon-LVK7vIIn.js} +1 -1
- package/dist/studio/assets/index-T46k2NyA.js +15 -0
- package/dist/studio/assets/{labels-B5gZ2suK.js → labels-Bn-KWaMa.js} +2 -2
- package/dist/studio/assets/{marked-text-B27DfhKS.js → marked-text-BdU5_qSK.js} +1 -1
- package/dist/studio/assets/models._name-BKzAVcW3.js +1 -0
- package/dist/studio/assets/{monaco-DRYJTcJl.js → monaco-33yj-p0X.js} +1 -1
- package/dist/studio/assets/prisma-DqpMbUwJ.js +2 -0
- package/dist/studio/assets/result-table-PN64JbsN.js +1 -0
- package/dist/studio/assets/{routes-Bl7HRpBJ.js → routes-C1Aze5QM.js} +1 -1
- package/dist/studio/assets/{schema-view-BEdx0cC2.js → schema-view-Bq2_9B5a.js} +4 -4
- package/dist/studio/assets/sql-FZV6GAdV.js +6 -0
- package/dist/studio/assets/{tabs-BpZ0hh1V.js → tabs-GBri63Xo.js} +1 -1
- package/dist/studio/index.html +2 -2
- package/package.json +12 -5
- package/dist/studio/assets/client-C-v3Paav.js +0 -2
- package/dist/studio/assets/docs-CXORFY0Y.js +0 -1
- package/dist/studio/assets/enums._name-BUBU_WMt.js +0 -1
- package/dist/studio/assets/index-DzCcMC-d.js +0 -15
- package/dist/studio/assets/models._name-CQFgPD0T.js +0 -1
- package/dist/studio/assets/prisma-1nDx08Wb.js +0 -2
- package/dist/studio/assets/result-table-0P18Nywh.js +0 -1
- package/dist/studio/assets/sql-GBZZtEKI.js +0 -6
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
import { t as exists } from "./file-DOGnu_Q2.js";
|
|
2
2
|
import { v as DEFAULT_SCHEMA_PATHS, y as prismaSchemaPath } from "./bin/hekireki.js";
|
|
3
|
-
import {
|
|
4
|
-
import { r as emitRaw } from "./emit-
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
3
|
+
import { C as isJoinSideA, I as chunks, a as schemaText, h as backRelation, i as readSchemaFiles, n as parseSchema, t as dialectOf } from "./schema-CyJTUCF1.js";
|
|
4
|
+
import { r as emitRaw } from "./emit-DsCT5gLu.js";
|
|
5
|
+
import { r as lowerFirst } from "./case-D3JCqTLI.js";
|
|
6
|
+
import { a as loadSeedConfig, c as SeedConfigError, d as resolveDatabaseUrl, i as withTypeScriptImports, l as SeedDatabaseError, r as sequenceSql, s as resolveConfigPath, t as makeSeedSql, u as SeedGenerationError } from "./sql-LMMCiAlf.js";
|
|
7
|
+
import { n as discoverClient, r as adapterOf } from "./discover-CLLycWcR.js";
|
|
7
8
|
import { Effect } from "effect";
|
|
8
9
|
import path from "node:path";
|
|
9
10
|
import * as z from "zod";
|
|
@@ -68,7 +69,7 @@ function seedWithClient(input) {
|
|
|
68
69
|
const client = input.client;
|
|
69
70
|
const models = input.entries.filter((entry) => entry.table.kind === "model");
|
|
70
71
|
const joins = input.entries.filter((entry) => entry.table.kind === "join");
|
|
71
|
-
const delegates = new Map(yield* Effect.forEach(models, (entry) => Effect.map(delegateOf(client, entry.table.name), (d) => [entry.table.name, d])));
|
|
72
|
+
const delegates = new Map(yield* Effect.forEach(models, (entry) => Effect.map(delegateOf(client, entry.table.name), (d) => [entry.table.name, d]), { concurrency: 1 }));
|
|
72
73
|
const resets = input.reset ? models.toReversed().flatMap((entry) => delegates.get(entry.table.name)?.deleteMany({}) ?? []) : [];
|
|
73
74
|
const creates = models.flatMap((entry) => chunks(entry.rows, CREATE_CHUNK).flatMap((rows) => delegates.get(entry.table.name)?.createMany({ data: rows }) ?? []));
|
|
74
75
|
const connects = joins.flatMap((entry) => {
|
|
@@ -119,7 +120,8 @@ function uniqueSets(model) {
|
|
|
119
120
|
const singles = model.fields.filter((f) => f.kind !== "object" && (f.isUnique || f.isId)).map((f) => [f.name]);
|
|
120
121
|
const composite = [...model.primaryKey === null ? [] : [model.primaryKey.fields], ...model.uniqueFields];
|
|
121
122
|
const all = [...singles, ...composite];
|
|
122
|
-
|
|
123
|
+
const first = new Map(all.map((set, index) => [set.join(" "), index]).toReversed());
|
|
124
|
+
return all.filter((set, index) => first.get(set.join(" ")) === index);
|
|
123
125
|
}
|
|
124
126
|
/** The scalar fields other models' foreign keys point at, so their values must exist. */
|
|
125
127
|
function referencedFields(models, target) {
|
|
@@ -140,6 +142,7 @@ function makeModelTable(model, models, enums) {
|
|
|
140
142
|
isList: f.isList,
|
|
141
143
|
enumValues: f.kind === "enum" ? enumMembers(enums, f.type) : null
|
|
142
144
|
}));
|
|
145
|
+
const uniqueKeys = new Set(uniques.map((set) => [...set].toSorted().join(" ")));
|
|
143
146
|
const foreignKeys = model.fields.filter(isForeignKeyField).map((f) => {
|
|
144
147
|
const fromFields = f.relationFromFields ?? [];
|
|
145
148
|
const key = [...fromFields].toSorted().join(" ");
|
|
@@ -149,7 +152,7 @@ function makeModelTable(model, models, enums) {
|
|
|
149
152
|
toModel: f.type,
|
|
150
153
|
toFields: f.relationToFields ?? [],
|
|
151
154
|
required: f.isRequired,
|
|
152
|
-
oneToOne:
|
|
155
|
+
oneToOne: uniqueKeys.has(key),
|
|
153
156
|
self: f.type === model.name
|
|
154
157
|
};
|
|
155
158
|
});
|
|
@@ -179,8 +182,9 @@ function joinSide(model, field, column, enums) {
|
|
|
179
182
|
}
|
|
180
183
|
/** Every implicit many-to-many relation once, as the join table Prisma creates for it. */
|
|
181
184
|
function makeJoinTables(models, enums) {
|
|
185
|
+
const byName = new Map(models.map((m) => [m.name, m]));
|
|
182
186
|
return models.flatMap((model) => model.fields.filter(isImplicitListField).flatMap((field) => {
|
|
183
|
-
const other =
|
|
187
|
+
const other = byName.get(field.type);
|
|
184
188
|
const inverse = backRelation(field, model, models);
|
|
185
189
|
if (other === void 0 || inverse === void 0 || !inverse.isList) return [];
|
|
186
190
|
if (!isJoinSideA(field, model, inverse)) return [];
|
|
@@ -263,7 +267,8 @@ function makeSeedPlan(datamodel) {
|
|
|
263
267
|
const tables = datamodel.models.map((model) => makeModelTable(model, datamodel.models, datamodel.enums));
|
|
264
268
|
const visited = orderModels(tables);
|
|
265
269
|
if (visited.cycle !== null) return yield* new SeedGenerationError({ message: `Required relations form a cycle: ${visited.cycle}.\n Make one of them optional so the seeder can insert the models in order.` });
|
|
266
|
-
|
|
270
|
+
const byName = Map.groupBy(tables, (table) => table.name);
|
|
271
|
+
return [...visited.order.flatMap((name) => byName.get(name) ?? []), ...makeJoinTables(datamodel.models, datamodel.enums)];
|
|
267
272
|
});
|
|
268
273
|
}
|
|
269
274
|
// src/seed/values.ts
|
|
@@ -377,9 +382,12 @@ function makeText(faker, name) {
|
|
|
377
382
|
max: 3
|
|
378
383
|
}) : match[1](faker);
|
|
379
384
|
}
|
|
385
|
+
function declaredLength(native, stringLength) {
|
|
386
|
+
if (native.name === null) return stringLength ?? void 0;
|
|
387
|
+
return /^(Var|N|NVar)?Char$/u.test(native.name) ? native.args[0] : void 0;
|
|
388
|
+
}
|
|
380
389
|
function maxLength(field, rule, stringLength) {
|
|
381
|
-
const
|
|
382
|
-
const bounds = [native.name === null ? stringLength ?? void 0 : /^(Var|N|NVar)?Char$/u.test(native.name) ? native.args[0] : void 0, typeof rule.length === "number" ? rule.length : rule.length?.max].filter((n) => n !== void 0 && n > 0);
|
|
390
|
+
const bounds = [declaredLength(nativeTypeOf(field), stringLength), typeof rule.length === "number" ? rule.length : rule.length?.max].filter((n) => n !== void 0 && n > 0);
|
|
383
391
|
return bounds.length === 0 ? null : Math.min(...bounds);
|
|
384
392
|
}
|
|
385
393
|
function makeString(faker, field, rule, bounds) {
|
|
@@ -387,31 +395,55 @@ function makeString(faker, field, rule, bounds) {
|
|
|
387
395
|
const limit = maxLength(field, rule, bounds.stringLength ?? null);
|
|
388
396
|
return limit === null ? text : text.slice(0, limit);
|
|
389
397
|
}
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
const native = nativeTypeOf(field);
|
|
393
|
-
const natural = /^age$/u.test(name) ? {
|
|
398
|
+
const INT_RANGES$1 = [
|
|
399
|
+
[/^age$/u, {
|
|
394
400
|
min: 18,
|
|
395
401
|
max: 90
|
|
396
|
-
}
|
|
402
|
+
}],
|
|
403
|
+
[/^year$/u, {
|
|
397
404
|
min: 1970,
|
|
398
405
|
max: 2030
|
|
399
|
-
}
|
|
406
|
+
}],
|
|
407
|
+
[/^(qty|quantity)$/u, {
|
|
400
408
|
min: 1,
|
|
401
409
|
max: 20
|
|
402
|
-
}
|
|
410
|
+
}],
|
|
411
|
+
[/^(rating|stars)$/u, {
|
|
403
412
|
min: 1,
|
|
404
413
|
max: 5
|
|
405
|
-
}
|
|
414
|
+
}],
|
|
415
|
+
[/^(price|amount|total|balance|salary|cost|fee)$/u, {
|
|
406
416
|
min: 0,
|
|
407
417
|
max: 1e5
|
|
408
|
-
}
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
418
|
+
}]
|
|
419
|
+
];
|
|
420
|
+
const NATIVE_INT_RANGES = /* @__PURE__ */ new Map([["TinyInt", {
|
|
421
|
+
min: 0,
|
|
422
|
+
max: 127
|
|
423
|
+
}], ["SmallInt", {
|
|
424
|
+
min: 0,
|
|
425
|
+
max: 32767
|
|
426
|
+
}]]);
|
|
427
|
+
const FLOAT_RANGES = [
|
|
428
|
+
[/^(lat|latitude)$/u, {
|
|
429
|
+
min: -90,
|
|
430
|
+
max: 90
|
|
431
|
+
}],
|
|
432
|
+
[/^(lng|lon|long|longitude)$/u, {
|
|
433
|
+
min: -180,
|
|
434
|
+
max: 180
|
|
435
|
+
}],
|
|
436
|
+
[/^(price|amount|total|balance|salary|cost|fee)$/u, {
|
|
412
437
|
min: 0,
|
|
413
|
-
max:
|
|
414
|
-
}
|
|
438
|
+
max: 1e4
|
|
439
|
+
}]
|
|
440
|
+
];
|
|
441
|
+
function rangeByName(ranges, name) {
|
|
442
|
+
return ranges.find(([pattern]) => pattern.test(name))?.[1];
|
|
443
|
+
}
|
|
444
|
+
function intRange(field, rule) {
|
|
445
|
+
const native = nativeTypeOf(field);
|
|
446
|
+
const natural = rangeByName(INT_RANGES$1, normalizedName(field.name)) ?? (native.name === null ? void 0 : NATIVE_INT_RANGES.get(native.name)) ?? {
|
|
415
447
|
min: 0,
|
|
416
448
|
max: 1e3
|
|
417
449
|
};
|
|
@@ -421,17 +453,7 @@ function intRange(field, rule) {
|
|
|
421
453
|
};
|
|
422
454
|
}
|
|
423
455
|
function floatRange(field, rule) {
|
|
424
|
-
const
|
|
425
|
-
const natural = /^(lat|latitude)$/u.test(name) ? {
|
|
426
|
-
min: -90,
|
|
427
|
-
max: 90
|
|
428
|
-
} : /^(lng|lon|long|longitude)$/u.test(name) ? {
|
|
429
|
-
min: -180,
|
|
430
|
-
max: 180
|
|
431
|
-
} : /^(price|amount|total|balance|salary|cost|fee)$/u.test(name) ? {
|
|
432
|
-
min: 0,
|
|
433
|
-
max: 1e4
|
|
434
|
-
} : {
|
|
456
|
+
const natural = rangeByName(FLOAT_RANGES, normalizedName(field.name)) ?? {
|
|
435
457
|
min: 0,
|
|
436
458
|
max: 1e3
|
|
437
459
|
};
|
|
@@ -473,15 +495,29 @@ function makeJson(faker) {
|
|
|
473
495
|
})
|
|
474
496
|
};
|
|
475
497
|
}
|
|
498
|
+
function makeEnum(faker, enumValues) {
|
|
499
|
+
return enumValues === null || enumValues.length === 0 ? null : faker.helpers.arrayElement(enumValues).name;
|
|
500
|
+
}
|
|
501
|
+
/** An id with no bounds set counts up from 1 with the row. */
|
|
502
|
+
function isCountedId(field, rule) {
|
|
503
|
+
return field.isId && rule.min === void 0 && rule.max === void 0;
|
|
504
|
+
}
|
|
505
|
+
function makeBytes(faker) {
|
|
506
|
+
return Uint8Array.from(Buffer.from(faker.string.hexadecimal({
|
|
507
|
+
length: 32,
|
|
508
|
+
prefix: "",
|
|
509
|
+
casing: "lower"
|
|
510
|
+
}), "hex"));
|
|
511
|
+
}
|
|
476
512
|
/** One scalar of the field's type, honouring its native type, name and the rule's bounds. */
|
|
477
513
|
function makeScalar(faker, field, enumValues, rule, bounds, index) {
|
|
478
|
-
if (field.kind === "enum") return
|
|
514
|
+
if (field.kind === "enum") return makeEnum(faker, enumValues);
|
|
479
515
|
switch (field.type) {
|
|
480
516
|
case "String": return makeString(faker, field, rule, bounds);
|
|
481
|
-
case "Int": return field
|
|
517
|
+
case "Int": return isCountedId(field, rule) ? index + 1 : faker.number.int(intRange(field, rule));
|
|
482
518
|
case "BigInt": {
|
|
483
519
|
const range = intRange(field, rule);
|
|
484
|
-
return field
|
|
520
|
+
return isCountedId(field, rule) ? BigInt(index + 1) : faker.number.bigInt({
|
|
485
521
|
min: range.min,
|
|
486
522
|
max: range.max
|
|
487
523
|
});
|
|
@@ -494,11 +530,7 @@ function makeScalar(faker, field, enumValues, rule, bounds, index) {
|
|
|
494
530
|
case "Boolean": return faker.datatype.boolean();
|
|
495
531
|
case "DateTime": return makeDateTime(faker, field, rule, bounds);
|
|
496
532
|
case "Json": return makeJson(faker);
|
|
497
|
-
case "Bytes": return
|
|
498
|
-
length: 32,
|
|
499
|
-
prefix: "",
|
|
500
|
-
casing: "lower"
|
|
501
|
-
}), "hex"));
|
|
533
|
+
case "Bytes": return makeBytes(faker);
|
|
502
534
|
default: return makeString(faker, field, rule, bounds);
|
|
503
535
|
}
|
|
504
536
|
}
|
|
@@ -527,22 +559,27 @@ function makeFieldValue(input) {
|
|
|
527
559
|
});
|
|
528
560
|
const rule = input.rule === void 0 || typeof input.rule === "function" ? {} : input.rule;
|
|
529
561
|
if (rule.value !== void 0) return rule.value;
|
|
530
|
-
if (!field.isRequired && !field.isList)
|
|
531
|
-
const nullRate = rule.nullRate ?? bounds.nullRate ?? 0;
|
|
532
|
-
if (nullRate > 0 && faker.datatype.boolean({ probability: nullRate })) return null;
|
|
533
|
-
}
|
|
562
|
+
if (!field.isRequired && !field.isList && drawsNull(faker, rule, bounds)) return null;
|
|
534
563
|
const choices = rule.values !== void 0 && rule.values.length > 0 ? rule.values : null;
|
|
535
|
-
if (
|
|
536
|
-
if (
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
return
|
|
564
|
+
if (field.isList) return makeList(input, rule, choices);
|
|
565
|
+
if (choices !== null) return faker.helpers.arrayElement(choices);
|
|
566
|
+
return (fieldDefault(field)?.name === "autoincrement" ? autoincrementValue(field, index) : generatedDefault(faker, field, rule, bounds)) ?? makeScalar(faker, field, enumValues, rule, bounds, index);
|
|
567
|
+
}
|
|
568
|
+
/** Whether an optional field is left null, as often as the rule's or the config's rate says. */
|
|
569
|
+
function drawsNull(faker, rule, bounds) {
|
|
570
|
+
const nullRate = rule.nullRate ?? bounds.nullRate ?? 0;
|
|
571
|
+
return nullRate > 0 && faker.datatype.boolean({ probability: nullRate });
|
|
572
|
+
}
|
|
573
|
+
function autoincrementValue(field, index) {
|
|
574
|
+
return field.type === "BigInt" ? BigInt(index + 1) : index + 1;
|
|
575
|
+
}
|
|
576
|
+
/** A list field: picked from the rule's values, or scalars made one by one. */
|
|
577
|
+
function makeList(input, rule, choices) {
|
|
578
|
+
const { faker, field, enumValues, bounds, index } = input;
|
|
579
|
+
const length = listLength(faker, rule);
|
|
580
|
+
if (choices !== null) return faker.helpers.arrayElements(choices, length);
|
|
581
|
+
const { min: _min, max: _max, ...element } = rule;
|
|
582
|
+
return Array.from({ length }, () => makeScalar(faker, field, enumValues, element, bounds, index));
|
|
546
583
|
}
|
|
547
584
|
// src/seed/validate.ts
|
|
548
585
|
const INT_RANGES = {
|
|
@@ -621,8 +658,9 @@ function fieldSchema(field, enumValues) {
|
|
|
621
658
|
}
|
|
622
659
|
/** The zod schema a whole row of the table must pass, one field per insertable column. */
|
|
623
660
|
function rowSchema(table) {
|
|
661
|
+
const fields = new Map(table.model.fields.map((f) => [f.name, f]));
|
|
624
662
|
return z.object(Object.fromEntries(table.columns.flatMap((column) => {
|
|
625
|
-
const field =
|
|
663
|
+
const field = fields.get(column.field);
|
|
626
664
|
return field === void 0 ? [] : [[column.field, fieldSchema(field, column.enumValues)]];
|
|
627
665
|
})));
|
|
628
666
|
}
|
|
@@ -749,26 +787,32 @@ function relatedCounts(context, table, field, rows) {
|
|
|
749
787
|
return rows.map((row) => children.get(JSON.stringify(to.map((t) => keyOf(row[t] ?? null))))?.length ?? 0);
|
|
750
788
|
}
|
|
751
789
|
/** Every relation rule against the rows made: each row must have between `min` and `max` related rows. */
|
|
790
|
+
/** The rows of one model with too few or too many related rows, one line each. */
|
|
791
|
+
function modelProblems(context, name, relations) {
|
|
792
|
+
const table = modelTable(context.tables, name);
|
|
793
|
+
if (table === void 0) return [];
|
|
794
|
+
const rows = context.rowsByModel.get(name) ?? [];
|
|
795
|
+
const fields = new Map(table.model.fields.map((f) => [f.name, f]));
|
|
796
|
+
return Object.entries(relations).flatMap(([fieldName, bound]) => {
|
|
797
|
+
const field = fields.get(fieldName);
|
|
798
|
+
if (field === void 0) return [];
|
|
799
|
+
return relatedCounts(context, table, field, rows).flatMap((count, index) => {
|
|
800
|
+
const row = rows[index];
|
|
801
|
+
if (row === void 0) return [];
|
|
802
|
+
return boundProblem(`${labelOf(table, index, row)}.${fieldName}: ${count} row${count === 1 ? "" : "s"}`, count, bound, `models.${name}.relations.${fieldName}`);
|
|
803
|
+
});
|
|
804
|
+
});
|
|
805
|
+
}
|
|
806
|
+
/** Why a count lies outside its bound, or nothing when it lies inside. */
|
|
807
|
+
function boundProblem(where, count, bound, path) {
|
|
808
|
+
if (bound.min !== void 0 && count < bound.min) return [`${where}, at least ${bound.min} expected (${path}.min)`];
|
|
809
|
+
if (bound.max !== void 0 && count > bound.max) return [`${where}, at most ${bound.max} expected (${path}.max)`];
|
|
810
|
+
return [];
|
|
811
|
+
}
|
|
752
812
|
/** Every relation rule against the rows made: each row must have between `min` and `max` related rows. */
|
|
753
813
|
function checkCardinality(context) {
|
|
754
814
|
return Effect.gen(function* () {
|
|
755
|
-
const problems = Object.entries(context.config.models).flatMap(([name, rule]) => {
|
|
756
|
-
const table = modelTable(context.tables, name);
|
|
757
|
-
const rows = context.rowsByModel.get(name) ?? [];
|
|
758
|
-
if (table === void 0) return [];
|
|
759
|
-
return Object.entries(rule.relations ?? {}).flatMap(([fieldName, bound]) => {
|
|
760
|
-
const field = table.model.fields.find((f) => f.name === fieldName);
|
|
761
|
-
if (field === void 0) return [];
|
|
762
|
-
return relatedCounts(context, table, field, rows).flatMap((count, index) => {
|
|
763
|
-
const row = rows[index];
|
|
764
|
-
if (row === void 0) return [];
|
|
765
|
-
const where = `${labelOf(table, index, row)}.${fieldName}: ${count} row${count === 1 ? "" : "s"}`;
|
|
766
|
-
if (bound.min !== void 0 && count < bound.min) return [`${where}, at least ${bound.min} expected (models.${name}.relations.${fieldName}.min)`];
|
|
767
|
-
if (bound.max !== void 0 && count > bound.max) return [`${where}, at most ${bound.max} expected (models.${name}.relations.${fieldName}.max)`];
|
|
768
|
-
return [];
|
|
769
|
-
});
|
|
770
|
-
});
|
|
771
|
-
});
|
|
815
|
+
const problems = Object.entries(context.config.models).flatMap(([name, rule]) => modelProblems(context, name, rule.relations ?? {}));
|
|
772
816
|
if (problems.length > 0) {
|
|
773
817
|
const shown = problems.slice(0, 20);
|
|
774
818
|
const more = problems.length - shown.length;
|
|
@@ -794,7 +838,7 @@ function listedPairsOf(own, other, index, given, row, partners) {
|
|
|
794
838
|
const links = given[own.field];
|
|
795
839
|
if (links === void 0 || links === null) return [];
|
|
796
840
|
if (!isSeedList(links)) return yield* new SeedGenerationError({ message: `${own.model} data[${index}].${own.field}: expected a list of ${other.model} ids or keys.` });
|
|
797
|
-
return (yield* Effect.forEach(links, (link) => linkedPartner(own, other, index, partners, link))).map((id) => ({
|
|
841
|
+
return (yield* Effect.forEach(links, (link) => linkedPartner(own, other, index, partners, link), { concurrency: 1 })).map((id) => ({
|
|
798
842
|
[own.column]: row?.[own.idField] ?? null,
|
|
799
843
|
[other.column]: id
|
|
800
844
|
}));
|
|
@@ -806,7 +850,7 @@ function listedPairs(context, own, other) {
|
|
|
806
850
|
const data = context.givenByModel.get(own.model) ?? [];
|
|
807
851
|
const rows = context.rowsByModel.get(own.model) ?? [];
|
|
808
852
|
const partners = context.rowsByModel.get(other.model) ?? [];
|
|
809
|
-
return Effect.forEach(data, (given, index) => listedPairsOf(own, other, index, given, rows[index], partners)).pipe(Effect.map((pairs) => pairs.flat()));
|
|
853
|
+
return Effect.forEach(data, (given, index) => listedPairsOf(own, other, index, given, rows[index], partners), { concurrency: 1 }).pipe(Effect.map((pairs) => pairs.flat()));
|
|
810
854
|
}
|
|
811
855
|
/**
|
|
812
856
|
* Pairs for a join table. Real rows list their own links (`tags: [{ label }]` on a Post row, or
|
|
@@ -858,7 +902,7 @@ function makeJoinRows(context, table) {
|
|
|
858
902
|
function dedupe(pairs) {
|
|
859
903
|
const seen = /* @__PURE__ */ new Set();
|
|
860
904
|
return pairs.filter((pair) => {
|
|
861
|
-
const key = `${keyOf(pair
|
|
905
|
+
const key = `${keyOf(pair["A"] ?? null)} ${keyOf(pair["B"] ?? null)}`;
|
|
862
906
|
if (seen.has(key)) return false;
|
|
863
907
|
seen.add(key);
|
|
864
908
|
return true;
|
|
@@ -892,9 +936,17 @@ function nestField(context, table, field, given, row) {
|
|
|
892
936
|
* was made; queued for the child model, or for this model itself under a self relation.
|
|
893
937
|
*/
|
|
894
938
|
function nestChildren(context, table, given, row) {
|
|
895
|
-
return Effect.forEach(table.model.fields.filter((field) => field.kind === "object" && (field.relationFromFields ?? []).length === 0), (field) => nestField(context, table, field, given, row), {
|
|
939
|
+
return Effect.forEach(table.model.fields.filter((field) => field.kind === "object" && (field.relationFromFields ?? []).length === 0), (field) => nestField(context, table, field, given, row), {
|
|
940
|
+
discard: true,
|
|
941
|
+
concurrency: 1
|
|
942
|
+
});
|
|
896
943
|
}
|
|
897
944
|
// src/seed/generate/parents.ts
|
|
945
|
+
/** Whether a row holds every value of a unique key, each compared as a key. */
|
|
946
|
+
function matchesKey(wanted) {
|
|
947
|
+
const keys = wanted.map(([field, value]) => [field, keyOf(value)]);
|
|
948
|
+
return (row) => keys.every(([field, key]) => keyOf(row[field] ?? null) === key);
|
|
949
|
+
}
|
|
898
950
|
/** The parent the given key values name; fails when no such row exists. */
|
|
899
951
|
function findParent(input, fk, given) {
|
|
900
952
|
return Effect.gen(function* () {
|
|
@@ -918,7 +970,7 @@ function findParentByKey(input, fk, key) {
|
|
|
918
970
|
const { context, table, index, own } = input;
|
|
919
971
|
const parents = fk.self ? own : context.rowsByModel.get(fk.toModel) ?? [];
|
|
920
972
|
const wanted = Object.entries(key);
|
|
921
|
-
const matches = parents.filter((
|
|
973
|
+
const matches = parents.filter(matchesKey(wanted));
|
|
922
974
|
const shown = wanted.map(([field, value]) => `${field} = ${describeValue(value)}`).join(", ");
|
|
923
975
|
if (matches.length === 1) return matches[0] ?? null;
|
|
924
976
|
return yield* new SeedGenerationError({ message: matches.length === 0 ? `${table.name} data[${index}].${fk.field}: no ${fk.toModel} has ${shown}.\n Give ${fk.toModel} that row.` : `${table.name} data[${index}].${fk.field}: ${matches.length} ${fk.toModel} rows have ${shown}; name the row by a unique key.` });
|
|
@@ -936,7 +988,7 @@ function findParentByKey(input, fk, key) {
|
|
|
936
988
|
*/
|
|
937
989
|
function pickParent(input, fk, self) {
|
|
938
990
|
return Effect.gen(function* () {
|
|
939
|
-
const { context,
|
|
991
|
+
const { context, own } = input;
|
|
940
992
|
if (input.given !== null) {
|
|
941
993
|
const reference = input.given[fk.field];
|
|
942
994
|
if (isKeyObject(reference)) return yield* findParentByKey(input, fk, reference);
|
|
@@ -944,20 +996,44 @@ function pickParent(input, fk, self) {
|
|
|
944
996
|
if (!fk.required) return null;
|
|
945
997
|
}
|
|
946
998
|
const parents = fk.self ? own : context.rowsByModel.get(fk.toModel) ?? [];
|
|
947
|
-
if (fk.oneToOne && !fk.self)
|
|
948
|
-
|
|
949
|
-
|
|
950
|
-
|
|
951
|
-
|
|
952
|
-
|
|
953
|
-
|
|
954
|
-
const
|
|
955
|
-
const
|
|
956
|
-
|
|
957
|
-
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
|
|
999
|
+
if (fk.oneToOne && !fk.self) return yield* pickOneToOne(input, fk, parents);
|
|
1000
|
+
return yield* pickCounted(input, fk, self, parents);
|
|
1001
|
+
});
|
|
1002
|
+
}
|
|
1003
|
+
/** The next parent of the pool dealt out to a one-to-one key, which no other row takes. */
|
|
1004
|
+
function pickOneToOne(input, fk, parents) {
|
|
1005
|
+
return Effect.gen(function* () {
|
|
1006
|
+
const { table, pools, slots } = input;
|
|
1007
|
+
const parent = pools.get(fk.field)?.[slots.get(fk.field) ?? 0];
|
|
1008
|
+
if (parent !== void 0) return parent;
|
|
1009
|
+
if (!fk.required) return null;
|
|
1010
|
+
return yield* new SeedGenerationError({ message: `${table.name}.${fk.field} is a one-to-one relation to ${fk.toModel}, so at most ${parents.length} ${table.name} rows can be seeded (${fk.toModel} has ${parents.length}).` });
|
|
1011
|
+
});
|
|
1012
|
+
}
|
|
1013
|
+
/**
|
|
1014
|
+
* The parents a row may point at under the parent model's relation rule: `open` (none above its
|
|
1015
|
+
* `max`) and, of those, `short` (the ones furthest below its `min`).
|
|
1016
|
+
*/
|
|
1017
|
+
function candidates(input, fk, parents) {
|
|
1018
|
+
const { context, table } = input;
|
|
1019
|
+
const bound = parentBound(context, table, fk);
|
|
1020
|
+
const counts = context.linkCounts.get(`${table.name}.${fk.field}`);
|
|
1021
|
+
const counted = (parent) => counts?.get(uniqueKey(parent, fk.toFields)) ?? 0;
|
|
1022
|
+
const { min, max } = bound;
|
|
1023
|
+
const open = max === null ? parents : parents.filter((parent) => counted(parent) < max);
|
|
1024
|
+
const behind = min === null ? [] : open.filter((parent) => counted(parent) < min);
|
|
1025
|
+
const fewest = Math.min(...behind.map(counted));
|
|
1026
|
+
return {
|
|
1027
|
+
bound,
|
|
1028
|
+
open,
|
|
1029
|
+
short: behind.filter((parent) => counted(parent) === fewest)
|
|
1030
|
+
};
|
|
1031
|
+
}
|
|
1032
|
+
/** A parent for a many-to-one key, spread over the parents as the relation rule asks. */
|
|
1033
|
+
function pickCounted(input, fk, self, parents) {
|
|
1034
|
+
return Effect.gen(function* () {
|
|
1035
|
+
const { context, table } = input;
|
|
1036
|
+
const { bound, open, short } = candidates(input, fk, parents);
|
|
961
1037
|
const nullRate = context.config.nullRate ?? 0;
|
|
962
1038
|
if (!fk.required && short.length === 0 && nullRate > 0 && context.faker.datatype.boolean({ probability: nullRate })) return null;
|
|
963
1039
|
if (short.length > 0) return context.faker.helpers.arrayElement(short);
|
|
@@ -989,8 +1065,7 @@ function parentBound(context, table, fk) {
|
|
|
989
1065
|
function claimOf(fk, given, parents) {
|
|
990
1066
|
const reference = given[fk.field];
|
|
991
1067
|
if (isKeyObject(reference)) {
|
|
992
|
-
const
|
|
993
|
-
const parent = parents.find((row) => wanted.every(([field, value]) => keyOf(row[field] ?? null) === keyOf(value)));
|
|
1068
|
+
const parent = parents.find(matchesKey(Object.entries(reference)));
|
|
994
1069
|
return parent === void 0 ? null : uniqueKey(parent, fk.toFields);
|
|
995
1070
|
}
|
|
996
1071
|
return specifies(given, fk) ? fk.fromFields.map((field) => keyOf(given[field] ?? null)).join(" ") : null;
|
|
@@ -1026,8 +1101,9 @@ function normalizeValue(field, value) {
|
|
|
1026
1101
|
function normalizeGiven(input) {
|
|
1027
1102
|
const { given, table } = input;
|
|
1028
1103
|
if (given === null) return {};
|
|
1104
|
+
const fields = new Map(table.model.fields.map((f) => [f.name, f]));
|
|
1029
1105
|
return Object.fromEntries(table.columns.filter((column) => column.field in given).map((column) => {
|
|
1030
|
-
const field =
|
|
1106
|
+
const field = fields.get(column.field);
|
|
1031
1107
|
const value = given[column.field] ?? null;
|
|
1032
1108
|
return [column.field, field === void 0 ? value : normalizeValue(field, value)];
|
|
1033
1109
|
}));
|
|
@@ -1096,7 +1172,7 @@ function defaultEntry(input, column) {
|
|
|
1096
1172
|
/** The columns a real row leaves out, each filled with its default. */
|
|
1097
1173
|
function fillDefaults(input, skip, row) {
|
|
1098
1174
|
return Effect.gen(function* () {
|
|
1099
|
-
const entries = yield* Effect.forEach(input.table.columns.filter((column) => !skip.has(column.field)), (column) => defaultEntry(input, column));
|
|
1175
|
+
const entries = yield* Effect.forEach(input.table.columns.filter((column) => !skip.has(column.field)), (column) => defaultEntry(input, column), { concurrency: 1 });
|
|
1100
1176
|
return {
|
|
1101
1177
|
...row,
|
|
1102
1178
|
...Object.fromEntries(entries)
|
|
@@ -1112,7 +1188,7 @@ function makeRow(input) {
|
|
|
1112
1188
|
const keys = table.foreignKeys.filter((fk) => given !== null || rules[fk.fromFields[0] ?? ""] === void 0);
|
|
1113
1189
|
const skip = /* @__PURE__ */ new Set([...Object.keys(fitted), ...keys.flatMap((fk) => fk.fromFields)]);
|
|
1114
1190
|
const scalars = given === null ? fillScalars(input, table.columns, skip, fitted) : yield* fillDefaults(input, skip, fitted);
|
|
1115
|
-
const parents = yield* Effect.forEach(keys, (fk) => pickParent(input, fk, scalars));
|
|
1191
|
+
const parents = yield* Effect.forEach(keys, (fk) => pickParent(input, fk, scalars), { concurrency: 1 });
|
|
1116
1192
|
const linked = {
|
|
1117
1193
|
...scalars,
|
|
1118
1194
|
...Object.fromEntries(keys.flatMap((fk, k) => fk.fromFields.map((from, i) => {
|
|
@@ -1120,7 +1196,7 @@ function makeRow(input) {
|
|
|
1120
1196
|
return [from, parent === null ? null : parent[fk.toFields[i] ?? ""] ?? null];
|
|
1121
1197
|
})))
|
|
1122
1198
|
};
|
|
1123
|
-
const created = table.model.fields.filter((f) => f.type === "DateTime" &&
|
|
1199
|
+
const created = table.model.fields.filter((f) => f.type === "DateTime" && f.isUpdatedAt !== true && fieldDefault(f)?.name === "now").map((f) => linked[f.name]).filter((v) => v instanceof Date).toSorted((a, b) => b.getTime() - a.getTime())[0];
|
|
1124
1200
|
const updated = created === void 0 ? [] : table.model.fields.filter((f) => f.isUpdatedAt === true && rules[f.name] === void 0 && !(f.name in fitted)).map((f) => {
|
|
1125
1201
|
const end = context.config.dates?.to ?? new Date(created.getTime() + 2592e6);
|
|
1126
1202
|
const to = end > created ? end : created;
|
|
@@ -1136,7 +1212,7 @@ function makeRow(input) {
|
|
|
1136
1212
|
});
|
|
1137
1213
|
}
|
|
1138
1214
|
function violated(row, table, seen) {
|
|
1139
|
-
return table.uniques.filter((fields) => seen.get(fields.join(" "))?.has(uniqueKey(row, fields)));
|
|
1215
|
+
return table.uniques.filter((fields) => seen.get(fields.join(" "))?.has(uniqueKey(row, fields)) === true);
|
|
1140
1216
|
}
|
|
1141
1217
|
/**
|
|
1142
1218
|
* A string field that collided gets the row number worked in, so an email stays an email. A
|
|
@@ -1145,10 +1221,10 @@ function violated(row, table, seen) {
|
|
|
1145
1221
|
*/
|
|
1146
1222
|
function disambiguate(row, fields, table, index) {
|
|
1147
1223
|
const keys = new Set(table.foreignKeys.flatMap((fk) => fk.fromFields));
|
|
1224
|
+
const types = new Map(table.model.fields.map((f) => [f.name, f.type]));
|
|
1148
1225
|
return fields.flatMap((field) => {
|
|
1149
1226
|
const value = row[field];
|
|
1150
|
-
|
|
1151
|
-
if (keys.has(field) || typeof value !== "string" || declared?.type !== "String") return [];
|
|
1227
|
+
if (keys.has(field) || typeof value !== "string" || types.get(field) !== "String") return [];
|
|
1152
1228
|
const at = value.indexOf("@");
|
|
1153
1229
|
const suffix = String(index + 1);
|
|
1154
1230
|
return [[field, at > 0 ? `${value.slice(0, at)}${suffix}${value.slice(at)}` : `${value}-${suffix}`]];
|
|
@@ -1180,10 +1256,16 @@ function attemptRow(input, seen) {
|
|
|
1180
1256
|
}
|
|
1181
1257
|
/** The rule the parent model sets on the list or single field this key is the other side of. */
|
|
1182
1258
|
// src/seed/generate/rules.ts
|
|
1259
|
+
/** What is wrong in the bound a rule sets on one relation field. */
|
|
1260
|
+
function relationRuleProblems(tables, table, name, field, bound) {
|
|
1261
|
+
if (!isLinkField(tables, name, field) && !isParentSide(tables, table, field)) return [`models.${name}.relations.${field}: ${name} has no relation field named ${field} that a rule can bound (a list, or the other side of a key).`];
|
|
1262
|
+
return bound.min !== void 0 && bound.max !== void 0 && bound.min > bound.max ? [`models.${name}.relations.${field}: min ${String(bound.min)} is above max ${String(bound.max)}.`] : [];
|
|
1263
|
+
}
|
|
1183
1264
|
/** What is wrong in one real row and the rows nested in it, as sentences. */
|
|
1184
1265
|
function checkDataRow(tables, table, row, where) {
|
|
1266
|
+
const fields = new Map(table.model.fields.map((f) => [f.name, f]));
|
|
1185
1267
|
return Object.entries(row).flatMap(([name, value]) => {
|
|
1186
|
-
const field =
|
|
1268
|
+
const field = fields.get(name);
|
|
1187
1269
|
if (field === void 0) return [`${where}.${name}: ${table.name} has no field named ${name}.`];
|
|
1188
1270
|
if (field.kind !== "object") return [];
|
|
1189
1271
|
if ((field.relationFromFields ?? []).length > 0) return isKeyObject(value) ? [] : [`${where}.${name}: name the ${field.type} row by a unique key, \`{ id }\` or \`{ email }\`, or give ${(field.relationFromFields ?? []).join(", ")}.`];
|
|
@@ -1214,7 +1296,7 @@ function checkModel(name, rule, tables) {
|
|
|
1214
1296
|
[dated, `models.${name}.fields.${field}: from ${new Date(fieldRule.from ?? 0).toISOString()} is after to ${new Date(fieldRule.to ?? 0).toISOString()}.`]
|
|
1215
1297
|
].filter(([found]) => found).map(([, message]) => message);
|
|
1216
1298
|
});
|
|
1217
|
-
const relations = Object.entries(rule.relations ?? {}).flatMap(([field, bound]) =>
|
|
1299
|
+
const relations = Object.entries(rule.relations ?? {}).flatMap(([field, bound]) => relationRuleProblems(tables, table, name, field, bound));
|
|
1218
1300
|
const count = rule.count !== void 0 && (!Number.isInteger(rule.count) || rule.count < 0) ? [`models.${name}.count: expected a non-negative integer, got ${String(rule.count)}.`] : [];
|
|
1219
1301
|
const exclusive = rule.data !== void 0 && (rule.count !== void 0 || rule.fields !== void 0) ? [`models.${name}: \`data\` and \`count\` / \`fields\` are exclusive. Give the real rows in \`data\`, or let faker make them with \`count\` and \`fields\`, not both.`] : [];
|
|
1220
1302
|
return [
|
|
@@ -1233,19 +1315,46 @@ function checkRules(tables, config) {
|
|
|
1233
1315
|
});
|
|
1234
1316
|
}
|
|
1235
1317
|
// src/seed/generate/index.ts
|
|
1318
|
+
/**
|
|
1319
|
+
* Faker rows come after the real ones: as many as the rule says, or the config's count for a
|
|
1320
|
+
* model with no rule, never more one-to-one children than there are parents left.
|
|
1321
|
+
*/
|
|
1322
|
+
function generatedCount(context, rule, parentCounts) {
|
|
1323
|
+
if (rule?.data !== void 0) return 0;
|
|
1324
|
+
if (rule?.count !== void 0) return rule.count;
|
|
1325
|
+
return context.config.count === null ? 0 : Math.min(context.config.count, ...parentCounts);
|
|
1326
|
+
}
|
|
1327
|
+
/** A parent a real row points at is spoken for; the others are dealt out to the rest. */
|
|
1328
|
+
function parentPools(context, oneToOne, queue) {
|
|
1329
|
+
return new Map(oneToOne.map((fk) => {
|
|
1330
|
+
const parents = context.rowsByModel.get(fk.toModel) ?? [];
|
|
1331
|
+
const claimed = new Set(queue.flatMap((given) => claimOf(fk, given, parents) ?? []));
|
|
1332
|
+
return [fk.field, context.faker.helpers.shuffle(parents.filter((parent) => !claimed.has(uniqueKey(parent, fk.toFields))))];
|
|
1333
|
+
}));
|
|
1334
|
+
}
|
|
1335
|
+
/** A row that claims no parent of a one-to-one key takes the next one of its pool. */
|
|
1336
|
+
function advanceSlots(context, oneToOne, given, slots) {
|
|
1337
|
+
for (const fk of oneToOne) if (given === null || claimOf(fk, given, context.rowsByModel.get(fk.toModel) ?? []) === null) slots.set(fk.field, (slots.get(fk.field) ?? 0) + 1);
|
|
1338
|
+
}
|
|
1339
|
+
/** How many rows point at each parent, by foreign key. */
|
|
1340
|
+
function countLinks(context, table, row) {
|
|
1341
|
+
for (const fk of table.foreignKeys) {
|
|
1342
|
+
const parent = uniqueKey(row, fk.fromFields);
|
|
1343
|
+
if (fk.fromFields.some((field) => row[field] !== null && row[field] !== void 0)) {
|
|
1344
|
+
const counts = context.linkCounts.get(`${table.name}.${fk.field}`) ?? /* @__PURE__ */ new Map();
|
|
1345
|
+
counts.set(parent, (counts.get(parent) ?? 0) + 1);
|
|
1346
|
+
context.linkCounts.set(`${table.name}.${fk.field}`, counts);
|
|
1347
|
+
}
|
|
1348
|
+
}
|
|
1349
|
+
}
|
|
1236
1350
|
function makeModelRows(context, table) {
|
|
1237
1351
|
return Effect.gen(function* () {
|
|
1238
1352
|
const rule = context.config.models[table.name];
|
|
1239
1353
|
const queue = [...rule?.data ?? [], ...context.pending.get(table.name) ?? []];
|
|
1240
1354
|
context.pending.set(table.name, queue);
|
|
1241
1355
|
const oneToOne = table.foreignKeys.filter((fk) => fk.oneToOne && !fk.self);
|
|
1242
|
-
const
|
|
1243
|
-
const
|
|
1244
|
-
const pools = new Map(oneToOne.map((fk) => {
|
|
1245
|
-
const parents = context.rowsByModel.get(fk.toModel) ?? [];
|
|
1246
|
-
const claimed = new Set(queue.flatMap((given) => claimOf(fk, given, parents) ?? []));
|
|
1247
|
-
return [fk.field, context.faker.helpers.shuffle(parents.filter((parent) => !claimed.has(uniqueKey(parent, fk.toFields))))];
|
|
1248
|
-
}));
|
|
1356
|
+
const generated = generatedCount(context, rule, oneToOne.filter((fk) => fk.required).map((fk) => (context.rowsByModel.get(fk.toModel) ?? []).length));
|
|
1357
|
+
const pools = parentPools(context, oneToOne, queue);
|
|
1249
1358
|
const slots = new Map(oneToOne.map((fk) => [fk.field, 0]));
|
|
1250
1359
|
const seen = new Map(table.uniques.map((fields) => [fields.join(" "), /* @__PURE__ */ new Set()]));
|
|
1251
1360
|
const rows = [];
|
|
@@ -1261,16 +1370,9 @@ function makeModelRows(context, table) {
|
|
|
1261
1370
|
slots: new Map(slots)
|
|
1262
1371
|
}, seen);
|
|
1263
1372
|
if (given !== null) yield* nestChildren(context, table, given, row);
|
|
1264
|
-
|
|
1373
|
+
advanceSlots(context, oneToOne, given, slots);
|
|
1265
1374
|
for (const fields of table.uniques) seen.get(fields.join(" "))?.add(uniqueKey(row, fields));
|
|
1266
|
-
|
|
1267
|
-
const parent = uniqueKey(row, fk.fromFields);
|
|
1268
|
-
if (fk.fromFields.some((field) => row[field] !== null && row[field] !== void 0)) {
|
|
1269
|
-
const counts = context.linkCounts.get(`${table.name}.${fk.field}`) ?? /* @__PURE__ */ new Map();
|
|
1270
|
-
counts.set(parent, (counts.get(parent) ?? 0) + 1);
|
|
1271
|
-
context.linkCounts.set(`${table.name}.${fk.field}`, counts);
|
|
1272
|
-
}
|
|
1273
|
-
}
|
|
1375
|
+
countLinks(context, table, row);
|
|
1274
1376
|
rows.push(row);
|
|
1275
1377
|
}
|
|
1276
1378
|
context.givenByModel.set(table.name, queue);
|
|
@@ -1321,7 +1423,7 @@ function generateSeedRows(input) {
|
|
|
1321
1423
|
givenByModel: /* @__PURE__ */ new Map(),
|
|
1322
1424
|
linkCounts: /* @__PURE__ */ new Map()
|
|
1323
1425
|
};
|
|
1324
|
-
const entries = yield* Effect.forEach(input.tables, (table) => makeTableRows(context, rowsByModel, table));
|
|
1426
|
+
const entries = yield* Effect.forEach(input.tables, (table) => makeTableRows(context, rowsByModel, table), { concurrency: 1 });
|
|
1325
1427
|
yield* checkCardinality(context);
|
|
1326
1428
|
return entries;
|
|
1327
1429
|
});
|
|
@@ -1344,7 +1446,7 @@ function resolveSeedConfig(config) {
|
|
|
1344
1446
|
return {
|
|
1345
1447
|
schema: config.schema ?? null,
|
|
1346
1448
|
seed: config.seed ?? null,
|
|
1347
|
-
locale: config.locale
|
|
1449
|
+
locale: locales(config.locale),
|
|
1348
1450
|
count: config.count ?? null,
|
|
1349
1451
|
nullRate: config.nullRate ?? null,
|
|
1350
1452
|
dates: window(config.dates),
|
|
@@ -1355,6 +1457,10 @@ function resolveSeedConfig(config) {
|
|
|
1355
1457
|
models: config.models ?? {}
|
|
1356
1458
|
};
|
|
1357
1459
|
}
|
|
1460
|
+
function locales(locale) {
|
|
1461
|
+
if (locale === void 0) return null;
|
|
1462
|
+
return typeof locale === "string" ? [locale] : locale;
|
|
1463
|
+
}
|
|
1358
1464
|
// src/seed/run.ts
|
|
1359
1465
|
/**
|
|
1360
1466
|
* The schema as named (resolved from `base`), else the one Prisma reads in the working directory
|
|
@@ -1402,9 +1508,10 @@ function makeFaker(seed, locales) {
|
|
|
1402
1508
|
* per-model counts of the config included; models given as `data` keep their rows.
|
|
1403
1509
|
*/
|
|
1404
1510
|
function withOverrides(config, overrides) {
|
|
1405
|
-
const
|
|
1511
|
+
const { count } = overrides;
|
|
1512
|
+
const models = count === null ? config.models : Object.fromEntries(Object.entries(config.models ?? {}).map(([name, rule]) => [name, rule.data === void 0 ? {
|
|
1406
1513
|
...rule,
|
|
1407
|
-
count
|
|
1514
|
+
count
|
|
1408
1515
|
} : rule]));
|
|
1409
1516
|
return {
|
|
1410
1517
|
...config,
|