orchid-orm 1.74.0 → 1.76.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/dist/index.d.ts +121 -107
- package/dist/index.js +22 -17
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +22 -17
- package/dist/index.mjs.map +1 -1
- package/dist/migrations/index.js +12 -5
- package/dist/migrations/index.js.map +1 -1
- package/dist/migrations/index.mjs +12 -5
- package/dist/migrations/index.mjs.map +1 -1
- package/package.json +5 -5
package/dist/index.mjs
CHANGED
|
@@ -67,6 +67,8 @@ function createBaseTable({ schemaConfig = defaultSchemaConfig, columnTypes: colu
|
|
|
67
67
|
let instance = instances.get(this);
|
|
68
68
|
if (!instance) {
|
|
69
69
|
instance = new this();
|
|
70
|
+
const name = instance.table || instance.name;
|
|
71
|
+
if (instance.nameInDb === void 0 && name) instance.nameInDb = instance.snakeCase ? toSnakeCase(name) : name;
|
|
70
72
|
instances.set(this, instance);
|
|
71
73
|
}
|
|
72
74
|
return instance;
|
|
@@ -214,7 +216,7 @@ function joinHasRelation(baseQuery, joiningQuery, primaryKeys, foreignKeys, len)
|
|
|
214
216
|
return q;
|
|
215
217
|
}
|
|
216
218
|
const addAutoForeignKey = (tableConfig, from, to, primaryKeys, foreignKeys, options, originalForeignKeys) => {
|
|
217
|
-
const toTable = to.table;
|
|
219
|
+
const toTable = to.q.nameInDb || to.table;
|
|
218
220
|
let fkeyOptions = options.foreignKey !== void 0 ? options.foreignKey : tableConfig.autoForeignKeys;
|
|
219
221
|
if (!fkeyOptions) return;
|
|
220
222
|
if (fkeyOptions === true) fkeyOptions = tableConfig.autoForeignKeys || emptyObject;
|
|
@@ -228,7 +230,10 @@ const addAutoForeignKey = (tableConfig, from, to, primaryKeys, foreignKeys, opti
|
|
|
228
230
|
if (typeof fkey.fnOrTable === "string") {
|
|
229
231
|
fkeyTable = fkey.fnOrTable;
|
|
230
232
|
fkeyColumn = getColumnKeyFromDbName(to, fkeyColumn);
|
|
231
|
-
} else
|
|
233
|
+
} else {
|
|
234
|
+
const instance = fkey.fnOrTable().instance();
|
|
235
|
+
fkeyTable = instance.nameInDb || instance.table;
|
|
236
|
+
}
|
|
232
237
|
if (toTable === fkeyTable && pkey === fkeyColumn) return;
|
|
233
238
|
}
|
|
234
239
|
}
|
|
@@ -239,7 +244,13 @@ const addAutoForeignKey = (tableConfig, from, to, primaryKeys, foreignKeys, opti
|
|
|
239
244
|
const sortedFkeys = [...foreignKeys].sort();
|
|
240
245
|
for (const { references: refs } of constraints) {
|
|
241
246
|
if (!refs) continue;
|
|
242
|
-
|
|
247
|
+
let sameForeignTable = false;
|
|
248
|
+
if (typeof refs.fnOrTable === "string") sameForeignTable = refs.fnOrTable === toTable && refs.foreignColumns.every((column, i) => getColumnKeyFromDbName(to, column) === sortedPkeys[i]);
|
|
249
|
+
else {
|
|
250
|
+
const instance = refs.fnOrTable().instance();
|
|
251
|
+
sameForeignTable = (instance.nameInDb || instance.table) === toTable && refs.foreignColumns.every((column, i) => column === sortedPkeys[i]);
|
|
252
|
+
}
|
|
253
|
+
if (refs.columns.length === sortedFkeys.length && refs.columns.every((column, i) => column === sortedFkeys[i]) && refs.foreignColumns.length === sortedPkeys.length && sameForeignTable) return;
|
|
243
254
|
}
|
|
244
255
|
}
|
|
245
256
|
(from.internal.tableData.constraints ??= []).push({
|
|
@@ -410,9 +421,10 @@ var BelongsToVirtualColumn = class extends VirtualColumn {
|
|
|
410
421
|
this.nestedUpdate(queryForUpdate, set, data);
|
|
411
422
|
}
|
|
412
423
|
};
|
|
413
|
-
const getBelongsToRequired = (tableConfig, relation) => {
|
|
424
|
+
const getBelongsToRequired = (tableConfig, relatedTableConfig, relation) => {
|
|
414
425
|
const { required } = relation.options;
|
|
415
426
|
if (typeof required === "boolean") return required;
|
|
427
|
+
if (relatedTableConfig.softDelete) return false;
|
|
416
428
|
return relation.options.columns.every((key) => {
|
|
417
429
|
return !tableConfig.columns.shape[key].data.isNullable;
|
|
418
430
|
});
|
|
@@ -1359,6 +1371,7 @@ const applyRelations = (qb, tables, result, schema) => {
|
|
|
1359
1371
|
const data = {
|
|
1360
1372
|
relationName,
|
|
1361
1373
|
relation,
|
|
1374
|
+
otherTable: otherTable[1],
|
|
1362
1375
|
dbTable,
|
|
1363
1376
|
otherDbTable
|
|
1364
1377
|
};
|
|
@@ -1402,7 +1415,7 @@ const delayRelation = (delayedRelations, table, relationName, data) => {
|
|
|
1402
1415
|
if (tableRelations[relationName]) tableRelations[relationName].push(data);
|
|
1403
1416
|
else tableRelations[relationName] = [data];
|
|
1404
1417
|
};
|
|
1405
|
-
const applyRelation = (table, qb, { relationName, relation, dbTable, otherDbTable }, delayedRelations, schema) => {
|
|
1418
|
+
const applyRelation = (table, qb, { relationName, relation, otherTable, dbTable, otherDbTable }, delayedRelations, schema) => {
|
|
1406
1419
|
const baseQuery = Object.create(otherDbTable);
|
|
1407
1420
|
baseQuery.baseQuery = baseQuery;
|
|
1408
1421
|
const query = baseQuery.as(relationName);
|
|
@@ -1415,21 +1428,12 @@ const applyRelation = (table, qb, { relationName, relation, dbTable, otherDbTabl
|
|
|
1415
1428
|
else if (type === "hasAndBelongsToMany") data = makeHasAndBelongsToManyMethod(table, dbTable, qb, relation, relationName, query, schema);
|
|
1416
1429
|
else throw new Error(`Unknown relation type ${type}`);
|
|
1417
1430
|
if (data.returns === "one") {
|
|
1418
|
-
if (type === "belongsTo" ? getBelongsToRequired(table, relation) : relation.options.required) _queryTake(query);
|
|
1431
|
+
if (type === "belongsTo" ? getBelongsToRequired(table, otherTable, relation) : relation.options.required) _queryTake(query);
|
|
1419
1432
|
else _queryTakeOptional(query);
|
|
1420
1433
|
query.q.returnsOne = true;
|
|
1421
1434
|
}
|
|
1422
1435
|
if (data.virtualColumn) dbTable.shape[relationName] = dbTable.q.selectShape[relationName] = data.virtualColumn;
|
|
1423
1436
|
baseQuery.joinQuery = data.joinQuery;
|
|
1424
|
-
const { join: originalJoin } = baseQuery;
|
|
1425
|
-
baseQuery.join = function(...args) {
|
|
1426
|
-
if (args.length) return originalJoin.apply(this, args);
|
|
1427
|
-
else {
|
|
1428
|
-
const q = this.clone();
|
|
1429
|
-
q.q.innerJoinLateral = true;
|
|
1430
|
-
return q;
|
|
1431
|
-
}
|
|
1432
|
-
};
|
|
1433
1437
|
dbTable.relations[relationName] = {
|
|
1434
1438
|
table: otherDbTable,
|
|
1435
1439
|
query,
|
|
@@ -1486,6 +1490,7 @@ const assignTablesToOrm = (isTable, tables, result, adapter, qb, asyncStorage, c
|
|
|
1486
1490
|
scopes: table.scopes,
|
|
1487
1491
|
softDelete: table.softDelete,
|
|
1488
1492
|
snakeCase: table.snakeCase,
|
|
1493
|
+
nameInDb: table.nameInDb,
|
|
1489
1494
|
comment: table.comment,
|
|
1490
1495
|
readOnly: isTable ? table.readOnly ? true : void 0 : table.materialized || table.readOnly !== false ? true : void 0,
|
|
1491
1496
|
materialized: !isTable && table.materialized ? true : void 0,
|
|
@@ -1615,12 +1620,12 @@ const privateOrchidORMWithAdapter = ({ log, logger, autoPreparedStatements, noPr
|
|
|
1615
1620
|
viewInstances = assignTablesToOrm(false, views, result.$views, adapter, qb, asyncStorage, commonOptions, schema);
|
|
1616
1621
|
const tableDbNames = Object.values(tableInstances).map((table) => {
|
|
1617
1622
|
const s = typeof table.schema === "function" ? table.schema() : table.schema;
|
|
1618
|
-
return `${s ? s + "." : ""}${table.
|
|
1623
|
+
return `${s ? s + "." : ""}${table.nameInDb}`;
|
|
1619
1624
|
});
|
|
1620
1625
|
for (const key in views) {
|
|
1621
1626
|
const view = viewInstances[key];
|
|
1622
1627
|
const s = typeof view.schema === "function" ? view.schema() : view.schema;
|
|
1623
|
-
const name = `${s ? s + "." : ""}${view.
|
|
1628
|
+
const name = `${s ? s + "." : ""}${view.nameInDb}`;
|
|
1624
1629
|
if (tableDbNames.includes(name)) throw new Error(`Cannot configure both a table and a view for database relation ${name}`);
|
|
1625
1630
|
}
|
|
1626
1631
|
}
|