@h3ravel/arquebus 0.6.5 → 0.6.7

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.
Files changed (47) hide show
  1. package/README.md +5 -4
  2. package/package.json +4 -3
  3. package/bin/index.cjs +0 -5964
  4. package/bin/index.d.cts +0 -1
  5. package/bin/index.d.ts +0 -1
  6. package/bin/index.js +0 -5960
  7. package/bin/seeders-8GJzfIIN.js +0 -3
  8. package/bin/seeders-ByeSoCAQ.cjs +0 -131
  9. package/bin/seeders-CltigymO.js +0 -79
  10. package/bin/seeders-_xJ6VGVS.cjs +0 -3
  11. package/dist/browser/index.cjs +0 -1271
  12. package/dist/browser/index.d.cts +0 -4932
  13. package/dist/browser/index.d.ts +0 -4932
  14. package/dist/browser/index.js +0 -1218
  15. package/dist/index.cjs +0 -5750
  16. package/dist/index.d.cts +0 -5129
  17. package/dist/index.d.ts +0 -5129
  18. package/dist/index.js +0 -5679
  19. package/dist/inspector/index.cjs +0 -4961
  20. package/dist/inspector/index.d.cts +0 -83
  21. package/dist/inspector/index.d.ts +0 -83
  22. package/dist/inspector/index.js +0 -4932
  23. package/dist/migrations/chunk-PECeCxCb.js +0 -15
  24. package/dist/migrations/index.cjs +0 -5507
  25. package/dist/migrations/index.d.cts +0 -4965
  26. package/dist/migrations/index.d.ts +0 -4962
  27. package/dist/migrations/index.js +0 -5454
  28. package/dist/migrations/stubs/migration-js.stub +0 -21
  29. package/dist/migrations/stubs/migration-ts.stub +0 -18
  30. package/dist/migrations/stubs/migration.create-js.stub +0 -24
  31. package/dist/migrations/stubs/migration.create-ts.stub +0 -21
  32. package/dist/migrations/stubs/migration.update-js.stub +0 -25
  33. package/dist/migrations/stubs/migration.update-ts.stub +0 -22
  34. package/dist/seeders/index.cjs +0 -141
  35. package/dist/seeders/index.d.cts +0 -4766
  36. package/dist/seeders/index.d.ts +0 -4766
  37. package/dist/seeders/index.js +0 -118
  38. package/dist/seeders/index.ts +0 -3
  39. package/dist/seeders/runner.ts +0 -101
  40. package/dist/seeders/seeder-creator.ts +0 -42
  41. package/dist/seeders/seeder.ts +0 -10
  42. package/dist/stubs/arquebus.config-js.stub +0 -25
  43. package/dist/stubs/arquebus.config-ts.stub +0 -24
  44. package/dist/stubs/model-js.stub +0 -5
  45. package/dist/stubs/model-ts.stub +0 -5
  46. package/dist/stubs/seeder-js.stub +0 -13
  47. package/dist/stubs/seeder-ts.stub +0 -9
