orchid-orm 1.68.6 → 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.mjs 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 foreignKeys = options.references;
975
- const originalForeignKeys = snakeCase ? [...foreignKeys] : foreignKeys;
974
+ const originalForeignKeys = options.references;
975
+ const foreignKeys = snakeCase ? [...originalForeignKeys] : originalForeignKeys;
976
976
  const joinTable = options.through.table;
977
- const throughForeignKeys = options.through.columns;
978
- const originalThroughForeignKeys = snakeCase ? [...throughForeignKeys] : throughForeignKeys;
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 orchidORMWithAdapter = ({ log, logger, autoPreparedStatements, noPrimaryKey = "error", schema, ...options }, tables) => {
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 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
- 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 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,6 +1517,6 @@ const createRepo = (table, methods) => {
1480
1517
  return q[key];
1481
1518
  } });
1482
1519
  };
1483
- export { createBaseTable, createRepo, defineRls, orchidORMWithAdapter };
1520
+ export { bundleOrchidORMTables, createBaseTable, createRepo, defineRls, makeOrchidOrmDbWithAdapter, orchidORMWithAdapter };
1484
1521
 
1485
1522
  //# sourceMappingURL=index.mjs.map