orchid-orm 1.69.0 → 1.69.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 +24 -11
- package/dist/index.js +71 -32
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +70 -33
- package/dist/index.mjs.map +1 -1
- package/dist/node-postgres.d.ts +5 -4
- package/dist/node-postgres.js +7 -3
- package/dist/node-postgres.js.map +1 -1
- package/dist/node-postgres.mjs +8 -5
- package/dist/node-postgres.mjs.map +1 -1
- package/dist/postgres-js.d.ts +5 -4
- package/dist/postgres-js.js +7 -3
- package/dist/postgres-js.js.map +1 -1
- package/dist/postgres-js.mjs +8 -5
- package/dist/postgres-js.mjs.map +1 -1
- package/package.json +5 -5
package/dist/index.d.ts
CHANGED
|
@@ -174,9 +174,23 @@ declare function afterCommit(this: {
|
|
|
174
174
|
interface FromQuery extends Query {
|
|
175
175
|
returnType: 'all';
|
|
176
176
|
}
|
|
177
|
-
|
|
177
|
+
interface OrchidORMQueryHelper<Q extends Query, Args extends unknown[], Result> {
|
|
178
|
+
<T extends Q>(q: T, ...args: Args): Result;
|
|
179
|
+
isQueryHelper: true;
|
|
180
|
+
table: Q['table'];
|
|
181
|
+
args: Args;
|
|
182
|
+
result: Result;
|
|
183
|
+
}
|
|
184
|
+
interface OrchidORMTableHelper<T extends Query> {
|
|
185
|
+
makeHelper<Args extends unknown[], Result>(fn: (q: T, ...args: Args) => Result): OrchidORMQueryHelper<T, Args, Result>;
|
|
186
|
+
}
|
|
187
|
+
type OrchidORMTables<T extends TableClasses = TableClasses> = { [K in keyof T]: T[K] extends {
|
|
188
|
+
new (): infer R extends ORMTableInput;
|
|
189
|
+
} ? OrchidORMTableHelper<TableToDb<R>> : never };
|
|
190
|
+
type OrchidORMDbTables<T extends TableClasses = TableClasses> = { [K in keyof T]: T[K] extends {
|
|
178
191
|
new (): infer R extends ORMTableInput;
|
|
179
|
-
} ? TableToDb<R> : never }
|
|
192
|
+
} ? TableToDb<R> : never };
|
|
193
|
+
type OrchidORM<T extends TableClasses = TableClasses> = OrchidORMDbTables<T> & OrchidORMMethods;
|
|
180
194
|
/**
|
|
181
195
|
* Identity helper for table row-level security configuration.
|
|
182
196
|
*/
|
|
@@ -328,14 +342,13 @@ interface OrchidORMMethods {
|
|
|
328
342
|
$close(): Promise<void>;
|
|
329
343
|
}
|
|
330
344
|
type OrchidOrmParam<Options> = true | null extends true ? 'Set strict: true to tsconfig' : Options;
|
|
331
|
-
declare const
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
}: OrchidOrmParam<({
|
|
345
|
+
declare const bundleOrchidORMTables: <T extends TableClasses>(tables: T) => OrchidORMTables<T>;
|
|
346
|
+
declare const makeOrchidOrmDbWithAdapter: <T extends TableClasses>(orm: OrchidORMTables<T>, options: OrchidOrmParam<({
|
|
347
|
+
db: Query;
|
|
348
|
+
} | {
|
|
349
|
+
adapter: Adapter;
|
|
350
|
+
}) & DbSharedOptions>) => OrchidORM<T>;
|
|
351
|
+
declare const orchidORMWithAdapter: <T extends TableClasses>(options: OrchidOrmParam<({
|
|
339
352
|
db: Query;
|
|
340
353
|
} | {
|
|
341
354
|
adapter: Adapter;
|
|
@@ -760,4 +773,4 @@ declare const createRepo: <T extends Query, Methods extends MethodsBase<T>>(tabl
|
|
|
760
773
|
table: T["table"];
|
|
761
774
|
shape: T["shape"];
|
|
762
775
|
}>(q: Q) => Query & Q & MapMethods<T, Methods>) & T, Methods>;
|
|
763
|
-
export { BaseTableClass, BaseTableInstance, DefaultSelect, FromQuery, Insertable, MapMethods, MapQueryMethods, MethodsBase, ORMTableInput, OrchidORM, OrchidOrmParam, Queryable, Repo, Selectable, SetColumnsResult, Table, TableClass, TableClasses, TableInfo, TableToDb, Updatable, createBaseTable, createRepo, defineRls, orchidORMWithAdapter };
|
|
776
|
+
export { BaseTableClass, BaseTableInstance, DefaultSelect, FromQuery, Insertable, MapMethods, MapQueryMethods, MethodsBase, ORMTableInput, OrchidORM, OrchidORMDbTables, OrchidORMTableHelper, OrchidORMTables, OrchidOrmParam, Queryable, Repo, Selectable, SetColumnsResult, Table, TableClass, TableClasses, TableInfo, TableToDb, Updatable, bundleOrchidORMTables, createBaseTable, createRepo, defineRls, makeOrchidOrmDbWithAdapter, orchidORMWithAdapter };
|
package/dist/index.js
CHANGED
|
@@ -971,11 +971,11 @@ const makeHasAndBelongsToManyMethod = (tableConfig, table, qb, relation, relatio
|
|
|
971
971
|
const { options } = relation;
|
|
972
972
|
const { snakeCase } = table.internal;
|
|
973
973
|
const primaryKeys = options.columns;
|
|
974
|
-
const
|
|
975
|
-
const
|
|
974
|
+
const originalForeignKeys = options.references;
|
|
975
|
+
const foreignKeys = snakeCase ? [...originalForeignKeys] : originalForeignKeys;
|
|
976
976
|
const joinTable = options.through.table;
|
|
977
|
-
const
|
|
978
|
-
const
|
|
977
|
+
const originalThroughForeignKeys = options.through.columns;
|
|
978
|
+
const throughForeignKeys = snakeCase ? [...originalThroughForeignKeys] : originalThroughForeignKeys;
|
|
979
979
|
const throughPrimaryKeys = options.through.references;
|
|
980
980
|
const { on } = options;
|
|
981
981
|
if (on) {
|
|
@@ -1375,7 +1375,68 @@ function afterCommit(hook) {
|
|
|
1375
1375
|
* Identity helper for table row-level security configuration.
|
|
1376
1376
|
*/
|
|
1377
1377
|
const defineRls = (rls) => rls;
|
|
1378
|
-
const
|
|
1378
|
+
const orchidORMBundleMetadataKey = Symbol("orchidORMBundleMetadataKey");
|
|
1379
|
+
const assignTablesToOrm = (tables, result, adapter, qb, asyncStorage, commonOptions, schema) => {
|
|
1380
|
+
const tableInstances = {};
|
|
1381
|
+
for (const key in tables) {
|
|
1382
|
+
if (key[0] === "$") throw new Error(`Table class name must not start with $`);
|
|
1383
|
+
const tableClass = tables[key];
|
|
1384
|
+
const table = tableClass.instance();
|
|
1385
|
+
tableInstances[key] = table;
|
|
1386
|
+
const options = {
|
|
1387
|
+
...commonOptions,
|
|
1388
|
+
schema: table.schema || schema,
|
|
1389
|
+
language: table.language,
|
|
1390
|
+
scopes: table.scopes,
|
|
1391
|
+
softDelete: table.softDelete,
|
|
1392
|
+
snakeCase: table.snakeCase,
|
|
1393
|
+
comment: table.comment,
|
|
1394
|
+
noPrimaryKey: table.noPrimaryKey ? "ignore" : void 0,
|
|
1395
|
+
computed: table.computed,
|
|
1396
|
+
nowSQL: tableClass.nowSQL
|
|
1397
|
+
};
|
|
1398
|
+
const dbTable = new pqb.Db(adapter, qb, table.table, table.columns.shape, table.types, asyncStorage, options, table.constructor.prototype.columns?.data ?? {});
|
|
1399
|
+
dbTable.definedAs = key;
|
|
1400
|
+
dbTable.db = result;
|
|
1401
|
+
dbTable.filePath = table.filePath;
|
|
1402
|
+
dbTable.name = table.constructor.name;
|
|
1403
|
+
dbTable.internal.tableRls = table.rls;
|
|
1404
|
+
result[key] = dbTable;
|
|
1405
|
+
}
|
|
1406
|
+
applyRelations(qb, tableInstances, result, schema);
|
|
1407
|
+
return tableInstances;
|
|
1408
|
+
};
|
|
1409
|
+
const bundleOrchidORMTables = (tables) => {
|
|
1410
|
+
const result = {};
|
|
1411
|
+
let dbAwareInstance;
|
|
1412
|
+
for (const key in tables) result[key] = { makeHelper(arg) {
|
|
1413
|
+
let fn;
|
|
1414
|
+
return (...args) => {
|
|
1415
|
+
if (!fn) fn = dbAwareInstance[key].makeHelper(arg);
|
|
1416
|
+
return fn(...args);
|
|
1417
|
+
};
|
|
1418
|
+
} };
|
|
1419
|
+
Object.defineProperty(result, orchidORMBundleMetadataKey, {
|
|
1420
|
+
enumerable: false,
|
|
1421
|
+
value: {
|
|
1422
|
+
tables,
|
|
1423
|
+
setDbAwareInstance(orm) {
|
|
1424
|
+
dbAwareInstance = orm;
|
|
1425
|
+
}
|
|
1426
|
+
}
|
|
1427
|
+
});
|
|
1428
|
+
return result;
|
|
1429
|
+
};
|
|
1430
|
+
const getOrchidORMBundleMetadata = (orm) => {
|
|
1431
|
+
const meta = orm[orchidORMBundleMetadataKey];
|
|
1432
|
+
if (!meta) throw new Error("Failed to bind Orchid ORM tables: pass a table bundle created by bundleOrchidORMTables.");
|
|
1433
|
+
return meta;
|
|
1434
|
+
};
|
|
1435
|
+
const makeOrchidOrmDbWithAdapter = (orm, options) => {
|
|
1436
|
+
const meta = getOrchidORMBundleMetadata(orm);
|
|
1437
|
+
return privateOrchidORMWithAdapter(options, meta.tables, meta.setDbAwareInstance);
|
|
1438
|
+
};
|
|
1439
|
+
const privateOrchidORMWithAdapter = ({ log, logger, autoPreparedStatements, noPrimaryKey = "error", schema, ...options }, tables, setDbAwareInstance) => {
|
|
1379
1440
|
const commonOptions = {
|
|
1380
1441
|
log,
|
|
1381
1442
|
logger,
|
|
@@ -1414,33 +1475,8 @@ const orchidORMWithAdapter = ({ log, logger, autoPreparedStatements, noPrimaryKe
|
|
|
1414
1475
|
$close: adapter.close.bind(adapter),
|
|
1415
1476
|
$withOptions: qb.withOptions.bind(qb)
|
|
1416
1477
|
};
|
|
1417
|
-
const tableInstances =
|
|
1418
|
-
|
|
1419
|
-
if (key[0] === "$") throw new Error(`Table class name must not start with $`);
|
|
1420
|
-
const tableClass = tables[key];
|
|
1421
|
-
const table = tableClass.instance();
|
|
1422
|
-
tableInstances[key] = table;
|
|
1423
|
-
const options = {
|
|
1424
|
-
...commonOptions,
|
|
1425
|
-
schema: table.schema || schema,
|
|
1426
|
-
language: table.language,
|
|
1427
|
-
scopes: table.scopes,
|
|
1428
|
-
softDelete: table.softDelete,
|
|
1429
|
-
snakeCase: table.snakeCase,
|
|
1430
|
-
comment: table.comment,
|
|
1431
|
-
noPrimaryKey: table.noPrimaryKey ? "ignore" : void 0,
|
|
1432
|
-
computed: table.computed,
|
|
1433
|
-
nowSQL: tableClass.nowSQL
|
|
1434
|
-
};
|
|
1435
|
-
const dbTable = new pqb.Db(adapter, qb, table.table, table.columns.shape, table.types, asyncStorage, options, table.constructor.prototype.columns?.data ?? {});
|
|
1436
|
-
dbTable.definedAs = key;
|
|
1437
|
-
dbTable.db = result;
|
|
1438
|
-
dbTable.filePath = table.filePath;
|
|
1439
|
-
dbTable.name = table.constructor.name;
|
|
1440
|
-
dbTable.internal.tableRls = table.rls;
|
|
1441
|
-
result[key] = dbTable;
|
|
1442
|
-
}
|
|
1443
|
-
applyRelations(qb, tableInstances, result, schema);
|
|
1478
|
+
const tableInstances = assignTablesToOrm(tables, result, adapter, qb, asyncStorage, commonOptions, schema);
|
|
1479
|
+
setDbAwareInstance?.(result);
|
|
1444
1480
|
for (const key in tables) {
|
|
1445
1481
|
const table = tableInstances[key];
|
|
1446
1482
|
if (table.init) {
|
|
@@ -1450,6 +1486,7 @@ const orchidORMWithAdapter = ({ log, logger, autoPreparedStatements, noPrimaryKe
|
|
|
1450
1486
|
}
|
|
1451
1487
|
return result;
|
|
1452
1488
|
};
|
|
1489
|
+
const orchidORMWithAdapter = (options, tables) => privateOrchidORMWithAdapter(options, tables);
|
|
1453
1490
|
function $getAdapter() {
|
|
1454
1491
|
return this.$qb.$getAdapter();
|
|
1455
1492
|
}
|
|
@@ -1480,9 +1517,11 @@ const createRepo = (table, methods) => {
|
|
|
1480
1517
|
return q[key];
|
|
1481
1518
|
} });
|
|
1482
1519
|
};
|
|
1520
|
+
exports.bundleOrchidORMTables = bundleOrchidORMTables;
|
|
1483
1521
|
exports.createBaseTable = createBaseTable;
|
|
1484
1522
|
exports.createRepo = createRepo;
|
|
1485
1523
|
exports.defineRls = defineRls;
|
|
1524
|
+
exports.makeOrchidOrmDbWithAdapter = makeOrchidOrmDbWithAdapter;
|
|
1486
1525
|
exports.orchidORMWithAdapter = orchidORMWithAdapter;
|
|
1487
1526
|
Object.keys(pqb).forEach(function(k) {
|
|
1488
1527
|
if (k !== "default" && !Object.prototype.hasOwnProperty.call(exports, k)) Object.defineProperty(exports, k, {
|