@@ -1,4961 +0,0 @@
1
- //#region rolldown:runtime
2
- var __create = Object.create;
3
- var __defProp = Object.defineProperty;
4
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
- var __getOwnPropNames = Object.getOwnPropertyNames;
6
- var __getProtoOf = Object.getPrototypeOf;
7
- var __hasOwnProp = Object.prototype.hasOwnProperty;
8
- var __export = (all) => {
9
- let target = {};
10
- for (var name in all) __defProp(target, name, {
11
- get: all[name],
12
- enumerable: true
13
- });
14
- return target;
15
- };
16
- var __copyProps = (to, from, except, desc) => {
17
- if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
18
- key = keys[i];
19
- if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
20
- get: ((k) => from[k]).bind(null, key),
21
- enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
22
- });
23
- }
24
- return to;
25
- };
26
- var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
27
- value: mod,
28
- enumerable: true
29
- }) : target, mod));
30
-
31
- //#endregion
32
- let radashi = require("radashi");
33
- radashi = __toESM(radashi);
34
- let dayjs_plugin_advancedFormat_js = require("dayjs/plugin/advancedFormat.js");
35
- dayjs_plugin_advancedFormat_js = __toESM(dayjs_plugin_advancedFormat_js);
36
- let dayjs = require("dayjs");
37
- dayjs = __toESM(dayjs);
38
- let collect_js = require("collect.js");
39
- collect_js = __toESM(collect_js);
40
- let knex = require("knex");
41
- knex = __toESM(knex);
42
- let path = require("path");
43
- path = __toESM(path);
44
- let fs = require("fs");
45
- fs = __toESM(fs);
46
- let fs_promises = require("fs/promises");
47
- fs_promises = __toESM(fs_promises);
48
- let escalade_sync = require("escalade/sync");
49
- escalade_sync = __toESM(escalade_sync);
50
- let resolve_from = require("resolve-from");
51
- resolve_from = __toESM(resolve_from);
52
- let pluralize = require("pluralize");
53
- pluralize = __toESM(pluralize);
54
- require("@h3ravel/shared");
55
- require("node:fs/promises");
56
- require("node:path");
57
- require("node:url");
58
-
59
- //#region src/inspector/utils/strip-quotes.ts
60
- /**
61
- * Strip leading/trailing quotes from a string and handle null values.
62
- */
63
- function stripQuotes(value) {
64
- if (value === null || value === void 0) return null;
65
- const trimmed = value.trim();
66
- if (trimmed.startsWith("'") && trimmed.endsWith("'") || trimmed.startsWith("\"") && trimmed.endsWith("\"")) return trimmed.slice(1, -1);
67
- return value;
68
- }
69
-
70
- //#endregion
71
- //#region src/inspector/dialects/cockroachdb.ts
72
- /**
73
- * Converts CockroachDB default value to JS
74
- * Eg `'example'::character varying` => `example`
75
- */
76
- function parseDefaultValue$5(value) {
77
- if (value === null) return null;
78
- if (value.startsWith("nextval(")) return value;
79
- value = value.split("::")[0];
80
- return stripQuotes(value);
81
- }
82
- var CockroachDB = class {
83
- knex;
84
- schema;
85
- explodedSchema;
86
- constructor(knex$1) {
87
- this.knex = knex$1;
88
- const config = knex$1.client.config;
89
- if (!config.searchPath) {
90
- this.schema = "public";
91
- this.explodedSchema = [this.schema];
92
- } else if (typeof config.searchPath === "string") {
93
- this.schema = config.searchPath;
94
- this.explodedSchema = [config.searchPath];
95
- } else {
96
- this.schema = config.searchPath[0];
97
- this.explodedSchema = config.searchPath;
98
- }
99
- }
100
- /**
101
- * Set the schema to be used in other methods
102
- */
103
- withSchema(schema) {
104
- this.schema = schema;
105
- this.explodedSchema = [this.schema];
106
- return this;
107
- }
108
- /**
109
- * List all existing tables in the current schema/database
110
- */
111
- async tables() {
112
- return (await this.knex.select("tablename").from("pg_catalog.pg_tables").whereIn("schemaname", this.explodedSchema)).map(({ tablename }) => tablename);
113
- }
114
- async tableInfo(table) {
115
- const query = this.knex.select("table_name", "table_schema", this.knex.select(this.knex.raw("obj_description(oid)")).from("pg_class").where({ relkind: "r" }).andWhere({ relname: "table_name" }).as("table_comment")).from("information_schema.tables").whereIn("table_schema", this.explodedSchema).andWhereRaw("\"table_catalog\" = current_database()").andWhere({ table_type: "BASE TABLE" }).orderBy("table_name", "asc");
116
- if (table) {
117
- const rawTable = await query.andWhere({ table_name: table }).limit(1).first();
118
- return {
119
- name: rawTable.table_name,
120
- schema: rawTable.table_schema,
121
- comment: rawTable.table_comment
122
- };
123
- }
124
- return (await query).map((rawTable) => {
125
- return {
126
- name: rawTable.table_name,
127
- schema: rawTable.table_schema,
128
- comment: rawTable.table_comment
129
- };
130
- });
131
- }
132
- /**
133
- * Check if a table exists in the current schema/database
134
- */
135
- async hasTable(table) {
136
- const subquery = this.knex.select().from("information_schema.tables").whereIn("table_schema", this.explodedSchema).andWhere({ table_name: table });
137
- const record = await this.knex.select(this.knex.raw("exists (?)", [subquery])).first();
138
- return (record === null || record === void 0 ? void 0 : record.exists) || false;
139
- }
140
- /**
141
- * Get all the available columns in the current schema/database. Can be filtered to a specific table
142
- */
143
- async columns(table) {
144
- const query = this.knex.select("table_name", "column_name").from("information_schema.columns").whereIn("table_schema", this.explodedSchema);
145
- if (table) query.andWhere({ table_name: table });
146
- return (await query).map(({ table_name, column_name }) => ({
147
- table: table_name,
148
- column: column_name
149
- }));
150
- }
151
- async columnInfo(table, column) {
152
- const { knex: knex$1 } = this;
153
- const bindings = [];
154
- if (table) bindings.push(table);
155
- if (column) bindings.push(column);
156
- const schemaIn = this.explodedSchema.map((schemaName) => `${this.knex.raw("?", [schemaName])}::regnamespace`);
157
- const [columns, constraints] = await Promise.all([knex$1.raw(`
158
- SELECT *, CASE WHEN res.is_generated THEN (
159
- SELECT
160
- generation_expression
161
- FROM
162
- information_schema.columns
163
- WHERE
164
- table_schema = res.schema
165
- AND table_name = res.table
166
- AND column_name = res.name
167
- ) ELSE NULL END AS generation_expression
168
- FROM (
169
- SELECT
170
- att.attname AS name,
171
- rel.relname AS table,
172
- rel.relnamespace::regnamespace::text AS schema,
173
- format_type(att.atttypid, null) AS data_type,
174
- NOT att.attnotnull AS is_nullable,
175
- CASE WHEN att.attgenerated = '' THEN pg_get_expr(ad.adbin, ad.adrelid) ELSE null END AS default_value,
176
- att.attgenerated = 's' AS is_generated,
177
- CASE
178
- WHEN att.atttypid IN (1042, 1043) THEN (att.atttypmod - 4)::int4
179
- WHEN att.atttypid IN (1560, 1562) THEN (att.atttypmod)::int4
180
- ELSE NULL
181
- END AS max_length,
182
- des.description AS comment,
183
- CASE att.atttypid
184
- WHEN 21 THEN 16
185
- WHEN 23 THEN 32
186
- WHEN 20 THEN 64
187
- WHEN 1700 THEN
188
- CASE WHEN atttypmod = -1 THEN NULL
189
- ELSE (((atttypmod - 4) >> 16) & 65535)::int4
190
- END
191
- WHEN 700 THEN 24
192
- WHEN 701 THEN 53
193
- ELSE NULL
194
- END AS numeric_precision,
195
- CASE
196
- WHEN atttypid IN (21, 23, 20) THEN 0
197
- WHEN atttypid = 1700 THEN
198
- CASE
199
- WHEN atttypmod = -1 THEN NULL
200
- ELSE ((atttypmod - 4) & 65535)::int4
201
- END
202
- ELSE null
203
- END AS numeric_scale
204
- FROM
205
- pg_attribute att
206
- LEFT JOIN pg_class rel ON att.attrelid = rel.oid
207
- LEFT JOIN pg_attrdef ad ON (att.attrelid, att.attnum) = (ad.adrelid, ad.adnum)
208
- LEFT JOIN pg_description des ON (att.attrelid, att.attnum) = (des.objoid, des.objsubid)
209
- WHERE
210
- rel.relnamespace IN (${schemaIn})
211
- ${table ? "AND rel.relname = ?" : ""}
212
- ${column ? "AND att.attname = ?" : ""}
213
- AND rel.relkind = 'r'
214
- AND att.attnum > 0
215
- AND NOT att.attisdropped
216
- ORDER BY rel.relname, att.attnum) res;
217
- `, bindings), knex$1.raw(`
218
- SELECT
219
- con.contype AS type,
220
- rel.relname AS table,
221
- att.attname AS column,
222
- frel.relnamespace::regnamespace::text AS foreign_key_schema,
223
- frel.relname AS foreign_key_table,
224
- fatt.attname AS foreign_key_column
225
- FROM
226
- pg_constraint con
227
- LEFT JOIN pg_class rel ON con.conrelid = rel.oid
228
- LEFT JOIN pg_class frel ON con.confrelid = frel.oid
229
- LEFT JOIN pg_attribute att ON att.attrelid = con.conrelid AND att.attnum = con.conkey[1]
230
- LEFT JOIN pg_attribute fatt ON fatt.attrelid = con.confrelid AND fatt.attnum = con.confkey[1]
231
- WHERE con.connamespace IN (${schemaIn})
232
- AND array_length(con.conkey, 1) <= 1
233
- AND (con.confkey IS NULL OR array_length(con.confkey, 1) = 1)
234
- ${table ? "AND rel.relname = ?" : ""}
235
- ${column ? "AND att.attname = ?" : ""}
236
- `, bindings)]);
237
- const parsedColumms = columns.rows.map((col) => {
238
- var _col$default_value;
239
- const constraintsForColumn = constraints.rows.filter((constraint) => constraint.table === col.table && constraint.column === col.name);
240
- const foreignKeyConstraint = constraintsForColumn.find((constraint) => constraint.type === "f");
241
- return {
242
- ...col,
243
- is_unique: constraintsForColumn.some((constraint) => ["u", "p"].includes(constraint.type)),
244
- is_primary_key: constraintsForColumn.some((constraint) => constraint.type === "p"),
245
- has_auto_increment: ["integer", "bigint"].includes(col.data_type) && (((_col$default_value = col.default_value) === null || _col$default_value === void 0 ? void 0 : _col$default_value.startsWith("nextval(")) ?? false),
246
- default_value: parseDefaultValue$5(col.default_value),
247
- foreign_key_schema: (foreignKeyConstraint === null || foreignKeyConstraint === void 0 ? void 0 : foreignKeyConstraint.foreign_key_schema) ?? null,
248
- foreign_key_table: (foreignKeyConstraint === null || foreignKeyConstraint === void 0 ? void 0 : foreignKeyConstraint.foreign_key_table) ?? null,
249
- foreign_key_column: (foreignKeyConstraint === null || foreignKeyConstraint === void 0 ? void 0 : foreignKeyConstraint.foreign_key_column) ?? null
250
- };
251
- });
252
- if (table && column) return parsedColumms[0];
253
- return parsedColumms;
254
- }
255
- /**
256
- * Check if the given table contains the given column
257
- */
258
- async hasColumn(table, column) {
259
- const subquery = this.knex.select().from("information_schema.columns").whereIn("table_schema", this.explodedSchema).andWhere({
260
- table_name: table,
261
- column_name: column
262
- });
263
- const record = await this.knex.select(this.knex.raw("exists (?)", [subquery])).first();
264
- return (record === null || record === void 0 ? void 0 : record.exists) || false;
265
- }
266
- /**
267
- * Get the primary key column for the given table
268
- */
269
- async primary(table) {
270
- const result = await this.knex.select("information_schema.key_column_usage.column_name").from("information_schema.key_column_usage").leftJoin("information_schema.table_constraints", function() {
271
- this.on("information_schema.table_constraints.constraint_name", "=", "information_schema.key_column_usage.constraint_name").andOn("information_schema.table_constraints.table_name", "=", "information_schema.key_column_usage.table_name");
272
- }).whereIn("information_schema.table_constraints.table_schema", this.explodedSchema).andWhere({
273
- "information_schema.table_constraints.constraint_type": "PRIMARY KEY",
274
- "information_schema.table_constraints.table_name": table
275
- });
276
- return result.length > 0 ? result.length === 1 ? result[0].column_name : result.map((r) => r.column_name) : null;
277
- }
278
- async foreignKeys(table) {
279
- const rowsWithoutQuotes = (await this.knex.raw(`
280
- SELECT
281
- c.conrelid::regclass::text AS "table",
282
- (
283
- SELECT
284
- STRING_AGG(a.attname, ','
285
- ORDER BY
286
- t.seq)
287
- FROM (
288
- SELECT
289
- ROW_NUMBER() OVER (ROWS UNBOUNDED PRECEDING) AS seq,
290
- attnum
291
- FROM
292
- UNNEST(c.conkey) AS t (attnum)) AS t
293
- INNER JOIN pg_attribute AS a ON a.attrelid = c.conrelid
294
- AND a.attnum = t.attnum) AS "column",
295
- tt.name AS foreign_key_table,
296
- (
297
- SELECT
298
- STRING_AGG(QUOTE_IDENT(a.attname), ','
299
- ORDER BY
300
- t.seq)
301
- FROM (
302
- SELECT
303
- ROW_NUMBER() OVER (ROWS UNBOUNDED PRECEDING) AS seq,
304
- attnum
305
- FROM
306
- UNNEST(c.confkey) AS t (attnum)) AS t
307
- INNER JOIN pg_attribute AS a ON a.attrelid = c.confrelid
308
- AND a.attnum = t.attnum) AS foreign_key_column,
309
- tt.schema AS foreign_key_schema,
310
- c.conname AS constraint_name,
311
- CASE confupdtype
312
- WHEN 'r' THEN
313
- 'RESTRICT'
314
- WHEN 'c' THEN
315
- 'CASCADE'
316
- WHEN 'n' THEN
317
- 'SET NULL'
318
- WHEN 'd' THEN
319
- 'SET DEFAULT'
320
- WHEN 'a' THEN
321
- 'NO ACTION'
322
- ELSE
323
- NULL
324
- END AS on_update,
325
- CASE confdeltype
326
- WHEN 'r' THEN
327
- 'RESTRICT'
328
- WHEN 'c' THEN
329
- 'CASCADE'
330
- WHEN 'n' THEN
331
- 'SET NULL'
332
- WHEN 'd' THEN
333
- 'SET DEFAULT'
334
- WHEN 'a' THEN
335
- 'NO ACTION'
336
- ELSE
337
- NULL
338
- END AS
339
- on_delete
340
- FROM
341
- pg_catalog.pg_constraint AS c
342
- INNER JOIN (
343
- SELECT
344
- pg_class.oid,
345
- QUOTE_IDENT(pg_namespace.nspname) AS SCHEMA,
346
- QUOTE_IDENT(pg_class.relname) AS name
347
- FROM
348
- pg_class
349
- INNER JOIN pg_namespace ON pg_class.relnamespace = pg_namespace.oid) AS tf ON tf.oid = c.conrelid
350
- INNER JOIN (
351
- SELECT
352
- pg_class.oid,
353
- QUOTE_IDENT(pg_namespace.nspname) AS SCHEMA,
354
- QUOTE_IDENT(pg_class.relname) AS name
355
- FROM
356
- pg_class
357
- INNER JOIN pg_namespace ON pg_class.relnamespace = pg_namespace.oid) AS tt ON tt.oid = c.confrelid
358
- WHERE
359
- c.contype = 'f';
360
- `)).rows.map(stripRowQuotes);
361
- if (table) return rowsWithoutQuotes.filter((row) => row.table === table);
362
- return rowsWithoutQuotes;
363
- function stripRowQuotes(row) {
364
- return Object.fromEntries(Object.entries(row).map(([key, value]) => {
365
- return [key, stripQuotes(value)];
366
- }));
367
- }
368
- }
369
- };
370
-
371
- //#endregion
372
- //#region src/inspector/dialects/mssql.ts
373
- function rawColumnToColumn$2(rawColumn) {
374
- return {
375
- ...rawColumn,
376
- default_value: parseDefaultValue$4(rawColumn.default_value),
377
- generation_expression: rawColumn.generation_expression || null,
378
- is_generated: !!rawColumn.is_generated,
379
- is_unique: rawColumn.is_unique === true,
380
- is_primary_key: rawColumn.is_primary_key === true,
381
- is_nullable: rawColumn.is_nullable === "YES",
382
- has_auto_increment: rawColumn.has_auto_increment === "YES",
383
- numeric_precision: rawColumn.numeric_precision || null,
384
- numeric_scale: rawColumn.numeric_scale || null,
385
- max_length: parseMaxLength(rawColumn)
386
- };
387
- function parseMaxLength(rawColumn$1) {
388
- const max_length = Number(rawColumn$1.max_length);
389
- if (Number.isNaN(max_length) || rawColumn$1.max_length === null || rawColumn$1.max_length === void 0) return null;
390
- if ([
391
- "nvarchar",
392
- "nchar",
393
- "ntext"
394
- ].includes(rawColumn$1.data_type)) return max_length === -1 ? max_length : max_length / 2;
395
- return max_length;
396
- }
397
- }
398
- function parseDefaultValue$4(value) {
399
- if (value === null) return null;
400
- while (value.startsWith("(") && value.endsWith(")")) value = value.slice(1, -1);
401
- if (value.trim().toLowerCase() === "null") return null;
402
- return stripQuotes(value);
403
- }
404
- var MSSQL = class {
405
- knex;
406
- _schema;
407
- constructor(knex$1) {
408
- this.knex = knex$1;
409
- }
410
- /**
411
- * Set the schema to be used in other methods
412
- */
413
- withSchema(schema) {
414
- this.schema = schema;
415
- return this;
416
- }
417
- get schema() {
418
- return this._schema || "dbo";
419
- }
420
- set schema(value) {
421
- this._schema = value;
422
- }
423
- /**
424
- * List all existing tables in the current schema/database
425
- */
426
- async tables() {
427
- return (await this.knex.select("TABLE_NAME").from("INFORMATION_SCHEMA.TABLES").where({
428
- TABLE_TYPE: "BASE TABLE",
429
- TABLE_CATALOG: this.knex.client.database(),
430
- TABLE_SCHEMA: this.schema
431
- })).map(({ TABLE_NAME }) => TABLE_NAME);
432
- }
433
- async tableInfo(table) {
434
- const query = this.knex.select("TABLE_NAME", "TABLE_SCHEMA", "TABLE_CATALOG", "TABLE_TYPE").from("INFORMATION_SCHEMA.TABLES").where({
435
- TABLE_CATALOG: this.knex.client.database(),
436
- TABLE_TYPE: "BASE TABLE",
437
- TABLE_SCHEMA: this.schema
438
- });
439
- if (table) {
440
- const rawTable = await query.andWhere({ table_name: table }).first();
441
- return {
442
- name: rawTable.TABLE_NAME,
443
- schema: rawTable.TABLE_SCHEMA,
444
- catalog: rawTable.TABLE_CATALOG
445
- };
446
- }
447
- return (await query).map((rawTable) => {
448
- return {
449
- name: rawTable.TABLE_NAME,
450
- schema: rawTable.TABLE_SCHEMA,
451
- catalog: rawTable.TABLE_CATALOG
452
- };
453
- });
454
- }
455
- /**
456
- * Check if a table exists in the current schema/database
457
- */
458
- async hasTable(table) {
459
- const result = await this.knex.count({ count: "*" }).from("INFORMATION_SCHEMA.TABLES").where({
460
- TABLE_CATALOG: this.knex.client.database(),
461
- table_name: table,
462
- TABLE_SCHEMA: this.schema
463
- }).first();
464
- return result && result.count === 1 || false;
465
- }
466
- /**
467
- * Get all the available columns in the current schema/database. Can be filtered to a specific table
468
- */
469
- async columns(table) {
470
- const query = this.knex.select("TABLE_NAME", "COLUMN_NAME").from("INFORMATION_SCHEMA.COLUMNS").where({
471
- TABLE_CATALOG: this.knex.client.database(),
472
- TABLE_SCHEMA: this.schema
473
- });
474
- if (table) query.andWhere({ TABLE_NAME: table });
475
- return (await query).map(({ TABLE_NAME, COLUMN_NAME }) => ({
476
- table: TABLE_NAME,
477
- column: COLUMN_NAME
478
- }));
479
- }
480
- async columnInfo(table, column) {
481
- const dbName = this.knex.client.database();
482
- const query = this.knex.select(this.knex.raw(`
483
- [o].[name] AS [table],
484
- [c].[name] AS [name],
485
- [t].[name] AS [data_type],
486
- [c].[max_length] AS [max_length],
487
- [c].[precision] AS [numeric_precision],
488
- [c].[scale] AS [numeric_scale],
489
- CASE WHEN [c].[is_nullable] = 0 THEN
490
- 'NO'
491
- ELSE
492
- 'YES'
493
- END AS [is_nullable],
494
- object_definition ([c].[default_object_id]) AS [default_value],
495
- [i].[is_primary_key],
496
- [i].[is_unique],
497
- CASE [c].[is_identity]
498
- WHEN 1 THEN
499
- 'YES'
500
- ELSE
501
- 'NO'
502
- END AS [has_auto_increment],
503
- OBJECT_NAME ([fk].[referenced_object_id]) AS [foreign_key_table],
504
- COL_NAME ([fk].[referenced_object_id],
505
- [fk].[referenced_column_id]) AS [foreign_key_column],
506
- [cc].[is_computed] as [is_generated],
507
- [cc].[definition] as [generation_expression]`)).from(this.knex.raw("??.[sys].[columns] [c]", [dbName])).joinRaw("JOIN [sys].[types] [t] ON [c].[user_type_id] = [t].[user_type_id]").joinRaw("JOIN [sys].[tables] [o] ON [o].[object_id] = [c].[object_id]").joinRaw("JOIN [sys].[schemas] [s] ON [s].[schema_id] = [o].[schema_id]").joinRaw("LEFT JOIN [sys].[computed_columns] AS [cc] ON [cc].[object_id] = [c].[object_id] AND [cc].[column_id] = [c].[column_id]").joinRaw("LEFT JOIN [sys].[foreign_key_columns] AS [fk] ON [fk].[parent_object_id] = [c].[object_id] AND [fk].[parent_column_id] = [c].[column_id]").joinRaw(`LEFT JOIN (
508
- SELECT
509
- [ic].[object_id],
510
- [ic].[column_id],
511
- [ix].[is_unique],
512
- [ix].[is_primary_key],
513
- MAX([ic].[index_column_id]) OVER(partition by [ic].[index_id], [ic].[object_id]) AS index_column_count,
514
- ROW_NUMBER() OVER (
515
- PARTITION BY [ic].[object_id], [ic].[column_id]
516
- ORDER BY [ix].[is_primary_key] DESC, [ix].[is_unique] DESC
517
- ) AS index_priority
518
- FROM
519
- [sys].[index_columns] [ic]
520
- JOIN [sys].[indexes] AS [ix] ON [ix].[object_id] = [ic].[object_id]
521
- AND [ix].[index_id] = [ic].[index_id]
522
- ) AS [i]
523
- ON [i].[object_id] = [c].[object_id]
524
- AND [i].[column_id] = [c].[column_id]
525
- AND ISNULL([i].[index_column_count], 1) = 1
526
- AND ISNULL([i].[index_priority], 1) = 1`).where({ "s.name": this.schema });
527
- if (table) query.andWhere({ "o.name": table });
528
- if (column) {
529
- const rawColumn = await query.andWhere({ "c.name": column }).first();
530
- return rawColumnToColumn$2(rawColumn);
531
- }
532
- return (await query).map(rawColumnToColumn$2);
533
- }
534
- /**
535
- * Check if a table exists in the current schema/database
536
- */
537
- async hasColumn(table, column) {
538
- const result = await this.knex.count({ count: "*" }).from("INFORMATION_SCHEMA.COLUMNS").where({
539
- TABLE_CATALOG: this.knex.client.database(),
540
- TABLE_NAME: table,
541
- COLUMN_NAME: column,
542
- TABLE_SCHEMA: this.schema
543
- }).first();
544
- return result && result.count === 1 || false;
545
- }
546
- /**
547
- * Get the primary key column for the given table
548
- */
549
- async primary(table) {
550
- const results = await this.knex.raw(`SELECT
551
- Col.Column_Name
552
- FROM
553
- INFORMATION_SCHEMA.TABLE_CONSTRAINTS Tab,
554
- INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE Col
555
- WHERE
556
- Col.Constraint_Name = Tab.Constraint_Name
557
- AND Col.Table_Name = Tab.Table_Name
558
- AND Constraint_Type = 'PRIMARY KEY'
559
- AND Col.Table_Name = ?
560
- AND Tab.CONSTRAINT_SCHEMA = ?`, [table, this.schema]);
561
- return results.length > 0 ? results.length === 1 ? results[0]["Column_Name"] : results.map((row) => row["Column_Name"]) : null;
562
- }
563
- async foreignKeys(table) {
564
- const result = await this.knex.raw(`
565
- SELECT
566
- OBJECT_NAME (fc.parent_object_id) AS "table",
567
- COL_NAME (fc.parent_object_id, fc.parent_column_id) AS "column",
568
- OBJECT_SCHEMA_NAME (f.referenced_object_id) AS foreign_key_schema,
569
- OBJECT_NAME (f.referenced_object_id) AS foreign_key_table,
570
- COL_NAME (fc.referenced_object_id, fc.referenced_column_id) AS foreign_key_column,
571
- f.name AS constraint_name,
572
- REPLACE(f.update_referential_action_desc, '_', ' ') AS on_update,
573
- REPLACE(f.delete_referential_action_desc, '_', ' ') AS on_delete
574
- FROM
575
- sys.foreign_keys AS f
576
- INNER JOIN sys.foreign_key_columns AS fc ON f.OBJECT_ID = fc.constraint_object_id
577
- WHERE
578
- OBJECT_SCHEMA_NAME (f.parent_object_id) = ?;
579
- `, [this.schema]);
580
- if (table) return result === null || result === void 0 ? void 0 : result.filter((row) => row.table === table);
581
- return result;
582
- }
583
- };
584
-
585
- //#endregion
586
- //#region src/inspector/dialects/mysql.ts
587
- function rawColumnToColumn$1(rawColumn) {
588
- var _rawColumn$EXTRA;
589
- let dataType = rawColumn.COLUMN_TYPE.replace(/\(.*?\)/, "");
590
- if (rawColumn.COLUMN_TYPE.startsWith("tinyint(1)")) dataType = "boolean";
591
- return {
592
- name: rawColumn.COLUMN_NAME,
593
- table: rawColumn.TABLE_NAME,
594
- collation: rawColumn.COLLATION_NAME,
595
- data_type: dataType,
596
- default_value: parseDefaultValue$3(rawColumn.COLUMN_DEFAULT),
597
- generation_expression: rawColumn.GENERATION_EXPRESSION || null,
598
- max_length: rawColumn.CHARACTER_MAXIMUM_LENGTH,
599
- numeric_precision: rawColumn.NUMERIC_PRECISION,
600
- numeric_scale: rawColumn.NUMERIC_SCALE,
601
- is_generated: !!((_rawColumn$EXTRA = rawColumn.EXTRA) === null || _rawColumn$EXTRA === void 0 ? void 0 : _rawColumn$EXTRA.endsWith("GENERATED")),
602
- is_nullable: rawColumn.IS_NULLABLE === "YES",
603
- is_unique: rawColumn.COLUMN_KEY === "UNI",
604
- is_primary_key: rawColumn.CONSTRAINT_NAME === "PRIMARY" || rawColumn.COLUMN_KEY === "PRI",
605
- has_auto_increment: rawColumn.EXTRA === "auto_increment",
606
- foreign_key_column: rawColumn.REFERENCED_COLUMN_NAME,
607
- foreign_key_table: rawColumn.REFERENCED_TABLE_NAME,
608
- comment: rawColumn.COLUMN_COMMENT
609
- };
610
- }
611
- function parseDefaultValue$3(value) {
612
- if (value === null || value.trim().toLowerCase() === "null") return null;
613
- return stripQuotes(value);
614
- }
615
- var MySQL = class {
616
- knex;
617
- constructor(knex$1) {
618
- this.knex = knex$1;
619
- }
620
- /**
621
- * List all existing tables in the current schema/database
622
- */
623
- async tables() {
624
- return (await this.knex.select("TABLE_NAME").from("INFORMATION_SCHEMA.TABLES").where({
625
- TABLE_TYPE: "BASE TABLE",
626
- TABLE_SCHEMA: this.knex.client.database()
627
- })).map(({ TABLE_NAME }) => TABLE_NAME);
628
- }
629
- async tableInfo(table) {
630
- const query = this.knex.select("TABLE_NAME", "ENGINE", "TABLE_SCHEMA", "TABLE_COLLATION", "TABLE_COMMENT").from("information_schema.tables").where({
631
- table_schema: this.knex.client.database(),
632
- table_type: "BASE TABLE"
633
- });
634
- if (table) {
635
- const rawTable = await query.andWhere({ table_name: table }).first();
636
- return {
637
- name: rawTable.TABLE_NAME,
638
- schema: rawTable.TABLE_SCHEMA,
639
- comment: rawTable.TABLE_COMMENT,
640
- collation: rawTable.TABLE_COLLATION,
641
- engine: rawTable.ENGINE
642
- };
643
- }
644
- return (await query).map((rawTable) => {
645
- return {
646
- name: rawTable.TABLE_NAME,
647
- schema: rawTable.TABLE_SCHEMA,
648
- comment: rawTable.TABLE_COMMENT,
649
- collation: rawTable.TABLE_COLLATION,
650
- engine: rawTable.ENGINE
651
- };
652
- });
653
- }
654
- /**
655
- * Check if a table exists in the current schema/database
656
- */
657
- async hasTable(table) {
658
- const result = await this.knex.count({ count: "*" }).from("information_schema.tables").where({
659
- table_schema: this.knex.client.database(),
660
- table_name: table
661
- }).first();
662
- return result && result.count === 1 || false;
663
- }
664
- /**
665
- * Get all the available columns in the current schema/database. Can be filtered to a specific table
666
- */
667
- async columns(table) {
668
- const query = this.knex.select("TABLE_NAME", "COLUMN_NAME").from("INFORMATION_SCHEMA.COLUMNS").where({ TABLE_SCHEMA: this.knex.client.database() });
669
- if (table) query.andWhere({ TABLE_NAME: table });
670
- return (await query).map(({ TABLE_NAME, COLUMN_NAME }) => ({
671
- table: TABLE_NAME,
672
- column: COLUMN_NAME
673
- }));
674
- }
675
- async columnInfo(table, column) {
676
- const query = this.knex.select("c.TABLE_NAME", "c.COLUMN_NAME", "c.COLUMN_DEFAULT", "c.COLUMN_TYPE", "c.CHARACTER_MAXIMUM_LENGTH", "c.IS_NULLABLE", "c.COLUMN_KEY", "c.EXTRA", "c.COLLATION_NAME", "c.COLUMN_COMMENT", "c.NUMERIC_PRECISION", "c.NUMERIC_SCALE", "c.GENERATION_EXPRESSION", "fk.REFERENCED_TABLE_NAME", "fk.REFERENCED_COLUMN_NAME", "fk.CONSTRAINT_NAME", "rc.UPDATE_RULE", "rc.DELETE_RULE", "rc.MATCH_OPTION").from("INFORMATION_SCHEMA.COLUMNS as c").leftJoin("INFORMATION_SCHEMA.KEY_COLUMN_USAGE as fk", function() {
677
- this.on("c.TABLE_NAME", "=", "fk.TABLE_NAME").andOn("fk.COLUMN_NAME", "=", "c.COLUMN_NAME").andOn("fk.CONSTRAINT_SCHEMA", "=", "c.TABLE_SCHEMA");
678
- }).leftJoin("INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS as rc", function() {
679
- this.on("rc.TABLE_NAME", "=", "fk.TABLE_NAME").andOn("rc.CONSTRAINT_NAME", "=", "fk.CONSTRAINT_NAME").andOn("rc.CONSTRAINT_SCHEMA", "=", "fk.CONSTRAINT_SCHEMA");
680
- }).where({ "c.TABLE_SCHEMA": this.knex.client.database() });
681
- if (table) query.andWhere({ "c.TABLE_NAME": table });
682
- if (column) {
683
- const rawColumn = await query.andWhere({ "c.column_name": column }).first();
684
- return rawColumnToColumn$1(rawColumn);
685
- }
686
- return (await query).map(rawColumnToColumn$1).sort((column$1) => +!column$1.foreign_key_column).filter((column$1, index, records) => {
687
- return records.findIndex((_column) => {
688
- return column$1.name === _column.name && column$1.table === _column.table;
689
- }) === index;
690
- });
691
- }
692
- /**
693
- * Check if a table exists in the current schema/database
694
- */
695
- async hasColumn(table, column) {
696
- const result = await this.knex.count("*", { as: "count" }).from("information_schema.columns").where({
697
- table_schema: this.knex.client.database(),
698
- table_name: table,
699
- column_name: column
700
- }).first();
701
- return !!(result && result.count);
702
- }
703
- /**
704
- * Get the primary key column for the given table
705
- */
706
- async primary(table) {
707
- const results = await this.knex.raw("SHOW KEYS FROM ?? WHERE Key_name = 'PRIMARY'", table);
708
- if (results && results.length && results[0].length) {
709
- if (results[0].length === 1) return results[0][0]["Column_name"];
710
- return results[0].map((row) => row["Column_name"]);
711
- }
712
- return null;
713
- }
714
- async foreignKeys(table) {
715
- const query = this.knex.select("rc.TABLE_NAME AS table", "kcu.COLUMN_NAME AS column", "rc.REFERENCED_TABLE_NAME AS foreign_key_table", "kcu.REFERENCED_COLUMN_NAME AS foreign_key_column", "rc.CONSTRAINT_NAME AS constraint_name", "rc.UPDATE_RULE AS on_update", "rc.DELETE_RULE AS on_delete").from("information_schema.referential_constraints AS rc").leftJoin("information_schema.key_column_usage AS kcu ", function() {
716
- this.on("rc.CONSTRAINT_NAME", "=", "kcu.CONSTRAINT_NAME").andOn("kcu.CONSTRAINT_SCHEMA", "=", "rc.CONSTRAINT_SCHEMA");
717
- }).where({ "rc.CONSTRAINT_SCHEMA": this.knex.client.database() });
718
- if (table) query.andWhere({ "rc.TABLE_NAME": table });
719
- return await query;
720
- }
721
- async uniqueConstraints(table) {
722
- const { knex: knex$1 } = this;
723
- const query = knex$1.select("stat.table_name AS table_name", "stat.index_name AS constraint_name", knex$1.raw("group_concat(stat.column_name ORDER BY stat.seq_in_index separator ', ') AS columns")).from("information_schema.statistics stat").join("information_schema.table_constraints tco", function() {
724
- this.on("stat.table_schema", "=", "tco.table_schema").andOn("stat.table_name", "=", "tco.table_name").andOn("stat.index_name", "=", "tco.constraint_name");
725
- }).where("stat.non_unique", "=", 0).andWhere("tco.constraint_type", "=", "UNIQUE").andWhere("stat.table_schema", knex$1.client.database()).groupBy([
726
- "stat.table_name",
727
- "stat.index_name",
728
- "tco.constraint_type"
729
- ]);
730
- if (table) query.andWhere("stat.table_name", "=", table);
731
- return (await query).map((v) => ({
732
- table: v.table_name,
733
- constraint_name: v.constraint_name,
734
- columns: v.columns
735
- }));
736
- }
737
- };
738
-
739
- //#endregion
740
- //#region src/inspector/dialects/oracledb.ts
741
- /**
742
- * NOTE: Use previous optimizer for better data dictionary performance.
743
- */
744
- const OPTIMIZER_FEATURES = "11.2.0.4";
745
- function rawColumnToColumn(rawColumn) {
746
- const is_generated = rawColumn.VIRTUAL_COLUMN === "YES";
747
- const default_value = parseDefaultValue$2(rawColumn.DATA_DEFAULT);
748
- return {
749
- name: rawColumn.COLUMN_NAME,
750
- table: rawColumn.TABLE_NAME,
751
- data_type: rawColumn.DATA_TYPE,
752
- default_value: !is_generated ? default_value : null,
753
- generation_expression: is_generated ? default_value : null,
754
- max_length: rawColumn.DATA_LENGTH,
755
- numeric_precision: rawColumn.DATA_PRECISION,
756
- numeric_scale: rawColumn.DATA_SCALE,
757
- is_generated: rawColumn.VIRTUAL_COLUMN === "YES",
758
- is_nullable: rawColumn.NULLABLE === "Y",
759
- is_unique: rawColumn.CONSTRAINT_TYPE === "U",
760
- is_primary_key: rawColumn.CONSTRAINT_TYPE === "P",
761
- has_auto_increment: rawColumn.IDENTITY_COLUMN === "YES",
762
- foreign_key_column: rawColumn.REFERENCED_COLUMN_NAME,
763
- foreign_key_table: rawColumn.REFERENCED_TABLE_NAME,
764
- comment: rawColumn.COLUMN_COMMENT
765
- };
766
- }
767
- function parseDefaultValue$2(value) {
768
- if (value === null || value.trim().toLowerCase() === "null") return null;
769
- if (value === "CURRENT_TIMESTAMP ") return "CURRENT_TIMESTAMP";
770
- return stripQuotes(value);
771
- }
772
- var oracleDB = class {
773
- knex;
774
- constructor(knex$1) {
775
- this.knex = knex$1;
776
- }
777
- /**
778
- * List all existing tables in the current schema/database
779
- */
780
- async tables() {
781
- return (await this.knex.select(this.knex.raw(`
782
- /*+ OPTIMIZER_FEATURES_ENABLE('${OPTIMIZER_FEATURES}') */
783
- "TABLE_NAME" "name"
784
- `)).from("USER_TABLES")).map(({ name }) => name);
785
- }
786
- async tableInfo(table) {
787
- const query = this.knex.select(this.knex.raw(`
788
- /*+ OPTIMIZER_FEATURES_ENABLE('${OPTIMIZER_FEATURES}') */
789
- "TABLE_NAME" "name"
790
- `)).from("USER_TABLES");
791
- if (table) return await query.andWhere({ TABLE_NAME: table }).first();
792
- return await query;
793
- }
794
- /**
795
- * Check if a table exists in the current schema/database
796
- */
797
- async hasTable(table) {
798
- const result = await this.knex.select(this.knex.raw(`
799
- /*+ OPTIMIZER_FEATURES_ENABLE('${OPTIMIZER_FEATURES}') */
800
- COUNT(*) "count"
801
- `)).from("USER_TABLES").where({ TABLE_NAME: table }).first();
802
- return !!(result === null || result === void 0 ? void 0 : result.count);
803
- }
804
- /**
805
- * Get all the available columns in the current schema/database. Can be filtered to a specific table
806
- */
807
- async columns(table) {
808
- const query = this.knex.select(this.knex.raw(`
809
- /*+ OPTIMIZER_FEATURES_ENABLE('${OPTIMIZER_FEATURES}') NO_QUERY_TRANSFORMATION */
810
- "TABLE_NAME" "table",
811
- "COLUMN_NAME" "column"
812
- `)).from("USER_TAB_COLS").where({ HIDDEN_COLUMN: "NO" });
813
- if (table) query.andWhere({ TABLE_NAME: table });
814
- return await query;
815
- }
816
- async columnInfo(table, column) {
817
- /**
818
- * NOTE: Keep in mind, this query is optimized for speed.
819
- */
820
- const query = this.knex.with("uc", this.knex.raw(`
821
- SELECT /*+ MATERIALIZE */
822
- "uc"."TABLE_NAME",
823
- "ucc"."COLUMN_NAME",
824
- "uc"."CONSTRAINT_NAME",
825
- "uc"."CONSTRAINT_TYPE",
826
- "uc"."R_CONSTRAINT_NAME",
827
- COUNT(*) OVER(
828
- PARTITION BY
829
- "uc"."CONSTRAINT_NAME"
830
- ) "CONSTRAINT_COUNT",
831
- ROW_NUMBER() OVER(
832
- PARTITION BY
833
- "uc"."TABLE_NAME",
834
- "ucc"."COLUMN_NAME"
835
- ORDER BY
836
- "uc"."CONSTRAINT_TYPE"
837
- ) "CONSTRAINT_PRIORITY"
838
- FROM "USER_CONSTRAINTS" "uc"
839
- INNER JOIN "USER_CONS_COLUMNS" "ucc"
840
- ON "uc"."CONSTRAINT_NAME" = "ucc"."CONSTRAINT_NAME"
841
- WHERE "uc"."CONSTRAINT_TYPE" IN ('P', 'U', 'R')
842
- `)).select(this.knex.raw(`
843
- /*+ OPTIMIZER_FEATURES_ENABLE('${OPTIMIZER_FEATURES}') */
844
- "c"."TABLE_NAME",
845
- "c"."COLUMN_NAME",
846
- "c"."DATA_DEFAULT",
847
- "c"."DATA_TYPE",
848
- "c"."DATA_LENGTH",
849
- "c"."DATA_PRECISION",
850
- "c"."DATA_SCALE",
851
- "c"."NULLABLE",
852
- "c"."IDENTITY_COLUMN",
853
- "c"."VIRTUAL_COLUMN",
854
- "cm"."COMMENTS" "COLUMN_COMMENT",
855
- "ct"."CONSTRAINT_TYPE",
856
- "fk"."TABLE_NAME" "REFERENCED_TABLE_NAME",
857
- "fk"."COLUMN_NAME" "REFERENCED_COLUMN_NAME"
858
- FROM "USER_TAB_COLS" "c"
859
- LEFT JOIN "USER_COL_COMMENTS" "cm"
860
- ON "c"."TABLE_NAME" = "cm"."TABLE_NAME"
861
- AND "c"."COLUMN_NAME" = "cm"."COLUMN_NAME"
862
- LEFT JOIN "uc" "ct"
863
- ON "c"."TABLE_NAME" = "ct"."TABLE_NAME"
864
- AND "c"."COLUMN_NAME" = "ct"."COLUMN_NAME"
865
- AND "ct"."CONSTRAINT_COUNT" = 1
866
- AND "ct"."CONSTRAINT_PRIORITY" = 1
867
- LEFT JOIN "uc" "fk"
868
- ON "ct"."R_CONSTRAINT_NAME" = "fk"."CONSTRAINT_NAME"
869
- `)).where({ "c.HIDDEN_COLUMN": "NO" });
870
- if (table) query.andWhere({ "c.TABLE_NAME": table });
871
- if (column) {
872
- const rawColumn = await query.andWhere({ "c.COLUMN_NAME": column }).first();
873
- return rawColumnToColumn(rawColumn);
874
- }
875
- return (await query).map(rawColumnToColumn);
876
- }
877
- /**
878
- * Check if a table exists in the current schema/database
879
- */
880
- async hasColumn(table, column) {
881
- const result = await this.knex.select(this.knex.raw(`
882
- /*+ OPTIMIZER_FEATURES_ENABLE('${OPTIMIZER_FEATURES}') NO_QUERY_TRANSFORMATION */
883
- COUNT(*) "count"
884
- `)).from("USER_TAB_COLS").where({
885
- TABLE_NAME: table,
886
- COLUMN_NAME: column,
887
- HIDDEN_COLUMN: "NO"
888
- }).first();
889
- return !!(result === null || result === void 0 ? void 0 : result.count);
890
- }
891
- /**
892
- * Get the primary key column for the given table
893
- */
894
- async primary(table) {
895
- /**
896
- * NOTE: Keep in mind, this query is optimized for speed.
897
- */
898
- const result = await this.knex.with("uc", this.knex.select(this.knex.raw("/*+ MATERIALIZE */ \"CONSTRAINT_NAME\"")).from("USER_CONSTRAINTS").where({
899
- TABLE_NAME: table,
900
- CONSTRAINT_TYPE: "P"
901
- })).select(this.knex.raw(`
902
- /*+ OPTIMIZER_FEATURES_ENABLE('${OPTIMIZER_FEATURES}') */
903
- "ucc"."COLUMN_NAME"
904
- FROM "USER_CONS_COLUMNS" "ucc"
905
- INNER JOIN "uc" "pk"
906
- ON "ucc"."CONSTRAINT_NAME" = "pk"."CONSTRAINT_NAME"
907
- `));
908
- return result.length > 0 ? result.length === 1 ? result[0].COLUMN_NAME : result.map((r) => r.COLUMN_NAME) : null;
909
- }
910
- async foreignKeys(table) {
911
- /**
912
- * NOTE: Keep in mind, this query is optimized for speed.
913
- */
914
- const query = this.knex.with("ucc", this.knex.raw(`
915
- SELECT /*+ MATERIALIZE */
916
- "TABLE_NAME",
917
- "COLUMN_NAME",
918
- "CONSTRAINT_NAME"
919
- FROM "USER_CONS_COLUMNS"
920
- `)).select(this.knex.raw(`
921
- /*+ OPTIMIZER_FEATURES_ENABLE('${OPTIMIZER_FEATURES}') */
922
- "uc"."TABLE_NAME" "table",
923
- "fcc"."COLUMN_NAME" "column",
924
- "rcc"."TABLE_NAME" AS "foreign_key_table",
925
- "rcc"."COLUMN_NAME" AS "foreign_key_column",
926
- "uc"."CONSTRAINT_NAME" "constraint_name",
927
- NULL as "on_update",
928
- "uc"."DELETE_RULE" "on_delete"
929
- FROM "USER_CONSTRAINTS" "uc"
930
- INNER JOIN "ucc" "fcc"
931
- ON "uc"."CONSTRAINT_NAME" = "fcc"."CONSTRAINT_NAME"
932
- INNER JOIN "ucc" "rcc"
933
- ON "uc"."R_CONSTRAINT_NAME" = "rcc"."CONSTRAINT_NAME"
934
- `)).where({ "uc.CONSTRAINT_TYPE": "R" });
935
- if (table) query.andWhere({ "uc.TABLE_NAME": table });
936
- return await query;
937
- }
938
- };
939
-
940
- //#endregion
941
- //#region src/inspector/dialects/postgres.ts
942
- /**
943
- * Converts Postgres default value to JS
944
- * Eg `'example'::character varying` => `example`
945
- */
946
- function parseDefaultValue$1(value) {
947
- if (value === null) return null;
948
- if (value.startsWith("nextval(")) return value;
949
- value = value.split("::")[0];
950
- if (value.trim().toLowerCase() === "null") return null;
951
- return stripQuotes(value);
952
- }
953
- var Postgres = class {
954
- knex;
955
- schema;
956
- explodedSchema;
957
- constructor(knex$1) {
958
- this.knex = knex$1;
959
- const config = knex$1.client.config;
960
- if (!config.searchPath) {
961
- this.schema = "public";
962
- this.explodedSchema = [this.schema];
963
- } else if (typeof config.searchPath === "string") {
964
- this.schema = config.searchPath;
965
- this.explodedSchema = [config.searchPath];
966
- } else {
967
- this.schema = config.searchPath[0];
968
- this.explodedSchema = config.searchPath;
969
- }
970
- }
971
- /**
972
- * Set the schema to be used in other methods
973
- */
974
- withSchema(schema) {
975
- this.schema = schema;
976
- this.explodedSchema = [this.schema];
977
- return this;
978
- }
979
- /**
980
- * List all existing tables in the current schema/database
981
- */
982
- async tables() {
983
- const schemaIn = this.explodedSchema.map((schemaName) => `${this.knex.raw("?", [schemaName])}::regnamespace`);
984
- return (await this.knex.raw(`
985
- SELECT
986
- rel.relname AS name
987
- FROM
988
- pg_class rel
989
- WHERE
990
- rel.relnamespace IN (${schemaIn})
991
- AND rel.relkind = 'r'
992
- ORDER BY rel.relname
993
- `)).rows.map((row) => row.name);
994
- }
995
- async tableInfo(table) {
996
- const schemaIn = this.explodedSchema.map((schemaName) => `${this.knex.raw("?", [schemaName])}::regnamespace`);
997
- const bindings = [];
998
- if (table) bindings.push(table);
999
- const result = await this.knex.raw(`
1000
- SELECT
1001
- rel.relnamespace::regnamespace::text AS schema,
1002
- rel.relname AS name,
1003
- des.description AS comment
1004
- FROM
1005
- pg_class rel
1006
- LEFT JOIN pg_description des ON rel.oid = des.objoid AND des.objsubid = 0
1007
- WHERE
1008
- rel.relnamespace IN (${schemaIn})
1009
- ${table ? "AND rel.relname = ?" : ""}
1010
- AND rel.relkind = 'r'
1011
- ORDER BY rel.relname
1012
- `, bindings);
1013
- if (table) return result.rows[0];
1014
- return result.rows;
1015
- }
1016
- /**
1017
- * Check if a table exists in the current schema/database
1018
- */
1019
- async hasTable(table) {
1020
- const schemaIn = this.explodedSchema.map((schemaName) => `${this.knex.raw("?", [schemaName])}::regnamespace`);
1021
- return (await this.knex.raw(`
1022
- SELECT
1023
- rel.relname AS name
1024
- FROM
1025
- pg_class rel
1026
- WHERE
1027
- rel.relnamespace IN (${schemaIn})
1028
- AND rel.relkind = 'r'
1029
- AND rel.relname = ?
1030
- ORDER BY rel.relname
1031
- `, [table])).rows.length > 0;
1032
- }
1033
- /**
1034
- * Get all the available columns in the current schema/database. Can be filtered to a specific table
1035
- */
1036
- async columns(table) {
1037
- const bindings = [];
1038
- if (table) bindings.push(table);
1039
- const schemaIn = this.explodedSchema.map((schemaName) => `${this.knex.raw("?", [schemaName])}::regnamespace`);
1040
- return (await this.knex.raw(`
1041
- SELECT
1042
- att.attname AS column,
1043
- rel.relname AS table
1044
- FROM
1045
- pg_attribute att
1046
- LEFT JOIN pg_class rel ON att.attrelid = rel.oid
1047
- WHERE
1048
- rel.relnamespace IN (${schemaIn})
1049
- ${table ? "AND rel.relname = ?" : ""}
1050
- AND rel.relkind = 'r'
1051
- AND att.attnum > 0
1052
- AND NOT att.attisdropped;
1053
- `, bindings)).rows;
1054
- }
1055
- async columnInfo(table, column) {
1056
- var _versionResponse$rows;
1057
- const { knex: knex$1 } = this;
1058
- const bindings = [];
1059
- if (table) bindings.push(table);
1060
- if (column) bindings.push(column);
1061
- const schemaIn = this.explodedSchema.map((schemaName) => `${this.knex.raw("?", [schemaName])}::regnamespace`);
1062
- const majorVersion = ((_versionResponse$rows = (await this.knex.raw("SHOW server_version")).rows) === null || _versionResponse$rows === void 0 || (_versionResponse$rows = _versionResponse$rows[0]) === null || _versionResponse$rows === void 0 || (_versionResponse$rows = _versionResponse$rows.server_version) === null || _versionResponse$rows === void 0 || (_versionResponse$rows = _versionResponse$rows.split(".")) === null || _versionResponse$rows === void 0 ? void 0 : _versionResponse$rows[0]) ?? 10;
1063
- let generationSelect = `
1064
- NULL AS generation_expression,
1065
- pg_get_expr(ad.adbin, ad.adrelid) AS default_value,
1066
- FALSE AS is_generated,
1067
- `;
1068
- if (Number(majorVersion) >= 12) generationSelect = `
1069
- CASE WHEN att.attgenerated = 's' THEN pg_get_expr(ad.adbin, ad.adrelid) ELSE null END AS generation_expression,
1070
- CASE WHEN att.attgenerated = '' THEN pg_get_expr(ad.adbin, ad.adrelid) ELSE null END AS default_value,
1071
- att.attgenerated = 's' AS is_generated,
1072
- `;
1073
- const [columns, constraints] = await Promise.all([knex$1.raw(`
1074
- SELECT
1075
- att.attname AS name,
1076
- rel.relname AS table,
1077
- rel.relnamespace::regnamespace::text as schema,
1078
- att.atttypid::regtype::text AS data_type,
1079
- NOT att.attnotnull AS is_nullable,
1080
- ${generationSelect}
1081
- CASE
1082
- WHEN att.atttypid IN (1042, 1043) THEN (att.atttypmod - 4)::int4
1083
- WHEN att.atttypid IN (1560, 1562) THEN (att.atttypmod)::int4
1084
- ELSE NULL
1085
- END AS max_length,
1086
- des.description AS comment,
1087
- CASE att.atttypid
1088
- WHEN 21 THEN 16
1089
- WHEN 23 THEN 32
1090
- WHEN 20 THEN 64
1091
- WHEN 1700 THEN
1092
- CASE WHEN atttypmod = -1 THEN NULL
1093
- ELSE (((atttypmod - 4) >> 16) & 65535)::int4
1094
- END
1095
- WHEN 700 THEN 24
1096
- WHEN 701 THEN 53
1097
- ELSE NULL
1098
- END AS numeric_precision,
1099
- CASE
1100
- WHEN atttypid IN (21, 23, 20) THEN 0
1101
- WHEN atttypid = 1700 THEN
1102
- CASE
1103
- WHEN atttypmod = -1 THEN NULL
1104
- ELSE ((atttypmod - 4) & 65535)::int4
1105
- END
1106
- ELSE null
1107
- END AS numeric_scale
1108
- FROM
1109
- pg_attribute att
1110
- LEFT JOIN pg_class rel ON att.attrelid = rel.oid
1111
- LEFT JOIN pg_attrdef ad ON (att.attrelid, att.attnum) = (ad.adrelid, ad.adnum)
1112
- LEFT JOIN pg_description des ON (att.attrelid, att.attnum) = (des.objoid, des.objsubid)
1113
- WHERE
1114
- rel.relnamespace IN (${schemaIn})
1115
- ${table ? "AND rel.relname = ?" : ""}
1116
- ${column ? "AND att.attname = ?" : ""}
1117
- AND rel.relkind = 'r'
1118
- AND att.attnum > 0
1119
- AND NOT att.attisdropped
1120
- ORDER BY rel.relname, att.attnum;
1121
- `, bindings), knex$1.raw(`
1122
- SELECT
1123
- con.contype AS type,
1124
- rel.relname AS table,
1125
- att.attname AS column,
1126
- frel.relnamespace::regnamespace::text AS foreign_key_schema,
1127
- frel.relname AS foreign_key_table,
1128
- fatt.attname AS foreign_key_column,
1129
- CASE
1130
- WHEN con.contype = 'p' THEN pg_get_serial_sequence(att.attrelid::regclass::text, att.attname) != ''
1131
- ELSE NULL
1132
- END AS has_auto_increment
1133
- FROM
1134
- pg_constraint con
1135
- LEFT JOIN pg_class rel ON con.conrelid = rel.oid
1136
- LEFT JOIN pg_class frel ON con.confrelid = frel.oid
1137
- LEFT JOIN pg_attribute att ON att.attrelid = con.conrelid AND att.attnum = con.conkey[1]
1138
- LEFT JOIN pg_attribute fatt ON fatt.attrelid = con.confrelid AND fatt.attnum = con.confkey[1]
1139
- WHERE con.connamespace IN (${schemaIn})
1140
- AND array_length(con.conkey, 1) <= 1
1141
- AND (con.confkey IS NULL OR array_length(con.confkey, 1) = 1)
1142
- ${table ? "AND rel.relname = ?" : ""}
1143
- ${column ? "AND att.attname = ?" : ""}
1144
- `, bindings)]);
1145
- const parsedColumms = columns.rows.map((col) => {
1146
- const constraintsForColumn = constraints.rows.filter((constraint) => constraint.table === col.table && constraint.column === col.name);
1147
- const foreignKeyConstraint = constraintsForColumn.find((constraint) => constraint.type === "f");
1148
- return {
1149
- ...col,
1150
- is_unique: constraintsForColumn.some((constraint) => ["u", "p"].includes(constraint.type)),
1151
- is_primary_key: constraintsForColumn.some((constraint) => constraint.type === "p"),
1152
- has_auto_increment: constraintsForColumn.some((constraint) => constraint.has_auto_increment),
1153
- default_value: parseDefaultValue$1(col.default_value),
1154
- foreign_key_schema: (foreignKeyConstraint === null || foreignKeyConstraint === void 0 ? void 0 : foreignKeyConstraint.foreign_key_schema) ?? null,
1155
- foreign_key_table: (foreignKeyConstraint === null || foreignKeyConstraint === void 0 ? void 0 : foreignKeyConstraint.foreign_key_table) ?? null,
1156
- foreign_key_column: (foreignKeyConstraint === null || foreignKeyConstraint === void 0 ? void 0 : foreignKeyConstraint.foreign_key_column) ?? null
1157
- };
1158
- });
1159
- if (table && column) return parsedColumms[0];
1160
- return parsedColumms;
1161
- }
1162
- /**
1163
- * Check if the given table contains the given column
1164
- */
1165
- async hasColumn(table, column) {
1166
- const schemaIn = this.explodedSchema.map((schemaName) => `${this.knex.raw("?", [schemaName])}::regnamespace`);
1167
- return (await this.knex.raw(`
1168
- SELECT
1169
- att.attname AS column,
1170
- rel.relname AS table
1171
- FROM
1172
- pg_attribute att
1173
- LEFT JOIN pg_class rel ON att.attrelid = rel.oid
1174
- WHERE
1175
- rel.relnamespace IN (${schemaIn})
1176
- AND rel.relname = ?
1177
- AND att.attname = ?
1178
- AND rel.relkind = 'r'
1179
- AND att.attnum > 0
1180
- AND NOT att.attisdropped;
1181
- `, [table, column])).rows;
1182
- }
1183
- /**
1184
- * Get the primary key column for the given table
1185
- */
1186
- async primary(table) {
1187
- const schemaIn = this.explodedSchema.map((schemaName) => `${this.knex.raw("?", [schemaName])}::regnamespace`);
1188
- const result = await this.knex.raw(`
1189
- SELECT
1190
- att.attname AS column
1191
- FROM
1192
- pg_constraint con
1193
- LEFT JOIN pg_class rel ON con.conrelid = rel.oid
1194
- LEFT JOIN pg_attribute att ON att.attrelid = con.conrelid AND att.attnum = ANY(con.conkey)
1195
- WHERE con.connamespace IN (${schemaIn})
1196
- AND con.contype = 'p'
1197
- AND rel.relname = ?
1198
- `, [table]);
1199
- return result.rows.length !== 0 ? result.rows.length === 1 ? result.rows[0].column : result.rows.map((r) => r.column) : null;
1200
- }
1201
- async foreignKeys(table) {
1202
- const schemaIn = this.explodedSchema.map((schemaName) => `${this.knex.raw("?", [schemaName])}::regnamespace`);
1203
- const bindings = [];
1204
- if (table) bindings.push(table);
1205
- return (await this.knex.raw(`
1206
- SELECT
1207
- con.conname AS constraint_name,
1208
- rel.relname AS table,
1209
- att.attname AS column,
1210
- frel.relnamespace::regnamespace::text AS foreign_key_schema,
1211
- frel.relname AS foreign_key_table,
1212
- fatt.attname AS foreign_key_column,
1213
- CASE con.confupdtype
1214
- WHEN 'r' THEN
1215
- 'RESTRICT'
1216
- WHEN 'c' THEN
1217
- 'CASCADE'
1218
- WHEN 'n' THEN
1219
- 'SET NULL'
1220
- WHEN 'd' THEN
1221
- 'SET DEFAULT'
1222
- WHEN 'a' THEN
1223
- 'NO ACTION'
1224
- ELSE
1225
- NULL
1226
- END AS on_update,
1227
- CASE con.confdeltype
1228
- WHEN 'r' THEN
1229
- 'RESTRICT'
1230
- WHEN 'c' THEN
1231
- 'CASCADE'
1232
- WHEN 'n' THEN
1233
- 'SET NULL'
1234
- WHEN 'd' THEN
1235
- 'SET DEFAULT'
1236
- WHEN 'a' THEN
1237
- 'NO ACTION'
1238
- ELSE
1239
- NULL
1240
- END AS on_delete
1241
- FROM
1242
- pg_constraint con
1243
- LEFT JOIN pg_class rel ON con.conrelid = rel.oid
1244
- LEFT JOIN pg_class frel ON con.confrelid = frel.oid
1245
- LEFT JOIN pg_attribute att ON att.attrelid = con.conrelid AND att.attnum = con.conkey[1]
1246
- LEFT JOIN pg_attribute fatt ON fatt.attrelid = con.confrelid AND fatt.attnum = con.confkey[1]
1247
- WHERE con.connamespace IN (${schemaIn})
1248
- AND array_length(con.conkey, 1) <= 1
1249
- AND (con.confkey IS NULL OR array_length(con.confkey, 1) = 1)
1250
- AND con.contype = 'f'
1251
- ${table ? "AND rel.relname = ?" : ""}
1252
- `, bindings)).rows;
1253
- }
1254
- async uniqueConstraints(table) {
1255
- const { knex: knex$1 } = this;
1256
- const schemaIn = this.explodedSchema.map((schemaName) => `${this.knex.raw("?", [schemaName])}::regnamespace`);
1257
- const bindings = [];
1258
- if (table) bindings.push(table);
1259
- return (await knex$1.raw(`
1260
- SELECT
1261
- con.conrelid::regclass AS table_name,
1262
- con.conname AS constraint_name,
1263
- array_agg(a.attname ORDER BY k.n) AS columns
1264
- FROM
1265
- pg_constraint AS con
1266
- CROSS JOIN LATERAL unnest(con.conkey) WITH ORDINALITY AS k(con,n)
1267
- LEFT JOIN pg_class rel ON con.conrelid = rel.oid
1268
- JOIN pg_attribute AS a ON a.attnum = k.con AND a.attrelid = con.conrelid
1269
- WHERE con.contype = 'u'
1270
- AND con.connamespace IN (${schemaIn})
1271
- ${table ? "AND rel.relname = ?" : ""}
1272
- GROUP BY con.oid, con.conrelid, con.conname;
1273
- `, bindings)).rows.map((v) => {
1274
- const columns = Array.isArray(v.columns) ? v.columns : v.columns.substring(1, v.columns.length - 1).split(",");
1275
- return {
1276
- table: v.table_name,
1277
- constraint_name: v.constraint_name,
1278
- columns
1279
- };
1280
- });
1281
- }
1282
- };
1283
-
1284
- //#endregion
1285
- //#region src/inspector/utils/extract-max-length.ts
1286
- /**
1287
- * Extracts the length value out of a given datatype
1288
- * For example: `varchar(32)` => 32
1289
- */
1290
- function extractMaxLength(type) {
1291
- const matches = /\(([^)]+)\)/.exec(type);
1292
- if (matches && matches.length > 0 && matches[1]) return Number(matches[1]);
1293
- return null;
1294
- }
1295
-
1296
- //#endregion
1297
- //#region src/inspector/utils/extract-type.ts
1298
- /**
1299
- * Extracts the type out of a given datatype
1300
- * For example: `varchar(32)` => varchar
1301
- */
1302
- function extractType(type) {
1303
- return type.replace(/[^a-zA-Z]/g, "").toLowerCase();
1304
- }
1305
-
1306
- //#endregion
1307
- //#region src/casts/attribute.ts
1308
- var Attribute = class Attribute {
1309
- get;
1310
- set;
1311
- withCaching = false;
1312
- withObjectCaching = true;
1313
- constructor({ get: get$1 = null, set: set$1 = null }) {
1314
- this.get = get$1;
1315
- this.set = set$1;
1316
- }
1317
- static make({ get: get$1 = null, set: set$1 = null }) {
1318
- return new Attribute({
1319
- get: get$1,
1320
- set: set$1
1321
- });
1322
- }
1323
- static get(get$1) {
1324
- return new Attribute({ get: get$1 });
1325
- }
1326
- static set(set$1) {
1327
- return new Attribute({ set: set$1 });
1328
- }
1329
- withoutObjectCaching() {
1330
- this.withObjectCaching = false;
1331
- return this;
1332
- }
1333
- shouldCache() {
1334
- this.withCaching = true;
1335
- return this;
1336
- }
1337
- };
1338
- var attribute_default = Attribute;
1339
-
1340
- //#endregion
1341
- //#region src/errors.ts
1342
- var BaseError = class extends Error {
1343
- constructor(message, _entity) {
1344
- super(message);
1345
- Error.captureStackTrace(this, this.constructor);
1346
- this.name = this.constructor.name;
1347
- this.message = message;
1348
- }
1349
- };
1350
- var ModelNotFoundError = class extends BaseError {
1351
- model;
1352
- ids = [];
1353
- constructor() {
1354
- super("");
1355
- }
1356
- setModel(model, ids = []) {
1357
- this.model = model;
1358
- this.ids = (0, radashi.isArray)(ids) ? ids : [ids];
1359
- this.message = `No query results for model [${model}]`;
1360
- if (this.ids.length > 0) this.message += " " + this.ids.join(", ");
1361
- else this.message += ".";
1362
- return this;
1363
- }
1364
- getModel() {
1365
- return this.model;
1366
- }
1367
- getIds() {
1368
- return this.ids;
1369
- }
1370
- };
1371
- var RelationNotFoundError = class extends BaseError {};
1372
- var InvalidArgumentError = class extends BaseError {};
1373
-
1374
- //#endregion
1375
- //#region src/mixin.ts
1376
- var mixin_exports = /* @__PURE__ */ __export({ compose: () => compose$1 });
1377
- /**
1378
- * Compose function that merges multiple classes and mixins
1379
- *
1380
- * @example
1381
- * const SomePlugin = <TBase extends new (...args: any[]) => TGeneric> (Base: TBase) => {
1382
- * return class extends Base {
1383
- * pluginAttribtue = 'plugin'
1384
- * pluginMethod () {
1385
- * return this.pluginAttribtue
1386
- * }
1387
- * }
1388
- * }
1389
- *
1390
- * // Base class
1391
- * class Model {
1392
- * make () {
1393
- * console.log('make')
1394
- * }
1395
- * pluginMethod (id: string) {
1396
- * }
1397
- * }
1398
- *
1399
- * class User extends compose(
1400
- * Model,
1401
- * SomePlugin,
1402
- * ) {
1403
- * relationPosts () {
1404
- * return 'hasMany Posts'
1405
- * }
1406
- * }
1407
- *
1408
- * const user = new User()
1409
- * user.make() // from Model
1410
- * user.pluginMethod() // from SomePlugin
1411
- * user.relationPosts() // from User
1412
- *
1413
- * console.log(user.pluginMethod('w')) // "plugin"
1414
- * console.log(user.pluginMethod()) // "plugin"
1415
- * console.log(user.relationPosts()) // "hasMany Posts"
1416
- *
1417
- * @param Base
1418
- * @param mixins
1419
- * @returns
1420
- */
1421
- function compose$1(Base, ...mixins) {
1422
- /**
1423
- * Apply each mixin or class in sequence
1424
- */
1425
- return mixins.reduce((acc, mixin) => {
1426
- if (typeof mixin === "function" && mixin.prototype)
1427
- /**
1428
- * If it's a class constructor, extend it
1429
- */
1430
- return class extends acc {
1431
- constructor(...args) {
1432
- super(...args);
1433
- /**
1434
- * Copy instance properties from mixin prototype
1435
- */
1436
- Object.getOwnPropertyNames(mixin.prototype).forEach((name) => {
1437
- if (name !== "constructor") Object.defineProperty(this, name, Object.getOwnPropertyDescriptor(mixin.prototype, name));
1438
- });
1439
- }
1440
- };
1441
- else if (typeof mixin === "function")
1442
- /**
1443
- * If it's a mixin function, call it with current class
1444
- */
1445
- return mixin(acc);
1446
- return acc;
1447
- }, Base);
1448
- }
1449
-
1450
- //#endregion
1451
- //#region src/utils.ts
1452
- dayjs.default.extend(dayjs_plugin_advancedFormat_js.default);
1453
- const getRelationName = (relationMethod) => {
1454
- return (0, radashi.snake)(relationMethod.substring(8));
1455
- };
1456
- const getRelationMethod = (relation) => {
1457
- return (0, radashi.camel)(`relation_${relation}`);
1458
- };
1459
- const getScopeMethod = (scope) => {
1460
- return (0, radashi.camel)(`scope_${scope}`);
1461
- };
1462
- const getAttrMethod = (attr) => {
1463
- return (0, radashi.camel)(`attribute_${attr}`);
1464
- };
1465
- const getGetterMethod = (attr) => {
1466
- return (0, radashi.camel)(`get_${attr}_attribute`);
1467
- };
1468
- const getSetterMethod = (attr) => {
1469
- return (0, radashi.camel)(`set_${attr}_attribute`);
1470
- };
1471
- /**
1472
- * Tap into a model a collection instance
1473
- *
1474
- * @param instance
1475
- * @param callback
1476
- * @returns
1477
- */
1478
- const tap = (instance, callback) => {
1479
- const result = callback(instance);
1480
- return result instanceof Promise ? result.then(() => instance) : instance;
1481
- };
1482
- const { compose } = mixin_exports;
1483
- const flatten = (arr) => arr.flat();
1484
- const flattenDeep = (arr) => Array.isArray(arr) ? arr.reduce((a, b) => a.concat(flattenDeep(b)), []) : [arr];
1485
- const snakeCase = (str) => (0, radashi.trim)((0, radashi.snake)(str.replace(/[^a-zA-Z0-9_-]/g, "-")), "_-");
1486
-
1487
- //#endregion
1488
- //#region src/relations/relation.ts
1489
- var Relation = class {
1490
- query;
1491
- parent;
1492
- related;
1493
- eagerKeysWereEmpty = false;
1494
- static constraints = true;
1495
- static selfJoinCount = 0;
1496
- constructor(query, parent) {
1497
- this.query = query;
1498
- this.parent = parent;
1499
- this.related = this.query.model;
1500
- }
1501
- static extend(trait) {
1502
- for (const methodName in trait) this.prototype[methodName] = trait[methodName];
1503
- }
1504
- static noConstraints(callback) {
1505
- const previous = this.constraints;
1506
- this.constraints = false;
1507
- try {
1508
- return callback();
1509
- } finally {
1510
- this.constraints = previous;
1511
- }
1512
- }
1513
- asProxy() {
1514
- return new Proxy(this, { get: function(target, prop) {
1515
- if (typeof target[prop] !== "undefined") return target[prop];
1516
- if (typeof prop === "string") {
1517
- if (typeof target.query[prop] === "function") return (...args) => {
1518
- target.query[prop](...args);
1519
- return target.asProxy();
1520
- };
1521
- }
1522
- } });
1523
- }
1524
- getRelated() {
1525
- return this.related;
1526
- }
1527
- getKeys(models, key) {
1528
- return models.map((model) => key ? model.attributes[key] : model.getKey()).sort();
1529
- }
1530
- getRelationQuery() {
1531
- return this.query;
1532
- }
1533
- whereInEager(whereIn, key, modelKeys, query = null) {
1534
- (query || this.query)[whereIn](key, modelKeys);
1535
- if (modelKeys.length === 0) this.eagerKeysWereEmpty = true;
1536
- }
1537
- whereInMethod(model, key) {
1538
- return "whereIn";
1539
- }
1540
- getEager() {
1541
- return this.eagerKeysWereEmpty ? this.query.getModel().newCollection() : this.get();
1542
- }
1543
- async get(columns = ["*"]) {
1544
- return await this.query.get(columns);
1545
- }
1546
- async first(columns = ["*"]) {
1547
- return await this.query.first(columns);
1548
- }
1549
- async paginate(...args) {
1550
- return await this.query.paginate(...args);
1551
- }
1552
- async count(...args) {
1553
- return await this.query.clearSelect().count(...args);
1554
- }
1555
- toSql() {
1556
- return this.query.toSql();
1557
- }
1558
- addConstraints() {}
1559
- getRelationCountHash(incrementJoinCount = true) {
1560
- return "arquebus_reserved_" + (incrementJoinCount ? this.constructor.selfJoinCount++ : this.constructor.selfJoinCount);
1561
- }
1562
- getRelationExistenceQuery(query, parentQuery, columns = ["*"]) {
1563
- return query.select(columns).whereColumn(this.getQualifiedParentKeyName(), "=", this.getExistenceCompareKey());
1564
- }
1565
- getRelationExistenceCountQuery(query, parentQuery) {
1566
- const db = this.related.getConnection();
1567
- return this.getRelationExistenceQuery(query, parentQuery, db.raw("count(*)"));
1568
- }
1569
- getQualifiedParentKeyName() {
1570
- return this.parent.getQualifiedKeyName();
1571
- }
1572
- getExistenceCompareKey() {
1573
- var _this$getQualifiedFor;
1574
- return (_this$getQualifiedFor = this.getQualifiedForeignKeyName) === null || _this$getQualifiedFor === void 0 ? void 0 : _this$getQualifiedFor.call(this);
1575
- }
1576
- };
1577
- var relation_default = Relation;
1578
-
1579
- //#endregion
1580
- //#region src/relations/concerns/supports-default-models.ts
1581
- const SupportsDefaultModels = (Relation$1) => {
1582
- return class extends Relation$1 {
1583
- _withDefault;
1584
- withDefault(callback = true) {
1585
- this._withDefault = callback;
1586
- return this;
1587
- }
1588
- getDefaultFor(parent) {
1589
- if (!this._withDefault) return null;
1590
- const instance = this.newRelatedInstanceFor(parent);
1591
- if (typeof this._withDefault === "function") return this._withDefault(instance, parent) || instance;
1592
- if (typeof this._withDefault === "object") for (const key in this._withDefault) instance.setAttribute(key, this._withDefault[key]);
1593
- return instance;
1594
- }
1595
- };
1596
- };
1597
- var supports_default_models_default = SupportsDefaultModels;
1598
-
1599
- //#endregion
1600
- //#region src/relations/belongs-to.ts
1601
- var BelongsTo = class extends compose(relation_default, supports_default_models_default) {
1602
- foreignKey;
1603
- ownerKey;
1604
- child;
1605
- relationName;
1606
- constructor(query, child, foreignKey, ownerKey, relationName) {
1607
- super(query, child);
1608
- this.foreignKey = foreignKey;
1609
- this.ownerKey = ownerKey;
1610
- this.child = child;
1611
- this.relationName = relationName;
1612
- this.addConstraints();
1613
- return this.asProxy();
1614
- }
1615
- async getResults() {
1616
- if (this.child[this.foreignKey] === null) return this.getDefaultFor(this.parent);
1617
- return await this.query.first() || this.getDefaultFor(this.parent);
1618
- }
1619
- match(models, results, relation) {
1620
- const foreign = this.foreignKey;
1621
- const owner = this.ownerKey;
1622
- const dictionary = {};
1623
- results.map((result) => {
1624
- const attribute = result.attributes[owner];
1625
- dictionary[attribute] = result;
1626
- });
1627
- models.map((model) => {
1628
- const attribute = model[foreign];
1629
- if (dictionary[attribute] !== void 0) model.setRelation(relation, dictionary[attribute]);
1630
- });
1631
- return models;
1632
- }
1633
- getQualifiedForeignKeyName() {
1634
- return this.child.qualifyColumn(this.foreignKey);
1635
- }
1636
- getRelationExistenceQuery(query, parentQuery, columns = ["*"]) {
1637
- if (parentQuery.getQuery()._single.table === query.getQuery()._single.table) return this.getRelationExistenceQueryForSelfRelation(query, parentQuery, columns);
1638
- return query.select(columns).whereColumn(this.getQualifiedForeignKeyName(), "=", query.qualifyColumn(this.ownerKey));
1639
- }
1640
- getRelationExistenceQueryForSelfRelation(query, parentQuery, columns = ["*"]) {
1641
- const hash = this.getRelationCountHash();
1642
- query.select(columns).from(query.getModel().getTable() + " as " + hash);
1643
- query.getModel().setTable(hash);
1644
- return query.whereColumn(`${hash}.${this.ownerKey}`, "=", this.getQualifiedForeignKeyName());
1645
- }
1646
- initRelation(models, relation) {
1647
- models.forEach((model) => {
1648
- model.setRelation(relation, this.getDefaultFor(model));
1649
- });
1650
- return models;
1651
- }
1652
- addEagerConstraints(models) {
1653
- const key = `${this.related.getTable()}.${this.ownerKey}`;
1654
- this.query.whereIn(key, this.getEagerModelKeys(models));
1655
- }
1656
- getEagerModelKeys(models) {
1657
- const keys = [];
1658
- models.forEach((model) => {
1659
- const value = model[this.foreignKey];
1660
- if (value !== null && value !== void 0) keys.push(value);
1661
- });
1662
- keys.sort();
1663
- return [...new Set(keys)];
1664
- }
1665
- associate(model) {
1666
- const ownerKey = model instanceof Model ? model.attributes[this.ownerKey] : model;
1667
- this.child[this.foreignKey] = ownerKey;
1668
- if (model instanceof Model) this.child.setRelation(this.relationName, model);
1669
- else this.child.unsetRelation(this.relationName);
1670
- return this.child;
1671
- }
1672
- dissociate() {
1673
- this.child[this.foreignKey] = null;
1674
- return this.child.setRelation(this.relationName, null);
1675
- }
1676
- addConstraints() {
1677
- if (this.constructor.constraints) {
1678
- const table = this.related.getTable();
1679
- this.query.where(`${table}.${this.ownerKey}`, "=", this.child[this.foreignKey]);
1680
- }
1681
- }
1682
- newRelatedInstanceFor(_parent) {
1683
- return this.related.newInstance();
1684
- }
1685
- };
1686
- var belongs_to_default = BelongsTo;
1687
-
1688
- //#endregion
1689
- //#region src/casts-attributes.ts
1690
- var CastsAttributes = class CastsAttributes {
1691
- constructor() {
1692
- if (this.constructor === CastsAttributes) throw new Error("CastsAttributes cannot be instantiated");
1693
- }
1694
- static get(_model, _key, _value, _attributes) {
1695
- throw new Error("get not implemented");
1696
- }
1697
- static set(_model, _key, _value, _attributes) {
1698
- throw new Error("set not implemented");
1699
- }
1700
- };
1701
- var casts_attributes_default = CastsAttributes;
1702
-
1703
- //#endregion
1704
- //#region src/concerns/has-attributes.ts
1705
- const HasAttributes = (Model$1) => {
1706
- return class extends Model$1 {
1707
- static castTypeCache = {};
1708
- attributes = {};
1709
- original = {};
1710
- casts = {};
1711
- changes = {};
1712
- appends = [];
1713
- setAppends(appends) {
1714
- this.appends = appends;
1715
- return this;
1716
- }
1717
- append(...keys) {
1718
- const appends = flattenDeep(keys);
1719
- this.appends = [...this.appends, ...appends];
1720
- return this;
1721
- }
1722
- normalizeCastClassResponse(key, value) {
1723
- var _value$constructor;
1724
- return (value === null || value === void 0 || (_value$constructor = value.constructor) === null || _value$constructor === void 0 ? void 0 : _value$constructor.name) === "Object" ? value : { [key]: value };
1725
- }
1726
- syncOriginal() {
1727
- this.original = this.getAttributes();
1728
- return this;
1729
- }
1730
- syncChanges() {
1731
- this.changes = this.getDirty();
1732
- return this;
1733
- }
1734
- syncOriginalAttribute(attribute) {
1735
- this.syncOriginalAttributes(attribute);
1736
- }
1737
- syncOriginalAttributes(...attributes) {
1738
- attributes = flattenDeep(attributes);
1739
- const modelAttributes = this.getAttributes();
1740
- for (const attribute of attributes) this.original[attribute] = modelAttributes[attribute];
1741
- return this;
1742
- }
1743
- isDirty(...attributes) {
1744
- const changes = this.getDirty();
1745
- attributes = flattenDeep(attributes);
1746
- if (attributes.length === 0) return Object.keys(changes).length > 0;
1747
- for (const attribute of attributes) if (attribute in changes) return true;
1748
- return false;
1749
- }
1750
- getDirty() {
1751
- const dirty = {};
1752
- const attributes = this.getAttributes();
1753
- for (const key in attributes) {
1754
- const value = attributes[key];
1755
- if (!this.originalIsEquivalent(key)) dirty[key] = value;
1756
- }
1757
- return dirty;
1758
- }
1759
- originalIsEquivalent(key) {
1760
- if (this.original[key] === void 0) return false;
1761
- const attribute = this.attributes[key];
1762
- const original = this.original[key];
1763
- if (attribute === original) return true;
1764
- else return false;
1765
- }
1766
- setAttributes(attributes) {
1767
- this.attributes = { ...attributes };
1768
- }
1769
- setRawAttributes(attributes, sync = false) {
1770
- this.attributes = attributes;
1771
- if (sync) this.syncOriginal();
1772
- return this;
1773
- }
1774
- getAttributes() {
1775
- return { ...this.attributes };
1776
- }
1777
- setAttribute(key, value) {
1778
- const setterMethod = getSetterMethod(key);
1779
- if (typeof this[setterMethod] === "function") {
1780
- this[setterMethod](value);
1781
- return this;
1782
- }
1783
- const attrMethod = getAttrMethod(key);
1784
- if (typeof this[attrMethod] === "function") {
1785
- const callback = this[attrMethod]().set || ((value$1) => {
1786
- this.attributes[key] = value$1;
1787
- });
1788
- this.attributes = {
1789
- ...this.attributes,
1790
- ...this.normalizeCastClassResponse(key, callback(value, this.attributes))
1791
- };
1792
- return this;
1793
- }
1794
- const castType = this.getCasts()[key];
1795
- if (this.isCustomCast(castType) && typeof castType !== "string") value = castType.set(this, key, value, this.attributes) ?? "";
1796
- if (castType === "json") value = JSON.stringify(value);
1797
- if (castType === "collection") value = JSON.stringify(value);
1798
- if (value !== null && this.isDateAttribute(key)) value = this.fromDateTime(value);
1799
- this.attributes[key] = value;
1800
- return this;
1801
- }
1802
- getAttribute(key) {
1803
- if (!key) return;
1804
- const getterMethod = getGetterMethod(key);
1805
- if (typeof this[getterMethod] === "function") return this[getterMethod](this.attributes[key], this.attributes);
1806
- const attrMethod = getAttrMethod(key);
1807
- if (typeof this[attrMethod] === "function") return this[attrMethod]().get(this.attributes[key], this.attributes);
1808
- if (key in this.attributes) {
1809
- if (this.hasCast(key)) return this.castAttribute(key, this.attributes[key]);
1810
- if (this.getDates().includes(key)) return this.asDateTime(this.attributes[key]);
1811
- return this.attributes[key];
1812
- }
1813
- if (key in this.relations) return this.relations[key];
1814
- }
1815
- castAttribute(key, value) {
1816
- const castType = this.getCastType(key);
1817
- if (!castType) return value;
1818
- if (value === null) return value;
1819
- switch (castType) {
1820
- case "int":
1821
- case "integer": return parseInt(value);
1822
- case "real":
1823
- case "float":
1824
- case "double": return parseFloat(value);
1825
- case "decimal": return this.asDecimal(value, castType.split(":")[1]);
1826
- case "string": return String(value);
1827
- case "bool":
1828
- case "boolean": return Boolean(value);
1829
- case "object":
1830
- case "json": try {
1831
- return JSON.parse(value);
1832
- } catch {
1833
- return null;
1834
- }
1835
- case "collection": try {
1836
- return (0, collect_js.default)(JSON.parse(value));
1837
- } catch {
1838
- return (0, collect_js.default)([]);
1839
- }
1840
- case "date": return this.asDate(value);
1841
- case "datetime":
1842
- case "custom_datetime": return this.asDateTime(value);
1843
- case "timestamp": return this.asTimestamp(value);
1844
- }
1845
- if (this.isCustomCast(castType)) return castType.get(this, key, value, this.attributes);
1846
- return value;
1847
- }
1848
- attributesToData() {
1849
- let attributes = { ...this.attributes };
1850
- for (const key in attributes) {
1851
- if (this.hidden.includes(key)) attributes = (0, radashi.omit)(attributes, [key]);
1852
- if (this.visible.length > 0 && this.visible.includes(key) === false) attributes = (0, radashi.omit)(attributes, [key]);
1853
- }
1854
- for (const key of this.getDates()) {
1855
- if (attributes[key] === void 0) continue;
1856
- attributes[key] = this.serializeDate(this.asDateTime(attributes[key]));
1857
- }
1858
- const casts = this.getCasts();
1859
- for (const key in casts) {
1860
- const value = casts[key];
1861
- if (key in attributes === false) continue;
1862
- attributes[key] = this.castAttribute(key, attributes[key]);
1863
- if (key in attributes && ["date", "datetime"].includes(String(value))) attributes[key] = this.serializeDate(attributes[key]);
1864
- if (key in attributes && this.isCustomDateTimeCast(value)) attributes[key] = (0, dayjs.default)(attributes[key]).format(String(value).split(":")[1]);
1865
- }
1866
- for (const key of this.appends) attributes[key] = this.mutateAttribute(key, null);
1867
- return attributes;
1868
- }
1869
- mutateAttribute(key, value) {
1870
- if (typeof this[getGetterMethod(key)] === "function") return this[getGetterMethod(key)](value);
1871
- else if (typeof this[getAttrMethod(key)] === "function") return this[getAttrMethod(key)]().get(key, this.attributes);
1872
- else if (key in this) return this[key];
1873
- return value;
1874
- }
1875
- mutateAttributeForArray(_key, _value) {}
1876
- isDateAttribute(key) {
1877
- return this.getDates().includes(key) || this.isDateCastable(key);
1878
- }
1879
- serializeDate(date) {
1880
- return date ? (0, dayjs.default)(date).toISOString() : null;
1881
- }
1882
- getDates() {
1883
- return this.usesTimestamps() ? [this.getCreatedAtColumn(), this.getUpdatedAtColumn()] : [];
1884
- }
1885
- getCasts() {
1886
- if (this.getIncrementing()) return {
1887
- [this.getKeyName()]: this.getKeyType(),
1888
- ...this.casts
1889
- };
1890
- return this.casts;
1891
- }
1892
- getCastType(key) {
1893
- const castType = this.getCasts()[key];
1894
- let castTypeCacheKey;
1895
- if (typeof castType === "string") castTypeCacheKey = castType;
1896
- else if (new castType() instanceof casts_attributes_default) castTypeCacheKey = castType.name;
1897
- if (castTypeCacheKey && this.getConstructor().castTypeCache[castTypeCacheKey] !== void 0) return this.getConstructor().castTypeCache[castTypeCacheKey];
1898
- let convertedCastType;
1899
- if (this.isCustomDateTimeCast(castType)) convertedCastType = "custom_datetime";
1900
- else if (this.isDecimalCast(castType)) convertedCastType = "decimal";
1901
- else if (this.isCustomCast(castType)) convertedCastType = castType;
1902
- else convertedCastType = String(castType).toLocaleLowerCase().trim();
1903
- return this.getConstructor()[castTypeCacheKey] = convertedCastType;
1904
- }
1905
- hasCast(key, types = []) {
1906
- if (key in this.casts) {
1907
- types = (0, radashi.flat)(types);
1908
- return types.length > 0 ? types.includes(this.getCastType(key)) : true;
1909
- }
1910
- return false;
1911
- }
1912
- withDayjs(date) {
1913
- return (0, dayjs.default)(date);
1914
- }
1915
- isCustomCast(cast) {
1916
- return typeof cast === "function" && new cast() instanceof casts_attributes_default;
1917
- }
1918
- isCustomDateTimeCast(cast) {
1919
- if (typeof cast !== "string") return false;
1920
- return cast.startsWith("date:") || cast.startsWith("datetime:");
1921
- }
1922
- isDecimalCast(cast) {
1923
- if (typeof cast !== "string") return false;
1924
- return cast.startsWith("decimal:");
1925
- }
1926
- isDateCastable(key) {
1927
- return this.hasCast(key, ["date", "datetime"]);
1928
- }
1929
- fromDateTime(value) {
1930
- return (0, dayjs.default)(this.asDateTime(value)).format(this.getDateFormat());
1931
- }
1932
- getDateFormat() {
1933
- return this.dateFormat || "YYYY-MM-DD HH:mm:ss";
1934
- }
1935
- asDecimal(value, decimals) {
1936
- return parseFloat(value).toFixed(decimals);
1937
- }
1938
- asDateTime(value) {
1939
- if (value === null) return null;
1940
- if (value instanceof Date) return value;
1941
- if (typeof value === "number") return /* @__PURE__ */ new Date(value * 1e3);
1942
- return new Date(value);
1943
- }
1944
- asDate(value) {
1945
- const date = this.asDateTime(value);
1946
- return (0, dayjs.default)(date).startOf("day").toDate();
1947
- }
1948
- };
1949
- };
1950
- var has_attributes_default = HasAttributes;
1951
-
1952
- //#endregion
1953
- //#region src/scope.ts
1954
- var Scope = class Scope {
1955
- constructor() {
1956
- if (this.constructor === Scope) throw new Error("Scope cannot be instantiated");
1957
- }
1958
- apply(_builder, _model) {
1959
- throw new Error("apply not implemented");
1960
- }
1961
- };
1962
- var scope_default = Scope;
1963
-
1964
- //#endregion
1965
- //#region src/concerns/has-global-scopes.ts
1966
- const HasGlobalScopes = (Model$1) => {
1967
- return class extends Model$1 {
1968
- static globalScopes;
1969
- static addGlobalScope(scope, implementation = null) {
1970
- if (typeof scope === "string" && implementation instanceof scope_default) {
1971
- this.globalScopes = (0, radashi.set)(this.globalScopes ?? {}, this.name + "." + scope, implementation);
1972
- return implementation;
1973
- } else if (scope instanceof scope_default) {
1974
- this.globalScopes = (0, radashi.set)(this.globalScopes ?? {}, this.name + "." + scope.constructor.name, scope);
1975
- return scope;
1976
- }
1977
- throw new InvalidArgumentError("Global scope must be an instance of Scope.");
1978
- }
1979
- static hasGlobalScope(scope) {
1980
- return this.getGlobalScope(scope) !== null;
1981
- }
1982
- static getGlobalScope(scope) {
1983
- if (typeof scope === "string") return (0, radashi.get)(this.globalScopes, this.name + "." + scope);
1984
- return (0, radashi.get)(this.globalScopes, this.name + "." + scope.constructor.name);
1985
- }
1986
- static getAllGlobalScopes() {
1987
- return this.globalScopes;
1988
- }
1989
- static setAllGlobalScopes(scopes) {
1990
- this.globalScopes = scopes;
1991
- }
1992
- getGlobalScopes() {
1993
- return (0, radashi.get)(this.constructor.globalScopes, this.constructor.name, {});
1994
- }
1995
- };
1996
- };
1997
- var has_global_scopes_default = HasGlobalScopes;
1998
-
1999
- //#endregion
2000
- //#region src/hooks.ts
2001
- var Hooks = class {
2002
- hooks = {
2003
- creating: [],
2004
- created: [],
2005
- updating: [],
2006
- updated: [],
2007
- saving: [],
2008
- saved: [],
2009
- deleting: [],
2010
- deleted: [],
2011
- restoring: [],
2012
- restored: [],
2013
- trashed: [],
2014
- forceDeleting: [],
2015
- forceDeleted: []
2016
- };
2017
- add(hook, callback) {
2018
- this.hooks[hook].push(callback);
2019
- }
2020
- async exec(hook, data) {
2021
- const callbacks = this.hooks[hook] ?? [];
2022
- for (const callback of callbacks) await callback(...data);
2023
- return true;
2024
- }
2025
- };
2026
- var hooks_default = Hooks;
2027
-
2028
- //#endregion
2029
- //#region src/concerns/has-hooks.ts
2030
- const HasHooks = (Model$1) => {
2031
- return class extends Model$1 {
2032
- static hooks = null;
2033
- static addHook(hook, callback) {
2034
- if (this.hooks instanceof hooks_default === false) this.hooks = new hooks_default();
2035
- this.hooks.add(hook, callback);
2036
- }
2037
- static creating(callback) {
2038
- this.addHook("creating", callback);
2039
- }
2040
- static created(callback) {
2041
- this.addHook("created", callback);
2042
- }
2043
- static updating(callback) {
2044
- this.addHook("updating", callback);
2045
- }
2046
- static updated(callback) {
2047
- this.addHook("updated", callback);
2048
- }
2049
- static saving(callback) {
2050
- this.addHook("saving", callback);
2051
- }
2052
- static saved(callback) {
2053
- this.addHook("saved", callback);
2054
- }
2055
- static deleting(callback) {
2056
- this.addHook("deleting", callback);
2057
- }
2058
- static deleted(callback) {
2059
- this.addHook("deleted", callback);
2060
- }
2061
- static restoring(callback) {
2062
- this.addHook("restoring", callback);
2063
- }
2064
- static restored(callback) {
2065
- this.addHook("restored", callback);
2066
- }
2067
- static trashed(callback) {
2068
- this.addHook("trashed", callback);
2069
- }
2070
- static forceDeleted(callback) {
2071
- this.addHook("forceDeleted", callback);
2072
- }
2073
- async execHooks(hook, options) {
2074
- if (this.constructor.hooks instanceof hooks_default === false) return;
2075
- return await this.constructor.hooks.exec(hook, [this, options]);
2076
- }
2077
- };
2078
- };
2079
- var has_hooks_default = HasHooks;
2080
-
2081
- //#endregion
2082
- //#region src/relations/has-one-or-many.ts
2083
- const HasOneOrMany = (Relation$1) => {
2084
- return class extends Relation$1 {
2085
- getRelationValue(dictionary, key, type) {
2086
- const value = dictionary[key];
2087
- return type === "one" ? value[0] : new collection_default(value);
2088
- }
2089
- matchOneOrMany(models, results, relation, type) {
2090
- const dictionary = this.buildDictionary(results);
2091
- models.map((model) => {
2092
- const key = model.attributes[this.localKey];
2093
- if (dictionary[key] !== void 0) model.setRelation(relation, this.getRelationValue(dictionary, key, type));
2094
- });
2095
- return models;
2096
- }
2097
- buildDictionary(results) {
2098
- const foreign = this.getForeignKeyName();
2099
- return (0, collect_js.default)(results).mapToDictionary((result) => [result[foreign], result]).all();
2100
- }
2101
- async save(model) {
2102
- this.setForeignAttributesForCreate(model);
2103
- return await model.save() ? model : false;
2104
- }
2105
- async saveMany(models) {
2106
- await Promise.all(models.map(async (model) => {
2107
- await this.save(model);
2108
- }));
2109
- return models instanceof collection_default ? models : new collection_default(models);
2110
- }
2111
- async create(attributes = {}) {
2112
- return await tap(this.related.constructor.init(attributes), async (instance) => {
2113
- this.setForeignAttributesForCreate(instance);
2114
- await instance.save();
2115
- });
2116
- }
2117
- async createMany(records) {
2118
- const instances = await Promise.all(records.map(async (record) => {
2119
- return await this.create(record);
2120
- }));
2121
- return instances instanceof collection_default ? instances : new collection_default(instances);
2122
- }
2123
- setForeignAttributesForCreate(model) {
2124
- model[this.getForeignKeyName()] = this.getParentKey();
2125
- }
2126
- getForeignKeyName() {
2127
- const segments = this.getQualifiedForeignKeyName().split(".");
2128
- return segments[segments.length - 1];
2129
- }
2130
- getParentKey() {
2131
- return this.parent.attributes[this.localKey];
2132
- }
2133
- getQualifiedForeignKeyName() {
2134
- return this.foreignKey;
2135
- }
2136
- getExistenceCompareKey() {
2137
- return this.getQualifiedForeignKeyName();
2138
- }
2139
- addConstraints() {
2140
- if (this.constructor.constraints) {
2141
- const query = this.getRelationQuery();
2142
- query.where(this.foreignKey, "=", this.getParentKey());
2143
- query.whereNotNull(this.foreignKey);
2144
- }
2145
- }
2146
- };
2147
- };
2148
- var has_one_or_many_default = HasOneOrMany;
2149
-
2150
- //#endregion
2151
- //#region src/relations/has-many.ts
2152
- var HasMany = class extends compose(relation_default, has_one_or_many_default) {
2153
- foreignKey;
2154
- localKey;
2155
- constructor(query, parent, foreignKey, localKey) {
2156
- super(query, parent);
2157
- this.foreignKey = foreignKey;
2158
- this.localKey = localKey;
2159
- this.addConstraints();
2160
- return this.asProxy();
2161
- }
2162
- initRelation(models, relation) {
2163
- models.map((model) => {
2164
- model.setRelation(relation, new collection_default([]));
2165
- });
2166
- return models;
2167
- }
2168
- async getResults() {
2169
- return this.getParentKey() !== null ? await this.query.get() : new collection_default([]);
2170
- }
2171
- getForeignKeyName() {
2172
- var _this$foreignKey;
2173
- const segments = (_this$foreignKey = this.foreignKey) === null || _this$foreignKey === void 0 ? void 0 : _this$foreignKey.split(".");
2174
- return segments === null || segments === void 0 ? void 0 : segments.pop();
2175
- }
2176
- buildDictionary(results) {
2177
- const foreign = this.getForeignKeyName();
2178
- return (0, collect_js.collect)(results).mapToDictionary((result) => [result[foreign], result]).all();
2179
- }
2180
- match(models, results, relation) {
2181
- return this.matchOneOrMany(models, results, relation, "many");
2182
- }
2183
- addEagerConstraints(models) {
2184
- this.query.whereIn(this.foreignKey, this.getKeys(models, this.localKey));
2185
- }
2186
- };
2187
- var has_many_default = HasMany;
2188
-
2189
- //#endregion
2190
- //#region src/relations/has-one.ts
2191
- var HasOne = class extends compose(relation_default, has_one_or_many_default, supports_default_models_default) {
2192
- foreignKey;
2193
- localKey;
2194
- constructor(query, parent, foreignKey, localKey) {
2195
- super(query, parent);
2196
- this.foreignKey = foreignKey;
2197
- this.localKey = localKey;
2198
- this.addConstraints();
2199
- return this.asProxy();
2200
- }
2201
- initRelation(models, relation) {
2202
- models.map((model) => {
2203
- model.setRelation(relation, this.getDefaultFor(model));
2204
- });
2205
- return models;
2206
- }
2207
- matchOne(models, results, relation) {
2208
- return this.matchOneOrMany(models, results, relation, "one");
2209
- }
2210
- getForeignKeyName() {
2211
- var _this$foreignKey;
2212
- const segments = (_this$foreignKey = this.foreignKey) === null || _this$foreignKey === void 0 ? void 0 : _this$foreignKey.split(".");
2213
- return segments === null || segments === void 0 ? void 0 : segments.pop();
2214
- }
2215
- async getResults() {
2216
- if (this.getParentKey() === null) return this.getDefaultFor(this.parent);
2217
- return await this.query.first() || this.getDefaultFor(this.parent);
2218
- }
2219
- match(models, results, relation) {
2220
- return this.matchOneOrMany(models, results, relation, "one");
2221
- }
2222
- addEagerConstraints(models) {
2223
- this.query.whereIn(this.foreignKey, this.getKeys(models, this.localKey));
2224
- }
2225
- newRelatedInstanceFor(parent) {
2226
- return this.related.newInstance().setAttribute(this.getForeignKeyName(), parent[this.localKey]);
2227
- }
2228
- };
2229
- var has_one_default = HasOne;
2230
-
2231
- //#endregion
2232
- //#region src/relations/has-many-through.ts
2233
- var HasManyThrough = class extends relation_default {
2234
- throughParent;
2235
- farParent;
2236
- firstKey;
2237
- secondKey;
2238
- localKey;
2239
- secondLocalKey;
2240
- constructor(query, farParent, throughParent, firstKey, secondKey, localKey, secondLocalKey) {
2241
- super(query, throughParent);
2242
- this.localKey = localKey;
2243
- this.firstKey = firstKey;
2244
- this.secondKey = secondKey;
2245
- this.farParent = farParent;
2246
- this.throughParent = throughParent;
2247
- this.secondLocalKey = secondLocalKey;
2248
- return this.asProxy();
2249
- }
2250
- addConstraints() {
2251
- const localValue = this.farParent[this.localKey];
2252
- this.performJoin();
2253
- if (this.constructor.constraints) this.query.where(this.getQualifiedFirstKeyName(), "=", localValue);
2254
- }
2255
- performJoin(query = null) {
2256
- query = query || this.query;
2257
- const farKey = this.getQualifiedFarKeyName();
2258
- query.join(this.throughParent.getTable(), this.getQualifiedParentKeyName(), "=", farKey);
2259
- if (this.throughParentSoftDeletes()) query.withGlobalScope("SoftDeletableHasManyThrough", (query$1) => {
2260
- query$1.whereNull(this.throughParent.getQualifiedDeletedAtColumn());
2261
- });
2262
- }
2263
- getQualifiedParentKeyName() {
2264
- return this.parent.qualifyColumn(this.secondLocalKey);
2265
- }
2266
- throughParentSoftDeletes() {
2267
- return this.throughParent.pluginInitializers["SoftDeletes"] !== void 0;
2268
- }
2269
- withTrashedParents() {
2270
- this.query.withoutGlobalScope("SoftDeletableHasManyThrough");
2271
- return this;
2272
- }
2273
- addEagerConstraints(models) {
2274
- const whereIn = this.whereInMethod(this.farParent, this.localKey);
2275
- this.whereInEager(whereIn, this.getQualifiedFirstKeyName(), this.getKeys(models, this.localKey));
2276
- }
2277
- initRelation(models, relation) {
2278
- for (const model of models) model.setRelation(relation, this.related.newCollection());
2279
- return models;
2280
- }
2281
- match(models, results, relation) {
2282
- const dictionary = this.buildDictionary(results);
2283
- for (const model of models) {
2284
- const key = this.getDictionaryKey(model.getAttribute(this.localKey));
2285
- if (dictionary[key] !== void 0) model.setRelation(relation, this.related.newCollection(dictionary[key]));
2286
- }
2287
- return models;
2288
- }
2289
- buildDictionary(results) {
2290
- const dictionary = {};
2291
- for (const result of results) {
2292
- if (dictionary[result.laravel_through_key] === void 0) dictionary[result.laravel_through_key] = [];
2293
- dictionary[result.laravel_through_key].push(result);
2294
- }
2295
- return dictionary;
2296
- }
2297
- async firstOrNew(attributes) {
2298
- return await this.where(attributes).first() || this.related.newInstance(attributes);
2299
- }
2300
- async updateOrCreate(attributes, values = {}) {
2301
- return tap(await this.firstOrCreate(attributes, values), async (instance) => {
2302
- if (!instance.wasRecentlyCreated) await instance.fill(values).save();
2303
- });
2304
- }
2305
- async firstWhere(column, operator = null, value = null, boolean = "and") {
2306
- return await this.where(column, operator, value, boolean).first();
2307
- }
2308
- async first(columns = ["*"]) {
2309
- const results = await this.take(1).get(columns);
2310
- return results.count() > 0 ? results.first() : null;
2311
- }
2312
- async firstOrFail(...columns) {
2313
- const model = await this.first(...columns);
2314
- if (model) return model;
2315
- throw new ModelNotFoundError().setModel(this.related.constructor);
2316
- }
2317
- async firstOr(columns = ["*"], callback = null) {
2318
- if (typeof columns === "function") {
2319
- callback = columns;
2320
- columns = ["*"];
2321
- }
2322
- const model = await this.first(columns);
2323
- if (model) return model;
2324
- return callback === null || callback === void 0 ? void 0 : callback();
2325
- }
2326
- async find(id, columns = ["*"]) {
2327
- if ((0, radashi.isArray)(id)) return await this.findMany(id, columns);
2328
- return await this.where(this.getRelated().getQualifiedKeyName(), "=", id).first(columns);
2329
- }
2330
- async findMany(ids, columns = ["*"]) {
2331
- if (ids.length === 0) return this.getRelated().newCollection();
2332
- return await this.whereIn(this.getRelated().getQualifiedKeyName(), ids).get(columns);
2333
- }
2334
- async findOrFail(id, columns = ["*"]) {
2335
- const result = await this.find(id, columns);
2336
- if (Array.isArray(id)) {
2337
- if (result.count() === id.length) return result;
2338
- } else if (result) return result;
2339
- throw new ModelNotFoundError().setModel(this.related.constructor, id);
2340
- }
2341
- async getResults() {
2342
- return this.farParent[this.localKey] ? await this.get() : this.related.newCollection();
2343
- }
2344
- async get(columns = ["*"]) {
2345
- const builder = this.prepareQueryBuilder(columns);
2346
- let models = await builder.getModels();
2347
- if (models.count() > 0) models = await builder.eagerLoadRelations(models);
2348
- return this.related.newCollection(models);
2349
- }
2350
- async paginate(perPage = null, columns = ["*"], pageName = "page", page = null) {
2351
- this.query.addSelect(this.shouldSelect(columns));
2352
- return await this.query.paginate(perPage ?? 15, columns, pageName, page);
2353
- }
2354
- shouldSelect(columns = ["*"]) {
2355
- if ((columns === null || columns === void 0 ? void 0 : columns.at(0)) == "*") columns = [this.related.getTable() + ".*"];
2356
- return [...columns, this.getQualifiedFirstKeyName() + " as laravel_through_key"];
2357
- }
2358
- async chunk(count, callback) {
2359
- return await this.prepareQueryBuilder().chunk(count, callback);
2360
- }
2361
- prepareQueryBuilder(columns = ["*"]) {
2362
- const builder = this.query.applyScopes();
2363
- return builder.addSelect(this.shouldSelect(builder.getQuery().columns ? [] : columns));
2364
- }
2365
- getRelationExistenceQuery(query, parentQuery, columns = ["*"]) {
2366
- if (parentQuery.getQuery().from === query.getQuery().from) return this.getRelationExistenceQueryForSelfRelation(query, parentQuery, columns);
2367
- if (parentQuery.getQuery().from === this.throughParent.getTable()) return this.getRelationExistenceQueryForThroughSelfRelation(query, parentQuery, columns);
2368
- this.performJoin(query);
2369
- return query.select(columns).where(this.getQualifiedLocalKeyName(), "=", this.getQualifiedFirstKeyName());
2370
- }
2371
- getRelationExistenceQueryForSelfRelation(query, parentQuery, columns = ["*"]) {
2372
- const hash = this.getRelationCountHash();
2373
- query.from(query.getModel().getTable() + " as " + hash);
2374
- query.join(this.throughParent.getTable(), this.getQualifiedParentKeyName(), "=", hash + "." + this.secondKey);
2375
- if (this.throughParentSoftDeletes()) query.whereNull(this.throughParent.getQualifiedDeletedAtColumn());
2376
- query.getModel().setTable(hash);
2377
- return query.select(columns).whereColumn(parentQuery.getQuery().from + "." + this.localKey, "=", this.getQualifiedFirstKeyName());
2378
- }
2379
- getRelationExistenceQueryForThroughSelfRelation(query, parentQuery, columns = ["*"]) {
2380
- const hash = this.getRelationCountHash();
2381
- const table = this.throughParent.getTable() + " as " + hash;
2382
- query.join(table, hash + "." + this.secondLocalKey, "=", this.getQualifiedFarKeyName());
2383
- if (this.throughParentSoftDeletes()) query.whereNull(hash + "." + this.throughParent.getDeletedAtColumn());
2384
- return query.select(columns).where(parentQuery.getQuery().from + "." + this.localKey, "=", hash + "." + this.firstKey);
2385
- }
2386
- getQualifiedFarKeyName() {
2387
- return this.getQualifiedForeignKeyName();
2388
- }
2389
- getFirstKeyName() {
2390
- return this.firstKey;
2391
- }
2392
- getQualifiedFirstKeyName() {
2393
- return this.throughParent.qualifyColumn(this.firstKey);
2394
- }
2395
- getForeignKeyName() {
2396
- return this.secondKey;
2397
- }
2398
- getQualifiedForeignKeyName() {
2399
- return this.related.qualifyColumn(this.secondKey);
2400
- }
2401
- getLocalKeyName() {
2402
- return this.localKey;
2403
- }
2404
- getQualifiedLocalKeyName() {
2405
- return this.farParent.qualifyColumn(this.localKey);
2406
- }
2407
- getSecondLocalKeyName() {
2408
- return this.secondLocalKey;
2409
- }
2410
- };
2411
- var has_many_through_default = HasManyThrough;
2412
-
2413
- //#endregion
2414
- //#region src/relations/has-one-through.ts
2415
- var HasOneThrough = class extends compose(has_many_through_default, supports_default_models_default) {
2416
- async getResults() {
2417
- return await this.first() || this.getDefaultFor(this.farParent);
2418
- }
2419
- initRelation(models, relation) {
2420
- for (const model of models) model.setRelation(relation, this.getDefaultFor(model));
2421
- return models;
2422
- }
2423
- match(models, results, relation) {
2424
- const dictionary = this.buildDictionary(results);
2425
- for (const model of models) {
2426
- const key = this.getDictionaryKey(model.getAttribute(this.localKey));
2427
- if (dictionary[key] !== void 0) {
2428
- const value = dictionary[key];
2429
- model.setRelation(relation, value[0]);
2430
- }
2431
- }
2432
- return models;
2433
- }
2434
- newRelatedInstanceFor(_parent) {
2435
- return this.related.newInstance();
2436
- }
2437
- };
2438
- var has_one_through_default = HasOneThrough;
2439
-
2440
- //#endregion
2441
- //#region src/concerns/has-relations.ts
2442
- const HasRelations = (Model$1) => {
2443
- return class extends Model$1 {
2444
- relations = {};
2445
- getRelation(relation) {
2446
- return this.relations[relation];
2447
- }
2448
- setRelation(relation, value) {
2449
- this.relations[relation] = value;
2450
- return this;
2451
- }
2452
- unsetRelation(relation) {
2453
- this.relations = (0, radashi.omit)(this.relations, [relation]);
2454
- return this;
2455
- }
2456
- relationLoaded(relation) {
2457
- return this.relations[relation] !== void 0;
2458
- }
2459
- related(relation) {
2460
- if (typeof this[getRelationMethod(relation)] !== "function") {
2461
- const message = `Model [${this.constructor.name}]'s relation [${relation}] doesn't exist.`;
2462
- throw new RelationNotFoundError(message);
2463
- }
2464
- return this[getRelationMethod(relation)]();
2465
- }
2466
- async getRelated(relation) {
2467
- return await this.related(relation).getResults();
2468
- }
2469
- relationsToData() {
2470
- const data = {};
2471
- for (const key in this.relations) {
2472
- if (this.hidden.includes(key)) continue;
2473
- if (this.visible.length > 0 && this.visible.includes(key) === false) continue;
2474
- data[key] = this.relations[key] instanceof Array ? this.relations[key].map((item) => item.toData()) : this.relations[key] === null ? null : this.relations[key].toData();
2475
- }
2476
- return data;
2477
- }
2478
- guessBelongsToRelation() {
2479
- const functionName = (/* @__PURE__ */ new Error()).stack.split("\n")[2].split(" ")[5];
2480
- return getRelationName(functionName);
2481
- }
2482
- joiningTable(related, instance = null) {
2483
- return [instance ? instance.joiningTableSegment() : snakeCase(related.name), this.joiningTableSegment()].sort().join("_").toLocaleLowerCase();
2484
- }
2485
- joiningTableSegment() {
2486
- return snakeCase(this.constructor.name);
2487
- }
2488
- hasOne(related, foreignKey = null, localKey = null) {
2489
- const query = related.query();
2490
- const instance = new related();
2491
- foreignKey = foreignKey || this.getForeignKey();
2492
- localKey = localKey || this.getKeyName();
2493
- return new has_one_default(query, this, instance.getTable() + "." + foreignKey, localKey);
2494
- }
2495
- hasMany(related, foreignKey = null, localKey = null) {
2496
- const query = related.query();
2497
- const instance = new related();
2498
- foreignKey = foreignKey || this.getForeignKey();
2499
- localKey = localKey || this.getKeyName();
2500
- return new has_many_default(query, this, instance.getTable() + "." + foreignKey, localKey);
2501
- }
2502
- belongsTo(related, foreignKey = null, ownerKey = null, relation = null) {
2503
- const query = related.query();
2504
- const instance = new related();
2505
- foreignKey = foreignKey || instance.getForeignKey();
2506
- ownerKey = ownerKey || instance.getKeyName();
2507
- relation = relation || this.guessBelongsToRelation();
2508
- return new belongs_to_default(query, this, foreignKey, ownerKey, relation);
2509
- }
2510
- belongsToMany(related, table = null, foreignPivotKey = null, relatedPivotKey = null, parentKey = null, relatedKey = null) {
2511
- const query = related.query();
2512
- const instance = new related();
2513
- table = table || this.joiningTable(related, instance);
2514
- foreignPivotKey = foreignPivotKey || this.getForeignKey();
2515
- relatedPivotKey = relatedPivotKey || instance.getForeignKey();
2516
- parentKey = parentKey || this.getKeyName();
2517
- relatedKey = relatedKey || instance.getKeyName();
2518
- return new belongs_to_many_default(query, this, table, foreignPivotKey, relatedPivotKey, parentKey, relatedKey);
2519
- }
2520
- hasOneThrough(related, through, firstKey = null, secondKey = null, localKey = null, secondLocalKey = null) {
2521
- through = new through();
2522
- const query = related.query();
2523
- firstKey = firstKey || this.getForeignKey();
2524
- secondKey = secondKey || through.getForeignKey();
2525
- return new has_one_through_default(query, this, through, firstKey, secondKey, localKey || this.getKeyName(), secondLocalKey || through.getKeyName());
2526
- }
2527
- hasManyThrough(related, through, firstKey = null, secondKey = null, localKey = null, secondLocalKey = null) {
2528
- through = new through();
2529
- const query = related.query();
2530
- firstKey = firstKey || this.getForeignKey();
2531
- secondKey = secondKey || through.getForeignKey();
2532
- return new has_many_through_default(query, this, through, firstKey, secondKey, localKey || this.getKeyName(), secondLocalKey || through.getKeyName());
2533
- }
2534
- };
2535
- };
2536
- var has_relations_default = HasRelations;
2537
-
2538
- //#endregion
2539
- //#region src/concerns/has-timestamps.ts
2540
- const HasTimestamps = (Model$1) => {
2541
- return class extends Model$1 {
2542
- static CREATED_AT = "created_at";
2543
- static UPDATED_AT = "updated_at";
2544
- static DELETED_AT = "deleted_at";
2545
- timestamps = true;
2546
- dateFormat = "YYYY-MM-DD HH:mm:ss";
2547
- usesTimestamps() {
2548
- return this.timestamps;
2549
- }
2550
- updateTimestamps() {
2551
- const time = this.freshTimestampString();
2552
- const updatedAtColumn = this.getUpdatedAtColumn();
2553
- if (updatedAtColumn && !this.isDirty(updatedAtColumn)) this.setUpdatedAt(time);
2554
- const createdAtColumn = this.getCreatedAtColumn();
2555
- if (!this.exists && createdAtColumn && !this.isDirty(createdAtColumn)) this.setCreatedAt(time);
2556
- return this;
2557
- }
2558
- getCreatedAtColumn() {
2559
- return this.constructor.CREATED_AT;
2560
- }
2561
- getUpdatedAtColumn() {
2562
- return this.constructor.UPDATED_AT;
2563
- }
2564
- setCreatedAt(value) {
2565
- this.attributes[this.getCreatedAtColumn()] = value;
2566
- return this;
2567
- }
2568
- setUpdatedAt(value) {
2569
- this.attributes[this.getUpdatedAtColumn()] = value;
2570
- return this;
2571
- }
2572
- freshTimestamp() {
2573
- const time = /* @__PURE__ */ new Date();
2574
- time.setMilliseconds(0);
2575
- return time;
2576
- }
2577
- freshTimestampString() {
2578
- return this.fromDateTime(this.freshTimestamp());
2579
- }
2580
- };
2581
- };
2582
- var has_timestamps_default = HasTimestamps;
2583
-
2584
- //#endregion
2585
- //#region src/concerns/hides-attributes.ts
2586
- const HidesAttributes = (Model$1) => {
2587
- return class extends Model$1 {
2588
- hidden = [];
2589
- visible = [];
2590
- makeVisible(...keys) {
2591
- const visible = flattenDeep(keys);
2592
- if (this.visible.length > 0) this.visible = [...this.visible, ...visible];
2593
- this.hidden = (0, radashi.diff)(this.hidden, visible);
2594
- return this;
2595
- }
2596
- makeHidden(key, ...keys) {
2597
- const hidden = flattenDeep([...key, ...keys]);
2598
- if (this.hidden.length > 0) this.hidden = [...this.hidden, ...hidden];
2599
- return this;
2600
- }
2601
- getHidden() {
2602
- return this.hidden;
2603
- }
2604
- getVisible() {
2605
- return this.visible;
2606
- }
2607
- setHidden(hidden) {
2608
- this.hidden = hidden;
2609
- return this;
2610
- }
2611
- setVisible(visible) {
2612
- this.visible = visible;
2613
- return this;
2614
- }
2615
- };
2616
- };
2617
- var hides_attributes_default = HidesAttributes;
2618
-
2619
- //#endregion
2620
- //#region src/concerns/unique-ids.ts
2621
- const UniqueIds = (Model$1) => {
2622
- return class extends Model$1 {
2623
- useUniqueIds = false;
2624
- usesUniqueIds() {
2625
- return this.useUniqueIds;
2626
- }
2627
- uniqueIds() {
2628
- return [];
2629
- }
2630
- setUniqueIds() {
2631
- const uniqueIds = this.uniqueIds();
2632
- for (const column of uniqueIds) if (this[column] === null || this[column] === void 0) this[column] = this.newUniqueId();
2633
- }
2634
- };
2635
- };
2636
- var unique_ids_default = UniqueIds;
2637
-
2638
- //#endregion
2639
- //#region src/paginator.ts
2640
- var Paginator = class {
2641
- static formatter;
2642
- _items;
2643
- _total;
2644
- _perPage;
2645
- _lastPage;
2646
- _currentPage;
2647
- hasMore = false;
2648
- options = {};
2649
- static setFormatter(formatter) {
2650
- if (typeof formatter !== "function" && formatter !== null && formatter !== void 0) throw new Error("Paginator formatter must be a function or null");
2651
- if (!formatter) return;
2652
- this.formatter = formatter;
2653
- }
2654
- constructor(items, total, perPage, currentPage = 1, options = {}) {
2655
- this.options = options;
2656
- for (const key in options) this[key] = options[key];
2657
- this._items = new collection_default([]);
2658
- this._total = total;
2659
- this._perPage = parseInt(String(perPage));
2660
- this._lastPage = Math.max(Math.ceil(total / perPage), 1);
2661
- this._currentPage = currentPage;
2662
- this.setItems(items);
2663
- }
2664
- setItems(items) {
2665
- this._items = items instanceof collection_default ? items : new collection_default(items);
2666
- this.hasMore = this._items.count() > this._perPage;
2667
- this._items = this._items.slice(0, this._perPage);
2668
- }
2669
- firstItem() {
2670
- return this.count() > 0 ? (this._currentPage - 1) * this._perPage + 1 : null;
2671
- }
2672
- lastItem() {
2673
- return this.count() > 0 ? (this.firstItem() ?? 0) + this.count() - 1 : null;
2674
- }
2675
- hasMorePages() {
2676
- return this._currentPage < this._lastPage;
2677
- }
2678
- get(index) {
2679
- return this._items.get(index);
2680
- }
2681
- count() {
2682
- return this._items.count();
2683
- }
2684
- items() {
2685
- return this._items;
2686
- }
2687
- map(callback) {
2688
- return this._items.map(callback);
2689
- }
2690
- currentPage() {
2691
- return this._currentPage;
2692
- }
2693
- onFirstPage() {
2694
- return this._currentPage === 1;
2695
- }
2696
- perPage() {
2697
- return this._perPage;
2698
- }
2699
- lastPage() {
2700
- return this._lastPage;
2701
- }
2702
- total() {
2703
- return this._total;
2704
- }
2705
- toData() {
2706
- if (this.constructor.formatter && typeof this.constructor.formatter === "function") return this.constructor.formatter(this);
2707
- return {
2708
- current_page: this._currentPage,
2709
- data: this._items.toData(),
2710
- per_page: this._perPage,
2711
- total: this._total,
2712
- last_page: this._lastPage,
2713
- count: this.count()
2714
- };
2715
- }
2716
- toJSON() {
2717
- return this.toData();
2718
- }
2719
- toJson(...args) {
2720
- return JSON.stringify(this.toData(), ...args);
2721
- }
2722
- };
2723
- var paginator_default = Paginator;
2724
-
2725
- //#endregion
2726
- //#region src/query-builder.ts
2727
- const Inference$1 = class {};
2728
- var QueryBuilder = class QueryBuilder extends Inference$1 {
2729
- model;
2730
- schema;
2731
- connector;
2732
- constructor(config, connector) {
2733
- super();
2734
- this.connector = connector(config);
2735
- return this.asProxy();
2736
- }
2737
- asProxy() {
2738
- return new Proxy(this, {
2739
- get: function(target, prop) {
2740
- var _target$connector$cli;
2741
- if (typeof target[prop] !== "undefined") return target[prop];
2742
- if (["destroy", "schema"].includes(prop)) return target.connector.schema;
2743
- const skipReturning = !!((_target$connector$cli = target.connector.client.config) === null || _target$connector$cli === void 0 || (_target$connector$cli = _target$connector$cli.client) === null || _target$connector$cli === void 0 ? void 0 : _target$connector$cli.includes("mysql")) && prop === "returning";
2744
- if ([
2745
- "select",
2746
- "from",
2747
- "where",
2748
- "orWhere",
2749
- "whereColumn",
2750
- "whereRaw",
2751
- "whereNot",
2752
- "orWhereNot",
2753
- "whereIn",
2754
- "orWhereIn",
2755
- "whereNotIn",
2756
- "orWhereNotIn",
2757
- "whereNull",
2758
- "orWhereNull",
2759
- "whereNotNull",
2760
- "orWhereNotNull",
2761
- "whereExists",
2762
- "orWhereExists",
2763
- "whereNotExists",
2764
- "orWhereNotExists",
2765
- "whereBetween",
2766
- "orWhereBetween",
2767
- "whereNotBetween",
2768
- "orWhereNotBetween",
2769
- "whereLike",
2770
- "orWhereLike",
2771
- "whereILike",
2772
- "orWhereILike",
2773
- "whereJsonObject",
2774
- "whereJsonPath",
2775
- "whereJsonSupersetOf",
2776
- "whereJsonSubsetOf",
2777
- "join",
2778
- "joinRaw",
2779
- "leftJoin",
2780
- "leftOuterJoin",
2781
- "rightJoin",
2782
- "rightOuterJoin",
2783
- "crossJoin",
2784
- "transacting",
2785
- "groupBy",
2786
- "groupByRaw",
2787
- "returning",
2788
- "having",
2789
- "havingRaw",
2790
- "havingBetween",
2791
- "limit",
2792
- "offset",
2793
- "orderBy",
2794
- "orderByRaw",
2795
- "union",
2796
- "insert",
2797
- "forUpdate",
2798
- "forShare",
2799
- "distinct",
2800
- "clearOrder",
2801
- "clear",
2802
- "clearSelect",
2803
- "clearWhere",
2804
- "clearHaving",
2805
- "clearGroup"
2806
- ].includes(prop) && !skipReturning) return (...args) => {
2807
- target.connector[prop](...args);
2808
- return target.asProxy();
2809
- };
2810
- return target.connector[prop];
2811
- },
2812
- set: function(target, prop, value) {
2813
- if (typeof target[prop] !== "undefined") {
2814
- target[prop] = value;
2815
- return target;
2816
- }
2817
- target.connector[prop] = value;
2818
- return target;
2819
- }
2820
- });
2821
- }
2822
- async beginTransaction() {
2823
- return await this.connector.transaction();
2824
- }
2825
- table(table) {
2826
- const c = this.connector.table(table);
2827
- return new QueryBuilder(null, () => c);
2828
- }
2829
- transaction(callback) {
2830
- if (callback) return this.connector.transaction((trx) => {
2831
- return callback(new QueryBuilder(null, () => trx));
2832
- });
2833
- return callback;
2834
- }
2835
- async find(id, columns = ["*"]) {
2836
- return await this.connector.where("id", id).first(...columns);
2837
- }
2838
- async get(_columns = ["*"]) {
2839
- return await this.connector;
2840
- }
2841
- async exists() {
2842
- return await this.connector.first() !== null;
2843
- }
2844
- skip(...args) {
2845
- return this.offset(...args);
2846
- }
2847
- take(...args) {
2848
- return this.limit(...args);
2849
- }
2850
- async chunk(count, callback) {
2851
- if (this.connector._statements.filter((item) => item.grouping === "order").length === 0) throw new Error("You must specify an orderBy clause when using this function.");
2852
- let page = 1;
2853
- let countResults;
2854
- do {
2855
- const results = await this.clone().forPage(page, count).get();
2856
- countResults = results.length;
2857
- if (countResults == 0) break;
2858
- if (await callback(results, page) === false) return false;
2859
- page++;
2860
- } while (countResults === count);
2861
- return true;
2862
- }
2863
- async paginate(page = 1, perPage = 15, _pageName, _page) {
2864
- const total = await this.clone().clearOrder().count("*");
2865
- let results;
2866
- if (total > 0) {
2867
- const skip = (page - 1) * perPage;
2868
- this.take(perPage).skip(skip);
2869
- results = await this.get();
2870
- } else results = [];
2871
- return new paginator_default(results, parseInt(total), perPage, page);
2872
- }
2873
- forPage(page = 1, perPage = 15) {
2874
- return this.offset((page - 1) * perPage).limit(perPage);
2875
- }
2876
- toSQL(...args) {
2877
- return this.connector.toSQL(...args);
2878
- }
2879
- async count(column) {
2880
- const [{ aggregate }] = await this.connector.count(column, { as: "aggregate" });
2881
- return Number(aggregate);
2882
- }
2883
- async min(column) {
2884
- const [{ aggregate }] = await this.connector.min(column, { as: "aggregate" });
2885
- return Number(aggregate);
2886
- }
2887
- async max(column) {
2888
- const [{ aggregate }] = await this.connector.max(column, { as: "aggregate" });
2889
- return Number(aggregate);
2890
- }
2891
- async sum(column) {
2892
- const [{ aggregate }] = await this.connector.sum(column, { as: "aggregate" });
2893
- return Number(aggregate);
2894
- }
2895
- async avg(column) {
2896
- const [{ aggregate }] = await this.connector.avg(column, { as: "aggregate" });
2897
- return Number(aggregate);
2898
- }
2899
- clone() {
2900
- const c = this.connector.clone();
2901
- return new QueryBuilder(null, () => c);
2902
- }
2903
- async delete() {
2904
- return await this.connector.delete();
2905
- }
2906
- async insert(...args) {
2907
- return await this.connector.insert(...args);
2908
- }
2909
- async update(...args) {
2910
- return await this.connector.update(...args);
2911
- }
2912
- destroy(...args) {
2913
- return this.connector.destroy(...args);
2914
- }
2915
- get _statements() {
2916
- return this.connector._statements;
2917
- }
2918
- get _single() {
2919
- return this.connector._single;
2920
- }
2921
- get from() {
2922
- return this.connector.from;
2923
- }
2924
- };
2925
- var query_builder_default = QueryBuilder;
2926
-
2927
- //#endregion
2928
- //#region src/cli/utils.ts
2929
- const join = path.default.join;
2930
- var Utils = class {
2931
- /**
2932
- * Wraps text with chalk
2933
- *
2934
- * @param txt
2935
- * @param color
2936
- * @returns
2937
- */
2938
- static textFormat(txt, color) {
2939
- return String(txt).split(":").map((e, i, a) => i == 0 && a.length > 1 ? color(" " + e + ": ") : e).join("");
2940
- }
2941
- static findModulePkg(moduleId, cwd) {
2942
- const parts = moduleId.replace(/\\/g, "/").split("/");
2943
- let packageName = "";
2944
- if (parts.length > 0 && parts[0][0] === "@") packageName += parts.shift() + "/";
2945
- packageName += parts.shift();
2946
- const packageJson = path.default.join(packageName, "package.json");
2947
- const resolved = resolve_from.default.silent(cwd ?? process.cwd(), packageJson);
2948
- if (!resolved) return;
2949
- return path.default.join(path.default.dirname(resolved), parts.join("/"));
2950
- }
2951
- static async getMigrationPaths(cwd, migrator, defaultPath, path$6) {
2952
- if (path$6) return [join(cwd, path$6)];
2953
- return [...migrator.getPaths(), join(cwd, defaultPath)];
2954
- }
2955
- /**
2956
- * Check if file exists
2957
- *
2958
- * @param path
2959
- * @returns
2960
- */
2961
- static async fileExists(path$6) {
2962
- try {
2963
- await (0, fs_promises.access)(path$6);
2964
- return true;
2965
- } catch {
2966
- return false;
2967
- }
2968
- }
2969
- static findUpConfig(cwd, name, extensions) {
2970
- return (0, escalade_sync.default)(cwd, (_dir, names) => {
2971
- for (const ext of extensions) {
2972
- const filename = `${name}.${ext}`;
2973
- if (names.includes(filename)) return filename;
2974
- }
2975
- return false;
2976
- });
2977
- }
2978
- };
2979
-
2980
- //#endregion
2981
- //#region src/arquebus.ts
2982
- var arquebus = class arquebus {
2983
- static connectorFactory = null;
2984
- static instance = null;
2985
- manager;
2986
- connections;
2987
- models;
2988
- constructor() {
2989
- this.manager = {};
2990
- this.connections = {};
2991
- this.models = {};
2992
- }
2993
- getConstructor() {
2994
- return this.constructor;
2995
- }
2996
- static getInstance() {
2997
- if (this.instance === null) this.instance = new arquebus();
2998
- return this.instance;
2999
- }
3000
- /**
3001
- * Initialize a new database connection
3002
- *
3003
- * @returns
3004
- */
3005
- static fire(connection = null) {
3006
- return this.getInstance().getConnection(connection);
3007
- }
3008
- /**
3009
- * Initialize a new database connection
3010
- *
3011
- * This is an alias of `arquebus.fire()` and will be removed in the future
3012
- *
3013
- * @deprecated since version 0.3.0
3014
- * @alias fire
3015
- *
3016
- * @returns
3017
- */
3018
- static connection(connection = null) {
3019
- return this.fire(connection);
3020
- }
3021
- static setConnectorFactory(connectorFactory) {
3022
- this.connectorFactory = connectorFactory;
3023
- }
3024
- static getConnectorFactory() {
3025
- return this.connectorFactory ?? knex.default;
3026
- }
3027
- static addConnection(config, name = "default") {
3028
- return this.getInstance().addConnection(config, name);
3029
- }
3030
- static beginTransaction(connection = null) {
3031
- return this.getInstance().beginTransaction(connection);
3032
- }
3033
- static transaction(callback, connection = null) {
3034
- return this.getInstance().transaction(callback, connection);
3035
- }
3036
- static table(name, connection = null) {
3037
- return this.getInstance().table(name, connection);
3038
- }
3039
- static schema(connection = null) {
3040
- return this.getInstance().schema(connection);
3041
- }
3042
- static async destroyAll() {
3043
- await this.getInstance().destroyAll();
3044
- }
3045
- static createModel(name, options) {
3046
- return this.getInstance().createModel(name, options);
3047
- }
3048
- connection(connection = null) {
3049
- return this.getConnection(connection);
3050
- }
3051
- getConnection(name = null) {
3052
- name = name || "default";
3053
- const resolvedName = this.connections[name] ? name : "default";
3054
- if (this.manager[resolvedName] === void 0) {
3055
- const queryBuilder = new query_builder_default(this.connections[resolvedName], arquebus.getConnectorFactory());
3056
- this.manager[resolvedName] = queryBuilder;
3057
- }
3058
- return this.manager[resolvedName];
3059
- }
3060
- addConnection(config, name = "default") {
3061
- this.connections[name] = {
3062
- ...config,
3063
- connection: {
3064
- ...config.connection,
3065
- dateStrings: true,
3066
- typeCast: function(field, next) {
3067
- if (field.type === "JSON") return field.string("utf8");
3068
- return next();
3069
- }
3070
- }
3071
- };
3072
- }
3073
- /**
3074
- * Autoload the config file
3075
- *
3076
- * @param addConnection
3077
- * @default true
3078
- * If set to `false` we will no attempt add the connection, we
3079
- * will just go ahead and return the config
3080
- *
3081
- * @returns
3082
- */
3083
- static async autoLoad(addConnection = true) {
3084
- let config;
3085
- const jsPath = path.default.resolve("arquebus.config.js");
3086
- const tsPath = path.default.resolve("arquebus.config.ts");
3087
- const instance = this.getInstance();
3088
- if ((0, fs.existsSync)(jsPath)) {
3089
- config = (await import(jsPath)).default;
3090
- if (addConnection) instance.addConnection(config, config.client);
3091
- return config;
3092
- }
3093
- if ((0, fs.existsSync)(tsPath)) if (process.env.NODE_ENV !== "production") {
3094
- config = (await import(tsPath)).default;
3095
- if (addConnection) instance.addConnection(config, config.client);
3096
- return config;
3097
- } else throw new Error("arquebus.config.ts found in production without build step");
3098
- const candidateDirs = [
3099
- process.cwd(),
3100
- path.default.join(process.cwd(), "test", "cli"),
3101
- path.default.join(process.cwd(), "test")
3102
- ];
3103
- for (const dir of candidateDirs) {
3104
- const found = Utils.findUpConfig(dir, "arquebus.config", [
3105
- "js",
3106
- "ts",
3107
- "cjs"
3108
- ]);
3109
- if (found) if (!found.endsWith(".ts") || process.env.NODE_ENV !== "production") {
3110
- config = (await import(found)).default;
3111
- if (addConnection) instance.addConnection(config, config.client);
3112
- return config;
3113
- } else throw new Error("arquebus.config.ts found in production without build step");
3114
- }
3115
- return {};
3116
- }
3117
- beginTransaction(connection = null) {
3118
- return this.connection(connection).beginTransaction();
3119
- }
3120
- transaction(callback, connection = null) {
3121
- return this.connection(connection).transaction(callback);
3122
- }
3123
- table(name, connection = null) {
3124
- return this.connection(connection).table(name);
3125
- }
3126
- schema(connection = null) {
3127
- return this.connection(connection).schema;
3128
- }
3129
- async destroyAll() {
3130
- await Promise.all(Object.values(this.manager).map((connection) => {
3131
- return connection === null || connection === void 0 ? void 0 : connection.destroy();
3132
- }));
3133
- }
3134
- createModel(name, options = {}) {
3135
- let BaseModel$1 = Model;
3136
- if ("plugins" in options) BaseModel$1 = compose(BaseModel$1, ...options.plugins ?? []);
3137
- this.models = {
3138
- ...this.models,
3139
- [name]: class extends BaseModel$1 {
3140
- table = (options === null || options === void 0 ? void 0 : options.table) ?? null;
3141
- connection = (options === null || options === void 0 ? void 0 : options.connection) ?? null;
3142
- timestamps = (options === null || options === void 0 ? void 0 : options.timestamps) ?? true;
3143
- primaryKey = (options === null || options === void 0 ? void 0 : options.primaryKey) ?? "id";
3144
- keyType = (options === null || options === void 0 ? void 0 : options.keyType) ?? "int";
3145
- incrementing = (options === null || options === void 0 ? void 0 : options.incrementing) ?? true;
3146
- with = (options === null || options === void 0 ? void 0 : options.with) ?? [];
3147
- casts = (options === null || options === void 0 ? void 0 : options.casts) ?? {};
3148
- static CREATED_AT = (options === null || options === void 0 ? void 0 : options.CREATED_AT) ?? "created_at";
3149
- static UPDATED_AT = (options === null || options === void 0 ? void 0 : options.UPDATED_AT) ?? "updated_at";
3150
- static DELETED_AT = (options === null || options === void 0 ? void 0 : options.DELETED_AT) ?? "deleted_at";
3151
- }
3152
- };
3153
- if ("attributes" in options) for (const attribute in options.attributes) {
3154
- if (options.attributes[attribute] instanceof attribute_default === false) throw new Error("Attribute must be an instance of \"Attribute\"");
3155
- this.models[name].prototype[getAttrMethod(attribute)] = () => {
3156
- var _options$attributes;
3157
- return (_options$attributes = options.attributes) === null || _options$attributes === void 0 ? void 0 : _options$attributes[attribute];
3158
- };
3159
- }
3160
- if ("relations" in options) for (const relation in options.relations) this.models[name].prototype[getRelationMethod(relation)] = function() {
3161
- var _options$relations;
3162
- return (_options$relations = options.relations) === null || _options$relations === void 0 ? void 0 : _options$relations[relation](this);
3163
- };
3164
- if ("scopes" in options) for (const scope in options.scopes) this.models[name].prototype[getScopeMethod(scope)] = options.scopes[scope];
3165
- this.models[name].setConnectionResolver(this);
3166
- return this.models[name];
3167
- }
3168
- };
3169
- var arquebus_default = arquebus;
3170
-
3171
- //#endregion
3172
- //#region src/model.ts
3173
- const ModelClass = class {};
3174
- const BaseModel = compose(ModelClass, has_attributes_default, hides_attributes_default, has_relations_default, has_timestamps_default, has_hooks_default, has_global_scopes_default, unique_ids_default);
3175
- var Model = class Model extends BaseModel {
3176
- builder = null;
3177
- table = null;
3178
- keyType = "int";
3179
- incrementing = true;
3180
- withCount = [];
3181
- primaryKey = "id";
3182
- perPage = 15;
3183
- static globalScopes = {};
3184
- static pluginInitializers = {};
3185
- static _booted = {};
3186
- static resolver;
3187
- connection = null;
3188
- eagerLoad = {};
3189
- exists = false;
3190
- with = [];
3191
- name;
3192
- trx = null;
3193
- constructor(attributes = {}) {
3194
- super();
3195
- this.bootIfNotBooted();
3196
- this.initializePlugins();
3197
- this.syncOriginal();
3198
- this.fill(attributes);
3199
- return this.asProxy();
3200
- }
3201
- static query(trx = null) {
3202
- return new this().newQuery(trx);
3203
- }
3204
- static on(connection = null) {
3205
- const instance = new this();
3206
- instance.setConnection(connection);
3207
- return instance.newQuery();
3208
- }
3209
- static init(attributes = {}) {
3210
- return new this(attributes);
3211
- }
3212
- static extend(plugin, options) {
3213
- plugin(this, options);
3214
- }
3215
- static make(attributes = {}) {
3216
- const instance = new this();
3217
- for (const attribute in attributes) if (typeof instance[getRelationMethod(attribute)] !== "function") instance.setAttribute(attribute, attributes[attribute]);
3218
- else {
3219
- const relation = instance[getRelationMethod(attribute)]();
3220
- const related = relation.getRelated().constructor;
3221
- if (relation instanceof has_one_default || relation instanceof belongs_to_default) instance.setRelation(attribute, related.make(attributes[attribute]));
3222
- else if ((relation instanceof has_many_default || relation instanceof belongs_to_many_default) && Array.isArray(attributes[attribute])) instance.setRelation(attribute, new collection_default(attributes[attribute].map((item) => related.make(item))));
3223
- }
3224
- return instance;
3225
- }
3226
- getConstructor() {
3227
- return this.constructor;
3228
- }
3229
- bootIfNotBooted() {
3230
- if (this.constructor._booted[this.constructor.name] === void 0) {
3231
- this.constructor._booted[this.constructor.name] = true;
3232
- this.constructor.booting();
3233
- this.initialize();
3234
- this.constructor.boot();
3235
- this.constructor.booted();
3236
- }
3237
- }
3238
- static booting() {}
3239
- static boot() {}
3240
- static booted() {}
3241
- static setConnectionResolver(resolver) {
3242
- this.resolver = resolver;
3243
- }
3244
- initialize() {}
3245
- initializePlugins() {
3246
- if (typeof this.constructor.pluginInitializers[this.constructor.name] === "undefined") return;
3247
- for (const method of this.constructor.pluginInitializers[this.constructor.name]) this[method]();
3248
- }
3249
- addPluginInitializer(method) {
3250
- if (!this.constructor.pluginInitializers[this.constructor.name]) this.constructor.pluginInitializers[this.constructor.name] = [];
3251
- this.constructor.pluginInitializers[this.constructor.name].push(method);
3252
- }
3253
- newInstance(attributes = {}, exists = false) {
3254
- const model = new this.constructor();
3255
- model.exists = exists;
3256
- model.setConnection(this.getConnectionName());
3257
- model.setTable(this.getTable());
3258
- model.fill(attributes);
3259
- return model;
3260
- }
3261
- newFromBuilder(attributes = {}, connection = null) {
3262
- const model = this.newInstance({}, true);
3263
- model.setRawAttributes(attributes, true);
3264
- model.setConnection(connection || this.getConnectionName());
3265
- return model;
3266
- }
3267
- asProxy() {
3268
- return new Proxy(this, {
3269
- get: function(target, prop) {
3270
- if (target[prop] !== void 0) return target[prop];
3271
- if (typeof prop === "string") return target.getAttribute(prop);
3272
- },
3273
- set: function(target, prop, value) {
3274
- if (target[prop] !== void 0 && typeof target !== "function") {
3275
- target[prop] = value;
3276
- return target;
3277
- }
3278
- if (typeof prop === "string") return target.setAttribute(prop, value);
3279
- return target;
3280
- }
3281
- });
3282
- }
3283
- getKey() {
3284
- return this.getAttribute(this.getKeyName());
3285
- }
3286
- getKeyName() {
3287
- return this.primaryKey;
3288
- }
3289
- getForeignKey() {
3290
- return snakeCase(this.constructor.name) + "_" + this.getKeyName();
3291
- }
3292
- getConnectionName() {
3293
- return this.connection;
3294
- }
3295
- getTable() {
3296
- return this.table || (0, pluralize.default)(snakeCase(this.constructor.name));
3297
- }
3298
- getConnection() {
3299
- if (this.constructor.resolver) return this.constructor.resolver.getConnection(this.connection);
3300
- return arquebus_default.fire(this.connection);
3301
- }
3302
- setConnection(connection) {
3303
- this.connection = connection;
3304
- return this;
3305
- }
3306
- getKeyType() {
3307
- return this.keyType;
3308
- }
3309
- newQuery(trx = null) {
3310
- return this.addGlobalScopes(this.newQueryWithoutScopes(trx));
3311
- }
3312
- newQueryWithoutScopes(trx = null) {
3313
- return this.newModelQuery(trx).with(this.with).withCount(this.withCount);
3314
- }
3315
- newModelQuery(trx = null) {
3316
- return new builder_default(trx || this.getConnection()).setModel(this);
3317
- }
3318
- addGlobalScopes(builder) {
3319
- const globalScopes = this.getGlobalScopes();
3320
- for (const identifier in globalScopes) {
3321
- const scope = globalScopes[identifier];
3322
- builder.withGlobalScope(identifier, scope);
3323
- }
3324
- return builder;
3325
- }
3326
- hasNamedScope(name) {
3327
- const scope = getScopeMethod(name);
3328
- return typeof this[scope] === "function";
3329
- }
3330
- callNamedScope(scope, parameters) {
3331
- const scopeMethod = getScopeMethod(scope);
3332
- return this[scopeMethod](...parameters);
3333
- }
3334
- setTable(table) {
3335
- this.table = table;
3336
- return this;
3337
- }
3338
- newCollection(models = []) {
3339
- return new collection_default(models);
3340
- }
3341
- async load(...relations) {
3342
- await this.constructor.query().with(...relations).eagerLoadRelations([this]);
3343
- return this;
3344
- }
3345
- async loadAggregate(relations, column, callback = null) {
3346
- console.log(relations);
3347
- await new collection_default([this]).loadAggregate(relations, column, callback);
3348
- return this;
3349
- }
3350
- async loadCount(...relations) {
3351
- relations = flattenDeep(relations);
3352
- return await this.loadAggregate(relations, "*", "count");
3353
- }
3354
- async loadMax(relations, column) {
3355
- return await this.loadAggregate(relations, column, "max");
3356
- }
3357
- async loadMin(relations, column) {
3358
- return await this.loadAggregate(relations, column, "min");
3359
- }
3360
- async loadSum(relations, column) {
3361
- return await this.loadAggregate(relations, column, "sum");
3362
- }
3363
- async increment(column, amount = 1, extra = {}, options = {}) {
3364
- return await this.incrementOrDecrement(column, amount, extra, "increment", options);
3365
- }
3366
- async decrement(column, amount = 1, extra = {}, options = {}) {
3367
- return await this.incrementOrDecrement(column, amount, extra, "decrement", options);
3368
- }
3369
- async incrementOrDecrement(column, amount, extra, method, options) {
3370
- const query = this.newModelQuery(options.client);
3371
- if (!this.exists) return await query[method](column, amount, extra);
3372
- this.attributes[column] = this[column] + (method === "increment" ? amount : amount * -1);
3373
- for (const key in extra) this.attributes[key] = extra[key];
3374
- await this.execHooks("updating", options);
3375
- return await tap(await query.where(this.getKeyName(), this.getKey())[method](column, amount, extra), async () => {
3376
- this.syncChanges();
3377
- await this.execHooks("updated", options);
3378
- this.syncOriginalAttribute(column);
3379
- });
3380
- }
3381
- toData() {
3382
- return (0, radashi.assign)(this.attributesToData(), this.relationsToData());
3383
- }
3384
- toJSON() {
3385
- return this.toData();
3386
- }
3387
- toJson(...args) {
3388
- return JSON.stringify(this.toData(), ...args);
3389
- }
3390
- toString() {
3391
- return this.toJson();
3392
- }
3393
- fill(attributes) {
3394
- for (const key in attributes) this.setAttribute(key, attributes[key]);
3395
- return this;
3396
- }
3397
- transacting(trx) {
3398
- this.trx = trx;
3399
- return this;
3400
- }
3401
- trashed() {
3402
- return this[this.getDeletedAtColumn()] !== null;
3403
- }
3404
- getIncrementing() {
3405
- return this.incrementing;
3406
- }
3407
- setIncrementing(value) {
3408
- this.incrementing = value;
3409
- return this;
3410
- }
3411
- async save(options = {}) {
3412
- const query = this.newModelQuery(options.client);
3413
- let saved;
3414
- await this.execHooks("saving", options);
3415
- if (this.exists) if (this.isDirty() === false) saved = true;
3416
- else {
3417
- await this.execHooks("updating", options);
3418
- if (this.usesTimestamps()) this.updateTimestamps();
3419
- const dirty = this.getDirty();
3420
- if (Object.keys(dirty).length > 0) {
3421
- await query.where(this.getKeyName(), this.getKey()).query.update(dirty);
3422
- this.syncChanges();
3423
- await this.execHooks("updated", options);
3424
- }
3425
- saved = true;
3426
- }
3427
- else {
3428
- if (this.usesUniqueIds()) this.setUniqueIds();
3429
- await this.execHooks("creating", options);
3430
- if (this.usesTimestamps()) this.updateTimestamps();
3431
- const attributes = this.getAttributes();
3432
- if (this.getIncrementing()) {
3433
- var _data$;
3434
- const keyName = this.getKeyName();
3435
- const data = await query.insert([attributes], [keyName]);
3436
- this.setAttribute(keyName, ((_data$ = data[0]) === null || _data$ === void 0 ? void 0 : _data$[keyName]) || data[0]);
3437
- } else if (Object.keys(attributes).length > 0) await query.insert(attributes);
3438
- this.exists = true;
3439
- await this.execHooks("created", options);
3440
- saved = true;
3441
- }
3442
- if (saved) {
3443
- await this.execHooks("saved", options);
3444
- this.syncOriginal();
3445
- }
3446
- return saved;
3447
- }
3448
- async update(attributes = {}, options = {}) {
3449
- if (!this.exists) return false;
3450
- for (const key in attributes) this[key] = attributes[key];
3451
- return await this.save(options);
3452
- }
3453
- async delete(options = {}) {
3454
- await this.execHooks("deleting", options);
3455
- await this.performDeleteOnModel(options);
3456
- await this.execHooks("deleted", options);
3457
- return true;
3458
- }
3459
- async performDeleteOnModel(options = {}) {
3460
- await this.setKeysForSaveQuery(this.newModelQuery(options.client)).delete();
3461
- this.exists = false;
3462
- }
3463
- setKeysForSaveQuery(query) {
3464
- query.where(this.getKeyName(), "=", this.getKey());
3465
- return query;
3466
- }
3467
- async forceDelete(options = {}) {
3468
- return await this.delete(options);
3469
- }
3470
- fresh() {
3471
- if (!this.exists) return;
3472
- return this.constructor.query().where(this.getKeyName(), this.getKey()).first();
3473
- }
3474
- async refresh() {
3475
- if (!this.exists) return Promise.resolve(void 0);
3476
- this.attributes = { ...(await this.constructor.query().where(this.getKeyName(), this.getKey()).first()).attributes };
3477
- await this.load((0, collect_js.default)(this.relations).reject((relation) => {
3478
- return relation instanceof Pivot;
3479
- }).keys().all());
3480
- this.syncOriginal();
3481
- return this;
3482
- }
3483
- newPivot(parent, attributes, table, exists, using = null) {
3484
- return using ? using.fromRawAttributes(parent, attributes, table, exists) : Pivot.fromAttributes(parent, attributes, table, exists);
3485
- }
3486
- qualifyColumn(column) {
3487
- if (column.includes(".")) return column;
3488
- return `${this.getTable()}.${column}`;
3489
- }
3490
- getQualifiedKeyName() {
3491
- return this.qualifyColumn(this.getKeyName());
3492
- }
3493
- async push(options = {}) {
3494
- if (!await this.save(options)) return false;
3495
- for (const relation in this.relations) {
3496
- let models = this.relations[relation];
3497
- models = models instanceof collection_default ? models.all() : [models];
3498
- for (const model of models) if (!await model.push(options)) return false;
3499
- }
3500
- return true;
3501
- }
3502
- is(model) {
3503
- return model && model instanceof Model && this.getKey() === model.getKey() && this.getTable() === model.getTable() && this.getConnectionName() === model.getConnectionName();
3504
- }
3505
- isNot(model) {
3506
- return !this.is(model);
3507
- }
3508
- };
3509
- var Pivot = class extends Model {
3510
- incrementing = false;
3511
- guarded = [];
3512
- pivotParent = null;
3513
- foreignKey = null;
3514
- relatedKey = null;
3515
- setPivotKeys(foreignKey, relatedKey) {
3516
- this.foreignKey = foreignKey;
3517
- this.relatedKey = relatedKey;
3518
- return this;
3519
- }
3520
- static fromRawAttributes(parent, attributes, table, exists = false) {
3521
- const instance = this.fromAttributes(parent, {}, table, exists);
3522
- instance.timestamps = instance.hasTimestampAttributes(attributes);
3523
- instance.attributes = attributes;
3524
- instance.exists = exists;
3525
- return instance;
3526
- }
3527
- static fromAttributes(parent, attributes, table, exists = false) {
3528
- const instance = new this();
3529
- instance.timestamps = instance.hasTimestampAttributes(attributes);
3530
- instance.setConnection(parent.connection).setTable(table).fill(attributes).syncOriginal();
3531
- instance.pivotParent = parent;
3532
- instance.exists = exists;
3533
- return instance;
3534
- }
3535
- hasTimestampAttributes(attributes = null) {
3536
- return (attributes || this.attributes)[this.constructor.CREATED_AT] !== void 0;
3537
- }
3538
- };
3539
- var model_default = Model;
3540
-
3541
- //#endregion
3542
- //#region src/collection.ts
3543
- var Collection = class Collection extends collect_js.Collection {
3544
- newConstructor(...args) {
3545
- return new (this.getConstructor())(...args);
3546
- }
3547
- getConstructor() {
3548
- return this.constructor;
3549
- }
3550
- async load(...relations) {
3551
- if (this.isNotEmpty()) {
3552
- const items = await this.first().constructor.query().with(...relations).eagerLoadRelations(this.items);
3553
- return this.newConstructor(items);
3554
- }
3555
- return this;
3556
- }
3557
- async loadAggregate(relations, column, action = null) {
3558
- if (this.isEmpty()) return this;
3559
- const models = (await this.first().newModelQuery().whereIn(this.first().getKeyName(), this.modelKeys()).select(this.first().getKeyName()).withAggregate(relations, column, action).get()).keyBy(this.first().getKeyName());
3560
- const attributes = (0, radashi.diff)(Object.keys(models.first().getAttributes()), [models.first().getKeyName()]);
3561
- this.each((model) => {
3562
- const extraAttributes = (0, radashi.pick)(models.get(model.getKey()).getAttributes(), attributes);
3563
- model.fill(extraAttributes).syncOriginalAttributes(...attributes);
3564
- });
3565
- return this;
3566
- }
3567
- loadCount(relations) {
3568
- return this.loadAggregate(relations, "*", "count");
3569
- }
3570
- loadMax(relation, column) {
3571
- return this.loadAggregate(relation, column, "max");
3572
- }
3573
- loadMin(relation, column) {
3574
- return this.loadAggregate(relation, column, "min");
3575
- }
3576
- loadSum(relation, column) {
3577
- return this.loadAggregate(relation, column, "sum");
3578
- }
3579
- loadAvg(relation, column) {
3580
- return this.loadAggregate(relation, column, "avg");
3581
- }
3582
- mapThen(callback) {
3583
- return Promise.all(this.map(callback));
3584
- }
3585
- modelKeys() {
3586
- return this.all().map((item) => item.getKey());
3587
- }
3588
- contains(key, operator, value) {
3589
- if (arguments.length > 1) return super.contains(key, value ?? operator);
3590
- if (key instanceof model_default) return super.contains((model) => {
3591
- return model.is(key);
3592
- });
3593
- return super.contains((model) => {
3594
- return model.getKey() == key;
3595
- });
3596
- }
3597
- diff(items) {
3598
- const diff = new this.constructor();
3599
- const dictionary = this.getDictionary(items);
3600
- this.items.map((item) => {
3601
- if (dictionary[item.getKey()] === void 0) diff.add(item);
3602
- });
3603
- return diff;
3604
- }
3605
- except(keys) {
3606
- const dictionary = (0, radashi.omit)(this.getDictionary(), keys);
3607
- return new this.constructor(Object.values(dictionary));
3608
- }
3609
- intersect(items) {
3610
- const intersect = new this.constructor();
3611
- if ((0, radashi.isEmpty)(items)) return intersect;
3612
- const dictionary = this.getDictionary(items);
3613
- for (const item of this.items) if (dictionary[item.getKey()] !== void 0) intersect.add(item);
3614
- return intersect;
3615
- }
3616
- unique(key, _strict = false) {
3617
- if (key) return super.unique(key);
3618
- return new this.constructor(Object.values(this.getDictionary()));
3619
- }
3620
- find(key, defaultValue = null) {
3621
- if (key instanceof model_default) key = key.getKey();
3622
- if ((0, radashi.isArray)(key)) {
3623
- if (this.isEmpty()) return new this.constructor();
3624
- return this.whereIn(this.first().getKeyName(), key);
3625
- }
3626
- (0, collect_js.collect)(this.items).first((model) => {
3627
- return model.getKey() == key;
3628
- });
3629
- return this.items.filter((model) => {
3630
- return model.getKey() == key;
3631
- })[0] || defaultValue;
3632
- }
3633
- async fresh(...args) {
3634
- if (this.isEmpty()) return new this.constructor();
3635
- const model = this.first();
3636
- const freshModels = (await model.newQuery().with(...args).whereIn(model.getKeyName(), this.modelKeys()).get()).getDictionary();
3637
- return this.filter((model$1) => {
3638
- return model$1.exists && freshModels[model$1.getKey()] !== void 0;
3639
- }).map((model$1) => {
3640
- return freshModels[model$1.getKey()];
3641
- });
3642
- }
3643
- makeVisible(attributes) {
3644
- return this.each((item) => {
3645
- item.makeVisible(attributes);
3646
- });
3647
- }
3648
- makeHidden(attributes) {
3649
- return this.each((item) => {
3650
- item.makeHidden(attributes);
3651
- });
3652
- }
3653
- append(attributes) {
3654
- return this.each((item) => {
3655
- item.append(attributes);
3656
- });
3657
- }
3658
- only(keys) {
3659
- if (keys === null) return new Collection(this.items);
3660
- const dictionary = (0, radashi.pick)(this.getDictionary(), keys);
3661
- return new this.constructor(Object.values(dictionary));
3662
- }
3663
- getDictionary(items) {
3664
- items = !items ? this.items : items;
3665
- const dictionary = {};
3666
- items.map((value) => {
3667
- dictionary[value.getKey()] = value;
3668
- });
3669
- return dictionary;
3670
- }
3671
- toQuery() {
3672
- const model = this.first();
3673
- if (!model) throw new Error("Unable to create query for empty collection.");
3674
- const modelName = model.constructor.name;
3675
- if (this.filter((model$1) => {
3676
- return !(model$1 instanceof modelName);
3677
- }).isNotEmpty()) throw new Error("Unable to create query for collection with mixed types.");
3678
- return model.newModelQuery().whereKey(this.modelKeys());
3679
- }
3680
- toData() {
3681
- return this.all().map((item) => typeof item.toData == "function" ? item.toData() : item);
3682
- }
3683
- toJSON() {
3684
- return this.toData();
3685
- }
3686
- toJson(...args) {
3687
- return JSON.stringify(this.toData(), ...args);
3688
- }
3689
- [Symbol.iterator] = () => {
3690
- const items = this.items;
3691
- const length = this.items.length;
3692
- let n = 0;
3693
- return { next() {
3694
- return n < length ? {
3695
- value: items[n++],
3696
- done: false
3697
- } : { done: true };
3698
- } };
3699
- };
3700
- };
3701
- var collection_default = Collection;
3702
-
3703
- //#endregion
3704
- //#region src/relations/concerns/interacts-with-pivot-table.ts
3705
- const InteractsWithPivotTable = (Relation$1) => {
3706
- return class extends Relation$1 {
3707
- newExistingPivot(attributes = []) {
3708
- return this.newPivot(attributes, true);
3709
- }
3710
- newPivot(attributes = [], exists = false) {
3711
- return this.related.newPivot(this.parent, attributes, this.getTable(), exists, this.using).setPivotKeys(this.foreignPivotKey, this.relatedPivotKey);
3712
- }
3713
- async attach(id, attributes = {}, _touch = true) {
3714
- if (this.using) await this.attachUsingCustomClass(id, attributes);
3715
- else await this.newPivotStatement().insert(this.formatAttachRecords(this.parseIds(id), attributes));
3716
- }
3717
- async detach(ids, _touch = true) {
3718
- let results;
3719
- if (this.using && ids !== null && this.pivotWheres.length == 0 && this.pivotWhereIns.length == 0 && this.pivotWhereNulls.length == 0) results = await this.detachUsingCustomClass(ids);
3720
- else {
3721
- const query = this.newPivotQuery();
3722
- if (ids !== null) {
3723
- ids = this.parseIds(ids);
3724
- if (ids.length == 0) return 0;
3725
- query.whereIn(this.getQualifiedRelatedPivotKeyName(), ids);
3726
- }
3727
- results = await query.delete();
3728
- }
3729
- return results;
3730
- }
3731
- async sync(ids, detaching = true) {
3732
- let changes = {
3733
- attached: [],
3734
- detached: [],
3735
- updated: []
3736
- };
3737
- let records;
3738
- const results = await this.getCurrentlyAttachedPivots();
3739
- const current = results.length === 0 ? [] : results.map((result) => result.toData()).pluck(this.relatedPivotKey).all().map((i) => String(i));
3740
- const detach = (0, radashi.diff)(current, Object.keys(records = this.formatRecordsList(this.parseIds(ids))));
3741
- if (detaching && detach.length > 0) {
3742
- await this.detach(detach);
3743
- changes.detached = this.castKeys(detach);
3744
- }
3745
- changes = (0, radashi.assign)(changes, await this.attachNew(records, current, false));
3746
- return changes;
3747
- }
3748
- syncWithoutDetaching(ids) {
3749
- return this.sync(ids, false);
3750
- }
3751
- syncWithPivotValues(ids, values, detaching = true) {
3752
- return this.sync((0, collect_js.collect)(this.parseIds(ids)).mapWithKeys((id) => {
3753
- return [id, values];
3754
- }), detaching);
3755
- }
3756
- withPivot(columns) {
3757
- this.pivotColumns = this.pivotColumns.concat((0, radashi.isArray)(columns) ? columns : Array.prototype.slice.call(columns));
3758
- return this;
3759
- }
3760
- async attachNew(records, current, touch = true) {
3761
- const changes = {
3762
- attached: [],
3763
- updated: []
3764
- };
3765
- for (const id in records) {
3766
- const attributes = records[id];
3767
- if (!current.includes(id)) {
3768
- await this.attach(id, attributes, touch);
3769
- changes.attached.push(this.castKey(id));
3770
- } else if (Object.keys(attributes).length > 0 && await this.updateExistingPivot(id, attributes, touch)) changes.updated.push(this.castKey(id));
3771
- }
3772
- return changes;
3773
- }
3774
- async updateExistingPivot(id, attributes, touch = true) {
3775
- if (this.using && this.pivotWheres.length > 0 && this.pivotWhereInspivotWheres.length > 0 && this.pivotWhereNullspivotWheres.length > 0) return await this.updateExistingPivotUsingCustomClass(id, attributes, touch);
3776
- if (this.hasPivotColumn(this.updatedAt())) attributes = this.addTimestampsToAttachment(attributes, true);
3777
- return this.newPivotStatementForId(this.parseId(id)).update(this.castAttributes(attributes));
3778
- }
3779
- addTimestampsToAttachment(record, exists = false) {
3780
- let fresh = this.parent.freshTimestamp();
3781
- if (this.using) fresh = new this.using().fromDateTime(fresh);
3782
- if (!exists && this.hasPivotColumn(this.createdAt())) record[this.createdAt()] = fresh;
3783
- if (this.hasPivotColumn(this.updatedAt())) record[this.updatedAt()] = fresh;
3784
- return record;
3785
- }
3786
- async updateExistingPivotUsingCustomClass(id, attributes, _touch) {
3787
- const pivot = await this.getCurrentlyAttachedPivots().where(this.foreignPivotKey, this.parent[this.parentKey]).where(this.relatedPivotKey, this.parseId(id)).first();
3788
- const updated = pivot ? pivot.fill(attributes).isDirty() : false;
3789
- if (updated) await pivot.save();
3790
- return parseInt(updated);
3791
- }
3792
- formatRecordsList(records) {
3793
- return (0, collect_js.collect)(records).mapWithKeys((attributes, id) => {
3794
- if (!(0, radashi.isArray)(attributes)) [id, attributes] = [attributes, {}];
3795
- return [id, attributes];
3796
- }).all();
3797
- }
3798
- async getCurrentlyAttachedPivots() {
3799
- return (await this.newPivotQuery().get()).map((record) => {
3800
- return (this.using || Pivot).fromRawAttributes(this.parent, record, this.getTable(), true).setPivotKeys(this.foreignPivotKey, this.relatedPivotKey);
3801
- });
3802
- }
3803
- castKeys(keys) {
3804
- return keys.map((v) => {
3805
- return this.castKey(v);
3806
- });
3807
- }
3808
- castKey(key) {
3809
- return this.getTypeSwapValue(this.related.getKeyType(), key);
3810
- }
3811
- getTypeSwapValue(type, value) {
3812
- switch (type.toLowerCase()) {
3813
- case "int":
3814
- case "integer": return parseInt(value);
3815
- case "real":
3816
- case "float":
3817
- case "double": return parseFloat(value);
3818
- case "string": return String(value);
3819
- default: return value;
3820
- }
3821
- }
3822
- newPivotQuery() {
3823
- const query = this.newPivotStatement();
3824
- this.pivotWheres.map((args) => {
3825
- query.where(...args);
3826
- });
3827
- this.pivotWhereIns.map((args) => {
3828
- query.whereIn(...args);
3829
- });
3830
- this.pivotWhereNulls.map((args) => {
3831
- query.whereNull(...args);
3832
- });
3833
- return query.where(this.getQualifiedForeignPivotKeyName(), this.parent[this.parentKey]);
3834
- }
3835
- async detachUsingCustomClass(ids) {
3836
- let results = 0;
3837
- for (const id in this.parseIds(ids)) results += await this.newPivot({
3838
- [this.foreignPivotKey]: this.parent[this.parentKey],
3839
- [this.relatedPivotKey]: id
3840
- }, true).delete();
3841
- return results;
3842
- }
3843
- newPivotStatement() {
3844
- const builder = this.parent.newQuery();
3845
- builder.setTable(this.table);
3846
- return builder;
3847
- }
3848
- async attachUsingCustomClass(id, attributes) {
3849
- const records = this.formatAttachRecords(this.parseIds(id), attributes);
3850
- await Promise.all(records.map(async (record) => {
3851
- await this.newPivot(record, false).save();
3852
- }));
3853
- }
3854
- formatAttachRecords(ids, attributes) {
3855
- const records = [];
3856
- const hasTimestamps = this.hasPivotColumn(this.createdAt()) || this.hasPivotColumn(this.updatedAt());
3857
- for (const key in ids) {
3858
- const value = ids[key];
3859
- records.push(this.formatAttachRecord(key, value, attributes, hasTimestamps));
3860
- }
3861
- return records;
3862
- }
3863
- formatAttachRecord(key, value, attributes, hasTimestamps) {
3864
- const [id, newAttributes] = this.extractAttachIdAndAttributes(key, value, attributes);
3865
- return (0, radashi.assign)(this.baseAttachRecord(id, hasTimestamps), newAttributes);
3866
- }
3867
- baseAttachRecord(id, timed) {
3868
- let record = {};
3869
- record[this.relatedPivotKey] = id;
3870
- record[this.foreignPivotKey] = this.parent[this.parentKey];
3871
- if (timed) record = this.addTimestampsToAttachment(record);
3872
- this.pivotValues.map((value) => {
3873
- record[value.column] = value.value;
3874
- });
3875
- return record;
3876
- }
3877
- extractAttachIdAndAttributes(key, value, newAttributes) {
3878
- return (0, radashi.isArray)(value) ? [key, {
3879
- ...value,
3880
- ...newAttributes
3881
- }] : [value, newAttributes];
3882
- }
3883
- hasPivotColumn(column) {
3884
- return this.pivotColumns.includes(column);
3885
- }
3886
- parseIds(value) {
3887
- if (value instanceof Model) return [value[this.relatedKey]];
3888
- if (value instanceof collection_default) return value.pluck(this.relatedKey).all();
3889
- return (0, radashi.isArray)(value) ? value : [value];
3890
- }
3891
- };
3892
- };
3893
- var interacts_with_pivot_table_default = InteractsWithPivotTable;
3894
-
3895
- //#endregion
3896
- //#region src/relations/belongs-to-many.ts
3897
- var BelongsToMany = class extends compose(relation_default, interacts_with_pivot_table_default) {
3898
- table;
3899
- foreignPivotKey;
3900
- relatedPivotKey;
3901
- parentKey;
3902
- relatedKey;
3903
- pivotColumns = [];
3904
- pivotValues = [];
3905
- pivotWheres = [];
3906
- pivotWhereIns = [];
3907
- pivotWhereNulls = [];
3908
- accessor = "pivot";
3909
- using;
3910
- pivotCreatedAt;
3911
- pivotUpdatedAt;
3912
- constructor(query, parent, table, foreignPivotKey, relatedPivotKey, parentKey, relatedKey) {
3913
- super(query, parent);
3914
- this.table = table;
3915
- this.foreignPivotKey = foreignPivotKey;
3916
- this.relatedPivotKey = relatedPivotKey;
3917
- this.parentKey = parentKey;
3918
- this.relatedKey = relatedKey;
3919
- this.addConstraints();
3920
- return this.asProxy();
3921
- }
3922
- initRelation(models, relation) {
3923
- models.map((model) => {
3924
- model.setRelation(relation, new collection_default([]));
3925
- });
3926
- return models;
3927
- }
3928
- addConstraints() {
3929
- this.performJoin();
3930
- if (this.constructor.constraints) this.addWhereConstraints();
3931
- }
3932
- performJoin(query = null) {
3933
- query = query || this.query;
3934
- query.join(this.getTable(), this.getQualifiedRelatedKeyName(), "=", this.qualifyPivotColumn(this.relatedPivotKey));
3935
- return this;
3936
- }
3937
- getTable() {
3938
- return this.table;
3939
- }
3940
- getQualifiedRelatedKeyName() {
3941
- return this.related.qualifyColumn(this.relatedKey);
3942
- }
3943
- async getResults() {
3944
- return this.parent[this.parentKey] !== null ? await this.get() : new collection_default([]);
3945
- }
3946
- addWhereConstraints() {
3947
- this.query.where(this.getQualifiedForeignPivotKeyName(), "=", this.parent[this.parentKey]);
3948
- return this;
3949
- }
3950
- async get(columns) {
3951
- var _builder$query;
3952
- const builder = this.query.applyScopes();
3953
- columns = ((_builder$query = builder.query) === null || _builder$query === void 0 || (_builder$query = _builder$query._statements) === null || _builder$query === void 0 ? void 0 : _builder$query.find((item) => item.grouping == "columns")) ? [] : columns;
3954
- let models = await builder.select(this.shouldSelect(columns)).getModels();
3955
- this.hydratePivotRelation(models);
3956
- if (models.length > 0) models = await builder.eagerLoadRelations(models);
3957
- return new collection_default(models);
3958
- }
3959
- async first(columns = ["*"]) {
3960
- const results = await this.take(1).get(columns);
3961
- return results.count() > 0 ? results.first() : null;
3962
- }
3963
- async firstOrFail(...columns) {
3964
- const model = await this.first(...columns);
3965
- if (model !== null) return model;
3966
- throw new ModelNotFoundError().setModel(this.related.constructor);
3967
- }
3968
- async paginate(page = 1, perPage = 15, columns = ["*"]) {
3969
- this.query.select(this.shouldSelect(columns));
3970
- return tap(await this.query.paginate(page, perPage), (paginator) => {
3971
- this.hydratePivotRelation(paginator.items());
3972
- });
3973
- }
3974
- async chunk(count, callback) {
3975
- return await this.prepareQueryBuilder().chunk(count, async (results, page) => {
3976
- this.hydratePivotRelation(results.all());
3977
- return await callback(results, page);
3978
- });
3979
- }
3980
- setUsing(model) {
3981
- this.using = model;
3982
- return this;
3983
- }
3984
- as(accessor) {
3985
- this.accessor = accessor;
3986
- return this;
3987
- }
3988
- prepareQueryBuilder() {
3989
- return this.query.select(this.shouldSelect());
3990
- }
3991
- hydratePivotRelation(models) {
3992
- models.map((model) => {
3993
- model.setRelation(this.accessor, this.newExistingPivot(this.migratePivotAttributes(model)));
3994
- });
3995
- }
3996
- migratePivotAttributes(model) {
3997
- const values = {};
3998
- for (const key in model.attributes) {
3999
- const value = model.attributes[key];
4000
- if (key.startsWith("pivot_")) {
4001
- values[key.substring(6)] = value;
4002
- model.attributes = (0, radashi.omit)(model.attributes, [key]);
4003
- }
4004
- }
4005
- return values;
4006
- }
4007
- withTimestamps(createdAt = null, updatedAt = null) {
4008
- this.pivotCreatedAt = createdAt;
4009
- this.pivotUpdatedAt = updatedAt;
4010
- return this.withPivot(this.createdAt(), this.updatedAt());
4011
- }
4012
- shouldSelect(columns = ["*"]) {
4013
- if ((0, radashi.isEqual)(columns, ["*"])) columns = [this.related.getTable() + ".*"];
4014
- return columns.concat(this.aliasedPivotColumns());
4015
- }
4016
- aliasedPivotColumns() {
4017
- const defaults = [this.foreignPivotKey, this.relatedPivotKey];
4018
- return (0, collect_js.collect)(defaults.concat(this.pivotColumns)).map((column) => {
4019
- return this.qualifyPivotColumn(column) + " as pivot_" + column;
4020
- }).unique().all();
4021
- }
4022
- qualifyPivotColumn(column) {
4023
- return column.includes(".") ? column : this.getTable() + "." + column;
4024
- }
4025
- match(models, results, relation) {
4026
- const dictionary = this.buildDictionary(results);
4027
- models.map((model) => {
4028
- const key = model.getKey();
4029
- if (dictionary[key] !== void 0) model.setRelation(relation, dictionary[key]);
4030
- });
4031
- return models;
4032
- }
4033
- buildDictionary(results) {
4034
- const dictionary = {};
4035
- results.map((result) => {
4036
- const value = result[this.accessor][this.foreignPivotKey];
4037
- if (dictionary[value] === void 0) dictionary[value] = new collection_default([]);
4038
- dictionary[value].push(result);
4039
- });
4040
- return dictionary;
4041
- }
4042
- addEagerConstraints(models) {
4043
- this.query.whereIn(this.getQualifiedForeignPivotKeyName(), this.getKeys(models, this.parentKey));
4044
- }
4045
- getQualifiedForeignPivotKeyName() {
4046
- return this.qualifyPivotColumn(this.foreignPivotKey);
4047
- }
4048
- getQualifiedRelatedPivotKeyName() {
4049
- return this.qualifyPivotColumn(this.relatedPivotKey);
4050
- }
4051
- wherePivot(column, operator = null, value = null, boolean = "and") {
4052
- this.pivotWheres.push(Array.prototype.slice.call(arguments));
4053
- return this.where(this.qualifyPivotColumn(column), operator, value, boolean);
4054
- }
4055
- wherePivotBetween(column, values, boolean = "and", not = false) {
4056
- return this.whereBetween(this.qualifyPivotColumn(column), values, boolean, not);
4057
- }
4058
- orWherePivotBetween(column, values) {
4059
- return this.wherePivotBetween(column, values, "or");
4060
- }
4061
- wherePivotNotBetween(column, values, boolean = "and") {
4062
- return this.wherePivotBetween(column, values, boolean, true);
4063
- }
4064
- orWherePivotNotBetween(column, values) {
4065
- return this.wherePivotBetween(column, values, "or", true);
4066
- }
4067
- wherePivotIn(column, values, boolean = "and", not = false) {
4068
- return this.whereIn(this.qualifyPivotColumn(column), values, boolean, not);
4069
- }
4070
- orWherePivot(column, operator = null, value = null) {
4071
- return this.wherePivot(column, operator, value, "or");
4072
- }
4073
- orWherePivotIn(column, values) {
4074
- return this.wherePivotIn(column, values, "or");
4075
- }
4076
- wherePivotNotIn(column, values, boolean = "and") {
4077
- return this.wherePivotIn(column, values, boolean, true);
4078
- }
4079
- orWherePivotNotIn(column, values) {
4080
- return this.wherePivotNotIn(column, values, "or");
4081
- }
4082
- wherePivotNull(column, boolean = "and", not = false) {
4083
- return this.whereNull(this.qualifyPivotColumn(column), boolean, not);
4084
- }
4085
- wherePivotNotNull(column, boolean = "and") {
4086
- return this.wherePivotNull(column, boolean, true);
4087
- }
4088
- orWherePivotNull(column, not = false) {
4089
- return this.wherePivotNull(column, "or", not);
4090
- }
4091
- orWherePivotNotNull(column) {
4092
- return this.orWherePivotNull(column, true);
4093
- }
4094
- orderByPivot(column, direction = "asc") {
4095
- return this.orderBy(this.qualifyPivotColumn(column), direction);
4096
- }
4097
- createdAt() {
4098
- return this.pivotCreatedAt || this.parent.getCreatedAtColumn();
4099
- }
4100
- updatedAt() {
4101
- return this.pivotUpdatedAt || this.parent.getUpdatedAtColumn();
4102
- }
4103
- getExistenceCompareKey() {
4104
- return this.getQualifiedForeignPivotKeyName();
4105
- }
4106
- getRelationExistenceQuery(query, parentQuery, columns = ["*"]) {
4107
- if (parentQuery.getQuery()._single.table == query.getQuery()._single.table) return this.getRelationExistenceQueryForSelfJoin(query, parentQuery, columns);
4108
- this.performJoin(query);
4109
- return super.getRelationExistenceQuery(query, parentQuery, columns);
4110
- }
4111
- getRelationExistenceQueryForSelfJoin(query, parentQuery, columns = ["*"]) {
4112
- const hash = this.getRelationCountHash();
4113
- query.select(columns).from(this.related.getTable() + " as " + hash);
4114
- this.related.setTable(hash);
4115
- this.performJoin(query);
4116
- return super.getRelationExistenceQuery(query, parentQuery, columns);
4117
- }
4118
- };
4119
- var belongs_to_many_default = BelongsToMany;
4120
-
4121
- //#endregion
4122
- //#region src/builder.ts
4123
- const Inference = class {};
4124
- var Builder = class Builder extends Inference {
4125
- query;
4126
- connection;
4127
- model;
4128
- actions;
4129
- localMacros = {};
4130
- eagerLoad = {};
4131
- globalScopes = {};
4132
- onDeleteCallback;
4133
- constructor(query) {
4134
- super();
4135
- this.query = query;
4136
- return this.asProxy();
4137
- }
4138
- asProxy() {
4139
- return new Proxy(this, { get(target, prop) {
4140
- var _target$query$connect;
4141
- if (typeof target[prop] !== "undefined") return target[prop];
4142
- const skipReturning = !!((_target$query$connect = target.query.connector) === null || _target$query$connect === void 0 || (_target$query$connect = _target$query$connect.client.config) === null || _target$query$connect === void 0 || (_target$query$connect = _target$query$connect.client) === null || _target$query$connect === void 0 ? void 0 : _target$query$connect.includes("mysql")) && prop === "returning";
4143
- if ([
4144
- "select",
4145
- "from",
4146
- "where",
4147
- "orWhere",
4148
- "whereColumn",
4149
- "whereRaw",
4150
- "whereNot",
4151
- "orWhereNot",
4152
- "whereIn",
4153
- "orWhereIn",
4154
- "whereNotIn",
4155
- "orWhereNotIn",
4156
- "whereNull",
4157
- "orWhereNull",
4158
- "whereNotNull",
4159
- "orWhereNotNull",
4160
- "whereExists",
4161
- "orWhereExists",
4162
- "whereNotExists",
4163
- "orWhereNotExists",
4164
- "whereBetween",
4165
- "orWhereBetween",
4166
- "whereNotBetween",
4167
- "orWhereNotBetween",
4168
- "whereLike",
4169
- "orWhereLike",
4170
- "whereILike",
4171
- "orWhereILike",
4172
- "whereJsonObject",
4173
- "whereJsonPath",
4174
- "whereJsonSupersetOf",
4175
- "whereJsonSubsetOf",
4176
- "join",
4177
- "joinRaw",
4178
- "leftJoin",
4179
- "leftOuterJoin",
4180
- "rightJoin",
4181
- "rightOuterJoin",
4182
- "crossJoin",
4183
- "transacting",
4184
- "groupBy",
4185
- "groupByRaw",
4186
- "returning",
4187
- "having",
4188
- "havingRaw",
4189
- "havingBetween",
4190
- "limit",
4191
- "offset",
4192
- "orderBy",
4193
- "orderByRaw",
4194
- "union",
4195
- "insert",
4196
- "forUpdate",
4197
- "forShare",
4198
- "distinct",
4199
- "clearOrder",
4200
- "clear",
4201
- "clearSelect",
4202
- "clearWhere",
4203
- "clearHaving",
4204
- "clearGroup"
4205
- ].includes(prop) && !skipReturning) return (...args) => {
4206
- target.query[prop](...args);
4207
- return target.asProxy();
4208
- };
4209
- if ([
4210
- "avg",
4211
- "max",
4212
- "min",
4213
- "sum",
4214
- "count"
4215
- ].includes(prop)) return (column) => {
4216
- const instance = target.asProxy();
4217
- instance.applyScopes();
4218
- column = !column && prop === "count" ? "*" : column;
4219
- return instance.query[prop](column);
4220
- };
4221
- if (typeof prop === "string") {
4222
- if (target.hasMacro(prop)) {
4223
- const instance = target.asProxy();
4224
- return (...args) => {
4225
- return instance.localMacros[prop](instance, ...args);
4226
- };
4227
- }
4228
- if (target.hasNamedScope(prop)) {
4229
- const instance = target.asProxy();
4230
- return (...args) => {
4231
- instance.callNamedScope(prop, args);
4232
- return instance;
4233
- };
4234
- }
4235
- if (prop.startsWith("where")) {
4236
- const column = (0, radashi.snake)(prop.substring(5));
4237
- return (...args) => {
4238
- target.query.where(column, ...args);
4239
- return target.asProxy();
4240
- };
4241
- }
4242
- }
4243
- } });
4244
- }
4245
- orWhere(...args) {
4246
- if (typeof args[0] === "function") {
4247
- const callback = args[0];
4248
- this.query.orWhere((query) => {
4249
- this.query = query;
4250
- callback(this);
4251
- });
4252
- return this;
4253
- }
4254
- this.query.orWhere(...args);
4255
- return this;
4256
- }
4257
- async chunk(count, callback) {
4258
- let page = 1;
4259
- let countResults;
4260
- do {
4261
- this.enforceOrderBy();
4262
- const results = await this.clone().forPage(page, count).get();
4263
- countResults = results.count();
4264
- if (countResults == 0) break;
4265
- if (await callback(results, page) === false) return false;
4266
- page++;
4267
- } while (countResults === count);
4268
- return true;
4269
- }
4270
- enforceOrderBy() {
4271
- if (this.query._statements.filter((item) => item.grouping === "order").length === 0) this.orderBy(this.model.getQualifiedKeyName(), "asc");
4272
- }
4273
- clone() {
4274
- const query = this.query.clone();
4275
- const builder = new this.constructor(query);
4276
- builder.connection = this.connection;
4277
- builder.setModel(this.model);
4278
- builder.globalScopes = { ...this.globalScopes };
4279
- builder.localMacros = { ...this.localMacros };
4280
- builder.eagerLoad = { ...this.eagerLoad };
4281
- return builder;
4282
- }
4283
- forPage(page, perPage = 15) {
4284
- return this.offset((page - 1) * perPage).limit(perPage);
4285
- }
4286
- insert(...args) {
4287
- return this.query.insert(...args);
4288
- }
4289
- update(values) {
4290
- this.applyScopes();
4291
- return this.query.update(this.addUpdatedAtColumn(values));
4292
- }
4293
- increment(column, amount = 1, extra = {}) {
4294
- this.applyScopes();
4295
- const db = this.model.getConnection();
4296
- return this.query.update(this.addUpdatedAtColumn({
4297
- ...extra,
4298
- [column]: db.raw(`${column} + ${amount}`)
4299
- }));
4300
- }
4301
- decrement(column, amount = 1, extra = {}) {
4302
- this.applyScopes();
4303
- const db = this.model.getConnection();
4304
- return this.query.update(this.addUpdatedAtColumn({
4305
- ...extra,
4306
- [column]: db.raw(`${column} - ${amount}`)
4307
- }));
4308
- }
4309
- addUpdatedAtColumn(values) {
4310
- if (!this.model.usesTimestamps() || this.model.getUpdatedAtColumn() === null) return values;
4311
- const column = this.model.getUpdatedAtColumn();
4312
- values = (0, radashi.assign)({ [column]: this.model.freshTimestampString() }, values);
4313
- return values;
4314
- }
4315
- delete() {
4316
- if (this.onDeleteCallback) return this.onDeleteCallback(this);
4317
- return this.query.delete();
4318
- }
4319
- onDelete(callback) {
4320
- this.onDeleteCallback = callback;
4321
- }
4322
- forceDelete() {
4323
- return this.query.delete();
4324
- }
4325
- async create(attributes = {}) {
4326
- return await tap(this.newModelInstance(attributes), async (instance) => {
4327
- await instance.save({ client: this.query });
4328
- });
4329
- }
4330
- newModelInstance(attributes = {}) {
4331
- return this.model.newInstance(attributes).setConnection(this.model.getConnectionName());
4332
- }
4333
- getQuery() {
4334
- return this.query;
4335
- }
4336
- getModel() {
4337
- return this.model;
4338
- }
4339
- setModel(model) {
4340
- var _this$query;
4341
- this.model = model;
4342
- if (typeof ((_this$query = this.query) === null || _this$query === void 0 || (_this$query = _this$query.client) === null || _this$query === void 0 ? void 0 : _this$query.table) == "function") this.query = this.query.client.table(this.model.getTable());
4343
- else this.query = this.query.table(this.model.getTable());
4344
- return this;
4345
- }
4346
- qualifyColumn(column) {
4347
- return this.model.qualifyColumn(column);
4348
- }
4349
- setTable(table) {
4350
- this.query = this.query.table(table);
4351
- return this;
4352
- }
4353
- applyScopes() {
4354
- if (!this.globalScopes) return this;
4355
- for (const identifier in this.globalScopes) {
4356
- const scope = this.globalScopes[identifier];
4357
- if (scope instanceof scope_default) scope.apply(this, this.getModel());
4358
- else scope(this);
4359
- }
4360
- return this;
4361
- }
4362
- hasNamedScope(name) {
4363
- return this.model && this.model.hasNamedScope(name);
4364
- }
4365
- callNamedScope(scope, parameters) {
4366
- return this.model.callNamedScope(scope, [this, ...parameters]);
4367
- }
4368
- callScope(scope, parameters = []) {
4369
- return scope(this, ...parameters) || this;
4370
- }
4371
- scopes(scopes) {
4372
- scopes.map((scopeName) => {
4373
- const scopeMethod = getScopeMethod(scopeName);
4374
- if (typeof this.model[scopeMethod] === "function") this.globalScopes[scopeName] = this.model[scopeMethod];
4375
- });
4376
- return this;
4377
- }
4378
- withGlobalScope(identifier, scope) {
4379
- this.globalScopes[identifier] = scope;
4380
- if (typeof scope.extend === "function") scope.extend(this);
4381
- return this;
4382
- }
4383
- withoutGlobalScope(scope) {
4384
- if (typeof scope !== "string") scope = scope.constructor.name;
4385
- this.globalScopes = (0, radashi.omit)(this.globalScopes, [scope]);
4386
- return this;
4387
- }
4388
- macro(name, callback) {
4389
- this.localMacros[name] = callback;
4390
- return this;
4391
- }
4392
- hasMacro(name) {
4393
- return name in this.localMacros;
4394
- }
4395
- getMacro(name) {
4396
- return this.localMacros[name];
4397
- }
4398
- with(...args) {
4399
- let eagerLoads = {};
4400
- if (typeof args[1] === "function") {
4401
- const eagerLoad = this.parseWithRelations({ [args[0]]: args[1] });
4402
- this.eagerLoad = (0, radashi.assign)(this.eagerLoad, eagerLoad);
4403
- return this;
4404
- }
4405
- const relations = flattenDeep(args);
4406
- if (relations.length === 0) return this;
4407
- for (const relation of relations) {
4408
- let eagerLoad;
4409
- if (typeof relation === "string") eagerLoad = { [relation]: (q) => q };
4410
- else if (typeof relation === "object") eagerLoad = relation;
4411
- eagerLoads = (0, radashi.assign)(eagerLoads, eagerLoad);
4412
- }
4413
- this.eagerLoad = (0, radashi.assign)(this.eagerLoad, this.parseWithRelations(eagerLoads));
4414
- return this;
4415
- }
4416
- has(relation, operator = ">=", count = 1, boolean = "and", callback = null) {
4417
- if ((0, radashi.isString)(relation)) {
4418
- if (relation.includes(".")) return this.hasNested(relation, operator, count, boolean, callback);
4419
- relation = this.getRelationWithoutConstraints(getRelationMethod(relation));
4420
- }
4421
- const method = this.canUseExistsForExistenceCheck(operator, count) ? "getRelationExistenceQuery" : "getRelationExistenceCountQuery";
4422
- const hasQuery = relation[method](relation.getRelated().newModelQuery(), this);
4423
- if (callback) callback(hasQuery);
4424
- return this.addHasWhere(hasQuery, relation, operator, count, boolean);
4425
- }
4426
- orHas(relation, operator = ">=", count = 1) {
4427
- return this.has(relation, operator, count, "or");
4428
- }
4429
- doesntHave(relation, boolean = "and", callback = null) {
4430
- return this.has(relation, "<", 1, boolean, callback);
4431
- }
4432
- orDoesntHave(relation) {
4433
- return this.doesntHave(relation, "or");
4434
- }
4435
- whereHas(relation, callback = null, operator = ">=", count = 1) {
4436
- return this.has(relation, operator, count, "and", callback);
4437
- }
4438
- orWhereHas(relation, callback = null, operator = ">=", count = 1) {
4439
- return this.has(relation, operator, count, "or", callback);
4440
- }
4441
- whereRelation(relation, ...args) {
4442
- const column = args.shift();
4443
- return this.whereHas(relation, (query) => {
4444
- if (typeof column === "function") column(query);
4445
- else query.where(column, ...args);
4446
- });
4447
- }
4448
- orWhereRelation(relation, ...args) {
4449
- const column = args.shift();
4450
- return this.orWhereHas(relation, function(query) {
4451
- if (typeof column === "function") column(query);
4452
- else query.where(column, ...args);
4453
- });
4454
- }
4455
- hasNested(relations, operator = ">=", count = 1, boolean = "and", callback = null) {
4456
- relations = relations.split(".");
4457
- const doesntHave = operator === "<" && count === 1;
4458
- if (doesntHave) {
4459
- operator = ">=";
4460
- count = 1;
4461
- }
4462
- const closure = (q) => {
4463
- if (relations.length > 1) q.whereHas(relations.shift(), closure);
4464
- else q.has(relations.shift(), operator, count, "and", callback);
4465
- return null;
4466
- };
4467
- return this.has(relations.shift(), doesntHave ? "<" : ">=", 1, boolean, closure);
4468
- }
4469
- canUseExistsForExistenceCheck(operator, count) {
4470
- return (operator === ">=" || operator === "<") && count === 1;
4471
- }
4472
- addHasWhere(hasQuery, relation, operator, count, boolean) {
4473
- hasQuery.mergeConstraintsFrom(relation.getQuery());
4474
- return this.canUseExistsForExistenceCheck(operator, count) ? this.addWhereExistsQuery(hasQuery.getQuery(), boolean, operator === "<" && count === 1) : this.addWhereCountQuery(hasQuery.getQuery(), operator, count, boolean);
4475
- }
4476
- addWhereExistsQuery(query, boolean = "and", not = false) {
4477
- const type = not ? "NotExists" : "Exists";
4478
- const method = boolean === "and" ? "where" + type : "orWhere" + type;
4479
- this[method](query.connector);
4480
- return this;
4481
- }
4482
- addWhereCountQuery(query, operator = ">=", count = 1, boolean = "and") {
4483
- const db = this.model.getConnection();
4484
- return this.where(db.raw("(" + query.toSQL().sql + ")"), operator, typeof count === "number" ? db.raw(count) : count, boolean);
4485
- }
4486
- withAggregate(relations, column, action = null) {
4487
- if (relations.length === 0) return this;
4488
- relations = flattenDeep([relations]);
4489
- let eagerLoads = {};
4490
- for (const relation of relations) {
4491
- let eagerLoad;
4492
- if (typeof relation === "string") eagerLoad = { [relation]: (q) => q };
4493
- else if (typeof relation === "object") eagerLoad = relation;
4494
- eagerLoads = (0, radashi.assign)(eagerLoads, eagerLoad);
4495
- }
4496
- relations = eagerLoads;
4497
- const db = this.model.getConnection();
4498
- if (this.query._statements.filter((item) => item.grouping == "columns").map((item) => item.value).flat().length === 0) this.query.select([this.query._single.table + ".*"]);
4499
- const parses = this.parseWithRelations(relations);
4500
- for (let name in parses) {
4501
- const constraints = parses[name];
4502
- const segments = name.split(" ");
4503
- let alias, expression;
4504
- if (segments.length === 3 && segments[1].toLocaleLowerCase() === "as") [name, alias] = [segments[0], segments[2]];
4505
- const relation = this.getRelationWithoutConstraints(getRelationMethod(name));
4506
- if (action) {
4507
- const hashedColumn = this.query._single.table === relation.query.query._single.table ? `${relation.getRelationCountHash(false)}.${column}` : column;
4508
- const wrappedColumn = column === "*" ? column : relation.getRelated().qualifyColumn(hashedColumn);
4509
- expression = action === "exists" ? wrappedColumn : `${action}(${wrappedColumn})`;
4510
- } else expression = column;
4511
- const query = relation.getRelationExistenceQuery(relation.getRelated().newModelQuery(), this, db.raw(expression));
4512
- constraints(query);
4513
- alias = alias || (0, radashi.snake)(`${name} ${action} ${column}`.replace("/[^[:alnum:][:space:]_]/u", ""));
4514
- if (action === "exists") this.select(db.raw(`exists(${query.toSql().sql}) as ${alias}`));
4515
- else this.selectSub(action ? query : query.limit(1), alias);
4516
- }
4517
- return this;
4518
- }
4519
- toSql() {
4520
- const query = this.clone();
4521
- query.applyScopes();
4522
- return query.query.toSQL();
4523
- }
4524
- mergeConstraintsFrom(_from) {
4525
- return this;
4526
- }
4527
- selectSub(query, as) {
4528
- const [querySub, bindings] = this.createSub(query);
4529
- const db = this.model.getConnection();
4530
- return this.select(db.raw("(" + querySub + ") as " + as, bindings));
4531
- }
4532
- createSub(query) {
4533
- return this.parseSub(query);
4534
- }
4535
- parseSub(query) {
4536
- if (query instanceof Builder || query instanceof relation_default) return [query.toSql().sql, query.toSql().bindings];
4537
- else if ((0, radashi.isString)(query)) return [query, []];
4538
- else throw new Error("A subquery must be a query builder instance, a Closure, or a string.");
4539
- }
4540
- prependDatabaseNameIfCrossDatabaseQuery(query) {
4541
- if (query.query._single.table !== this.query._single.table) {
4542
- const databaseName = query.query._single.table;
4543
- if (!query.query._single.table.startsWith(databaseName) && !query.query._single.table.contains(".")) query.from(databaseName + "." + query.from);
4544
- }
4545
- return query;
4546
- }
4547
- getRelationWithoutConstraints(relation) {
4548
- return relation_default.noConstraints(() => {
4549
- return this.getModel()[relation]();
4550
- });
4551
- }
4552
- withCount(...args) {
4553
- return this.withAggregate(flattenDeep(args), "*", "count");
4554
- }
4555
- withMax(relation, column) {
4556
- return this.withAggregate(relation, column, "max");
4557
- }
4558
- withMin(relation, column) {
4559
- return this.withAggregate(relation, column, "min");
4560
- }
4561
- withAvg(relation, column) {
4562
- return this.withAggregate(relation, column, "avg");
4563
- }
4564
- withSum(relation, column) {
4565
- return this.withAggregate(relation, column, "sum");
4566
- }
4567
- withExists(relation) {
4568
- return this.withAggregate(relation, "*", "exists");
4569
- }
4570
- parseWithRelations(relations) {
4571
- if (relations.length === 0) return [];
4572
- let results = {};
4573
- const constraintsMap = this.prepareNestedWithRelationships(relations);
4574
- for (const name in constraintsMap) {
4575
- results = this.addNestedWiths(name, results);
4576
- results[name] = constraintsMap[name];
4577
- }
4578
- return results;
4579
- }
4580
- addNestedWiths(name, results) {
4581
- const progress = [];
4582
- name.split(".").map((segment) => {
4583
- progress.push(segment);
4584
- const last = progress.join(".");
4585
- if (results[last] === void 0) results[last] = () => {};
4586
- });
4587
- return results;
4588
- }
4589
- prepareNestedWithRelationships(relations, prefix = "") {
4590
- let preparedRelationships = {};
4591
- if (prefix !== "") prefix += ".";
4592
- for (const key in relations) {
4593
- const value = relations[key];
4594
- if ((0, radashi.isString)(value) || Number.isFinite(parseInt(value))) continue;
4595
- const [attribute, attributeSelectConstraint] = this.parseNameAndAttributeSelectionConstraint(key, value);
4596
- preparedRelationships = Object.assign({}, preparedRelationships, { [`${prefix}${attribute}`]: attributeSelectConstraint }, this.prepareNestedWithRelationships(value, `${prefix}${attribute}`));
4597
- relations = (0, radashi.omit)(relations, [key]);
4598
- }
4599
- for (const key in relations) {
4600
- const value = relations[key];
4601
- let attribute = key, attributeSelectConstraint = value;
4602
- if ((0, radashi.isString)(value)) [attribute, attributeSelectConstraint] = this.parseNameAndAttributeSelectionConstraint(value);
4603
- preparedRelationships[`${prefix}${attribute}`] = this.combineConstraints([attributeSelectConstraint, preparedRelationships[`${prefix}${attribute}`] || (() => {})]);
4604
- }
4605
- return preparedRelationships;
4606
- }
4607
- combineConstraints(constraints) {
4608
- return (builder) => {
4609
- constraints.map((constraint) => {
4610
- builder = constraint(builder) || builder;
4611
- });
4612
- return builder;
4613
- };
4614
- }
4615
- parseNameAndAttributeSelectionConstraint(name, value) {
4616
- return name.includes(":") ? this.createSelectWithConstraint(name) : [name, value];
4617
- }
4618
- createSelectWithConstraint(name) {
4619
- return [name.split(":")[0], (query) => {
4620
- query.select(name.split(":")[1].split(",").map((column) => {
4621
- if (column.includes(".")) return column;
4622
- return query instanceof belongs_to_many_default ? query.related.getTable() + "." + column : column;
4623
- }));
4624
- }];
4625
- }
4626
- related(relation) {
4627
- if (typeof this.model[getRelationMethod(relation)] !== "function") {
4628
- const message = `Model [${this.model.constructor.name}]'s relation [${relation}] doesn't exist.`;
4629
- throw new RelationNotFoundError(message);
4630
- }
4631
- return this.model[getRelationMethod(relation)]();
4632
- }
4633
- take(...args) {
4634
- return this.limit(...args);
4635
- }
4636
- skip(...args) {
4637
- return this.offset(...args);
4638
- }
4639
- async first(...columns) {
4640
- this.applyScopes();
4641
- this.limit(1);
4642
- let models = await this.getModels(columns);
4643
- if (models.length > 0) models = await this.eagerLoadRelations(models);
4644
- return models[0] || null;
4645
- }
4646
- async firstOrFail(...columns) {
4647
- const data = await this.first(...columns);
4648
- if (data === null) throw new ModelNotFoundError().setModel(this.model.constructor.name);
4649
- return data;
4650
- }
4651
- async findOrFail(...args) {
4652
- const data = await this.find(...args);
4653
- if ((0, radashi.isArray)(args[0])) {
4654
- if (data.count() !== args[0].length) throw new ModelNotFoundError().setModel(this.model.constructor.name, (0, radashi.diff)(args[0], data.modelKeys()));
4655
- return data;
4656
- }
4657
- if (data === null) throw new ModelNotFoundError().setModel(this.model.constructor.name, args[0]);
4658
- return data;
4659
- }
4660
- async findOrNew(id, columns = ["*"]) {
4661
- const model = await this.find(id, columns);
4662
- if (model !== null) return model;
4663
- return this.newModelInstance();
4664
- }
4665
- async firstOrNew(attributes = {}, values = {}) {
4666
- const instance = await this.where(attributes).first();
4667
- if (instance !== null) return instance;
4668
- return this.newModelInstance((0, radashi.assign)(attributes, values));
4669
- }
4670
- async firstOrCreate(attributes = {}, values = {}) {
4671
- const instance = await this.where(attributes).first();
4672
- if (instance !== null) return instance;
4673
- return tap(this.newModelInstance((0, radashi.assign)(attributes, values)), async (instance$1) => {
4674
- await instance$1.save({ client: this.query });
4675
- });
4676
- }
4677
- async updateOrCreate(attributes, values = {}) {
4678
- return await tap(await this.firstOrNew(attributes), async (instance) => {
4679
- await instance.fill(values).save({ client: this.query });
4680
- });
4681
- }
4682
- latest(column = "id") {
4683
- if (column === null) column = this.model.getCreatedAtColumn() || "created_at";
4684
- this.query.orderBy(column, "desc");
4685
- return this;
4686
- }
4687
- oldest(column = "id") {
4688
- if (column === null) column = this.model.getCreatedAtColumn() || "created_at";
4689
- this.query.orderBy(column, "asc");
4690
- return this;
4691
- }
4692
- async find(id, columns) {
4693
- if ((0, radashi.isArray)(id) || id instanceof collection_default) return await this.findMany(id, columns);
4694
- return await this.where(this.model.getKeyName(), id).first(columns);
4695
- }
4696
- async findMany(ids, columns = ["*"]) {
4697
- if (ids instanceof collection_default) ids = ids.modelKeys();
4698
- ids = (0, radashi.isArray)(ids) ? ids : [ids];
4699
- if (ids.length === 0) return new collection_default([]);
4700
- return await this.whereIn(this.model.getKeyName(), ids).get(columns);
4701
- }
4702
- async pluck(column) {
4703
- const data = await this.query.pluck(column);
4704
- return new collection_default(data);
4705
- }
4706
- async destroy(ids) {
4707
- if (ids instanceof collection_default) ids = ids.modelKeys();
4708
- if (ids instanceof collect_js.Collection) ids = ids.all();
4709
- ids = (0, radashi.isArray)(ids) ? ids : Array.prototype.slice.call(ids);
4710
- if (ids.length === 0) return 0;
4711
- const key = this.model.newInstance().getKeyName();
4712
- let count = 0;
4713
- const models = await this.model.newModelQuery().whereIn(key, ids).get();
4714
- for (const model of models) if (await model.delete()) count++;
4715
- return count;
4716
- }
4717
- async get(columns = ["*"]) {
4718
- this.applyScopes();
4719
- let models = await this.getModels(columns);
4720
- if (models.length > 0) models = await this.eagerLoadRelations(models);
4721
- return new collection_default(models);
4722
- }
4723
- async all(columns = ["*"]) {
4724
- return await this.model.newModelQuery().get(columns);
4725
- }
4726
- async paginate(page = 1, perPage = 10) {
4727
- var _this;
4728
- page = page || 1;
4729
- perPage = perPage || ((_this = this) === null || _this === void 0 || (_this = _this.model) === null || _this === void 0 ? void 0 : _this.perPage) || 15;
4730
- this.applyScopes();
4731
- const total = await this.query.clone().clearOrder().clearSelect().count(this.primaryKey);
4732
- let results = [];
4733
- if (total > 0) {
4734
- const skip = (page - 1) * (perPage ?? 10);
4735
- this.take(perPage).skip(skip);
4736
- results = await this.getModels();
4737
- if (results.length > 0) results = await this.eagerLoadRelations(results);
4738
- } else results = [];
4739
- return new paginator_default(results, parseInt(total), perPage, page);
4740
- }
4741
- async getModels(...columns) {
4742
- columns = (0, radashi.flat)(columns);
4743
- if (columns.length > 0) {
4744
- if (this.query._statements.filter((item) => item.grouping == "columns").length == 0 && columns[0] !== "*") this.query.select(...columns);
4745
- }
4746
- return this.hydrate(await this.query.get()).all();
4747
- }
4748
- getRelation(name) {
4749
- if (typeof this.model[getRelationMethod(name)] !== "function") {
4750
- const message = `Model [${this.model.constructor.name}]'s relation [${name}] doesn't exist.`;
4751
- throw new RelationNotFoundError(message);
4752
- }
4753
- const relation = relation_default.noConstraints(() => this.model.newInstance(this.model.attributes)[getRelationMethod(name)]());
4754
- const nested = this.relationsNestedUnder(name);
4755
- if (Object.keys(nested).length > 0) relation.query.with(nested);
4756
- return relation.asProxy();
4757
- }
4758
- relationsNestedUnder(relation) {
4759
- const nested = {};
4760
- for (const name in this.eagerLoad) {
4761
- const constraints = this.eagerLoad[name];
4762
- if (this.isNestedUnder(relation, name)) nested[name.substring((relation + ".").length)] = constraints;
4763
- }
4764
- return nested;
4765
- }
4766
- isNestedUnder(relation, name) {
4767
- return name.includes(".") && name.startsWith(relation + ".");
4768
- }
4769
- async eagerLoadRelation(models, name, constraints) {
4770
- const relation = this.getRelation(name);
4771
- relation.addEagerConstraints(models);
4772
- constraints(relation);
4773
- return relation.match(relation.initRelation(models, name), await relation.get(), name);
4774
- }
4775
- async eagerLoadRelations(models) {
4776
- for (const name in this.eagerLoad) {
4777
- const constraints = this.eagerLoad[name];
4778
- if (!name.includes(".")) models = await this.eagerLoadRelation(models, name, constraints);
4779
- }
4780
- return models;
4781
- }
4782
- hydrate(items) {
4783
- return new collection_default(items.map((item) => {
4784
- if (!this.model) return item;
4785
- return this.model.newFromBuilder(item);
4786
- }));
4787
- }
4788
- };
4789
- var builder_default = Builder;
4790
-
4791
- //#endregion
4792
- //#region src/inspector/dialects/sqlite.ts
4793
- function parseDefaultValue(value) {
4794
- if (value === null || value.trim().toLowerCase() === "null") return null;
4795
- return stripQuotes(value);
4796
- }
4797
- var SQLite = class {
4798
- knex;
4799
- constructor(knex$1) {
4800
- this.knex = knex$1;
4801
- }
4802
- /**
4803
- * List all existing tables in the current schema/database
4804
- */
4805
- async tables() {
4806
- return (await this.knex.select("name").from("sqlite_master").whereRaw("type = 'table' AND name NOT LIKE 'sqlite_%'")).map(({ name }) => name);
4807
- }
4808
- async tableInfo(table) {
4809
- const query = this.knex.select("name", "sql").from("sqlite_master").where({ type: "table" }).andWhereRaw("name NOT LIKE 'sqlite_%'");
4810
- if (table) query.andWhere({ name: table });
4811
- let records = await query;
4812
- records = records.map((table$1) => ({
4813
- name: table$1.name,
4814
- sql: table$1.sql
4815
- }));
4816
- if (table) return records[0];
4817
- return records;
4818
- }
4819
- /**
4820
- * Check if a table exists in the current schema/database
4821
- */
4822
- async hasTable(table) {
4823
- return (await this.knex.select(1).from("sqlite_master").where({
4824
- type: "table",
4825
- name: table
4826
- })).length > 0;
4827
- }
4828
- /**
4829
- * Get all the available columns in the current schema/database. Can be filtered to a specific table
4830
- */
4831
- async columns(table) {
4832
- if (table) return (await this.knex.raw("PRAGMA table_xinfo(??)", table)).map((column) => ({
4833
- table,
4834
- column: column.name
4835
- }));
4836
- const tables = await this.tables();
4837
- const columnsPerTable = await Promise.all(tables.map(async (table$1) => await this.columns(table$1)));
4838
- return flatten(columnsPerTable);
4839
- }
4840
- async columnInfo(table, column) {
4841
- const getColumnsForTable = async (table$1) => {
4842
- const tablesWithAutoIncrementPrimaryKeys = (await this.knex.select("name").from("sqlite_master").whereRaw("sql LIKE '%AUTOINCREMENT%'")).map(({ name }) => name);
4843
- const columns = await this.knex.raw("PRAGMA table_xinfo(??)", table$1);
4844
- const foreignKeys = await this.knex.raw("PRAGMA foreign_key_list(??)", table$1);
4845
- const indexList = await this.knex.raw("PRAGMA index_list(??)", table$1);
4846
- const indexInfoList = await Promise.all(indexList.map((index) => this.knex.raw("PRAGMA index_info(??)", index.name)));
4847
- return columns.map((raw) => {
4848
- const foreignKey = foreignKeys.find((fk) => fk.from === raw.name);
4849
- const indexIndex = indexInfoList.findIndex((list) => list.find((fk) => fk.name === raw.name));
4850
- const index = indexList[indexIndex];
4851
- const indexInfo = indexInfoList[indexIndex];
4852
- return {
4853
- name: raw.name,
4854
- table: table$1,
4855
- data_type: extractType(raw.type),
4856
- default_value: parseDefaultValue(raw.dflt_value),
4857
- max_length: extractMaxLength(raw.type),
4858
- numeric_precision: null,
4859
- numeric_scale: null,
4860
- is_generated: raw.hidden !== 0,
4861
- generation_expression: null,
4862
- is_nullable: raw.notnull === 0,
4863
- is_unique: !!(index === null || index === void 0 ? void 0 : index.unique) && (indexInfo === null || indexInfo === void 0 ? void 0 : indexInfo.length) === 1,
4864
- is_primary_key: raw.pk === 1,
4865
- has_auto_increment: raw.pk === 1 && tablesWithAutoIncrementPrimaryKeys.includes(table$1),
4866
- foreign_key_column: (foreignKey === null || foreignKey === void 0 ? void 0 : foreignKey.to) || null,
4867
- foreign_key_table: (foreignKey === null || foreignKey === void 0 ? void 0 : foreignKey.table) || null
4868
- };
4869
- });
4870
- };
4871
- if (!table) {
4872
- const tables = await this.tables();
4873
- const columnsPerTable = await Promise.all(tables.map(async (table$1) => await getColumnsForTable(table$1)));
4874
- return flatten(columnsPerTable);
4875
- }
4876
- if (table && !column) return await getColumnsForTable(table);
4877
- return (await getColumnsForTable(table)).find((columnInfo) => columnInfo.name === column);
4878
- }
4879
- /**
4880
- * Check if a table exists in the current schema/database
4881
- */
4882
- async hasColumn(table, column) {
4883
- let isColumn = false;
4884
- if ((await this.knex.raw(`SELECT COUNT(*) AS ct FROM pragma_table_xinfo('${table}') WHERE name='${column}'`))[0]["ct"] !== 0) isColumn = true;
4885
- return isColumn;
4886
- }
4887
- /**
4888
- * Get the primary key column for the given table
4889
- */
4890
- async primary(table) {
4891
- const pkColumns = (await this.knex.raw("PRAGMA table_xinfo(??)", table)).filter((col) => col.pk !== 0);
4892
- return pkColumns.length > 0 ? pkColumns.length === 1 ? pkColumns[0].name : pkColumns.map((col) => col.name) : null;
4893
- }
4894
- async foreignKeys(table) {
4895
- if (table) return (await this.knex.raw("PRAGMA foreign_key_list(??)", table)).map((key) => ({
4896
- table,
4897
- column: key.from,
4898
- foreign_key_table: key.table,
4899
- foreign_key_column: key.to,
4900
- on_update: key.on_update,
4901
- on_delete: key.on_delete,
4902
- constraint_name: null
4903
- }));
4904
- const tables = await this.tables();
4905
- const keysPerTable = await Promise.all(tables.map(async (table$1) => await this.foreignKeys(table$1)));
4906
- return flatten(keysPerTable);
4907
- }
4908
- async uniqueConstraints(table) {
4909
- if (table) {
4910
- const indexList = await this.knex.raw("PRAGMA index_list(??)", table);
4911
- const indexInfoList = await Promise.all(indexList.map((index) => this.knex.raw("PRAGMA index_info(??)", index.name)));
4912
- return indexList.filter((i) => i.unique).map((index, i) => {
4913
- const info = indexInfoList[i];
4914
- return {
4915
- table,
4916
- constraint_name: index.name,
4917
- columns: info.map((c) => c.name)
4918
- };
4919
- });
4920
- }
4921
- const tables = await this.tables();
4922
- const constraintsPerTable = await Promise.all(tables.map(async (table$1) => await this.uniqueConstraints(table$1)));
4923
- return flatten(constraintsPerTable);
4924
- }
4925
- };
4926
-
4927
- //#endregion
4928
- //#region src/inspector/index.ts
4929
- var SchemaInspector = class {
4930
- static inspect(knex$1) {
4931
- let constructor;
4932
- switch (knex$1.client.constructor.name) {
4933
- case "Client_MySQL":
4934
- case "Client_MySQL2":
4935
- constructor = MySQL;
4936
- break;
4937
- case "Client_PG":
4938
- constructor = Postgres;
4939
- break;
4940
- case "Client_CockroachDB":
4941
- constructor = CockroachDB;
4942
- break;
4943
- case "Client_SQLite3":
4944
- case "Client_BetterSQLite3":
4945
- constructor = SQLite;
4946
- break;
4947
- case "Client_Oracledb":
4948
- case "Client_Oracle":
4949
- constructor = oracleDB;
4950
- break;
4951
- case "Client_MSSQL":
4952
- constructor = MSSQL;
4953
- break;
4954
- default: throw Error("Unsupported driver used: " + knex$1.client.constructor.name);
4955
- }
4956
- return new constructor(knex$1);
4957
- }
4958
- };
4959
-
4960
- //#endregion
4961
- exports.SchemaInspector = SchemaInspector;