convex-ents 0.4.1 → 0.4.3

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/functions.js CHANGED
@@ -530,7 +530,12 @@ var PromisePaginationResultOrNullImpl = class extends Promise {
530
530
  };
531
531
  var PromiseTableImpl = class extends PromiseQueryOrNullImpl {
532
532
  constructor(ctx, entDefinitions, table) {
533
- super(ctx, entDefinitions, table, async () => ctx.db.query(table));
533
+ super(
534
+ ctx,
535
+ entDefinitions,
536
+ table,
537
+ async () => isSystemTable(table) ? ctx.db.system.query(table) : ctx.db.query(table)
538
+ );
534
539
  }
535
540
  get(...args) {
536
541
  return this.getImpl(args);
@@ -557,7 +562,7 @@ var PromiseTableImpl = class extends PromiseQueryOrNullImpl {
557
562
  return {
558
563
  id,
559
564
  doc: async () => {
560
- const doc = await this.ctx.db.get(id);
565
+ const doc = await (isSystemTable(this.table) ? this.ctx.db.system.get(id) : this.ctx.db.get(id));
561
566
  if (throwIfNull && doc === null) {
562
567
  throw new Error(
563
568
  `Document not found with id \`${id}\` in table "${this.table}"`
@@ -595,7 +600,7 @@ var PromiseTableImpl = class extends PromiseQueryOrNullImpl {
595
600
  });
596
601
  return await Promise.all(
597
602
  ids.map(async (id) => {
598
- const doc = await this.ctx.db.get(id);
603
+ const doc = await (isSystemTable(this.table) ? this.ctx.db.system.get(id) : this.ctx.db.get(id));
599
604
  if (doc === null) {
600
605
  throw new Error(
601
606
  `Document not found with id \`${id}\` in table "${this.table}"`
@@ -984,7 +989,7 @@ function entWrapper(fields, ctx, entDefinitions, table) {
984
989
  writable: false,
985
990
  configurable: false
986
991
  });
987
- Object.entries(entDefinitions[table].defaults).map(
992
+ Object.entries(entDefinitions[table]?.defaults ?? []).map(
988
993
  ([field, value]) => {
989
994
  if (doc[field] === void 0) {
990
995
  doc[field] = value;
@@ -995,26 +1000,28 @@ function entWrapper(fields, ctx, entDefinitions, table) {
995
1000
  }
996
1001
  function entsTableFactory(ctx, entDefinitions, options) {
997
1002
  const enrichedCtx = options !== void 0 ? { ...ctx, ...options } : ctx;
998
- return (table, indexName, indexRange) => {
999
- if (typeof table !== "string") {
1000
- throw new Error(`Expected table name, got \`${table}\``);
1003
+ const table = (table2, indexName, indexRange) => {
1004
+ if (typeof table2 !== "string") {
1005
+ throw new Error(`Expected table name, got \`${table2}\``);
1001
1006
  }
1002
1007
  if (indexName !== void 0) {
1003
1008
  return new PromiseTableImpl(
1004
1009
  enrichedCtx,
1005
1010
  entDefinitions,
1006
- table
1011
+ table2
1007
1012
  ).withIndex(indexName, indexRange);
1008
1013
  }
1009
1014
  if (ctx.db.insert !== void 0) {
1010
1015
  return new PromiseTableWriterImpl(
1011
1016
  enrichedCtx,
1012
1017
  entDefinitions,
1013
- table
1018
+ table2
1014
1019
  );
1015
1020
  }
1016
- return new PromiseTableImpl(enrichedCtx, entDefinitions, table);
1021
+ return new PromiseTableImpl(enrichedCtx, entDefinitions, table2);
1017
1022
  };
1023
+ table.system = table;
1024
+ return table;
1018
1025
  }
1019
1026
  var PromiseTableWriterImpl = class extends PromiseTableImpl {
1020
1027
  constructor(ctx, entDefinitions, table) {
@@ -1118,7 +1125,7 @@ var PromiseEntWriterImpl = class extends PromiseEntOrNullImpl {
1118
1125
  ).collect() : []
1119
1126
  )
1120
1127
  )
1121
- )).map((edgeDoc) => edgeDoc._id);
1128
+ )).flat().map((edgeDoc) => edgeDoc._id);
1122
1129
  edges[key] = {
1123
1130
  add,
1124
1131
  removeEdges
@@ -1274,6 +1281,9 @@ function getEdgeDefinitions(entDefinitions, table) {
1274
1281
  function getDeletionConfig(entDefinitions, table) {
1275
1282
  return entDefinitions[table].deletionConfig;
1276
1283
  }
1284
+ function isSystemTable(table) {
1285
+ return table.startsWith("_");
1286
+ }
1277
1287
  // Annotate the CommonJS export names for ESM import in node:
1278
1288
  0 && (module.exports = {
1279
1289
  addEntRules,