@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/inspector/index.js
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
import path from "path";
|
|
2
|
-
import {
|
|
3
|
-
import
|
|
4
|
-
import
|
|
5
|
-
import
|
|
2
|
+
import { Arr, Obj, Str, data_get, data_set } from "@h3ravel/support";
|
|
3
|
+
import * as dayjsModule from "dayjs";
|
|
4
|
+
import * as advancedFormatModule from "dayjs/plugin/advancedFormat.js";
|
|
5
|
+
import { Collection, collect } from "@h3ravel/collect.js";
|
|
6
6
|
import Knex$1 from "knex";
|
|
7
7
|
import { existsSync } from "fs";
|
|
8
|
-
import { FileSystem } from "@h3ravel/shared";
|
|
8
|
+
import { FileSystem, importFile } from "@h3ravel/shared";
|
|
9
9
|
import pluralize from "pluralize";
|
|
10
10
|
import "resolve-from";
|
|
11
11
|
//#region \0rolldown/runtime.js
|
|
@@ -97,8 +97,7 @@ var CockroachDB = class {
|
|
|
97
97
|
*/
|
|
98
98
|
async hasTable(table) {
|
|
99
99
|
const subquery = this.knex.select().from("information_schema.tables").whereIn("table_schema", this.explodedSchema).andWhere({ table_name: table });
|
|
100
|
-
|
|
101
|
-
return (record === null || record === void 0 ? void 0 : record.exists) || false;
|
|
100
|
+
return (await this.knex.select(this.knex.raw("exists (?)", [subquery])).first())?.exists || false;
|
|
102
101
|
}
|
|
103
102
|
/**
|
|
104
103
|
* Get all the available columns in the current schema/database. Can be filtered to a specific table
|
|
@@ -198,18 +197,17 @@ var CockroachDB = class {
|
|
|
198
197
|
${column ? "AND att.attname = ?" : ""}
|
|
199
198
|
`, bindings)]);
|
|
200
199
|
const parsedColumms = columns.rows.map((col) => {
|
|
201
|
-
var _col$default_value;
|
|
202
200
|
const constraintsForColumn = constraints.rows.filter((constraint) => constraint.table === col.table && constraint.column === col.name);
|
|
203
201
|
const foreignKeyConstraint = constraintsForColumn.find((constraint) => constraint.type === "f");
|
|
204
202
|
return {
|
|
205
203
|
...col,
|
|
206
204
|
is_unique: constraintsForColumn.some((constraint) => ["u", "p"].includes(constraint.type)),
|
|
207
205
|
is_primary_key: constraintsForColumn.some((constraint) => constraint.type === "p"),
|
|
208
|
-
has_auto_increment: ["integer", "bigint"].includes(col.data_type) && (
|
|
206
|
+
has_auto_increment: ["integer", "bigint"].includes(col.data_type) && (col.default_value?.startsWith("nextval(") ?? false),
|
|
209
207
|
default_value: parseDefaultValue$5(col.default_value),
|
|
210
|
-
foreign_key_schema:
|
|
211
|
-
foreign_key_table:
|
|
212
|
-
foreign_key_column:
|
|
208
|
+
foreign_key_schema: foreignKeyConstraint?.foreign_key_schema ?? null,
|
|
209
|
+
foreign_key_table: foreignKeyConstraint?.foreign_key_table ?? null,
|
|
210
|
+
foreign_key_column: foreignKeyConstraint?.foreign_key_column ?? null
|
|
213
211
|
};
|
|
214
212
|
});
|
|
215
213
|
if (table && column) return parsedColumms[0];
|
|
@@ -223,8 +221,7 @@ var CockroachDB = class {
|
|
|
223
221
|
table_name: table,
|
|
224
222
|
column_name: column
|
|
225
223
|
});
|
|
226
|
-
|
|
227
|
-
return (record === null || record === void 0 ? void 0 : record.exists) || false;
|
|
224
|
+
return (await this.knex.select(this.knex.raw("exists (?)", [subquery])).first())?.exists || false;
|
|
228
225
|
}
|
|
229
226
|
/**
|
|
230
227
|
* Get the primary key column for the given table
|
|
@@ -536,14 +533,13 @@ var MSSQL = class {
|
|
|
536
533
|
WHERE
|
|
537
534
|
OBJECT_SCHEMA_NAME (f.parent_object_id) = ?;
|
|
538
535
|
`, [this.schema]);
|
|
539
|
-
if (table) return result
|
|
536
|
+
if (table) return result?.filter((row) => row.table === table);
|
|
540
537
|
return result;
|
|
541
538
|
}
|
|
542
539
|
};
|
|
543
540
|
//#endregion
|
|
544
541
|
//#region src/inspector/dialects/mysql.ts
|
|
545
542
|
function rawColumnToColumn$1(rawColumn) {
|
|
546
|
-
var _rawColumn$EXTRA;
|
|
547
543
|
let dataType = rawColumn.COLUMN_TYPE.replace(/\(.*?\)/, "");
|
|
548
544
|
if (rawColumn.COLUMN_TYPE.startsWith("tinyint(1)")) dataType = "boolean";
|
|
549
545
|
return {
|
|
@@ -556,7 +552,7 @@ function rawColumnToColumn$1(rawColumn) {
|
|
|
556
552
|
max_length: rawColumn.CHARACTER_MAXIMUM_LENGTH,
|
|
557
553
|
numeric_precision: rawColumn.NUMERIC_PRECISION,
|
|
558
554
|
numeric_scale: rawColumn.NUMERIC_SCALE,
|
|
559
|
-
is_generated: !!
|
|
555
|
+
is_generated: !!rawColumn.EXTRA?.endsWith("GENERATED"),
|
|
560
556
|
is_nullable: rawColumn.IS_NULLABLE === "YES",
|
|
561
557
|
is_unique: rawColumn.COLUMN_KEY === "UNI",
|
|
562
558
|
is_primary_key: rawColumn.CONSTRAINT_NAME === "PRIMARY" || rawColumn.COLUMN_KEY === "PRI",
|
|
@@ -749,11 +745,10 @@ var oracleDB = class {
|
|
|
749
745
|
* Check if a table exists in the current schema/database
|
|
750
746
|
*/
|
|
751
747
|
async hasTable(table) {
|
|
752
|
-
|
|
748
|
+
return !!(await this.knex.select(this.knex.raw(`
|
|
753
749
|
/*+ OPTIMIZER_FEATURES_ENABLE('${OPTIMIZER_FEATURES}') */
|
|
754
750
|
COUNT(*) "count"
|
|
755
|
-
`)).from("USER_TABLES").where({ TABLE_NAME: table }).first();
|
|
756
|
-
return !!(result === null || result === void 0 ? void 0 : result.count);
|
|
751
|
+
`)).from("USER_TABLES").where({ TABLE_NAME: table }).first())?.count;
|
|
757
752
|
}
|
|
758
753
|
/**
|
|
759
754
|
* Get all the available columns in the current schema/database. Can be filtered to a specific table
|
|
@@ -829,15 +824,14 @@ var oracleDB = class {
|
|
|
829
824
|
* Check if a table exists in the current schema/database
|
|
830
825
|
*/
|
|
831
826
|
async hasColumn(table, column) {
|
|
832
|
-
|
|
827
|
+
return !!(await this.knex.select(this.knex.raw(`
|
|
833
828
|
/*+ OPTIMIZER_FEATURES_ENABLE('${OPTIMIZER_FEATURES}') NO_QUERY_TRANSFORMATION */
|
|
834
829
|
COUNT(*) "count"
|
|
835
830
|
`)).from("USER_TAB_COLS").where({
|
|
836
831
|
TABLE_NAME: table,
|
|
837
832
|
COLUMN_NAME: column,
|
|
838
833
|
HIDDEN_COLUMN: "NO"
|
|
839
|
-
}).first();
|
|
840
|
-
return !!(result === null || result === void 0 ? void 0 : result.count);
|
|
834
|
+
}).first())?.count;
|
|
841
835
|
}
|
|
842
836
|
/**
|
|
843
837
|
* Get the primary key column for the given table
|
|
@@ -1003,13 +997,12 @@ var Postgres = class {
|
|
|
1003
997
|
`, bindings)).rows;
|
|
1004
998
|
}
|
|
1005
999
|
async columnInfo(table, column) {
|
|
1006
|
-
var _versionResponse$rows;
|
|
1007
1000
|
const { knex } = this;
|
|
1008
1001
|
const bindings = [];
|
|
1009
1002
|
if (table) bindings.push(table);
|
|
1010
1003
|
if (column) bindings.push(column);
|
|
1011
1004
|
const schemaIn = this.explodedSchema.map((schemaName) => `${this.knex.raw("?", [schemaName])}::regnamespace`);
|
|
1012
|
-
const majorVersion = (
|
|
1005
|
+
const majorVersion = (await this.knex.raw("SHOW server_version")).rows?.[0]?.server_version?.split(".")?.[0] ?? 10;
|
|
1013
1006
|
let generationSelect = `
|
|
1014
1007
|
NULL AS generation_expression,
|
|
1015
1008
|
pg_get_expr(ad.adbin, ad.adrelid) AS default_value,
|
|
@@ -1101,9 +1094,9 @@ var Postgres = class {
|
|
|
1101
1094
|
is_primary_key: constraintsForColumn.some((constraint) => constraint.type === "p"),
|
|
1102
1095
|
has_auto_increment: constraintsForColumn.some((constraint) => constraint.has_auto_increment),
|
|
1103
1096
|
default_value: parseDefaultValue$1(col.default_value),
|
|
1104
|
-
foreign_key_schema:
|
|
1105
|
-
foreign_key_table:
|
|
1106
|
-
foreign_key_column:
|
|
1097
|
+
foreign_key_schema: foreignKeyConstraint?.foreign_key_schema ?? null,
|
|
1098
|
+
foreign_key_table: foreignKeyConstraint?.foreign_key_table ?? null,
|
|
1099
|
+
foreign_key_column: foreignKeyConstraint?.foreign_key_column ?? null
|
|
1107
1100
|
};
|
|
1108
1101
|
});
|
|
1109
1102
|
if (table && column) return parsedColumms[0];
|
|
@@ -1300,7 +1293,7 @@ var ModelNotFoundError = class extends BaseError {
|
|
|
1300
1293
|
}
|
|
1301
1294
|
setModel(model, ids = []) {
|
|
1302
1295
|
this.model = model;
|
|
1303
|
-
this.ids = isArray(ids) ? ids : [ids];
|
|
1296
|
+
this.ids = Array.isArray(ids) ? ids : [ids];
|
|
1304
1297
|
this.message = `No query results for model [${model}]`;
|
|
1305
1298
|
if (this.ids.length > 0) this.message += " " + this.ids.join(", ");
|
|
1306
1299
|
else this.message += ".";
|
|
@@ -1391,25 +1384,29 @@ function compose$1(Base, ...mixins) {
|
|
|
1391
1384
|
}, Base);
|
|
1392
1385
|
}
|
|
1393
1386
|
//#endregion
|
|
1394
|
-
//#region src/
|
|
1387
|
+
//#region src/dayjs.ts
|
|
1388
|
+
const dayjs = dayjsModule.default ?? dayjsModule;
|
|
1389
|
+
const advancedFormat = advancedFormatModule.default ?? advancedFormatModule;
|
|
1395
1390
|
dayjs.extend(advancedFormat);
|
|
1391
|
+
//#endregion
|
|
1392
|
+
//#region src/utils.ts
|
|
1396
1393
|
const getRelationName = (relationMethod) => {
|
|
1397
|
-
return snake(relationMethod.substring(8));
|
|
1394
|
+
return Str.snake(relationMethod.substring(8));
|
|
1398
1395
|
};
|
|
1399
1396
|
const getRelationMethod = (relation) => {
|
|
1400
|
-
return camel(`relation_${relation}`);
|
|
1397
|
+
return Str.camel(`relation_${relation}`);
|
|
1401
1398
|
};
|
|
1402
1399
|
const getScopeMethod = (scope) => {
|
|
1403
|
-
return camel(`scope_${scope}`);
|
|
1400
|
+
return Str.camel(`scope_${scope}`);
|
|
1404
1401
|
};
|
|
1405
1402
|
const getAttrMethod = (attr) => {
|
|
1406
|
-
return camel(`attribute_${attr}`);
|
|
1403
|
+
return Str.camel(`attribute_${attr}`);
|
|
1407
1404
|
};
|
|
1408
1405
|
const getGetterMethod = (attr) => {
|
|
1409
|
-
return camel(`get_${attr}_attribute`);
|
|
1406
|
+
return Str.camel(`get_${attr}_attribute`);
|
|
1410
1407
|
};
|
|
1411
1408
|
const getSetterMethod = (attr) => {
|
|
1412
|
-
return camel(`set_${attr}_attribute`);
|
|
1409
|
+
return Str.camel(`set_${attr}_attribute`);
|
|
1413
1410
|
};
|
|
1414
1411
|
/**
|
|
1415
1412
|
* Tap into a model a collection instance
|
|
@@ -1425,7 +1422,7 @@ const tap = (instance, callback) => {
|
|
|
1425
1422
|
const { compose } = mixin_exports;
|
|
1426
1423
|
const flatten = (arr) => arr.flat();
|
|
1427
1424
|
const flattenDeep = (arr) => Array.isArray(arr) ? arr.reduce((a, b) => a.concat(flattenDeep(b)), []) : [arr];
|
|
1428
|
-
const snakeCase = (str) => trim(snake(str.replace(/[^a-zA-Z0-9_-]/g, "-")), "_-");
|
|
1425
|
+
const snakeCase = (str) => Str.trim(Str.snake(str.replace(/[^a-zA-Z0-9_-]/g, "-")), "_-").replace(/_+/g, "_");
|
|
1429
1426
|
//#endregion
|
|
1430
1427
|
//#region src/relations/relation.ts
|
|
1431
1428
|
var Relation = class {
|
|
@@ -1512,8 +1509,7 @@ var Relation = class {
|
|
|
1512
1509
|
return this.parent.getQualifiedKeyName();
|
|
1513
1510
|
}
|
|
1514
1511
|
getExistenceCompareKey() {
|
|
1515
|
-
|
|
1516
|
-
return (_this$getQualifiedFor = this.getQualifiedForeignKeyName) === null || _this$getQualifiedFor === void 0 ? void 0 : _this$getQualifiedFor.call(this);
|
|
1512
|
+
return this.getQualifiedForeignKeyName?.();
|
|
1517
1513
|
}
|
|
1518
1514
|
};
|
|
1519
1515
|
//#endregion
|
|
@@ -1654,8 +1650,7 @@ const HasAttributes = (Model) => {
|
|
|
1654
1650
|
return this;
|
|
1655
1651
|
}
|
|
1656
1652
|
normalizeCastClassResponse(key, value) {
|
|
1657
|
-
|
|
1658
|
-
return (value === null || value === void 0 || (_value$constructor = value.constructor) === null || _value$constructor === void 0 ? void 0 : _value$constructor.name) === "Object" ? value : { [key]: value };
|
|
1653
|
+
return value?.constructor?.name === "Object" ? value : { [key]: value };
|
|
1659
1654
|
}
|
|
1660
1655
|
syncOriginal() {
|
|
1661
1656
|
this.original = this.getAttributes();
|
|
@@ -1765,9 +1760,9 @@ const HasAttributes = (Model) => {
|
|
|
1765
1760
|
return null;
|
|
1766
1761
|
}
|
|
1767
1762
|
case "collection": try {
|
|
1768
|
-
return collect
|
|
1763
|
+
return collect(JSON.parse(value));
|
|
1769
1764
|
} catch {
|
|
1770
|
-
return collect
|
|
1765
|
+
return collect([]);
|
|
1771
1766
|
}
|
|
1772
1767
|
case "date": return this.asDate(value);
|
|
1773
1768
|
case "datetime":
|
|
@@ -1780,8 +1775,8 @@ const HasAttributes = (Model) => {
|
|
|
1780
1775
|
attributesToData() {
|
|
1781
1776
|
let attributes = { ...this.attributes };
|
|
1782
1777
|
for (const key in attributes) {
|
|
1783
|
-
if (this.hidden.includes(key)) attributes =
|
|
1784
|
-
if (this.visible.length > 0 && this.visible.includes(key) === false) attributes =
|
|
1778
|
+
if (this.hidden.includes(key)) attributes = Arr.except(attributes, [key]);
|
|
1779
|
+
if (this.visible.length > 0 && this.visible.includes(key) === false) attributes = Arr.except(attributes, [key]);
|
|
1785
1780
|
}
|
|
1786
1781
|
for (const key of this.getDates()) {
|
|
1787
1782
|
if (attributes[key] === void 0) continue;
|
|
@@ -1836,7 +1831,7 @@ const HasAttributes = (Model) => {
|
|
|
1836
1831
|
}
|
|
1837
1832
|
hasCast(key, types = []) {
|
|
1838
1833
|
if (key in this.casts) {
|
|
1839
|
-
types =
|
|
1834
|
+
types = flatten(types);
|
|
1840
1835
|
return types.length > 0 ? types.includes(this.getCastType(key)) : true;
|
|
1841
1836
|
}
|
|
1842
1837
|
return false;
|
|
@@ -1895,10 +1890,14 @@ const HasGlobalScopes = (Model) => {
|
|
|
1895
1890
|
static globalScopes;
|
|
1896
1891
|
static addGlobalScope(scope, implementation = null) {
|
|
1897
1892
|
if (typeof scope === "string" && implementation instanceof Scope) {
|
|
1898
|
-
|
|
1893
|
+
const scopes = this.globalScopes ?? {};
|
|
1894
|
+
data_set(scopes, this.name + "." + scope, implementation);
|
|
1895
|
+
this.globalScopes = scopes;
|
|
1899
1896
|
return implementation;
|
|
1900
1897
|
} else if (scope instanceof Scope) {
|
|
1901
|
-
|
|
1898
|
+
const scopes = this.globalScopes ?? {};
|
|
1899
|
+
data_set(scopes, this.name + "." + scope.constructor.name, scope);
|
|
1900
|
+
this.globalScopes = scopes;
|
|
1902
1901
|
return scope;
|
|
1903
1902
|
}
|
|
1904
1903
|
throw new InvalidArgumentError("Global scope must be an instance of Scope.");
|
|
@@ -1907,8 +1906,8 @@ const HasGlobalScopes = (Model) => {
|
|
|
1907
1906
|
return this.getGlobalScope(scope) !== null;
|
|
1908
1907
|
}
|
|
1909
1908
|
static getGlobalScope(scope) {
|
|
1910
|
-
if (typeof scope === "string") return
|
|
1911
|
-
return
|
|
1909
|
+
if (typeof scope === "string") return data_get(this.globalScopes ?? {}, this.name + "." + scope);
|
|
1910
|
+
return data_get(this.globalScopes ?? {}, this.name + "." + scope.constructor.name);
|
|
1912
1911
|
}
|
|
1913
1912
|
static getAllGlobalScopes() {
|
|
1914
1913
|
return this.globalScopes;
|
|
@@ -1917,7 +1916,7 @@ const HasGlobalScopes = (Model) => {
|
|
|
1917
1916
|
this.globalScopes = scopes;
|
|
1918
1917
|
}
|
|
1919
1918
|
getGlobalScopes() {
|
|
1920
|
-
return
|
|
1919
|
+
return data_get(this.constructor.globalScopes ?? {}, this.constructor.name, {});
|
|
1921
1920
|
}
|
|
1922
1921
|
};
|
|
1923
1922
|
};
|
|
@@ -2017,7 +2016,7 @@ const HasOneOrMany = (Relation) => {
|
|
|
2017
2016
|
}
|
|
2018
2017
|
buildDictionary(results) {
|
|
2019
2018
|
const foreign = this.getForeignKeyName();
|
|
2020
|
-
return collect
|
|
2019
|
+
return collect(results).mapToDictionary((result) => [result[foreign], result]).all();
|
|
2021
2020
|
}
|
|
2022
2021
|
async save(model) {
|
|
2023
2022
|
this.setForeignAttributesForCreate(model);
|
|
@@ -2088,9 +2087,7 @@ var HasMany = class extends compose(Relation, HasOneOrMany) {
|
|
|
2088
2087
|
return this.getParentKey() !== null ? await this.query.get() : new Collection$1([]);
|
|
2089
2088
|
}
|
|
2090
2089
|
getForeignKeyName() {
|
|
2091
|
-
|
|
2092
|
-
const segments = (_this$foreignKey = this.foreignKey) === null || _this$foreignKey === void 0 ? void 0 : _this$foreignKey.split(".");
|
|
2093
|
-
return segments === null || segments === void 0 ? void 0 : segments.pop();
|
|
2090
|
+
return (this.foreignKey?.split("."))?.pop();
|
|
2094
2091
|
}
|
|
2095
2092
|
buildDictionary(results) {
|
|
2096
2093
|
const foreign = this.getForeignKeyName();
|
|
@@ -2125,9 +2122,7 @@ var HasOne = class extends compose(Relation, HasOneOrMany, SupportsDefaultModels
|
|
|
2125
2122
|
return this.matchOneOrMany(models, results, relation, "one");
|
|
2126
2123
|
}
|
|
2127
2124
|
getForeignKeyName() {
|
|
2128
|
-
|
|
2129
|
-
const segments = (_this$foreignKey = this.foreignKey) === null || _this$foreignKey === void 0 ? void 0 : _this$foreignKey.split(".");
|
|
2130
|
-
return segments === null || segments === void 0 ? void 0 : segments.pop();
|
|
2125
|
+
return (this.foreignKey?.split("."))?.pop();
|
|
2131
2126
|
}
|
|
2132
2127
|
async getResults() {
|
|
2133
2128
|
if (this.getParentKey() === null) return this.getDefaultFor(this.parent);
|
|
@@ -2236,10 +2231,10 @@ var HasManyThrough = class extends Relation {
|
|
|
2236
2231
|
}
|
|
2237
2232
|
const model = await this.first(columns);
|
|
2238
2233
|
if (model) return model;
|
|
2239
|
-
return callback
|
|
2234
|
+
return callback?.();
|
|
2240
2235
|
}
|
|
2241
2236
|
async find(id, columns = ["*"]) {
|
|
2242
|
-
if (isArray(id)) return await this.findMany(id, columns);
|
|
2237
|
+
if (Array.isArray(id)) return await this.findMany(id, columns);
|
|
2243
2238
|
return await this.where(this.getRelated().getQualifiedKeyName(), "=", id).first(columns);
|
|
2244
2239
|
}
|
|
2245
2240
|
async findMany(ids, columns = ["*"]) {
|
|
@@ -2267,7 +2262,7 @@ var HasManyThrough = class extends Relation {
|
|
|
2267
2262
|
return await this.query.paginate(perPage ?? 15, columns, pageName, page);
|
|
2268
2263
|
}
|
|
2269
2264
|
shouldSelect(columns = ["*"]) {
|
|
2270
|
-
if (
|
|
2265
|
+
if (columns?.at(0) == "*") columns = [this.related.getTable() + ".*"];
|
|
2271
2266
|
return [...columns, this.getQualifiedFirstKeyName() + " as laravel_through_key"];
|
|
2272
2267
|
}
|
|
2273
2268
|
async chunk(count, callback) {
|
|
@@ -2361,7 +2356,7 @@ const HasRelations = (Model) => {
|
|
|
2361
2356
|
return this;
|
|
2362
2357
|
}
|
|
2363
2358
|
unsetRelation(relation) {
|
|
2364
|
-
this.relations =
|
|
2359
|
+
this.relations = Arr.except(this.relations, [relation]);
|
|
2365
2360
|
return this;
|
|
2366
2361
|
}
|
|
2367
2362
|
relationLoaded(relation) {
|
|
@@ -2384,8 +2379,7 @@ const HasRelations = (Model) => {
|
|
|
2384
2379
|
return data;
|
|
2385
2380
|
}
|
|
2386
2381
|
guessBelongsToRelation() {
|
|
2387
|
-
|
|
2388
|
-
const functionName = (((_e$stack = (/* @__PURE__ */ new Error()).stack) === null || _e$stack === void 0 ? void 0 : _e$stack.split("\n")[2]) ?? "").split(" ")[5];
|
|
2382
|
+
const functionName = ((/* @__PURE__ */ new Error()).stack?.split("\n")[2] ?? "").split(" ")[5];
|
|
2389
2383
|
return getRelationName(functionName);
|
|
2390
2384
|
}
|
|
2391
2385
|
joiningTable(related, instance = null) {
|
|
@@ -2495,7 +2489,7 @@ const HidesAttributes = (Model) => {
|
|
|
2495
2489
|
makeVisible(...keys) {
|
|
2496
2490
|
const visible = flattenDeep(keys);
|
|
2497
2491
|
if (this.visible.length > 0) this.visible = [...this.visible, ...visible];
|
|
2498
|
-
this.hidden =
|
|
2492
|
+
this.hidden = collect(this.hidden).diff(visible).all();
|
|
2499
2493
|
return this;
|
|
2500
2494
|
}
|
|
2501
2495
|
makeHidden(key, ...keys) {
|
|
@@ -2639,10 +2633,9 @@ var QueryBuilder = class QueryBuilder extends Inference$1 {
|
|
|
2639
2633
|
asProxy() {
|
|
2640
2634
|
return new Proxy(this, {
|
|
2641
2635
|
get: function(target, prop) {
|
|
2642
|
-
var _target$connector$cli;
|
|
2643
2636
|
if (typeof target[prop] !== "undefined") return target[prop];
|
|
2644
2637
|
if (["destroy", "schema"].includes(prop)) return target.connector.schema;
|
|
2645
|
-
const skipReturning = !!
|
|
2638
|
+
const skipReturning = !!target.connector.client.config?.client?.includes("mysql") && prop === "returning";
|
|
2646
2639
|
if ([
|
|
2647
2640
|
"select",
|
|
2648
2641
|
"from",
|
|
@@ -2845,6 +2838,9 @@ var arquebus = class arquebus {
|
|
|
2845
2838
|
if (this.instance === null) this.instance = new arquebus();
|
|
2846
2839
|
return this.instance;
|
|
2847
2840
|
}
|
|
2841
|
+
static withSchema() {
|
|
2842
|
+
return this;
|
|
2843
|
+
}
|
|
2848
2844
|
/**
|
|
2849
2845
|
* Initialize a new database connection
|
|
2850
2846
|
*
|
|
@@ -2934,12 +2930,12 @@ var arquebus = class arquebus {
|
|
|
2934
2930
|
const tsPath = path.resolve("arquebus.config.ts");
|
|
2935
2931
|
const instance = this.getInstance();
|
|
2936
2932
|
if (existsSync(jsPath)) {
|
|
2937
|
-
config = (await
|
|
2933
|
+
config = (await importFile(jsPath)).default;
|
|
2938
2934
|
if (addConnection) instance.addConnection(config, config.client);
|
|
2939
2935
|
return config;
|
|
2940
2936
|
}
|
|
2941
2937
|
if (existsSync(tsPath)) if (process.env.NODE_ENV !== "production") {
|
|
2942
|
-
config = (await
|
|
2938
|
+
config = (await importFile(tsPath)).default;
|
|
2943
2939
|
if (addConnection) instance.addConnection(config, config.client);
|
|
2944
2940
|
return config;
|
|
2945
2941
|
} else throw new Error("arquebus.config.ts found in production without build step");
|
|
@@ -2955,7 +2951,7 @@ var arquebus = class arquebus {
|
|
|
2955
2951
|
"cjs"
|
|
2956
2952
|
], dir);
|
|
2957
2953
|
if (found) if (!found.endsWith(".ts") || process.env.NODE_ENV !== "production") {
|
|
2958
|
-
config = (await
|
|
2954
|
+
config = (await importFile(found)).default;
|
|
2959
2955
|
if (addConnection) instance.addConnection(config, config.client);
|
|
2960
2956
|
return config;
|
|
2961
2957
|
} else throw new Error("arquebus.config.ts found in production without build step");
|
|
@@ -2976,7 +2972,7 @@ var arquebus = class arquebus {
|
|
|
2976
2972
|
}
|
|
2977
2973
|
async destroyAll() {
|
|
2978
2974
|
await Promise.all(Object.values(this.manager).map((connection) => {
|
|
2979
|
-
return connection
|
|
2975
|
+
return connection?.destroy();
|
|
2980
2976
|
}));
|
|
2981
2977
|
}
|
|
2982
2978
|
createModel(name, options = {}) {
|
|
@@ -2985,29 +2981,25 @@ var arquebus = class arquebus {
|
|
|
2985
2981
|
this.models = {
|
|
2986
2982
|
...this.models,
|
|
2987
2983
|
[name]: class extends BaseModel {
|
|
2988
|
-
table =
|
|
2989
|
-
connection =
|
|
2990
|
-
timestamps =
|
|
2991
|
-
primaryKey =
|
|
2992
|
-
keyType =
|
|
2993
|
-
incrementing =
|
|
2994
|
-
with =
|
|
2995
|
-
casts =
|
|
2996
|
-
static CREATED_AT =
|
|
2997
|
-
static UPDATED_AT =
|
|
2998
|
-
static DELETED_AT =
|
|
2984
|
+
table = options?.table ?? null;
|
|
2985
|
+
connection = options?.connection ?? null;
|
|
2986
|
+
timestamps = options?.timestamps ?? true;
|
|
2987
|
+
primaryKey = options?.primaryKey ?? "id";
|
|
2988
|
+
keyType = options?.keyType ?? "int";
|
|
2989
|
+
incrementing = options?.incrementing ?? true;
|
|
2990
|
+
with = options?.with ?? [];
|
|
2991
|
+
casts = options?.casts ?? {};
|
|
2992
|
+
static CREATED_AT = options?.CREATED_AT ?? "created_at";
|
|
2993
|
+
static UPDATED_AT = options?.UPDATED_AT ?? "updated_at";
|
|
2994
|
+
static DELETED_AT = options?.DELETED_AT ?? "deleted_at";
|
|
2999
2995
|
}
|
|
3000
2996
|
};
|
|
3001
2997
|
if ("attributes" in options) for (const attribute in options.attributes) {
|
|
3002
2998
|
if (options.attributes[attribute] instanceof Attribute === false) throw new Error("Attribute must be an instance of \"Attribute\"");
|
|
3003
|
-
this.models[name].prototype[getAttrMethod(attribute)] = () =>
|
|
3004
|
-
var _options$attributes;
|
|
3005
|
-
return (_options$attributes = options.attributes) === null || _options$attributes === void 0 ? void 0 : _options$attributes[attribute];
|
|
3006
|
-
};
|
|
2999
|
+
this.models[name].prototype[getAttrMethod(attribute)] = () => options.attributes?.[attribute];
|
|
3007
3000
|
}
|
|
3008
3001
|
if ("relations" in options) for (const relation in options.relations) this.models[name].prototype[getRelationMethod(relation)] = function() {
|
|
3009
|
-
|
|
3010
|
-
return (_options$relations = options.relations) === null || _options$relations === void 0 ? void 0 : _options$relations[relation](this);
|
|
3002
|
+
return options.relations?.[relation](this);
|
|
3011
3003
|
};
|
|
3012
3004
|
if ("scopes" in options) for (const scope in options.scopes) this.models[name].prototype[getScopeMethod(scope)] = options.scopes[scope];
|
|
3013
3005
|
this.models[name].setConnectionResolver(this);
|
|
@@ -3045,6 +3037,9 @@ var Model = class Model extends BaseModel {
|
|
|
3045
3037
|
this.buildRelationships(attributes);
|
|
3046
3038
|
return this.asProxy();
|
|
3047
3039
|
}
|
|
3040
|
+
static define() {
|
|
3041
|
+
return this;
|
|
3042
|
+
}
|
|
3048
3043
|
static query(trx = null) {
|
|
3049
3044
|
return new this().newQuery(trx);
|
|
3050
3045
|
}
|
|
@@ -3228,7 +3223,7 @@ var Model = class Model extends BaseModel {
|
|
|
3228
3223
|
});
|
|
3229
3224
|
}
|
|
3230
3225
|
toData() {
|
|
3231
|
-
return
|
|
3226
|
+
return Obj.deepMerge(this.attributesToData(), this.relationsToData());
|
|
3232
3227
|
}
|
|
3233
3228
|
toJSON() {
|
|
3234
3229
|
return this.toData();
|
|
@@ -3239,6 +3234,15 @@ var Model = class Model extends BaseModel {
|
|
|
3239
3234
|
toString() {
|
|
3240
3235
|
return this.toJson();
|
|
3241
3236
|
}
|
|
3237
|
+
getAttributes() {
|
|
3238
|
+
return super.getAttributes();
|
|
3239
|
+
}
|
|
3240
|
+
getAttribute(key) {
|
|
3241
|
+
return super.getAttribute(key);
|
|
3242
|
+
}
|
|
3243
|
+
setAttribute(key, value) {
|
|
3244
|
+
return super.setAttribute(key, value);
|
|
3245
|
+
}
|
|
3242
3246
|
fill(attributes) {
|
|
3243
3247
|
for (const key in attributes) this.setAttribute(key, attributes[key]);
|
|
3244
3248
|
return this;
|
|
@@ -3279,10 +3283,9 @@ var Model = class Model extends BaseModel {
|
|
|
3279
3283
|
if (this.usesTimestamps()) this.updateTimestamps();
|
|
3280
3284
|
const attributes = this.getAttributes();
|
|
3281
3285
|
if (this.getIncrementing()) {
|
|
3282
|
-
var _data$;
|
|
3283
3286
|
const keyName = this.getKeyName();
|
|
3284
3287
|
const data = await query.insert([attributes], [keyName]);
|
|
3285
|
-
this.setAttribute(keyName,
|
|
3288
|
+
this.setAttribute(keyName, data[0]?.[keyName] || data[0]);
|
|
3286
3289
|
} else if (Object.keys(attributes).length > 0) await query.insert(attributes);
|
|
3287
3290
|
this.exists = true;
|
|
3288
3291
|
await this.execHooks("created", options);
|
|
@@ -3324,7 +3327,7 @@ var Model = class Model extends BaseModel {
|
|
|
3324
3327
|
if (!this.exists) return Promise.resolve(void 0);
|
|
3325
3328
|
const model = await this.constructor.query().where(this.getKeyName(), this.getKey()).first();
|
|
3326
3329
|
this.attributes = { ...model.attributes };
|
|
3327
|
-
await this.load(collect
|
|
3330
|
+
await this.load(collect(this.relations).reject((relation) => {
|
|
3328
3331
|
return relation instanceof Pivot;
|
|
3329
3332
|
}).keys().all());
|
|
3330
3333
|
this.syncOriginal();
|
|
@@ -3388,7 +3391,7 @@ var Pivot = class extends Model {
|
|
|
3388
3391
|
};
|
|
3389
3392
|
//#endregion
|
|
3390
3393
|
//#region src/collection.ts
|
|
3391
|
-
var Collection$1 = class
|
|
3394
|
+
var Collection$1 = class extends Collection {
|
|
3392
3395
|
newConstructor(...args) {
|
|
3393
3396
|
return new (this.getConstructor())(...args);
|
|
3394
3397
|
}
|
|
@@ -3397,17 +3400,18 @@ var Collection$1 = class Collection$1 extends Collection {
|
|
|
3397
3400
|
}
|
|
3398
3401
|
async load(...relations) {
|
|
3399
3402
|
if (this.isNotEmpty()) {
|
|
3400
|
-
const items = await this.first().constructor.query().with(...relations).eagerLoadRelations(this.
|
|
3403
|
+
const items = await this.first().constructor.query().with(...relations).eagerLoadRelations(this.all());
|
|
3401
3404
|
return this.newConstructor(items);
|
|
3402
3405
|
}
|
|
3403
3406
|
return this;
|
|
3404
3407
|
}
|
|
3405
3408
|
async loadAggregate(relations, column, action = null) {
|
|
3406
3409
|
if (this.isEmpty()) return this;
|
|
3407
|
-
const
|
|
3408
|
-
const
|
|
3410
|
+
const first = this.first();
|
|
3411
|
+
const models = (await first.newModelQuery().whereIn(first.getKeyName(), this.modelKeys()).select(first.getKeyName()).withAggregate(relations, column, action).get()).keyBy(first.getKeyName());
|
|
3412
|
+
const attributes = collect(Object.keys(models.first().getAttributes())).diff([models.first().getKeyName()]).all();
|
|
3409
3413
|
this.each((model) => {
|
|
3410
|
-
const extraAttributes =
|
|
3414
|
+
const extraAttributes = Arr.select(models.get(model.getKey()).getAttributes(), attributes);
|
|
3411
3415
|
model.fill(extraAttributes).syncOriginalAttributes(...attributes);
|
|
3412
3416
|
});
|
|
3413
3417
|
return this;
|
|
@@ -3445,20 +3449,21 @@ var Collection$1 = class Collection$1 extends Collection {
|
|
|
3445
3449
|
diff(items) {
|
|
3446
3450
|
const diff = new this.constructor();
|
|
3447
3451
|
const dictionary = this.getDictionary(items);
|
|
3448
|
-
this.
|
|
3452
|
+
this.all().map((item) => {
|
|
3449
3453
|
if (dictionary[item.getKey()] === void 0) diff.add(item);
|
|
3450
3454
|
});
|
|
3451
3455
|
return diff;
|
|
3452
3456
|
}
|
|
3453
|
-
except(keys) {
|
|
3454
|
-
const
|
|
3457
|
+
except(...keys) {
|
|
3458
|
+
const values = keys.length === 1 && Array.isArray(keys[0]) ? keys[0].map(String) : keys.map(String);
|
|
3459
|
+
const dictionary = Arr.except(this.getDictionary(), values);
|
|
3455
3460
|
return new this.constructor(Object.values(dictionary));
|
|
3456
3461
|
}
|
|
3457
3462
|
intersect(items) {
|
|
3458
3463
|
const intersect = new this.constructor();
|
|
3459
|
-
if (isEmpty(items)) return intersect;
|
|
3464
|
+
if (Arr.isEmpty(items)) return intersect;
|
|
3460
3465
|
const dictionary = this.getDictionary(items);
|
|
3461
|
-
for (const item of this.
|
|
3466
|
+
for (const item of this.all()) if (dictionary[item.getKey()] !== void 0) intersect.add(item);
|
|
3462
3467
|
return intersect;
|
|
3463
3468
|
}
|
|
3464
3469
|
unique(key, _strict = false) {
|
|
@@ -3467,20 +3472,21 @@ var Collection$1 = class Collection$1 extends Collection {
|
|
|
3467
3472
|
}
|
|
3468
3473
|
find(key, defaultValue = null) {
|
|
3469
3474
|
if (key instanceof Model) key = key.getKey();
|
|
3470
|
-
if (isArray(key)) {
|
|
3475
|
+
if (Array.isArray(key)) {
|
|
3471
3476
|
if (this.isEmpty()) return new this.constructor();
|
|
3472
3477
|
return this.whereIn(this.first().getKeyName(), key);
|
|
3473
3478
|
}
|
|
3474
|
-
collect(this.
|
|
3479
|
+
collect(this.all()).first((model) => {
|
|
3475
3480
|
return model.getKey() == key;
|
|
3476
3481
|
});
|
|
3477
|
-
return this.
|
|
3482
|
+
return this.all().filter((model) => {
|
|
3478
3483
|
return model.getKey() == key;
|
|
3479
3484
|
})[0] || defaultValue;
|
|
3480
3485
|
}
|
|
3481
3486
|
async fresh(...args) {
|
|
3482
3487
|
if (this.isEmpty()) return new this.constructor();
|
|
3483
3488
|
const model = this.first();
|
|
3489
|
+
if (!model) return new this.constructor();
|
|
3484
3490
|
const freshModels = (await model.newQuery().with(...args).whereIn(model.getKeyName(), this.modelKeys()).get()).getDictionary();
|
|
3485
3491
|
return this.filter((model) => {
|
|
3486
3492
|
return model.exists && freshModels[model.getKey()] !== void 0;
|
|
@@ -3503,15 +3509,15 @@ var Collection$1 = class Collection$1 extends Collection {
|
|
|
3503
3509
|
item.append(attributes);
|
|
3504
3510
|
});
|
|
3505
3511
|
}
|
|
3506
|
-
only(keys) {
|
|
3507
|
-
|
|
3508
|
-
const dictionary =
|
|
3512
|
+
only(...keys) {
|
|
3513
|
+
const values = keys.length === 1 && Array.isArray(keys[0]) ? keys[0].map(String) : keys.map(String);
|
|
3514
|
+
const dictionary = Arr.select(this.getDictionary(), values);
|
|
3509
3515
|
return new this.constructor(Object.values(dictionary));
|
|
3510
3516
|
}
|
|
3511
3517
|
getDictionary(items) {
|
|
3512
|
-
|
|
3518
|
+
const values = !items ? this.all() : items instanceof Collection ? items.all() : items;
|
|
3513
3519
|
const dictionary = {};
|
|
3514
|
-
|
|
3520
|
+
values.map((value) => {
|
|
3515
3521
|
dictionary[value.getKey()] = value;
|
|
3516
3522
|
});
|
|
3517
3523
|
return dictionary;
|
|
@@ -3535,8 +3541,8 @@ var Collection$1 = class Collection$1 extends Collection {
|
|
|
3535
3541
|
return JSON.stringify(this.toData(), ...args);
|
|
3536
3542
|
}
|
|
3537
3543
|
[Symbol.iterator] = () => {
|
|
3538
|
-
const items = this.
|
|
3539
|
-
const length =
|
|
3544
|
+
const items = this.all();
|
|
3545
|
+
const length = items.length;
|
|
3540
3546
|
let n = 0;
|
|
3541
3547
|
return { next() {
|
|
3542
3548
|
return n < length ? {
|
|
@@ -3583,12 +3589,12 @@ const InteractsWithPivotTable = (Relation) => {
|
|
|
3583
3589
|
let records;
|
|
3584
3590
|
const results = await this.getCurrentlyAttachedPivots();
|
|
3585
3591
|
const current = results.length === 0 ? [] : results.map((result) => result.toData()).pluck(this.relatedPivotKey).all().map((i) => String(i));
|
|
3586
|
-
const detach = diff(
|
|
3592
|
+
const detach = collect(current).diff(Object.keys(records = this.formatRecordsList(this.parseIds(ids)))).all();
|
|
3587
3593
|
if (detaching && detach.length > 0) {
|
|
3588
3594
|
await this.detach(detach);
|
|
3589
3595
|
changes.detached = this.castKeys(detach);
|
|
3590
3596
|
}
|
|
3591
|
-
changes =
|
|
3597
|
+
changes = Obj.deepMerge(changes, await this.attachNew(records, current, false));
|
|
3592
3598
|
return changes;
|
|
3593
3599
|
}
|
|
3594
3600
|
syncWithoutDetaching(ids) {
|
|
@@ -3596,11 +3602,11 @@ const InteractsWithPivotTable = (Relation) => {
|
|
|
3596
3602
|
}
|
|
3597
3603
|
syncWithPivotValues(ids, values, detaching = true) {
|
|
3598
3604
|
return this.sync(collect(this.parseIds(ids)).mapWithKeys((id) => {
|
|
3599
|
-
return [id, values];
|
|
3605
|
+
return [String(id), values];
|
|
3600
3606
|
}), detaching);
|
|
3601
3607
|
}
|
|
3602
3608
|
withPivot(columns) {
|
|
3603
|
-
this.pivotColumns = this.pivotColumns.concat(isArray(columns) ? columns : Array.prototype.slice.call(columns));
|
|
3609
|
+
this.pivotColumns = this.pivotColumns.concat(Array.isArray(columns) ? columns : Array.prototype.slice.call(columns));
|
|
3604
3610
|
return this;
|
|
3605
3611
|
}
|
|
3606
3612
|
async attachNew(records, current, touch = true) {
|
|
@@ -3637,8 +3643,8 @@ const InteractsWithPivotTable = (Relation) => {
|
|
|
3637
3643
|
}
|
|
3638
3644
|
formatRecordsList(records) {
|
|
3639
3645
|
return collect(records).mapWithKeys((attributes, id) => {
|
|
3640
|
-
if (!isArray(attributes)) [id, attributes] = [attributes, {}];
|
|
3641
|
-
return [id, attributes];
|
|
3646
|
+
if (!Array.isArray(attributes)) [id, attributes] = [attributes, {}];
|
|
3647
|
+
return [String(id), attributes];
|
|
3642
3648
|
}).all();
|
|
3643
3649
|
}
|
|
3644
3650
|
async getCurrentlyAttachedPivots() {
|
|
@@ -3708,7 +3714,7 @@ const InteractsWithPivotTable = (Relation) => {
|
|
|
3708
3714
|
}
|
|
3709
3715
|
formatAttachRecord(key, value, attributes, hasTimestamps) {
|
|
3710
3716
|
const [id, newAttributes] = this.extractAttachIdAndAttributes(key, value, attributes);
|
|
3711
|
-
return
|
|
3717
|
+
return Obj.deepMerge(this.baseAttachRecord(id, hasTimestamps), newAttributes);
|
|
3712
3718
|
}
|
|
3713
3719
|
baseAttachRecord(id, timed) {
|
|
3714
3720
|
let record = {};
|
|
@@ -3721,7 +3727,7 @@ const InteractsWithPivotTable = (Relation) => {
|
|
|
3721
3727
|
return record;
|
|
3722
3728
|
}
|
|
3723
3729
|
extractAttachIdAndAttributes(key, value, newAttributes) {
|
|
3724
|
-
return isArray(value) ? [key, {
|
|
3730
|
+
return Array.isArray(value) ? [key, {
|
|
3725
3731
|
...value,
|
|
3726
3732
|
...newAttributes
|
|
3727
3733
|
}] : [value, newAttributes];
|
|
@@ -3732,7 +3738,7 @@ const InteractsWithPivotTable = (Relation) => {
|
|
|
3732
3738
|
parseIds(value) {
|
|
3733
3739
|
if (value instanceof Model) return [value[this.relatedKey]];
|
|
3734
3740
|
if (value instanceof Collection$1) return value.pluck(this.relatedKey).all();
|
|
3735
|
-
return isArray(value) ? value : [value];
|
|
3741
|
+
return Array.isArray(value) ? value : [value];
|
|
3736
3742
|
}
|
|
3737
3743
|
};
|
|
3738
3744
|
};
|
|
@@ -3792,9 +3798,8 @@ var BelongsToMany = class extends compose(Relation, InteractsWithPivotTable) {
|
|
|
3792
3798
|
return this;
|
|
3793
3799
|
}
|
|
3794
3800
|
async get(columns) {
|
|
3795
|
-
var _builder$query;
|
|
3796
3801
|
const builder = this.query.applyScopes();
|
|
3797
|
-
columns =
|
|
3802
|
+
columns = builder.query?._statements?.find((item) => item.grouping == "columns") ? [] : columns;
|
|
3798
3803
|
let models = await builder.select(this.shouldSelect(columns)).getModels();
|
|
3799
3804
|
this.hydratePivotRelation(models);
|
|
3800
3805
|
if (models.length > 0) models = await builder.eagerLoadRelations(models);
|
|
@@ -3843,7 +3848,7 @@ var BelongsToMany = class extends compose(Relation, InteractsWithPivotTable) {
|
|
|
3843
3848
|
const value = model.attributes[key];
|
|
3844
3849
|
if (key.startsWith("pivot_")) {
|
|
3845
3850
|
values[key.substring(6)] = value;
|
|
3846
|
-
model.attributes =
|
|
3851
|
+
model.attributes = Arr.except(model.attributes, [key]);
|
|
3847
3852
|
}
|
|
3848
3853
|
}
|
|
3849
3854
|
return values;
|
|
@@ -3854,7 +3859,7 @@ var BelongsToMany = class extends compose(Relation, InteractsWithPivotTable) {
|
|
|
3854
3859
|
return this.withPivot(this.createdAt(), this.updatedAt());
|
|
3855
3860
|
}
|
|
3856
3861
|
shouldSelect(columns = ["*"]) {
|
|
3857
|
-
if (
|
|
3862
|
+
if (columns.length === 1 && columns[0] === "*") columns = [this.related.getTable() + ".*"];
|
|
3858
3863
|
return columns.concat(this.aliasedPivotColumns());
|
|
3859
3864
|
}
|
|
3860
3865
|
aliasedPivotColumns() {
|
|
@@ -3978,9 +3983,8 @@ var Builder = class Builder extends Inference {
|
|
|
3978
3983
|
}
|
|
3979
3984
|
asProxy() {
|
|
3980
3985
|
return new Proxy(this, { get(target, prop) {
|
|
3981
|
-
var _target$query$connect;
|
|
3982
3986
|
if (typeof target[prop] !== "undefined") return target[prop];
|
|
3983
|
-
const skipReturning = !!
|
|
3987
|
+
const skipReturning = !!target.query.connector?.client.config?.client?.includes("mysql") && prop === "returning";
|
|
3984
3988
|
if ([
|
|
3985
3989
|
"select",
|
|
3986
3990
|
"from",
|
|
@@ -4074,7 +4078,7 @@ var Builder = class Builder extends Inference {
|
|
|
4074
4078
|
};
|
|
4075
4079
|
}
|
|
4076
4080
|
if (prop.startsWith("where")) {
|
|
4077
|
-
const column = snake(prop.substring(5));
|
|
4081
|
+
const column = Str.snake(prop.substring(5));
|
|
4078
4082
|
return (...args) => {
|
|
4079
4083
|
target.query.where(column, ...args);
|
|
4080
4084
|
return target.asProxy();
|
|
@@ -4149,7 +4153,8 @@ var Builder = class Builder extends Inference {
|
|
|
4149
4153
|
}
|
|
4150
4154
|
addUpdatedAtColumn(values) {
|
|
4151
4155
|
if (!this.model.usesTimestamps() || this.model.getUpdatedAtColumn() === null) return values;
|
|
4152
|
-
|
|
4156
|
+
const column = this.model.getUpdatedAtColumn();
|
|
4157
|
+
values = Obj.deepMerge({ [column]: this.model.freshTimestampString() }, values);
|
|
4153
4158
|
return values;
|
|
4154
4159
|
}
|
|
4155
4160
|
delete() {
|
|
@@ -4177,9 +4182,8 @@ var Builder = class Builder extends Inference {
|
|
|
4177
4182
|
return this.model;
|
|
4178
4183
|
}
|
|
4179
4184
|
setModel(model) {
|
|
4180
|
-
var _this$query;
|
|
4181
4185
|
this.model = model;
|
|
4182
|
-
if (typeof
|
|
4186
|
+
if (typeof this.query?.client?.table == "function") this.query = this.query.client.table(this.model.getTable());
|
|
4183
4187
|
else this.query = this.query.table(this.model.getTable());
|
|
4184
4188
|
return this;
|
|
4185
4189
|
}
|
|
@@ -4222,7 +4226,7 @@ var Builder = class Builder extends Inference {
|
|
|
4222
4226
|
}
|
|
4223
4227
|
withoutGlobalScope(scope) {
|
|
4224
4228
|
if (typeof scope !== "string") scope = scope.constructor.name;
|
|
4225
|
-
this.globalScopes =
|
|
4229
|
+
this.globalScopes = Arr.except(this.globalScopes, [scope]);
|
|
4226
4230
|
return this;
|
|
4227
4231
|
}
|
|
4228
4232
|
macro(name, callback) {
|
|
@@ -4239,7 +4243,7 @@ var Builder = class Builder extends Inference {
|
|
|
4239
4243
|
let eagerLoads = {};
|
|
4240
4244
|
if (typeof args[1] === "function") {
|
|
4241
4245
|
const eagerLoad = this.parseWithRelations({ [args[0]]: args[1] });
|
|
4242
|
-
this.eagerLoad =
|
|
4246
|
+
this.eagerLoad = Obj.deepMerge(this.eagerLoad, eagerLoad);
|
|
4243
4247
|
return this;
|
|
4244
4248
|
}
|
|
4245
4249
|
const relations = flattenDeep(args);
|
|
@@ -4248,13 +4252,13 @@ var Builder = class Builder extends Inference {
|
|
|
4248
4252
|
let eagerLoad;
|
|
4249
4253
|
if (typeof relation === "string") eagerLoad = { [relation]: (q) => q };
|
|
4250
4254
|
else if (typeof relation === "object") eagerLoad = relation;
|
|
4251
|
-
eagerLoads =
|
|
4255
|
+
eagerLoads = Obj.deepMerge(eagerLoads, eagerLoad);
|
|
4252
4256
|
}
|
|
4253
|
-
this.eagerLoad =
|
|
4257
|
+
this.eagerLoad = Obj.deepMerge(this.eagerLoad, this.parseWithRelations(eagerLoads));
|
|
4254
4258
|
return this;
|
|
4255
4259
|
}
|
|
4256
4260
|
has(relation, operator = ">=", count = 1, boolean = "and", callback = null) {
|
|
4257
|
-
if (
|
|
4261
|
+
if (typeof relation === "string") {
|
|
4258
4262
|
if (relation.includes(".")) return this.hasNested(relation, operator, count, boolean, callback);
|
|
4259
4263
|
relation = this.getRelationWithoutConstraints(getRelationMethod(relation));
|
|
4260
4264
|
}
|
|
@@ -4331,7 +4335,7 @@ var Builder = class Builder extends Inference {
|
|
|
4331
4335
|
let eagerLoad;
|
|
4332
4336
|
if (typeof relation === "string") eagerLoad = { [relation]: (q) => q };
|
|
4333
4337
|
else if (typeof relation === "object") eagerLoad = relation;
|
|
4334
|
-
eagerLoads =
|
|
4338
|
+
eagerLoads = Obj.deepMerge(eagerLoads, eagerLoad);
|
|
4335
4339
|
}
|
|
4336
4340
|
relations = eagerLoads;
|
|
4337
4341
|
const db = this.model.getConnection();
|
|
@@ -4350,7 +4354,7 @@ var Builder = class Builder extends Inference {
|
|
|
4350
4354
|
} else expression = column;
|
|
4351
4355
|
const query = relation.getRelationExistenceQuery(relation.getRelated().newModelQuery(), this, db.raw(expression));
|
|
4352
4356
|
constraints(query);
|
|
4353
|
-
alias = alias || snake(`${name} ${action} ${column}`.replace("/[^[:alnum:][:space:]_]/u", ""));
|
|
4357
|
+
alias = alias || Str.snake(`${name} ${action} ${column}`.replace("/[^[:alnum:][:space:]_]/u", ""));
|
|
4354
4358
|
if (action === "exists") this.select(db.raw(`exists(${query.toSql().sql}) as ${alias}`));
|
|
4355
4359
|
else this.selectSub(action ? query : query.limit(1), alias);
|
|
4356
4360
|
}
|
|
@@ -4374,7 +4378,7 @@ var Builder = class Builder extends Inference {
|
|
|
4374
4378
|
}
|
|
4375
4379
|
parseSub(query) {
|
|
4376
4380
|
if (query instanceof Builder || query instanceof Relation) return [query.toSql().sql, query.toSql().bindings];
|
|
4377
|
-
else if (
|
|
4381
|
+
else if (typeof query === "string") return [query, []];
|
|
4378
4382
|
else throw new Error("A subquery must be a query builder instance, a Closure, or a string.");
|
|
4379
4383
|
}
|
|
4380
4384
|
prependDatabaseNameIfCrossDatabaseQuery(query) {
|
|
@@ -4431,15 +4435,15 @@ var Builder = class Builder extends Inference {
|
|
|
4431
4435
|
if (prefix !== "") prefix += ".";
|
|
4432
4436
|
for (const key in relations) {
|
|
4433
4437
|
const value = relations[key];
|
|
4434
|
-
if (
|
|
4438
|
+
if (typeof value === "string" || Number.isFinite(parseInt(value))) continue;
|
|
4435
4439
|
const [attribute, attributeSelectConstraint] = this.parseNameAndAttributeSelectionConstraint(key, value);
|
|
4436
4440
|
preparedRelationships = Object.assign({}, preparedRelationships, { [`${prefix}${attribute}`]: attributeSelectConstraint }, this.prepareNestedWithRelationships(value, `${prefix}${attribute}`));
|
|
4437
|
-
relations =
|
|
4441
|
+
relations = Arr.except(relations, [key]);
|
|
4438
4442
|
}
|
|
4439
4443
|
for (const key in relations) {
|
|
4440
4444
|
const value = relations[key];
|
|
4441
4445
|
let attribute = key, attributeSelectConstraint = value;
|
|
4442
|
-
if (
|
|
4446
|
+
if (typeof value === "string") [attribute, attributeSelectConstraint] = this.parseNameAndAttributeSelectionConstraint(value);
|
|
4443
4447
|
preparedRelationships[`${prefix}${attribute}`] = this.combineConstraints([attributeSelectConstraint, preparedRelationships[`${prefix}${attribute}`] || (() => {})]);
|
|
4444
4448
|
}
|
|
4445
4449
|
return preparedRelationships;
|
|
@@ -4487,8 +4491,8 @@ var Builder = class Builder extends Inference {
|
|
|
4487
4491
|
}
|
|
4488
4492
|
async findOrFail(...args) {
|
|
4489
4493
|
const data = await this.find(...args);
|
|
4490
|
-
if (isArray(args[0])) {
|
|
4491
|
-
if (data.count() !== args[0].length) throw new ModelNotFoundError().setModel(this.model.constructor.name,
|
|
4494
|
+
if (Array.isArray(args[0])) {
|
|
4495
|
+
if (data.count() !== args[0].length) throw new ModelNotFoundError().setModel(this.model.constructor.name, collect(args[0]).diff(data.modelKeys()).all());
|
|
4492
4496
|
return data;
|
|
4493
4497
|
}
|
|
4494
4498
|
if (data === null) throw new ModelNotFoundError().setModel(this.model.constructor.name, args[0]);
|
|
@@ -4502,12 +4506,12 @@ var Builder = class Builder extends Inference {
|
|
|
4502
4506
|
async firstOrNew(attributes = {}, values = {}) {
|
|
4503
4507
|
const instance = await this.where(attributes).first();
|
|
4504
4508
|
if (instance !== null) return instance;
|
|
4505
|
-
return this.newModelInstance(
|
|
4509
|
+
return this.newModelInstance(Obj.deepMerge(attributes, values));
|
|
4506
4510
|
}
|
|
4507
4511
|
async firstOrCreate(attributes = {}, values = {}) {
|
|
4508
4512
|
const instance = await this.where(attributes).first();
|
|
4509
4513
|
if (instance !== null) return instance;
|
|
4510
|
-
return tap(this.newModelInstance(
|
|
4514
|
+
return tap(this.newModelInstance(Obj.deepMerge(attributes, values)), async (instance) => {
|
|
4511
4515
|
await instance.save({ client: this.query });
|
|
4512
4516
|
});
|
|
4513
4517
|
}
|
|
@@ -4527,14 +4531,13 @@ var Builder = class Builder extends Inference {
|
|
|
4527
4531
|
return this;
|
|
4528
4532
|
}
|
|
4529
4533
|
async find(id, columns) {
|
|
4530
|
-
if (isArray(id) || id instanceof Collection$1) return await this.findMany(id, columns);
|
|
4534
|
+
if (Array.isArray(id) || id instanceof Collection$1) return await this.findMany(id, columns);
|
|
4531
4535
|
return await this.where(this.model.getKeyName(), id).first(columns);
|
|
4532
4536
|
}
|
|
4533
4537
|
async findMany(ids, columns = ["*"]) {
|
|
4534
|
-
|
|
4535
|
-
|
|
4536
|
-
|
|
4537
|
-
return await this.whereIn(this.model.getKeyName(), ids).get(columns);
|
|
4538
|
+
const values = ids instanceof Collection$1 ? ids.modelKeys() : Array.isArray(ids) ? ids : [ids];
|
|
4539
|
+
if (values.length === 0) return new Collection$1([]);
|
|
4540
|
+
return await this.whereIn(this.model.getKeyName(), values).get(columns);
|
|
4538
4541
|
}
|
|
4539
4542
|
async pluck(column) {
|
|
4540
4543
|
return new Collection$1(await this.query.pluck(column));
|
|
@@ -4542,7 +4545,7 @@ var Builder = class Builder extends Inference {
|
|
|
4542
4545
|
async destroy(ids) {
|
|
4543
4546
|
if (ids instanceof Collection$1) ids = ids.modelKeys();
|
|
4544
4547
|
if (ids instanceof Collection) ids = ids.all();
|
|
4545
|
-
ids = isArray(ids) ? ids : Array.prototype.slice.call(ids);
|
|
4548
|
+
ids = Array.isArray(ids) ? ids : Array.prototype.slice.call(ids);
|
|
4546
4549
|
if (ids.length === 0) return 0;
|
|
4547
4550
|
const key = this.model.newInstance().getKeyName();
|
|
4548
4551
|
let count = 0;
|
|
@@ -4560,12 +4563,11 @@ var Builder = class Builder extends Inference {
|
|
|
4560
4563
|
return await this.model.newModelQuery().get(columns);
|
|
4561
4564
|
}
|
|
4562
4565
|
async paginate(page = 1, perPage = 10) {
|
|
4563
|
-
var _this;
|
|
4564
4566
|
page = page || 1;
|
|
4565
|
-
perPage = perPage ||
|
|
4567
|
+
perPage = perPage || this?.model?.perPage || 15;
|
|
4566
4568
|
this.applyScopes();
|
|
4567
4569
|
const total = await this.query.clone().clearOrder().clearSelect().count(this.primaryKey);
|
|
4568
|
-
let results
|
|
4570
|
+
let results;
|
|
4569
4571
|
if (total > 0) {
|
|
4570
4572
|
const skip = (page - 1) * (perPage ?? 10);
|
|
4571
4573
|
this.take(perPage).skip(skip);
|
|
@@ -4575,7 +4577,7 @@ var Builder = class Builder extends Inference {
|
|
|
4575
4577
|
return new Paginator(results, parseInt(total), perPage, page);
|
|
4576
4578
|
}
|
|
4577
4579
|
async getModels(...columns) {
|
|
4578
|
-
columns =
|
|
4580
|
+
columns = flatten(columns);
|
|
4579
4581
|
if (columns.length > 0) {
|
|
4580
4582
|
if (this.query._statements.filter((item) => item.grouping == "columns").length == 0 && columns[0] !== "*") this.query.select(...columns);
|
|
4581
4583
|
}
|
|
@@ -4692,11 +4694,11 @@ var SQLite = class {
|
|
|
4692
4694
|
is_generated: raw.hidden !== 0,
|
|
4693
4695
|
generation_expression: null,
|
|
4694
4696
|
is_nullable: raw.notnull === 0,
|
|
4695
|
-
is_unique: !!
|
|
4697
|
+
is_unique: !!index?.unique && indexInfo?.length === 1,
|
|
4696
4698
|
is_primary_key: raw.pk === 1,
|
|
4697
4699
|
has_auto_increment: raw.pk === 1 && tablesWithAutoIncrementPrimaryKeys.includes(table),
|
|
4698
|
-
foreign_key_column:
|
|
4699
|
-
foreign_key_table:
|
|
4700
|
+
foreign_key_column: foreignKey?.to || null,
|
|
4701
|
+
foreign_key_table: foreignKey?.table || null
|
|
4700
4702
|
};
|
|
4701
4703
|
});
|
|
4702
4704
|
};
|