orchid-orm 1.72.7 → 1.72.9

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
@@ -131,6 +131,19 @@ function createBaseTable({ schemaConfig = defaultSchemaConfig, columnTypes: colu
131
131
  }
132
132
  };
133
133
  applyMixins(base, [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,14 @@ 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
+ generatorIgnore: table.generatorIgnore,
1486
+ noPrimaryKey: table.noPrimaryKey || !isTable ? "ignore" : void 0,
1472
1487
  computed: table.computed,
1473
1488
  nowSQL: tableClass.nowSQL
1474
1489
  };
1475
- const dbTable = new Db(adapter, qb, table.table, table.columns.shape, table.types, asyncStorage, options, table.columns?.data ?? {});
1490
+ const dbTable = new Db(adapter, qb, isTable ? table.table : table.name, table.columns.shape, table.types, asyncStorage, options, table.columns?.data ?? {}, getViewData(isTable, table));
1476
1491
  dbTable.definedAs = key;
1477
1492
  dbTable.db = result;
1478
1493
  dbTable.filePath = table.filePath;
@@ -1481,11 +1496,28 @@ const assignTablesToOrm = (tables, result, adapter, qb, asyncStorage, commonOpti
1481
1496
  dbTable.internal.tableGrants = table.grants;
1482
1497
  result[key] = dbTable;
1483
1498
  }
1484
- applyRelations(qb, tableInstances, result, schema);
1485
1499
  return tableInstances;
1486
1500
  };
