convex-ents 0.18.0 → 0.19.0

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
@@ -463,6 +463,15 @@ function getEntDefinitions(schema) {
463
463
  function getEdgeDefinitions(entDefinitions, table) {
464
464
  return entDefinitions[table].edges;
465
465
  }
466
+ function systemAwareGet(db, table, id) {
467
+ return isSystemTable(table) ? db.system.get(table, id) : db.get(table, id);
468
+ }
469
+ function systemAwareQuery(db, table) {
470
+ return isSystemTable(table) ? db.system.query(table) : db.query(table);
471
+ }
472
+ function isSystemTable(table) {
473
+ return table.startsWith("_");
474
+ }
466
475
 
467
476
  // src/writer.ts
468
477
  var import_server2 = require("convex/server");
@@ -496,7 +505,7 @@ var WriterImplBase = class _WriterImplBase {
496
505
  }
497
506
  } else if (edgeDefinition.cardinality === "single") {
498
507
  if (edgeDefinition.deletion !== void 0 && (!isDeletingSoftly || edgeDefinition.deletion === "soft")) {
499
- const doc = await this.ctx.db.get(id);
508
+ const doc = await this.ctx.db.get(this.table, id);
500
509
  if (doc !== null) {
501
510
  const otherId = doc[edgeDefinition.field];
502
511
  edges[key] = {
@@ -702,7 +711,7 @@ var WriterImplBase = class _WriterImplBase {
702
711
  if (id !== void 0) {
703
712
  const readPolicy = getReadRule(this.entDefinitions, this.table);
704
713
  if (readPolicy !== void 0) {
705
- const doc = await this.ctx.db.get(id);
714
+ const doc = await this.ctx.db.get(this.table, id);
706
715
  if (doc === null) {
707
716
  throw new Error(
708
717
  `Cannot update document with ID "${id}" in table "${this.table} because it does not exist"`
@@ -721,7 +730,7 @@ var WriterImplBase = class _WriterImplBase {
721
730
  return;
722
731
  }
723
732
  const ent = id === void 0 ? void 0 : entWrapper(
724
- await this.ctx.db.get(id),
733
+ await this.ctx.db.get(this.table, id),
725
734
  this.ctx,
726
735
  this.entDefinitions,
727
736
  this.table
@@ -751,9 +760,6 @@ var WriterImplBase = class _WriterImplBase {
751
760
  }
752
761
  }
753
762
  };
754
- function isSystemTable(table) {
755
- return table.startsWith("_");
756
- }
757
763
 
758
764
  // src/functions.ts
759
765
  var PromiseQueryOrNullImpl = class _PromiseQueryOrNullImpl extends Promise {
@@ -993,7 +999,7 @@ var PromiseTableImpl = class extends PromiseQueryOrNullImpl {
993
999
  ctx,
994
1000
  entDefinitions,
995
1001
  table,
996
- async () => isSystemTable(table) ? ctx.db.system.query(table) : ctx.db.query(table)
1002
+ async () => systemAwareQuery(ctx.db, table)
997
1003
  );
998
1004
  }
999
1005
  get(...args) {
@@ -1021,7 +1027,7 @@ var PromiseTableImpl = class extends PromiseQueryOrNullImpl {
1021
1027
  return {
1022
1028
  id,
1023
1029
  doc: async () => {
1024
- const doc = await (isSystemTable(this.table) ? this.ctx.db.system.get(id) : this.ctx.db.get(id));
1030
+ const doc = await systemAwareGet(this.ctx.db, this.table, id);
1025
1031
  if (throwIfNull && doc === null) {
1026
1032
  throw new Error(
1027
1033
  `Document not found with id \`${id}\` in table "${this.table}"`
@@ -1037,7 +1043,7 @@ var PromiseTableImpl = class extends PromiseQueryOrNullImpl {
1037
1043
  this.table,
1038
1044
  indexName
1039
1045
  );
1040
- const doc = await this.ctx.db.query(this.table).withIndex(
1046
+ const doc = await systemAwareQuery(this.ctx.db, this.table).withIndex(
1041
1047
  indexName,
1042
1048
  (q) => values.reduce((q2, value, i) => q2.eq(fieldNames[i], value), q)
1043
1049
  ).unique();
@@ -1070,7 +1076,7 @@ var PromiseTableImpl = class extends PromiseQueryOrNullImpl {
1070
1076
  });
1071
1077
  return await Promise.all(
1072
1078
  ids.map(async (id) => {
1073
- const doc = await (isSystemTable(this.table) ? this.ctx.db.system.get(id) : this.ctx.db.get(id));
1079
+ const doc = await systemAwareGet(this.ctx.db, this.table, id);
1074
1080
  if (throwIfNull && doc === null) {
1075
1081
  throw new Error(
1076
1082
  `Document not found with id \`${id}\` in table "${this.table}"`
@@ -1081,12 +1087,23 @@ var PromiseTableImpl = class extends PromiseQueryOrNullImpl {
1081
1087
  );
1082
1088
  } : async () => {
1083
1089
  const [indexName, values] = args;
1090
+ const fieldNames = getIndexFields(
1091
+ this.entDefinitions,
1092
+ this.table,
1093
+ indexName
1094
+ );
1095
+ if (fieldNames.length > 1) {
1096
+ throw new Error(
1097
+ `Index "${indexName}" has ${fieldNames.length} fields, but getMany() supports only single field indexes`
1098
+ );
1099
+ }
1100
+ const fieldName = fieldNames[0];
1084
1101
  return await Promise.all(
1085
1102
  values.map(async (value) => {
1086
- const doc = await this.ctx.db.query(this.table).withIndex(indexName, (q) => q.eq(indexName, value)).unique();
1103
+ const doc = await systemAwareQuery(this.ctx.db, this.table).withIndex(indexName, (q) => q.eq(fieldName, value)).unique();
1087
1104
  if (throwIfNull && doc === null) {
1088
1105
  throw new Error(
1089
- `Table "${this.table}" does not contain document with field "${indexName}" = \`${value}\``
1106
+ `Table "${this.table}" does not contain document with field "${fieldName}" = \`${value}\``
1090
1107
  );
1091
1108
  }
1092
1109
  return doc;
@@ -1247,7 +1264,11 @@ var PromiseEdgeOrNullImpl = class _PromiseEdgeOrNullImpl extends PromiseEntsOrNu
1247
1264
  constructor(ctx, entDefinitions, table, edgeDefinition, retrieveSourceId, retrieveQuery, retrieveDoc = async (edgeDoc) => {
1248
1265
  const sourceId = edgeDoc[edgeDefinition.field];
1249
1266
  const targetId = edgeDoc[edgeDefinition.ref];
1250
- const doc = await this.ctx.db.get(targetId);
1267
+ const doc = await systemAwareGet(
1268
+ this.ctx.db,
1269
+ edgeDefinition.to,
1270
+ targetId
1271
+ );
1251
1272
  if (doc === null) {
1252
1273
  throw new Error(
1253
1274
  `Dangling reference for edge "${edgeDefinition.name}" in table "${this.table}" for document with ID "${sourceId}": Could not find a document with ID "${targetId}" in table "${edgeDefinition.to}" (edge document ID is "${edgeDoc._id}").`
@@ -1543,7 +1564,11 @@ var PromiseEntOrNullImpl = class extends Promise {
1543
1564
  `Unexpected null reference for edge "${edgeDefinition.name}" in table "${this.table}" on document with ID "${id}": Expected an ID for a document in table "${edgeDefinition.to}".`
1544
1565
  );
1545
1566
  }
1546
- const otherDoc = await this.ctx.db.get(otherId);
1567
+ const otherDoc = await systemAwareGet(
1568
+ this.ctx.db,
1569
+ edgeDefinition.to,
1570
+ otherId
1571
+ );
1547
1572
  if (otherDoc === null && edgeDefinition.to !== "_scheduled_functions") {
1548
1573
  throw new Error(
1549
1574
  `Dangling reference for edge "${edgeDefinition.name}" in table "${this.table}" on document with ID "${id}": Could not find a document with ID "${otherId}" in table "${edgeDefinition.to}".`
@@ -1816,7 +1841,7 @@ var PromiseEntWriterImpl = class extends PromiseEntOrNullImpl {
1816
1841
  }
1817
1842
  if (edgeDefinition.cardinality === "single") {
1818
1843
  if (edgeDefinition.type === "ref") {
1819
- const oldDoc = await this.ctx.db.get(docId);
1844
+ const oldDoc = await this.ctx.db.get(this.table, docId);
1820
1845
  if (oldDoc[key] !== void 0 && oldDoc[key] !== idOrIds) {
1821
1846
  throw new Error("Cannot set 1:1 edge from ref end.");
1822
1847
  }
@@ -1878,7 +1903,10 @@ var PromiseEntIdImpl = class extends Promise {
1878
1903
  this.table,
1879
1904
  async () => {
1880
1905
  const id = await this.retrieve();
1881
- return { id, doc: async () => this.ctx.db.get(id) };
1906
+ return {
1907
+ id,
1908
+ doc: async () => systemAwareGet(this.ctx.db, this.table, id)
1909
+ };
1882
1910
  },
1883
1911
  true
1884
1912
  );
@@ -1963,6 +1991,12 @@ async function filterByReadRule(ctx, entDefinitions, table, docs, throwIfNull) {
1963
1991
  return docs.filter((_, i) => decisions[i]);
1964
1992
  }
1965
1993
  function getIndexFields(entDefinitions, table, index) {
1994
+ if (index === "by_id") {
1995
+ return ["_id"];
1996
+ }
1997
+ if (index === "by_creation_time") {
1998
+ return ["_creationTime"];
1999
+ }
1966
2000
  return entDefinitions[table].indexes[index];
1967
2001
  }
1968
2002
  function getReadRule(entDefinitions, table) {
@@ -2015,11 +2049,7 @@ function scheduledDeleteFactory(entDefinitions, options) {
2015
2049
  inProgress: import_values2.v.boolean()
2016
2050
  },
2017
2051
  handler: async (ctx, { origin, stack, inProgress }) => {
2018
- const originId = ctx.db.normalizeId(origin.table, origin.id);
2019
- if (originId === null) {
2020
- throw new Error(`Invalid ID "${origin.id}" for table ${origin.table}`);
2021
- }
2022
- const doc = await ctx.db.get(originId);
2052
+ const doc = await ctx.db.get(origin.table, origin.id);
2023
2053
  if (doc.deletionTime !== origin.deletionTime) {
2024
2054
  if (inProgress) {
2025
2055
  console.error(
@@ -2037,7 +2067,7 @@ function scheduledDeleteFactory(entDefinitions, options) {
2037
2067
  newCounter(),
2038
2068
  inProgress ? stack : [
2039
2069
  {
2040
- id: originId,
2070
+ id: origin.id,
2041
2071
  table: origin.table,
2042
2072
  edges: getEdgeArgs(entDefinitions, origin.table)
2043
2073
  }