@tinacms/graphql 0.0.0-ebe1b69-20250211022853 → 0.0.0-ec43c87-20250804021103

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");
@@ -1435,13 +1435,12 @@ var checkPasswordHash = async ({
1435
1435
  return true;
1436
1436
  };
1437
1437
  var mapUserFields = (collectable, prefix = []) => {
1438
- var _a, _b, _c, _d, _e;
1439
1438
  const results = [];
1440
- const passwordFields = ((_a = collectable.fields) == null ? void 0 : _a.filter((field) => field.type === "password")) || [];
1439
+ const passwordFields = collectable.fields?.filter((field) => field.type === "password") || [];
1441
1440
  if (passwordFields.length > 1) {
1442
1441
  throw new Error("Only one password field is allowed");
1443
1442
  }
1444
- const idFields = ((_b = collectable.fields) == null ? void 0 : _b.filter((field) => field.uid)) || [];
1443
+ const idFields = collectable.fields?.filter((field) => field.uid) || [];
1445
1444
  if (idFields.length > 1) {
1446
1445
  throw new Error("Only one uid field is allowed");
1447
1446
  }
@@ -1449,11 +1448,11 @@ var mapUserFields = (collectable, prefix = []) => {
1449
1448
  results.push({
1450
1449
  path: prefix,
1451
1450
  collectable,
1452
- idFieldName: (_c = idFields[0]) == null ? void 0 : _c.name,
1453
- passwordFieldName: (_d = passwordFields[0]) == null ? void 0 : _d.name
1451
+ idFieldName: idFields[0]?.name,
1452
+ passwordFieldName: passwordFields[0]?.name
1454
1453
  });
1455
1454
  }
1456
- (_e = collectable.fields) == null ? void 0 : _e.forEach((field) => {
1455
+ collectable.fields?.forEach((field) => {
1457
1456
  if (field.type === "object" && field.fields) {
1458
1457
  results.push(...mapUserFields(field, [...prefix, field.name]));
1459
1458
  }
@@ -1914,7 +1913,7 @@ var Builder = class {
1914
1913
  * ```
1915
1914
  *
1916
1915
  * @public
1917
- * @param collection a Tina Cloud collection
1916
+ * @param collection a TinaCloud collection
1918
1917
  */
1919
1918
  this.collectionFragment = async (collection) => {
1920
1919
  const name = NAMER.dataTypeName(collection.namespace);
@@ -1944,13 +1943,12 @@ var Builder = class {
1944
1943
  *
1945
1944
  * */
1946
1945
  this._getCollectionFragmentSelections = async (collection, depth) => {
1947
- var _a;
1948
1946
  const selections = [];
1949
1947
  selections.push({
1950
1948
  name: { kind: "Name", value: "__typename" },
1951
1949
  kind: "Field"
1952
1950
  });
1953
- if (((_a = collection.fields) == null ? void 0 : _a.length) > 0) {
1951
+ if (collection.fields?.length > 0) {
1954
1952
  await sequential(collection.fields, async (x) => {
1955
1953
  const field = await this._buildFieldNodeForFragments(x, depth);
1956
1954
  selections.push(field);
@@ -1965,7 +1963,6 @@ var Builder = class {
1965
1963
  return selections;
1966
1964
  };
1967
1965
  this._buildFieldNodeForFragments = async (field, depth) => {
1968
- var _a, _b;
1969
1966
  switch (field.type) {
1970
1967
  case "string":
1971
1968
  case "image":
@@ -1998,7 +1995,7 @@ var Builder = class {
1998
1995
  selections: filterSelections([passwordValue, passwordChangeRequired])
1999
1996
  });
2000
1997
  case "object":
2001
- if (((_a = field.fields) == null ? void 0 : _a.length) > 0) {
1998
+ if (field.fields?.length > 0) {
2002
1999
  const selections2 = [];
2003
2000
  await sequential(field.fields, async (item) => {
2004
2001
  const field2 = await this._buildFieldNodeForFragments(item, depth);
@@ -2011,7 +2008,7 @@ var Builder = class {
2011
2008
  ...filterSelections(selections2)
2012
2009
  ]
2013
2010
  });
2014
- } else if (((_b = field.templates) == null ? void 0 : _b.length) > 0) {
2011
+ } else if (field.templates?.length > 0) {
2015
2012
  const selections2 = [];
2016
2013
  await sequential(field.templates, async (tem) => {
2017
2014
  if (typeof tem === "object") {
@@ -2673,7 +2670,7 @@ var Builder = class {
2673
2670
  this.addToLookupMap({
2674
2671
  type: name,
2675
2672
  resolveType: "unionData",
2676
- collection: collection == null ? void 0 : collection.name,
2673
+ collection: collection?.name,
2677
2674
  typeMap
2678
2675
  });
2679
2676
  return astBuilder.UnionTypeDefinition({ name, types });
@@ -2883,9 +2880,8 @@ Visit https://tina.io/docs/errors/ui-not-supported/ for more information
2883
2880
  ]
2884
2881
  });
2885
2882
  };
2886
- var _a, _b, _c, _d;
2887
2883
  this.maxDepth = // @ts-ignore
2888
- (_d = (_c = (_b = (_a = config == null ? void 0 : config.tinaSchema.schema) == null ? void 0 : _a.config) == null ? void 0 : _b.client) == null ? void 0 : _c.referenceDepth) != null ? _d : 2;
2884
+ config?.tinaSchema.schema?.config?.client?.referenceDepth ?? 2;
2889
2885
  this.tinaSchema = config.tinaSchema;
2890
2886
  this.lookupMap = {};
2891
2887
  }
@@ -2974,7 +2970,7 @@ var validationCollectionsPathAndMatch = (collections) => {
2974
2970
  return;
2975
2971
  }
2976
2972
  const noMatchCollections = collections.filter((x) => {
2977
- return typeof (x == null ? void 0 : x.match) === "undefined";
2973
+ return typeof x?.match === "undefined";
2978
2974
  }).map((x) => `${x.path}${x.format || "md"}`);
2979
2975
  if (noMatchCollections.length !== new Set(noMatchCollections).size) {
2980
2976
  throw new Error(
@@ -2985,10 +2981,7 @@ var validationCollectionsPathAndMatch = (collections) => {
2985
2981
  const hasMatchAndPath = collections.filter((x) => {
2986
2982
  return typeof x.path !== "undefined" && typeof x.match !== "undefined";
2987
2983
  }).map(
2988
- (x) => {
2989
- var _a, _b;
2990
- return `${x.path}|${((_a = x == null ? void 0 : x.match) == null ? void 0 : _a.exclude) || ""}|${((_b = x == null ? void 0 : x.match) == null ? void 0 : _b.include) || ""}|${x.format || "md"}`;
2991
- }
2984
+ (x) => `${x.path}|${x?.match?.exclude || ""}|${x?.match?.include || ""}|${x.format || "md"}`
2992
2985
  );
2993
2986
  if (hasMatchAndPath.length !== new Set(hasMatchAndPath).size) {
2994
2987
  throw new Error(
@@ -3012,7 +3005,7 @@ var validationCollectionsPathAndMatch = (collections) => {
3012
3005
  );
3013
3006
  }
3014
3007
  const matches = collectionsArr.map(
3015
- (x) => typeof (x == null ? void 0 : x.match) === "object" ? JSON.stringify(x.match) : ""
3008
+ (x) => typeof x?.match === "object" ? JSON.stringify(x.match) : ""
3016
3009
  );
3017
3010
  if (matches.length === new Set(matches).size) {
3018
3011
  return;
@@ -3090,7 +3083,7 @@ var validateField = async (field) => {
3090
3083
  // package.json
3091
3084
  var package_default = {
3092
3085
  name: "@tinacms/graphql",
3093
- version: "1.5.11",
3086
+ version: "1.6.0",
3094
3087
  main: "dist/index.js",
3095
3088
  module: "dist/index.mjs",
3096
3089
  typings: "dist/index.d.ts",
@@ -3116,33 +3109,32 @@ var package_default = {
3116
3109
  types: "pnpm tsc",
3117
3110
  build: "tinacms-scripts build",
3118
3111
  docs: "pnpm typedoc",
3119
- serve: "pnpm nodemon dist/server.js",
3120
3112
  test: "vitest run",
3121
3113
  "test-watch": "vitest"
3122
3114
  },
3123
3115
  dependencies: {
3124
- "@iarna/toml": "^2.2.5",
3116
+ "@iarna/toml": "catalog:",
3125
3117
  "@tinacms/mdx": "workspace:*",
3126
3118
  "@tinacms/schema-tools": "workspace:*",
3127
- "abstract-level": "^1.0.4",
3119
+ "abstract-level": "catalog:",
3128
3120
  "date-fns": "^2.30.0",
3129
- "fast-glob": "^3.3.2",
3130
- "fs-extra": "^11.2.0",
3131
- "glob-parent": "^6.0.2",
3121
+ "fast-glob": "catalog:",
3122
+ "fs-extra": "catalog:",
3123
+ "glob-parent": "catalog:",
3132
3124
  graphql: "15.8.0",
3133
- "gray-matter": "^4.0.3",
3134
- "isomorphic-git": "^1.27.1",
3135
- "js-sha1": "^0.6.0",
3125
+ "gray-matter": "catalog:",
3126
+ "isomorphic-git": "catalog:",
3127
+ "js-sha1": "catalog:",
3136
3128
  "js-yaml": "^3.14.1",
3137
- "jsonpath-plus": "10.1.0",
3138
- "lodash.clonedeep": "^4.5.0",
3139
- "lodash.set": "^4.3.2",
3140
- "lodash.uniqby": "^4.7.0",
3141
- "many-level": "^2.0.0",
3142
- micromatch: "4.0.8",
3143
- "normalize-path": "^3.0.0",
3144
- "readable-stream": "^4.5.2",
3145
- scmp: "^2.1.0",
3129
+ "jsonpath-plus": "catalog:",
3130
+ "lodash.clonedeep": "catalog:",
3131
+ "lodash.set": "catalog:",
3132
+ "lodash.uniqby": "catalog:",
3133
+ "many-level": "catalog:",
3134
+ micromatch: "catalog:",
3135
+ "normalize-path": "catalog:",
3136
+ "readable-stream": "catalog:",
3137
+ scmp: "catalog:",
3146
3138
  yup: "^0.32.11"
3147
3139
  },
3148
3140
  publishConfig: {
@@ -3157,25 +3149,24 @@ var package_default = {
3157
3149
  "@tinacms/scripts": "workspace:*",
3158
3150
  "@types/cors": "^2.8.17",
3159
3151
  "@types/estree": "^0.0.50",
3160
- "@types/express": "^4.17.21",
3152
+ "@types/express": "catalog:",
3161
3153
  "@types/fs-extra": "^9.0.13",
3162
3154
  "@types/js-yaml": "^3.12.10",
3163
- "@types/lodash.camelcase": "^4.3.9",
3164
- "@types/lodash.upperfirst": "^4.3.9",
3165
- "@types/lru-cache": "^5.1.1",
3166
- "@types/mdast": "^3.0.15",
3167
- "@types/micromatch": "^4.0.9",
3168
- "@types/node": "^22.9.0",
3169
- "@types/normalize-path": "^3.0.2",
3170
- "@types/ws": "^7.4.7",
3155
+ "@types/lodash.camelcase": "catalog:",
3156
+ "@types/lodash.upperfirst": "catalog:",
3157
+ "@types/lru-cache": "catalog:",
3158
+ "@types/mdast": "catalog:",
3159
+ "@types/micromatch": "catalog:",
3160
+ "@types/node": "^22.13.1",
3161
+ "@types/normalize-path": "catalog:",
3162
+ "@types/ws": "catalog:",
3171
3163
  "@types/yup": "^0.29.14",
3172
3164
  "jest-file-snapshot": "^0.5.0",
3173
- "memory-level": "^1.0.0",
3174
- nodemon: "3.1.4",
3175
- typescript: "^5.6.3",
3176
- vite: "^4.3.9",
3177
- vitest: "^0.32.2",
3178
- zod: "^3.23.8"
3165
+ "memory-level": "catalog:",
3166
+ typescript: "^5.7.3",
3167
+ vite: "^4.5.9",
3168
+ vitest: "^0.32.4",
3169
+ zod: "^3.24.2"
3179
3170
  }
3180
3171
  };
3181
3172
 
@@ -3257,7 +3248,6 @@ var _buildQueries = async (builder, tinaSchema) => {
3257
3248
  const operationsDefinitions = [];
3258
3249
  const collections = tinaSchema.getCollections();
3259
3250
  await sequential(collections, async (collection) => {
3260
- var _a, _b, _c;
3261
3251
  const queryName = NAMER.queryName(collection.namespace);
3262
3252
  const queryListName = NAMER.generateQueryListName(collection.namespace);
3263
3253
  const queryFilterTypeName = NAMER.dataFilterTypeName(collection.namespace);
@@ -3272,7 +3262,7 @@ var _buildQueries = async (builder, tinaSchema) => {
3272
3262
  filterType: queryFilterTypeName,
3273
3263
  // look for flag to see if the data layer is enabled
3274
3264
  dataLayer: Boolean(
3275
- (_c = (_b = (_a = tinaSchema.config) == null ? void 0 : _a.meta) == null ? void 0 : _b.flags) == null ? void 0 : _c.find((x) => x === "experimentalData")
3265
+ tinaSchema.config?.meta?.flags?.find((x) => x === "experimentalData")
3276
3266
  )
3277
3267
  })
3278
3268
  );
@@ -3332,7 +3322,9 @@ var _buildSchema = async (builder, tinaSchema) => {
3332
3322
  await builder.buildCreateCollectionFolderMutation()
3333
3323
  );
3334
3324
  await sequential(collections, async (collection) => {
3335
- queryTypeDefinitionFields.push(await builder.collectionDocument(collection));
3325
+ queryTypeDefinitionFields.push(
3326
+ await builder.collectionDocument(collection)
3327
+ );
3336
3328
  if (collection.isAuthCollection) {
3337
3329
  queryTypeDefinitionFields.push(
3338
3330
  await builder.authenticationCollectionDocument(collection)
@@ -3382,251 +3374,11 @@ var import_graphql5 = require("graphql");
3382
3374
  // src/resolver/index.ts
3383
3375
  var import_path3 = __toESM(require("path"));
3384
3376
  var import_isValid = __toESM(require("date-fns/isValid/index.js"));
3377
+ var import_jsonpath_plus2 = require("jsonpath-plus");
3385
3378
 
3386
3379
  // src/mdx/index.ts
3387
3380
  var import_mdx = require("@tinacms/mdx");
3388
3381
 
3389
- // src/resolver/index.ts
3390
- var import_jsonpath_plus2 = require("jsonpath-plus");
3391
-
3392
- // src/resolver/error.ts
3393
- var TinaGraphQLError = class extends Error {
3394
- constructor(message, extensions) {
3395
- super(message);
3396
- if (!this.name) {
3397
- Object.defineProperty(this, "name", { value: "TinaGraphQLError" });
3398
- }
3399
- this.extensions = { ...extensions };
3400
- }
3401
- };
3402
- var TinaFetchError = class extends Error {
3403
- constructor(message, args) {
3404
- super(message);
3405
- this.name = "TinaFetchError";
3406
- this.collection = args.collection;
3407
- this.stack = args.stack;
3408
- this.file = args.file;
3409
- this.originalError = args.originalError;
3410
- }
3411
- };
3412
- var TinaQueryError = class extends TinaFetchError {
3413
- constructor(args) {
3414
- super(
3415
- `Error querying file ${args.file} from collection ${args.collection}. ${auditMessage(args.includeAuditMessage)}`,
3416
- args
3417
- );
3418
- }
3419
- };
3420
- var TinaParseDocumentError = class extends TinaFetchError {
3421
- constructor(args) {
3422
- super(
3423
- `Error parsing file ${args.file} from collection ${args.collection}. ${auditMessage(args.includeAuditMessage)}`,
3424
- args
3425
- );
3426
- }
3427
- toString() {
3428
- return super.toString() + "\n OriginalError: \n" + this.originalError.toString();
3429
- }
3430
- };
3431
- var auditMessage = (includeAuditMessage = true) => includeAuditMessage ? `Please run "tinacms audit" or add the --verbose option for more info` : "";
3432
- var handleFetchErrorError = (e, verbose) => {
3433
- if (e instanceof Error) {
3434
- if (e instanceof TinaFetchError) {
3435
- if (verbose) {
3436
- console.log(e.toString());
3437
- console.log(e);
3438
- console.log(e.stack);
3439
- }
3440
- }
3441
- } else {
3442
- console.error(e);
3443
- }
3444
- throw e;
3445
- };
3446
-
3447
- // src/resolver/filter-utils.ts
3448
- var resolveReferences = async (filter, fields, resolver) => {
3449
- for (const fieldKey of Object.keys(filter)) {
3450
- const fieldDefinition = fields.find(
3451
- (f) => f.name === fieldKey
3452
- );
3453
- if (fieldDefinition) {
3454
- if (fieldDefinition.type === "reference") {
3455
- const { edges, values } = await resolver(filter, fieldDefinition);
3456
- if (edges.length === 1) {
3457
- filter[fieldKey] = {
3458
- eq: values[0]
3459
- };
3460
- } else if (edges.length > 1) {
3461
- filter[fieldKey] = {
3462
- in: values
3463
- };
3464
- } else {
3465
- filter[fieldKey] = {
3466
- eq: "___null___"
3467
- };
3468
- }
3469
- } else if (fieldDefinition.type === "object") {
3470
- if (fieldDefinition.templates) {
3471
- for (const templateName of Object.keys(filter[fieldKey])) {
3472
- const template = fieldDefinition.templates.find(
3473
- (template2) => !(typeof template2 === "string") && template2.name === templateName
3474
- );
3475
- if (template) {
3476
- await resolveReferences(
3477
- filter[fieldKey][templateName],
3478
- template.fields,
3479
- resolver
3480
- );
3481
- } else {
3482
- throw new Error(`Template ${templateName} not found`);
3483
- }
3484
- }
3485
- } else {
3486
- await resolveReferences(
3487
- filter[fieldKey],
3488
- fieldDefinition.fields,
3489
- resolver
3490
- );
3491
- }
3492
- }
3493
- } else {
3494
- throw new Error(`Unable to find field ${fieldKey}`);
3495
- }
3496
- }
3497
- };
3498
- var collectConditionsForChildFields = (filterNode, fields, pathExpression, collectCondition) => {
3499
- for (const childFieldName of Object.keys(filterNode)) {
3500
- const childField = fields.find((field) => field.name === childFieldName);
3501
- if (!childField) {
3502
- throw new Error(`Unable to find type for field ${childFieldName}`);
3503
- }
3504
- collectConditionsForField(
3505
- childFieldName,
3506
- childField,
3507
- filterNode[childFieldName],
3508
- pathExpression,
3509
- collectCondition
3510
- );
3511
- }
3512
- };
3513
- var collectConditionsForObjectField = (fieldName, field, filterNode, pathExpression, collectCondition) => {
3514
- if (field.list && field.templates) {
3515
- for (const [filterKey, childFilterNode] of Object.entries(filterNode)) {
3516
- const template = field.templates.find(
3517
- (template2) => !(typeof template2 === "string") && template2.name === filterKey
3518
- );
3519
- const jsonPath = `${fieldName}[?(@._template=="${filterKey}")]`;
3520
- const filterPath = pathExpression ? `${pathExpression}.${jsonPath}` : jsonPath;
3521
- collectConditionsForChildFields(
3522
- childFilterNode,
3523
- template.fields,
3524
- filterPath,
3525
- collectCondition
3526
- );
3527
- }
3528
- } else {
3529
- const jsonPath = `${fieldName}${field.list ? "[*]" : ""}`;
3530
- const filterPath = pathExpression ? `${pathExpression}.${jsonPath}` : `${jsonPath}`;
3531
- collectConditionsForChildFields(
3532
- filterNode,
3533
- field.fields,
3534
- filterPath,
3535
- collectCondition
3536
- );
3537
- }
3538
- };
3539
- var collectConditionsForField = (fieldName, field, filterNode, pathExpression, collectCondition) => {
3540
- if (field.type === "object") {
3541
- collectConditionsForObjectField(
3542
- fieldName,
3543
- field,
3544
- filterNode,
3545
- pathExpression,
3546
- collectCondition
3547
- );
3548
- } else {
3549
- collectCondition({
3550
- filterPath: pathExpression ? `${pathExpression}.${fieldName}` : fieldName,
3551
- filterExpression: {
3552
- _type: field.type,
3553
- _list: !!field.list,
3554
- ...filterNode
3555
- }
3556
- });
3557
- }
3558
- };
3559
-
3560
- // src/resolver/media-utils.ts
3561
- var resolveMediaCloudToRelative = (value, config = { useRelativeMedia: true }, schema) => {
3562
- if (config && value) {
3563
- if (config.useRelativeMedia === true) {
3564
- return value;
3565
- }
3566
- if (hasTinaMediaConfig(schema) === true) {
3567
- const assetsURL = `https://${config.assetsHost}/${config.clientId}`;
3568
- if (typeof value === "string" && value.includes(assetsURL)) {
3569
- const cleanMediaRoot = cleanUpSlashes(
3570
- schema.config.media.tina.mediaRoot
3571
- );
3572
- const strippedURL = value.replace(assetsURL, "");
3573
- return `${cleanMediaRoot}${strippedURL}`;
3574
- }
3575
- if (Array.isArray(value)) {
3576
- return value.map((v) => {
3577
- if (!v || typeof v !== "string") return v;
3578
- const cleanMediaRoot = cleanUpSlashes(
3579
- schema.config.media.tina.mediaRoot
3580
- );
3581
- const strippedURL = v.replace(assetsURL, "");
3582
- return `${cleanMediaRoot}${strippedURL}`;
3583
- });
3584
- }
3585
- return value;
3586
- }
3587
- return value;
3588
- } else {
3589
- return value;
3590
- }
3591
- };
3592
- var resolveMediaRelativeToCloud = (value, config = { useRelativeMedia: true }, schema) => {
3593
- if (config && value) {
3594
- if (config.useRelativeMedia === true) {
3595
- return value;
3596
- }
3597
- if (hasTinaMediaConfig(schema) === true) {
3598
- const cleanMediaRoot = cleanUpSlashes(schema.config.media.tina.mediaRoot);
3599
- if (typeof value === "string") {
3600
- const strippedValue = value.replace(cleanMediaRoot, "");
3601
- return `https://${config.assetsHost}/${config.clientId}${strippedValue}`;
3602
- }
3603
- if (Array.isArray(value)) {
3604
- return value.map((v) => {
3605
- if (!v || typeof v !== "string") return v;
3606
- const strippedValue = v.replace(cleanMediaRoot, "");
3607
- return `https://${config.assetsHost}/${config.clientId}${strippedValue}`;
3608
- });
3609
- }
3610
- }
3611
- return value;
3612
- } else {
3613
- return value;
3614
- }
3615
- };
3616
- var cleanUpSlashes = (path7) => {
3617
- if (path7) {
3618
- return `/${path7.replace(/^\/+|\/+$/gm, "")}`;
3619
- }
3620
- return "";
3621
- };
3622
- var hasTinaMediaConfig = (schema) => {
3623
- var _a, _b, _c, _d, _e, _f, _g, _h;
3624
- if (!((_b = (_a = schema.config) == null ? void 0 : _a.media) == null ? void 0 : _b.tina)) return false;
3625
- if (typeof ((_e = (_d = (_c = schema.config) == null ? void 0 : _c.media) == null ? void 0 : _d.tina) == null ? void 0 : _e.publicFolder) !== "string" && typeof ((_h = (_g = (_f = schema.config) == null ? void 0 : _f.media) == null ? void 0 : _g.tina) == null ? void 0 : _h.mediaRoot) !== "string")
3626
- return false;
3627
- return true;
3628
- };
3629
-
3630
3382
  // src/resolver/index.ts
3631
3383
  var import_graphql3 = require("graphql");
3632
3384
 
@@ -3648,7 +3400,9 @@ var LevelProxyHandler = {
3648
3400
  throw new Error(`The property, ${property.toString()}, doesn't exist`);
3649
3401
  }
3650
3402
  if (typeof target[property] !== "function") {
3651
- throw new Error(`The property, ${property.toString()}, is not a function`);
3403
+ throw new Error(
3404
+ `The property, ${property.toString()}, is not a function`
3405
+ );
3652
3406
  }
3653
3407
  if (property === "get") {
3654
3408
  return async (...args) => {
@@ -3686,11 +3440,11 @@ var import_path2 = __toESM(require("path"));
3686
3440
 
3687
3441
  // src/database/util.ts
3688
3442
  var import_toml = __toESM(require("@iarna/toml"));
3689
- var import_js_yaml = __toESM(require("js-yaml"));
3690
- var import_gray_matter = __toESM(require("gray-matter"));
3691
3443
  var import_schema_tools4 = require("@tinacms/schema-tools");
3692
- var import_micromatch = __toESM(require("micromatch"));
3444
+ var import_gray_matter = __toESM(require("gray-matter"));
3445
+ var import_js_yaml = __toESM(require("js-yaml"));
3693
3446
  var import_path = __toESM(require("path"));
3447
+ var import_micromatch = __toESM(require("micromatch"));
3694
3448
 
3695
3449
  // src/database/alias-utils.ts
3696
3450
  var replaceBlockAliases = (template, item) => {
@@ -3727,22 +3481,20 @@ var replaceNameOverrides = (template, obj) => {
3727
3481
  }
3728
3482
  };
3729
3483
  function isBlockField(field) {
3730
- var _a;
3731
- return field && field.type === "object" && ((_a = field.templates) == null ? void 0 : _a.length) > 0;
3484
+ return field && field.type === "object" && field.templates?.length > 0;
3732
3485
  }
3733
3486
  var _replaceNameOverrides = (fields, obj) => {
3734
3487
  const output = {};
3735
3488
  Object.keys(obj).forEach((key) => {
3736
3489
  const field = fields.find(
3737
- (fieldWithMatchingAlias) => ((fieldWithMatchingAlias == null ? void 0 : fieldWithMatchingAlias.nameOverride) || (fieldWithMatchingAlias == null ? void 0 : fieldWithMatchingAlias.name)) === key
3490
+ (fieldWithMatchingAlias) => (fieldWithMatchingAlias?.nameOverride || fieldWithMatchingAlias?.name) === key
3738
3491
  );
3739
- output[(field == null ? void 0 : field.name) || key] = (field == null ? void 0 : field.type) == "object" ? replaceNameOverrides(field, obj[key]) : obj[key];
3492
+ output[field?.name || key] = field?.type == "object" ? replaceNameOverrides(field, obj[key]) : obj[key];
3740
3493
  });
3741
3494
  return output;
3742
3495
  };
3743
3496
  var getTemplateForData = (field, data) => {
3744
- var _a;
3745
- if ((_a = field.templates) == null ? void 0 : _a.length) {
3497
+ if (field.templates?.length) {
3746
3498
  const templateKey = "_template";
3747
3499
  if (data[templateKey]) {
3748
3500
  const result = field.templates.find(
@@ -3800,8 +3552,8 @@ var _applyNameOverrides = (fields, obj) => {
3800
3552
  const output = {};
3801
3553
  Object.keys(obj).forEach((key) => {
3802
3554
  const field = fields.find((field2) => field2.name === key);
3803
- const outputKey = (field == null ? void 0 : field.nameOverride) || key;
3804
- output[outputKey] = (field == null ? void 0 : field.type) === "object" ? applyNameOverrides(field, obj[key]) : obj[key];
3555
+ const outputKey = field?.nameOverride || key;
3556
+ output[outputKey] = field?.type === "object" ? applyNameOverrides(field, obj[key]) : obj[key];
3805
3557
  });
3806
3558
  return output;
3807
3559
  };
@@ -3814,7 +3566,6 @@ var matterEngines = {
3814
3566
  }
3815
3567
  };
3816
3568
  var stringifyFile = (content, format, keepTemplateKey, markdownParseConfig) => {
3817
- var _a, _b;
3818
3569
  const {
3819
3570
  _relativePath,
3820
3571
  _keepTemplateKey,
@@ -3838,9 +3589,9 @@ var stringifyFile = (content, format, keepTemplateKey, markdownParseConfig) => {
3838
3589
  ${$_body}`,
3839
3590
  strippedContent,
3840
3591
  {
3841
- language: (_a = markdownParseConfig == null ? void 0 : markdownParseConfig.frontmatterFormat) != null ? _a : "yaml",
3592
+ language: markdownParseConfig?.frontmatterFormat ?? "yaml",
3842
3593
  engines: matterEngines,
3843
- delimiters: (_b = markdownParseConfig == null ? void 0 : markdownParseConfig.frontmatterDelimiters) != null ? _b : "---"
3594
+ delimiters: markdownParseConfig?.frontmatterDelimiters ?? "---"
3844
3595
  }
3845
3596
  );
3846
3597
  return ok;
@@ -3856,15 +3607,14 @@ ${$_body}`,
3856
3607
  }
3857
3608
  };
3858
3609
  var parseFile = (content, format, yupSchema, markdownParseConfig) => {
3859
- var _a, _b;
3860
3610
  try {
3861
3611
  switch (format) {
3862
3612
  case ".markdown":
3863
3613
  case ".mdx":
3864
3614
  case ".md":
3865
3615
  const contentJSON = (0, import_gray_matter.default)(content || "", {
3866
- language: (_a = markdownParseConfig == null ? void 0 : markdownParseConfig.frontmatterFormat) != null ? _a : "yaml",
3867
- delimiters: (_b = markdownParseConfig == null ? void 0 : markdownParseConfig.frontmatterDelimiters) != null ? _b : "---",
3616
+ language: markdownParseConfig?.frontmatterFormat ?? "yaml",
3617
+ delimiters: markdownParseConfig?.frontmatterDelimiters ?? "---",
3868
3618
  engines: matterEngines
3869
3619
  });
3870
3620
  const markdownData = {
@@ -3963,7 +3713,7 @@ var transformDocument = (filepath, contentObject, tinaSchema) => {
3963
3713
  ),
3964
3714
  template: void 0
3965
3715
  } : tinaSchema.getCollectionAndTemplateByFullPath(filepath, templateName);
3966
- const field = template == null ? void 0 : template.fields.find((field2) => {
3716
+ const field = template?.fields.find((field2) => {
3967
3717
  if (field2.type === "string" || field2.type === "rich-text") {
3968
3718
  if (field2.isBody) {
3969
3719
  return true;
@@ -3983,7 +3733,7 @@ var transformDocument = (filepath, contentObject, tinaSchema) => {
3983
3733
  ...data,
3984
3734
  _collection: collection.name,
3985
3735
  _keepTemplateKey: !!collection.templates,
3986
- _template: (template == null ? void 0 : template.namespace) ? lastItem(template == null ? void 0 : template.namespace) : void 0,
3736
+ _template: template?.namespace ? lastItem(template?.namespace) : void 0,
3987
3737
  _relativePath: filepath.replace(collection.path, "").replace(/^\/|\/$/g, ""),
3988
3738
  _id: filepath
3989
3739
  };
@@ -3992,10 +3742,10 @@ function hasOwnProperty(obj, prop) {
3992
3742
  return obj.hasOwnProperty(prop);
3993
3743
  }
3994
3744
  var getTemplateForFile = (templateInfo, data) => {
3995
- if ((templateInfo == null ? void 0 : templateInfo.type) === "object") {
3745
+ if (templateInfo?.type === "object") {
3996
3746
  return templateInfo.template;
3997
3747
  }
3998
- if ((templateInfo == null ? void 0 : templateInfo.type) === "union") {
3748
+ if (templateInfo?.type === "union") {
3999
3749
  if (hasOwnProperty(data, "_template")) {
4000
3750
  const template = templateInfo.templates.find(
4001
3751
  (t) => lastItem(t.namespace) === data._template
@@ -4019,8 +3769,8 @@ var loadAndParseWithAliases = async (bridge, filepath, collection, templateInfo)
4019
3769
  import_path.default.extname(filepath),
4020
3770
  (yup3) => yup3.object({}),
4021
3771
  {
4022
- frontmatterDelimiters: collection == null ? void 0 : collection.frontmatterDelimiters,
4023
- frontmatterFormat: collection == null ? void 0 : collection.frontmatterFormat
3772
+ frontmatterDelimiters: collection?.frontmatterDelimiters,
3773
+ frontmatterFormat: collection?.frontmatterFormat
4024
3774
  }
4025
3775
  );
4026
3776
  const template = getTemplateForFile(templateInfo, data);
@@ -4035,6 +3785,9 @@ var loadAndParseWithAliases = async (bridge, filepath, collection, templateInfo)
4035
3785
 
4036
3786
  // src/database/datalayer.ts
4037
3787
  var DEFAULT_COLLECTION_SORT_KEY = "__filepath__";
3788
+ var REFS_COLLECTIONS_SORT_KEY = "__refs__";
3789
+ var REFS_REFERENCE_FIELD = "__tina_ref__";
3790
+ var REFS_PATH_FIELD = "__tina_ref_path__";
4038
3791
  var DEFAULT_NUMERIC_LPAD = 4;
4039
3792
  var applyPadding = (input, pad) => {
4040
3793
  if (pad) {
@@ -4578,62 +4331,349 @@ var makeFolderOpsForCollection = (folderTree, collection, indexDefinitions, opTy
4578
4331
  });
4579
4332
  }
4580
4333
  }
4581
- return result;
4334
+ return result;
4335
+ };
4336
+ var makeIndexOpsForDocument = (filepath, collection, indexDefinitions, data, opType, level, escapeStr = stringEscaper) => {
4337
+ const result = [];
4338
+ if (collection) {
4339
+ const collectionSublevel = level.sublevel(collection, SUBLEVEL_OPTIONS);
4340
+ for (const [sort, definition] of Object.entries(indexDefinitions)) {
4341
+ const indexedValue = makeKeyForField(definition, data, escapeStr);
4342
+ const indexSublevel = collectionSublevel.sublevel(sort, SUBLEVEL_OPTIONS);
4343
+ if (sort === DEFAULT_COLLECTION_SORT_KEY) {
4344
+ result.push({
4345
+ type: opType,
4346
+ key: filepath,
4347
+ sublevel: indexSublevel,
4348
+ value: opType === "put" ? {} : void 0
4349
+ });
4350
+ } else {
4351
+ if (indexedValue) {
4352
+ result.push({
4353
+ type: opType,
4354
+ key: `${indexedValue}${INDEX_KEY_FIELD_SEPARATOR}${filepath}`,
4355
+ sublevel: indexSublevel,
4356
+ value: opType === "put" ? {} : void 0
4357
+ });
4358
+ }
4359
+ }
4360
+ }
4361
+ }
4362
+ return result;
4363
+ };
4364
+ var makeRefOpsForDocument = (filepath, collection, references, data, opType, level) => {
4365
+ const result = [];
4366
+ if (collection) {
4367
+ for (const [c, referencePaths] of Object.entries(references || {})) {
4368
+ if (!referencePaths.length) {
4369
+ continue;
4370
+ }
4371
+ const collectionSublevel = level.sublevel(c, SUBLEVEL_OPTIONS);
4372
+ const refSublevel = collectionSublevel.sublevel(
4373
+ REFS_COLLECTIONS_SORT_KEY,
4374
+ SUBLEVEL_OPTIONS
4375
+ );
4376
+ const references2 = {};
4377
+ for (const path7 of referencePaths) {
4378
+ const ref = (0, import_jsonpath_plus.JSONPath)({ path: path7, json: data });
4379
+ if (!ref) {
4380
+ continue;
4381
+ }
4382
+ if (Array.isArray(ref)) {
4383
+ for (const r of ref) {
4384
+ if (!r) {
4385
+ continue;
4386
+ }
4387
+ if (references2[r]) {
4388
+ references2[r].push(path7);
4389
+ } else {
4390
+ references2[r] = [path7];
4391
+ }
4392
+ }
4393
+ } else {
4394
+ if (references2[ref]) {
4395
+ references2[ref].push(path7);
4396
+ } else {
4397
+ references2[ref] = [path7];
4398
+ }
4399
+ }
4400
+ }
4401
+ for (const ref of Object.keys(references2)) {
4402
+ for (const path7 of references2[ref]) {
4403
+ result.push({
4404
+ type: opType,
4405
+ key: `${ref}${INDEX_KEY_FIELD_SEPARATOR}${path7}${INDEX_KEY_FIELD_SEPARATOR}${filepath}`,
4406
+ sublevel: refSublevel,
4407
+ value: opType === "put" ? {} : void 0
4408
+ });
4409
+ }
4410
+ }
4411
+ }
4412
+ }
4413
+ return result;
4414
+ };
4415
+ var makeStringEscaper = (regex, replacement) => {
4416
+ return (input) => {
4417
+ if (Array.isArray(input)) {
4418
+ return input.map(
4419
+ (val) => val.replace(regex, replacement)
4420
+ );
4421
+ } else {
4422
+ if (typeof input === "string") {
4423
+ return input.replace(regex, replacement);
4424
+ } else {
4425
+ return input;
4426
+ }
4427
+ }
4428
+ };
4429
+ };
4430
+ var stringEscaper = makeStringEscaper(
4431
+ new RegExp(INDEX_KEY_FIELD_SEPARATOR, "gm"),
4432
+ encodeURIComponent(INDEX_KEY_FIELD_SEPARATOR)
4433
+ );
4434
+
4435
+ // src/resolver/error.ts
4436
+ var TinaGraphQLError = class extends Error {
4437
+ constructor(message, extensions) {
4438
+ super(message);
4439
+ if (!this.name) {
4440
+ Object.defineProperty(this, "name", { value: "TinaGraphQLError" });
4441
+ }
4442
+ this.extensions = { ...extensions };
4443
+ }
4444
+ };
4445
+ var TinaFetchError = class extends Error {
4446
+ constructor(message, args) {
4447
+ super(message);
4448
+ this.name = "TinaFetchError";
4449
+ this.collection = args.collection;
4450
+ this.stack = args.stack;
4451
+ this.file = args.file;
4452
+ this.originalError = args.originalError;
4453
+ }
4454
+ };
4455
+ var TinaQueryError = class extends TinaFetchError {
4456
+ constructor(args) {
4457
+ super(
4458
+ `Error querying file ${args.file} from collection ${args.collection}. ${auditMessage(args.includeAuditMessage)}`,
4459
+ args
4460
+ );
4461
+ }
4462
+ };
4463
+ var TinaParseDocumentError = class extends TinaFetchError {
4464
+ constructor(args) {
4465
+ super(
4466
+ `Error parsing file ${args.file} from collection ${args.collection}. ${auditMessage(args.includeAuditMessage)}`,
4467
+ args
4468
+ );
4469
+ }
4470
+ toString() {
4471
+ return super.toString() + "\n OriginalError: \n" + this.originalError.toString();
4472
+ }
4473
+ };
4474
+ var auditMessage = (includeAuditMessage = true) => includeAuditMessage ? `Please run "tinacms audit" or add the --verbose option for more info` : "";
4475
+ var handleFetchErrorError = (e, verbose) => {
4476
+ if (e instanceof Error) {
4477
+ if (e instanceof TinaFetchError) {
4478
+ if (verbose) {
4479
+ console.log(e.toString());
4480
+ console.log(e);
4481
+ console.log(e.stack);
4482
+ }
4483
+ }
4484
+ } else {
4485
+ console.error(e);
4486
+ }
4487
+ throw e;
4488
+ };
4489
+
4490
+ // src/resolver/filter-utils.ts
4491
+ var resolveReferences = async (filter, fields, resolver) => {
4492
+ for (const fieldKey of Object.keys(filter)) {
4493
+ const fieldDefinition = fields.find(
4494
+ (f) => f.name === fieldKey
4495
+ );
4496
+ if (fieldDefinition) {
4497
+ if (fieldDefinition.type === "reference") {
4498
+ const { edges, values } = await resolver(filter, fieldDefinition);
4499
+ if (edges.length === 1) {
4500
+ filter[fieldKey] = {
4501
+ eq: values[0]
4502
+ };
4503
+ } else if (edges.length > 1) {
4504
+ filter[fieldKey] = {
4505
+ in: values
4506
+ };
4507
+ } else {
4508
+ filter[fieldKey] = {
4509
+ eq: "___null___"
4510
+ };
4511
+ }
4512
+ } else if (fieldDefinition.type === "object") {
4513
+ if (fieldDefinition.templates) {
4514
+ for (const templateName of Object.keys(filter[fieldKey])) {
4515
+ const template = fieldDefinition.templates.find(
4516
+ (template2) => !(typeof template2 === "string") && template2.name === templateName
4517
+ );
4518
+ if (template) {
4519
+ await resolveReferences(
4520
+ filter[fieldKey][templateName],
4521
+ template.fields,
4522
+ resolver
4523
+ );
4524
+ } else {
4525
+ throw new Error(`Template ${templateName} not found`);
4526
+ }
4527
+ }
4528
+ } else {
4529
+ await resolveReferences(
4530
+ filter[fieldKey],
4531
+ fieldDefinition.fields,
4532
+ resolver
4533
+ );
4534
+ }
4535
+ }
4536
+ } else {
4537
+ throw new Error(`Unable to find field ${fieldKey}`);
4538
+ }
4539
+ }
4540
+ };
4541
+ var collectConditionsForChildFields = (filterNode, fields, pathExpression, collectCondition) => {
4542
+ for (const childFieldName of Object.keys(filterNode)) {
4543
+ const childField = fields.find((field) => field.name === childFieldName);
4544
+ if (!childField) {
4545
+ throw new Error(`Unable to find type for field ${childFieldName}`);
4546
+ }
4547
+ collectConditionsForField(
4548
+ childFieldName,
4549
+ childField,
4550
+ filterNode[childFieldName],
4551
+ pathExpression,
4552
+ collectCondition
4553
+ );
4554
+ }
4555
+ };
4556
+ var collectConditionsForObjectField = (fieldName, field, filterNode, pathExpression, collectCondition) => {
4557
+ if (field.list && field.templates) {
4558
+ for (const [filterKey, childFilterNode] of Object.entries(filterNode)) {
4559
+ const template = field.templates.find(
4560
+ (template2) => !(typeof template2 === "string") && template2.name === filterKey
4561
+ );
4562
+ const jsonPath = `${fieldName}[?(@._template=="${filterKey}")]`;
4563
+ const filterPath = pathExpression ? `${pathExpression}.${jsonPath}` : jsonPath;
4564
+ collectConditionsForChildFields(
4565
+ childFilterNode,
4566
+ template.fields,
4567
+ filterPath,
4568
+ collectCondition
4569
+ );
4570
+ }
4571
+ } else {
4572
+ const jsonPath = `${fieldName}${field.list ? "[*]" : ""}`;
4573
+ const filterPath = pathExpression ? `${pathExpression}.${jsonPath}` : `${jsonPath}`;
4574
+ collectConditionsForChildFields(
4575
+ filterNode,
4576
+ field.fields,
4577
+ filterPath,
4578
+ collectCondition
4579
+ );
4580
+ }
4581
+ };
4582
+ var collectConditionsForField = (fieldName, field, filterNode, pathExpression, collectCondition) => {
4583
+ if (field.type === "object") {
4584
+ collectConditionsForObjectField(
4585
+ fieldName,
4586
+ field,
4587
+ filterNode,
4588
+ pathExpression,
4589
+ collectCondition
4590
+ );
4591
+ } else {
4592
+ collectCondition({
4593
+ filterPath: pathExpression ? `${pathExpression}.${fieldName}` : fieldName,
4594
+ filterExpression: {
4595
+ _type: field.type,
4596
+ _list: !!field.list,
4597
+ ...filterNode
4598
+ }
4599
+ });
4600
+ }
4601
+ };
4602
+
4603
+ // src/resolver/media-utils.ts
4604
+ var resolveMediaCloudToRelative = (value, config = { useRelativeMedia: true }, schema) => {
4605
+ if (config && value) {
4606
+ if (config.useRelativeMedia === true) {
4607
+ return value;
4608
+ }
4609
+ if (hasTinaMediaConfig(schema) === true) {
4610
+ const assetsURL = `https://${config.assetsHost}/${config.clientId}`;
4611
+ if (typeof value === "string" && value.includes(assetsURL)) {
4612
+ const cleanMediaRoot = cleanUpSlashes(
4613
+ schema.config.media.tina.mediaRoot
4614
+ );
4615
+ const strippedURL = value.replace(assetsURL, "");
4616
+ return `${cleanMediaRoot}${strippedURL}`;
4617
+ }
4618
+ if (Array.isArray(value)) {
4619
+ return value.map((v) => {
4620
+ if (!v || typeof v !== "string") return v;
4621
+ const cleanMediaRoot = cleanUpSlashes(
4622
+ schema.config.media.tina.mediaRoot
4623
+ );
4624
+ const strippedURL = v.replace(assetsURL, "");
4625
+ return `${cleanMediaRoot}${strippedURL}`;
4626
+ });
4627
+ }
4628
+ return value;
4629
+ }
4630
+ return value;
4631
+ } else {
4632
+ return value;
4633
+ }
4582
4634
  };
4583
- var makeIndexOpsForDocument = (filepath, collection, indexDefinitions, data, opType, level, escapeStr = stringEscaper) => {
4584
- const result = [];
4585
- if (collection) {
4586
- const collectionSublevel = level.sublevel(collection, SUBLEVEL_OPTIONS);
4587
- for (const [sort, definition] of Object.entries(indexDefinitions)) {
4588
- const indexedValue = makeKeyForField(definition, data, escapeStr);
4589
- const indexSublevel = collectionSublevel.sublevel(sort, SUBLEVEL_OPTIONS);
4590
- if (sort === DEFAULT_COLLECTION_SORT_KEY) {
4591
- result.push({
4592
- type: opType,
4593
- key: filepath,
4594
- sublevel: indexSublevel,
4595
- value: opType === "put" ? {} : void 0
4635
+ var resolveMediaRelativeToCloud = (value, config = { useRelativeMedia: true }, schema) => {
4636
+ if (config && value) {
4637
+ if (config.useRelativeMedia === true) {
4638
+ return value;
4639
+ }
4640
+ if (hasTinaMediaConfig(schema) === true) {
4641
+ const cleanMediaRoot = cleanUpSlashes(schema.config.media.tina.mediaRoot);
4642
+ if (typeof value === "string") {
4643
+ const strippedValue = value.replace(cleanMediaRoot, "");
4644
+ return `https://${config.assetsHost}/${config.clientId}${strippedValue}`;
4645
+ }
4646
+ if (Array.isArray(value)) {
4647
+ return value.map((v) => {
4648
+ if (!v || typeof v !== "string") return v;
4649
+ const strippedValue = v.replace(cleanMediaRoot, "");
4650
+ return `https://${config.assetsHost}/${config.clientId}${strippedValue}`;
4596
4651
  });
4597
- } else {
4598
- if (indexedValue) {
4599
- result.push({
4600
- type: opType,
4601
- key: `${indexedValue}${INDEX_KEY_FIELD_SEPARATOR}${filepath}`,
4602
- sublevel: indexSublevel,
4603
- value: opType === "put" ? {} : void 0
4604
- });
4605
- }
4606
4652
  }
4607
4653
  }
4654
+ return value;
4655
+ } else {
4656
+ return value;
4608
4657
  }
4609
- return result;
4610
4658
  };
4611
- var makeStringEscaper = (regex, replacement) => {
4612
- return (input) => {
4613
- if (Array.isArray(input)) {
4614
- return input.map(
4615
- (val) => val.replace(regex, replacement)
4616
- );
4617
- } else {
4618
- if (typeof input === "string") {
4619
- return input.replace(regex, replacement);
4620
- } else {
4621
- return input;
4622
- }
4623
- }
4624
- };
4659
+ var cleanUpSlashes = (path7) => {
4660
+ if (path7) {
4661
+ return `/${path7.replace(/^\/+|\/+$/gm, "")}`;
4662
+ }
4663
+ return "";
4664
+ };
4665
+ var hasTinaMediaConfig = (schema) => {
4666
+ if (!schema.config?.media?.tina) return false;
4667
+ if (typeof schema.config?.media?.tina?.publicFolder !== "string" && typeof schema.config?.media?.tina?.mediaRoot !== "string")
4668
+ return false;
4669
+ return true;
4625
4670
  };
4626
- var stringEscaper = makeStringEscaper(
4627
- new RegExp(INDEX_KEY_FIELD_SEPARATOR, "gm"),
4628
- encodeURIComponent(INDEX_KEY_FIELD_SEPARATOR)
4629
- );
4630
4671
 
4631
4672
  // src/resolver/index.ts
4632
4673
  var createResolver = (args) => {
4633
4674
  return new Resolver(args);
4634
4675
  };
4635
4676
  var resolveFieldData = async ({ namespace, ...field }, rawData, accumulator, tinaSchema, config, isAudit) => {
4636
- var _a, _b;
4637
4677
  if (!rawData) {
4638
4678
  return void 0;
4639
4679
  }
@@ -4661,7 +4701,7 @@ var resolveFieldData = async ({ namespace, ...field }, rawData, accumulator, tin
4661
4701
  accumulator[field.name] = {
4662
4702
  value: void 0,
4663
4703
  // never resolve the password hash
4664
- passwordChangeRequired: (_a = value["passwordChangeRequired"]) != null ? _a : false
4704
+ passwordChangeRequired: value["passwordChangeRequired"] ?? false
4665
4705
  };
4666
4706
  break;
4667
4707
  case "image":
@@ -4677,11 +4717,11 @@ var resolveFieldData = async ({ namespace, ...field }, rawData, accumulator, tin
4677
4717
  field,
4678
4718
  (value2) => resolveMediaRelativeToCloud(value2, config, tinaSchema.schema)
4679
4719
  );
4680
- if (((_b = tree == null ? void 0 : tree.children[0]) == null ? void 0 : _b.type) === "invalid_markdown") {
4720
+ if (tree?.children[0]?.type === "invalid_markdown") {
4681
4721
  if (isAudit) {
4682
- const invalidNode = tree == null ? void 0 : tree.children[0];
4722
+ const invalidNode = tree?.children[0];
4683
4723
  throw new import_graphql3.GraphQLError(
4684
- `${invalidNode == null ? void 0 : invalidNode.message}${invalidNode.position ? ` at line ${invalidNode.position.start.line}, column ${invalidNode.position.start.column}` : ""}`
4724
+ `${invalidNode?.message}${invalidNode.position ? ` at line ${invalidNode.position.start.line}, column ${invalidNode.position.start.column}` : ""}`
4685
4725
  );
4686
4726
  }
4687
4727
  }
@@ -4794,11 +4834,11 @@ var transformDocumentIntoPayload = async (fullPath, rawData, tinaSchema, config,
4794
4834
  });
4795
4835
  }
4796
4836
  const titleField = template.fields.find((x) => {
4797
- if (x.type === "string" && (x == null ? void 0 : x.isTitle)) {
4837
+ if (x.type === "string" && x?.isTitle) {
4798
4838
  return true;
4799
4839
  }
4800
4840
  });
4801
- const titleFieldName = titleField == null ? void 0 : titleField.name;
4841
+ const titleFieldName = titleField?.name;
4802
4842
  const title = data[titleFieldName || " "] || null;
4803
4843
  return {
4804
4844
  __typename: collection.fields ? NAMER.documentTypeName(collection.namespace) : NAMER.documentTypeName(template.namespace),
@@ -4829,24 +4869,33 @@ var transformDocumentIntoPayload = async (fullPath, rawData, tinaSchema, config,
4829
4869
  throw e;
4830
4870
  }
4831
4871
  };
4832
- var updateObjectWithJsonPath = (obj, path7, newValue) => {
4872
+ var updateObjectWithJsonPath = (obj, path7, oldValue, newValue) => {
4873
+ let updated = false;
4833
4874
  if (!path7.includes(".") && !path7.includes("[")) {
4834
- if (path7 in obj) {
4875
+ if (path7 in obj && obj[path7] === oldValue) {
4835
4876
  obj[path7] = newValue;
4877
+ updated = true;
4836
4878
  }
4837
- return obj;
4838
- }
4839
- const parentPath = path7.replace(/\.[^.]+$/, "");
4840
- const keyToUpdate = path7.match(/[^.]+$/)[0];
4841
- const parents = (0, import_jsonpath_plus2.JSONPath)({ path: parentPath, json: obj, resultType: "value" });
4879
+ return { object: obj, updated };
4880
+ }
4881
+ const parentPath = path7.replace(/\.[^.\[\]]+$/, "");
4882
+ const keyToUpdate = path7.match(/[^.\[\]]+$/)[0];
4883
+ const parents = (0, import_jsonpath_plus2.JSONPath)({
4884
+ path: parentPath,
4885
+ json: obj,
4886
+ resultType: "value"
4887
+ });
4842
4888
  if (parents.length > 0) {
4843
4889
  parents.forEach((parent) => {
4844
4890
  if (parent && typeof parent === "object" && keyToUpdate in parent) {
4845
- parent[keyToUpdate] = newValue;
4891
+ if (parent[keyToUpdate] === oldValue) {
4892
+ parent[keyToUpdate] = newValue;
4893
+ updated = true;
4894
+ }
4846
4895
  }
4847
4896
  });
4848
4897
  }
4849
- return obj;
4898
+ return { object: obj, updated };
4850
4899
  };
4851
4900
  var Resolver = class {
4852
4901
  constructor(init) {
@@ -4863,7 +4912,9 @@ var Resolver = class {
4863
4912
  };
4864
4913
  this.getRaw = async (fullPath) => {
4865
4914
  if (typeof fullPath !== "string") {
4866
- throw new Error(`fullPath must be of type string for getDocument request`);
4915
+ throw new Error(
4916
+ `fullPath must be of type string for getDocument request`
4917
+ );
4867
4918
  }
4868
4919
  return this.database.get(fullPath);
4869
4920
  };
@@ -4892,10 +4943,12 @@ var Resolver = class {
4892
4943
  };
4893
4944
  this.getDocument = async (fullPath, opts = {}) => {
4894
4945
  if (typeof fullPath !== "string") {
4895
- throw new Error(`fullPath must be of type string for getDocument request`);
4946
+ throw new Error(
4947
+ `fullPath must be of type string for getDocument request`
4948
+ );
4896
4949
  }
4897
4950
  const rawData = await this.getRaw(fullPath);
4898
- const hasReferences = (opts == null ? void 0 : opts.checkReferences) ? await this.hasReferences(fullPath, opts.collection) : void 0;
4951
+ const hasReferences = opts?.checkReferences ? await this.hasReferences(fullPath, opts.collection) : void 0;
4899
4952
  return transformDocumentIntoPayload(
4900
4953
  fullPath,
4901
4954
  rawData,
@@ -4907,7 +4960,9 @@ var Resolver = class {
4907
4960
  };
4908
4961
  this.deleteDocument = async (fullPath) => {
4909
4962
  if (typeof fullPath !== "string") {
4910
- throw new Error(`fullPath must be of type string for getDocument request`);
4963
+ throw new Error(
4964
+ `fullPath must be of type string for getDocument request`
4965
+ );
4911
4966
  }
4912
4967
  await this.database.delete(fullPath);
4913
4968
  };
@@ -4933,9 +4988,9 @@ var Resolver = class {
4933
4988
  return this.buildFieldMutations(
4934
4989
  item,
4935
4990
  objectTemplate,
4936
- idField && existingData && (existingData == null ? void 0 : existingData.find(
4991
+ idField && existingData && existingData?.find(
4937
4992
  (d) => d[idField.name] === item[idField.name]
4938
- ))
4993
+ )
4939
4994
  );
4940
4995
  }
4941
4996
  )
@@ -5061,7 +5116,7 @@ var Resolver = class {
5061
5116
  isCollectionSpecific
5062
5117
  }) => {
5063
5118
  const doc = await this.getDocument(realPath);
5064
- const oldDoc = this.resolveLegacyValues((doc == null ? void 0 : doc._rawData) || {}, collection);
5119
+ const oldDoc = this.resolveLegacyValues(doc?._rawData || {}, collection);
5065
5120
  if (isAddPendingDocument === true) {
5066
5121
  const templateInfo = this.tinaSchema.getTemplatesForCollectable(collection);
5067
5122
  const params2 = this.buildParams(args);
@@ -5071,7 +5126,7 @@ var Resolver = class {
5071
5126
  const values = await this.buildFieldMutations(
5072
5127
  params2,
5073
5128
  templateInfo.template,
5074
- doc == null ? void 0 : doc._rawData
5129
+ doc?._rawData
5075
5130
  );
5076
5131
  await this.database.put(
5077
5132
  realPath,
@@ -5095,7 +5150,7 @@ var Resolver = class {
5095
5150
  // @ts-ignore FIXME: failing on unknown, which we don't need to know because it's recursive
5096
5151
  templateParams,
5097
5152
  template,
5098
- doc == null ? void 0 : doc._rawData
5153
+ doc?._rawData
5099
5154
  ),
5100
5155
  _template: lastItem(template.namespace)
5101
5156
  };
@@ -5109,9 +5164,13 @@ var Resolver = class {
5109
5164
  //@ts-ignore
5110
5165
  isCollectionSpecific ? args.params : args.params[collection.name],
5111
5166
  collection,
5112
- doc == null ? void 0 : doc._rawData
5167
+ doc?._rawData
5168
+ );
5169
+ await this.database.put(
5170
+ realPath,
5171
+ { ...oldDoc, ...params },
5172
+ collection.name
5113
5173
  );
5114
- await this.database.put(realPath, { ...oldDoc, ...params }, collection.name);
5115
5174
  return this.getDocument(realPath);
5116
5175
  };
5117
5176
  /**
@@ -5121,7 +5180,6 @@ var Resolver = class {
5121
5180
  this.resolveLegacyValues = (oldDoc, collection) => {
5122
5181
  const legacyValues = {};
5123
5182
  Object.entries(oldDoc).forEach(([key, value]) => {
5124
- var _a;
5125
5183
  const reservedKeys = [
5126
5184
  "$_body",
5127
5185
  "_collection",
@@ -5134,7 +5192,7 @@ var Resolver = class {
5134
5192
  return;
5135
5193
  }
5136
5194
  if (oldDoc._template && collection.templates) {
5137
- const template = (_a = collection.templates) == null ? void 0 : _a.find(
5195
+ const template = collection.templates?.find(
5138
5196
  ({ name }) => name === oldDoc._template
5139
5197
  );
5140
5198
  if (template) {
@@ -5181,7 +5239,7 @@ var Resolver = class {
5181
5239
  (yup3) => yup3.object({ relativePath: yup3.string().required() })
5182
5240
  );
5183
5241
  const collection = await this.tinaSchema.getCollection(collectionLookup);
5184
- let realPath = import_path3.default.join(collection == null ? void 0 : collection.path, args.relativePath);
5242
+ let realPath = import_path3.default.join(collection?.path, args.relativePath);
5185
5243
  if (isFolderCreation) {
5186
5244
  realPath = `${realPath}/.gitkeep.${collection.format || "md"}`;
5187
5245
  }
@@ -5225,17 +5283,35 @@ var Resolver = class {
5225
5283
  await this.deleteDocument(realPath);
5226
5284
  if (await this.hasReferences(realPath, collection)) {
5227
5285
  const collRefs = await this.findReferences(realPath, collection);
5228
- for (const [collection2, refFields] of Object.entries(collRefs)) {
5229
- for (const [refPath, refs] of Object.entries(refFields)) {
5230
- let refDoc = await this.getRaw(refPath);
5231
- for (const ref of refs) {
5232
- refDoc = updateObjectWithJsonPath(
5286
+ for (const [collection2, docsWithRefs] of Object.entries(collRefs)) {
5287
+ for (const [pathToDocWithRef, referencePaths] of Object.entries(
5288
+ docsWithRefs
5289
+ )) {
5290
+ let refDoc = await this.getRaw(pathToDocWithRef);
5291
+ let hasUpdate = false;
5292
+ for (const path7 of referencePaths) {
5293
+ const { object: object2, updated } = updateObjectWithJsonPath(
5233
5294
  refDoc,
5234
- ref.path.join("."),
5295
+ path7,
5296
+ realPath,
5235
5297
  null
5236
5298
  );
5299
+ refDoc = object2;
5300
+ hasUpdate = updated || hasUpdate;
5301
+ }
5302
+ if (hasUpdate) {
5303
+ const collectionWithRef = this.tinaSchema.getCollectionByFullPath(pathToDocWithRef);
5304
+ if (!collectionWithRef) {
5305
+ throw new Error(
5306
+ `Unable to find collection for ${pathToDocWithRef}`
5307
+ );
5308
+ }
5309
+ await this.database.put(
5310
+ pathToDocWithRef,
5311
+ refDoc,
5312
+ collectionWithRef.name
5313
+ );
5237
5314
  }
5238
- await this.database.put(refPath, refDoc, collection2);
5239
5315
  }
5240
5316
  }
5241
5317
  }
@@ -5247,34 +5323,57 @@ var Resolver = class {
5247
5323
  (yup3) => yup3.object({ params: yup3.object().required() })
5248
5324
  );
5249
5325
  assertShape(
5250
- args == null ? void 0 : args.params,
5326
+ args?.params,
5251
5327
  (yup3) => yup3.object({ relativePath: yup3.string().required() })
5252
5328
  );
5253
5329
  const doc = await this.getDocument(realPath);
5254
5330
  const newRealPath = import_path3.default.join(
5255
- collection == null ? void 0 : collection.path,
5331
+ collection?.path,
5256
5332
  args.params.relativePath
5257
5333
  );
5334
+ if (newRealPath === realPath) {
5335
+ return doc;
5336
+ }
5258
5337
  await this.database.put(newRealPath, doc._rawData, collection.name);
5259
5338
  await this.deleteDocument(realPath);
5260
5339
  const collRefs = await this.findReferences(realPath, collection);
5261
- for (const [collection2, refFields] of Object.entries(collRefs)) {
5262
- for (const [refPath, refs] of Object.entries(refFields)) {
5263
- let refDoc = await this.getRaw(refPath);
5264
- for (const ref of refs) {
5265
- refDoc = updateObjectWithJsonPath(
5266
- refDoc,
5267
- ref.path.join("."),
5340
+ for (const [collection2, docsWithRefs] of Object.entries(collRefs)) {
5341
+ for (const [pathToDocWithRef, referencePaths] of Object.entries(
5342
+ docsWithRefs
5343
+ )) {
5344
+ let docWithRef = await this.getRaw(pathToDocWithRef);
5345
+ let hasUpdate = false;
5346
+ for (const path7 of referencePaths) {
5347
+ const { object: object2, updated } = updateObjectWithJsonPath(
5348
+ docWithRef,
5349
+ path7,
5350
+ realPath,
5268
5351
  newRealPath
5269
5352
  );
5353
+ docWithRef = object2;
5354
+ hasUpdate = updated || hasUpdate;
5355
+ }
5356
+ if (hasUpdate) {
5357
+ const collectionWithRef = this.tinaSchema.getCollectionByFullPath(pathToDocWithRef);
5358
+ if (!collectionWithRef) {
5359
+ throw new Error(
5360
+ `Unable to find collection for ${pathToDocWithRef}`
5361
+ );
5362
+ }
5363
+ await this.database.put(
5364
+ pathToDocWithRef,
5365
+ docWithRef,
5366
+ collectionWithRef.name
5367
+ );
5270
5368
  }
5271
- await this.database.put(refPath, refDoc, collection2);
5272
5369
  }
5273
5370
  }
5274
5371
  return this.getDocument(newRealPath);
5275
5372
  }
5276
5373
  if (alreadyExists === false) {
5277
- throw new Error(`Unable to update document, ${realPath} does not exist`);
5374
+ throw new Error(
5375
+ `Unable to update document, ${realPath} does not exist`
5376
+ );
5278
5377
  }
5279
5378
  return this.updateResolveDocument({
5280
5379
  collection,
@@ -5400,35 +5499,30 @@ var Resolver = class {
5400
5499
  */
5401
5500
  this.hasReferences = async (id, c) => {
5402
5501
  let count = 0;
5403
- const deepRefs = this.tinaSchema.findReferences(c.name);
5404
- for (const [collection, refs] of Object.entries(deepRefs)) {
5405
- for (const ref of refs) {
5406
- await this.database.query(
5407
- {
5408
- collection,
5409
- filterChain: makeFilterChain({
5410
- conditions: [
5411
- {
5412
- filterPath: ref.path.join("."),
5413
- filterExpression: {
5414
- _type: "reference",
5415
- _list: false,
5416
- eq: id
5417
- }
5418
- }
5419
- ]
5420
- }),
5421
- sort: ref.field.name
5422
- },
5423
- (refId) => {
5424
- count++;
5425
- return refId;
5426
- }
5427
- );
5428
- if (count) {
5429
- return true;
5430
- }
5502
+ await this.database.query(
5503
+ {
5504
+ collection: c.name,
5505
+ filterChain: makeFilterChain({
5506
+ conditions: [
5507
+ {
5508
+ filterPath: REFS_REFERENCE_FIELD,
5509
+ filterExpression: {
5510
+ _type: "string",
5511
+ _list: false,
5512
+ eq: id
5513
+ }
5514
+ }
5515
+ ]
5516
+ }),
5517
+ sort: REFS_COLLECTIONS_SORT_KEY
5518
+ },
5519
+ (refId) => {
5520
+ count++;
5521
+ return refId;
5431
5522
  }
5523
+ );
5524
+ if (count) {
5525
+ return true;
5432
5526
  }
5433
5527
  return false;
5434
5528
  };
@@ -5436,50 +5530,44 @@ var Resolver = class {
5436
5530
  * Finds references to a document
5437
5531
  * @param id the id of the document to find references to
5438
5532
  * @param c the collection to find references in
5439
- * @returns references to the document in the form of a map of collection names to a list of fields that reference the document
5533
+ * @returns a map of references to the document
5440
5534
  */
5441
5535
  this.findReferences = async (id, c) => {
5442
5536
  const references = {};
5443
- const deepRefs = this.tinaSchema.findReferences(c.name);
5444
- for (const [collection, refs] of Object.entries(deepRefs)) {
5445
- for (const ref of refs) {
5446
- await this.database.query(
5447
- {
5448
- collection,
5449
- filterChain: makeFilterChain({
5450
- conditions: [
5451
- {
5452
- filterPath: ref.path.join("."),
5453
- filterExpression: {
5454
- _type: "reference",
5455
- _list: false,
5456
- eq: id
5457
- }
5458
- }
5459
- ]
5460
- }),
5461
- sort: ref.field.name
5462
- },
5463
- (refId) => {
5464
- if (!references[collection]) {
5465
- references[collection] = {};
5466
- }
5467
- if (!references[collection][refId]) {
5468
- references[collection][refId] = [];
5537
+ await this.database.query(
5538
+ {
5539
+ collection: c.name,
5540
+ filterChain: makeFilterChain({
5541
+ conditions: [
5542
+ {
5543
+ filterPath: REFS_REFERENCE_FIELD,
5544
+ filterExpression: {
5545
+ _type: "string",
5546
+ _list: false,
5547
+ eq: id
5548
+ }
5469
5549
  }
5470
- references[collection][refId].push({
5471
- path: ref.path,
5472
- field: ref.field
5473
- });
5474
- return refId;
5475
- }
5476
- );
5550
+ ]
5551
+ }),
5552
+ sort: REFS_COLLECTIONS_SORT_KEY
5553
+ },
5554
+ (refId, rawItem) => {
5555
+ if (!references[c.name]) {
5556
+ references[c.name] = {};
5557
+ }
5558
+ if (!references[c.name][refId]) {
5559
+ references[c.name][refId] = [];
5560
+ }
5561
+ const referencePath = rawItem?.[REFS_PATH_FIELD];
5562
+ if (referencePath) {
5563
+ references[c.name][refId].push(referencePath);
5564
+ }
5565
+ return refId;
5477
5566
  }
5478
- }
5567
+ );
5479
5568
  return references;
5480
5569
  };
5481
5570
  this.buildFieldMutations = async (fieldParams, template, existingData) => {
5482
- var _a;
5483
5571
  const accum = {};
5484
5572
  for (const passwordField of template.fields.filter(
5485
5573
  (f) => f.type === "password"
@@ -5522,7 +5610,7 @@ var Resolver = class {
5522
5610
  accum[fieldName] = await this.buildObjectMutations(
5523
5611
  fieldValue,
5524
5612
  field,
5525
- existingData == null ? void 0 : existingData[fieldName]
5613
+ existingData?.[fieldName]
5526
5614
  );
5527
5615
  break;
5528
5616
  case "password":
@@ -5541,12 +5629,12 @@ var Resolver = class {
5541
5629
  } else {
5542
5630
  accum[fieldName] = {
5543
5631
  ...fieldValue,
5544
- value: (_a = existingData == null ? void 0 : existingData[fieldName]) == null ? void 0 : _a["value"]
5632
+ value: existingData?.[fieldName]?.["value"]
5545
5633
  };
5546
5634
  }
5547
5635
  break;
5548
5636
  case "rich-text":
5549
- accum[fieldName] = (0, import_mdx.stringifyMDX)(
5637
+ accum[fieldName] = (0, import_mdx.serializeMDX)(
5550
5638
  fieldValue,
5551
5639
  field,
5552
5640
  (fieldValue2) => resolveMediaCloudToRelative(
@@ -5676,9 +5764,8 @@ var resolve = async ({
5676
5764
  isAudit,
5677
5765
  ctxUser
5678
5766
  }) => {
5679
- var _a;
5680
5767
  try {
5681
- const verboseValue = verbose != null ? verbose : true;
5768
+ const verboseValue = verbose ?? true;
5682
5769
  const graphQLSchemaAst = await database.getGraphQLSchema();
5683
5770
  if (!graphQLSchemaAst) {
5684
5771
  throw new import_graphql5.GraphQLError("GraphQL schema not found");
@@ -5690,7 +5777,7 @@ var resolve = async ({
5690
5777
  // @ts-ignore
5691
5778
  schema: tinaConfig,
5692
5779
  // @ts-ignore
5693
- flags: (_a = tinaConfig == null ? void 0 : tinaConfig.meta) == null ? void 0 : _a.flags
5780
+ flags: tinaConfig?.meta?.flags
5694
5781
  });
5695
5782
  const resolver = createResolver({
5696
5783
  config,
@@ -5715,7 +5802,6 @@ var resolve = async ({
5715
5802
  throw new Error(`Unable to find lookup key for ${namedType}`);
5716
5803
  },
5717
5804
  fieldResolver: async (source = {}, _args = {}, _context, info) => {
5718
- var _a2, _b, _c, _d;
5719
5805
  try {
5720
5806
  const args = JSON.parse(JSON.stringify(_args));
5721
5807
  const returnType = (0, import_graphql5.getNamedType)(info.returnType).toString();
@@ -5732,8 +5818,7 @@ var resolve = async ({
5732
5818
  );
5733
5819
  const hasDocuments2 = collectionNode2.selectionSet.selections.find(
5734
5820
  (x) => {
5735
- var _a3;
5736
- return ((_a3 = x == null ? void 0 : x.name) == null ? void 0 : _a3.value) === "documents";
5821
+ return x?.name?.value === "documents";
5737
5822
  }
5738
5823
  );
5739
5824
  return tinaSchema.getCollections().map((collection) => {
@@ -5749,8 +5834,7 @@ var resolve = async ({
5749
5834
  );
5750
5835
  const hasDocuments = collectionNode.selectionSet.selections.find(
5751
5836
  (x) => {
5752
- var _a3;
5753
- return ((_a3 = x == null ? void 0 : x.name) == null ? void 0 : _a3.value) === "documents";
5837
+ return x?.name?.value === "documents";
5754
5838
  }
5755
5839
  );
5756
5840
  return resolver.resolveCollection(
@@ -5769,7 +5853,7 @@ var resolve = async ({
5769
5853
  }
5770
5854
  }
5771
5855
  if (info.fieldName === "authenticate" || info.fieldName === "authorize") {
5772
- const sub = args.sub || (ctxUser == null ? void 0 : ctxUser.sub);
5856
+ const sub = args.sub || ctxUser?.sub;
5773
5857
  const collection = tinaSchema.getCollections().find((c) => c.isAuthCollection);
5774
5858
  if (!collection) {
5775
5859
  throw new Error("Auth collection not found");
@@ -5817,7 +5901,7 @@ var resolve = async ({
5817
5901
  return user;
5818
5902
  }
5819
5903
  if (info.fieldName === "updatePassword") {
5820
- if (!(ctxUser == null ? void 0 : ctxUser.sub)) {
5904
+ if (!ctxUser?.sub) {
5821
5905
  throw new Error("Not authorized");
5822
5906
  }
5823
5907
  if (!args.password) {
@@ -5900,7 +5984,7 @@ var resolve = async ({
5900
5984
  if (typeof value === "string" && value !== "") {
5901
5985
  return resolver.getDocument(value);
5902
5986
  }
5903
- if ((args == null ? void 0 : args.collection) && info.fieldName === "addPendingDocument") {
5987
+ if (args?.collection && info.fieldName === "addPendingDocument") {
5904
5988
  return resolver.resolveDocument({
5905
5989
  args: { ...args, params: {} },
5906
5990
  collection: args.collection,
@@ -5924,7 +6008,7 @@ var resolve = async ({
5924
6008
  // Right now this is the only case for deletion
5925
6009
  isDeletion: info.fieldName === "deleteDocument",
5926
6010
  isFolderCreation: info.fieldName === "createFolder",
5927
- isUpdateName: Boolean((_a2 = args == null ? void 0 : args.params) == null ? void 0 : _a2.relativePath),
6011
+ isUpdateName: Boolean(args?.params?.relativePath),
5928
6012
  isAddPendingDocument: false,
5929
6013
  isCollectionSpecific: false
5930
6014
  });
@@ -5943,16 +6027,16 @@ var resolve = async ({
5943
6027
  })
5944
6028
  };
5945
6029
  }
5946
- if (info.fieldName === "documents" && (value == null ? void 0 : value.collection) && (value == null ? void 0 : value.hasDocuments)) {
6030
+ if (info.fieldName === "documents" && value?.collection && value?.hasDocuments) {
5947
6031
  let filter = args.filter;
5948
6032
  if (
5949
6033
  // 1. Make sure that the filter exists
5950
- typeof (args == null ? void 0 : args.filter) !== "undefined" && (args == null ? void 0 : args.filter) !== null && // 2. Make sure that the collection name exists
6034
+ typeof args?.filter !== "undefined" && args?.filter !== null && // 2. Make sure that the collection name exists
5951
6035
  // @ts-ignore
5952
- typeof ((_b = value == null ? void 0 : value.collection) == null ? void 0 : _b.name) === "string" && // 3. Make sure that the collection name is in the filter and is not undefined
6036
+ typeof value?.collection?.name === "string" && // 3. Make sure that the collection name is in the filter and is not undefined
5953
6037
  // @ts-ignore
5954
- Object.keys(args.filter).includes((_c = value == null ? void 0 : value.collection) == null ? void 0 : _c.name) && // @ts-ignore
5955
- typeof args.filter[(_d = value == null ? void 0 : value.collection) == null ? void 0 : _d.name] !== "undefined"
6038
+ Object.keys(args.filter).includes(value?.collection?.name) && // @ts-ignore
6039
+ typeof args.filter[value?.collection?.name] !== "undefined"
5956
6040
  ) {
5957
6041
  filter = args.filter[value.collection.name];
5958
6042
  }
@@ -6089,15 +6173,15 @@ var TinaLevelClient = class extends import_many_level.ManyLevelGuest {
6089
6173
  };
6090
6174
 
6091
6175
  // src/database/index.ts
6092
- var import_node_path = __toESM(require("path"));
6176
+ var import_node_path = __toESM(require("node:path"));
6093
6177
  var import_graphql6 = require("graphql");
6094
6178
  var import_micromatch2 = __toESM(require("micromatch"));
6095
6179
  var import_js_sha12 = __toESM(require("js-sha1"));
6096
6180
  var import_lodash5 = __toESM(require("lodash.set"));
6097
6181
  var createLocalDatabase = (config) => {
6098
- const level = new TinaLevelClient(config == null ? void 0 : config.port);
6182
+ const level = new TinaLevelClient(config?.port);
6099
6183
  level.openConnection();
6100
- const fsBridge = new FilesystemBridge((config == null ? void 0 : config.rootPath) || process.cwd());
6184
+ const fsBridge = new FilesystemBridge(config?.rootPath || process.cwd());
6101
6185
  return new Database({
6102
6186
  bridge: fsBridge,
6103
6187
  ...config || {},
@@ -6170,7 +6254,7 @@ var Database = class {
6170
6254
  );
6171
6255
  }
6172
6256
  const metadata = await metadataLevel.get(`metadata_${key}`);
6173
- return metadata == null ? void 0 : metadata.value;
6257
+ return metadata?.value;
6174
6258
  };
6175
6259
  this.setMetadata = async (key, value) => {
6176
6260
  await this.initLevel();
@@ -6192,7 +6276,7 @@ var Database = class {
6192
6276
  let level = this.contentLevel;
6193
6277
  if (this.appLevel) {
6194
6278
  collection = await this.collectionForPath(filepath);
6195
- if (collection == null ? void 0 : collection.isDetached) {
6279
+ if (collection?.isDetached) {
6196
6280
  level = this.appLevel.sublevel(collection.name, SUBLEVEL_OPTIONS);
6197
6281
  }
6198
6282
  }
@@ -6223,9 +6307,10 @@ var Database = class {
6223
6307
  collection
6224
6308
  );
6225
6309
  const indexDefinitions = await this.getIndexDefinitions(this.contentLevel);
6226
- const collectionIndexDefinitions = indexDefinitions == null ? void 0 : indexDefinitions[collection.name];
6310
+ const collectionIndexDefinitions = indexDefinitions?.[collection.name];
6311
+ const collectionReferences = (await this.getCollectionReferences())?.[collection.name];
6227
6312
  const normalizedPath = (0, import_schema_tools4.normalizePath)(filepath);
6228
- if (!(collection == null ? void 0 : collection.isDetached)) {
6313
+ if (!collection?.isDetached) {
6229
6314
  if (this.bridge) {
6230
6315
  await this.bridge.put(normalizedPath, stringifiedFile);
6231
6316
  }
@@ -6243,7 +6328,7 @@ var Database = class {
6243
6328
  }
6244
6329
  }
6245
6330
  let level = this.contentLevel;
6246
- if (collection == null ? void 0 : collection.isDetached) {
6331
+ if (collection?.isDetached) {
6247
6332
  level = this.appLevel.sublevel(collection.name, SUBLEVEL_OPTIONS);
6248
6333
  }
6249
6334
  const folderTreeBuilder = new FolderTreeBuilder();
@@ -6252,9 +6337,17 @@ var Database = class {
6252
6337
  let delOps = [];
6253
6338
  if (!isGitKeep(normalizedPath, collection)) {
6254
6339
  putOps = [
6340
+ ...makeRefOpsForDocument(
6341
+ normalizedPath,
6342
+ collection?.name,
6343
+ collectionReferences,
6344
+ dataFields,
6345
+ "put",
6346
+ level
6347
+ ),
6255
6348
  ...makeIndexOpsForDocument(
6256
6349
  normalizedPath,
6257
- collection == null ? void 0 : collection.name,
6350
+ collection?.name,
6258
6351
  collectionIndexDefinitions,
6259
6352
  dataFields,
6260
6353
  "put",
@@ -6263,7 +6356,7 @@ var Database = class {
6263
6356
  // folder indices
6264
6357
  ...makeIndexOpsForDocument(
6265
6358
  normalizedPath,
6266
- `${collection == null ? void 0 : collection.name}_${folderKey}`,
6359
+ `${collection?.name}_${folderKey}`,
6267
6360
  collectionIndexDefinitions,
6268
6361
  dataFields,
6269
6362
  "put",
@@ -6275,9 +6368,17 @@ var Database = class {
6275
6368
  SUBLEVEL_OPTIONS
6276
6369
  ).get(normalizedPath);
6277
6370
  delOps = existingItem ? [
6371
+ ...makeRefOpsForDocument(
6372
+ normalizedPath,
6373
+ collection?.name,
6374
+ collectionReferences,
6375
+ existingItem,
6376
+ "del",
6377
+ level
6378
+ ),
6278
6379
  ...makeIndexOpsForDocument(
6279
6380
  normalizedPath,
6280
- collection == null ? void 0 : collection.name,
6381
+ collection?.name,
6281
6382
  collectionIndexDefinitions,
6282
6383
  existingItem,
6283
6384
  "del",
@@ -6286,7 +6387,7 @@ var Database = class {
6286
6387
  // folder indices
6287
6388
  ...makeIndexOpsForDocument(
6288
6389
  normalizedPath,
6289
- `${collection == null ? void 0 : collection.name}_${folderKey}`,
6390
+ `${collection?.name}_${folderKey}`,
6290
6391
  collectionIndexDefinitions,
6291
6392
  existingItem,
6292
6393
  "del",
@@ -6310,7 +6411,6 @@ var Database = class {
6310
6411
  await level.batch(ops);
6311
6412
  };
6312
6413
  this.put = async (filepath, data, collectionName) => {
6313
- var _a, _b;
6314
6414
  await this.initLevel();
6315
6415
  try {
6316
6416
  if (SYSTEM_FILES.includes(filepath)) {
@@ -6321,15 +6421,16 @@ var Database = class {
6321
6421
  const indexDefinitions = await this.getIndexDefinitions(
6322
6422
  this.contentLevel
6323
6423
  );
6324
- collectionIndexDefinitions = indexDefinitions == null ? void 0 : indexDefinitions[collectionName];
6424
+ collectionIndexDefinitions = indexDefinitions?.[collectionName];
6325
6425
  }
6426
+ const collectionReferences = (await this.getCollectionReferences())?.[collectionName];
6326
6427
  const normalizedPath = (0, import_schema_tools4.normalizePath)(filepath);
6327
6428
  const dataFields = await this.formatBodyOnPayload(filepath, data);
6328
6429
  const collection = await this.collectionForPath(filepath);
6329
6430
  if (!collection) {
6330
6431
  throw new import_graphql6.GraphQLError(`Unable to find collection for ${filepath}.`);
6331
6432
  }
6332
- if (((_a = collection.match) == null ? void 0 : _a.exclude) || ((_b = collection.match) == null ? void 0 : _b.include)) {
6433
+ if (collection.match?.exclude || collection.match?.include) {
6333
6434
  const matches = this.tinaSchema.getMatches({ collection });
6334
6435
  const match = import_micromatch2.default.isMatch(filepath, matches);
6335
6436
  if (!match) {
@@ -6343,7 +6444,7 @@ var Database = class {
6343
6444
  const stringifiedFile = filepath.endsWith(
6344
6445
  `.gitkeep.${collection.format || "md"}`
6345
6446
  ) ? "" : await this.stringifyFile(filepath, dataFields, collection);
6346
- if (!(collection == null ? void 0 : collection.isDetached)) {
6447
+ if (!collection?.isDetached) {
6347
6448
  if (this.bridge) {
6348
6449
  await this.bridge.put(normalizedPath, stringifiedFile);
6349
6450
  }
@@ -6365,11 +6466,19 @@ var Database = class {
6365
6466
  filepath,
6366
6467
  collection.path || ""
6367
6468
  );
6368
- const level = (collection == null ? void 0 : collection.isDetached) ? this.appLevel.sublevel(collection == null ? void 0 : collection.name, SUBLEVEL_OPTIONS) : this.contentLevel;
6469
+ const level = collection?.isDetached ? this.appLevel.sublevel(collection?.name, SUBLEVEL_OPTIONS) : this.contentLevel;
6369
6470
  let putOps = [];
6370
6471
  let delOps = [];
6371
6472
  if (!isGitKeep(normalizedPath, collection)) {
6372
6473
  putOps = [
6474
+ ...makeRefOpsForDocument(
6475
+ normalizedPath,
6476
+ collectionName,
6477
+ collectionReferences,
6478
+ dataFields,
6479
+ "put",
6480
+ level
6481
+ ),
6373
6482
  ...makeIndexOpsForDocument(
6374
6483
  normalizedPath,
6375
6484
  collectionName,
@@ -6381,7 +6490,7 @@ var Database = class {
6381
6490
  // folder indices
6382
6491
  ...makeIndexOpsForDocument(
6383
6492
  normalizedPath,
6384
- `${collection == null ? void 0 : collection.name}_${folderKey}`,
6493
+ `${collection?.name}_${folderKey}`,
6385
6494
  collectionIndexDefinitions,
6386
6495
  dataFields,
6387
6496
  "put",
@@ -6393,6 +6502,14 @@ var Database = class {
6393
6502
  SUBLEVEL_OPTIONS
6394
6503
  ).get(normalizedPath);
6395
6504
  delOps = existingItem ? [
6505
+ ...makeRefOpsForDocument(
6506
+ normalizedPath,
6507
+ collectionName,
6508
+ collectionReferences,
6509
+ existingItem,
6510
+ "del",
6511
+ level
6512
+ ),
6396
6513
  ...makeIndexOpsForDocument(
6397
6514
  normalizedPath,
6398
6515
  collectionName,
@@ -6404,7 +6521,7 @@ var Database = class {
6404
6521
  // folder indices
6405
6522
  ...makeIndexOpsForDocument(
6406
6523
  normalizedPath,
6407
- `${collection == null ? void 0 : collection.name}_${folderKey}`,
6524
+ `${collection?.name}_${folderKey}`,
6408
6525
  collectionIndexDefinitions,
6409
6526
  existingItem,
6410
6527
  "del",
@@ -6481,8 +6598,8 @@ var Database = class {
6481
6598
  writeTemplateKey,
6482
6599
  //templateInfo.type === 'union',
6483
6600
  {
6484
- frontmatterFormat: collection == null ? void 0 : collection.frontmatterFormat,
6485
- frontmatterDelimiters: collection == null ? void 0 : collection.frontmatterDelimiters
6601
+ frontmatterFormat: collection?.frontmatterFormat,
6602
+ frontmatterDelimiters: collection?.frontmatterDelimiters
6486
6603
  }
6487
6604
  );
6488
6605
  };
@@ -6555,6 +6672,22 @@ var Database = class {
6555
6672
  this.tinaSchema = await createSchema({ schema });
6556
6673
  return this.tinaSchema;
6557
6674
  };
6675
+ this.getCollectionReferences = async (level) => {
6676
+ if (this.collectionReferences) {
6677
+ return this.collectionReferences;
6678
+ }
6679
+ const result = {};
6680
+ const schema = await this.getSchema(level || this.contentLevel);
6681
+ const collections = schema.getCollections();
6682
+ for (const collection of collections) {
6683
+ const collectionReferences = this.tinaSchema.findReferencesFromCollection(
6684
+ collection.name
6685
+ );
6686
+ result[collection.name] = collectionReferences;
6687
+ }
6688
+ this.collectionReferences = result;
6689
+ return result;
6690
+ };
6558
6691
  this.getIndexDefinitions = async (level) => {
6559
6692
  if (!this.collectionIndexDefinitions) {
6560
6693
  await new Promise(async (resolve2, reject) => {
@@ -6564,11 +6697,53 @@ var Database = class {
6564
6697
  const collections = schema.getCollections();
6565
6698
  for (const collection of collections) {
6566
6699
  const indexDefinitions = {
6567
- [DEFAULT_COLLECTION_SORT_KEY]: { fields: [] }
6700
+ [DEFAULT_COLLECTION_SORT_KEY]: { fields: [] },
6568
6701
  // provide a default sort key which is the file sort
6702
+ // pseudo-index for the collection's references
6703
+ [REFS_COLLECTIONS_SORT_KEY]: {
6704
+ fields: [
6705
+ {
6706
+ name: REFS_REFERENCE_FIELD,
6707
+ type: "string",
6708
+ list: false
6709
+ },
6710
+ {
6711
+ name: REFS_PATH_FIELD,
6712
+ type: "string",
6713
+ list: false
6714
+ }
6715
+ ]
6716
+ }
6569
6717
  };
6570
- if (collection.fields) {
6571
- for (const field of collection.fields) {
6718
+ let fields = [];
6719
+ if (collection.templates) {
6720
+ const templateFieldMap = {};
6721
+ const conflictedFields = /* @__PURE__ */ new Set();
6722
+ for (const template of collection.templates) {
6723
+ for (const field of template.fields) {
6724
+ if (!templateFieldMap[field.name]) {
6725
+ templateFieldMap[field.name] = field;
6726
+ } else {
6727
+ if (templateFieldMap[field.name].type !== field.type) {
6728
+ console.warn(
6729
+ `Field ${field.name} has conflicting types in templates - skipping index`
6730
+ );
6731
+ conflictedFields.add(field.name);
6732
+ }
6733
+ }
6734
+ }
6735
+ }
6736
+ for (const conflictedField in conflictedFields) {
6737
+ delete templateFieldMap[conflictedField];
6738
+ }
6739
+ for (const field of Object.values(templateFieldMap)) {
6740
+ fields.push(field);
6741
+ }
6742
+ } else if (collection.fields) {
6743
+ fields = collection.fields;
6744
+ }
6745
+ if (fields) {
6746
+ for (const field of fields) {
6572
6747
  if (field.indexed !== void 0 && field.indexed === false || field.type === "object") {
6573
6748
  continue;
6574
6749
  }
@@ -6593,8 +6768,8 @@ var Database = class {
6593
6768
  );
6594
6769
  return {
6595
6770
  name: indexField.name,
6596
- type: field == null ? void 0 : field.type,
6597
- list: !!(field == null ? void 0 : field.list)
6771
+ type: field?.type,
6772
+ list: !!field?.list
6598
6773
  };
6599
6774
  })
6600
6775
  };
@@ -6620,7 +6795,6 @@ var Database = class {
6620
6795
  return true;
6621
6796
  };
6622
6797
  this.query = async (queryOptions, hydrator) => {
6623
- var _a;
6624
6798
  await this.initLevel();
6625
6799
  const {
6626
6800
  first,
@@ -6648,14 +6822,14 @@ var Database = class {
6648
6822
  const allIndexDefinitions = await this.getIndexDefinitions(
6649
6823
  this.contentLevel
6650
6824
  );
6651
- const indexDefinitions = allIndexDefinitions == null ? void 0 : allIndexDefinitions[collection.name];
6825
+ const indexDefinitions = allIndexDefinitions?.[collection.name];
6652
6826
  if (!indexDefinitions) {
6653
6827
  throw new Error(`No indexDefinitions for collection ${collection.name}`);
6654
6828
  }
6655
6829
  const filterChain = coerceFilterChainOperands(rawFilterChain);
6656
- const indexDefinition = sort && (indexDefinitions == null ? void 0 : indexDefinitions[sort]);
6830
+ const indexDefinition = sort && indexDefinitions?.[sort];
6657
6831
  const filterSuffixes = indexDefinition && makeFilterSuffixes(filterChain, indexDefinition);
6658
- const level = (collection == null ? void 0 : collection.isDetached) ? this.appLevel.sublevel(collection == null ? void 0 : collection.name, SUBLEVEL_OPTIONS) : this.contentLevel;
6832
+ const level = collection?.isDetached ? this.appLevel.sublevel(collection?.name, SUBLEVEL_OPTIONS) : this.contentLevel;
6659
6833
  const rootLevel = level.sublevel(
6660
6834
  CONTENT_ROOT_PREFIX,
6661
6835
  SUBLEVEL_OPTIONS
@@ -6665,17 +6839,17 @@ var Database = class {
6665
6839
  SUBLEVEL_OPTIONS
6666
6840
  ).sublevel(sort, SUBLEVEL_OPTIONS) : rootLevel;
6667
6841
  if (!query.gt && !query.gte) {
6668
- query.gte = (filterSuffixes == null ? void 0 : filterSuffixes.left) ? filterSuffixes.left : "";
6842
+ query.gte = filterSuffixes?.left ? filterSuffixes.left : "";
6669
6843
  }
6670
6844
  if (!query.lt && !query.lte) {
6671
- query.lte = (filterSuffixes == null ? void 0 : filterSuffixes.right) ? `${filterSuffixes.right}\uFFFF` : "\uFFFF";
6845
+ query.lte = filterSuffixes?.right ? `${filterSuffixes.right}\uFFFF` : "\uFFFF";
6672
6846
  }
6673
6847
  let edges = [];
6674
6848
  let startKey = "";
6675
6849
  let endKey = "";
6676
6850
  let hasPreviousPage = false;
6677
6851
  let hasNextPage = false;
6678
- const fieldsPattern = ((_a = indexDefinition == null ? void 0 : indexDefinition.fields) == null ? void 0 : _a.length) ? `${indexDefinition.fields.map((p) => `(?<${p.name}>.+)${INDEX_KEY_FIELD_SEPARATOR}`).join("")}` : "";
6852
+ const fieldsPattern = indexDefinition?.fields?.length ? `${indexDefinition.fields.map((p) => `(?<${p.name}>.+)${INDEX_KEY_FIELD_SEPARATOR}`).join("")}` : "";
6679
6853
  const valuesRegex = indexDefinition ? new RegExp(`^${fieldsPattern}(?<_filepath_>.+)`) : new RegExp(`^(?<_filepath_>.+)`);
6680
6854
  const itemFilter = makeFilter({ filterChain });
6681
6855
  const iterator = sublevel.iterator(query);
@@ -6717,29 +6891,36 @@ var Database = class {
6717
6891
  }
6718
6892
  startKey = startKey || key || "";
6719
6893
  endKey = key || "";
6720
- edges = [...edges, { cursor: key, path: filepath }];
6894
+ edges = [...edges, { cursor: key, path: filepath, value: itemRecord }];
6721
6895
  }
6722
6896
  return {
6723
- edges: await sequential(edges, async (edge) => {
6724
- try {
6725
- const node = await hydrator(edge.path);
6726
- return {
6727
- node,
6728
- cursor: btoa(edge.cursor)
6729
- };
6730
- } catch (error) {
6731
- console.log(error);
6732
- if (error instanceof Error && (!edge.path.includes(".tina/__generated__/_graphql.json") || !edge.path.includes("tina/__generated__/_graphql.json"))) {
6733
- throw new TinaQueryError({
6734
- originalError: error,
6735
- file: edge.path,
6736
- collection: collection.name,
6737
- stack: error.stack
6738
- });
6897
+ edges: await sequential(
6898
+ edges,
6899
+ async ({
6900
+ cursor,
6901
+ path: path7,
6902
+ value
6903
+ }) => {
6904
+ try {
6905
+ const node = await hydrator(path7, value);
6906
+ return {
6907
+ node,
6908
+ cursor: btoa(cursor)
6909
+ };
6910
+ } catch (error) {
6911
+ console.log(error);
6912
+ if (error instanceof Error && (!path7.includes(".tina/__generated__/_graphql.json") || !path7.includes("tina/__generated__/_graphql.json"))) {
6913
+ throw new TinaQueryError({
6914
+ originalError: error,
6915
+ file: path7,
6916
+ collection: collection.name,
6917
+ stack: error.stack
6918
+ });
6919
+ }
6920
+ throw error;
6739
6921
  }
6740
- throw error;
6741
6922
  }
6742
- }),
6923
+ ),
6743
6924
  pageInfo: {
6744
6925
  hasPreviousPage,
6745
6926
  hasNextPage,
@@ -6863,13 +7044,14 @@ var Database = class {
6863
7044
  documentPaths,
6864
7045
  async (collection, documentPaths2) => {
6865
7046
  if (collection && !collection.isDetached) {
6866
- await _indexContent(
6867
- this,
6868
- this.contentLevel,
6869
- documentPaths2,
7047
+ await _indexContent({
7048
+ database: this,
7049
+ level: this.contentLevel,
7050
+ documentPaths: documentPaths2,
6870
7051
  enqueueOps,
6871
- collection
6872
- );
7052
+ collection,
7053
+ isPartialReindex: true
7054
+ });
6873
7055
  }
6874
7056
  }
6875
7057
  );
@@ -6885,10 +7067,11 @@ var Database = class {
6885
7067
  throw new Error(`No collection found for path: ${filepath}`);
6886
7068
  }
6887
7069
  const indexDefinitions = await this.getIndexDefinitions(this.contentLevel);
6888
- const collectionIndexDefinitions = indexDefinitions == null ? void 0 : indexDefinitions[collection.name];
7070
+ const collectionReferences = (await this.getCollectionReferences())?.[collection.name];
7071
+ const collectionIndexDefinitions = indexDefinitions?.[collection.name];
6889
7072
  let level = this.contentLevel;
6890
- if (collection == null ? void 0 : collection.isDetached) {
6891
- level = this.appLevel.sublevel(collection == null ? void 0 : collection.name, SUBLEVEL_OPTIONS);
7073
+ if (collection?.isDetached) {
7074
+ level = this.appLevel.sublevel(collection?.name, SUBLEVEL_OPTIONS);
6892
7075
  }
6893
7076
  const normalizedPath = (0, import_schema_tools4.normalizePath)(filepath);
6894
7077
  const rootSublevel = level.sublevel(
@@ -6903,6 +7086,14 @@ var Database = class {
6903
7086
  collection.path || ""
6904
7087
  );
6905
7088
  await this.contentLevel.batch([
7089
+ ...makeRefOpsForDocument(
7090
+ normalizedPath,
7091
+ collection.name,
7092
+ collectionReferences,
7093
+ item,
7094
+ "del",
7095
+ level
7096
+ ),
6906
7097
  ...makeIndexOpsForDocument(
6907
7098
  normalizedPath,
6908
7099
  collection.name,
@@ -6927,7 +7118,7 @@ var Database = class {
6927
7118
  }
6928
7119
  ]);
6929
7120
  }
6930
- if (!(collection == null ? void 0 : collection.isDetached)) {
7121
+ if (!collection?.isDetached) {
6931
7122
  if (this.bridge) {
6932
7123
  await this.bridge.delete(normalizedPath);
6933
7124
  }
@@ -6967,20 +7158,26 @@ var Database = class {
6967
7158
  );
6968
7159
  const doc = await level2.keys({ limit: 1 }).next();
6969
7160
  if (!doc) {
6970
- await _indexContent(
6971
- this,
6972
- level2,
6973
- contentPaths,
7161
+ await _indexContent({
7162
+ database: this,
7163
+ level: level2,
7164
+ documentPaths: contentPaths,
6974
7165
  enqueueOps,
6975
7166
  collection,
6976
- userFields.map((field) => [
7167
+ passwordFields: userFields.map((field) => [
6977
7168
  ...field.path,
6978
7169
  field.passwordFieldName
6979
7170
  ])
6980
- );
7171
+ });
6981
7172
  }
6982
7173
  } else {
6983
- await _indexContent(this, level, contentPaths, enqueueOps, collection);
7174
+ await _indexContent({
7175
+ database: this,
7176
+ level,
7177
+ documentPaths: contentPaths,
7178
+ enqueueOps,
7179
+ collection
7180
+ });
6984
7181
  }
6985
7182
  }
6986
7183
  );
@@ -7016,7 +7213,7 @@ var Database = class {
7016
7213
  );
7017
7214
  }
7018
7215
  const metadata = await metadataLevel.get("metadata");
7019
- return metadata == null ? void 0 : metadata.version;
7216
+ return metadata?.version;
7020
7217
  }
7021
7218
  async initLevel() {
7022
7219
  if (this.contentLevel) {
@@ -7102,7 +7299,7 @@ var hashPasswordVisitor = async (node, path7) => {
7102
7299
  };
7103
7300
  var visitNodes = async (node, path7, callback) => {
7104
7301
  const [currentLevel, ...remainingLevels] = path7;
7105
- if (!(remainingLevels == null ? void 0 : remainingLevels.length)) {
7302
+ if (!remainingLevels?.length) {
7106
7303
  return callback(node, path7);
7107
7304
  }
7108
7305
  if (Array.isArray(node[currentLevel])) {
@@ -7118,18 +7315,27 @@ var hashPasswordValues = async (data, passwordFields) => Promise.all(
7118
7315
  async (passwordField) => visitNodes(data, passwordField, hashPasswordVisitor)
7119
7316
  )
7120
7317
  );
7121
- var isGitKeep = (filepath, collection) => filepath.endsWith(`.gitkeep.${(collection == null ? void 0 : collection.format) || "md"}`);
7122
- var _indexContent = async (database, level, documentPaths, enqueueOps, collection, passwordFields) => {
7318
+ var isGitKeep = (filepath, collection) => filepath.endsWith(`.gitkeep.${collection?.format || "md"}`);
7319
+ var _indexContent = async ({
7320
+ database,
7321
+ level,
7322
+ documentPaths,
7323
+ enqueueOps,
7324
+ collection,
7325
+ passwordFields,
7326
+ isPartialReindex
7327
+ }) => {
7123
7328
  let collectionIndexDefinitions;
7124
7329
  let collectionPath;
7125
7330
  if (collection) {
7126
7331
  const indexDefinitions = await database.getIndexDefinitions(level);
7127
- collectionIndexDefinitions = indexDefinitions == null ? void 0 : indexDefinitions[collection.name];
7332
+ collectionIndexDefinitions = indexDefinitions?.[collection.name];
7128
7333
  if (!collectionIndexDefinitions) {
7129
7334
  throw new Error(`No indexDefinitions for collection ${collection.name}`);
7130
7335
  }
7131
7336
  collectionPath = collection.path;
7132
7337
  }
7338
+ const collectionReferences = (await database.getCollectionReferences())?.[collection?.name];
7133
7339
  const tinaSchema = await database.getSchema();
7134
7340
  let templateInfo = null;
7135
7341
  if (collection) {
@@ -7147,7 +7353,7 @@ var _indexContent = async (database, level, documentPaths, enqueueOps, collectio
7147
7353
  if (!aliasedData) {
7148
7354
  return;
7149
7355
  }
7150
- if (passwordFields == null ? void 0 : passwordFields.length) {
7356
+ if (passwordFields?.length) {
7151
7357
  await hashPasswordValues(aliasedData, passwordFields);
7152
7358
  }
7153
7359
  const normalizedPath = (0, import_schema_tools4.normalizePath)(filepath);
@@ -7159,38 +7365,56 @@ var _indexContent = async (database, level, documentPaths, enqueueOps, collectio
7159
7365
  normalizedPath,
7160
7366
  collectionPath || ""
7161
7367
  );
7162
- const item = await rootSublevel.get(normalizedPath);
7163
- if (item) {
7164
- await database.contentLevel.batch([
7165
- ...makeIndexOpsForDocument(
7166
- normalizedPath,
7167
- collection.name,
7168
- collectionIndexDefinitions,
7169
- item,
7170
- "del",
7171
- level
7172
- ),
7173
- // folder indices
7174
- ...makeIndexOpsForDocument(
7175
- normalizedPath,
7176
- `${collection.name}_${folderKey}`,
7177
- collectionIndexDefinitions,
7178
- item,
7179
- "del",
7180
- level
7181
- ),
7182
- {
7183
- type: "del",
7184
- key: normalizedPath,
7185
- sublevel: rootSublevel
7186
- }
7187
- ]);
7368
+ if (isPartialReindex) {
7369
+ const item = await rootSublevel.get(normalizedPath);
7370
+ if (item) {
7371
+ await database.contentLevel.batch([
7372
+ ...makeRefOpsForDocument(
7373
+ normalizedPath,
7374
+ collection?.name,
7375
+ collectionReferences,
7376
+ item,
7377
+ "del",
7378
+ level
7379
+ ),
7380
+ ...makeIndexOpsForDocument(
7381
+ normalizedPath,
7382
+ collection.name,
7383
+ collectionIndexDefinitions,
7384
+ item,
7385
+ "del",
7386
+ level
7387
+ ),
7388
+ // folder indices
7389
+ ...makeIndexOpsForDocument(
7390
+ normalizedPath,
7391
+ `${collection.name}_${folderKey}`,
7392
+ collectionIndexDefinitions,
7393
+ item,
7394
+ "del",
7395
+ level
7396
+ ),
7397
+ {
7398
+ type: "del",
7399
+ key: normalizedPath,
7400
+ sublevel: rootSublevel
7401
+ }
7402
+ ]);
7403
+ }
7188
7404
  }
7189
7405
  if (!isGitKeep(filepath, collection)) {
7190
7406
  await enqueueOps([
7407
+ ...makeRefOpsForDocument(
7408
+ normalizedPath,
7409
+ collection?.name,
7410
+ collectionReferences,
7411
+ aliasedData,
7412
+ "put",
7413
+ level
7414
+ ),
7191
7415
  ...makeIndexOpsForDocument(
7192
7416
  normalizedPath,
7193
- collection == null ? void 0 : collection.name,
7417
+ collection?.name,
7194
7418
  collectionIndexDefinitions,
7195
7419
  aliasedData,
7196
7420
  "put",
@@ -7199,7 +7423,7 @@ var _indexContent = async (database, level, documentPaths, enqueueOps, collectio
7199
7423
  // folder indexes
7200
7424
  ...makeIndexOpsForDocument(
7201
7425
  normalizedPath,
7202
- `${collection == null ? void 0 : collection.name}_${folderKey}`,
7426
+ `${collection?.name}_${folderKey}`,
7203
7427
  collectionIndexDefinitions,
7204
7428
  aliasedData,
7205
7429
  "put",
@@ -7220,7 +7444,7 @@ var _indexContent = async (database, level, documentPaths, enqueueOps, collectio
7220
7444
  throw new TinaFetchError(`Unable to seed ${filepath}`, {
7221
7445
  originalError: error,
7222
7446
  file: filepath,
7223
- collection: collection == null ? void 0 : collection.name,
7447
+ collection: collection?.name,
7224
7448
  stack: error.stack
7225
7449
  });
7226
7450
  }
@@ -7246,11 +7470,12 @@ var _deleteIndexContent = async (database, documentPaths, enqueueOps, collection
7246
7470
  const indexDefinitions = await database.getIndexDefinitions(
7247
7471
  database.contentLevel
7248
7472
  );
7249
- collectionIndexDefinitions = indexDefinitions == null ? void 0 : indexDefinitions[collection.name];
7473
+ collectionIndexDefinitions = indexDefinitions?.[collection.name];
7250
7474
  if (!collectionIndexDefinitions) {
7251
7475
  throw new Error(`No indexDefinitions for collection ${collection.name}`);
7252
7476
  }
7253
7477
  }
7478
+ const collectionReferences = (await database.getCollectionReferences())?.[collection?.name];
7254
7479
  const tinaSchema = await database.getSchema();
7255
7480
  let templateInfo = null;
7256
7481
  if (collection) {
@@ -7267,13 +7492,21 @@ var _deleteIndexContent = async (database, documentPaths, enqueueOps, collection
7267
7492
  if (item) {
7268
7493
  const folderKey = folderTreeBuilder.update(
7269
7494
  itemKey,
7270
- (collection == null ? void 0 : collection.path) || ""
7495
+ collection?.path || ""
7271
7496
  );
7272
7497
  const aliasedData = templateInfo ? replaceNameOverrides(
7273
7498
  getTemplateForFile(templateInfo, item),
7274
7499
  item
7275
7500
  ) : item;
7276
7501
  await enqueueOps([
7502
+ ...makeRefOpsForDocument(
7503
+ itemKey,
7504
+ collection?.name,
7505
+ collectionReferences,
7506
+ aliasedData,
7507
+ "del",
7508
+ database.contentLevel
7509
+ ),
7277
7510
  ...makeIndexOpsForDocument(
7278
7511
  itemKey,
7279
7512
  collection.name,
@@ -7285,7 +7518,7 @@ var _deleteIndexContent = async (database, documentPaths, enqueueOps, collection
7285
7518
  // folder indexes
7286
7519
  ...makeIndexOpsForDocument(
7287
7520
  itemKey,
7288
- `${collection == null ? void 0 : collection.name}_${folderKey}`,
7521
+ `${collection?.name}_${folderKey}`,
7289
7522
  collectionIndexDefinitions,
7290
7523
  aliasedData,
7291
7524
  "del",
@@ -7370,12 +7603,12 @@ var getChangedFiles = async ({
7370
7603
  }
7371
7604
  }
7372
7605
  }
7373
- if (await (B == null ? void 0 : B.type()) === "tree") {
7606
+ if (await B?.type() === "tree") {
7374
7607
  return;
7375
7608
  }
7376
7609
  if (matches) {
7377
- const oidA = await (A == null ? void 0 : A.oid());
7378
- const oidB = await (B == null ? void 0 : B.oid());
7610
+ const oidA = await A?.oid();
7611
+ const oidB = await B?.oid();
7379
7612
  if (oidA !== oidB) {
7380
7613
  if (oidA === void 0) {
7381
7614
  results.added.push(relativePath);
@@ -7403,8 +7636,8 @@ var import_path5 = __toESM(require("path"));
7403
7636
  var import_normalize_path = __toESM(require("normalize-path"));
7404
7637
  var FilesystemBridge = class {
7405
7638
  constructor(rootPath, outputPath) {
7406
- this.rootPath = rootPath || "";
7407
- this.outputPath = outputPath || rootPath;
7639
+ this.rootPath = import_path5.default.resolve(rootPath);
7640
+ this.outputPath = outputPath ? import_path5.default.resolve(outputPath) : this.rootPath;
7408
7641
  }
7409
7642
  async glob(pattern, extension) {
7410
7643
  const basePath = import_path5.default.join(this.outputPath, ...pattern.split("/"));
@@ -7416,19 +7649,19 @@ var FilesystemBridge = class {
7416
7649
  }
7417
7650
  );
7418
7651
  const posixRootPath = (0, import_normalize_path.default)(this.outputPath);
7419
- return items.map((item) => {
7420
- return item.replace(posixRootPath, "").replace(/^\/|\/$/g, "");
7421
- });
7652
+ return items.map(
7653
+ (item) => item.substring(posixRootPath.length).replace(/^\/|\/$/g, "")
7654
+ );
7422
7655
  }
7423
7656
  async delete(filepath) {
7424
7657
  await import_fs_extra2.default.remove(import_path5.default.join(this.outputPath, filepath));
7425
7658
  }
7426
7659
  async get(filepath) {
7427
- return import_fs_extra2.default.readFileSync(import_path5.default.join(this.outputPath, filepath)).toString();
7660
+ return (await import_fs_extra2.default.readFile(import_path5.default.join(this.outputPath, filepath))).toString();
7428
7661
  }
7429
7662
  async put(filepath, data, basePathOverride) {
7430
7663
  const basePath = basePathOverride || this.outputPath;
7431
- await import_fs_extra2.default.outputFileSync(import_path5.default.join(basePath, filepath), data);
7664
+ await import_fs_extra2.default.outputFile(import_path5.default.join(basePath, filepath), data);
7432
7665
  }
7433
7666
  };
7434
7667
  var AuditFileSystemBridge = class extends FilesystemBridge {