@tinacms/graphql 1.5.13 → 1.5.15

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
@@ -3019,7 +3019,7 @@ var validateField = async (field) => {
3019
3019
  // package.json
3020
3020
  var package_default = {
3021
3021
  name: "@tinacms/graphql",
3022
- version: "1.5.13",
3022
+ version: "1.5.15",
3023
3023
  main: "dist/index.js",
3024
3024
  module: "dist/index.mjs",
3025
3025
  typings: "dist/index.d.ts",
@@ -3260,7 +3260,9 @@ var _buildSchema = async (builder, tinaSchema) => {
3260
3260
  await builder.buildCreateCollectionFolderMutation()
3261
3261
  );
3262
3262
  await sequential(collections, async (collection) => {
3263
- queryTypeDefinitionFields.push(await builder.collectionDocument(collection));
3263
+ queryTypeDefinitionFields.push(
3264
+ await builder.collectionDocument(collection)
3265
+ );
3264
3266
  if (collection.isAuthCollection) {
3265
3267
  queryTypeDefinitionFields.push(
3266
3268
  await builder.authenticationCollectionDocument(collection)
@@ -3575,7 +3577,9 @@ var LevelProxyHandler = {
3575
3577
  throw new Error(`The property, ${property.toString()}, doesn't exist`);
3576
3578
  }
3577
3579
  if (typeof target[property] !== "function") {
3578
- throw new Error(`The property, ${property.toString()}, is not a function`);
3580
+ throw new Error(
3581
+ `The property, ${property.toString()}, is not a function`
3582
+ );
3579
3583
  }
3580
3584
  if (property === "get") {
3581
3585
  return async (...args) => {
@@ -3960,6 +3964,9 @@ var loadAndParseWithAliases = async (bridge, filepath, collection, templateInfo)
3960
3964
 
3961
3965
  // src/database/datalayer.ts
3962
3966
  var DEFAULT_COLLECTION_SORT_KEY = "__filepath__";
3967
+ var REFS_COLLECTIONS_SORT_KEY = "__refs__";
3968
+ var REFS_REFERENCE_FIELD = "__tina_ref__";
3969
+ var REFS_PATH_FIELD = "__tina_ref_path__";
3963
3970
  var DEFAULT_NUMERIC_LPAD = 4;
3964
3971
  var applyPadding = (input, pad) => {
3965
3972
  if (pad) {
@@ -4533,6 +4540,57 @@ var makeIndexOpsForDocument = (filepath, collection, indexDefinitions, data, opT
4533
4540
  }
4534
4541
  return result;
4535
4542
  };
4543
+ var makeRefOpsForDocument = (filepath, collection, references, data, opType, level) => {
4544
+ const result = [];
4545
+ if (collection) {
4546
+ for (const [c, referencePaths] of Object.entries(references || {})) {
4547
+ if (!referencePaths.length) {
4548
+ continue;
4549
+ }
4550
+ const collectionSublevel = level.sublevel(c, SUBLEVEL_OPTIONS);
4551
+ const refSublevel = collectionSublevel.sublevel(
4552
+ REFS_COLLECTIONS_SORT_KEY,
4553
+ SUBLEVEL_OPTIONS
4554
+ );
4555
+ const references2 = {};
4556
+ for (const path7 of referencePaths) {
4557
+ const ref = JSONPath({ path: path7, json: data });
4558
+ if (!ref) {
4559
+ continue;
4560
+ }
4561
+ if (Array.isArray(ref)) {
4562
+ for (const r of ref) {
4563
+ if (!r) {
4564
+ continue;
4565
+ }
4566
+ if (references2[r]) {
4567
+ references2[r].push(path7);
4568
+ } else {
4569
+ references2[r] = [path7];
4570
+ }
4571
+ }
4572
+ } else {
4573
+ if (references2[ref]) {
4574
+ references2[ref].push(path7);
4575
+ } else {
4576
+ references2[ref] = [path7];
4577
+ }
4578
+ }
4579
+ }
4580
+ for (const ref of Object.keys(references2)) {
4581
+ for (const path7 of references2[ref]) {
4582
+ result.push({
4583
+ type: opType,
4584
+ key: `${ref}${INDEX_KEY_FIELD_SEPARATOR}${path7}${INDEX_KEY_FIELD_SEPARATOR}${filepath}`,
4585
+ sublevel: refSublevel,
4586
+ value: opType === "put" ? {} : void 0
4587
+ });
4588
+ }
4589
+ }
4590
+ }
4591
+ }
4592
+ return result;
4593
+ };
4536
4594
  var makeStringEscaper = (regex, replacement) => {
4537
4595
  return (input) => {
4538
4596
  if (Array.isArray(input)) {
@@ -4753,24 +4811,33 @@ var transformDocumentIntoPayload = async (fullPath, rawData, tinaSchema, config,
4753
4811
  throw e;
4754
4812
  }
4755
4813
  };
4756
- var updateObjectWithJsonPath = (obj, path7, newValue) => {
4814
+ var updateObjectWithJsonPath = (obj, path7, oldValue, newValue) => {
4815
+ let updated = false;
4757
4816
  if (!path7.includes(".") && !path7.includes("[")) {
4758
- if (path7 in obj) {
4817
+ if (path7 in obj && obj[path7] === oldValue) {
4759
4818
  obj[path7] = newValue;
4819
+ updated = true;
4760
4820
  }
4761
- return obj;
4762
- }
4763
- const parentPath = path7.replace(/\.[^.]+$/, "");
4764
- const keyToUpdate = path7.match(/[^.]+$/)[0];
4765
- const parents = JSONPath2({ path: parentPath, json: obj, resultType: "value" });
4821
+ return { object: obj, updated };
4822
+ }
4823
+ const parentPath = path7.replace(/\.[^.\[\]]+$/, "");
4824
+ const keyToUpdate = path7.match(/[^.\[\]]+$/)[0];
4825
+ const parents = JSONPath2({
4826
+ path: parentPath,
4827
+ json: obj,
4828
+ resultType: "value"
4829
+ });
4766
4830
  if (parents.length > 0) {
4767
4831
  parents.forEach((parent) => {
4768
4832
  if (parent && typeof parent === "object" && keyToUpdate in parent) {
4769
- parent[keyToUpdate] = newValue;
4833
+ if (parent[keyToUpdate] === oldValue) {
4834
+ parent[keyToUpdate] = newValue;
4835
+ updated = true;
4836
+ }
4770
4837
  }
4771
4838
  });
4772
4839
  }
4773
- return obj;
4840
+ return { object: obj, updated };
4774
4841
  };
4775
4842
  var Resolver = class {
4776
4843
  constructor(init) {
@@ -4787,7 +4854,9 @@ var Resolver = class {
4787
4854
  };
4788
4855
  this.getRaw = async (fullPath) => {
4789
4856
  if (typeof fullPath !== "string") {
4790
- throw new Error(`fullPath must be of type string for getDocument request`);
4857
+ throw new Error(
4858
+ `fullPath must be of type string for getDocument request`
4859
+ );
4791
4860
  }
4792
4861
  return this.database.get(fullPath);
4793
4862
  };
@@ -4816,7 +4885,9 @@ var Resolver = class {
4816
4885
  };
4817
4886
  this.getDocument = async (fullPath, opts = {}) => {
4818
4887
  if (typeof fullPath !== "string") {
4819
- throw new Error(`fullPath must be of type string for getDocument request`);
4888
+ throw new Error(
4889
+ `fullPath must be of type string for getDocument request`
4890
+ );
4820
4891
  }
4821
4892
  const rawData = await this.getRaw(fullPath);
4822
4893
  const hasReferences = opts?.checkReferences ? await this.hasReferences(fullPath, opts.collection) : void 0;
@@ -4831,7 +4902,9 @@ var Resolver = class {
4831
4902
  };
4832
4903
  this.deleteDocument = async (fullPath) => {
4833
4904
  if (typeof fullPath !== "string") {
4834
- throw new Error(`fullPath must be of type string for getDocument request`);
4905
+ throw new Error(
4906
+ `fullPath must be of type string for getDocument request`
4907
+ );
4835
4908
  }
4836
4909
  await this.database.delete(fullPath);
4837
4910
  };
@@ -5035,7 +5108,11 @@ var Resolver = class {
5035
5108
  collection,
5036
5109
  doc?._rawData
5037
5110
  );
5038
- await this.database.put(realPath, { ...oldDoc, ...params }, collection.name);
5111
+ await this.database.put(
5112
+ realPath,
5113
+ { ...oldDoc, ...params },
5114
+ collection.name
5115
+ );
5039
5116
  return this.getDocument(realPath);
5040
5117
  };
5041
5118
  /**
@@ -5148,17 +5225,35 @@ var Resolver = class {
5148
5225
  await this.deleteDocument(realPath);
5149
5226
  if (await this.hasReferences(realPath, collection)) {
5150
5227
  const collRefs = await this.findReferences(realPath, collection);
5151
- for (const [collection2, refFields] of Object.entries(collRefs)) {
5152
- for (const [refPath, refs] of Object.entries(refFields)) {
5153
- let refDoc = await this.getRaw(refPath);
5154
- for (const ref of refs) {
5155
- refDoc = updateObjectWithJsonPath(
5228
+ for (const [collection2, docsWithRefs] of Object.entries(collRefs)) {
5229
+ for (const [pathToDocWithRef, referencePaths] of Object.entries(
5230
+ docsWithRefs
5231
+ )) {
5232
+ let refDoc = await this.getRaw(pathToDocWithRef);
5233
+ let hasUpdate = false;
5234
+ for (const path7 of referencePaths) {
5235
+ const { object: object2, updated } = updateObjectWithJsonPath(
5156
5236
  refDoc,
5157
- ref.path.join("."),
5237
+ path7,
5238
+ realPath,
5158
5239
  null
5159
5240
  );
5241
+ refDoc = object2;
5242
+ hasUpdate = updated || hasUpdate;
5243
+ }
5244
+ if (hasUpdate) {
5245
+ const collectionWithRef = this.tinaSchema.getCollectionByFullPath(pathToDocWithRef);
5246
+ if (!collectionWithRef) {
5247
+ throw new Error(
5248
+ `Unable to find collection for ${pathToDocWithRef}`
5249
+ );
5250
+ }
5251
+ await this.database.put(
5252
+ pathToDocWithRef,
5253
+ refDoc,
5254
+ collectionWithRef.name
5255
+ );
5160
5256
  }
5161
- await this.database.put(refPath, refDoc, collection2);
5162
5257
  }
5163
5258
  }
5164
5259
  }
@@ -5184,23 +5279,43 @@ var Resolver = class {
5184
5279
  await this.database.put(newRealPath, doc._rawData, collection.name);
5185
5280
  await this.deleteDocument(realPath);
5186
5281
  const collRefs = await this.findReferences(realPath, collection);
5187
- for (const [collection2, refFields] of Object.entries(collRefs)) {
5188
- for (const [refPath, refs] of Object.entries(refFields)) {
5189
- let refDoc = await this.getRaw(refPath);
5190
- for (const ref of refs) {
5191
- refDoc = updateObjectWithJsonPath(
5192
- refDoc,
5193
- ref.path.join("."),
5282
+ for (const [collection2, docsWithRefs] of Object.entries(collRefs)) {
5283
+ for (const [pathToDocWithRef, referencePaths] of Object.entries(
5284
+ docsWithRefs
5285
+ )) {
5286
+ let docWithRef = await this.getRaw(pathToDocWithRef);
5287
+ let hasUpdate = false;
5288
+ for (const path7 of referencePaths) {
5289
+ const { object: object2, updated } = updateObjectWithJsonPath(
5290
+ docWithRef,
5291
+ path7,
5292
+ realPath,
5194
5293
  newRealPath
5195
5294
  );
5295
+ docWithRef = object2;
5296
+ hasUpdate = updated || hasUpdate;
5297
+ }
5298
+ if (hasUpdate) {
5299
+ const collectionWithRef = this.tinaSchema.getCollectionByFullPath(pathToDocWithRef);
5300
+ if (!collectionWithRef) {
5301
+ throw new Error(
5302
+ `Unable to find collection for ${pathToDocWithRef}`
5303
+ );
5304
+ }
5305
+ await this.database.put(
5306
+ pathToDocWithRef,
5307
+ docWithRef,
5308
+ collectionWithRef.name
5309
+ );
5196
5310
  }
5197
- await this.database.put(refPath, refDoc, collection2);
5198
5311
  }
5199
5312
  }
5200
5313
  return this.getDocument(newRealPath);
5201
5314
  }
5202
5315
  if (alreadyExists === false) {
5203
- throw new Error(`Unable to update document, ${realPath} does not exist`);
5316
+ throw new Error(
5317
+ `Unable to update document, ${realPath} does not exist`
5318
+ );
5204
5319
  }
5205
5320
  return this.updateResolveDocument({
5206
5321
  collection,
@@ -5326,35 +5441,30 @@ var Resolver = class {
5326
5441
  */
5327
5442
  this.hasReferences = async (id, c) => {
5328
5443
  let count = 0;
5329
- const deepRefs = this.tinaSchema.findReferences(c.name);
5330
- for (const [collection, refs] of Object.entries(deepRefs)) {
5331
- for (const ref of refs) {
5332
- await this.database.query(
5333
- {
5334
- collection,
5335
- filterChain: makeFilterChain({
5336
- conditions: [
5337
- {
5338
- filterPath: ref.path.join("."),
5339
- filterExpression: {
5340
- _type: "reference",
5341
- _list: false,
5342
- eq: id
5343
- }
5344
- }
5345
- ]
5346
- }),
5347
- sort: ref.field.name
5348
- },
5349
- (refId) => {
5350
- count++;
5351
- return refId;
5352
- }
5353
- );
5354
- if (count) {
5355
- return true;
5356
- }
5444
+ await this.database.query(
5445
+ {
5446
+ collection: c.name,
5447
+ filterChain: makeFilterChain({
5448
+ conditions: [
5449
+ {
5450
+ filterPath: REFS_REFERENCE_FIELD,
5451
+ filterExpression: {
5452
+ _type: "string",
5453
+ _list: false,
5454
+ eq: id
5455
+ }
5456
+ }
5457
+ ]
5458
+ }),
5459
+ sort: REFS_COLLECTIONS_SORT_KEY
5460
+ },
5461
+ (refId) => {
5462
+ count++;
5463
+ return refId;
5357
5464
  }
5465
+ );
5466
+ if (count) {
5467
+ return true;
5358
5468
  }
5359
5469
  return false;
5360
5470
  };
@@ -5362,46 +5472,41 @@ var Resolver = class {
5362
5472
  * Finds references to a document
5363
5473
  * @param id the id of the document to find references to
5364
5474
  * @param c the collection to find references in
5365
- * @returns references to the document in the form of a map of collection names to a list of fields that reference the document
5475
+ * @returns a map of references to the document
5366
5476
  */
5367
5477
  this.findReferences = async (id, c) => {
5368
5478
  const references = {};
5369
- const deepRefs = this.tinaSchema.findReferences(c.name);
5370
- for (const [collection, refs] of Object.entries(deepRefs)) {
5371
- for (const ref of refs) {
5372
- await this.database.query(
5373
- {
5374
- collection,
5375
- filterChain: makeFilterChain({
5376
- conditions: [
5377
- {
5378
- filterPath: ref.path.join("."),
5379
- filterExpression: {
5380
- _type: "reference",
5381
- _list: false,
5382
- eq: id
5383
- }
5384
- }
5385
- ]
5386
- }),
5387
- sort: ref.field.name
5388
- },
5389
- (refId) => {
5390
- if (!references[collection]) {
5391
- references[collection] = {};
5392
- }
5393
- if (!references[collection][refId]) {
5394
- references[collection][refId] = [];
5479
+ await this.database.query(
5480
+ {
5481
+ collection: c.name,
5482
+ filterChain: makeFilterChain({
5483
+ conditions: [
5484
+ {
5485
+ filterPath: REFS_REFERENCE_FIELD,
5486
+ filterExpression: {
5487
+ _type: "string",
5488
+ _list: false,
5489
+ eq: id
5490
+ }
5395
5491
  }
5396
- references[collection][refId].push({
5397
- path: ref.path,
5398
- field: ref.field
5399
- });
5400
- return refId;
5401
- }
5402
- );
5492
+ ]
5493
+ }),
5494
+ sort: REFS_COLLECTIONS_SORT_KEY
5495
+ },
5496
+ (refId, rawItem) => {
5497
+ if (!references[c.name]) {
5498
+ references[c.name] = {};
5499
+ }
5500
+ if (!references[c.name][refId]) {
5501
+ references[c.name][refId] = [];
5502
+ }
5503
+ const referencePath = rawItem?.[REFS_PATH_FIELD];
5504
+ if (referencePath) {
5505
+ references[c.name][refId].push(referencePath);
5506
+ }
5507
+ return refId;
5403
5508
  }
5404
- }
5509
+ );
5405
5510
  return references;
5406
5511
  };
5407
5512
  this.buildFieldMutations = async (fieldParams, template, existingData) => {
@@ -6145,6 +6250,7 @@ var Database = class {
6145
6250
  );
6146
6251
  const indexDefinitions = await this.getIndexDefinitions(this.contentLevel);
6147
6252
  const collectionIndexDefinitions = indexDefinitions?.[collection.name];
6253
+ const collectionReferences = (await this.getCollectionReferences())?.[collection.name];
6148
6254
  const normalizedPath = normalizePath(filepath);
6149
6255
  if (!collection?.isDetached) {
6150
6256
  if (this.bridge) {
@@ -6173,6 +6279,14 @@ var Database = class {
6173
6279
  let delOps = [];
6174
6280
  if (!isGitKeep(normalizedPath, collection)) {
6175
6281
  putOps = [
6282
+ ...makeRefOpsForDocument(
6283
+ normalizedPath,
6284
+ collection?.name,
6285
+ collectionReferences,
6286
+ dataFields,
6287
+ "put",
6288
+ level
6289
+ ),
6176
6290
  ...makeIndexOpsForDocument(
6177
6291
  normalizedPath,
6178
6292
  collection?.name,
@@ -6196,6 +6310,14 @@ var Database = class {
6196
6310
  SUBLEVEL_OPTIONS
6197
6311
  ).get(normalizedPath);
6198
6312
  delOps = existingItem ? [
6313
+ ...makeRefOpsForDocument(
6314
+ normalizedPath,
6315
+ collection?.name,
6316
+ collectionReferences,
6317
+ existingItem,
6318
+ "del",
6319
+ level
6320
+ ),
6199
6321
  ...makeIndexOpsForDocument(
6200
6322
  normalizedPath,
6201
6323
  collection?.name,
@@ -6243,6 +6365,7 @@ var Database = class {
6243
6365
  );
6244
6366
  collectionIndexDefinitions = indexDefinitions?.[collectionName];
6245
6367
  }
6368
+ const collectionReferences = (await this.getCollectionReferences())?.[collectionName];
6246
6369
  const normalizedPath = normalizePath(filepath);
6247
6370
  const dataFields = await this.formatBodyOnPayload(filepath, data);
6248
6371
  const collection = await this.collectionForPath(filepath);
@@ -6290,6 +6413,14 @@ var Database = class {
6290
6413
  let delOps = [];
6291
6414
  if (!isGitKeep(normalizedPath, collection)) {
6292
6415
  putOps = [
6416
+ ...makeRefOpsForDocument(
6417
+ normalizedPath,
6418
+ collectionName,
6419
+ collectionReferences,
6420
+ dataFields,
6421
+ "put",
6422
+ level
6423
+ ),
6293
6424
  ...makeIndexOpsForDocument(
6294
6425
  normalizedPath,
6295
6426
  collectionName,
@@ -6313,6 +6444,14 @@ var Database = class {
6313
6444
  SUBLEVEL_OPTIONS
6314
6445
  ).get(normalizedPath);
6315
6446
  delOps = existingItem ? [
6447
+ ...makeRefOpsForDocument(
6448
+ normalizedPath,
6449
+ collectionName,
6450
+ collectionReferences,
6451
+ existingItem,
6452
+ "del",
6453
+ level
6454
+ ),
6316
6455
  ...makeIndexOpsForDocument(
6317
6456
  normalizedPath,
6318
6457
  collectionName,
@@ -6475,6 +6614,22 @@ var Database = class {
6475
6614
  this.tinaSchema = await createSchema({ schema });
6476
6615
  return this.tinaSchema;
6477
6616
  };
6617
+ this.getCollectionReferences = async (level) => {
6618
+ if (this.collectionReferences) {
6619
+ return this.collectionReferences;
6620
+ }
6621
+ const result = {};
6622
+ const schema = await this.getSchema(level || this.contentLevel);
6623
+ const collections = schema.getCollections();
6624
+ for (const collection of collections) {
6625
+ const collectionReferences = this.tinaSchema.findReferencesFromCollection(
6626
+ collection.name
6627
+ );
6628
+ result[collection.name] = collectionReferences;
6629
+ }
6630
+ this.collectionReferences = result;
6631
+ return result;
6632
+ };
6478
6633
  this.getIndexDefinitions = async (level) => {
6479
6634
  if (!this.collectionIndexDefinitions) {
6480
6635
  await new Promise(async (resolve2, reject) => {
@@ -6484,11 +6639,53 @@ var Database = class {
6484
6639
  const collections = schema.getCollections();
6485
6640
  for (const collection of collections) {
6486
6641
  const indexDefinitions = {
6487
- [DEFAULT_COLLECTION_SORT_KEY]: { fields: [] }
6642
+ [DEFAULT_COLLECTION_SORT_KEY]: { fields: [] },
6488
6643
  // provide a default sort key which is the file sort
6644
+ // pseudo-index for the collection's references
6645
+ [REFS_COLLECTIONS_SORT_KEY]: {
6646
+ fields: [
6647
+ {
6648
+ name: REFS_REFERENCE_FIELD,
6649
+ type: "string",
6650
+ list: false
6651
+ },
6652
+ {
6653
+ name: REFS_PATH_FIELD,
6654
+ type: "string",
6655
+ list: false
6656
+ }
6657
+ ]
6658
+ }
6489
6659
  };
6490
- if (collection.fields) {
6491
- for (const field of collection.fields) {
6660
+ let fields = [];
6661
+ if (collection.templates) {
6662
+ const templateFieldMap = {};
6663
+ const conflictedFields = /* @__PURE__ */ new Set();
6664
+ for (const template of collection.templates) {
6665
+ for (const field of template.fields) {
6666
+ if (!templateFieldMap[field.name]) {
6667
+ templateFieldMap[field.name] = field;
6668
+ } else {
6669
+ if (templateFieldMap[field.name].type !== field.type) {
6670
+ console.warn(
6671
+ `Field ${field.name} has conflicting types in templates - skipping index`
6672
+ );
6673
+ conflictedFields.add(field.name);
6674
+ }
6675
+ }
6676
+ }
6677
+ }
6678
+ for (const conflictedField in conflictedFields) {
6679
+ delete templateFieldMap[conflictedField];
6680
+ }
6681
+ for (const field of Object.values(templateFieldMap)) {
6682
+ fields.push(field);
6683
+ }
6684
+ } else if (collection.fields) {
6685
+ fields = collection.fields;
6686
+ }
6687
+ if (fields) {
6688
+ for (const field of fields) {
6492
6689
  if (field.indexed !== void 0 && field.indexed === false || field.type === "object") {
6493
6690
  continue;
6494
6691
  }
@@ -6636,29 +6833,36 @@ var Database = class {
6636
6833
  }
6637
6834
  startKey = startKey || key || "";
6638
6835
  endKey = key || "";
6639
- edges = [...edges, { cursor: key, path: filepath }];
6836
+ edges = [...edges, { cursor: key, path: filepath, value: itemRecord }];
6640
6837
  }
6641
6838
  return {
6642
- edges: await sequential(edges, async (edge) => {
6643
- try {
6644
- const node = await hydrator(edge.path);
6645
- return {
6646
- node,
6647
- cursor: btoa(edge.cursor)
6648
- };
6649
- } catch (error) {
6650
- console.log(error);
6651
- if (error instanceof Error && (!edge.path.includes(".tina/__generated__/_graphql.json") || !edge.path.includes("tina/__generated__/_graphql.json"))) {
6652
- throw new TinaQueryError({
6653
- originalError: error,
6654
- file: edge.path,
6655
- collection: collection.name,
6656
- stack: error.stack
6657
- });
6839
+ edges: await sequential(
6840
+ edges,
6841
+ async ({
6842
+ cursor,
6843
+ path: path7,
6844
+ value
6845
+ }) => {
6846
+ try {
6847
+ const node = await hydrator(path7, value);
6848
+ return {
6849
+ node,
6850
+ cursor: btoa(cursor)
6851
+ };
6852
+ } catch (error) {
6853
+ console.log(error);
6854
+ if (error instanceof Error && (!path7.includes(".tina/__generated__/_graphql.json") || !path7.includes("tina/__generated__/_graphql.json"))) {
6855
+ throw new TinaQueryError({
6856
+ originalError: error,
6857
+ file: path7,
6858
+ collection: collection.name,
6859
+ stack: error.stack
6860
+ });
6861
+ }
6862
+ throw error;
6658
6863
  }
6659
- throw error;
6660
6864
  }
6661
- }),
6865
+ ),
6662
6866
  pageInfo: {
6663
6867
  hasPreviousPage,
6664
6868
  hasNextPage,
@@ -6804,6 +7008,7 @@ var Database = class {
6804
7008
  throw new Error(`No collection found for path: ${filepath}`);
6805
7009
  }
6806
7010
  const indexDefinitions = await this.getIndexDefinitions(this.contentLevel);
7011
+ const collectionReferences = (await this.getCollectionReferences())?.[collection.name];
6807
7012
  const collectionIndexDefinitions = indexDefinitions?.[collection.name];
6808
7013
  let level = this.contentLevel;
6809
7014
  if (collection?.isDetached) {
@@ -6822,6 +7027,14 @@ var Database = class {
6822
7027
  collection.path || ""
6823
7028
  );
6824
7029
  await this.contentLevel.batch([
7030
+ ...makeRefOpsForDocument(
7031
+ normalizedPath,
7032
+ collection.name,
7033
+ collectionReferences,
7034
+ item,
7035
+ "del",
7036
+ level
7037
+ ),
6825
7038
  ...makeIndexOpsForDocument(
6826
7039
  normalizedPath,
6827
7040
  collection.name,
@@ -6899,7 +7112,13 @@ var Database = class {
6899
7112
  );
6900
7113
  }
6901
7114
  } else {
6902
- await _indexContent(this, level, contentPaths, enqueueOps, collection);
7115
+ await _indexContent(
7116
+ this,
7117
+ level,
7118
+ contentPaths,
7119
+ enqueueOps,
7120
+ collection
7121
+ );
6903
7122
  }
6904
7123
  }
6905
7124
  );
@@ -7049,6 +7268,7 @@ var _indexContent = async (database, level, documentPaths, enqueueOps, collectio
7049
7268
  }
7050
7269
  collectionPath = collection.path;
7051
7270
  }
7271
+ const collectionReferences = (await database.getCollectionReferences())?.[collection?.name];
7052
7272
  const tinaSchema = await database.getSchema();
7053
7273
  let templateInfo = null;
7054
7274
  if (collection) {
@@ -7081,6 +7301,14 @@ var _indexContent = async (database, level, documentPaths, enqueueOps, collectio
7081
7301
  const item = await rootSublevel.get(normalizedPath);
7082
7302
  if (item) {
7083
7303
  await database.contentLevel.batch([
7304
+ ...makeRefOpsForDocument(
7305
+ normalizedPath,
7306
+ collection?.name,
7307
+ collectionReferences,
7308
+ item,
7309
+ "del",
7310
+ level
7311
+ ),
7084
7312
  ...makeIndexOpsForDocument(
7085
7313
  normalizedPath,
7086
7314
  collection.name,
@@ -7107,6 +7335,14 @@ var _indexContent = async (database, level, documentPaths, enqueueOps, collectio
7107
7335
  }
7108
7336
  if (!isGitKeep(filepath, collection)) {
7109
7337
  await enqueueOps([
7338
+ ...makeRefOpsForDocument(
7339
+ normalizedPath,
7340
+ collection?.name,
7341
+ collectionReferences,
7342
+ aliasedData,
7343
+ "put",
7344
+ level
7345
+ ),
7110
7346
  ...makeIndexOpsForDocument(
7111
7347
  normalizedPath,
7112
7348
  collection?.name,
@@ -7170,6 +7406,7 @@ var _deleteIndexContent = async (database, documentPaths, enqueueOps, collection
7170
7406
  throw new Error(`No indexDefinitions for collection ${collection.name}`);
7171
7407
  }
7172
7408
  }
7409
+ const collectionReferences = (await database.getCollectionReferences())?.[collection?.name];
7173
7410
  const tinaSchema = await database.getSchema();
7174
7411
  let templateInfo = null;
7175
7412
  if (collection) {
@@ -7193,6 +7430,14 @@ var _deleteIndexContent = async (database, documentPaths, enqueueOps, collection
7193
7430
  item
7194
7431
  ) : item;
7195
7432
  await enqueueOps([
7433
+ ...makeRefOpsForDocument(
7434
+ itemKey,
7435
+ collection?.name,
7436
+ collectionReferences,
7437
+ aliasedData,
7438
+ "del",
7439
+ database.contentLevel
7440
+ ),
7196
7441
  ...makeIndexOpsForDocument(
7197
7442
  itemKey,
7198
7443
  collection.name,