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.mjs CHANGED
@@ -1,4 +1,4 @@
1
- import { DynamicRawSQL, QueryHooks, RawSql, VirtualColumn, _appendQuery, _clone, _createDbSqlMethod, _hookSelectColumns, _initQueryBuilder, _orCreate, _prependWith, _queryCreate, _queryCreateMany, _queryCreateManyFrom, _queryDefaults, _queryDelete, _queryFindBy, _queryFindByOptional, _queryHookAfterCreate, _queryHookAfterUpdate, _queryInsert, _queryInsertMany, _queryJoinOn, _queryRows, _querySelect, _queryTake, _queryTakeOptional, _queryUpdate, _queryUpdateOrThrow, _queryUpsert, _queryWhere, _queryWhereExists, _queryWhereIn, applyMixins, cloneQueryBaseUnscoped, defaultSchemaConfig, emptyArray, emptyObject, getCallerFilePath, getClonedQueryData, getColumnTypes, getFreeAlias, getPrimaryKeys, getQueryAs, getShapeFromSelect, getStackTrace, isExpression, isQueryReturnsAll, makeColumnTypes, noop, objectHasValues, parseTableData, pick, prepareSubQueryForSql, pushQueryOnForOuter, setQueryObjectValueImmutable, snakeCaseKey, toArray, toSnakeCase } from "pqb/internal";
1
+ import { DynamicRawSQL, QueryHooks, RawSql, VirtualColumn, _appendQuery, _clone, _createDbSqlMethod, _hookSelectColumns, _initQueryBuilder, _orCreate, _prependWith, _queryCreate, _queryCreateMany, _queryCreateManyFrom, _queryDefaults, _queryDelete, _queryFindBy, _queryFindByOptional, _queryHookAfterCreate, _queryHookAfterUpdate, _queryInsert, _queryInsertMany, _queryJoinOn, _queryRows, _querySelect, _queryTake, _queryTakeOptional, _queryUpdate, _queryUpdateOrThrow, _queryUpsert, _queryWhere, _queryWhereExists, _queryWhereIn, applyMixins, cloneQueryBaseUnscoped, defaultSchemaConfig, emptyArray, emptyObject, getCallerFilePath, getClonedQueryData, getColumnTypes, getFreeAlias, getPrimaryKeys, getQueryAs, getShapeFromSelect, getStackTrace, isExpression, isQueryReturnsAll, makeColumnTypes, noop, objectHasValues, parseTableData, pick, prepareSubQueryForSql, pushQueryOnForOuter, setQueryObjectValueImmutable, toArray, toSnakeCase } from "pqb/internal";
2
2
  import { Db, NotFoundError, OrchidOrmInternalError } from "pqb";
3
3
  import { AsyncLocalStorage } from "node:async_hooks";
4
4
  export * from "pqb";
@@ -81,7 +81,6 @@ function createBaseTable({ schemaConfig = defaultSchemaConfig, columnTypes: 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[snakeCaseKey] = this.snakeCase;
85
84
  const shape = getColumnTypes(columnTypes, fn, nowSQL, this.language);
86
85
  const tableData = 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 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 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,6 +1522,6 @@ const createRepo = (table, methods) => {
1480
1522
  return q[key];
1481
1523
  } });
1482
1524
  };
1483
- export { createBaseTable, createRepo, defineRls, orchidORMWithAdapter };
1525
+ export { bundleOrchidORMTables, createBaseTable, createRepo, defineRls, makeOrchidOrmDbWithAdapter, orchidORMWithAdapter };
1484
1526
 
1485
1527
  //# sourceMappingURL=index.mjs.map