orchid-orm 1.75.0 → 1.76.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +55 -36
- package/dist/index.js +17 -5
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +17 -5
- 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({
|
|
@@ -1479,6 +1490,7 @@ const assignTablesToOrm = (isTable, tables, result, adapter, qb, asyncStorage, c
|
|
|
1479
1490
|
scopes: table.scopes,
|
|
1480
1491
|
softDelete: table.softDelete,
|
|
1481
1492
|
snakeCase: table.snakeCase,
|
|
1493
|
+
nameInDb: table.nameInDb,
|
|
1482
1494
|
comment: table.comment,
|
|
1483
1495
|
readOnly: isTable ? table.readOnly ? true : void 0 : table.materialized || table.readOnly !== false ? true : void 0,
|
|
1484
1496
|
materialized: !isTable && table.materialized ? true : void 0,
|
|
@@ -1608,12 +1620,12 @@ const privateOrchidORMWithAdapter = ({ log, logger, autoPreparedStatements, noPr
|
|
|
1608
1620
|
viewInstances = assignTablesToOrm(false, views, result.$views, adapter, qb, asyncStorage, commonOptions, schema);
|
|
1609
1621
|
const tableDbNames = Object.values(tableInstances).map((table) => {
|
|
1610
1622
|
const s = typeof table.schema === "function" ? table.schema() : table.schema;
|
|
1611
|
-
return `${s ? s + "." : ""}${table.
|
|
1623
|
+
return `${s ? s + "." : ""}${table.nameInDb}`;
|
|
1612
1624
|
});
|
|
1613
1625
|
for (const key in views) {
|
|
1614
1626
|
const view = viewInstances[key];
|
|
1615
1627
|
const s = typeof view.schema === "function" ? view.schema() : view.schema;
|
|
1616
|
-
const name = `${s ? s + "." : ""}${view.
|
|
1628
|
+
const name = `${s ? s + "." : ""}${view.nameInDb}`;
|
|
1617
1629
|
if (tableDbNames.includes(name)) throw new Error(`Cannot configure both a table and a view for database relation ${name}`);
|
|
1618
1630
|
}
|
|
1619
1631
|
}
|