@tinacms/graphql 0.0.0-b832ee4-20250102012112 → 0.0.0-bdc07c1-20250506013835

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
@@ -27,8 +27,8 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
27
27
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
28
28
 
29
29
  // src/index.ts
30
- var src_exports = {};
31
- __export(src_exports, {
30
+ var index_exports = {};
31
+ __export(index_exports, {
32
32
  AuditFileSystemBridge: () => AuditFileSystemBridge,
33
33
  Database: () => Database,
34
34
  FilesystemBridge: () => FilesystemBridge,
@@ -62,7 +62,7 @@ __export(src_exports, {
62
62
  transformDocument: () => transformDocument,
63
63
  transformDocumentIntoPayload: () => transformDocumentIntoPayload
64
64
  });
65
- module.exports = __toCommonJS(src_exports);
65
+ module.exports = __toCommonJS(index_exports);
66
66
 
67
67
  // src/build.ts
68
68
  var import_graphql2 = require("graphql");
@@ -1095,41 +1095,6 @@ function* walk(maybeNode, visited = /* @__PURE__ */ new WeakSet()) {
1095
1095
  yield maybeNode;
1096
1096
  visited.add(maybeNode);
1097
1097
  }
1098
- function addNamespaceToSchema(maybeNode, namespace = []) {
1099
- if (typeof maybeNode === "string") {
1100
- return maybeNode;
1101
- }
1102
- if (typeof maybeNode === "boolean") {
1103
- return maybeNode;
1104
- }
1105
- const newNode = maybeNode;
1106
- const keys = Object.keys(maybeNode);
1107
- Object.values(maybeNode).map((m, index) => {
1108
- const key = keys[index];
1109
- if (Array.isArray(m)) {
1110
- newNode[key] = m.map((element) => {
1111
- if (!element) {
1112
- return;
1113
- }
1114
- if (!element.hasOwnProperty("name")) {
1115
- return element;
1116
- }
1117
- const value = element.name || element.value;
1118
- return addNamespaceToSchema(element, [...namespace, value]);
1119
- });
1120
- } else {
1121
- if (!m) {
1122
- return;
1123
- }
1124
- if (!m.hasOwnProperty("name")) {
1125
- newNode[key] = m;
1126
- } else {
1127
- newNode[key] = addNamespaceToSchema(m, [...namespace, m.name]);
1128
- }
1129
- }
1130
- });
1131
- return { ...newNode, namespace };
1132
- }
1133
1098
  var generateNamespacedFieldName = (names, suffix = "") => {
1134
1099
  return (suffix ? [...names, suffix] : names).map(capitalize).join("");
1135
1100
  };
@@ -1949,7 +1914,7 @@ var Builder = class {
1949
1914
  * ```
1950
1915
  *
1951
1916
  * @public
1952
- * @param collection a Tina Cloud collection
1917
+ * @param collection a TinaCloud collection
1953
1918
  */
1954
1919
  this.collectionFragment = async (collection) => {
1955
1920
  const name = NAMER.dataTypeName(collection.namespace);
@@ -2965,12 +2930,13 @@ var filterSelections = (arr) => {
2965
2930
  };
2966
2931
 
2967
2932
  // src/schema/createSchema.ts
2968
- var import_schema_tools2 = require("@tinacms/schema-tools");
2933
+ var import_schema_tools3 = require("@tinacms/schema-tools");
2969
2934
 
2970
2935
  // src/schema/validate.ts
2936
+ var import_schema_tools = require("@tinacms/schema-tools");
2971
2937
  var import_lodash2 = __toESM(require("lodash.clonedeep"));
2972
2938
  var yup2 = __toESM(require("yup"));
2973
- var import_schema_tools = require("@tinacms/schema-tools");
2939
+ var import_schema_tools2 = require("@tinacms/schema-tools");
2974
2940
  var FIELD_TYPES = [
2975
2941
  "string",
2976
2942
  "number",
@@ -2983,7 +2949,7 @@ var FIELD_TYPES = [
2983
2949
  "password"
2984
2950
  ];
2985
2951
  var validateSchema = async (schema) => {
2986
- const schema2 = addNamespaceToSchema(
2952
+ const schema2 = (0, import_schema_tools.addNamespaceToSchema)(
2987
2953
  (0, import_lodash2.default)(schema)
2988
2954
  );
2989
2955
  const collections = await sequential(
@@ -2992,7 +2958,7 @@ var validateSchema = async (schema) => {
2992
2958
  );
2993
2959
  validationCollectionsPathAndMatch(collections);
2994
2960
  if (schema2.config) {
2995
- const config = (0, import_schema_tools.validateTinaCloudSchemaConfig)(schema2.config);
2961
+ const config = (0, import_schema_tools2.validateTinaCloudSchemaConfig)(schema2.config);
2996
2962
  return {
2997
2963
  collections,
2998
2964
  config
@@ -3124,7 +3090,7 @@ var validateField = async (field) => {
3124
3090
  // package.json
3125
3091
  var package_default = {
3126
3092
  name: "@tinacms/graphql",
3127
- version: "1.5.9",
3093
+ version: "1.5.16",
3128
3094
  main: "dist/index.js",
3129
3095
  module: "dist/index.mjs",
3130
3096
  typings: "dist/index.d.ts",
@@ -3151,8 +3117,8 @@ var package_default = {
3151
3117
  build: "tinacms-scripts build",
3152
3118
  docs: "pnpm typedoc",
3153
3119
  serve: "pnpm nodemon dist/server.js",
3154
- test: "jest",
3155
- "test-watch": "jest --watch"
3120
+ test: "vitest run",
3121
+ "test-watch": "vitest"
3156
3122
  },
3157
3123
  dependencies: {
3158
3124
  "@iarna/toml": "^2.2.5",
@@ -3160,12 +3126,12 @@ var package_default = {
3160
3126
  "@tinacms/schema-tools": "workspace:*",
3161
3127
  "abstract-level": "^1.0.4",
3162
3128
  "date-fns": "^2.30.0",
3163
- "fast-glob": "^3.3.2",
3164
- "fs-extra": "^11.2.0",
3129
+ "fast-glob": "^3.3.3",
3130
+ "fs-extra": "^11.3.0",
3165
3131
  "glob-parent": "^6.0.2",
3166
3132
  graphql: "15.8.0",
3167
3133
  "gray-matter": "^4.0.3",
3168
- "isomorphic-git": "^1.27.1",
3134
+ "isomorphic-git": "^1.29.0",
3169
3135
  "js-sha1": "^0.6.0",
3170
3136
  "js-yaml": "^3.14.1",
3171
3137
  "jsonpath-plus": "10.1.0",
@@ -3175,10 +3141,13 @@ var package_default = {
3175
3141
  "many-level": "^2.0.0",
3176
3142
  micromatch: "4.0.8",
3177
3143
  "normalize-path": "^3.0.0",
3178
- "readable-stream": "^4.5.2",
3144
+ "readable-stream": "^4.7.0",
3179
3145
  scmp: "^2.1.0",
3180
3146
  yup: "^0.32.11"
3181
3147
  },
3148
+ peerDependencies: {
3149
+ "@tinacms/common": "workspace:*"
3150
+ },
3182
3151
  publishConfig: {
3183
3152
  registry: "https://registry.npmjs.org"
3184
3153
  },
@@ -3193,24 +3162,23 @@ var package_default = {
3193
3162
  "@types/estree": "^0.0.50",
3194
3163
  "@types/express": "^4.17.21",
3195
3164
  "@types/fs-extra": "^9.0.13",
3196
- "@types/jest": "^26.0.24",
3197
3165
  "@types/js-yaml": "^3.12.10",
3198
3166
  "@types/lodash.camelcase": "^4.3.9",
3199
3167
  "@types/lodash.upperfirst": "^4.3.9",
3200
3168
  "@types/lru-cache": "^5.1.1",
3201
3169
  "@types/mdast": "^3.0.15",
3202
3170
  "@types/micromatch": "^4.0.9",
3203
- "@types/node": "^22.9.0",
3171
+ "@types/node": "^22.13.1",
3204
3172
  "@types/normalize-path": "^3.0.2",
3205
3173
  "@types/ws": "^7.4.7",
3206
3174
  "@types/yup": "^0.29.14",
3207
- jest: "^29.7.0",
3208
- "jest-diff": "^29.7.0",
3209
3175
  "jest-file-snapshot": "^0.5.0",
3210
- "jest-matcher-utils": "^29.7.0",
3211
3176
  "memory-level": "^1.0.0",
3212
3177
  nodemon: "3.1.4",
3213
- typescript: "^5.6.3"
3178
+ typescript: "^5.7.3",
3179
+ vite: "^4.5.9",
3180
+ vitest: "^0.32.4",
3181
+ zod: "^3.24.2"
3214
3182
  }
3215
3183
  };
3216
3184
 
@@ -3225,7 +3193,7 @@ var createSchema = async ({
3225
3193
  if (flags && flags.length > 0) {
3226
3194
  meta["flags"] = flags;
3227
3195
  }
3228
- return new import_schema_tools2.TinaSchema({
3196
+ return new import_schema_tools3.TinaSchema({
3229
3197
  version: {
3230
3198
  fullVersion: package_default.version,
3231
3199
  major,
@@ -3367,7 +3335,9 @@ var _buildSchema = async (builder, tinaSchema) => {
3367
3335
  await builder.buildCreateCollectionFolderMutation()
3368
3336
  );
3369
3337
  await sequential(collections, async (collection) => {
3370
- queryTypeDefinitionFields.push(await builder.collectionDocument(collection));
3338
+ queryTypeDefinitionFields.push(
3339
+ await builder.collectionDocument(collection)
3340
+ );
3371
3341
  if (collection.isAuthCollection) {
3372
3342
  queryTypeDefinitionFields.push(
3373
3343
  await builder.authenticationCollectionDocument(collection)
@@ -3683,7 +3653,9 @@ var LevelProxyHandler = {
3683
3653
  throw new Error(`The property, ${property.toString()}, doesn't exist`);
3684
3654
  }
3685
3655
  if (typeof target[property] !== "function") {
3686
- throw new Error(`The property, ${property.toString()}, is not a function`);
3656
+ throw new Error(
3657
+ `The property, ${property.toString()}, is not a function`
3658
+ );
3687
3659
  }
3688
3660
  if (property === "get") {
3689
3661
  return async (...args) => {
@@ -3723,7 +3695,7 @@ var import_path2 = __toESM(require("path"));
3723
3695
  var import_toml = __toESM(require("@iarna/toml"));
3724
3696
  var import_js_yaml = __toESM(require("js-yaml"));
3725
3697
  var import_gray_matter = __toESM(require("gray-matter"));
3726
- var import_schema_tools3 = require("@tinacms/schema-tools");
3698
+ var import_schema_tools4 = require("@tinacms/schema-tools");
3727
3699
  var import_micromatch = __toESM(require("micromatch"));
3728
3700
  var import_path = __toESM(require("path"));
3729
3701
 
@@ -3936,7 +3908,7 @@ var scanAllContent = async (tinaSchema, bridge, callback) => {
3936
3908
  const filesSeen = /* @__PURE__ */ new Map();
3937
3909
  const duplicateFiles = /* @__PURE__ */ new Set();
3938
3910
  await sequential(tinaSchema.getCollections(), async (collection) => {
3939
- const normalPath = (0, import_schema_tools3.normalizePath)(collection.path);
3911
+ const normalPath = (0, import_schema_tools4.normalizePath)(collection.path);
3940
3912
  const format = collection.format || "md";
3941
3913
  const documentPaths = await bridge.glob(normalPath, format);
3942
3914
  const matches = tinaSchema.getMatches({ collection });
@@ -4048,7 +4020,7 @@ var getTemplateForFile = (templateInfo, data) => {
4048
4020
  throw new Error(`Unable to determine template`);
4049
4021
  };
4050
4022
  var loadAndParseWithAliases = async (bridge, filepath, collection, templateInfo) => {
4051
- const dataString = await bridge.get((0, import_schema_tools3.normalizePath)(filepath));
4023
+ const dataString = await bridge.get((0, import_schema_tools4.normalizePath)(filepath));
4052
4024
  const data = parseFile(
4053
4025
  dataString,
4054
4026
  import_path.default.extname(filepath),
@@ -4070,6 +4042,9 @@ var loadAndParseWithAliases = async (bridge, filepath, collection, templateInfo)
4070
4042
 
4071
4043
  // src/database/datalayer.ts
4072
4044
  var DEFAULT_COLLECTION_SORT_KEY = "__filepath__";
4045
+ var REFS_COLLECTIONS_SORT_KEY = "__refs__";
4046
+ var REFS_REFERENCE_FIELD = "__tina_ref__";
4047
+ var REFS_PATH_FIELD = "__tina_ref_path__";
4073
4048
  var DEFAULT_NUMERIC_LPAD = 4;
4074
4049
  var applyPadding = (input, pad) => {
4075
4050
  if (pad) {
@@ -4527,7 +4502,7 @@ var FolderTreeBuilder = class {
4527
4502
  return this._tree;
4528
4503
  }
4529
4504
  update(documentPath, collectionPath) {
4530
- let folderPath = import_path2.default.dirname((0, import_schema_tools3.normalizePath)(documentPath));
4505
+ let folderPath = import_path2.default.dirname((0, import_schema_tools4.normalizePath)(documentPath));
4531
4506
  if (folderPath === ".") {
4532
4507
  folderPath = "";
4533
4508
  }
@@ -4540,7 +4515,7 @@ var FolderTreeBuilder = class {
4540
4515
  if (!this._tree[current2]) {
4541
4516
  this._tree[current2] = /* @__PURE__ */ new Set();
4542
4517
  }
4543
- this._tree[current2].add((0, import_schema_tools3.normalizePath)(import_path2.default.join(current2, part)));
4518
+ this._tree[current2].add((0, import_schema_tools4.normalizePath)(import_path2.default.join(current2, part)));
4544
4519
  parent.push(part);
4545
4520
  });
4546
4521
  const current = parent.join("/");
@@ -4643,6 +4618,57 @@ var makeIndexOpsForDocument = (filepath, collection, indexDefinitions, data, opT
4643
4618
  }
4644
4619
  return result;
4645
4620
  };
4621
+ var makeRefOpsForDocument = (filepath, collection, references, data, opType, level) => {
4622
+ const result = [];
4623
+ if (collection) {
4624
+ for (const [c, referencePaths] of Object.entries(references || {})) {
4625
+ if (!referencePaths.length) {
4626
+ continue;
4627
+ }
4628
+ const collectionSublevel = level.sublevel(c, SUBLEVEL_OPTIONS);
4629
+ const refSublevel = collectionSublevel.sublevel(
4630
+ REFS_COLLECTIONS_SORT_KEY,
4631
+ SUBLEVEL_OPTIONS
4632
+ );
4633
+ const references2 = {};
4634
+ for (const path7 of referencePaths) {
4635
+ const ref = (0, import_jsonpath_plus.JSONPath)({ path: path7, json: data });
4636
+ if (!ref) {
4637
+ continue;
4638
+ }
4639
+ if (Array.isArray(ref)) {
4640
+ for (const r of ref) {
4641
+ if (!r) {
4642
+ continue;
4643
+ }
4644
+ if (references2[r]) {
4645
+ references2[r].push(path7);
4646
+ } else {
4647
+ references2[r] = [path7];
4648
+ }
4649
+ }
4650
+ } else {
4651
+ if (references2[ref]) {
4652
+ references2[ref].push(path7);
4653
+ } else {
4654
+ references2[ref] = [path7];
4655
+ }
4656
+ }
4657
+ }
4658
+ for (const ref of Object.keys(references2)) {
4659
+ for (const path7 of references2[ref]) {
4660
+ result.push({
4661
+ type: opType,
4662
+ key: `${ref}${INDEX_KEY_FIELD_SEPARATOR}${path7}${INDEX_KEY_FIELD_SEPARATOR}${filepath}`,
4663
+ sublevel: refSublevel,
4664
+ value: opType === "put" ? {} : void 0
4665
+ });
4666
+ }
4667
+ }
4668
+ }
4669
+ }
4670
+ return result;
4671
+ };
4646
4672
  var makeStringEscaper = (regex, replacement) => {
4647
4673
  return (input) => {
4648
4674
  if (Array.isArray(input)) {
@@ -4864,24 +4890,33 @@ var transformDocumentIntoPayload = async (fullPath, rawData, tinaSchema, config,
4864
4890
  throw e;
4865
4891
  }
4866
4892
  };
4867
- var updateObjectWithJsonPath = (obj, path7, newValue) => {
4893
+ var updateObjectWithJsonPath = (obj, path7, oldValue, newValue) => {
4894
+ let updated = false;
4868
4895
  if (!path7.includes(".") && !path7.includes("[")) {
4869
- if (path7 in obj) {
4896
+ if (path7 in obj && obj[path7] === oldValue) {
4870
4897
  obj[path7] = newValue;
4898
+ updated = true;
4871
4899
  }
4872
- return obj;
4873
- }
4874
- const parentPath = path7.replace(/\.[^.]+$/, "");
4875
- const keyToUpdate = path7.match(/[^.]+$/)[0];
4876
- const parents = (0, import_jsonpath_plus2.JSONPath)({ path: parentPath, json: obj, resultType: "value" });
4900
+ return { object: obj, updated };
4901
+ }
4902
+ const parentPath = path7.replace(/\.[^.\[\]]+$/, "");
4903
+ const keyToUpdate = path7.match(/[^.\[\]]+$/)[0];
4904
+ const parents = (0, import_jsonpath_plus2.JSONPath)({
4905
+ path: parentPath,
4906
+ json: obj,
4907
+ resultType: "value"
4908
+ });
4877
4909
  if (parents.length > 0) {
4878
4910
  parents.forEach((parent) => {
4879
4911
  if (parent && typeof parent === "object" && keyToUpdate in parent) {
4880
- parent[keyToUpdate] = newValue;
4912
+ if (parent[keyToUpdate] === oldValue) {
4913
+ parent[keyToUpdate] = newValue;
4914
+ updated = true;
4915
+ }
4881
4916
  }
4882
4917
  });
4883
4918
  }
4884
- return obj;
4919
+ return { object: obj, updated };
4885
4920
  };
4886
4921
  var Resolver = class {
4887
4922
  constructor(init) {
@@ -4898,7 +4933,9 @@ var Resolver = class {
4898
4933
  };
4899
4934
  this.getRaw = async (fullPath) => {
4900
4935
  if (typeof fullPath !== "string") {
4901
- throw new Error(`fullPath must be of type string for getDocument request`);
4936
+ throw new Error(
4937
+ `fullPath must be of type string for getDocument request`
4938
+ );
4902
4939
  }
4903
4940
  return this.database.get(fullPath);
4904
4941
  };
@@ -4927,7 +4964,9 @@ var Resolver = class {
4927
4964
  };
4928
4965
  this.getDocument = async (fullPath, opts = {}) => {
4929
4966
  if (typeof fullPath !== "string") {
4930
- throw new Error(`fullPath must be of type string for getDocument request`);
4967
+ throw new Error(
4968
+ `fullPath must be of type string for getDocument request`
4969
+ );
4931
4970
  }
4932
4971
  const rawData = await this.getRaw(fullPath);
4933
4972
  const hasReferences = (opts == null ? void 0 : opts.checkReferences) ? await this.hasReferences(fullPath, opts.collection) : void 0;
@@ -4942,7 +4981,9 @@ var Resolver = class {
4942
4981
  };
4943
4982
  this.deleteDocument = async (fullPath) => {
4944
4983
  if (typeof fullPath !== "string") {
4945
- throw new Error(`fullPath must be of type string for getDocument request`);
4984
+ throw new Error(
4985
+ `fullPath must be of type string for getDocument request`
4986
+ );
4946
4987
  }
4947
4988
  await this.database.delete(fullPath);
4948
4989
  };
@@ -5146,7 +5187,11 @@ var Resolver = class {
5146
5187
  collection,
5147
5188
  doc == null ? void 0 : doc._rawData
5148
5189
  );
5149
- await this.database.put(realPath, { ...oldDoc, ...params }, collection.name);
5190
+ await this.database.put(
5191
+ realPath,
5192
+ { ...oldDoc, ...params },
5193
+ collection.name
5194
+ );
5150
5195
  return this.getDocument(realPath);
5151
5196
  };
5152
5197
  /**
@@ -5260,17 +5305,35 @@ var Resolver = class {
5260
5305
  await this.deleteDocument(realPath);
5261
5306
  if (await this.hasReferences(realPath, collection)) {
5262
5307
  const collRefs = await this.findReferences(realPath, collection);
5263
- for (const [collection2, refFields] of Object.entries(collRefs)) {
5264
- for (const [refPath, refs] of Object.entries(refFields)) {
5265
- let refDoc = await this.getRaw(refPath);
5266
- for (const ref of refs) {
5267
- refDoc = updateObjectWithJsonPath(
5308
+ for (const [collection2, docsWithRefs] of Object.entries(collRefs)) {
5309
+ for (const [pathToDocWithRef, referencePaths] of Object.entries(
5310
+ docsWithRefs
5311
+ )) {
5312
+ let refDoc = await this.getRaw(pathToDocWithRef);
5313
+ let hasUpdate = false;
5314
+ for (const path7 of referencePaths) {
5315
+ const { object: object2, updated } = updateObjectWithJsonPath(
5268
5316
  refDoc,
5269
- ref.path.join("."),
5317
+ path7,
5318
+ realPath,
5270
5319
  null
5271
5320
  );
5321
+ refDoc = object2;
5322
+ hasUpdate = updated || hasUpdate;
5323
+ }
5324
+ if (hasUpdate) {
5325
+ const collectionWithRef = this.tinaSchema.getCollectionByFullPath(pathToDocWithRef);
5326
+ if (!collectionWithRef) {
5327
+ throw new Error(
5328
+ `Unable to find collection for ${pathToDocWithRef}`
5329
+ );
5330
+ }
5331
+ await this.database.put(
5332
+ pathToDocWithRef,
5333
+ refDoc,
5334
+ collectionWithRef.name
5335
+ );
5272
5336
  }
5273
- await this.database.put(refPath, refDoc, collection2);
5274
5337
  }
5275
5338
  }
5276
5339
  }
@@ -5290,26 +5353,49 @@ var Resolver = class {
5290
5353
  collection == null ? void 0 : collection.path,
5291
5354
  args.params.relativePath
5292
5355
  );
5356
+ if (newRealPath === realPath) {
5357
+ return doc;
5358
+ }
5293
5359
  await this.database.put(newRealPath, doc._rawData, collection.name);
5294
5360
  await this.deleteDocument(realPath);
5295
5361
  const collRefs = await this.findReferences(realPath, collection);
5296
- for (const [collection2, refFields] of Object.entries(collRefs)) {
5297
- for (const [refPath, refs] of Object.entries(refFields)) {
5298
- let refDoc = await this.getRaw(refPath);
5299
- for (const ref of refs) {
5300
- refDoc = updateObjectWithJsonPath(
5301
- refDoc,
5302
- ref.path.join("."),
5362
+ for (const [collection2, docsWithRefs] of Object.entries(collRefs)) {
5363
+ for (const [pathToDocWithRef, referencePaths] of Object.entries(
5364
+ docsWithRefs
5365
+ )) {
5366
+ let docWithRef = await this.getRaw(pathToDocWithRef);
5367
+ let hasUpdate = false;
5368
+ for (const path7 of referencePaths) {
5369
+ const { object: object2, updated } = updateObjectWithJsonPath(
5370
+ docWithRef,
5371
+ path7,
5372
+ realPath,
5303
5373
  newRealPath
5304
5374
  );
5375
+ docWithRef = object2;
5376
+ hasUpdate = updated || hasUpdate;
5377
+ }
5378
+ if (hasUpdate) {
5379
+ const collectionWithRef = this.tinaSchema.getCollectionByFullPath(pathToDocWithRef);
5380
+ if (!collectionWithRef) {
5381
+ throw new Error(
5382
+ `Unable to find collection for ${pathToDocWithRef}`
5383
+ );
5384
+ }
5385
+ await this.database.put(
5386
+ pathToDocWithRef,
5387
+ docWithRef,
5388
+ collectionWithRef.name
5389
+ );
5305
5390
  }
5306
- await this.database.put(refPath, refDoc, collection2);
5307
5391
  }
5308
5392
  }
5309
5393
  return this.getDocument(newRealPath);
5310
5394
  }
5311
5395
  if (alreadyExists === false) {
5312
- throw new Error(`Unable to update document, ${realPath} does not exist`);
5396
+ throw new Error(
5397
+ `Unable to update document, ${realPath} does not exist`
5398
+ );
5313
5399
  }
5314
5400
  return this.updateResolveDocument({
5315
5401
  collection,
@@ -5435,35 +5521,30 @@ var Resolver = class {
5435
5521
  */
5436
5522
  this.hasReferences = async (id, c) => {
5437
5523
  let count = 0;
5438
- const deepRefs = this.tinaSchema.findReferences(c.name);
5439
- for (const [collection, refs] of Object.entries(deepRefs)) {
5440
- for (const ref of refs) {
5441
- await this.database.query(
5442
- {
5443
- collection,
5444
- filterChain: makeFilterChain({
5445
- conditions: [
5446
- {
5447
- filterPath: ref.path.join("."),
5448
- filterExpression: {
5449
- _type: "reference",
5450
- _list: false,
5451
- eq: id
5452
- }
5453
- }
5454
- ]
5455
- }),
5456
- sort: ref.field.name
5457
- },
5458
- (refId) => {
5459
- count++;
5460
- return refId;
5461
- }
5462
- );
5463
- if (count) {
5464
- return true;
5465
- }
5524
+ await this.database.query(
5525
+ {
5526
+ collection: c.name,
5527
+ filterChain: makeFilterChain({
5528
+ conditions: [
5529
+ {
5530
+ filterPath: REFS_REFERENCE_FIELD,
5531
+ filterExpression: {
5532
+ _type: "string",
5533
+ _list: false,
5534
+ eq: id
5535
+ }
5536
+ }
5537
+ ]
5538
+ }),
5539
+ sort: REFS_COLLECTIONS_SORT_KEY
5540
+ },
5541
+ (refId) => {
5542
+ count++;
5543
+ return refId;
5466
5544
  }
5545
+ );
5546
+ if (count) {
5547
+ return true;
5467
5548
  }
5468
5549
  return false;
5469
5550
  };
@@ -5471,46 +5552,41 @@ var Resolver = class {
5471
5552
  * Finds references to a document
5472
5553
  * @param id the id of the document to find references to
5473
5554
  * @param c the collection to find references in
5474
- * @returns references to the document in the form of a map of collection names to a list of fields that reference the document
5555
+ * @returns a map of references to the document
5475
5556
  */
5476
5557
  this.findReferences = async (id, c) => {
5477
5558
  const references = {};
5478
- const deepRefs = this.tinaSchema.findReferences(c.name);
5479
- for (const [collection, refs] of Object.entries(deepRefs)) {
5480
- for (const ref of refs) {
5481
- await this.database.query(
5482
- {
5483
- collection,
5484
- filterChain: makeFilterChain({
5485
- conditions: [
5486
- {
5487
- filterPath: ref.path.join("."),
5488
- filterExpression: {
5489
- _type: "reference",
5490
- _list: false,
5491
- eq: id
5492
- }
5493
- }
5494
- ]
5495
- }),
5496
- sort: ref.field.name
5497
- },
5498
- (refId) => {
5499
- if (!references[collection]) {
5500
- references[collection] = {};
5501
- }
5502
- if (!references[collection][refId]) {
5503
- references[collection][refId] = [];
5559
+ await this.database.query(
5560
+ {
5561
+ collection: c.name,
5562
+ filterChain: makeFilterChain({
5563
+ conditions: [
5564
+ {
5565
+ filterPath: REFS_REFERENCE_FIELD,
5566
+ filterExpression: {
5567
+ _type: "string",
5568
+ _list: false,
5569
+ eq: id
5570
+ }
5504
5571
  }
5505
- references[collection][refId].push({
5506
- path: ref.path,
5507
- field: ref.field
5508
- });
5509
- return refId;
5510
- }
5511
- );
5572
+ ]
5573
+ }),
5574
+ sort: REFS_COLLECTIONS_SORT_KEY
5575
+ },
5576
+ (refId, rawItem) => {
5577
+ if (!references[c.name]) {
5578
+ references[c.name] = {};
5579
+ }
5580
+ if (!references[c.name][refId]) {
5581
+ references[c.name][refId] = [];
5582
+ }
5583
+ const referencePath = rawItem == null ? void 0 : rawItem[REFS_PATH_FIELD];
5584
+ if (referencePath) {
5585
+ references[c.name][refId].push(referencePath);
5586
+ }
5587
+ return refId;
5512
5588
  }
5513
- }
5589
+ );
5514
5590
  return references;
5515
5591
  };
5516
5592
  this.buildFieldMutations = async (fieldParams, template, existingData) => {
@@ -6234,7 +6310,7 @@ var Database = class {
6234
6310
  const contentObject = await level.sublevel(
6235
6311
  CONTENT_ROOT_PREFIX,
6236
6312
  SUBLEVEL_OPTIONS
6237
- ).get((0, import_schema_tools3.normalizePath)(filepath));
6313
+ ).get((0, import_schema_tools4.normalizePath)(filepath));
6238
6314
  if (!contentObject) {
6239
6315
  throw new NotFoundError(`Unable to find record ${filepath}`);
6240
6316
  }
@@ -6246,6 +6322,7 @@ var Database = class {
6246
6322
  }
6247
6323
  };
6248
6324
  this.addPendingDocument = async (filepath, data) => {
6325
+ var _a;
6249
6326
  await this.initLevel();
6250
6327
  const dataFields = await this.formatBodyOnPayload(filepath, data);
6251
6328
  const collection = await this.collectionForPath(filepath);
@@ -6259,7 +6336,8 @@ var Database = class {
6259
6336
  );
6260
6337
  const indexDefinitions = await this.getIndexDefinitions(this.contentLevel);
6261
6338
  const collectionIndexDefinitions = indexDefinitions == null ? void 0 : indexDefinitions[collection.name];
6262
- const normalizedPath = (0, import_schema_tools3.normalizePath)(filepath);
6339
+ const collectionReferences = (_a = await this.getCollectionReferences()) == null ? void 0 : _a[collection.name];
6340
+ const normalizedPath = (0, import_schema_tools4.normalizePath)(filepath);
6263
6341
  if (!(collection == null ? void 0 : collection.isDetached)) {
6264
6342
  if (this.bridge) {
6265
6343
  await this.bridge.put(normalizedPath, stringifiedFile);
@@ -6287,6 +6365,14 @@ var Database = class {
6287
6365
  let delOps = [];
6288
6366
  if (!isGitKeep(normalizedPath, collection)) {
6289
6367
  putOps = [
6368
+ ...makeRefOpsForDocument(
6369
+ normalizedPath,
6370
+ collection == null ? void 0 : collection.name,
6371
+ collectionReferences,
6372
+ dataFields,
6373
+ "put",
6374
+ level
6375
+ ),
6290
6376
  ...makeIndexOpsForDocument(
6291
6377
  normalizedPath,
6292
6378
  collection == null ? void 0 : collection.name,
@@ -6310,6 +6396,14 @@ var Database = class {
6310
6396
  SUBLEVEL_OPTIONS
6311
6397
  ).get(normalizedPath);
6312
6398
  delOps = existingItem ? [
6399
+ ...makeRefOpsForDocument(
6400
+ normalizedPath,
6401
+ collection == null ? void 0 : collection.name,
6402
+ collectionReferences,
6403
+ existingItem,
6404
+ "del",
6405
+ level
6406
+ ),
6313
6407
  ...makeIndexOpsForDocument(
6314
6408
  normalizedPath,
6315
6409
  collection == null ? void 0 : collection.name,
@@ -6345,7 +6439,7 @@ var Database = class {
6345
6439
  await level.batch(ops);
6346
6440
  };
6347
6441
  this.put = async (filepath, data, collectionName) => {
6348
- var _a, _b;
6442
+ var _a, _b, _c;
6349
6443
  await this.initLevel();
6350
6444
  try {
6351
6445
  if (SYSTEM_FILES.includes(filepath)) {
@@ -6358,13 +6452,14 @@ var Database = class {
6358
6452
  );
6359
6453
  collectionIndexDefinitions = indexDefinitions == null ? void 0 : indexDefinitions[collectionName];
6360
6454
  }
6361
- const normalizedPath = (0, import_schema_tools3.normalizePath)(filepath);
6455
+ const collectionReferences = (_a = await this.getCollectionReferences()) == null ? void 0 : _a[collectionName];
6456
+ const normalizedPath = (0, import_schema_tools4.normalizePath)(filepath);
6362
6457
  const dataFields = await this.formatBodyOnPayload(filepath, data);
6363
6458
  const collection = await this.collectionForPath(filepath);
6364
6459
  if (!collection) {
6365
6460
  throw new import_graphql6.GraphQLError(`Unable to find collection for ${filepath}.`);
6366
6461
  }
6367
- if (((_a = collection.match) == null ? void 0 : _a.exclude) || ((_b = collection.match) == null ? void 0 : _b.include)) {
6462
+ if (((_b = collection.match) == null ? void 0 : _b.exclude) || ((_c = collection.match) == null ? void 0 : _c.include)) {
6368
6463
  const matches = this.tinaSchema.getMatches({ collection });
6369
6464
  const match = import_micromatch2.default.isMatch(filepath, matches);
6370
6465
  if (!match) {
@@ -6405,6 +6500,14 @@ var Database = class {
6405
6500
  let delOps = [];
6406
6501
  if (!isGitKeep(normalizedPath, collection)) {
6407
6502
  putOps = [
6503
+ ...makeRefOpsForDocument(
6504
+ normalizedPath,
6505
+ collectionName,
6506
+ collectionReferences,
6507
+ dataFields,
6508
+ "put",
6509
+ level
6510
+ ),
6408
6511
  ...makeIndexOpsForDocument(
6409
6512
  normalizedPath,
6410
6513
  collectionName,
@@ -6428,6 +6531,14 @@ var Database = class {
6428
6531
  SUBLEVEL_OPTIONS
6429
6532
  ).get(normalizedPath);
6430
6533
  delOps = existingItem ? [
6534
+ ...makeRefOpsForDocument(
6535
+ normalizedPath,
6536
+ collectionName,
6537
+ collectionReferences,
6538
+ existingItem,
6539
+ "del",
6540
+ level
6541
+ ),
6431
6542
  ...makeIndexOpsForDocument(
6432
6543
  normalizedPath,
6433
6544
  collectionName,
@@ -6532,7 +6643,7 @@ var Database = class {
6532
6643
  };
6533
6644
  this.getLookup = async (returnType) => {
6534
6645
  await this.initLevel();
6535
- const lookupPath = (0, import_schema_tools3.normalizePath)(
6646
+ const lookupPath = (0, import_schema_tools4.normalizePath)(
6536
6647
  import_node_path.default.join(this.getGeneratedFolder(), `_lookup.json`)
6537
6648
  );
6538
6649
  if (!this._lookup) {
@@ -6545,7 +6656,7 @@ var Database = class {
6545
6656
  };
6546
6657
  this.getGraphQLSchema = async () => {
6547
6658
  await this.initLevel();
6548
- const graphqlPath = (0, import_schema_tools3.normalizePath)(
6659
+ const graphqlPath = (0, import_schema_tools4.normalizePath)(
6549
6660
  import_node_path.default.join(this.getGeneratedFolder(), `_graphql.json`)
6550
6661
  );
6551
6662
  return await this.contentLevel.sublevel(
@@ -6558,7 +6669,7 @@ var Database = class {
6558
6669
  if (!this.bridge) {
6559
6670
  throw new Error(`No bridge configured`);
6560
6671
  }
6561
- const graphqlPath = (0, import_schema_tools3.normalizePath)(
6672
+ const graphqlPath = (0, import_schema_tools4.normalizePath)(
6562
6673
  import_node_path.default.join(this.getGeneratedFolder(), `_graphql.json`)
6563
6674
  );
6564
6675
  const _graphql = await this.bridge.get(graphqlPath);
@@ -6566,7 +6677,7 @@ var Database = class {
6566
6677
  };
6567
6678
  this.getTinaSchema = async (level) => {
6568
6679
  await this.initLevel();
6569
- const schemaPath = (0, import_schema_tools3.normalizePath)(
6680
+ const schemaPath = (0, import_schema_tools4.normalizePath)(
6570
6681
  import_node_path.default.join(this.getGeneratedFolder(), `_schema.json`)
6571
6682
  );
6572
6683
  return await (level || this.contentLevel).sublevel(
@@ -6582,7 +6693,7 @@ var Database = class {
6582
6693
  const schema = existingSchema || await this.getTinaSchema(level || this.contentLevel);
6583
6694
  if (!schema) {
6584
6695
  throw new Error(
6585
- `Unable to get schema from level db: ${(0, import_schema_tools3.normalizePath)(
6696
+ `Unable to get schema from level db: ${(0, import_schema_tools4.normalizePath)(
6586
6697
  import_node_path.default.join(this.getGeneratedFolder(), `_schema.json`)
6587
6698
  )}`
6588
6699
  );
@@ -6590,6 +6701,22 @@ var Database = class {
6590
6701
  this.tinaSchema = await createSchema({ schema });
6591
6702
  return this.tinaSchema;
6592
6703
  };
6704
+ this.getCollectionReferences = async (level) => {
6705
+ if (this.collectionReferences) {
6706
+ return this.collectionReferences;
6707
+ }
6708
+ const result = {};
6709
+ const schema = await this.getSchema(level || this.contentLevel);
6710
+ const collections = schema.getCollections();
6711
+ for (const collection of collections) {
6712
+ const collectionReferences = this.tinaSchema.findReferencesFromCollection(
6713
+ collection.name
6714
+ );
6715
+ result[collection.name] = collectionReferences;
6716
+ }
6717
+ this.collectionReferences = result;
6718
+ return result;
6719
+ };
6593
6720
  this.getIndexDefinitions = async (level) => {
6594
6721
  if (!this.collectionIndexDefinitions) {
6595
6722
  await new Promise(async (resolve2, reject) => {
@@ -6599,11 +6726,53 @@ var Database = class {
6599
6726
  const collections = schema.getCollections();
6600
6727
  for (const collection of collections) {
6601
6728
  const indexDefinitions = {
6602
- [DEFAULT_COLLECTION_SORT_KEY]: { fields: [] }
6729
+ [DEFAULT_COLLECTION_SORT_KEY]: { fields: [] },
6603
6730
  // provide a default sort key which is the file sort
6731
+ // pseudo-index for the collection's references
6732
+ [REFS_COLLECTIONS_SORT_KEY]: {
6733
+ fields: [
6734
+ {
6735
+ name: REFS_REFERENCE_FIELD,
6736
+ type: "string",
6737
+ list: false
6738
+ },
6739
+ {
6740
+ name: REFS_PATH_FIELD,
6741
+ type: "string",
6742
+ list: false
6743
+ }
6744
+ ]
6745
+ }
6604
6746
  };
6605
- if (collection.fields) {
6606
- for (const field of collection.fields) {
6747
+ let fields = [];
6748
+ if (collection.templates) {
6749
+ const templateFieldMap = {};
6750
+ const conflictedFields = /* @__PURE__ */ new Set();
6751
+ for (const template of collection.templates) {
6752
+ for (const field of template.fields) {
6753
+ if (!templateFieldMap[field.name]) {
6754
+ templateFieldMap[field.name] = field;
6755
+ } else {
6756
+ if (templateFieldMap[field.name].type !== field.type) {
6757
+ console.warn(
6758
+ `Field ${field.name} has conflicting types in templates - skipping index`
6759
+ );
6760
+ conflictedFields.add(field.name);
6761
+ }
6762
+ }
6763
+ }
6764
+ }
6765
+ for (const conflictedField in conflictedFields) {
6766
+ delete templateFieldMap[conflictedField];
6767
+ }
6768
+ for (const field of Object.values(templateFieldMap)) {
6769
+ fields.push(field);
6770
+ }
6771
+ } else if (collection.fields) {
6772
+ fields = collection.fields;
6773
+ }
6774
+ if (fields) {
6775
+ for (const field of fields) {
6607
6776
  if (field.indexed !== void 0 && field.indexed === false || field.type === "object") {
6608
6777
  continue;
6609
6778
  }
@@ -6752,29 +6921,36 @@ var Database = class {
6752
6921
  }
6753
6922
  startKey = startKey || key || "";
6754
6923
  endKey = key || "";
6755
- edges = [...edges, { cursor: key, path: filepath }];
6924
+ edges = [...edges, { cursor: key, path: filepath, value: itemRecord }];
6756
6925
  }
6757
6926
  return {
6758
- edges: await sequential(edges, async (edge) => {
6759
- try {
6760
- const node = await hydrator(edge.path);
6761
- return {
6762
- node,
6763
- cursor: btoa(edge.cursor)
6764
- };
6765
- } catch (error) {
6766
- console.log(error);
6767
- if (error instanceof Error && (!edge.path.includes(".tina/__generated__/_graphql.json") || !edge.path.includes("tina/__generated__/_graphql.json"))) {
6768
- throw new TinaQueryError({
6769
- originalError: error,
6770
- file: edge.path,
6771
- collection: collection.name,
6772
- stack: error.stack
6773
- });
6927
+ edges: await sequential(
6928
+ edges,
6929
+ async ({
6930
+ cursor,
6931
+ path: path7,
6932
+ value
6933
+ }) => {
6934
+ try {
6935
+ const node = await hydrator(path7, value);
6936
+ return {
6937
+ node,
6938
+ cursor: btoa(cursor)
6939
+ };
6940
+ } catch (error) {
6941
+ console.log(error);
6942
+ if (error instanceof Error && (!path7.includes(".tina/__generated__/_graphql.json") || !path7.includes("tina/__generated__/_graphql.json"))) {
6943
+ throw new TinaQueryError({
6944
+ originalError: error,
6945
+ file: path7,
6946
+ collection: collection.name,
6947
+ stack: error.stack
6948
+ });
6949
+ }
6950
+ throw error;
6774
6951
  }
6775
- throw error;
6776
6952
  }
6777
- }),
6953
+ ),
6778
6954
  pageInfo: {
6779
6955
  hasPreviousPage,
6780
6956
  hasNextPage,
@@ -6799,7 +6975,7 @@ var Database = class {
6799
6975
  try {
6800
6976
  lookup = lookupFromLockFile || JSON.parse(
6801
6977
  await this.bridge.get(
6802
- (0, import_schema_tools3.normalizePath)(
6978
+ (0, import_schema_tools4.normalizePath)(
6803
6979
  import_node_path.default.join(this.getGeneratedFolder(), "_lookup.json")
6804
6980
  )
6805
6981
  )
@@ -6824,15 +7000,15 @@ var Database = class {
6824
7000
  }
6825
7001
  const contentRootLevel = nextLevel.sublevel(CONTENT_ROOT_PREFIX, SUBLEVEL_OPTIONS);
6826
7002
  await contentRootLevel.put(
6827
- (0, import_schema_tools3.normalizePath)(import_node_path.default.join(this.getGeneratedFolder(), "_graphql.json")),
7003
+ (0, import_schema_tools4.normalizePath)(import_node_path.default.join(this.getGeneratedFolder(), "_graphql.json")),
6828
7004
  graphQLSchema
6829
7005
  );
6830
7006
  await contentRootLevel.put(
6831
- (0, import_schema_tools3.normalizePath)(import_node_path.default.join(this.getGeneratedFolder(), "_schema.json")),
7007
+ (0, import_schema_tools4.normalizePath)(import_node_path.default.join(this.getGeneratedFolder(), "_schema.json")),
6832
7008
  tinaSchema.schema
6833
7009
  );
6834
7010
  await contentRootLevel.put(
6835
- (0, import_schema_tools3.normalizePath)(import_node_path.default.join(this.getGeneratedFolder(), "_lookup.json")),
7011
+ (0, import_schema_tools4.normalizePath)(import_node_path.default.join(this.getGeneratedFolder(), "_lookup.json")),
6836
7012
  lookup
6837
7013
  );
6838
7014
  const result = await this._indexAllContent(
@@ -6898,13 +7074,14 @@ var Database = class {
6898
7074
  documentPaths,
6899
7075
  async (collection, documentPaths2) => {
6900
7076
  if (collection && !collection.isDetached) {
6901
- await _indexContent(
6902
- this,
6903
- this.contentLevel,
6904
- documentPaths2,
7077
+ await _indexContent({
7078
+ database: this,
7079
+ level: this.contentLevel,
7080
+ documentPaths: documentPaths2,
6905
7081
  enqueueOps,
6906
- collection
6907
- );
7082
+ collection,
7083
+ isPartialReindex: true
7084
+ });
6908
7085
  }
6909
7086
  }
6910
7087
  );
@@ -6914,18 +7091,20 @@ var Database = class {
6914
7091
  }
6915
7092
  };
6916
7093
  this.delete = async (filepath) => {
7094
+ var _a;
6917
7095
  await this.initLevel();
6918
7096
  const collection = await this.collectionForPath(filepath);
6919
7097
  if (!collection) {
6920
7098
  throw new Error(`No collection found for path: ${filepath}`);
6921
7099
  }
6922
7100
  const indexDefinitions = await this.getIndexDefinitions(this.contentLevel);
7101
+ const collectionReferences = (_a = await this.getCollectionReferences()) == null ? void 0 : _a[collection.name];
6923
7102
  const collectionIndexDefinitions = indexDefinitions == null ? void 0 : indexDefinitions[collection.name];
6924
7103
  let level = this.contentLevel;
6925
7104
  if (collection == null ? void 0 : collection.isDetached) {
6926
7105
  level = this.appLevel.sublevel(collection == null ? void 0 : collection.name, SUBLEVEL_OPTIONS);
6927
7106
  }
6928
- const normalizedPath = (0, import_schema_tools3.normalizePath)(filepath);
7107
+ const normalizedPath = (0, import_schema_tools4.normalizePath)(filepath);
6929
7108
  const rootSublevel = level.sublevel(
6930
7109
  CONTENT_ROOT_PREFIX,
6931
7110
  SUBLEVEL_OPTIONS
@@ -6938,6 +7117,14 @@ var Database = class {
6938
7117
  collection.path || ""
6939
7118
  );
6940
7119
  await this.contentLevel.batch([
7120
+ ...makeRefOpsForDocument(
7121
+ normalizedPath,
7122
+ collection.name,
7123
+ collectionReferences,
7124
+ item,
7125
+ "del",
7126
+ level
7127
+ ),
6941
7128
  ...makeIndexOpsForDocument(
6942
7129
  normalizedPath,
6943
7130
  collection.name,
@@ -7002,20 +7189,26 @@ var Database = class {
7002
7189
  );
7003
7190
  const doc = await level2.keys({ limit: 1 }).next();
7004
7191
  if (!doc) {
7005
- await _indexContent(
7006
- this,
7007
- level2,
7008
- contentPaths,
7192
+ await _indexContent({
7193
+ database: this,
7194
+ level: level2,
7195
+ documentPaths: contentPaths,
7009
7196
  enqueueOps,
7010
7197
  collection,
7011
- userFields.map((field) => [
7198
+ passwordFields: userFields.map((field) => [
7012
7199
  ...field.path,
7013
7200
  field.passwordFieldName
7014
7201
  ])
7015
- );
7202
+ });
7016
7203
  }
7017
7204
  } else {
7018
- await _indexContent(this, level, contentPaths, enqueueOps, collection);
7205
+ await _indexContent({
7206
+ database: this,
7207
+ level,
7208
+ documentPaths: contentPaths,
7209
+ enqueueOps,
7210
+ collection
7211
+ });
7019
7212
  }
7020
7213
  }
7021
7214
  );
@@ -7154,7 +7347,16 @@ var hashPasswordValues = async (data, passwordFields) => Promise.all(
7154
7347
  )
7155
7348
  );
7156
7349
  var isGitKeep = (filepath, collection) => filepath.endsWith(`.gitkeep.${(collection == null ? void 0 : collection.format) || "md"}`);
7157
- var _indexContent = async (database, level, documentPaths, enqueueOps, collection, passwordFields) => {
7350
+ var _indexContent = async ({
7351
+ database,
7352
+ level,
7353
+ documentPaths,
7354
+ enqueueOps,
7355
+ collection,
7356
+ passwordFields,
7357
+ isPartialReindex
7358
+ }) => {
7359
+ var _a;
7158
7360
  let collectionIndexDefinitions;
7159
7361
  let collectionPath;
7160
7362
  if (collection) {
@@ -7165,6 +7367,7 @@ var _indexContent = async (database, level, documentPaths, enqueueOps, collectio
7165
7367
  }
7166
7368
  collectionPath = collection.path;
7167
7369
  }
7370
+ const collectionReferences = (_a = await database.getCollectionReferences()) == null ? void 0 : _a[collection == null ? void 0 : collection.name];
7168
7371
  const tinaSchema = await database.getSchema();
7169
7372
  let templateInfo = null;
7170
7373
  if (collection) {
@@ -7185,13 +7388,62 @@ var _indexContent = async (database, level, documentPaths, enqueueOps, collectio
7185
7388
  if (passwordFields == null ? void 0 : passwordFields.length) {
7186
7389
  await hashPasswordValues(aliasedData, passwordFields);
7187
7390
  }
7188
- const normalizedPath = (0, import_schema_tools3.normalizePath)(filepath);
7391
+ const normalizedPath = (0, import_schema_tools4.normalizePath)(filepath);
7392
+ const rootSublevel = level.sublevel(
7393
+ CONTENT_ROOT_PREFIX,
7394
+ SUBLEVEL_OPTIONS
7395
+ );
7189
7396
  const folderKey = folderTreeBuilder.update(
7190
7397
  normalizedPath,
7191
7398
  collectionPath || ""
7192
7399
  );
7400
+ if (isPartialReindex) {
7401
+ const item = await rootSublevel.get(normalizedPath);
7402
+ if (item) {
7403
+ await database.contentLevel.batch([
7404
+ ...makeRefOpsForDocument(
7405
+ normalizedPath,
7406
+ collection == null ? void 0 : collection.name,
7407
+ collectionReferences,
7408
+ item,
7409
+ "del",
7410
+ level
7411
+ ),
7412
+ ...makeIndexOpsForDocument(
7413
+ normalizedPath,
7414
+ collection.name,
7415
+ collectionIndexDefinitions,
7416
+ item,
7417
+ "del",
7418
+ level
7419
+ ),
7420
+ // folder indices
7421
+ ...makeIndexOpsForDocument(
7422
+ normalizedPath,
7423
+ `${collection.name}_${folderKey}`,
7424
+ collectionIndexDefinitions,
7425
+ item,
7426
+ "del",
7427
+ level
7428
+ ),
7429
+ {
7430
+ type: "del",
7431
+ key: normalizedPath,
7432
+ sublevel: rootSublevel
7433
+ }
7434
+ ]);
7435
+ }
7436
+ }
7193
7437
  if (!isGitKeep(filepath, collection)) {
7194
7438
  await enqueueOps([
7439
+ ...makeRefOpsForDocument(
7440
+ normalizedPath,
7441
+ collection == null ? void 0 : collection.name,
7442
+ collectionReferences,
7443
+ aliasedData,
7444
+ "put",
7445
+ level
7446
+ ),
7195
7447
  ...makeIndexOpsForDocument(
7196
7448
  normalizedPath,
7197
7449
  collection == null ? void 0 : collection.name,
@@ -7242,6 +7494,7 @@ var _indexContent = async (database, level, documentPaths, enqueueOps, collectio
7242
7494
  }
7243
7495
  };
7244
7496
  var _deleteIndexContent = async (database, documentPaths, enqueueOps, collection) => {
7497
+ var _a;
7245
7498
  if (!documentPaths.length) {
7246
7499
  return;
7247
7500
  }
@@ -7255,6 +7508,7 @@ var _deleteIndexContent = async (database, documentPaths, enqueueOps, collection
7255
7508
  throw new Error(`No indexDefinitions for collection ${collection.name}`);
7256
7509
  }
7257
7510
  }
7511
+ const collectionReferences = (_a = await database.getCollectionReferences()) == null ? void 0 : _a[collection == null ? void 0 : collection.name];
7258
7512
  const tinaSchema = await database.getSchema();
7259
7513
  let templateInfo = null;
7260
7514
  if (collection) {
@@ -7266,7 +7520,7 @@ var _deleteIndexContent = async (database, documentPaths, enqueueOps, collection
7266
7520
  );
7267
7521
  const folderTreeBuilder = new FolderTreeBuilder();
7268
7522
  await sequential(documentPaths, async (filepath) => {
7269
- const itemKey = (0, import_schema_tools3.normalizePath)(filepath);
7523
+ const itemKey = (0, import_schema_tools4.normalizePath)(filepath);
7270
7524
  const item = await rootLevel.get(itemKey);
7271
7525
  if (item) {
7272
7526
  const folderKey = folderTreeBuilder.update(
@@ -7278,6 +7532,14 @@ var _deleteIndexContent = async (database, documentPaths, enqueueOps, collection
7278
7532
  item
7279
7533
  ) : item;
7280
7534
  await enqueueOps([
7535
+ ...makeRefOpsForDocument(
7536
+ itemKey,
7537
+ collection == null ? void 0 : collection.name,
7538
+ collectionReferences,
7539
+ aliasedData,
7540
+ "del",
7541
+ database.contentLevel
7542
+ ),
7281
7543
  ...makeIndexOpsForDocument(
7282
7544
  itemKey,
7283
7545
  collection.name,
@@ -7353,14 +7615,14 @@ var getChangedFiles = async ({
7353
7615
  const rootDir = await findGitRoot(dir);
7354
7616
  let pathPrefix = "";
7355
7617
  if (rootDir !== dir) {
7356
- pathPrefix = (0, import_schema_tools3.normalizePath)(dir.substring(rootDir.length + 1));
7618
+ pathPrefix = (0, import_schema_tools4.normalizePath)(dir.substring(rootDir.length + 1));
7357
7619
  }
7358
7620
  await import_isomorphic_git.default.walk({
7359
7621
  fs: fs4,
7360
7622
  dir: rootDir,
7361
7623
  trees: [import_isomorphic_git.default.TREE({ ref: from }), import_isomorphic_git.default.TREE({ ref: to })],
7362
7624
  map: async function(filename, [A, B]) {
7363
- const relativePath = (0, import_schema_tools3.normalizePath)(filename).substring(pathPrefix.length);
7625
+ const relativePath = (0, import_schema_tools4.normalizePath)(filename).substring(pathPrefix.length);
7364
7626
  let matches = false;
7365
7627
  for (const [key, matcher] of Object.entries(pathFilter)) {
7366
7628
  if (relativePath.startsWith(key)) {