1487
- const bundleOrchidORMTables = (tables) => {
1501
+ const getViewData = (isTable, table) => {
1502
+ if (isTable) return;
1503
+ return {
1504
+ query: table.query,
1505
+ sql: table.sql,
1506
+ recursive: table.recursive,
1507
+ checkOption: table.checkOption,
1508
+ securityBarrier: table.securityBarrier,
1509
+ securityInvoker: table.securityInvoker,
1510
+ withData: table.withData
1511
+ };
1512
+ };
1513
+ const bundleOrchidORM = ({ tables = {}, views = {} }) => {
1488
1514
  const result = {};
1515
+ const bundledViews = {};
1516
+ const hasViews = Object.keys(views).length > 0;
1517
+ Object.defineProperty(result, "$views", {
1518
+ enumerable: hasViews,
1519
+ value: bundledViews
1520
+ });
1489
1521
  let dbAwareInstance;
1490
1522
  for (const key in tables) result[key] = {
1491
1523
  table: tables[key].instance().table,
@@ -1497,10 +1529,21 @@ const bundleOrchidORMTables = (tables) => {
1497
1529
  };
1498
1530
  }
1499
1531
  };
1532
+ for (const key in views) bundledViews[key] = {
1533
+ table: views[key].instance().name,
1534
+ makeHelper(arg) {
1535
+ let fn;
1536
+ return (...args) => {
1537
+ if (!fn) fn = dbAwareInstance.$views[key].makeHelper(arg);
1538
+ return fn(...args);
1539
+ };
1540
+ }
1541
+ };
1500
1542
  Object.defineProperty(result, orchidORMBundleMetadataKey, {
1501
1543
  enumerable: false,
1502
1544
  value: {
1503
1545
  tables,
1546
+ views,
1504
1547
  setDbAwareInstance(orm) {
1505
1548
  dbAwareInstance = orm;
1506
1549
  }
@@ -1508,16 +1551,18 @@ const bundleOrchidORMTables = (tables) => {
1508
1551
  });
1509
1552
  return result;
1510
1553
  };
1554
+ const bundleOrchidORMTables = (tables) => bundleOrchidORM({ tables });
1511
1555
  const getOrchidORMBundleMetadata = (orm) => {
1512
1556
  const meta = orm[orchidORMBundleMetadataKey];
1513
- if (!meta) throw new Error("Failed to bind Orchid ORM tables: pass a table bundle created by bundleOrchidORMTables.");
1557
+ if (!meta) throw new Error("Failed to bind Orchid ORM tables: pass a bundle created by bundleOrchidORM.");
1514
1558
  return meta;
1515
1559
  };
1516
1560
  const makeOrchidOrmDbWithAdapter = (orm, options) => {
1517
1561
  const meta = getOrchidORMBundleMetadata(orm);
1518
- return privateOrchidORMWithAdapter(options, meta.tables, meta.setDbAwareInstance);
1562
+ return privateOrchidORMWithAdapter(options, meta.tables, meta.views, meta.setDbAwareInstance);
1519
1563
  };
1520
- const privateOrchidORMWithAdapter = ({ log, logger, autoPreparedStatements, noPrimaryKey = "error", schema, ...options }, tables, setDbAwareInstance) => {
1564
+ const privateOrchidORMWithAdapter = ({ log, logger, autoPreparedStatements, noPrimaryKey = "error", schema, views, ...options }, tables, bundledViews, setDbAwareInstance) => {
1565
+ if (bundledViews) views = bundledViews;
1521
1566
  const commonOptions = {
1522
1567
  log,
1523
1568
  logger,
@@ -1556,13 +1601,48 @@ const privateOrchidORMWithAdapter = ({ log, logger, autoPreparedStatements, noPr
1556
1601
  $close: adapter.close.bind(adapter),
1557
1602
  $withOptions: qb.withOptions.bind(qb)
1558
1603
  };
1559
- const tableInstances = assignTablesToOrm(tables, result, adapter, qb, asyncStorage, commonOptions, schema);
1604
+ result.$views = {};
1605
+ const tableInstances = assignTablesToOrm(true, tables, result, adapter, qb, asyncStorage, commonOptions, schema);
1606
+ let viewInstances;
1607
+ if (views) {
1608
+ viewInstances = assignTablesToOrm(false, views, result.$views, adapter, qb, asyncStorage, commonOptions, schema);
1609
+ const tableDbNames = Object.values(tableInstances).map((table) => {
1610
+ const s = typeof table.schema === "function" ? table.schema() : table.schema;
1611
+ return `${s ? s + "." : ""}${table.table}`;
1612
+ });
1613
+ for (const key in views) {
1614
+ const view = viewInstances[key];
1615
+ const s = typeof view.schema === "function" ? view.schema() : view.schema;
1616
+ const name = `${s ? s + "." : ""}${view.name}`;
1617
+ if (tableDbNames.includes(name)) throw new Error(`Cannot configure both a table and a view for database relation ${name}`);
1618
+ }
1619
+ }
1620
+ applyRelations(qb, {
1621
+ ...tableInstances,
1622
+ ...viewInstances
1623
+ }, {
1624
+ ...result,
1625
+ ...result.$views
1626
+ }, schema);
1560
1627
  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);
1628
+ const initItems = [[
1629
+ tableInstances,
1630
+ result,
1631
+ true
1632
+ ], [
1633
+ viewInstances,
1634
+ result.$views,
1635
+ false
1636
+ ]];
1637
+ for (const [items, queries, isTable] of initItems) {
1638
+ if (!items) continue;
1639
+ for (const key in items) {
1640
+ const table = items[key];
1641
+ if (table.init) {
1642
+ table.init(result);
1643
+ Object.assign(queries[key].baseQuery.q, table.q);
1644
+ if (!isTable) queries[key].internal.viewData = getViewData(false, table);
1645
+ }
1566
1646
  }
1567
1647
  }
1568
1648
  return result;
@@ -1598,6 +1678,6 @@ const createRepo = (table, methods) => {
1598
1678
  return q[key];
1599
1679
  } });
1600
1680
  };
1601
- export { bundleOrchidORMTables, createBaseTable, createRepo, defineRls, makeOrchidOrmDbWithAdapter, orchidORMWithAdapter, setGrants };
1681
+ export { bundleOrchidORM, bundleOrchidORMTables, createBaseTable, createRepo, defineRls, makeOrchidOrmDbWithAdapter, orchidORMWithAdapter, setGrants };
1602
1682
 
1603
1683
  //# sourceMappingURL=index.mjs.map