convex-ents 0.11.0 → 0.12.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
@@ -305,15 +305,19 @@ var EntDefinitionImpl = class {
305
305
  throw new Error(`Duplicate edge "${edgeName}"`);
306
306
  }
307
307
  const to = options?.to ?? edgeName + "s";
308
- if (options?.optional !== true) {
308
+ if (options?.field !== void 0 || options?.optional !== true) {
309
309
  const fieldName = options?.field ?? edgeName + "Id";
310
- this.documentSchema = { ...this.documentSchema, [fieldName]: import_values.v.id(to) };
310
+ this.documentSchema = {
311
+ ...this.documentSchema,
312
+ [fieldName]: options?.optional === true ? import_values.v.optional(import_values.v.id(to)) : import_values.v.id(to)
313
+ };
311
314
  this.edgeConfigs[edgeName] = {
312
315
  name: edgeName,
313
316
  to,
314
317
  cardinality: "single",
315
318
  type: "field",
316
- field: fieldName
319
+ field: fieldName,
320
+ optional: options?.optional === true
317
321
  };
318
322
  this.indexes.push({
319
323
  indexDescriptor: fieldName,
@@ -1185,7 +1189,7 @@ var PromiseEdgeOrNullImpl = class _PromiseEdgeOrNullImpl extends PromiseEntsOrNu
1185
1189
  if (sourceId === null) {
1186
1190
  return null;
1187
1191
  }
1188
- const edgeDoc = this.ctx.db.query(this.edgeDefinition.table).withIndex(
1192
+ const edgeDoc = await this.ctx.db.query(this.edgeDefinition.table).withIndex(
1189
1193
  edgeCompoundIndexName(this.edgeDefinition),
1190
1194
  (q) => q.eq(this.edgeDefinition.field, sourceId).eq(
1191
1195
  this.edgeDefinition.ref,
@@ -1441,10 +1445,18 @@ var PromiseEntOrNullImpl = class extends Promise {
1441
1445
  return {
1442
1446
  id: otherId,
1443
1447
  doc: async () => {
1448
+ if (otherId === void 0) {
1449
+ if (edgeDefinition.optional) {
1450
+ return null;
1451
+ }
1452
+ throw new Error(
1453
+ `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}".`
1454
+ );
1455
+ }
1444
1456
  const otherDoc = await this.ctx.db.get(otherId);
1445
1457
  if (otherDoc === null) {
1446
1458
  throw new Error(
1447
- `Dangling reference for edge "${edgeDefinition.name}" in table "${this.table}" for document with ID "${id}": Could not find a document with ID "${otherId}" in table "${edgeDefinition.to}".`
1459
+ `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}".`
1448
1460
  );
1449
1461
  }
1450
1462
  return otherDoc;