orchid-orm 1.69.0 → 1.69.2

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 CHANGED
@@ -174,9 +174,23 @@ declare function afterCommit(this: {
174
174
  interface FromQuery extends Query {
175
175
  returnType: 'all';
176
176
  }
177
- type OrchidORM<T extends TableClasses = TableClasses> = { [K in keyof T]: T[K] extends {
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 } & OrchidORMMethods;
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 orchidORMWithAdapter: <T extends TableClasses>({
332
- log,
333
- logger,
334
- autoPreparedStatements,
335
- noPrimaryKey,
336
- schema,
337
- ...options
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
@@ -81,7 +81,6 @@ function createBaseTable({ schemaConfig = pqb_internal.defaultSchemaConfig, colu
81
81
  throw new Error(`Failed to determine file path for table ${this.constructor.name}. Please set \`filePath\` property manually`);
82
82
  }
83
83
  setColumns(fn, dataFn) {
84
- columnTypes[pqb_internal.snakeCaseKey] = this.snakeCase;
85
84
  const shape = (0, pqb_internal.getColumnTypes)(columnTypes, fn, nowSQL, this.language);
86
85
  const tableData = (0, pqb_internal.parseTableData)(dataFn);
87
86
  if (this.snakeCase) for (const key in shape) {
@@ -971,11 +970,11 @@ const makeHasAndBelongsToManyMethod = (tableConfig, table, qb, relation, relatio
971
970
  const { options } = relation;
972
971
  const { snakeCase } = table.internal;
973
972
  const primaryKeys = options.columns;
974
- const foreignKeys = options.references;
975
- const originalForeignKeys = snakeCase ? [...foreignKeys] : foreignKeys;
973
+ const originalForeignKeys = options.references;
974
+ const foreignKeys = snakeCase ? [...originalForeignKeys] : originalForeignKeys;
976
975
  const joinTable = options.through.table;
977
- const throughForeignKeys = options.through.columns;
978
- const originalThroughForeignKeys = snakeCase ? [...throughForeignKeys] : throughForeignKeys;
976
+ const originalThroughForeignKeys = options.through.columns;
977
+ const throughForeignKeys = snakeCase ? [...originalThroughForeignKeys] : originalThroughForeignKeys;
979
978
  const throughPrimaryKeys = options.through.references;
980
979
  const { on } = options;
981
980
  if (on) {
@@ -1375,7 +1374,74 @@ function afterCommit(hook) {
1375
1374
  * Identity helper for table row-level security configuration.
1376
1375
  */
1377
1376
  const defineRls = (rls) => rls;
1378
- const orchidORMWithAdapter = ({ log, logger, autoPreparedStatements, noPrimaryKey = "error", schema, ...options }, tables) => {
1377
+ const orchidORMBundleMetadataKey = Symbol("orchidORMBundleMetadataKey");
1378
+ const assignTablesToOrm = (tables, result, adapter, qb, asyncStorage, commonOptions, schema) => {
1379
+ const tableInstances = {};
1380
+ for (const key in tables) {
1381
+ if (key[0] === "$") throw new Error(`Table class name must not start with $`);
1382
+ const tableClass = tables[key];
1383
+ const tableImmutable = tableClass.instance();
1384
+ const table = Object.create(tableImmutable);
1385
+ table.q = { ...table.q };
1386
+ table.columns = {
1387
+ shape: { ...table.columns.shape },
1388
+ data: { ...table.columns.data }
1389
+ };
1390
+ tableInstances[key] = table;
1391
+ const options = {
1392
+ ...commonOptions,
1393
+ schema: table.schema || schema,
1394
+ language: table.language,
1395
+ scopes: table.scopes,
1396
+ softDelete: table.softDelete,
1397
+ snakeCase: table.snakeCase,
1398
+ comment: table.comment,
1399
+ noPrimaryKey: table.noPrimaryKey ? "ignore" : void 0,
1400
+ computed: table.computed,
1401
+ nowSQL: tableClass.nowSQL
1402
+ };
1403
+ const dbTable = new pqb.Db(adapter, qb, table.table, table.columns.shape, table.types, asyncStorage, options, table.columns?.data ?? {});
1404
+ dbTable.definedAs = key;
1405
+ dbTable.db = result;
1406
+ dbTable.filePath = table.filePath;
1407
+ dbTable.name = table.constructor.name;
1408
+ dbTable.internal.tableRls = table.rls;
1409
+ result[key] = dbTable;
1410
+ }
1411
+ applyRelations(qb, tableInstances, result, schema);
1412
+ return tableInstances;
1413
+ };
1414
+ const bundleOrchidORMTables = (tables) => {
1415
+ const result = {};
1416
+ let dbAwareInstance;
1417
+ for (const key in tables) result[key] = { makeHelper(arg) {
1418
+ let fn;
1419
+ return (...args) => {
1420
+ if (!fn) fn = dbAwareInstance[key].makeHelper(arg);
1421
+ return fn(...args);
1422
+ };
1423
+ } };
1424
+ Object.defineProperty(result, orchidORMBundleMetadataKey, {
1425
+ enumerable: false,
1426
+ value: {
1427
+ tables,
1428
+ setDbAwareInstance(orm) {
1429
+ dbAwareInstance = orm;
1430
+ }
1431
+ }
1432
+ });
1433
+ return result;
1434
+ };
1435
+ const getOrchidORMBundleMetadata = (orm) => {
1436
+ const meta = orm[orchidORMBundleMetadataKey];
1437
+ if (!meta) throw new Error("Failed to bind Orchid ORM tables: pass a table bundle created by bundleOrchidORMTables.");
1438
+ return meta;
1439
+ };
1440
+ const makeOrchidOrmDbWithAdapter = (orm, options) => {
1441
+ const meta = getOrchidORMBundleMetadata(orm);
1442
+ return privateOrchidORMWithAdapter(options, meta.tables, meta.setDbAwareInstance);
1443
+ };
1444
+ const privateOrchidORMWithAdapter = ({ log, logger, autoPreparedStatements, noPrimaryKey = "error", schema, ...options }, tables, setDbAwareInstance) => {
1379
1445
  const commonOptions = {
1380
1446
  log,
1381
1447
  logger,
@@ -1414,33 +1480,8 @@ const orchidORMWithAdapter = ({ log, logger, autoPreparedStatements, noPrimaryKe
1414
1480
  $close: adapter.close.bind(adapter),
1415
1481
  $withOptions: qb.withOptions.bind(qb)
1416
1482
  };
1417
- const tableInstances = {};
1418
- for (const key in tables) {
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);
1483
+ const tableInstances = assignTablesToOrm(tables, result, adapter, qb, asyncStorage, commonOptions, schema);
1484
+ setDbAwareInstance?.(result);
1444
1485
  for (const key in tables) {
1445
1486
  const table = tableInstances[key];
1446
1487
  if (table.init) {
@@ -1450,6 +1491,7 @@ const orchidORMWithAdapter = ({ log, logger, autoPreparedStatements, noPrimaryKe
1450
1491
  }
1451
1492
  return result;
1452
1493
  };
1494
+ const orchidORMWithAdapter = (options, tables) => privateOrchidORMWithAdapter(options, tables);
1453
1495
  function $getAdapter() {
1454
1496
  return this.$qb.$getAdapter();
1455
1497
  }
@@ -1480,9 +1522,11 @@ const createRepo = (table, methods) => {
1480
1522
  return q[key];
1481
1523
  } });
1482
1524
  };
1525
+ exports.bundleOrchidORMTables = bundleOrchidORMTables;
1483
1526
  exports.createBaseTable = createBaseTable;
1484
1527
  exports.createRepo = createRepo;
1485
1528
  exports.defineRls = defineRls;
1529
+ exports.makeOrchidOrmDbWithAdapter = makeOrchidOrmDbWithAdapter;
1486
1530
  exports.orchidORMWithAdapter = orchidORMWithAdapter;
1487
1531
  Object.keys(pqb).forEach(function(k) {
1488
1532
  if (k !== "default" && !Object.prototype.hasOwnProperty.call(exports, k)) Object.defineProperty(exports, k, {