@h3ravel/arquebus 2.0.0 → 3.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bin/index.cjs +580 -451
- package/bin/index.js +566 -437
- package/dist/browser/index.cjs +54 -51
- package/dist/browser/index.d.ts +804 -737
- package/dist/browser/index.js +48 -44
- package/dist/concerns/index.cjs +152 -143
- package/dist/concerns/index.d.ts +849 -776
- package/dist/concerns/index.js +143 -133
- package/dist/index.cjs +186 -185
- package/dist/index.d.ts +856 -782
- package/dist/index.js +179 -177
- package/dist/inspector/index.cjs +175 -174
- package/dist/inspector/index.d.ts +0 -1
- package/dist/inspector/index.js +161 -159
- package/dist/migrations/index.cjs +183 -182
- package/dist/migrations/index.d.ts +799 -727
- package/dist/migrations/index.js +172 -170
- package/dist/relations/index.cjs +152 -143
- package/dist/relations/index.d.ts +835 -762
- package/dist/relations/index.js +143 -133
- package/dist/seeders/index.cjs +7 -5
- package/dist/seeders/index.d.ts +797 -724
- package/dist/seeders/index.js +8 -6
- package/dist/{migrations/stubs/stubs → stubs}/migration-js.stub +1 -0
- package/dist/{migrations/stubs/stubs → stubs}/migration-ts.stub +1 -0
- package/dist/{migrations/stubs/stubs → stubs}/migration.create-js.stub +1 -0
- package/dist/{migrations/stubs/stubs → stubs}/migration.create-ts.stub +1 -0
- package/dist/{migrations/stubs/stubs → stubs}/migration.update-js.stub +1 -0
- package/dist/{migrations/stubs/stubs → stubs}/migration.update-ts.stub +1 -0
- package/dist/stubs/model-ts.stub +9 -0
- package/package.json +31 -31
- package/types/builder.ts +1 -1
- package/types/database.ts +69 -0
- package/types/index.ts +3 -0
- package/types/model-builder.ts +132 -0
- package/types/query-builder.ts +0 -1
- package/types/query-methods.ts +0 -1
- package/types/schema.ts +92 -0
- package/types/utils.ts +1 -40
- package/dist/stubs/stubs/model-ts.stub +0 -5
- /package/dist/stubs/{stubs/arquebus.config-js.stub → arquebus.config-js.stub} +0 -0
- /package/dist/stubs/{stubs/arquebus.config-ts.stub → arquebus.config-ts.stub} +0 -0
- /package/dist/stubs/{stubs/model-js.stub → model-js.stub} +0 -0
- /package/dist/stubs/{stubs/seeder-js.stub → seeder-js.stub} +0 -0
- /package/dist/stubs/{stubs/seeder-ts.stub → seeder-ts.stub} +0 -0
package/dist/migrations/index.js
CHANGED
|
@@ -1,15 +1,16 @@
|
|
|
1
1
|
import { t as __exportAll } from "./rolldown-runtime-D7D4PA-g.js";
|
|
2
|
-
import { FileSystem, Logger, TaskManager } from "@h3ravel/shared";
|
|
2
|
+
import { FileSystem, Logger, TaskManager, importFile } from "@h3ravel/shared";
|
|
3
3
|
import fs, { copyFile, mkdir, readFile, readdir, writeFile } from "node:fs/promises";
|
|
4
4
|
import path from "path";
|
|
5
5
|
import path$1, { dirname } from "node:path";
|
|
6
6
|
import { fileURLToPath } from "node:url";
|
|
7
|
-
import {
|
|
8
|
-
import
|
|
9
|
-
import
|
|
10
|
-
import
|
|
7
|
+
import { existsSync } from "node:fs";
|
|
8
|
+
import { Arr, Obj, Str, data_get, data_set } from "@h3ravel/support";
|
|
9
|
+
import * as dayjsModule from "dayjs";
|
|
10
|
+
import * as advancedFormatModule from "dayjs/plugin/advancedFormat.js";
|
|
11
|
+
import { Collection, collect } from "@h3ravel/collect.js";
|
|
11
12
|
import Knex$1 from "knex";
|
|
12
|
-
import { existsSync } from "fs";
|
|
13
|
+
import { existsSync as existsSync$1 } from "fs";
|
|
13
14
|
import pluralize from "pluralize";
|
|
14
15
|
import resolveFrom from "resolve-from";
|
|
15
16
|
//#region src/migrations/migration-repository.ts
|
|
@@ -157,8 +158,7 @@ var CockroachDB = class {
|
|
|
157
158
|
*/
|
|
158
159
|
async hasTable(table) {
|
|
159
160
|
const subquery = this.knex.select().from("information_schema.tables").whereIn("table_schema", this.explodedSchema).andWhere({ table_name: table });
|
|
160
|
-
|
|
161
|
-
return (record === null || record === void 0 ? void 0 : record.exists) || false;
|
|
161
|
+
return (await this.knex.select(this.knex.raw("exists (?)", [subquery])).first())?.exists || false;
|
|
162
162
|
}
|
|
163
163
|
/**
|
|
164
164
|
* Get all the available columns in the current schema/database. Can be filtered to a specific table
|
|
@@ -258,18 +258,17 @@ var CockroachDB = class {
|
|
|
258
258
|
${column ? "AND att.attname = ?" : ""}
|
|
259
259
|
`, bindings)]);
|
|
260
260
|
const parsedColumms = columns.rows.map((col) => {
|
|
261
|
-
var _col$default_value;
|
|
262
261
|
const constraintsForColumn = constraints.rows.filter((constraint) => constraint.table === col.table && constraint.column === col.name);
|
|
263
262
|
const foreignKeyConstraint = constraintsForColumn.find((constraint) => constraint.type === "f");
|
|
264
263
|
return {
|
|
265
264
|
...col,
|
|
266
265
|
is_unique: constraintsForColumn.some((constraint) => ["u", "p"].includes(constraint.type)),
|
|
267
266
|
is_primary_key: constraintsForColumn.some((constraint) => constraint.type === "p"),
|
|
268
|
-
has_auto_increment: ["integer", "bigint"].includes(col.data_type) && (
|
|
267
|
+
has_auto_increment: ["integer", "bigint"].includes(col.data_type) && (col.default_value?.startsWith("nextval(") ?? false),
|
|
269
268
|
default_value: parseDefaultValue$5(col.default_value),
|
|
270
|
-
foreign_key_schema:
|
|
271
|
-
foreign_key_table:
|
|
272
|
-
foreign_key_column:
|
|
269
|
+
foreign_key_schema: foreignKeyConstraint?.foreign_key_schema ?? null,
|
|
270
|
+
foreign_key_table: foreignKeyConstraint?.foreign_key_table ?? null,
|
|
271
|
+
foreign_key_column: foreignKeyConstraint?.foreign_key_column ?? null
|
|
273
272
|
};
|
|
274
273
|
});
|
|
275
274
|
if (table && column) return parsedColumms[0];
|
|
@@ -283,8 +282,7 @@ var CockroachDB = class {
|
|
|
283
282
|
table_name: table,
|
|
284
283
|
column_name: column
|
|
285
284
|
});
|
|
286
|
-
|
|
287
|
-
return (record === null || record === void 0 ? void 0 : record.exists) || false;
|
|
285
|
+
return (await this.knex.select(this.knex.raw("exists (?)", [subquery])).first())?.exists || false;
|
|
288
286
|
}
|
|
289
287
|
/**
|
|
290
288
|
* Get the primary key column for the given table
|
|
@@ -596,14 +594,13 @@ var MSSQL = class {
|
|
|
596
594
|
WHERE
|
|
597
595
|
OBJECT_SCHEMA_NAME (f.parent_object_id) = ?;
|
|
598
596
|
`, [this.schema]);
|
|
599
|
-
if (table) return result
|
|
597
|
+
if (table) return result?.filter((row) => row.table === table);
|
|
600
598
|
return result;
|
|
601
599
|
}
|
|
602
600
|
};
|
|
603
601
|
//#endregion
|
|
604
602
|
//#region src/inspector/dialects/mysql.ts
|
|
605
603
|
function rawColumnToColumn$1(rawColumn) {
|
|
606
|
-
var _rawColumn$EXTRA;
|
|
607
604
|
let dataType = rawColumn.COLUMN_TYPE.replace(/\(.*?\)/, "");
|
|
608
605
|
if (rawColumn.COLUMN_TYPE.startsWith("tinyint(1)")) dataType = "boolean";
|
|
609
606
|
return {
|
|
@@ -616,7 +613,7 @@ function rawColumnToColumn$1(rawColumn) {
|
|
|
616
613
|
max_length: rawColumn.CHARACTER_MAXIMUM_LENGTH,
|
|
617
614
|
numeric_precision: rawColumn.NUMERIC_PRECISION,
|
|
618
615
|
numeric_scale: rawColumn.NUMERIC_SCALE,
|
|
619
|
-
is_generated: !!
|
|
616
|
+
is_generated: !!rawColumn.EXTRA?.endsWith("GENERATED"),
|
|
620
617
|
is_nullable: rawColumn.IS_NULLABLE === "YES",
|
|
621
618
|
is_unique: rawColumn.COLUMN_KEY === "UNI",
|
|
622
619
|
is_primary_key: rawColumn.CONSTRAINT_NAME === "PRIMARY" || rawColumn.COLUMN_KEY === "PRI",
|
|
@@ -809,11 +806,10 @@ var oracleDB = class {
|
|
|
809
806
|
* Check if a table exists in the current schema/database
|
|
810
807
|
*/
|
|
811
808
|
async hasTable(table) {
|
|
812
|
-
|
|
809
|
+
return !!(await this.knex.select(this.knex.raw(`
|
|
813
810
|
/*+ OPTIMIZER_FEATURES_ENABLE('${OPTIMIZER_FEATURES}') */
|
|
814
811
|
COUNT(*) "count"
|
|
815
|
-
`)).from("USER_TABLES").where({ TABLE_NAME: table }).first();
|
|
816
|
-
return !!(result === null || result === void 0 ? void 0 : result.count);
|
|
812
|
+
`)).from("USER_TABLES").where({ TABLE_NAME: table }).first())?.count;
|
|
817
813
|
}
|
|
818
814
|
/**
|
|
819
815
|
* Get all the available columns in the current schema/database. Can be filtered to a specific table
|
|
@@ -889,15 +885,14 @@ var oracleDB = class {
|
|
|
889
885
|
* Check if a table exists in the current schema/database
|
|
890
886
|
*/
|
|
891
887
|
async hasColumn(table, column) {
|
|
892
|
-
|
|
888
|
+
return !!(await this.knex.select(this.knex.raw(`
|
|
893
889
|
/*+ OPTIMIZER_FEATURES_ENABLE('${OPTIMIZER_FEATURES}') NO_QUERY_TRANSFORMATION */
|
|
894
890
|
COUNT(*) "count"
|
|
895
891
|
`)).from("USER_TAB_COLS").where({
|
|
896
892
|
TABLE_NAME: table,
|
|
897
893
|
COLUMN_NAME: column,
|
|
898
894
|
HIDDEN_COLUMN: "NO"
|
|
899
|
-
}).first();
|
|
900
|
-
return !!(result === null || result === void 0 ? void 0 : result.count);
|
|
895
|
+
}).first())?.count;
|
|
901
896
|
}
|
|
902
897
|
/**
|
|
903
898
|
* Get the primary key column for the given table
|
|
@@ -1063,13 +1058,12 @@ var Postgres = class {
|
|
|
1063
1058
|
`, bindings)).rows;
|
|
1064
1059
|
}
|
|
1065
1060
|
async columnInfo(table, column) {
|
|
1066
|
-
var _versionResponse$rows;
|
|
1067
1061
|
const { knex } = this;
|
|
1068
1062
|
const bindings = [];
|
|
1069
1063
|
if (table) bindings.push(table);
|
|
1070
1064
|
if (column) bindings.push(column);
|
|
1071
1065
|
const schemaIn = this.explodedSchema.map((schemaName) => `${this.knex.raw("?", [schemaName])}::regnamespace`);
|
|
1072
|
-
const majorVersion = (
|
|
1066
|
+
const majorVersion = (await this.knex.raw("SHOW server_version")).rows?.[0]?.server_version?.split(".")?.[0] ?? 10;
|
|
1073
1067
|
let generationSelect = `
|
|
1074
1068
|
NULL AS generation_expression,
|
|
1075
1069
|
pg_get_expr(ad.adbin, ad.adrelid) AS default_value,
|
|
@@ -1161,9 +1155,9 @@ var Postgres = class {
|
|
|
1161
1155
|
is_primary_key: constraintsForColumn.some((constraint) => constraint.type === "p"),
|
|
1162
1156
|
has_auto_increment: constraintsForColumn.some((constraint) => constraint.has_auto_increment),
|
|
1163
1157
|
default_value: parseDefaultValue$1(col.default_value),
|
|
1164
|
-
foreign_key_schema:
|
|
1165
|
-
foreign_key_table:
|
|
1166
|
-
foreign_key_column:
|
|
1158
|
+
foreign_key_schema: foreignKeyConstraint?.foreign_key_schema ?? null,
|
|
1159
|
+
foreign_key_table: foreignKeyConstraint?.foreign_key_table ?? null,
|
|
1160
|
+
foreign_key_column: foreignKeyConstraint?.foreign_key_column ?? null
|
|
1167
1161
|
};
|
|
1168
1162
|
});
|
|
1169
1163
|
if (table && column) return parsedColumms[0];
|
|
@@ -1311,7 +1305,7 @@ function extractType(type) {
|
|
|
1311
1305
|
return type.replace(/[^a-zA-Z]/g, "").toLowerCase();
|
|
1312
1306
|
}
|
|
1313
1307
|
//#endregion
|
|
1314
|
-
//#region node_modules/.pnpm/tsdown@0.22.
|
|
1308
|
+
//#region node_modules/.pnpm/tsdown@0.22.5_tsx@4.23.0_typescript@6.0.3/node_modules/tsdown/esm-shims.js
|
|
1315
1309
|
const getFilename = () => fileURLToPath(import.meta.url);
|
|
1316
1310
|
const getDirname = () => path$1.dirname(getFilename());
|
|
1317
1311
|
const __dirname = /* @__PURE__ */ getDirname();
|
|
@@ -1365,7 +1359,7 @@ var ModelNotFoundError = class extends BaseError {
|
|
|
1365
1359
|
}
|
|
1366
1360
|
setModel(model, ids = []) {
|
|
1367
1361
|
this.model = model;
|
|
1368
|
-
this.ids = isArray(ids) ? ids : [ids];
|
|
1362
|
+
this.ids = Array.isArray(ids) ? ids : [ids];
|
|
1369
1363
|
this.message = `No query results for model [${model}]`;
|
|
1370
1364
|
if (this.ids.length > 0) this.message += " " + this.ids.join(", ");
|
|
1371
1365
|
else this.message += ".";
|
|
@@ -1456,25 +1450,29 @@ function compose$1(Base, ...mixins) {
|
|
|
1456
1450
|
}, Base);
|
|
1457
1451
|
}
|
|
1458
1452
|
//#endregion
|
|
1459
|
-
//#region src/
|
|
1453
|
+
//#region src/dayjs.ts
|
|
1454
|
+
const dayjs = dayjsModule.default ?? dayjsModule;
|
|
1455
|
+
const advancedFormat = advancedFormatModule.default ?? advancedFormatModule;
|
|
1460
1456
|
dayjs.extend(advancedFormat);
|
|
1457
|
+
//#endregion
|
|
1458
|
+
//#region src/utils.ts
|
|
1461
1459
|
const getRelationName = (relationMethod) => {
|
|
1462
|
-
return snake(relationMethod.substring(8));
|
|
1460
|
+
return Str.snake(relationMethod.substring(8));
|
|
1463
1461
|
};
|
|
1464
1462
|
const getRelationMethod = (relation) => {
|
|
1465
|
-
return camel(`relation_${relation}`);
|
|
1463
|
+
return Str.camel(`relation_${relation}`);
|
|
1466
1464
|
};
|
|
1467
1465
|
const getScopeMethod = (scope) => {
|
|
1468
|
-
return camel(`scope_${scope}`);
|
|
1466
|
+
return Str.camel(`scope_${scope}`);
|
|
1469
1467
|
};
|
|
1470
1468
|
const getAttrMethod = (attr) => {
|
|
1471
|
-
return camel(`attribute_${attr}`);
|
|
1469
|
+
return Str.camel(`attribute_${attr}`);
|
|
1472
1470
|
};
|
|
1473
1471
|
const getGetterMethod = (attr) => {
|
|
1474
|
-
return camel(`get_${attr}_attribute`);
|
|
1472
|
+
return Str.camel(`get_${attr}_attribute`);
|
|
1475
1473
|
};
|
|
1476
1474
|
const getSetterMethod = (attr) => {
|
|
1477
|
-
return camel(`set_${attr}_attribute`);
|
|
1475
|
+
return Str.camel(`set_${attr}_attribute`);
|
|
1478
1476
|
};
|
|
1479
1477
|
/**
|
|
1480
1478
|
* Tap into a model a collection instance
|
|
@@ -1490,7 +1488,7 @@ const tap = (instance, callback) => {
|
|
|
1490
1488
|
const { compose } = mixin_exports;
|
|
1491
1489
|
const flatten = (arr) => arr.flat();
|
|
1492
1490
|
const flattenDeep = (arr) => Array.isArray(arr) ? arr.reduce((a, b) => a.concat(flattenDeep(b)), []) : [arr];
|
|
1493
|
-
const snakeCase = (str) => trim(snake(str.replace(/[^a-zA-Z0-9_-]/g, "-")), "_-");
|
|
1491
|
+
const snakeCase = (str) => Str.trim(Str.snake(str.replace(/[^a-zA-Z0-9_-]/g, "-")), "_-").replace(/_+/g, "_");
|
|
1494
1492
|
//#endregion
|
|
1495
1493
|
//#region src/relations/relation.ts
|
|
1496
1494
|
var Relation = class {
|
|
@@ -1577,8 +1575,7 @@ var Relation = class {
|
|
|
1577
1575
|
return this.parent.getQualifiedKeyName();
|
|
1578
1576
|
}
|
|
1579
1577
|
getExistenceCompareKey() {
|
|
1580
|
-
|
|
1581
|
-
return (_this$getQualifiedFor = this.getQualifiedForeignKeyName) === null || _this$getQualifiedFor === void 0 ? void 0 : _this$getQualifiedFor.call(this);
|
|
1578
|
+
return this.getQualifiedForeignKeyName?.();
|
|
1582
1579
|
}
|
|
1583
1580
|
};
|
|
1584
1581
|
//#endregion
|
|
@@ -1719,8 +1716,7 @@ const HasAttributes = (Model) => {
|
|
|
1719
1716
|
return this;
|
|
1720
1717
|
}
|
|
1721
1718
|
normalizeCastClassResponse(key, value) {
|
|
1722
|
-
|
|
1723
|
-
return (value === null || value === void 0 || (_value$constructor = value.constructor) === null || _value$constructor === void 0 ? void 0 : _value$constructor.name) === "Object" ? value : { [key]: value };
|
|
1719
|
+
return value?.constructor?.name === "Object" ? value : { [key]: value };
|
|
1724
1720
|
}
|
|
1725
1721
|
syncOriginal() {
|
|
1726
1722
|
this.original = this.getAttributes();
|
|
@@ -1845,8 +1841,8 @@ const HasAttributes = (Model) => {
|
|
|
1845
1841
|
attributesToData() {
|
|
1846
1842
|
let attributes = { ...this.attributes };
|
|
1847
1843
|
for (const key in attributes) {
|
|
1848
|
-
if (this.hidden.includes(key)) attributes =
|
|
1849
|
-
if (this.visible.length > 0 && this.visible.includes(key) === false) attributes =
|
|
1844
|
+
if (this.hidden.includes(key)) attributes = Arr.except(attributes, [key]);
|
|
1845
|
+
if (this.visible.length > 0 && this.visible.includes(key) === false) attributes = Arr.except(attributes, [key]);
|
|
1850
1846
|
}
|
|
1851
1847
|
for (const key of this.getDates()) {
|
|
1852
1848
|
if (attributes[key] === void 0) continue;
|
|
@@ -1901,7 +1897,7 @@ const HasAttributes = (Model) => {
|
|
|
1901
1897
|
}
|
|
1902
1898
|
hasCast(key, types = []) {
|
|
1903
1899
|
if (key in this.casts) {
|
|
1904
|
-
types =
|
|
1900
|
+
types = flatten(types);
|
|
1905
1901
|
return types.length > 0 ? types.includes(this.getCastType(key)) : true;
|
|
1906
1902
|
}
|
|
1907
1903
|
return false;
|
|
@@ -1960,10 +1956,14 @@ const HasGlobalScopes = (Model) => {
|
|
|
1960
1956
|
static globalScopes;
|
|
1961
1957
|
static addGlobalScope(scope, implementation = null) {
|
|
1962
1958
|
if (typeof scope === "string" && implementation instanceof Scope) {
|
|
1963
|
-
|
|
1959
|
+
const scopes = this.globalScopes ?? {};
|
|
1960
|
+
data_set(scopes, this.name + "." + scope, implementation);
|
|
1961
|
+
this.globalScopes = scopes;
|
|
1964
1962
|
return implementation;
|
|
1965
1963
|
} else if (scope instanceof Scope) {
|
|
1966
|
-
|
|
1964
|
+
const scopes = this.globalScopes ?? {};
|
|
1965
|
+
data_set(scopes, this.name + "." + scope.constructor.name, scope);
|
|
1966
|
+
this.globalScopes = scopes;
|
|
1967
1967
|
return scope;
|
|
1968
1968
|
}
|
|
1969
1969
|
throw new InvalidArgumentError("Global scope must be an instance of Scope.");
|
|
@@ -1972,8 +1972,8 @@ const HasGlobalScopes = (Model) => {
|
|
|
1972
1972
|
return this.getGlobalScope(scope) !== null;
|
|
1973
1973
|
}
|
|
1974
1974
|
static getGlobalScope(scope) {
|
|
1975
|
-
if (typeof scope === "string") return
|
|
1976
|
-
return
|
|
1975
|
+
if (typeof scope === "string") return data_get(this.globalScopes ?? {}, this.name + "." + scope);
|
|
1976
|
+
return data_get(this.globalScopes ?? {}, this.name + "." + scope.constructor.name);
|
|
1977
1977
|
}
|
|
1978
1978
|
static getAllGlobalScopes() {
|
|
1979
1979
|
return this.globalScopes;
|
|
@@ -1982,7 +1982,7 @@ const HasGlobalScopes = (Model) => {
|
|
|
1982
1982
|
this.globalScopes = scopes;
|
|
1983
1983
|
}
|
|
1984
1984
|
getGlobalScopes() {
|
|
1985
|
-
return
|
|
1985
|
+
return data_get(this.constructor.globalScopes ?? {}, this.constructor.name, {});
|
|
1986
1986
|
}
|
|
1987
1987
|
};
|
|
1988
1988
|
};
|
|
@@ -2153,13 +2153,11 @@ var HasMany = class extends compose(Relation, HasOneOrMany) {
|
|
|
2153
2153
|
return this.getParentKey() !== null ? await this.query.get() : new Collection$1([]);
|
|
2154
2154
|
}
|
|
2155
2155
|
getForeignKeyName() {
|
|
2156
|
-
|
|
2157
|
-
const segments = (_this$foreignKey = this.foreignKey) === null || _this$foreignKey === void 0 ? void 0 : _this$foreignKey.split(".");
|
|
2158
|
-
return segments === null || segments === void 0 ? void 0 : segments.pop();
|
|
2156
|
+
return (this.foreignKey?.split("."))?.pop();
|
|
2159
2157
|
}
|
|
2160
2158
|
buildDictionary(results) {
|
|
2161
2159
|
const foreign = this.getForeignKeyName();
|
|
2162
|
-
return collect
|
|
2160
|
+
return collect(results).mapToDictionary((result) => [result[foreign], result]).all();
|
|
2163
2161
|
}
|
|
2164
2162
|
match(models, results, relation) {
|
|
2165
2163
|
return this.matchOneOrMany(models, results, relation, "many");
|
|
@@ -2190,9 +2188,7 @@ var HasOne = class extends compose(Relation, HasOneOrMany, SupportsDefaultModels
|
|
|
2190
2188
|
return this.matchOneOrMany(models, results, relation, "one");
|
|
2191
2189
|
}
|
|
2192
2190
|
getForeignKeyName() {
|
|
2193
|
-
|
|
2194
|
-
const segments = (_this$foreignKey = this.foreignKey) === null || _this$foreignKey === void 0 ? void 0 : _this$foreignKey.split(".");
|
|
2195
|
-
return segments === null || segments === void 0 ? void 0 : segments.pop();
|
|
2191
|
+
return (this.foreignKey?.split("."))?.pop();
|
|
2196
2192
|
}
|
|
2197
2193
|
async getResults() {
|
|
2198
2194
|
if (this.getParentKey() === null) return this.getDefaultFor(this.parent);
|
|
@@ -2301,10 +2297,10 @@ var HasManyThrough = class extends Relation {
|
|
|
2301
2297
|
}
|
|
2302
2298
|
const model = await this.first(columns);
|
|
2303
2299
|
if (model) return model;
|
|
2304
|
-
return callback
|
|
2300
|
+
return callback?.();
|
|
2305
2301
|
}
|
|
2306
2302
|
async find(id, columns = ["*"]) {
|
|
2307
|
-
if (isArray(id)) return await this.findMany(id, columns);
|
|
2303
|
+
if (Array.isArray(id)) return await this.findMany(id, columns);
|
|
2308
2304
|
return await this.where(this.getRelated().getQualifiedKeyName(), "=", id).first(columns);
|
|
2309
2305
|
}
|
|
2310
2306
|
async findMany(ids, columns = ["*"]) {
|
|
@@ -2332,7 +2328,7 @@ var HasManyThrough = class extends Relation {
|
|
|
2332
2328
|
return await this.query.paginate(perPage ?? 15, columns, pageName, page);
|
|
2333
2329
|
}
|
|
2334
2330
|
shouldSelect(columns = ["*"]) {
|
|
2335
|
-
if (
|
|
2331
|
+
if (columns?.at(0) == "*") columns = [this.related.getTable() + ".*"];
|
|
2336
2332
|
return [...columns, this.getQualifiedFirstKeyName() + " as laravel_through_key"];
|
|
2337
2333
|
}
|
|
2338
2334
|
async chunk(count, callback) {
|
|
@@ -2426,7 +2422,7 @@ const HasRelations = (Model) => {
|
|
|
2426
2422
|
return this;
|
|
2427
2423
|
}
|
|
2428
2424
|
unsetRelation(relation) {
|
|
2429
|
-
this.relations =
|
|
2425
|
+
this.relations = Arr.except(this.relations, [relation]);
|
|
2430
2426
|
return this;
|
|
2431
2427
|
}
|
|
2432
2428
|
relationLoaded(relation) {
|
|
@@ -2449,8 +2445,7 @@ const HasRelations = (Model) => {
|
|
|
2449
2445
|
return data;
|
|
2450
2446
|
}
|
|
2451
2447
|
guessBelongsToRelation() {
|
|
2452
|
-
|
|
2453
|
-
const functionName = (((_e$stack = (/* @__PURE__ */ new Error()).stack) === null || _e$stack === void 0 ? void 0 : _e$stack.split("\n")[2]) ?? "").split(" ")[5];
|
|
2448
|
+
const functionName = ((/* @__PURE__ */ new Error()).stack?.split("\n")[2] ?? "").split(" ")[5];
|
|
2454
2449
|
return getRelationName(functionName);
|
|
2455
2450
|
}
|
|
2456
2451
|
joiningTable(related, instance = null) {
|
|
@@ -2560,7 +2555,7 @@ const HidesAttributes = (Model) => {
|
|
|
2560
2555
|
makeVisible(...keys) {
|
|
2561
2556
|
const visible = flattenDeep(keys);
|
|
2562
2557
|
if (this.visible.length > 0) this.visible = [...this.visible, ...visible];
|
|
2563
|
-
this.hidden =
|
|
2558
|
+
this.hidden = collect(this.hidden).diff(visible).all();
|
|
2564
2559
|
return this;
|
|
2565
2560
|
}
|
|
2566
2561
|
makeHidden(key, ...keys) {
|
|
@@ -2704,10 +2699,9 @@ var QueryBuilder = class QueryBuilder extends Inference$2 {
|
|
|
2704
2699
|
asProxy() {
|
|
2705
2700
|
return new Proxy(this, {
|
|
2706
2701
|
get: function(target, prop) {
|
|
2707
|
-
var _target$connector$cli;
|
|
2708
2702
|
if (typeof target[prop] !== "undefined") return target[prop];
|
|
2709
2703
|
if (["destroy", "schema"].includes(prop)) return target.connector.schema;
|
|
2710
|
-
const skipReturning = !!
|
|
2704
|
+
const skipReturning = !!target.connector.client.config?.client?.includes("mysql") && prop === "returning";
|
|
2711
2705
|
if ([
|
|
2712
2706
|
"select",
|
|
2713
2707
|
"from",
|
|
@@ -2910,6 +2904,9 @@ var arquebus = class arquebus {
|
|
|
2910
2904
|
if (this.instance === null) this.instance = new arquebus();
|
|
2911
2905
|
return this.instance;
|
|
2912
2906
|
}
|
|
2907
|
+
static withSchema() {
|
|
2908
|
+
return this;
|
|
2909
|
+
}
|
|
2913
2910
|
/**
|
|
2914
2911
|
* Initialize a new database connection
|
|
2915
2912
|
*
|
|
@@ -2998,13 +2995,13 @@ var arquebus = class arquebus {
|
|
|
2998
2995
|
const jsPath = path.resolve("arquebus.config.js");
|
|
2999
2996
|
const tsPath = path.resolve("arquebus.config.ts");
|
|
3000
2997
|
const instance = this.getInstance();
|
|
3001
|
-
if (existsSync(jsPath)) {
|
|
3002
|
-
config = (await
|
|
2998
|
+
if (existsSync$1(jsPath)) {
|
|
2999
|
+
config = (await importFile(jsPath)).default;
|
|
3003
3000
|
if (addConnection) instance.addConnection(config, config.client);
|
|
3004
3001
|
return config;
|
|
3005
3002
|
}
|
|
3006
|
-
if (existsSync(tsPath)) if (process.env.NODE_ENV !== "production") {
|
|
3007
|
-
config = (await
|
|
3003
|
+
if (existsSync$1(tsPath)) if (process.env.NODE_ENV !== "production") {
|
|
3004
|
+
config = (await importFile(tsPath)).default;
|
|
3008
3005
|
if (addConnection) instance.addConnection(config, config.client);
|
|
3009
3006
|
return config;
|
|
3010
3007
|
} else throw new Error("arquebus.config.ts found in production without build step");
|
|
@@ -3020,7 +3017,7 @@ var arquebus = class arquebus {
|
|
|
3020
3017
|
"cjs"
|
|
3021
3018
|
], dir);
|
|
3022
3019
|
if (found) if (!found.endsWith(".ts") || process.env.NODE_ENV !== "production") {
|
|
3023
|
-
config = (await
|
|
3020
|
+
config = (await importFile(found)).default;
|
|
3024
3021
|
if (addConnection) instance.addConnection(config, config.client);
|
|
3025
3022
|
return config;
|
|
3026
3023
|
} else throw new Error("arquebus.config.ts found in production without build step");
|
|
@@ -3041,7 +3038,7 @@ var arquebus = class arquebus {
|
|
|
3041
3038
|
}
|
|
3042
3039
|
async destroyAll() {
|
|
3043
3040
|
await Promise.all(Object.values(this.manager).map((connection) => {
|
|
3044
|
-
return connection
|
|
3041
|
+
return connection?.destroy();
|
|
3045
3042
|
}));
|
|
3046
3043
|
}
|
|
3047
3044
|
createModel(name, options = {}) {
|
|
@@ -3050,29 +3047,25 @@ var arquebus = class arquebus {
|
|
|
3050
3047
|
this.models = {
|
|
3051
3048
|
...this.models,
|
|
3052
3049
|
[name]: class extends BaseModel {
|
|
3053
|
-
table =
|
|
3054
|
-
connection =
|
|
3055
|
-
timestamps =
|
|
3056
|
-
primaryKey =
|
|
3057
|
-
keyType =
|
|
3058
|
-
incrementing =
|
|
3059
|
-
with =
|
|
3060
|
-
casts =
|
|
3061
|
-
static CREATED_AT =
|
|
3062
|
-
static UPDATED_AT =
|
|
3063
|
-
static DELETED_AT =
|
|
3050
|
+
table = options?.table ?? null;
|
|
3051
|
+
connection = options?.connection ?? null;
|
|
3052
|
+
timestamps = options?.timestamps ?? true;
|
|
3053
|
+
primaryKey = options?.primaryKey ?? "id";
|
|
3054
|
+
keyType = options?.keyType ?? "int";
|
|
3055
|
+
incrementing = options?.incrementing ?? true;
|
|
3056
|
+
with = options?.with ?? [];
|
|
3057
|
+
casts = options?.casts ?? {};
|
|
3058
|
+
static CREATED_AT = options?.CREATED_AT ?? "created_at";
|
|
3059
|
+
static UPDATED_AT = options?.UPDATED_AT ?? "updated_at";
|
|
3060
|
+
static DELETED_AT = options?.DELETED_AT ?? "deleted_at";
|
|
3064
3061
|
}
|
|
3065
3062
|
};
|
|
3066
3063
|
if ("attributes" in options) for (const attribute in options.attributes) {
|
|
3067
3064
|
if (options.attributes[attribute] instanceof Attribute === false) throw new Error("Attribute must be an instance of \"Attribute\"");
|
|
3068
|
-
this.models[name].prototype[getAttrMethod(attribute)] = () =>
|
|
3069
|
-
var _options$attributes;
|
|
3070
|
-
return (_options$attributes = options.attributes) === null || _options$attributes === void 0 ? void 0 : _options$attributes[attribute];
|
|
3071
|
-
};
|
|
3065
|
+
this.models[name].prototype[getAttrMethod(attribute)] = () => options.attributes?.[attribute];
|
|
3072
3066
|
}
|
|
3073
3067
|
if ("relations" in options) for (const relation in options.relations) this.models[name].prototype[getRelationMethod(relation)] = function() {
|
|
3074
|
-
|
|
3075
|
-
return (_options$relations = options.relations) === null || _options$relations === void 0 ? void 0 : _options$relations[relation](this);
|
|
3068
|
+
return options.relations?.[relation](this);
|
|
3076
3069
|
};
|
|
3077
3070
|
if ("scopes" in options) for (const scope in options.scopes) this.models[name].prototype[getScopeMethod(scope)] = options.scopes[scope];
|
|
3078
3071
|
this.models[name].setConnectionResolver(this);
|
|
@@ -3110,6 +3103,9 @@ var Model = class Model extends BaseModel {
|
|
|
3110
3103
|
this.buildRelationships(attributes);
|
|
3111
3104
|
return this.asProxy();
|
|
3112
3105
|
}
|
|
3106
|
+
static define() {
|
|
3107
|
+
return this;
|
|
3108
|
+
}
|
|
3113
3109
|
static query(trx = null) {
|
|
3114
3110
|
return new this().newQuery(trx);
|
|
3115
3111
|
}
|
|
@@ -3293,7 +3289,7 @@ var Model = class Model extends BaseModel {
|
|
|
3293
3289
|
});
|
|
3294
3290
|
}
|
|
3295
3291
|
toData() {
|
|
3296
|
-
return
|
|
3292
|
+
return Obj.deepMerge(this.attributesToData(), this.relationsToData());
|
|
3297
3293
|
}
|
|
3298
3294
|
toJSON() {
|
|
3299
3295
|
return this.toData();
|
|
@@ -3304,6 +3300,15 @@ var Model = class Model extends BaseModel {
|
|
|
3304
3300
|
toString() {
|
|
3305
3301
|
return this.toJson();
|
|
3306
3302
|
}
|
|
3303
|
+
getAttributes() {
|
|
3304
|
+
return super.getAttributes();
|
|
3305
|
+
}
|
|
3306
|
+
getAttribute(key) {
|
|
3307
|
+
return super.getAttribute(key);
|
|
3308
|
+
}
|
|
3309
|
+
setAttribute(key, value) {
|
|
3310
|
+
return super.setAttribute(key, value);
|
|
3311
|
+
}
|
|
3307
3312
|
fill(attributes) {
|
|
3308
3313
|
for (const key in attributes) this.setAttribute(key, attributes[key]);
|
|
3309
3314
|
return this;
|
|
@@ -3344,10 +3349,9 @@ var Model = class Model extends BaseModel {
|
|
|
3344
3349
|
if (this.usesTimestamps()) this.updateTimestamps();
|
|
3345
3350
|
const attributes = this.getAttributes();
|
|
3346
3351
|
if (this.getIncrementing()) {
|
|
3347
|
-
var _data$;
|
|
3348
3352
|
const keyName = this.getKeyName();
|
|
3349
3353
|
const data = await query.insert([attributes], [keyName]);
|
|
3350
|
-
this.setAttribute(keyName,
|
|
3354
|
+
this.setAttribute(keyName, data[0]?.[keyName] || data[0]);
|
|
3351
3355
|
} else if (Object.keys(attributes).length > 0) await query.insert(attributes);
|
|
3352
3356
|
this.exists = true;
|
|
3353
3357
|
await this.execHooks("created", options);
|
|
@@ -3453,7 +3457,7 @@ var Pivot = class extends Model {
|
|
|
3453
3457
|
};
|
|
3454
3458
|
//#endregion
|
|
3455
3459
|
//#region src/collection.ts
|
|
3456
|
-
var Collection$1 = class
|
|
3460
|
+
var Collection$1 = class extends Collection {
|
|
3457
3461
|
newConstructor(...args) {
|
|
3458
3462
|
return new (this.getConstructor())(...args);
|
|
3459
3463
|
}
|
|
@@ -3462,17 +3466,18 @@ var Collection$1 = class Collection$1 extends Collection {
|
|
|
3462
3466
|
}
|
|
3463
3467
|
async load(...relations) {
|
|
3464
3468
|
if (this.isNotEmpty()) {
|
|
3465
|
-
const items = await this.first().constructor.query().with(...relations).eagerLoadRelations(this.
|
|
3469
|
+
const items = await this.first().constructor.query().with(...relations).eagerLoadRelations(this.all());
|
|
3466
3470
|
return this.newConstructor(items);
|
|
3467
3471
|
}
|
|
3468
3472
|
return this;
|
|
3469
3473
|
}
|
|
3470
3474
|
async loadAggregate(relations, column, action = null) {
|
|
3471
3475
|
if (this.isEmpty()) return this;
|
|
3472
|
-
const
|
|
3473
|
-
const
|
|
3476
|
+
const first = this.first();
|
|
3477
|
+
const models = (await first.newModelQuery().whereIn(first.getKeyName(), this.modelKeys()).select(first.getKeyName()).withAggregate(relations, column, action).get()).keyBy(first.getKeyName());
|
|
3478
|
+
const attributes = collect(Object.keys(models.first().getAttributes())).diff([models.first().getKeyName()]).all();
|
|
3474
3479
|
this.each((model) => {
|
|
3475
|
-
const extraAttributes =
|
|
3480
|
+
const extraAttributes = Arr.select(models.get(model.getKey()).getAttributes(), attributes);
|
|
3476
3481
|
model.fill(extraAttributes).syncOriginalAttributes(...attributes);
|
|
3477
3482
|
});
|
|
3478
3483
|
return this;
|
|
@@ -3510,20 +3515,21 @@ var Collection$1 = class Collection$1 extends Collection {
|
|
|
3510
3515
|
diff(items) {
|
|
3511
3516
|
const diff = new this.constructor();
|
|
3512
3517
|
const dictionary = this.getDictionary(items);
|
|
3513
|
-
this.
|
|
3518
|
+
this.all().map((item) => {
|
|
3514
3519
|
if (dictionary[item.getKey()] === void 0) diff.add(item);
|
|
3515
3520
|
});
|
|
3516
3521
|
return diff;
|
|
3517
3522
|
}
|
|
3518
|
-
except(keys) {
|
|
3519
|
-
const
|
|
3523
|
+
except(...keys) {
|
|
3524
|
+
const values = keys.length === 1 && Array.isArray(keys[0]) ? keys[0].map(String) : keys.map(String);
|
|
3525
|
+
const dictionary = Arr.except(this.getDictionary(), values);
|
|
3520
3526
|
return new this.constructor(Object.values(dictionary));
|
|
3521
3527
|
}
|
|
3522
3528
|
intersect(items) {
|
|
3523
3529
|
const intersect = new this.constructor();
|
|
3524
|
-
if (isEmpty(items)) return intersect;
|
|
3530
|
+
if (Arr.isEmpty(items)) return intersect;
|
|
3525
3531
|
const dictionary = this.getDictionary(items);
|
|
3526
|
-
for (const item of this.
|
|
3532
|
+
for (const item of this.all()) if (dictionary[item.getKey()] !== void 0) intersect.add(item);
|
|
3527
3533
|
return intersect;
|
|
3528
3534
|
}
|
|
3529
3535
|
unique(key, _strict = false) {
|
|
@@ -3532,20 +3538,21 @@ var Collection$1 = class Collection$1 extends Collection {
|
|
|
3532
3538
|
}
|
|
3533
3539
|
find(key, defaultValue = null) {
|
|
3534
3540
|
if (key instanceof Model) key = key.getKey();
|
|
3535
|
-
if (isArray(key)) {
|
|
3541
|
+
if (Array.isArray(key)) {
|
|
3536
3542
|
if (this.isEmpty()) return new this.constructor();
|
|
3537
3543
|
return this.whereIn(this.first().getKeyName(), key);
|
|
3538
3544
|
}
|
|
3539
|
-
collect
|
|
3545
|
+
collect(this.all()).first((model) => {
|
|
3540
3546
|
return model.getKey() == key;
|
|
3541
3547
|
});
|
|
3542
|
-
return this.
|
|
3548
|
+
return this.all().filter((model) => {
|
|
3543
3549
|
return model.getKey() == key;
|
|
3544
3550
|
})[0] || defaultValue;
|
|
3545
3551
|
}
|
|
3546
3552
|
async fresh(...args) {
|
|
3547
3553
|
if (this.isEmpty()) return new this.constructor();
|
|
3548
3554
|
const model = this.first();
|
|
3555
|
+
if (!model) return new this.constructor();
|
|
3549
3556
|
const freshModels = (await model.newQuery().with(...args).whereIn(model.getKeyName(), this.modelKeys()).get()).getDictionary();
|
|
3550
3557
|
return this.filter((model) => {
|
|
3551
3558
|
return model.exists && freshModels[model.getKey()] !== void 0;
|
|
@@ -3568,15 +3575,15 @@ var Collection$1 = class Collection$1 extends Collection {
|
|
|
3568
3575
|
item.append(attributes);
|
|
3569
3576
|
});
|
|
3570
3577
|
}
|
|
3571
|
-
only(keys) {
|
|
3572
|
-
|
|
3573
|
-
const dictionary =
|
|
3578
|
+
only(...keys) {
|
|
3579
|
+
const values = keys.length === 1 && Array.isArray(keys[0]) ? keys[0].map(String) : keys.map(String);
|
|
3580
|
+
const dictionary = Arr.select(this.getDictionary(), values);
|
|
3574
3581
|
return new this.constructor(Object.values(dictionary));
|
|
3575
3582
|
}
|
|
3576
3583
|
getDictionary(items) {
|
|
3577
|
-
|
|
3584
|
+
const values = !items ? this.all() : items instanceof Collection ? items.all() : items;
|
|
3578
3585
|
const dictionary = {};
|
|
3579
|
-
|
|
3586
|
+
values.map((value) => {
|
|
3580
3587
|
dictionary[value.getKey()] = value;
|
|
3581
3588
|
});
|
|
3582
3589
|
return dictionary;
|
|
@@ -3600,8 +3607,8 @@ var Collection$1 = class Collection$1 extends Collection {
|
|
|
3600
3607
|
return JSON.stringify(this.toData(), ...args);
|
|
3601
3608
|
}
|
|
3602
3609
|
[Symbol.iterator] = () => {
|
|
3603
|
-
const items = this.
|
|
3604
|
-
const length =
|
|
3610
|
+
const items = this.all();
|
|
3611
|
+
const length = items.length;
|
|
3605
3612
|
let n = 0;
|
|
3606
3613
|
return { next() {
|
|
3607
3614
|
return n < length ? {
|
|
@@ -3648,24 +3655,24 @@ const InteractsWithPivotTable = (Relation) => {
|
|
|
3648
3655
|
let records;
|
|
3649
3656
|
const results = await this.getCurrentlyAttachedPivots();
|
|
3650
3657
|
const current = results.length === 0 ? [] : results.map((result) => result.toData()).pluck(this.relatedPivotKey).all().map((i) => String(i));
|
|
3651
|
-
const detach = diff(
|
|
3658
|
+
const detach = collect(current).diff(Object.keys(records = this.formatRecordsList(this.parseIds(ids)))).all();
|
|
3652
3659
|
if (detaching && detach.length > 0) {
|
|
3653
3660
|
await this.detach(detach);
|
|
3654
3661
|
changes.detached = this.castKeys(detach);
|
|
3655
3662
|
}
|
|
3656
|
-
changes =
|
|
3663
|
+
changes = Obj.deepMerge(changes, await this.attachNew(records, current, false));
|
|
3657
3664
|
return changes;
|
|
3658
3665
|
}
|
|
3659
3666
|
syncWithoutDetaching(ids) {
|
|
3660
3667
|
return this.sync(ids, false);
|
|
3661
3668
|
}
|
|
3662
3669
|
syncWithPivotValues(ids, values, detaching = true) {
|
|
3663
|
-
return this.sync(collect
|
|
3664
|
-
return [id, values];
|
|
3670
|
+
return this.sync(collect(this.parseIds(ids)).mapWithKeys((id) => {
|
|
3671
|
+
return [String(id), values];
|
|
3665
3672
|
}), detaching);
|
|
3666
3673
|
}
|
|
3667
3674
|
withPivot(columns) {
|
|
3668
|
-
this.pivotColumns = this.pivotColumns.concat(isArray(columns) ? columns : Array.prototype.slice.call(columns));
|
|
3675
|
+
this.pivotColumns = this.pivotColumns.concat(Array.isArray(columns) ? columns : Array.prototype.slice.call(columns));
|
|
3669
3676
|
return this;
|
|
3670
3677
|
}
|
|
3671
3678
|
async attachNew(records, current, touch = true) {
|
|
@@ -3701,9 +3708,9 @@ const InteractsWithPivotTable = (Relation) => {
|
|
|
3701
3708
|
return parseInt(updated);
|
|
3702
3709
|
}
|
|
3703
3710
|
formatRecordsList(records) {
|
|
3704
|
-
return collect
|
|
3705
|
-
if (!isArray(attributes)) [id, attributes] = [attributes, {}];
|
|
3706
|
-
return [id, attributes];
|
|
3711
|
+
return collect(records).mapWithKeys((attributes, id) => {
|
|
3712
|
+
if (!Array.isArray(attributes)) [id, attributes] = [attributes, {}];
|
|
3713
|
+
return [String(id), attributes];
|
|
3707
3714
|
}).all();
|
|
3708
3715
|
}
|
|
3709
3716
|
async getCurrentlyAttachedPivots() {
|
|
@@ -3773,7 +3780,7 @@ const InteractsWithPivotTable = (Relation) => {
|
|
|
3773
3780
|
}
|
|
3774
3781
|
formatAttachRecord(key, value, attributes, hasTimestamps) {
|
|
3775
3782
|
const [id, newAttributes] = this.extractAttachIdAndAttributes(key, value, attributes);
|
|
3776
|
-
return
|
|
3783
|
+
return Obj.deepMerge(this.baseAttachRecord(id, hasTimestamps), newAttributes);
|
|
3777
3784
|
}
|
|
3778
3785
|
baseAttachRecord(id, timed) {
|
|
3779
3786
|
let record = {};
|
|
@@ -3786,7 +3793,7 @@ const InteractsWithPivotTable = (Relation) => {
|
|
|
3786
3793
|
return record;
|
|
3787
3794
|
}
|
|
3788
3795
|
extractAttachIdAndAttributes(key, value, newAttributes) {
|
|
3789
|
-
return isArray(value) ? [key, {
|
|
3796
|
+
return Array.isArray(value) ? [key, {
|
|
3790
3797
|
...value,
|
|
3791
3798
|
...newAttributes
|
|
3792
3799
|
}] : [value, newAttributes];
|
|
@@ -3797,7 +3804,7 @@ const InteractsWithPivotTable = (Relation) => {
|
|
|
3797
3804
|
parseIds(value) {
|
|
3798
3805
|
if (value instanceof Model) return [value[this.relatedKey]];
|
|
3799
3806
|
if (value instanceof Collection$1) return value.pluck(this.relatedKey).all();
|
|
3800
|
-
return isArray(value) ? value : [value];
|
|
3807
|
+
return Array.isArray(value) ? value : [value];
|
|
3801
3808
|
}
|
|
3802
3809
|
};
|
|
3803
3810
|
};
|
|
@@ -3857,9 +3864,8 @@ var BelongsToMany = class extends compose(Relation, InteractsWithPivotTable) {
|
|
|
3857
3864
|
return this;
|
|
3858
3865
|
}
|
|
3859
3866
|
async get(columns) {
|
|
3860
|
-
var _builder$query;
|
|
3861
3867
|
const builder = this.query.applyScopes();
|
|
3862
|
-
columns =
|
|
3868
|
+
columns = builder.query?._statements?.find((item) => item.grouping == "columns") ? [] : columns;
|
|
3863
3869
|
let models = await builder.select(this.shouldSelect(columns)).getModels();
|
|
3864
3870
|
this.hydratePivotRelation(models);
|
|
3865
3871
|
if (models.length > 0) models = await builder.eagerLoadRelations(models);
|
|
@@ -3908,7 +3914,7 @@ var BelongsToMany = class extends compose(Relation, InteractsWithPivotTable) {
|
|
|
3908
3914
|
const value = model.attributes[key];
|
|
3909
3915
|
if (key.startsWith("pivot_")) {
|
|
3910
3916
|
values[key.substring(6)] = value;
|
|
3911
|
-
model.attributes =
|
|
3917
|
+
model.attributes = Arr.except(model.attributes, [key]);
|
|
3912
3918
|
}
|
|
3913
3919
|
}
|
|
3914
3920
|
return values;
|
|
@@ -3919,11 +3925,11 @@ var BelongsToMany = class extends compose(Relation, InteractsWithPivotTable) {
|
|
|
3919
3925
|
return this.withPivot(this.createdAt(), this.updatedAt());
|
|
3920
3926
|
}
|
|
3921
3927
|
shouldSelect(columns = ["*"]) {
|
|
3922
|
-
if (
|
|
3928
|
+
if (columns.length === 1 && columns[0] === "*") columns = [this.related.getTable() + ".*"];
|
|
3923
3929
|
return columns.concat(this.aliasedPivotColumns());
|
|
3924
3930
|
}
|
|
3925
3931
|
aliasedPivotColumns() {
|
|
3926
|
-
return collect
|
|
3932
|
+
return collect([this.foreignPivotKey, this.relatedPivotKey].concat(this.pivotColumns)).map((column) => {
|
|
3927
3933
|
return this.qualifyPivotColumn(column) + " as pivot_" + column;
|
|
3928
3934
|
}).unique().all();
|
|
3929
3935
|
}
|
|
@@ -4043,9 +4049,8 @@ var Builder = class Builder extends Inference$1 {
|
|
|
4043
4049
|
}
|
|
4044
4050
|
asProxy() {
|
|
4045
4051
|
return new Proxy(this, { get(target, prop) {
|
|
4046
|
-
var _target$query$connect;
|
|
4047
4052
|
if (typeof target[prop] !== "undefined") return target[prop];
|
|
4048
|
-
const skipReturning = !!
|
|
4053
|
+
const skipReturning = !!target.query.connector?.client.config?.client?.includes("mysql") && prop === "returning";
|
|
4049
4054
|
if ([
|
|
4050
4055
|
"select",
|
|
4051
4056
|
"from",
|
|
@@ -4139,7 +4144,7 @@ var Builder = class Builder extends Inference$1 {
|
|
|
4139
4144
|
};
|
|
4140
4145
|
}
|
|
4141
4146
|
if (prop.startsWith("where")) {
|
|
4142
|
-
const column = snake(prop.substring(5));
|
|
4147
|
+
const column = Str.snake(prop.substring(5));
|
|
4143
4148
|
return (...args) => {
|
|
4144
4149
|
target.query.where(column, ...args);
|
|
4145
4150
|
return target.asProxy();
|
|
@@ -4214,7 +4219,8 @@ var Builder = class Builder extends Inference$1 {
|
|
|
4214
4219
|
}
|
|
4215
4220
|
addUpdatedAtColumn(values) {
|
|
4216
4221
|
if (!this.model.usesTimestamps() || this.model.getUpdatedAtColumn() === null) return values;
|
|
4217
|
-
|
|
4222
|
+
const column = this.model.getUpdatedAtColumn();
|
|
4223
|
+
values = Obj.deepMerge({ [column]: this.model.freshTimestampString() }, values);
|
|
4218
4224
|
return values;
|
|
4219
4225
|
}
|
|
4220
4226
|
delete() {
|
|
@@ -4242,9 +4248,8 @@ var Builder = class Builder extends Inference$1 {
|
|
|
4242
4248
|
return this.model;
|
|
4243
4249
|
}
|
|
4244
4250
|
setModel(model) {
|
|
4245
|
-
var _this$query;
|
|
4246
4251
|
this.model = model;
|
|
4247
|
-
if (typeof
|
|
4252
|
+
if (typeof this.query?.client?.table == "function") this.query = this.query.client.table(this.model.getTable());
|
|
4248
4253
|
else this.query = this.query.table(this.model.getTable());
|
|
4249
4254
|
return this;
|
|
4250
4255
|
}
|
|
@@ -4287,7 +4292,7 @@ var Builder = class Builder extends Inference$1 {
|
|
|
4287
4292
|
}
|
|
4288
4293
|
withoutGlobalScope(scope) {
|
|
4289
4294
|
if (typeof scope !== "string") scope = scope.constructor.name;
|
|
4290
|
-
this.globalScopes =
|
|
4295
|
+
this.globalScopes = Arr.except(this.globalScopes, [scope]);
|
|
4291
4296
|
return this;
|
|
4292
4297
|
}
|
|
4293
4298
|
macro(name, callback) {
|
|
@@ -4304,7 +4309,7 @@ var Builder = class Builder extends Inference$1 {
|
|
|
4304
4309
|
let eagerLoads = {};
|
|
4305
4310
|
if (typeof args[1] === "function") {
|
|
4306
4311
|
const eagerLoad = this.parseWithRelations({ [args[0]]: args[1] });
|
|
4307
|
-
this.eagerLoad =
|
|
4312
|
+
this.eagerLoad = Obj.deepMerge(this.eagerLoad, eagerLoad);
|
|
4308
4313
|
return this;
|
|
4309
4314
|
}
|
|
4310
4315
|
const relations = flattenDeep(args);
|
|
@@ -4313,13 +4318,13 @@ var Builder = class Builder extends Inference$1 {
|
|
|
4313
4318
|
let eagerLoad;
|
|
4314
4319
|
if (typeof relation === "string") eagerLoad = { [relation]: (q) => q };
|
|
4315
4320
|
else if (typeof relation === "object") eagerLoad = relation;
|
|
4316
|
-
eagerLoads =
|
|
4321
|
+
eagerLoads = Obj.deepMerge(eagerLoads, eagerLoad);
|
|
4317
4322
|
}
|
|
4318
|
-
this.eagerLoad =
|
|
4323
|
+
this.eagerLoad = Obj.deepMerge(this.eagerLoad, this.parseWithRelations(eagerLoads));
|
|
4319
4324
|
return this;
|
|
4320
4325
|
}
|
|
4321
4326
|
has(relation, operator = ">=", count = 1, boolean = "and", callback = null) {
|
|
4322
|
-
if (
|
|
4327
|
+
if (typeof relation === "string") {
|
|
4323
4328
|
if (relation.includes(".")) return this.hasNested(relation, operator, count, boolean, callback);
|
|
4324
4329
|
relation = this.getRelationWithoutConstraints(getRelationMethod(relation));
|
|
4325
4330
|
}
|
|
@@ -4396,7 +4401,7 @@ var Builder = class Builder extends Inference$1 {
|
|
|
4396
4401
|
let eagerLoad;
|
|
4397
4402
|
if (typeof relation === "string") eagerLoad = { [relation]: (q) => q };
|
|
4398
4403
|
else if (typeof relation === "object") eagerLoad = relation;
|
|
4399
|
-
eagerLoads =
|
|
4404
|
+
eagerLoads = Obj.deepMerge(eagerLoads, eagerLoad);
|
|
4400
4405
|
}
|
|
4401
4406
|
relations = eagerLoads;
|
|
4402
4407
|
const db = this.model.getConnection();
|
|
@@ -4415,7 +4420,7 @@ var Builder = class Builder extends Inference$1 {
|
|
|
4415
4420
|
} else expression = column;
|
|
4416
4421
|
const query = relation.getRelationExistenceQuery(relation.getRelated().newModelQuery(), this, db.raw(expression));
|
|
4417
4422
|
constraints(query);
|
|
4418
|
-
alias = alias || snake(`${name} ${action} ${column}`.replace("/[^[:alnum:][:space:]_]/u", ""));
|
|
4423
|
+
alias = alias || Str.snake(`${name} ${action} ${column}`.replace("/[^[:alnum:][:space:]_]/u", ""));
|
|
4419
4424
|
if (action === "exists") this.select(db.raw(`exists(${query.toSql().sql}) as ${alias}`));
|
|
4420
4425
|
else this.selectSub(action ? query : query.limit(1), alias);
|
|
4421
4426
|
}
|
|
@@ -4439,7 +4444,7 @@ var Builder = class Builder extends Inference$1 {
|
|
|
4439
4444
|
}
|
|
4440
4445
|
parseSub(query) {
|
|
4441
4446
|
if (query instanceof Builder || query instanceof Relation) return [query.toSql().sql, query.toSql().bindings];
|
|
4442
|
-
else if (
|
|
4447
|
+
else if (typeof query === "string") return [query, []];
|
|
4443
4448
|
else throw new Error("A subquery must be a query builder instance, a Closure, or a string.");
|
|
4444
4449
|
}
|
|
4445
4450
|
prependDatabaseNameIfCrossDatabaseQuery(query) {
|
|
@@ -4496,15 +4501,15 @@ var Builder = class Builder extends Inference$1 {
|
|
|
4496
4501
|
if (prefix !== "") prefix += ".";
|
|
4497
4502
|
for (const key in relations) {
|
|
4498
4503
|
const value = relations[key];
|
|
4499
|
-
if (
|
|
4504
|
+
if (typeof value === "string" || Number.isFinite(parseInt(value))) continue;
|
|
4500
4505
|
const [attribute, attributeSelectConstraint] = this.parseNameAndAttributeSelectionConstraint(key, value);
|
|
4501
4506
|
preparedRelationships = Object.assign({}, preparedRelationships, { [`${prefix}${attribute}`]: attributeSelectConstraint }, this.prepareNestedWithRelationships(value, `${prefix}${attribute}`));
|
|
4502
|
-
relations =
|
|
4507
|
+
relations = Arr.except(relations, [key]);
|
|
4503
4508
|
}
|
|
4504
4509
|
for (const key in relations) {
|
|
4505
4510
|
const value = relations[key];
|
|
4506
4511
|
let attribute = key, attributeSelectConstraint = value;
|
|
4507
|
-
if (
|
|
4512
|
+
if (typeof value === "string") [attribute, attributeSelectConstraint] = this.parseNameAndAttributeSelectionConstraint(value);
|
|
4508
4513
|
preparedRelationships[`${prefix}${attribute}`] = this.combineConstraints([attributeSelectConstraint, preparedRelationships[`${prefix}${attribute}`] || (() => {})]);
|
|
4509
4514
|
}
|
|
4510
4515
|
return preparedRelationships;
|
|
@@ -4552,8 +4557,8 @@ var Builder = class Builder extends Inference$1 {
|
|
|
4552
4557
|
}
|
|
4553
4558
|
async findOrFail(...args) {
|
|
4554
4559
|
const data = await this.find(...args);
|
|
4555
|
-
if (isArray(args[0])) {
|
|
4556
|
-
if (data.count() !== args[0].length) throw new ModelNotFoundError().setModel(this.model.constructor.name,
|
|
4560
|
+
if (Array.isArray(args[0])) {
|
|
4561
|
+
if (data.count() !== args[0].length) throw new ModelNotFoundError().setModel(this.model.constructor.name, collect(args[0]).diff(data.modelKeys()).all());
|
|
4557
4562
|
return data;
|
|
4558
4563
|
}
|
|
4559
4564
|
if (data === null) throw new ModelNotFoundError().setModel(this.model.constructor.name, args[0]);
|
|
@@ -4567,12 +4572,12 @@ var Builder = class Builder extends Inference$1 {
|
|
|
4567
4572
|
async firstOrNew(attributes = {}, values = {}) {
|
|
4568
4573
|
const instance = await this.where(attributes).first();
|
|
4569
4574
|
if (instance !== null) return instance;
|
|
4570
|
-
return this.newModelInstance(
|
|
4575
|
+
return this.newModelInstance(Obj.deepMerge(attributes, values));
|
|
4571
4576
|
}
|
|
4572
4577
|
async firstOrCreate(attributes = {}, values = {}) {
|
|
4573
4578
|
const instance = await this.where(attributes).first();
|
|
4574
4579
|
if (instance !== null) return instance;
|
|
4575
|
-
return tap(this.newModelInstance(
|
|
4580
|
+
return tap(this.newModelInstance(Obj.deepMerge(attributes, values)), async (instance) => {
|
|
4576
4581
|
await instance.save({ client: this.query });
|
|
4577
4582
|
});
|
|
4578
4583
|
}
|
|
@@ -4592,14 +4597,13 @@ var Builder = class Builder extends Inference$1 {
|
|
|
4592
4597
|
return this;
|
|
4593
4598
|
}
|
|
4594
4599
|
async find(id, columns) {
|
|
4595
|
-
if (isArray(id) || id instanceof Collection$1) return await this.findMany(id, columns);
|
|
4600
|
+
if (Array.isArray(id) || id instanceof Collection$1) return await this.findMany(id, columns);
|
|
4596
4601
|
return await this.where(this.model.getKeyName(), id).first(columns);
|
|
4597
4602
|
}
|
|
4598
4603
|
async findMany(ids, columns = ["*"]) {
|
|
4599
|
-
|
|
4600
|
-
|
|
4601
|
-
|
|
4602
|
-
return await this.whereIn(this.model.getKeyName(), ids).get(columns);
|
|
4604
|
+
const values = ids instanceof Collection$1 ? ids.modelKeys() : Array.isArray(ids) ? ids : [ids];
|
|
4605
|
+
if (values.length === 0) return new Collection$1([]);
|
|
4606
|
+
return await this.whereIn(this.model.getKeyName(), values).get(columns);
|
|
4603
4607
|
}
|
|
4604
4608
|
async pluck(column) {
|
|
4605
4609
|
return new Collection$1(await this.query.pluck(column));
|
|
@@ -4607,7 +4611,7 @@ var Builder = class Builder extends Inference$1 {
|
|
|
4607
4611
|
async destroy(ids) {
|
|
4608
4612
|
if (ids instanceof Collection$1) ids = ids.modelKeys();
|
|
4609
4613
|
if (ids instanceof Collection) ids = ids.all();
|
|
4610
|
-
ids = isArray(ids) ? ids : Array.prototype.slice.call(ids);
|
|
4614
|
+
ids = Array.isArray(ids) ? ids : Array.prototype.slice.call(ids);
|
|
4611
4615
|
if (ids.length === 0) return 0;
|
|
4612
4616
|
const key = this.model.newInstance().getKeyName();
|
|
4613
4617
|
let count = 0;
|
|
@@ -4625,12 +4629,11 @@ var Builder = class Builder extends Inference$1 {
|
|
|
4625
4629
|
return await this.model.newModelQuery().get(columns);
|
|
4626
4630
|
}
|
|
4627
4631
|
async paginate(page = 1, perPage = 10) {
|
|
4628
|
-
var _this;
|
|
4629
4632
|
page = page || 1;
|
|
4630
|
-
perPage = perPage ||
|
|
4633
|
+
perPage = perPage || this?.model?.perPage || 15;
|
|
4631
4634
|
this.applyScopes();
|
|
4632
4635
|
const total = await this.query.clone().clearOrder().clearSelect().count(this.primaryKey);
|
|
4633
|
-
let results
|
|
4636
|
+
let results;
|
|
4634
4637
|
if (total > 0) {
|
|
4635
4638
|
const skip = (page - 1) * (perPage ?? 10);
|
|
4636
4639
|
this.take(perPage).skip(skip);
|
|
@@ -4640,7 +4643,7 @@ var Builder = class Builder extends Inference$1 {
|
|
|
4640
4643
|
return new Paginator(results, parseInt(total), perPage, page);
|
|
4641
4644
|
}
|
|
4642
4645
|
async getModels(...columns) {
|
|
4643
|
-
columns =
|
|
4646
|
+
columns = flatten(columns);
|
|
4644
4647
|
if (columns.length > 0) {
|
|
4645
4648
|
if (this.query._statements.filter((item) => item.grouping == "columns").length == 0 && columns[0] !== "*") this.query.select(...columns);
|
|
4646
4649
|
}
|
|
@@ -4770,11 +4773,11 @@ var SQLite = class {
|
|
|
4770
4773
|
is_generated: raw.hidden !== 0,
|
|
4771
4774
|
generation_expression: null,
|
|
4772
4775
|
is_nullable: raw.notnull === 0,
|
|
4773
|
-
is_unique: !!
|
|
4776
|
+
is_unique: !!index?.unique && indexInfo?.length === 1,
|
|
4774
4777
|
is_primary_key: raw.pk === 1,
|
|
4775
4778
|
has_auto_increment: raw.pk === 1 && tablesWithAutoIncrementPrimaryKeys.includes(table),
|
|
4776
|
-
foreign_key_column:
|
|
4777
|
-
foreign_key_table:
|
|
4779
|
+
foreign_key_column: foreignKey?.to || null,
|
|
4780
|
+
foreign_key_table: foreignKey?.table || null
|
|
4778
4781
|
};
|
|
4779
4782
|
});
|
|
4780
4783
|
};
|
|
@@ -5000,8 +5003,7 @@ var Migrator = class {
|
|
|
5000
5003
|
else await callback(connection);
|
|
5001
5004
|
}
|
|
5002
5005
|
async runMethod(connection, migration, method) {
|
|
5003
|
-
|
|
5004
|
-
await ((_migration$method = migration[method]) === null || _migration$method === void 0 ? void 0 : _migration$method.call(migration, connection.schema, connection));
|
|
5006
|
+
await migration[method]?.(connection.schema, connection);
|
|
5005
5007
|
}
|
|
5006
5008
|
async resolvePath(filePath) {
|
|
5007
5009
|
try {
|
|
@@ -5030,7 +5032,7 @@ var Migrator = class {
|
|
|
5030
5032
|
return path.basename(filePath).replace(".js", "");
|
|
5031
5033
|
}
|
|
5032
5034
|
path(p) {
|
|
5033
|
-
this.paths = Array.from(new Set([...this.paths, p]));
|
|
5035
|
+
this.paths = Array.from(/* @__PURE__ */ new Set([...this.paths, p]));
|
|
5034
5036
|
}
|
|
5035
5037
|
getPaths() {
|
|
5036
5038
|
return this.paths;
|
|
@@ -5218,8 +5220,7 @@ var Migrate = class {
|
|
|
5218
5220
|
* @returns
|
|
5219
5221
|
*/
|
|
5220
5222
|
async setupConnection(config) {
|
|
5221
|
-
|
|
5222
|
-
const table = (config === null || config === void 0 || (_config$migrations = config.migrations) === null || _config$migrations === void 0 ? void 0 : _config$migrations.table) || "migrations";
|
|
5223
|
+
const table = config?.migrations?.table || "migrations";
|
|
5223
5224
|
if (config.skipConnection !== true) {
|
|
5224
5225
|
arquebus.addConnection(config, "default");
|
|
5225
5226
|
Object.entries(config.connections || {}).forEach(([name, connection]) => {
|
|
@@ -5279,7 +5280,6 @@ var MigrationCreator = class {
|
|
|
5279
5280
|
let stub;
|
|
5280
5281
|
if (!table) {
|
|
5281
5282
|
const customPath = path.join(this.customStubPath ?? "", `migration-${this.type}.stub`);
|
|
5282
|
-
console.log("\n", customPath, "---");
|
|
5283
5283
|
stub = await FileSystem.fileExists(customPath) ? customPath : this.stubPath(`/migration-${this.type}.stub`);
|
|
5284
5284
|
} else if (create) {
|
|
5285
5285
|
const customPath = path.join(this.customStubPath ?? "", `migration.create-${this.type}.stub`);
|
|
@@ -5312,7 +5312,9 @@ var MigrationCreator = class {
|
|
|
5312
5312
|
}
|
|
5313
5313
|
stubPath(stub = "") {
|
|
5314
5314
|
const __dirname = this.getDirname(import.meta);
|
|
5315
|
-
|
|
5315
|
+
const resolved = [path.join(__dirname, "stubs", stub), path.join(__dirname, "../stubs", stub)].find((candidate) => existsSync(candidate));
|
|
5316
|
+
if (!resolved) throw new Error(`Migration stub not found: ${stub}`);
|
|
5317
|
+
return resolved;
|
|
5316
5318
|
}
|
|
5317
5319
|
getDirname(meta) {
|
|
5318
5320
|
if (typeof __dirname !== "undefined") return __dirname;
|