convex-ents 0.14.0-alpha.0 → 0.14.0-alpha.1

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
@@ -226,7 +226,15 @@ function deletionConfigFromEntDefinition(table) {
226
226
  return table.deletionConfig;
227
227
  }
228
228
  function defineEnt(documentSchema) {
229
- return new EntDefinitionImpl(documentSchema);
229
+ if (isValidator(documentSchema)) {
230
+ if (!(documentSchema.kind === "object" || documentSchema.kind === "union" && documentSchema.members.every((member) => member.kind === "object"))) {
231
+ throw new Error("Ent shape must be an object or a union of objects");
232
+ }
233
+ }
234
+ return new EntDefinitionImpl((0, import_values.asObjectValidator)(documentSchema));
235
+ }
236
+ function isValidator(v3) {
237
+ return !!v3.isConvexValidator;
230
238
  }
231
239
  function defineEntFromTable(definition) {
232
240
  const validator = definition.validator;
@@ -259,14 +267,12 @@ var EntDefinitionImpl = class {
259
267
  // eslint-disable-next-line @typescript-eslint/ban-ts-comment
260
268
  // @ts-ignore
261
269
  vectorIndexes = [];
262
- documentSchema;
263
270
  edgeConfigs = {};
264
271
  fieldConfigs = {};
265
272
  defaults = {};
266
273
  deletionConfig;
267
274
  constructor(documentSchema) {
268
- this.documentSchema = documentSchema;
269
- this.validator = import_values.v.object(documentSchema);
275
+ this.validator = documentSchema;
270
276
  }
271
277
  index(name, fields) {
272
278
  this.indexes.push({ indexDescriptor: name, fields });
@@ -300,15 +306,15 @@ var EntDefinitionImpl = class {
300
306
  indexes: this.indexes,
301
307
  searchIndexes: this.searchIndexes,
302
308
  vectorIndexes: this.vectorIndexes,
303
- documentType: import_values.v.object(this.documentSchema).json
309
+ documentType: this.validator.json
304
310
  };
305
311
  }
306
312
  field(name, validator, options) {
307
- if (this.documentSchema[name] !== void 0) {
313
+ if (this._has(name)) {
308
314
  throw new Error(`Duplicate field "${name}"`);
309
315
  }
310
316
  const finalValidator = options?.default !== void 0 ? import_values.v.optional(validator) : validator;
311
- this.documentSchema = { ...this.documentSchema, [name]: finalValidator };
317
+ this._expand(name, finalValidator);
312
318
  if (options?.unique === true || options?.index === true) {
313
319
  this.indexes.push({ indexDescriptor: name, fields: [name] });
314
320
  }
@@ -332,10 +338,10 @@ var EntDefinitionImpl = class {
332
338
  }
333
339
  if (options?.field !== void 0 || options?.ref === void 0) {
334
340
  const fieldName = options?.field ?? edgeName + "Id";
335
- this.documentSchema = {
336
- ...this.documentSchema,
337
- [fieldName]: options?.optional === true ? import_values.v.optional(import_values.v.id(to)) : import_values.v.id(to)
338
- };
341
+ this._expand(
342
+ fieldName,
343
+ options?.optional === true ? import_values.v.optional(import_values.v.id(to)) : import_values.v.id(to)
344
+ );
339
345
  this.edgeConfigs[edgeName] = {
340
346
  name: edgeName,
341
347
  to,
@@ -394,7 +400,7 @@ var EntDefinitionImpl = class {
394
400
  return this;
395
401
  }
396
402
  deletion(type, options) {
397
- if (this.documentSchema.deletionTime !== void 0) {
403
+ if (this._has("deletionTime")) {
398
404
  throw new Error(
399
405
  `Cannot enable "${type}" deletion because "deletionTime" field was already defined.`
400
406
  );
@@ -402,13 +408,33 @@ var EntDefinitionImpl = class {
402
408
  if (this.deletionConfig !== void 0) {
403
409
  throw new Error(`Deletion behavior can only be specified once.`);
404
410
  }
405
- this.documentSchema = {
406
- ...this.documentSchema,
407
- deletionTime: import_values.v.optional(import_values.v.number())
408
- };
411
+ this._expand("deletionTime", import_values.v.optional(import_values.v.number()));
409
412
  this.deletionConfig = { type, ...options };
410
413
  return this;
411
414
  }
415
+ _has(name) {
416
+ if (this.validator.kind === "object") {
417
+ return this.validator.fields[name] !== void 0;
418
+ }
419
+ if (this.validator.kind === "union") {
420
+ return this.validator.members.some(
421
+ (member) => member.kind === "object" && member.fields[name] !== void 0
422
+ );
423
+ }
424
+ return false;
425
+ }
426
+ _expand(name, validator) {
427
+ if (this.validator.kind === "object") {
428
+ this.validator.fields[name] = validator;
429
+ }
430
+ if (this.validator.kind === "union") {
431
+ this.validator.members.forEach((member) => {
432
+ if (member.kind === "object") {
433
+ member.fields[name] = validator;
434
+ }
435
+ });
436
+ }
437
+ }
412
438
  };
413
439
  function getEntDefinitions(schema) {
414
440
  const tables = schema.tables;