evikit 2.5.3 → 2.6.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/db.d.ts +10 -3
- package/package.json +1 -1
- package/sqlite.js +525 -496
package/db.d.ts
CHANGED
|
@@ -20,8 +20,15 @@ export type Db<
|
|
|
20
20
|
| InferOutput<Schema[Table]["entries"][K]>[];
|
|
21
21
|
}): void;
|
|
22
22
|
|
|
23
|
-
|
|
24
|
-
|
|
23
|
+
select<
|
|
24
|
+
W extends Partial<Join<R, Table>> & {
|
|
25
|
+
where?: {
|
|
26
|
+
[K in keyof Schema[Table]["entries"]]?:
|
|
27
|
+
| InferOutput<Schema[Table]["entries"][K]>
|
|
28
|
+
| InferOutput<Schema[Table]["entries"][K]>[];
|
|
29
|
+
};
|
|
30
|
+
},
|
|
31
|
+
>(
|
|
25
32
|
props?: W,
|
|
26
33
|
): Select<Schema, R, F, Table, W>[];
|
|
27
34
|
|
|
@@ -213,7 +220,7 @@ type Select<
|
|
|
213
220
|
: never
|
|
214
221
|
: never;
|
|
215
222
|
}
|
|
216
|
-
:
|
|
223
|
+
: InferOutput<Schema[Table]>;
|
|
217
224
|
|
|
218
225
|
// https://www.geeksforgeeks.org/typescript/union-type-to-intersection-type-in-typescript/
|
|
219
226
|
type UnionToIntersection<U> = (
|
package/package.json
CHANGED
package/sqlite.js
CHANGED
|
@@ -1,496 +1,525 @@
|
|
|
1
|
-
import envPaths from "env-paths";
|
|
2
|
-
import { fail, ok } from "node:assert/strict";
|
|
3
|
-
import { mkdirSync } from "node:fs";
|
|
4
|
-
import { DatabaseSync } from "node:sqlite";
|
|
5
|
-
import { getMetadata, parse } from "valibot";
|
|
6
|
-
|
|
7
|
-
import { flatten, relations } from "./db.js";
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
* @typedef Column
|
|
11
|
-
* @property {string} name
|
|
12
|
-
* @property {0 | 1} notnull
|
|
13
|
-
* @property {0 | 1} pk
|
|
14
|
-
* @property {"INTEGER" | "TEXT"} type
|
|
15
|
-
*/
|
|
16
|
-
|
|
17
|
-
/** @type {typeof import("./sqlite.d.ts").sqlite} */
|
|
18
|
-
export function sqlite({ pkg, schema }) {
|
|
19
|
-
const data = pkg
|
|
20
|
-
? (() => {
|
|
21
|
-
const { data } = envPaths(pkg.name);
|
|
22
|
-
mkdirSync(data, { recursive: true });
|
|
23
|
-
return data;
|
|
24
|
-
})()
|
|
25
|
-
: undefined;
|
|
26
|
-
const database = new DatabaseSync(data ? `${data}/db.sqlite3` : ":memory:", {
|
|
27
|
-
timeout: 1000,
|
|
28
|
-
});
|
|
29
|
-
const flat = flatten(relations(schema));
|
|
30
|
-
|
|
31
|
-
const haveTables = /** @type {{ name: string }[]} */ (
|
|
32
|
-
database
|
|
33
|
-
.prepare(
|
|
34
|
-
"select * from sqlite_schema where type='table' and name not like 'sqlite_%'",
|
|
35
|
-
)
|
|
36
|
-
.all()
|
|
37
|
-
).map((table) => {
|
|
38
|
-
const columns = /** @type {Column[]} */ (
|
|
39
|
-
database.prepare(`pragma table_info(${table.name})`).all()
|
|
40
|
-
);
|
|
41
|
-
const foreignKeys =
|
|
42
|
-
/** @type {{ from: string, on_delete: "CASCADE" | "SET NULL", on_update: "CASCADE" | "SET NULL", table: string, to: string, }[]} */ (
|
|
43
|
-
database.prepare(`pragma foreign_key_list(${table.name})`).all()
|
|
44
|
-
);
|
|
45
|
-
return {
|
|
46
|
-
columns,
|
|
47
|
-
foreignKeys,
|
|
48
|
-
indices: /** @type {{ name: string }[]} */ (
|
|
49
|
-
database.prepare(`pragma index_list(${table.name})`).all()
|
|
50
|
-
)
|
|
51
|
-
.filter(({ name }) => !/^sqlite_/.test(name))
|
|
52
|
-
.map((index) => ({
|
|
53
|
-
...index,
|
|
54
|
-
info: /** @type {{ name: string; }} */ (
|
|
55
|
-
database.prepare(`pragma index_info(${index.name})`).get()
|
|
56
|
-
),
|
|
57
|
-
})),
|
|
58
|
-
table,
|
|
59
|
-
};
|
|
60
|
-
});
|
|
61
|
-
|
|
62
|
-
const wantTables = Object.entries(schema).map(([name, { entries }]) => ({
|
|
63
|
-
columns: Object.entries(entries).map(([name, maybeEntry]) => {
|
|
64
|
-
const entry =
|
|
65
|
-
/** @type {import("valibot").BaseSchema<undefined, undefined, any>} */ (
|
|
66
|
-
maybeEntry
|
|
67
|
-
);
|
|
68
|
-
const pk = /** @type {{ primary?: true }} */ (getMetadata(entry)).primary
|
|
69
|
-
? 1
|
|
70
|
-
: 0;
|
|
71
|
-
return {
|
|
72
|
-
name,
|
|
73
|
-
notnull: !pk && entry.expects.includes("undefined") ? 0 : 1,
|
|
74
|
-
pk,
|
|
75
|
-
type:
|
|
76
|
-
entry.expects == "string" || entry.expects == "(string | undefined)"
|
|
77
|
-
? "TEXT"
|
|
78
|
-
: entry.expects == "number" ||
|
|
79
|
-
entry.expects == "(number | undefined)"
|
|
80
|
-
? "INTEGER"
|
|
81
|
-
: entry.expects == "Uint8Array" ||
|
|
82
|
-
entry.expects == "(Uint8Array | undefined)"
|
|
83
|
-
? "BLOB"
|
|
84
|
-
: fail(name),
|
|
85
|
-
};
|
|
86
|
-
}),
|
|
87
|
-
foreignKeys: Object.entries(entries)
|
|
88
|
-
.map(([name, maybeEntry]) => {
|
|
89
|
-
const entry =
|
|
90
|
-
/** @type {import("valibot").BaseSchema<undefined, undefined, any>} */ (
|
|
91
|
-
maybeEntry
|
|
92
|
-
);
|
|
93
|
-
|
|
94
|
-
const metadata =
|
|
95
|
-
/** @type {{ key?: string, references?: any } | undefined} */ (
|
|
96
|
-
getMetadata(entry)
|
|
97
|
-
);
|
|
98
|
-
const table = metadata
|
|
99
|
-
? Object.entries(schema)
|
|
100
|
-
.filter(([, value]) => value == metadata.references)
|
|
101
|
-
.find(Boolean)
|
|
102
|
-
: undefined;
|
|
103
|
-
if (!metadata?.references) {
|
|
104
|
-
return;
|
|
105
|
-
}
|
|
106
|
-
ok(table);
|
|
107
|
-
const to = Object.entries(table[1].entries).find(
|
|
108
|
-
([, value]) => value == metadata.key,
|
|
109
|
-
);
|
|
110
|
-
ok(to);
|
|
111
|
-
return {
|
|
112
|
-
from: name,
|
|
113
|
-
on_delete: /undefined/.test(entry.expects) ? "SET NULL" : "CASCADE",
|
|
114
|
-
on_update: /undefined/.test(entry.expects) ? "SET NULL" : "CASCADE",
|
|
115
|
-
table: table[0],
|
|
116
|
-
to: to[0],
|
|
117
|
-
};
|
|
118
|
-
})
|
|
119
|
-
.filter(Boolean)
|
|
120
|
-
.map((x) => {
|
|
121
|
-
ok(x);
|
|
122
|
-
return x;
|
|
123
|
-
}),
|
|
124
|
-
table: { name },
|
|
125
|
-
}));
|
|
126
|
-
|
|
127
|
-
const migration = [];
|
|
128
|
-
|
|
129
|
-
for (const wantTable of wantTables) {
|
|
130
|
-
const haveTable = haveTables.find(
|
|
131
|
-
({ table }) => table.name == wantTable.table.name,
|
|
132
|
-
);
|
|
133
|
-
if (haveTable) {
|
|
134
|
-
for (const { info, name } of haveTable.indices) {
|
|
135
|
-
if (!wantTable.foreignKeys.find(({ from }) => from == info.name)) {
|
|
136
|
-
migration.push(`drop index ${name}`);
|
|
137
|
-
}
|
|
138
|
-
}
|
|
139
|
-
const haveColumns = haveTable.columns;
|
|
140
|
-
const columnsNowMissing = haveColumns.filter(
|
|
141
|
-
(haveColumn) =>
|
|
142
|
-
!wantTable.columns.find(({ name }) => name == haveColumn.name),
|
|
143
|
-
);
|
|
144
|
-
for (const { name, pk } of columnsNowMissing) {
|
|
145
|
-
if (pk) {
|
|
146
|
-
fail(`${haveTable.table.name}.${name}: Can't drop primary key`);
|
|
147
|
-
}
|
|
148
|
-
}
|
|
149
|
-
for (const wantColumn of wantTable.columns) {
|
|
150
|
-
const haveColumn = haveColumns.find(
|
|
151
|
-
({ name }) => name == wantColumn.name,
|
|
152
|
-
);
|
|
153
|
-
|
|
154
|
-
/** @param {Column} column */
|
|
155
|
-
function checkConstraint(column) {
|
|
156
|
-
ok(column);
|
|
157
|
-
ok(haveTable);
|
|
158
|
-
if (column.pk != wantColumn.pk) {
|
|
159
|
-
fail(
|
|
160
|
-
`${haveTable.table.name}.${column.name}: Can't toggle primary key`,
|
|
161
|
-
);
|
|
162
|
-
}
|
|
163
|
-
if (
|
|
164
|
-
!wantColumn.pk && // Without this condition I get an error with real database created by drizzle-kit.
|
|
165
|
-
column.notnull != wantColumn.notnull
|
|
166
|
-
) {
|
|
167
|
-
fail(
|
|
168
|
-
`${haveTable.table.name}.${column.name}: Can't toggle optional`,
|
|
169
|
-
);
|
|
170
|
-
}
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
if (haveColumn) {
|
|
174
|
-
checkConstraint(haveColumn);
|
|
175
|
-
} else {
|
|
176
|
-
const renamedColumn = columnsNowMissing.find(
|
|
177
|
-
({ notnull, type }) =>
|
|
178
|
-
wantColumn.type == type && wantColumn.notnull == notnull,
|
|
179
|
-
);
|
|
180
|
-
if (renamedColumn) {
|
|
181
|
-
checkConstraint(renamedColumn);
|
|
182
|
-
columnsNowMissing.splice(
|
|
183
|
-
columnsNowMissing.indexOf(renamedColumn),
|
|
184
|
-
1,
|
|
185
|
-
);
|
|
186
|
-
migration.push(
|
|
187
|
-
`alter table ${wantTable.table.name} rename column ${renamedColumn.name} to ${wantColumn.name}`,
|
|
188
|
-
);
|
|
189
|
-
} else {
|
|
190
|
-
if (wantColumn.pk) {
|
|
191
|
-
fail(
|
|
192
|
-
`${wantTable.table.name}.${wantColumn.name}: Can't add primary key`,
|
|
193
|
-
);
|
|
194
|
-
}
|
|
195
|
-
migration.push(
|
|
196
|
-
[
|
|
197
|
-
`alter table ${wantTable.table.name} add column`,
|
|
198
|
-
wantColumn.name,
|
|
199
|
-
wantColumn.type,
|
|
200
|
-
wantColumn.notnull ? "not null" : "",
|
|
201
|
-
...wantTable.foreignKeys
|
|
202
|
-
.map(({ from, on_delete, on_update, table, to }) =>
|
|
203
|
-
from == wantColumn.name
|
|
204
|
-
? `references ${table}(${to}) on delete ${on_delete} on update ${on_update}`
|
|
205
|
-
: "",
|
|
206
|
-
)
|
|
207
|
-
.filter(Boolean),
|
|
208
|
-
]
|
|
209
|
-
.filter(Boolean)
|
|
210
|
-
.join(" "),
|
|
211
|
-
);
|
|
212
|
-
}
|
|
213
|
-
}
|
|
214
|
-
|
|
215
|
-
for (const { from } of wantTable.foreignKeys) {
|
|
216
|
-
if (
|
|
217
|
-
from == wantColumn.name &&
|
|
218
|
-
!haveTable.indices.find(({ info }) => info.name == from)
|
|
219
|
-
) {
|
|
220
|
-
migration.push(
|
|
221
|
-
`create index ${wantTable.table.name}_${from} on ${wantTable.table.name}(${from})`,
|
|
222
|
-
);
|
|
223
|
-
}
|
|
224
|
-
}
|
|
225
|
-
}
|
|
226
|
-
for (const missingColumn of columnsNowMissing) {
|
|
227
|
-
migration.push(
|
|
228
|
-
`alter table ${wantTable.table.name} drop column ${missingColumn.name}`,
|
|
229
|
-
);
|
|
230
|
-
}
|
|
231
|
-
} else {
|
|
232
|
-
if (!wantTable.columns.some(({ pk }) => pk)) {
|
|
233
|
-
fail(`${wantTable.table.name} has no primary key`);
|
|
234
|
-
}
|
|
235
|
-
migration.push(
|
|
236
|
-
`create table ${wantTable.table.name} (\n` +
|
|
237
|
-
wantTable.columns
|
|
238
|
-
.map(({ name, notnull, pk, type }) =>
|
|
239
|
-
[
|
|
240
|
-
name,
|
|
241
|
-
type,
|
|
242
|
-
pk ? "primary key" : "",
|
|
243
|
-
!pk && notnull ? "not null" : "",
|
|
244
|
-
...wantTable.foreignKeys
|
|
245
|
-
.map(({ from, on_delete, on_update, table, to }) =>
|
|
246
|
-
from == name
|
|
247
|
-
? `references ${table}(${to}) on delete ${on_delete} on update ${on_update}`
|
|
248
|
-
: "",
|
|
249
|
-
)
|
|
250
|
-
.filter(Boolean),
|
|
251
|
-
]
|
|
252
|
-
.filter(Boolean)
|
|
253
|
-
.join(" "),
|
|
254
|
-
)
|
|
255
|
-
.join(",\n") +
|
|
256
|
-
`)`,
|
|
257
|
-
);
|
|
258
|
-
for (const { from } of wantTable.foreignKeys) {
|
|
259
|
-
migration.push(
|
|
260
|
-
`create index ${wantTable.table.name}_${from} on ${wantTable.table.name}(${from})`,
|
|
261
|
-
);
|
|
262
|
-
}
|
|
263
|
-
}
|
|
264
|
-
}
|
|
265
|
-
|
|
266
|
-
for (const haveTable of haveTables) {
|
|
267
|
-
const wantTable = wantTables.find(
|
|
268
|
-
({ table }) => table.name == haveTable.table.name,
|
|
269
|
-
);
|
|
270
|
-
if (!wantTable) {
|
|
271
|
-
migration.push(`drop table ${haveTable.table.name}`);
|
|
272
|
-
}
|
|
273
|
-
}
|
|
274
|
-
|
|
275
|
-
for (const sql of migration) {
|
|
276
|
-
database.exec(sql);
|
|
277
|
-
}
|
|
278
|
-
|
|
279
|
-
return /** @type {any} */ (
|
|
280
|
-
Object.fromEntries(
|
|
281
|
-
Object.entries(schema).map(([maybeTable]) => {
|
|
282
|
-
const table = /** @type {keyof typeof schema & string} */ (maybeTable);
|
|
283
|
-
|
|
284
|
-
/** @param {{ [K in keyof (typeof schema)[typeof table]["entries"]]?: import("valibot").InferOutput<(typeof schema)[typeof table]["entries"][K]> | import("valibot").InferOutput<(typeof schema)[typeof table]["entries"][K]>[]; }} where */
|
|
285
|
-
function ensureArrayRecord(where) {
|
|
286
|
-
for (const key of Object.keys(where)) {
|
|
287
|
-
if (!Array.isArray(where[key])) {
|
|
288
|
-
Object.assign(where, { [key]: [where[key]] });
|
|
289
|
-
}
|
|
290
|
-
}
|
|
291
|
-
}
|
|
292
|
-
|
|
293
|
-
return [
|
|
294
|
-
table,
|
|
295
|
-
{
|
|
296
|
-
/** @param {{ [K in keyof (typeof schema)[typeof table]["entries"]]?: import("valibot").InferOutput<(typeof schema)[typeof table]["entries"][K]> | import("valibot").InferOutput<(typeof schema)[typeof table]["entries"][K]>[]; }} where */
|
|
297
|
-
delete(where) {
|
|
298
|
-
ensureArrayRecord(where);
|
|
299
|
-
database
|
|
300
|
-
.prepare(
|
|
301
|
-
`delete from ${table} where true\n` +
|
|
302
|
-
Object.entries(where)
|
|
303
|
-
.map(
|
|
304
|
-
([name, values]) =>
|
|
305
|
-
`and ${name} in (${values.map(() => "?").join(", ")})`,
|
|
306
|
-
)
|
|
307
|
-
.join("\n"),
|
|
308
|
-
)
|
|
309
|
-
.run(
|
|
310
|
-
...Object.values(where)
|
|
311
|
-
.flatMap((values) => values)
|
|
312
|
-
.map((value) => value ?? null),
|
|
313
|
-
);
|
|
314
|
-
},
|
|
315
|
-
|
|
316
|
-
/** @param {any} join */
|
|
317
|
-
select(join) {
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
.
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
}
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
let
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
/**
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
const
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
merge(
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
1
|
+
import envPaths from "env-paths";
|
|
2
|
+
import { fail, ok } from "node:assert/strict";
|
|
3
|
+
import { mkdirSync } from "node:fs";
|
|
4
|
+
import { DatabaseSync } from "node:sqlite";
|
|
5
|
+
import { getMetadata, parse } from "valibot";
|
|
6
|
+
|
|
7
|
+
import { flatten, relations } from "./db.js";
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* @typedef Column
|
|
11
|
+
* @property {string} name
|
|
12
|
+
* @property {0 | 1} notnull
|
|
13
|
+
* @property {0 | 1} pk
|
|
14
|
+
* @property {"INTEGER" | "TEXT"} type
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
/** @type {typeof import("./sqlite.d.ts").sqlite} */
|
|
18
|
+
export function sqlite({ pkg, schema }) {
|
|
19
|
+
const data = pkg
|
|
20
|
+
? (() => {
|
|
21
|
+
const { data } = envPaths(pkg.name);
|
|
22
|
+
mkdirSync(data, { recursive: true });
|
|
23
|
+
return data;
|
|
24
|
+
})()
|
|
25
|
+
: undefined;
|
|
26
|
+
const database = new DatabaseSync(data ? `${data}/db.sqlite3` : ":memory:", {
|
|
27
|
+
timeout: 1000,
|
|
28
|
+
});
|
|
29
|
+
const flat = flatten(relations(schema));
|
|
30
|
+
|
|
31
|
+
const haveTables = /** @type {{ name: string }[]} */ (
|
|
32
|
+
database
|
|
33
|
+
.prepare(
|
|
34
|
+
"select * from sqlite_schema where type='table' and name not like 'sqlite_%'",
|
|
35
|
+
)
|
|
36
|
+
.all()
|
|
37
|
+
).map((table) => {
|
|
38
|
+
const columns = /** @type {Column[]} */ (
|
|
39
|
+
database.prepare(`pragma table_info(${table.name})`).all()
|
|
40
|
+
);
|
|
41
|
+
const foreignKeys =
|
|
42
|
+
/** @type {{ from: string, on_delete: "CASCADE" | "SET NULL", on_update: "CASCADE" | "SET NULL", table: string, to: string, }[]} */ (
|
|
43
|
+
database.prepare(`pragma foreign_key_list(${table.name})`).all()
|
|
44
|
+
);
|
|
45
|
+
return {
|
|
46
|
+
columns,
|
|
47
|
+
foreignKeys,
|
|
48
|
+
indices: /** @type {{ name: string }[]} */ (
|
|
49
|
+
database.prepare(`pragma index_list(${table.name})`).all()
|
|
50
|
+
)
|
|
51
|
+
.filter(({ name }) => !/^sqlite_/.test(name))
|
|
52
|
+
.map((index) => ({
|
|
53
|
+
...index,
|
|
54
|
+
info: /** @type {{ name: string; }} */ (
|
|
55
|
+
database.prepare(`pragma index_info(${index.name})`).get()
|
|
56
|
+
),
|
|
57
|
+
})),
|
|
58
|
+
table,
|
|
59
|
+
};
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
const wantTables = Object.entries(schema).map(([name, { entries }]) => ({
|
|
63
|
+
columns: Object.entries(entries).map(([name, maybeEntry]) => {
|
|
64
|
+
const entry =
|
|
65
|
+
/** @type {import("valibot").BaseSchema<undefined, undefined, any>} */ (
|
|
66
|
+
maybeEntry
|
|
67
|
+
);
|
|
68
|
+
const pk = /** @type {{ primary?: true }} */ (getMetadata(entry)).primary
|
|
69
|
+
? 1
|
|
70
|
+
: 0;
|
|
71
|
+
return {
|
|
72
|
+
name,
|
|
73
|
+
notnull: !pk && entry.expects.includes("undefined") ? 0 : 1,
|
|
74
|
+
pk,
|
|
75
|
+
type:
|
|
76
|
+
entry.expects == "string" || entry.expects == "(string | undefined)"
|
|
77
|
+
? "TEXT"
|
|
78
|
+
: entry.expects == "number" ||
|
|
79
|
+
entry.expects == "(number | undefined)"
|
|
80
|
+
? "INTEGER"
|
|
81
|
+
: entry.expects == "Uint8Array" ||
|
|
82
|
+
entry.expects == "(Uint8Array | undefined)"
|
|
83
|
+
? "BLOB"
|
|
84
|
+
: fail(name),
|
|
85
|
+
};
|
|
86
|
+
}),
|
|
87
|
+
foreignKeys: Object.entries(entries)
|
|
88
|
+
.map(([name, maybeEntry]) => {
|
|
89
|
+
const entry =
|
|
90
|
+
/** @type {import("valibot").BaseSchema<undefined, undefined, any>} */ (
|
|
91
|
+
maybeEntry
|
|
92
|
+
);
|
|
93
|
+
|
|
94
|
+
const metadata =
|
|
95
|
+
/** @type {{ key?: string, references?: any } | undefined} */ (
|
|
96
|
+
getMetadata(entry)
|
|
97
|
+
);
|
|
98
|
+
const table = metadata
|
|
99
|
+
? Object.entries(schema)
|
|
100
|
+
.filter(([, value]) => value == metadata.references)
|
|
101
|
+
.find(Boolean)
|
|
102
|
+
: undefined;
|
|
103
|
+
if (!metadata?.references) {
|
|
104
|
+
return;
|
|
105
|
+
}
|
|
106
|
+
ok(table);
|
|
107
|
+
const to = Object.entries(table[1].entries).find(
|
|
108
|
+
([, value]) => value == metadata.key,
|
|
109
|
+
);
|
|
110
|
+
ok(to);
|
|
111
|
+
return {
|
|
112
|
+
from: name,
|
|
113
|
+
on_delete: /undefined/.test(entry.expects) ? "SET NULL" : "CASCADE",
|
|
114
|
+
on_update: /undefined/.test(entry.expects) ? "SET NULL" : "CASCADE",
|
|
115
|
+
table: table[0],
|
|
116
|
+
to: to[0],
|
|
117
|
+
};
|
|
118
|
+
})
|
|
119
|
+
.filter(Boolean)
|
|
120
|
+
.map((x) => {
|
|
121
|
+
ok(x);
|
|
122
|
+
return x;
|
|
123
|
+
}),
|
|
124
|
+
table: { name },
|
|
125
|
+
}));
|
|
126
|
+
|
|
127
|
+
const migration = [];
|
|
128
|
+
|
|
129
|
+
for (const wantTable of wantTables) {
|
|
130
|
+
const haveTable = haveTables.find(
|
|
131
|
+
({ table }) => table.name == wantTable.table.name,
|
|
132
|
+
);
|
|
133
|
+
if (haveTable) {
|
|
134
|
+
for (const { info, name } of haveTable.indices) {
|
|
135
|
+
if (!wantTable.foreignKeys.find(({ from }) => from == info.name)) {
|
|
136
|
+
migration.push(`drop index ${name}`);
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
const haveColumns = haveTable.columns;
|
|
140
|
+
const columnsNowMissing = haveColumns.filter(
|
|
141
|
+
(haveColumn) =>
|
|
142
|
+
!wantTable.columns.find(({ name }) => name == haveColumn.name),
|
|
143
|
+
);
|
|
144
|
+
for (const { name, pk } of columnsNowMissing) {
|
|
145
|
+
if (pk) {
|
|
146
|
+
fail(`${haveTable.table.name}.${name}: Can't drop primary key`);
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
for (const wantColumn of wantTable.columns) {
|
|
150
|
+
const haveColumn = haveColumns.find(
|
|
151
|
+
({ name }) => name == wantColumn.name,
|
|
152
|
+
);
|
|
153
|
+
|
|
154
|
+
/** @param {Column} column */
|
|
155
|
+
function checkConstraint(column) {
|
|
156
|
+
ok(column);
|
|
157
|
+
ok(haveTable);
|
|
158
|
+
if (column.pk != wantColumn.pk) {
|
|
159
|
+
fail(
|
|
160
|
+
`${haveTable.table.name}.${column.name}: Can't toggle primary key`,
|
|
161
|
+
);
|
|
162
|
+
}
|
|
163
|
+
if (
|
|
164
|
+
!wantColumn.pk && // Without this condition I get an error with real database created by drizzle-kit.
|
|
165
|
+
column.notnull != wantColumn.notnull
|
|
166
|
+
) {
|
|
167
|
+
fail(
|
|
168
|
+
`${haveTable.table.name}.${column.name}: Can't toggle optional`,
|
|
169
|
+
);
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
if (haveColumn) {
|
|
174
|
+
checkConstraint(haveColumn);
|
|
175
|
+
} else {
|
|
176
|
+
const renamedColumn = columnsNowMissing.find(
|
|
177
|
+
({ notnull, type }) =>
|
|
178
|
+
wantColumn.type == type && wantColumn.notnull == notnull,
|
|
179
|
+
);
|
|
180
|
+
if (renamedColumn) {
|
|
181
|
+
checkConstraint(renamedColumn);
|
|
182
|
+
columnsNowMissing.splice(
|
|
183
|
+
columnsNowMissing.indexOf(renamedColumn),
|
|
184
|
+
1,
|
|
185
|
+
);
|
|
186
|
+
migration.push(
|
|
187
|
+
`alter table ${wantTable.table.name} rename column ${renamedColumn.name} to ${wantColumn.name}`,
|
|
188
|
+
);
|
|
189
|
+
} else {
|
|
190
|
+
if (wantColumn.pk) {
|
|
191
|
+
fail(
|
|
192
|
+
`${wantTable.table.name}.${wantColumn.name}: Can't add primary key`,
|
|
193
|
+
);
|
|
194
|
+
}
|
|
195
|
+
migration.push(
|
|
196
|
+
[
|
|
197
|
+
`alter table ${wantTable.table.name} add column`,
|
|
198
|
+
wantColumn.name,
|
|
199
|
+
wantColumn.type,
|
|
200
|
+
wantColumn.notnull ? "not null" : "",
|
|
201
|
+
...wantTable.foreignKeys
|
|
202
|
+
.map(({ from, on_delete, on_update, table, to }) =>
|
|
203
|
+
from == wantColumn.name
|
|
204
|
+
? `references ${table}(${to}) on delete ${on_delete} on update ${on_update}`
|
|
205
|
+
: "",
|
|
206
|
+
)
|
|
207
|
+
.filter(Boolean),
|
|
208
|
+
]
|
|
209
|
+
.filter(Boolean)
|
|
210
|
+
.join(" "),
|
|
211
|
+
);
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
for (const { from } of wantTable.foreignKeys) {
|
|
216
|
+
if (
|
|
217
|
+
from == wantColumn.name &&
|
|
218
|
+
!haveTable.indices.find(({ info }) => info.name == from)
|
|
219
|
+
) {
|
|
220
|
+
migration.push(
|
|
221
|
+
`create index ${wantTable.table.name}_${from} on ${wantTable.table.name}(${from})`,
|
|
222
|
+
);
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
for (const missingColumn of columnsNowMissing) {
|
|
227
|
+
migration.push(
|
|
228
|
+
`alter table ${wantTable.table.name} drop column ${missingColumn.name}`,
|
|
229
|
+
);
|
|
230
|
+
}
|
|
231
|
+
} else {
|
|
232
|
+
if (!wantTable.columns.some(({ pk }) => pk)) {
|
|
233
|
+
fail(`${wantTable.table.name} has no primary key`);
|
|
234
|
+
}
|
|
235
|
+
migration.push(
|
|
236
|
+
`create table ${wantTable.table.name} (\n` +
|
|
237
|
+
wantTable.columns
|
|
238
|
+
.map(({ name, notnull, pk, type }) =>
|
|
239
|
+
[
|
|
240
|
+
name,
|
|
241
|
+
type,
|
|
242
|
+
pk ? "primary key" : "",
|
|
243
|
+
!pk && notnull ? "not null" : "",
|
|
244
|
+
...wantTable.foreignKeys
|
|
245
|
+
.map(({ from, on_delete, on_update, table, to }) =>
|
|
246
|
+
from == name
|
|
247
|
+
? `references ${table}(${to}) on delete ${on_delete} on update ${on_update}`
|
|
248
|
+
: "",
|
|
249
|
+
)
|
|
250
|
+
.filter(Boolean),
|
|
251
|
+
]
|
|
252
|
+
.filter(Boolean)
|
|
253
|
+
.join(" "),
|
|
254
|
+
)
|
|
255
|
+
.join(",\n") +
|
|
256
|
+
`)`,
|
|
257
|
+
);
|
|
258
|
+
for (const { from } of wantTable.foreignKeys) {
|
|
259
|
+
migration.push(
|
|
260
|
+
`create index ${wantTable.table.name}_${from} on ${wantTable.table.name}(${from})`,
|
|
261
|
+
);
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
for (const haveTable of haveTables) {
|
|
267
|
+
const wantTable = wantTables.find(
|
|
268
|
+
({ table }) => table.name == haveTable.table.name,
|
|
269
|
+
);
|
|
270
|
+
if (!wantTable) {
|
|
271
|
+
migration.push(`drop table ${haveTable.table.name}`);
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
for (const sql of migration) {
|
|
276
|
+
database.exec(sql);
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
return /** @type {any} */ (
|
|
280
|
+
Object.fromEntries(
|
|
281
|
+
Object.entries(schema).map(([maybeTable]) => {
|
|
282
|
+
const table = /** @type {keyof typeof schema & string} */ (maybeTable);
|
|
283
|
+
|
|
284
|
+
/** @param {{ [K in keyof (typeof schema)[typeof table]["entries"]]?: import("valibot").InferOutput<(typeof schema)[typeof table]["entries"][K]> | import("valibot").InferOutput<(typeof schema)[typeof table]["entries"][K]>[]; }} where */
|
|
285
|
+
function ensureArrayRecord(where) {
|
|
286
|
+
for (const key of Object.keys(where)) {
|
|
287
|
+
if (!Array.isArray(where[key])) {
|
|
288
|
+
Object.assign(where, { [key]: [where[key]] });
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
return [
|
|
294
|
+
table,
|
|
295
|
+
{
|
|
296
|
+
/** @param {{ [K in keyof (typeof schema)[typeof table]["entries"]]?: import("valibot").InferOutput<(typeof schema)[typeof table]["entries"][K]> | import("valibot").InferOutput<(typeof schema)[typeof table]["entries"][K]>[]; }} where */
|
|
297
|
+
delete(where) {
|
|
298
|
+
ensureArrayRecord(where);
|
|
299
|
+
database
|
|
300
|
+
.prepare(
|
|
301
|
+
`delete from ${table} where true\n` +
|
|
302
|
+
Object.entries(where)
|
|
303
|
+
.map(
|
|
304
|
+
([name, values]) =>
|
|
305
|
+
`and ${name} in (${values.map(() => "?").join(", ")})`,
|
|
306
|
+
)
|
|
307
|
+
.join("\n"),
|
|
308
|
+
)
|
|
309
|
+
.run(
|
|
310
|
+
...Object.values(where)
|
|
311
|
+
.flatMap((values) => values)
|
|
312
|
+
.map((value) => value ?? null),
|
|
313
|
+
);
|
|
314
|
+
},
|
|
315
|
+
|
|
316
|
+
/** @param {any} join */
|
|
317
|
+
select(join) {
|
|
318
|
+
if (join?.where) {
|
|
319
|
+
ensureArrayRecord(join.where);
|
|
320
|
+
}
|
|
321
|
+
return select({ database, flat, join, schema, table });
|
|
322
|
+
},
|
|
323
|
+
|
|
324
|
+
/** @param {{ set: Partial<import("valibot").InferOutput<(typeof schema)[typeof table]>>, where: { [K in keyof (typeof schema)[typeof table]["entries"]]?: import("valibot").InferOutput<(typeof schema)[typeof table]["entries"][K]> | import("valibot").InferOutput<(typeof schema)[typeof table]["entries"][K]>[]; } }} props */
|
|
325
|
+
update({ set, where }) {
|
|
326
|
+
ensureArrayRecord(where);
|
|
327
|
+
database
|
|
328
|
+
.prepare(
|
|
329
|
+
`update ${table} set\n` +
|
|
330
|
+
Object.keys(set)
|
|
331
|
+
.map((name) => `${name} = ?`)
|
|
332
|
+
.join(",\n") +
|
|
333
|
+
`\nwhere true\n` +
|
|
334
|
+
Object.entries(where)
|
|
335
|
+
.map(
|
|
336
|
+
([name, values]) =>
|
|
337
|
+
`and ${name} in (${values.map(() => "?").join(", ")})`,
|
|
338
|
+
)
|
|
339
|
+
.join("\n"),
|
|
340
|
+
)
|
|
341
|
+
.run(
|
|
342
|
+
...[
|
|
343
|
+
...Object.values(set).map((value) => value ?? null),
|
|
344
|
+
...Object.values(where)
|
|
345
|
+
.flatMap((values) => values)
|
|
346
|
+
.map((value) => value ?? null),
|
|
347
|
+
],
|
|
348
|
+
);
|
|
349
|
+
},
|
|
350
|
+
|
|
351
|
+
/** @param {any[]} values */
|
|
352
|
+
upsert(...values) {
|
|
353
|
+
ok(schema[table]);
|
|
354
|
+
const columns = Object.entries(schema[table].entries);
|
|
355
|
+
const primaryKey = columns.find(
|
|
356
|
+
([, column]) =>
|
|
357
|
+
/** @type {{ primary?: true }} */ (getMetadata(column))
|
|
358
|
+
.primary,
|
|
359
|
+
);
|
|
360
|
+
ok(primaryKey);
|
|
361
|
+
const query = database.prepare(
|
|
362
|
+
`insert into ${table} (${columns.map(([name]) => name).join(", ")}) values\n` +
|
|
363
|
+
`(${columns.map(() => "?").join(", ")})\n` +
|
|
364
|
+
`on conflict (${primaryKey[0]}) do update set\n` +
|
|
365
|
+
columns.map(([name]) => `${name} = ?`).join(",\n"),
|
|
366
|
+
);
|
|
367
|
+
const outputs = [];
|
|
368
|
+
for (const row of values) {
|
|
369
|
+
const output = parse(schema[table], row);
|
|
370
|
+
const values = columns.map(([name]) => output[name] ?? null);
|
|
371
|
+
query.run(...values, ...values);
|
|
372
|
+
outputs.push(output);
|
|
373
|
+
}
|
|
374
|
+
return outputs;
|
|
375
|
+
},
|
|
376
|
+
},
|
|
377
|
+
];
|
|
378
|
+
}),
|
|
379
|
+
)
|
|
380
|
+
);
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
/** @type {<Schema extends Record<string, import("valibot").ObjectSchema<any, any>>, R extends import("./db.d.ts").Relations<Schema> = import("./db.d.ts").Relations<Schema>, F extends import("./db.d.ts").Flatten<Schema> = import("./db.d.ts").Flatten<Schema>, Table extends keyof F & keyof Schema = keyof F & keyof Schema >(props: { database: DatabaseSync; flat: F; join?: import("./db.d.ts").Join<R, Table> & { where?: { [K in keyof Schema[Table]["entries"]]?: import("valibot").InferOutput<Schema[Table]["entries"][K]> | import("valibot").InferOutput<Schema[Table]["entries"][K]>[]; } }; schema: Schema; table: Table; }) => any[]} */
|
|
384
|
+
function select(props) {
|
|
385
|
+
ok(typeof props.table == "string");
|
|
386
|
+
let alias = `${props.table}_0`;
|
|
387
|
+
let sql = `from ${props.table} ${alias}`;
|
|
388
|
+
|
|
389
|
+
let columns = Object.keys(
|
|
390
|
+
/** @type {any} */ (props.schema)[props.table].entries,
|
|
391
|
+
);
|
|
392
|
+
if (/** @type {any} */ (props.join)?.with) {
|
|
393
|
+
columns = columns.map((name) => `${alias}.${name} ${alias}_${name}`);
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
/** @type {Record<string, { entities: any[]; table: string }>} */
|
|
397
|
+
const aliases = {};
|
|
398
|
+
/**
|
|
399
|
+
* @param {number} unique
|
|
400
|
+
* @param {string} table
|
|
401
|
+
* @param {string} alias
|
|
402
|
+
* @param {{ with: any }} ownJoin // eslint-disable-line @typescript-eslint/no-explicit-any
|
|
403
|
+
*/
|
|
404
|
+
function join(unique, table, alias, ownJoin) {
|
|
405
|
+
if (!aliases[alias]) {
|
|
406
|
+
aliases[alias] = { entities: [], table };
|
|
407
|
+
}
|
|
408
|
+
for (const [name, itsJoin] of ownJoin?.with
|
|
409
|
+
? Object.entries(ownJoin.with)
|
|
410
|
+
: []) {
|
|
411
|
+
const otherFlat = props.flat[table];
|
|
412
|
+
ok(otherFlat);
|
|
413
|
+
const relation = otherFlat[/** @type {keyof typeof otherFlat} */ (name)];
|
|
414
|
+
ok(relation);
|
|
415
|
+
|
|
416
|
+
const { Its, Other, Own } =
|
|
417
|
+
/** @type {{ Its: string, Other: string, Own: string }} */ (
|
|
418
|
+
/** @type {any} */ (relation)
|
|
419
|
+
);
|
|
420
|
+
const otherAlias = `${name}_${unique + 1}`;
|
|
421
|
+
if (!aliases[otherAlias]) {
|
|
422
|
+
aliases[otherAlias] = { entities: [], table: Other };
|
|
423
|
+
}
|
|
424
|
+
columns = [
|
|
425
|
+
...columns,
|
|
426
|
+
|
|
427
|
+
...Object.keys(/** @type {any} */ (props.schema)[Other].entries).map(
|
|
428
|
+
(name) => `${otherAlias}.${name} ${otherAlias}_${name}`,
|
|
429
|
+
),
|
|
430
|
+
];
|
|
431
|
+
sql += `\n left join ${Other} ${otherAlias} on ${otherAlias}.${Its} = ${alias}.${Own}`;
|
|
432
|
+
if (typeof itsJoin == "object") {
|
|
433
|
+
ok(itsJoin);
|
|
434
|
+
join(unique + 1, Other, otherAlias, itsJoin);
|
|
435
|
+
}
|
|
436
|
+
}
|
|
437
|
+
}
|
|
438
|
+
|
|
439
|
+
join(0, props.table, alias, /** @type {any} */ (props.join));
|
|
440
|
+
sql =
|
|
441
|
+
`select ${columns.join(",\n")} ${sql}\nwhere true\n` +
|
|
442
|
+
Object.entries(/** @type {any} */ (props.join)?.where || {})
|
|
443
|
+
.map(
|
|
444
|
+
([name, values]) =>
|
|
445
|
+
`and ${alias}.${name} in (${values.map(() => "?").join(", ")})`,
|
|
446
|
+
)
|
|
447
|
+
.join("\n");
|
|
448
|
+
|
|
449
|
+
const rows = props.database.prepare(sql).all(
|
|
450
|
+
...Object.values(/** @type {any} */ (props.join)?.where || {})
|
|
451
|
+
.flatMap((values) => values)
|
|
452
|
+
.map((value) => value ?? null),
|
|
453
|
+
);
|
|
454
|
+
if (!(/** @type {any} */ (props.join)?.with)) {
|
|
455
|
+
for (const row of rows) {
|
|
456
|
+
for (const name of Object.keys(row)) {
|
|
457
|
+
Object.assign(row, { [name]: row[name] ?? undefined });
|
|
458
|
+
}
|
|
459
|
+
}
|
|
460
|
+
return rows;
|
|
461
|
+
}
|
|
462
|
+
for (const [alias, state] of Object.entries(aliases)) {
|
|
463
|
+
/** @type {Record<string, (typeof state.entities)[0]>} */
|
|
464
|
+
const unique = {};
|
|
465
|
+
for (const row of rows) {
|
|
466
|
+
/** @type {Record<string, any>} */
|
|
467
|
+
const unwrapped = {};
|
|
468
|
+
|
|
469
|
+
let empty = true;
|
|
470
|
+
for (const name of Object.keys(
|
|
471
|
+
/** @type {any} */ (props.schema)[state.table].entries,
|
|
472
|
+
)) {
|
|
473
|
+
unwrapped[name] = row[`${alias}_${name}`] ?? undefined;
|
|
474
|
+
if (unwrapped[name] !== undefined) {
|
|
475
|
+
empty = false;
|
|
476
|
+
}
|
|
477
|
+
}
|
|
478
|
+
if (!empty) {
|
|
479
|
+
unique[JSON.stringify(unwrapped)] = unwrapped;
|
|
480
|
+
}
|
|
481
|
+
}
|
|
482
|
+
state.entities = Object.values(unique);
|
|
483
|
+
}
|
|
484
|
+
|
|
485
|
+
alias = `${props.table}_0`;
|
|
486
|
+
/**
|
|
487
|
+
* @param {number} unique
|
|
488
|
+
* @param {string} table
|
|
489
|
+
* @param {string} alias
|
|
490
|
+
* @param {{ with: any }} ownJoin // eslint-disable-line @typescript-eslint/no-explicit-any
|
|
491
|
+
*/
|
|
492
|
+
function merge(unique, table, alias, ownJoin) {
|
|
493
|
+
ok(aliases[alias]);
|
|
494
|
+
for (const [name, itsJoin] of ownJoin?.with
|
|
495
|
+
? Object.entries(ownJoin.with)
|
|
496
|
+
: []) {
|
|
497
|
+
const otherFlat = props.flat[table];
|
|
498
|
+
ok(otherFlat);
|
|
499
|
+
const relation = otherFlat[/** @type {keyof typeof otherFlat} */ (name)];
|
|
500
|
+
ok(relation);
|
|
501
|
+
|
|
502
|
+
const { Its, Other, Own, type } =
|
|
503
|
+
/** @type {{ Its: string, Other: string, Own: string, type: "referencedBy" | "references" }} */ (
|
|
504
|
+
/** @type {any} */ (relation)
|
|
505
|
+
);
|
|
506
|
+
const otherAlias = `${name}_${unique + 1}`;
|
|
507
|
+
ok(aliases[otherAlias]?.entities);
|
|
508
|
+
for (const self of aliases[alias].entities) {
|
|
509
|
+
const matchingOthers = aliases[otherAlias].entities.filter(
|
|
510
|
+
(other) => other[Its] == self[Own],
|
|
511
|
+
);
|
|
512
|
+
self[name] = type == "references" ? matchingOthers[0] : matchingOthers;
|
|
513
|
+
}
|
|
514
|
+
if (typeof itsJoin == "object") {
|
|
515
|
+
ok(itsJoin);
|
|
516
|
+
merge(unique + 1, Other, otherAlias, itsJoin);
|
|
517
|
+
}
|
|
518
|
+
}
|
|
519
|
+
}
|
|
520
|
+
|
|
521
|
+
merge(0, props.table, alias, /** @type {any} */ (props.join));
|
|
522
|
+
const state = aliases[alias];
|
|
523
|
+
ok(state);
|
|
524
|
+
return state.entities;
|
|
525
|
+
}
|