orchid-orm 1.72.7 → 1.72.8

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.js CHANGED
@@ -131,6 +131,19 @@ function createBaseTable({ schemaConfig = pqb_internal.defaultSchemaConfig, colu
131
131
  }
132
132
  };
133
133
  (0, pqb_internal.applyMixins)(base, [pqb_internal.QueryHooks]);
134
+ const baseWithView = base;
135
+ baseWithView.View = class View extends base {
136
+ constructor(..._args) {
137
+ super(..._args);
138
+ this.readOnly = true;
139
+ }
140
+ };
141
+ baseWithView.MaterializedView = class MaterializedView extends baseWithView.View {
142
+ constructor(..._args2) {
143
+ super(..._args2);
144
+ this.materialized = true;
145
+ }
146
+ };
134
147
  base.prototype.types = columnTypes;
135
148
  base.prototype.snakeCase = snakeCase;
136
149
  base.prototype.language = language;
@@ -1446,10 +1459,10 @@ const defineRls = (rls) => rls;
1446
1459
  */
1447
1460
  const setGrants = (grants) => grants;
1448
1461
  const orchidORMBundleMetadataKey = Symbol("orchidORMBundleMetadataKey");
1449
- const assignTablesToOrm = (tables, result, adapter, qb, asyncStorage, commonOptions, schema) => {
1462
+ const assignTablesToOrm = (isTable, tables, result, adapter, qb, asyncStorage, commonOptions, schema) => {
1450
1463
  const tableInstances = {};
1451
1464
  for (const key in tables) {
1452
- if (key[0] === "$") throw new Error(`Table class name must not start with $`);
1465
+ if (key[0] === "$") throw new Error(`${isTable ? "Table" : "View"} class name must not start with $`);
1453
1466
  const tableClass = tables[key];
1454
1467
  const tableImmutable = tableClass.instance();
1455
1468
  const table = Object.create(tableImmutable);
@@ -1467,12 +1480,20 @@ const assignTablesToOrm = (tables, result, adapter, qb, asyncStorage, commonOpti
1467
1480
  softDelete: table.softDelete,
1468
1481
  snakeCase: table.snakeCase,
1469
1482
  comment: table.comment,
1470
- readOnly: table.readOnly,
1471
- noPrimaryKey: table.noPrimaryKey ? "ignore" : void 0,
1483
+ readOnly: isTable ? table.readOnly ? true : void 0 : table.materialized || table.readOnly !== false ? true : void 0,
1484
+ materialized: !isTable && table.materialized ? true : void 0,
1485
+ noPrimaryKey: table.noPrimaryKey || !isTable ? "ignore" : void 0,
1472
1486
  computed: table.computed,
1473
1487
  nowSQL: tableClass.nowSQL
1474
1488
  };
1475
- const dbTable = new pqb.Db(adapter, qb, table.table, table.columns.shape, table.types, asyncStorage, options, table.columns?.data ?? {});
1489
+ const dbTable = new pqb.Db(adapter, qb, isTable ? table.table : table.name, table.columns.shape, table.types, asyncStorage, options, table.columns?.data ?? {}, isTable ? void 0 : {
1490
+ sql: table.sql,
1491
+ recursive: table.recursive,
1492
+ checkOption: table.checkOption,
1493
+ securityBarrier: table.securityBarrier,
1494
+ securityInvoker: table.securityInvoker,
1495
+ withData: table.withData
1496
+ });
1476
1497
  dbTable.definedAs = key;
1477
1498
  dbTable.db = result;
1478
1499
  dbTable.filePath = table.filePath;
@@ -1481,11 +1502,16 @@ const assignTablesToOrm = (tables, result, adapter, qb, asyncStorage, commonOpti
1481
1502
  dbTable.internal.tableGrants = table.grants;
1482
1503
  result[key] = dbTable;
1483
1504
  }
1484
- applyRelations(qb, tableInstances, result, schema);
1485
1505
  return tableInstances;
1486
1506
  };
1487
- const bundleOrchidORMTables = (tables) => {
1507
+ const bundleOrchidORM = ({ tables = {}, views = {} }) => {
1488
1508
  const result = {};
1509
+ const bundledViews = {};
1510
+ const hasViews = Object.keys(views).length > 0;
1511
+ Object.defineProperty(result, "$views", {
1512
+ enumerable: hasViews,
1513
+ value: bundledViews
1514
+ });
1489
1515
  let dbAwareInstance;
1490
1516
  for (const key in tables) result[key] = {
1491
1517
  table: tables[key].instance().table,
@@ -1497,10 +1523,21 @@ const bundleOrchidORMTables = (tables) => {
1497
1523
  };
1498
1524
  }
1499
1525
  };
1526
+ for (const key in views) bundledViews[key] = {
1527
+ table: views[key].instance().name,
1528
+ makeHelper(arg) {
1529
+ let fn;
1530
+ return (...args) => {
1531
+ if (!fn) fn = dbAwareInstance.$views[key].makeHelper(arg);
1532
+ return fn(...args);
1533
+ };
1534
+ }
1535
+ };
1500
1536
  Object.defineProperty(result, orchidORMBundleMetadataKey, {
1501
1537
  enumerable: false,
1502
1538
  value: {
1503
1539
  tables,
1540
+ views,
1504
1541
  setDbAwareInstance(orm) {
1505
1542
  dbAwareInstance = orm;
1506
1543
  }
@@ -1508,16 +1545,18 @@ const bundleOrchidORMTables = (tables) => {
1508
1545
  });
1509
1546
  return result;
1510
1547
  };
1548
+ const bundleOrchidORMTables = (tables) => bundleOrchidORM({ tables });
1511
1549
  const getOrchidORMBundleMetadata = (orm) => {
1512
1550
  const meta = orm[orchidORMBundleMetadataKey];
1513
- if (!meta) throw new Error("Failed to bind Orchid ORM tables: pass a table bundle created by bundleOrchidORMTables.");
1551
+ if (!meta) throw new Error("Failed to bind Orchid ORM tables: pass a bundle created by bundleOrchidORM.");
1514
1552
  return meta;
1515
1553
  };
1516
1554
  const makeOrchidOrmDbWithAdapter = (orm, options) => {
1517
1555
  const meta = getOrchidORMBundleMetadata(orm);
1518
- return privateOrchidORMWithAdapter(options, meta.tables, meta.setDbAwareInstance);
1556
+ return privateOrchidORMWithAdapter(options, meta.tables, meta.views, meta.setDbAwareInstance);
1519
1557
  };
1520
- const privateOrchidORMWithAdapter = ({ log, logger, autoPreparedStatements, noPrimaryKey = "error", schema, ...options }, tables, setDbAwareInstance) => {
1558
+ const privateOrchidORMWithAdapter = ({ log, logger, autoPreparedStatements, noPrimaryKey = "error", schema, views, ...options }, tables, bundledViews, setDbAwareInstance) => {
1559
+ if (bundledViews) views = bundledViews;
1521
1560
  const commonOptions = {
1522
1561
  log,
1523
1562
  logger,
@@ -1556,13 +1595,39 @@ const privateOrchidORMWithAdapter = ({ log, logger, autoPreparedStatements, noPr
1556
1595
  $close: adapter.close.bind(adapter),
1557
1596
  $withOptions: qb.withOptions.bind(qb)
1558
1597
  };
1559
- const tableInstances = assignTablesToOrm(tables, result, adapter, qb, asyncStorage, commonOptions, schema);
1598
+ result.$views = {};
1599
+ const tableInstances = assignTablesToOrm(true, tables, result, adapter, qb, asyncStorage, commonOptions, schema);
1600
+ let viewInstances;
1601
+ if (views) {
1602
+ viewInstances = assignTablesToOrm(false, views, result.$views, adapter, qb, asyncStorage, commonOptions, schema);
1603
+ const tableDbNames = Object.values(tableInstances).map((table) => {
1604
+ const s = typeof table.schema === "function" ? table.schema() : table.schema;
1605
+ return `${s ? s + "." : ""}${table.table}`;
1606
+ });
1607
+ for (const key in views) {
1608
+ const view = viewInstances[key];
1609
+ const s = typeof view.schema === "function" ? view.schema() : view.schema;
1610
+ const name = `${s ? s + "." : ""}${view.name}`;
1611
+ if (tableDbNames.includes(name)) throw new Error(`Cannot configure both a table and a view for database relation ${name}`);
1612
+ }
1613
+ }
1614
+ applyRelations(qb, {
1615
+ ...tableInstances,
1616
+ ...viewInstances
1617
+ }, {
1618
+ ...result,
1619
+ ...result.$views
1620
+ }, schema);
1560
1621
  setDbAwareInstance?.(result);
1561
- for (const key in tables) {
1562
- const table = tableInstances[key];
1563
- if (table.init) {
1564
- table.init(result);
1565
- Object.assign(result[key].baseQuery.q, table.q);
1622
+ const initItems = [[tableInstances, result], [viewInstances, result.$views]];
1623
+ for (const [items, queries] of initItems) {
1624
+ if (!items) continue;
1625
+ for (const key in items) {
1626
+ const table = items[key];
1627
+ if (table.init) {
1628
+ table.init(result);
1629
+ Object.assign(queries[key].baseQuery.q, table.q);
1630
+ }
1566
1631
  }
1567
1632
  }
1568
1633
  return result;
@@ -1598,6 +1663,7 @@ const createRepo = (table, methods) => {
1598
1663
  return q[key];
1599
1664
  } });
1600
1665
  };
1666
+ exports.bundleOrchidORM = bundleOrchidORM;
1601
1667
  exports.bundleOrchidORMTables = bundleOrchidORMTables;
1602
1668
  exports.createBaseTable = createBaseTable;
1603
1669
  exports.createRepo = createRepo;