@tinacms/graphql 1.4.15 → 1.4.16

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
@@ -39,10 +39,15 @@ __export(src_exports, {
39
39
  createDatabase: () => createDatabase,
40
40
  createSchema: () => createSchema,
41
41
  handleFetchErrorError: () => handleFetchErrorError,
42
+ loadAndParseWithAliases: () => loadAndParseWithAliases,
42
43
  parseFile: () => parseFile,
43
44
  resolve: () => resolve,
45
+ scanAllContent: () => scanAllContent,
46
+ scanContentByPaths: () => scanContentByPaths,
44
47
  sequential: () => sequential,
45
- stringifyFile: () => stringifyFile
48
+ stringifyFile: () => stringifyFile,
49
+ transformDocument: () => transformDocument,
50
+ transformDocumentIntoPayload: () => transformDocumentIntoPayload
46
51
  });
47
52
  module.exports = __toCommonJS(src_exports);
48
53
 
@@ -2577,7 +2582,7 @@ var validateField = async (field) => {
2577
2582
  // package.json
2578
2583
  var package_default = {
2579
2584
  name: "@tinacms/graphql",
2580
- version: "1.4.15",
2585
+ version: "1.4.16",
2581
2586
  main: "dist/index.js",
2582
2587
  module: "dist/index.es.js",
2583
2588
  typings: "dist/index.d.ts",
@@ -2880,7 +2885,7 @@ var _buildSchema = async (builder, tinaSchema) => {
2880
2885
  var import_graphql4 = require("graphql");
2881
2886
 
2882
2887
  // src/resolver/index.ts
2883
- var import_path2 = __toESM(require("path"));
2888
+ var import_path3 = __toESM(require("path"));
2884
2889
  var import_isValid = __toESM(require("date-fns/isValid"));
2885
2890
 
2886
2891
  // src/mdx/index.ts
@@ -3112,9 +3117,9 @@ var resolveMediaRelativeToCloud = (value, config = { useRelativeMedia: true }, s
3112
3117
  return value;
3113
3118
  }
3114
3119
  };
3115
- var cleanUpSlashes = (path5) => {
3116
- if (path5) {
3117
- return `/${path5.replace(/^\/+|\/+$/gm, "")}`;
3120
+ var cleanUpSlashes = (path6) => {
3121
+ if (path6) {
3122
+ return `/${path6.replace(/^\/+|\/+$/gm, "")}`;
3118
3123
  }
3119
3124
  return "";
3120
3125
  };
@@ -3181,13 +3186,131 @@ var LevelProxy = class {
3181
3186
  };
3182
3187
 
3183
3188
  // src/database/datalayer.ts
3184
- var import_path = __toESM(require("path"));
3189
+ var import_path2 = __toESM(require("path"));
3185
3190
 
3186
3191
  // src/database/util.ts
3187
3192
  var import_toml = __toESM(require("@iarna/toml"));
3188
3193
  var import_js_yaml = __toESM(require("js-yaml"));
3189
3194
  var import_gray_matter = __toESM(require("gray-matter"));
3190
3195
  var import_schema_tools3 = require("@tinacms/schema-tools");
3196
+ var import_micromatch = __toESM(require("micromatch"));
3197
+ var import_path = __toESM(require("path"));
3198
+
3199
+ // src/database/alias-utils.ts
3200
+ var replaceBlockAliases = (template, item) => {
3201
+ const output = { ...item };
3202
+ const templateKey = template.templateKey || "_template";
3203
+ const templateName = output[templateKey];
3204
+ const matchingTemplate = template.templates.find(
3205
+ (t) => t.nameOverride == templateName || t.name == templateName
3206
+ );
3207
+ if (!matchingTemplate) {
3208
+ throw new Error(
3209
+ `Block template "${templateName}" is not defined for field "${template.name}"`
3210
+ );
3211
+ }
3212
+ output._template = matchingTemplate.name;
3213
+ if (templateKey != "_template") {
3214
+ delete output[templateKey];
3215
+ }
3216
+ return output;
3217
+ };
3218
+ var replaceNameOverrides = (template, obj) => {
3219
+ if (template.list) {
3220
+ return obj.map((item) => {
3221
+ if (isBlockField(template)) {
3222
+ item = replaceBlockAliases(template, item);
3223
+ }
3224
+ return _replaceNameOverrides(
3225
+ getTemplateForData(template, item).fields,
3226
+ item
3227
+ );
3228
+ });
3229
+ } else {
3230
+ return _replaceNameOverrides(getTemplateForData(template, obj).fields, obj);
3231
+ }
3232
+ };
3233
+ function isBlockField(field) {
3234
+ var _a;
3235
+ return field && field.type === "object" && ((_a = field.templates) == null ? void 0 : _a.length) > 0;
3236
+ }
3237
+ var _replaceNameOverrides = (fields, obj) => {
3238
+ const output = {};
3239
+ Object.keys(obj).forEach((key) => {
3240
+ const field = fields.find(
3241
+ (fieldWithMatchingAlias) => ((fieldWithMatchingAlias == null ? void 0 : fieldWithMatchingAlias.nameOverride) || (fieldWithMatchingAlias == null ? void 0 : fieldWithMatchingAlias.name)) === key
3242
+ );
3243
+ output[(field == null ? void 0 : field.name) || key] = (field == null ? void 0 : field.type) == "object" ? replaceNameOverrides(field, obj[key]) : obj[key];
3244
+ });
3245
+ return output;
3246
+ };
3247
+ var getTemplateForData = (field, data) => {
3248
+ var _a;
3249
+ if ((_a = field.templates) == null ? void 0 : _a.length) {
3250
+ const templateKey = "_template";
3251
+ if (data[templateKey]) {
3252
+ const result = field.templates.find(
3253
+ (template) => template.nameOverride === data[templateKey] || template.name === data[templateKey]
3254
+ );
3255
+ if (result) {
3256
+ return result;
3257
+ }
3258
+ throw new Error(
3259
+ `Template "${data[templateKey]}" is not defined for field "${field.name}"`
3260
+ );
3261
+ }
3262
+ throw new Error(
3263
+ `Missing required key "${templateKey}" on field "${field.name}"`
3264
+ );
3265
+ } else {
3266
+ return field;
3267
+ }
3268
+ };
3269
+ var applyBlockAliases = (template, item) => {
3270
+ const output = { ...item };
3271
+ const templateKey = template.templateKey || "_template";
3272
+ const templateName = output._template;
3273
+ const matchingTemplate = template.templates.find(
3274
+ (t) => t.nameOverride == templateName || t.name == templateName
3275
+ );
3276
+ if (!matchingTemplate) {
3277
+ throw new Error(
3278
+ `Block template "${templateName}" is not defined for field "${template.name}"`
3279
+ );
3280
+ }
3281
+ output[templateKey] = matchingTemplate.nameOverride || matchingTemplate.name;
3282
+ if (templateKey != "_template") {
3283
+ delete output._template;
3284
+ }
3285
+ return output;
3286
+ };
3287
+ var applyNameOverrides = (template, obj) => {
3288
+ if (template.list) {
3289
+ return obj.map((item) => {
3290
+ let result = _applyNameOverrides(
3291
+ getTemplateForData(template, item).fields,
3292
+ item
3293
+ );
3294
+ if (isBlockField(template)) {
3295
+ result = applyBlockAliases(template, result);
3296
+ }
3297
+ return result;
3298
+ });
3299
+ } else {
3300
+ return _applyNameOverrides(getTemplateForData(template, obj).fields, obj);
3301
+ }
3302
+ };
3303
+ var _applyNameOverrides = (fields, obj) => {
3304
+ const output = {};
3305
+ Object.keys(obj).forEach((key) => {
3306
+ const field = fields.find((field2) => field2.name === key);
3307
+ const outputKey = (field == null ? void 0 : field.nameOverride) || key;
3308
+ output[outputKey] = (field == null ? void 0 : field.type) === "object" ? applyNameOverrides(field, obj[key]) : obj[key];
3309
+ });
3310
+ return output;
3311
+ };
3312
+
3313
+ // src/database/util.ts
3191
3314
  var matterEngines = {
3192
3315
  toml: {
3193
3316
  parse: (val) => import_toml.default.parse(val),
@@ -3273,6 +3396,142 @@ var parseFile = (content, format, yupSchema, markdownParseConfig) => {
3273
3396
  throw new Error(`Must specify a valid format, got ${format}`);
3274
3397
  }
3275
3398
  };
3399
+ var scanAllContent = async (tinaSchema, bridge, callback) => {
3400
+ const warnings = [];
3401
+ const filesSeen = /* @__PURE__ */ new Map();
3402
+ const duplicateFiles = /* @__PURE__ */ new Set();
3403
+ await sequential(tinaSchema.getCollections(), async (collection) => {
3404
+ const normalPath = (0, import_schema_tools3.normalizePath)(collection.path);
3405
+ const format = collection.format || "md";
3406
+ const documentPaths = await bridge.glob(normalPath, format);
3407
+ const matches = tinaSchema.getMatches({ collection });
3408
+ const filteredPaths = matches.length > 0 ? (0, import_micromatch.default)(documentPaths, matches) : documentPaths;
3409
+ filteredPaths.forEach((path6) => {
3410
+ if (filesSeen.has(path6)) {
3411
+ filesSeen.get(path6).push(collection.name);
3412
+ duplicateFiles.add(path6);
3413
+ } else {
3414
+ filesSeen.set(path6, [collection.name]);
3415
+ }
3416
+ });
3417
+ duplicateFiles.forEach((path6) => {
3418
+ warnings.push(
3419
+ `"${path6}" Found in multiple collections: ${filesSeen.get(path6).map((collection2) => `"${collection2}"`).join(
3420
+ ", "
3421
+ )}. This can cause unexpected behavior. We recommend updating the \`match\` property of those collections so that each file is in only one collection.
3422
+ This will be an error in the future. See https://tina.io/docs/errors/file-in-mutpliple-collections/
3423
+ `
3424
+ );
3425
+ });
3426
+ await callback(collection, filteredPaths);
3427
+ });
3428
+ return warnings;
3429
+ };
3430
+ var scanContentByPaths = async (tinaSchema, documentPaths, callback) => {
3431
+ const { pathsByCollection, nonCollectionPaths, collections } = await partitionPathsByCollection(tinaSchema, documentPaths);
3432
+ for (const collection of Object.keys(pathsByCollection)) {
3433
+ await callback(collections[collection], pathsByCollection[collection]);
3434
+ }
3435
+ if (nonCollectionPaths.length) {
3436
+ await callback(void 0, nonCollectionPaths);
3437
+ }
3438
+ };
3439
+ var partitionPathsByCollection = async (tinaSchema, documentPaths) => {
3440
+ const pathsByCollection = {};
3441
+ const nonCollectionPaths = [];
3442
+ const collections = {};
3443
+ for (const documentPath of documentPaths) {
3444
+ const collection = await tinaSchema.getCollectionByFullPath(documentPath);
3445
+ if (collection) {
3446
+ if (!pathsByCollection[collection.name]) {
3447
+ pathsByCollection[collection.name] = [];
3448
+ }
3449
+ collections[collection.name] = collection;
3450
+ pathsByCollection[collection.name].push(documentPath);
3451
+ } else {
3452
+ nonCollectionPaths.push(documentPath);
3453
+ }
3454
+ }
3455
+ return { pathsByCollection, nonCollectionPaths, collections };
3456
+ };
3457
+ var transformDocument = (filepath, contentObject, tinaSchema) => {
3458
+ const extension = import_path.default.extname(filepath);
3459
+ const templateName = hasOwnProperty(contentObject, "_template") && typeof contentObject._template === "string" ? contentObject._template : void 0;
3460
+ const { collection, template } = hasOwnProperty(contentObject, "__collection") ? {
3461
+ collection: tinaSchema.getCollection(
3462
+ contentObject["__collection"]
3463
+ ),
3464
+ template: void 0
3465
+ } : tinaSchema.getCollectionAndTemplateByFullPath(filepath, templateName);
3466
+ const field = template == null ? void 0 : template.fields.find((field2) => {
3467
+ if (field2.type === "string" || field2.type === "rich-text") {
3468
+ if (field2.isBody) {
3469
+ return true;
3470
+ }
3471
+ }
3472
+ return false;
3473
+ });
3474
+ let data = contentObject;
3475
+ if ((extension === ".md" || extension === ".mdx") && field) {
3476
+ if (hasOwnProperty(contentObject, "$_body")) {
3477
+ const { $_body, ...rest } = contentObject;
3478
+ data = rest;
3479
+ data[field.name] = $_body;
3480
+ }
3481
+ }
3482
+ return {
3483
+ ...data,
3484
+ _collection: collection.name,
3485
+ _keepTemplateKey: !!collection.templates,
3486
+ _template: (template == null ? void 0 : template.namespace) ? lastItem(template == null ? void 0 : template.namespace) : void 0,
3487
+ _relativePath: filepath.replace(collection.path, "").replace(/^\/|\/$/g, ""),
3488
+ _id: filepath
3489
+ };
3490
+ };
3491
+ function hasOwnProperty(obj, prop) {
3492
+ return obj.hasOwnProperty(prop);
3493
+ }
3494
+ var getTemplateForFile = (templateInfo, data) => {
3495
+ if ((templateInfo == null ? void 0 : templateInfo.type) === "object") {
3496
+ return templateInfo.template;
3497
+ }
3498
+ if ((templateInfo == null ? void 0 : templateInfo.type) === "union") {
3499
+ if (hasOwnProperty(data, "_template")) {
3500
+ const template = templateInfo.templates.find(
3501
+ (t) => lastItem(t.namespace) === data._template
3502
+ );
3503
+ if (!template) {
3504
+ throw new Error(
3505
+ `Unable to find template "${data._template}". Possible templates are: ${templateInfo.templates.map((template2) => `"${template2.name}"`).join(", ")}.`
3506
+ );
3507
+ }
3508
+ return template;
3509
+ } else {
3510
+ return void 0;
3511
+ }
3512
+ }
3513
+ throw new Error(`Unable to determine template`);
3514
+ };
3515
+ var loadAndParseWithAliases = async (bridge, filepath, collection, templateInfo) => {
3516
+ const dataString = await bridge.get((0, import_schema_tools3.normalizePath)(filepath));
3517
+ const data = parseFile(
3518
+ dataString,
3519
+ import_path.default.extname(filepath),
3520
+ (yup3) => yup3.object({}),
3521
+ {
3522
+ frontmatterDelimiters: collection == null ? void 0 : collection.frontmatterDelimiters,
3523
+ frontmatterFormat: collection == null ? void 0 : collection.frontmatterFormat
3524
+ }
3525
+ );
3526
+ const template = getTemplateForFile(templateInfo, data);
3527
+ if (!template) {
3528
+ console.warn(
3529
+ `Document: ${filepath} has an ambiguous template, skipping from indexing`
3530
+ );
3531
+ return;
3532
+ }
3533
+ return templateInfo ? replaceNameOverrides(template, data) : data;
3534
+ };
3276
3535
 
3277
3536
  // src/database/datalayer.ts
3278
3537
  var DEFAULT_COLLECTION_SORT_KEY = "__filepath__";
@@ -3717,9 +3976,9 @@ var makeFilterSuffixes = (filterChain, index) => {
3717
3976
  }
3718
3977
  };
3719
3978
  var FOLDER_ROOT = "~";
3720
- var stripCollectionFromPath = (collectionPath, path5) => {
3979
+ var stripCollectionFromPath = (collectionPath, path6) => {
3721
3980
  const collectionPathParts = collectionPath.split("/");
3722
- const pathParts = path5.split("/");
3981
+ const pathParts = path6.split("/");
3723
3982
  const strippedPathParts = pathParts.slice(collectionPathParts.length);
3724
3983
  return strippedPathParts.join("/");
3725
3984
  };
@@ -3733,7 +3992,7 @@ var FolderTreeBuilder = class {
3733
3992
  return this._tree;
3734
3993
  }
3735
3994
  update(documentPath, collectionPath) {
3736
- let folderPath = import_path.default.dirname((0, import_schema_tools3.normalizePath)(documentPath));
3995
+ let folderPath = import_path2.default.dirname((0, import_schema_tools3.normalizePath)(documentPath));
3737
3996
  if (folderPath === ".") {
3738
3997
  folderPath = "";
3739
3998
  }
@@ -3746,7 +4005,7 @@ var FolderTreeBuilder = class {
3746
4005
  if (!this._tree[current2]) {
3747
4006
  this._tree[current2] = /* @__PURE__ */ new Set();
3748
4007
  }
3749
- this._tree[current2].add((0, import_schema_tools3.normalizePath)(import_path.default.join(current2, part)));
4008
+ this._tree[current2].add((0, import_schema_tools3.normalizePath)(import_path2.default.join(current2, part)));
3750
4009
  parent.push(part);
3751
4010
  });
3752
4011
  const current = parent.join("/");
@@ -3774,13 +4033,13 @@ var makeFolderOpsForCollection = (folderTree, collection, indexDefinitions, opTy
3774
4033
  SUBLEVEL_OPTIONS
3775
4034
  );
3776
4035
  let folderSortingIdx = 0;
3777
- for (const path5 of Array.from(folder).sort()) {
4036
+ for (const path6 of Array.from(folder).sort()) {
3778
4037
  for (const [sort] of Object.entries(indexDefinitions)) {
3779
4038
  const indexSublevel = folderCollectionSublevel.sublevel(
3780
4039
  sort,
3781
4040
  SUBLEVEL_OPTIONS
3782
4041
  );
3783
- const subFolderKey = import_js_sha1.default.hex(path5);
4042
+ const subFolderKey = import_js_sha1.default.hex(path6);
3784
4043
  if (sort === DEFAULT_COLLECTION_SORT_KEY) {
3785
4044
  result.push({
3786
4045
  type: opType,
@@ -3808,7 +4067,7 @@ var makeFolderOpsForCollection = (folderTree, collection, indexDefinitions, opTy
3808
4067
  key: `${collection.path}/${parentFolderKey}.${collection.format}`,
3809
4068
  value: {
3810
4069
  __collection: collection.name,
3811
- __folderBasename: import_path.default.basename(folderName),
4070
+ __folderBasename: import_path2.default.basename(folderName),
3812
4071
  __folderPath: folderName
3813
4072
  },
3814
4073
  sublevel: level.sublevel(
@@ -3872,6 +4131,195 @@ var stringEscaper = makeStringEscaper(
3872
4131
  var createResolver = (args) => {
3873
4132
  return new Resolver(args);
3874
4133
  };
4134
+ var resolveFieldData = async ({ namespace, ...field }, rawData, accumulator, tinaSchema, config, isAudit) => {
4135
+ var _a;
4136
+ if (!rawData) {
4137
+ return void 0;
4138
+ }
4139
+ assertShape(rawData, (yup3) => yup3.object());
4140
+ const value = rawData[field.name];
4141
+ switch (field.type) {
4142
+ case "datetime":
4143
+ if (value instanceof Date) {
4144
+ accumulator[field.name] = value.toISOString();
4145
+ } else {
4146
+ accumulator[field.name] = value;
4147
+ }
4148
+ break;
4149
+ case "string":
4150
+ case "boolean":
4151
+ case "number":
4152
+ accumulator[field.name] = value;
4153
+ break;
4154
+ case "reference":
4155
+ if (value) {
4156
+ accumulator[field.name] = value;
4157
+ }
4158
+ break;
4159
+ case "image":
4160
+ accumulator[field.name] = resolveMediaRelativeToCloud(
4161
+ value,
4162
+ config,
4163
+ tinaSchema.schema
4164
+ );
4165
+ break;
4166
+ case "rich-text":
4167
+ const tree = (0, import_mdx.parseMDX)(
4168
+ value,
4169
+ field,
4170
+ (value2) => resolveMediaRelativeToCloud(value2, config, tinaSchema.schema)
4171
+ );
4172
+ if (((_a = tree == null ? void 0 : tree.children[0]) == null ? void 0 : _a.type) === "invalid_markdown") {
4173
+ if (isAudit) {
4174
+ const invalidNode = tree == null ? void 0 : tree.children[0];
4175
+ throw new import_graphql3.GraphQLError(
4176
+ `${invalidNode == null ? void 0 : invalidNode.message}${invalidNode.position ? ` at line ${invalidNode.position.start.line}, column ${invalidNode.position.start.column}` : ""}`
4177
+ );
4178
+ }
4179
+ }
4180
+ accumulator[field.name] = tree;
4181
+ break;
4182
+ case "object":
4183
+ if (field.list) {
4184
+ if (!value) {
4185
+ return;
4186
+ }
4187
+ assertShape(
4188
+ value,
4189
+ (yup3) => yup3.array().of(yup3.object().required())
4190
+ );
4191
+ accumulator[field.name] = await sequential(value, async (item) => {
4192
+ const template = await tinaSchema.getTemplateForData({
4193
+ data: item,
4194
+ collection: {
4195
+ namespace,
4196
+ ...field
4197
+ }
4198
+ });
4199
+ const payload = {};
4200
+ await sequential(template.fields, async (field2) => {
4201
+ await resolveFieldData(
4202
+ field2,
4203
+ item,
4204
+ payload,
4205
+ tinaSchema,
4206
+ config,
4207
+ isAudit
4208
+ );
4209
+ });
4210
+ const isUnion = !!field.templates;
4211
+ return isUnion ? {
4212
+ _template: lastItem(template.namespace),
4213
+ ...payload
4214
+ } : payload;
4215
+ });
4216
+ } else {
4217
+ if (!value) {
4218
+ return;
4219
+ }
4220
+ const template = await tinaSchema.getTemplateForData({
4221
+ data: value,
4222
+ collection: {
4223
+ namespace,
4224
+ ...field
4225
+ }
4226
+ });
4227
+ const payload = {};
4228
+ await sequential(template.fields, async (field2) => {
4229
+ await resolveFieldData(
4230
+ field2,
4231
+ value,
4232
+ payload,
4233
+ tinaSchema,
4234
+ config,
4235
+ isAudit
4236
+ );
4237
+ });
4238
+ const isUnion = !!field.templates;
4239
+ accumulator[field.name] = isUnion ? {
4240
+ _template: lastItem(template.namespace),
4241
+ ...payload
4242
+ } : payload;
4243
+ }
4244
+ break;
4245
+ default:
4246
+ return field;
4247
+ }
4248
+ return accumulator;
4249
+ };
4250
+ var transformDocumentIntoPayload = async (fullPath, rawData, tinaSchema, config, isAudit) => {
4251
+ const collection = tinaSchema.getCollection(rawData._collection);
4252
+ try {
4253
+ const template = tinaSchema.getTemplateForData({
4254
+ data: rawData,
4255
+ collection
4256
+ });
4257
+ const {
4258
+ base: basename,
4259
+ ext: extension,
4260
+ name: filename
4261
+ } = import_path3.default.parse(fullPath);
4262
+ const relativePath = fullPath.replace(/\\/g, "/").replace(collection.path, "").replace(/^\/|\/$/g, "");
4263
+ const breadcrumbs = relativePath.replace(extension, "").split("/");
4264
+ const data = {
4265
+ _collection: rawData._collection,
4266
+ _template: rawData._template
4267
+ };
4268
+ try {
4269
+ await sequential(template.fields, async (field) => {
4270
+ return resolveFieldData(
4271
+ field,
4272
+ rawData,
4273
+ data,
4274
+ tinaSchema,
4275
+ config,
4276
+ isAudit
4277
+ );
4278
+ });
4279
+ } catch (e) {
4280
+ throw new TinaParseDocumentError({
4281
+ originalError: e,
4282
+ collection: collection.name,
4283
+ includeAuditMessage: !isAudit,
4284
+ file: relativePath,
4285
+ stack: e.stack
4286
+ });
4287
+ }
4288
+ const titleField = template.fields.find((x) => {
4289
+ if (x.type === "string" && (x == null ? void 0 : x.isTitle)) {
4290
+ return true;
4291
+ }
4292
+ });
4293
+ const titleFieldName = titleField == null ? void 0 : titleField.name;
4294
+ const title = data[titleFieldName || " "] || null;
4295
+ return {
4296
+ __typename: collection.fields ? NAMER.documentTypeName(collection.namespace) : NAMER.documentTypeName(template.namespace),
4297
+ id: fullPath,
4298
+ ...data,
4299
+ _sys: {
4300
+ title: title || "",
4301
+ basename,
4302
+ filename,
4303
+ extension,
4304
+ path: fullPath,
4305
+ relativePath,
4306
+ breadcrumbs,
4307
+ collection,
4308
+ template: lastItem(template.namespace)
4309
+ },
4310
+ _values: data,
4311
+ _rawData: rawData
4312
+ };
4313
+ } catch (e) {
4314
+ if (e instanceof TinaGraphQLError) {
4315
+ throw new TinaGraphQLError(e.message, {
4316
+ requestedDocument: fullPath,
4317
+ ...e.extensions
4318
+ });
4319
+ }
4320
+ throw e;
4321
+ }
4322
+ };
3875
4323
  var Resolver = class {
3876
4324
  constructor(init) {
3877
4325
  this.init = init;
@@ -3904,7 +4352,13 @@ var Resolver = class {
3904
4352
  path: rawData["__folderPath"]
3905
4353
  };
3906
4354
  } else {
3907
- return this.transformDocumentIntoPayload(fullPath, rawData);
4355
+ return transformDocumentIntoPayload(
4356
+ fullPath,
4357
+ rawData,
4358
+ this.tinaSchema,
4359
+ this.config,
4360
+ this.isAudit
4361
+ );
3908
4362
  }
3909
4363
  };
3910
4364
  this.getDocument = async (fullPath) => {
@@ -3912,73 +4366,13 @@ var Resolver = class {
3912
4366
  throw new Error(`fullPath must be of type string for getDocument request`);
3913
4367
  }
3914
4368
  const rawData = await this.getRaw(fullPath);
3915
- return this.transformDocumentIntoPayload(fullPath, rawData);
3916
- };
3917
- this.transformDocumentIntoPayload = async (fullPath, rawData) => {
3918
- const collection = this.tinaSchema.getCollection(rawData._collection);
3919
- try {
3920
- const template = await this.tinaSchema.getTemplateForData({
3921
- data: rawData,
3922
- collection
3923
- });
3924
- const {
3925
- base: basename,
3926
- ext: extension,
3927
- name: filename
3928
- } = import_path2.default.parse(fullPath);
3929
- const relativePath = fullPath.replace(/\\/g, "/").replace(collection.path, "").replace(/^\/|\/$/g, "");
3930
- const breadcrumbs = relativePath.replace(extension, "").split("/");
3931
- const data = {
3932
- _collection: rawData._collection,
3933
- _template: rawData._template
3934
- };
3935
- try {
3936
- await sequential(template.fields, async (field) => {
3937
- return this.resolveFieldData(field, rawData, data);
3938
- });
3939
- } catch (e) {
3940
- throw new TinaParseDocumentError({
3941
- originalError: e,
3942
- collection: collection.name,
3943
- includeAuditMessage: !this.isAudit,
3944
- file: relativePath,
3945
- stack: e.stack
3946
- });
3947
- }
3948
- const titleField = template.fields.find((x) => {
3949
- if (x.type === "string" && (x == null ? void 0 : x.isTitle)) {
3950
- return true;
3951
- }
3952
- });
3953
- const titleFieldName = titleField == null ? void 0 : titleField.name;
3954
- const title = data[titleFieldName || " "] || null;
3955
- return {
3956
- __typename: collection.fields ? NAMER.documentTypeName(collection.namespace) : NAMER.documentTypeName(template.namespace),
3957
- id: fullPath,
3958
- ...data,
3959
- _sys: {
3960
- title: title || "",
3961
- basename,
3962
- filename,
3963
- extension,
3964
- path: fullPath,
3965
- relativePath,
3966
- breadcrumbs,
3967
- collection,
3968
- template: lastItem(template.namespace)
3969
- },
3970
- _values: data,
3971
- _rawData: rawData
3972
- };
3973
- } catch (e) {
3974
- if (e instanceof TinaGraphQLError) {
3975
- throw new TinaGraphQLError(e.message, {
3976
- requestedDocument: fullPath,
3977
- ...e.extensions
3978
- });
3979
- }
3980
- throw e;
3981
- }
4369
+ return transformDocumentIntoPayload(
4370
+ fullPath,
4371
+ rawData,
4372
+ this.tinaSchema,
4373
+ this.config,
4374
+ this.isAudit
4375
+ );
3982
4376
  };
3983
4377
  this.deleteDocument = async (fullPath) => {
3984
4378
  if (typeof fullPath !== "string") {
@@ -4168,7 +4562,7 @@ var Resolver = class {
4168
4562
  (yup3) => yup3.object({ relativePath: yup3.string().required() })
4169
4563
  );
4170
4564
  const collection = await this.tinaSchema.getCollection(collectionLookup);
4171
- const realPath = import_path2.default.join(collection == null ? void 0 : collection.path, args.relativePath);
4565
+ const realPath = import_path3.default.join(collection == null ? void 0 : collection.path, args.relativePath);
4172
4566
  const alreadyExists = await this.database.documentExists(realPath);
4173
4567
  if (isMutation) {
4174
4568
  if (isCreation) {
@@ -4209,7 +4603,7 @@ var Resolver = class {
4209
4603
  (yup3) => yup3.object({ relativePath: yup3.string().required() })
4210
4604
  );
4211
4605
  const doc = await this.getDocument(realPath);
4212
- const newRealPath = import_path2.default.join(
4606
+ const newRealPath = import_path3.default.join(
4213
4607
  collection == null ? void 0 : collection.path,
4214
4608
  args.params.relativePath
4215
4609
  );
@@ -4264,7 +4658,7 @@ var Resolver = class {
4264
4658
  first: -1
4265
4659
  },
4266
4660
  collection: referencedCollection,
4267
- hydrator: (path5) => path5
4661
+ hydrator: (path6) => path6
4268
4662
  }
4269
4663
  );
4270
4664
  const { edges } = resolvedCollectionConnection;
@@ -4384,112 +4778,6 @@ var Resolver = class {
4384
4778
  });
4385
4779
  return accum;
4386
4780
  };
4387
- this.resolveFieldData = async ({ namespace, ...field }, rawData, accumulator) => {
4388
- var _a;
4389
- if (!rawData) {
4390
- return void 0;
4391
- }
4392
- assertShape(rawData, (yup3) => yup3.object());
4393
- const value = rawData[field.name];
4394
- switch (field.type) {
4395
- case "datetime":
4396
- if (value instanceof Date) {
4397
- accumulator[field.name] = value.toISOString();
4398
- } else {
4399
- accumulator[field.name] = value;
4400
- }
4401
- break;
4402
- case "string":
4403
- case "boolean":
4404
- case "number":
4405
- accumulator[field.name] = value;
4406
- break;
4407
- case "reference":
4408
- if (value) {
4409
- accumulator[field.name] = value;
4410
- }
4411
- break;
4412
- case "image":
4413
- accumulator[field.name] = resolveMediaRelativeToCloud(
4414
- value,
4415
- this.config,
4416
- this.tinaSchema.schema
4417
- );
4418
- break;
4419
- case "rich-text":
4420
- const tree = (0, import_mdx.parseMDX)(
4421
- value,
4422
- field,
4423
- (value2) => resolveMediaRelativeToCloud(
4424
- value2,
4425
- this.config,
4426
- this.tinaSchema.schema
4427
- )
4428
- );
4429
- if (((_a = tree == null ? void 0 : tree.children[0]) == null ? void 0 : _a.type) === "invalid_markdown") {
4430
- if (this.isAudit) {
4431
- const invalidNode = tree == null ? void 0 : tree.children[0];
4432
- throw new import_graphql3.GraphQLError(
4433
- `${invalidNode == null ? void 0 : invalidNode.message}${invalidNode.position ? ` at line ${invalidNode.position.start.line}, column ${invalidNode.position.start.column}` : ""}`
4434
- );
4435
- }
4436
- }
4437
- accumulator[field.name] = tree;
4438
- break;
4439
- case "object":
4440
- if (field.list) {
4441
- if (!value) {
4442
- return;
4443
- }
4444
- assertShape(
4445
- value,
4446
- (yup3) => yup3.array().of(yup3.object().required())
4447
- );
4448
- accumulator[field.name] = await sequential(value, async (item) => {
4449
- const template = await this.tinaSchema.getTemplateForData({
4450
- data: item,
4451
- collection: {
4452
- namespace,
4453
- ...field
4454
- }
4455
- });
4456
- const payload = {};
4457
- await sequential(template.fields, async (field2) => {
4458
- await this.resolveFieldData(field2, item, payload);
4459
- });
4460
- const isUnion = !!field.templates;
4461
- return isUnion ? {
4462
- _template: lastItem(template.namespace),
4463
- ...payload
4464
- } : payload;
4465
- });
4466
- } else {
4467
- if (!value) {
4468
- return;
4469
- }
4470
- const template = await this.tinaSchema.getTemplateForData({
4471
- data: value,
4472
- collection: {
4473
- namespace,
4474
- ...field
4475
- }
4476
- });
4477
- const payload = {};
4478
- await sequential(template.fields, async (field2) => {
4479
- await this.resolveFieldData(field2, value, payload);
4480
- });
4481
- const isUnion = !!field.templates;
4482
- accumulator[field.name] = isUnion ? {
4483
- _template: lastItem(template.namespace),
4484
- ...payload
4485
- } : payload;
4486
- }
4487
- break;
4488
- default:
4489
- return field;
4490
- }
4491
- return accumulator;
4492
- };
4493
4781
  this.buildParams = (args) => {
4494
4782
  try {
4495
4783
  assertShape(
@@ -4806,125 +5094,9 @@ var resolve = async ({
4806
5094
  };
4807
5095
 
4808
5096
  // src/database/index.ts
4809
- var import_path3 = __toESM(require("path"));
5097
+ var import_path4 = __toESM(require("path"));
4810
5098
  var import_graphql5 = require("graphql");
4811
- var import_micromatch = __toESM(require("micromatch"));
4812
-
4813
- // src/database/alias-utils.ts
4814
- var replaceBlockAliases = (template, item) => {
4815
- const output = { ...item };
4816
- const templateKey = template.templateKey || "_template";
4817
- const templateName = output[templateKey];
4818
- const matchingTemplate = template.templates.find(
4819
- (t) => t.nameOverride == templateName || t.name == templateName
4820
- );
4821
- if (!matchingTemplate) {
4822
- throw new Error(
4823
- `Block template "${templateName}" is not defined for field "${template.name}"`
4824
- );
4825
- }
4826
- output._template = matchingTemplate.name;
4827
- if (templateKey != "_template") {
4828
- delete output[templateKey];
4829
- }
4830
- return output;
4831
- };
4832
- var replaceNameOverrides = (template, obj) => {
4833
- if (template.list) {
4834
- return obj.map((item) => {
4835
- if (isBlockField(template)) {
4836
- item = replaceBlockAliases(template, item);
4837
- }
4838
- return _replaceNameOverrides(
4839
- getTemplateForData(template, item).fields,
4840
- item
4841
- );
4842
- });
4843
- } else {
4844
- return _replaceNameOverrides(getTemplateForData(template, obj).fields, obj);
4845
- }
4846
- };
4847
- function isBlockField(field) {
4848
- var _a;
4849
- return field && field.type === "object" && ((_a = field.templates) == null ? void 0 : _a.length) > 0;
4850
- }
4851
- var _replaceNameOverrides = (fields, obj) => {
4852
- const output = {};
4853
- Object.keys(obj).forEach((key) => {
4854
- const field = fields.find(
4855
- (fieldWithMatchingAlias) => ((fieldWithMatchingAlias == null ? void 0 : fieldWithMatchingAlias.nameOverride) || (fieldWithMatchingAlias == null ? void 0 : fieldWithMatchingAlias.name)) === key
4856
- );
4857
- output[(field == null ? void 0 : field.name) || key] = (field == null ? void 0 : field.type) == "object" ? replaceNameOverrides(field, obj[key]) : obj[key];
4858
- });
4859
- return output;
4860
- };
4861
- var getTemplateForData = (field, data) => {
4862
- var _a;
4863
- if ((_a = field.templates) == null ? void 0 : _a.length) {
4864
- const templateKey = "_template";
4865
- if (data[templateKey]) {
4866
- const result = field.templates.find(
4867
- (template) => template.nameOverride === data[templateKey] || template.name === data[templateKey]
4868
- );
4869
- if (result) {
4870
- return result;
4871
- }
4872
- throw new Error(
4873
- `Template "${data[templateKey]}" is not defined for field "${field.name}"`
4874
- );
4875
- }
4876
- throw new Error(
4877
- `Missing required key "${templateKey}" on field "${field.name}"`
4878
- );
4879
- } else {
4880
- return field;
4881
- }
4882
- };
4883
- var applyBlockAliases = (template, item) => {
4884
- const output = { ...item };
4885
- const templateKey = template.templateKey || "_template";
4886
- const templateName = output._template;
4887
- const matchingTemplate = template.templates.find(
4888
- (t) => t.nameOverride == templateName || t.name == templateName
4889
- );
4890
- if (!matchingTemplate) {
4891
- throw new Error(
4892
- `Block template "${templateName}" is not defined for field "${template.name}"`
4893
- );
4894
- }
4895
- output[templateKey] = matchingTemplate.nameOverride || matchingTemplate.name;
4896
- if (templateKey != "_template") {
4897
- delete output._template;
4898
- }
4899
- return output;
4900
- };
4901
- var applyNameOverrides = (template, obj) => {
4902
- if (template.list) {
4903
- return obj.map((item) => {
4904
- let result = _applyNameOverrides(
4905
- getTemplateForData(template, item).fields,
4906
- item
4907
- );
4908
- if (isBlockField(template)) {
4909
- result = applyBlockAliases(template, result);
4910
- }
4911
- return result;
4912
- });
4913
- } else {
4914
- return _applyNameOverrides(getTemplateForData(template, obj).fields, obj);
4915
- }
4916
- };
4917
- var _applyNameOverrides = (fields, obj) => {
4918
- const output = {};
4919
- Object.keys(obj).forEach((key) => {
4920
- const field = fields.find((field2) => field2.name === key);
4921
- const outputKey = (field == null ? void 0 : field.nameOverride) || key;
4922
- output[outputKey] = (field == null ? void 0 : field.type) === "object" ? applyNameOverrides(field, obj[key]) : obj[key];
4923
- });
4924
- return output;
4925
- };
4926
-
4927
- // src/database/index.ts
5099
+ var import_micromatch2 = __toESM(require("micromatch"));
4928
5100
  var import_js_sha12 = __toESM(require("js-sha1"));
4929
5101
  var createDatabase = (config) => {
4930
5102
  return new Database({
@@ -4947,14 +5119,12 @@ var Database = class {
4947
5119
  } catch (e) {
4948
5120
  }
4949
5121
  };
4950
- this.getGeneratedFolder = () => import_path3.default.join(this.tinaDirectory, "__generated__");
5122
+ this.getGeneratedFolder = () => import_path4.default.join(this.tinaDirectory, "__generated__");
4951
5123
  this.get = async (filepath) => {
4952
5124
  await this.initLevel();
4953
5125
  if (SYSTEM_FILES.includes(filepath)) {
4954
5126
  throw new Error(`Unexpected get for config file ${filepath}`);
4955
5127
  } else {
4956
- const tinaSchema = await this.getSchema(this.level);
4957
- const extension = import_path3.default.extname(filepath);
4958
5128
  const contentObject = await this.level.sublevel(
4959
5129
  CONTENT_ROOT_PREFIX,
4960
5130
  SUBLEVEL_OPTIONS
@@ -4962,40 +5132,11 @@ var Database = class {
4962
5132
  if (!contentObject) {
4963
5133
  throw new import_graphql5.GraphQLError(`Unable to find record ${filepath}`);
4964
5134
  }
4965
- const templateName = hasOwnProperty(contentObject, "_template") && typeof contentObject._template === "string" ? contentObject._template : void 0;
4966
- const { collection, template } = hasOwnProperty(
5135
+ return transformDocument(
5136
+ filepath,
4967
5137
  contentObject,
4968
- "__collection"
4969
- ) ? {
4970
- collection: tinaSchema.getCollection(
4971
- contentObject["__collection"]
4972
- ),
4973
- template: void 0
4974
- } : tinaSchema.getCollectionAndTemplateByFullPath(filepath, templateName);
4975
- const field = template == null ? void 0 : template.fields.find((field2) => {
4976
- if (field2.type === "string" || field2.type === "rich-text") {
4977
- if (field2.isBody) {
4978
- return true;
4979
- }
4980
- }
4981
- return false;
4982
- });
4983
- let data = contentObject;
4984
- if ((extension === ".md" || extension === ".mdx") && field) {
4985
- if (hasOwnProperty(contentObject, "$_body")) {
4986
- const { $_body, ...rest } = contentObject;
4987
- data = rest;
4988
- data[field.name] = $_body;
4989
- }
4990
- }
4991
- return {
4992
- ...data,
4993
- _collection: collection.name,
4994
- _keepTemplateKey: !!collection.templates,
4995
- _template: (template == null ? void 0 : template.namespace) ? lastItem(template == null ? void 0 : template.namespace) : void 0,
4996
- _relativePath: filepath.replace(collection.path, "").replace(/^\/|\/$/g, ""),
4997
- _id: filepath
4998
- };
5138
+ await this.getSchema(this.level)
5139
+ );
4999
5140
  }
5000
5141
  };
5001
5142
  this.addPendingDocument = async (filepath, data) => {
@@ -5094,7 +5235,7 @@ var Database = class {
5094
5235
  }
5095
5236
  if (((_a = collection.match) == null ? void 0 : _a.exclude) || ((_b = collection.match) == null ? void 0 : _b.include)) {
5096
5237
  const matches = this.tinaSchema.getMatches({ collection });
5097
- const match = import_micromatch.default.isMatch(filepath, matches);
5238
+ const match = import_micromatch2.default.isMatch(filepath, matches);
5098
5239
  if (!match) {
5099
5240
  throw new import_graphql5.GraphQLError(
5100
5241
  `File ${filepath} does not match collection ${collection.name} glob ${matches.join(
@@ -5216,7 +5357,7 @@ var Database = class {
5216
5357
  );
5217
5358
  const writeTemplateKey = templateDetails.info.type === "union";
5218
5359
  const aliasedData = applyNameOverrides(templateDetails.template, payload);
5219
- const extension = import_path3.default.extname(filepath);
5360
+ const extension = import_path4.default.extname(filepath);
5220
5361
  const stringifiedFile = stringifyFile(
5221
5362
  aliasedData,
5222
5363
  extension,
@@ -5245,7 +5386,7 @@ var Database = class {
5245
5386
  this.getLookup = async (returnType) => {
5246
5387
  await this.initLevel();
5247
5388
  const lookupPath = (0, import_schema_tools3.normalizePath)(
5248
- import_path3.default.join(this.getGeneratedFolder(), `_lookup.json`)
5389
+ import_path4.default.join(this.getGeneratedFolder(), `_lookup.json`)
5249
5390
  );
5250
5391
  if (!this._lookup) {
5251
5392
  const _lookup = await this.level.sublevel(
@@ -5259,7 +5400,7 @@ var Database = class {
5259
5400
  this.getGraphQLSchema = async () => {
5260
5401
  await this.initLevel();
5261
5402
  const graphqlPath = (0, import_schema_tools3.normalizePath)(
5262
- import_path3.default.join(this.getGeneratedFolder(), `_graphql.json`)
5403
+ import_path4.default.join(this.getGeneratedFolder(), `_graphql.json`)
5263
5404
  );
5264
5405
  return await this.level.sublevel(
5265
5406
  CONTENT_ROOT_PREFIX,
@@ -5271,7 +5412,7 @@ var Database = class {
5271
5412
  throw new Error(`No bridge configured`);
5272
5413
  }
5273
5414
  const graphqlPath = (0, import_schema_tools3.normalizePath)(
5274
- import_path3.default.join(this.getGeneratedFolder(), `_graphql.json`)
5415
+ import_path4.default.join(this.getGeneratedFolder(), `_graphql.json`)
5275
5416
  );
5276
5417
  const _graphql = await this.bridge.get(graphqlPath);
5277
5418
  return JSON.parse(_graphql);
@@ -5279,7 +5420,7 @@ var Database = class {
5279
5420
  this.getTinaSchema = async (level) => {
5280
5421
  await this.initLevel();
5281
5422
  const schemaPath = (0, import_schema_tools3.normalizePath)(
5282
- import_path3.default.join(this.getGeneratedFolder(), `_schema.json`)
5423
+ import_path4.default.join(this.getGeneratedFolder(), `_schema.json`)
5283
5424
  );
5284
5425
  return await (level || this.level).sublevel(
5285
5426
  CONTENT_ROOT_PREFIX,
@@ -5295,7 +5436,7 @@ var Database = class {
5295
5436
  if (!schema) {
5296
5437
  throw new Error(
5297
5438
  `Unable to get schema from level db: ${(0, import_schema_tools3.normalizePath)(
5298
- import_path3.default.join(this.getGeneratedFolder(), `_schema.json`)
5439
+ import_path4.default.join(this.getGeneratedFolder(), `_schema.json`)
5299
5440
  )}`
5300
5441
  );
5301
5442
  }
@@ -5510,7 +5651,7 @@ var Database = class {
5510
5651
  lookup = lookupFromLockFile || JSON.parse(
5511
5652
  await this.bridge.get(
5512
5653
  (0, import_schema_tools3.normalizePath)(
5513
- import_path3.default.join(this.getGeneratedFolder(), "_lookup.json")
5654
+ import_path4.default.join(this.getGeneratedFolder(), "_lookup.json")
5514
5655
  )
5515
5656
  )
5516
5657
  );
@@ -5534,15 +5675,15 @@ var Database = class {
5534
5675
  }
5535
5676
  const contentRootLevel = nextLevel.sublevel(CONTENT_ROOT_PREFIX, SUBLEVEL_OPTIONS);
5536
5677
  await contentRootLevel.put(
5537
- (0, import_schema_tools3.normalizePath)(import_path3.default.join(this.getGeneratedFolder(), "_graphql.json")),
5678
+ (0, import_schema_tools3.normalizePath)(import_path4.default.join(this.getGeneratedFolder(), "_graphql.json")),
5538
5679
  graphQLSchema
5539
5680
  );
5540
5681
  await contentRootLevel.put(
5541
- (0, import_schema_tools3.normalizePath)(import_path3.default.join(this.getGeneratedFolder(), "_schema.json")),
5682
+ (0, import_schema_tools3.normalizePath)(import_path4.default.join(this.getGeneratedFolder(), "_schema.json")),
5542
5683
  tinaSchema.schema
5543
5684
  );
5544
5685
  await contentRootLevel.put(
5545
- (0, import_schema_tools3.normalizePath)(import_path3.default.join(this.getGeneratedFolder(), "_lookup.json")),
5686
+ (0, import_schema_tools3.normalizePath)(import_path4.default.join(this.getGeneratedFolder(), "_lookup.json")),
5546
5687
  lookup
5547
5688
  );
5548
5689
  const result = await this._indexAllContent(
@@ -5573,8 +5714,9 @@ var Database = class {
5573
5714
  await this.level.batch(operations.splice(0, 25));
5574
5715
  }
5575
5716
  };
5717
+ const tinaSchema = await this.getSchema(this.level);
5576
5718
  await this.indexStatusCallbackWrapper(async () => {
5577
- const { pathsByCollection, nonCollectionPaths, collections } = await this.partitionPathsByCollection(documentPaths);
5719
+ const { pathsByCollection, nonCollectionPaths, collections } = await partitionPathsByCollection(tinaSchema, documentPaths);
5578
5720
  for (const collection of Object.keys(pathsByCollection)) {
5579
5721
  await _deleteIndexContent(
5580
5722
  this,
@@ -5600,18 +5742,25 @@ var Database = class {
5600
5742
  await this.level.batch(operations.splice(0, 25));
5601
5743
  }
5602
5744
  };
5745
+ const tinaSchema = await this.getSchema(this.level);
5603
5746
  await this.indexStatusCallbackWrapper(async () => {
5604
- const { pathsByCollection, nonCollectionPaths, collections } = await this.partitionPathsByCollection(documentPaths);
5605
- for (const collection of Object.keys(pathsByCollection)) {
5606
- await _indexContent(
5607
- this,
5608
- this.level,
5609
- pathsByCollection[collection],
5610
- enqueueOps,
5611
- collections[collection]
5612
- );
5613
- }
5614
- await _indexContent(this, this.level, nonCollectionPaths, enqueueOps);
5747
+ await scanContentByPaths(
5748
+ tinaSchema,
5749
+ documentPaths,
5750
+ async (collection, documentPaths2) => {
5751
+ if (collection) {
5752
+ await _indexContent(
5753
+ this,
5754
+ this.level,
5755
+ documentPaths2,
5756
+ enqueueOps,
5757
+ collection
5758
+ );
5759
+ } else {
5760
+ await _indexContent(this, this.level, documentPaths2, enqueueOps);
5761
+ }
5762
+ }
5763
+ );
5615
5764
  });
5616
5765
  while (operations.length) {
5617
5766
  await this.level.batch(operations.splice(0, 25));
@@ -5671,7 +5820,6 @@ var Database = class {
5671
5820
  await this.onDelete((0, import_schema_tools3.normalizePath)(filepath));
5672
5821
  };
5673
5822
  this._indexAllContent = async (level, schema) => {
5674
- const warnings = [];
5675
5823
  const tinaSchema = await this.getSchema(level, schema);
5676
5824
  const operations = [];
5677
5825
  const enqueueOps = async (ops) => {
@@ -5681,33 +5829,13 @@ var Database = class {
5681
5829
  await level.batch(batchOps);
5682
5830
  }
5683
5831
  };
5684
- const filesSeen = /* @__PURE__ */ new Map();
5685
- const duplicateFiles = /* @__PURE__ */ new Set();
5686
- await sequential(tinaSchema.getCollections(), async (collection) => {
5687
- const normalPath = (0, import_schema_tools3.normalizePath)(collection.path);
5688
- const format = collection.format || "md";
5689
- const documentPaths = await this.bridge.glob(normalPath, format);
5690
- const matches = this.tinaSchema.getMatches({ collection });
5691
- const filteredPaths = matches.length > 0 ? (0, import_micromatch.default)(documentPaths, matches) : documentPaths;
5692
- filteredPaths.forEach((path5) => {
5693
- if (filesSeen.has(path5)) {
5694
- filesSeen.get(path5).push(collection.name);
5695
- duplicateFiles.add(path5);
5696
- } else {
5697
- filesSeen.set(path5, [collection.name]);
5698
- }
5699
- });
5700
- duplicateFiles.forEach((path5) => {
5701
- warnings.push(
5702
- `"${path5}" Found in multiple collections: ${filesSeen.get(path5).map((collection2) => `"${collection2}"`).join(
5703
- ", "
5704
- )}. This can cause unexpected behavior. We recommend updating the \`match\` property of those collections so that each file is in only one collection.
5705
- This will be an error in the future. See https://tina.io/docs/errors/file-in-mutpliple-collections/
5706
- `
5707
- );
5708
- });
5709
- await _indexContent(this, level, filteredPaths, enqueueOps, collection);
5710
- });
5832
+ const warnings = await scanAllContent(
5833
+ tinaSchema,
5834
+ this.bridge,
5835
+ async (collection, contentPaths) => {
5836
+ await _indexContent(this, level, contentPaths, enqueueOps, collection);
5837
+ }
5838
+ );
5711
5839
  while (operations.length) {
5712
5840
  await level.batch(operations.splice(0, 25));
5713
5841
  }
@@ -5720,24 +5848,6 @@ This will be an error in the future. See https://tina.io/docs/errors/file-in-mut
5720
5848
  this.onPut = config.onPut || defaultOnPut;
5721
5849
  this.onDelete = config.onDelete || defaultOnDelete;
5722
5850
  }
5723
- async partitionPathsByCollection(documentPaths) {
5724
- const pathsByCollection = {};
5725
- const nonCollectionPaths = [];
5726
- const collections = {};
5727
- for (const documentPath of documentPaths) {
5728
- const collection = await this.collectionForPath(documentPath);
5729
- if (collection) {
5730
- if (!pathsByCollection[collection.name]) {
5731
- pathsByCollection[collection.name] = [];
5732
- }
5733
- collections[collection.name] = collection;
5734
- pathsByCollection[collection.name].push(documentPath);
5735
- } else {
5736
- nonCollectionPaths.push(documentPath);
5737
- }
5738
- }
5739
- return { pathsByCollection, nonCollectionPaths, collections };
5740
- }
5741
5851
  async updateDatabaseVersion(version) {
5742
5852
  const metadataLevel = this.rootLevel.sublevel("_metadata", SUBLEVEL_OPTIONS);
5743
5853
  await metadataLevel.put("metadata", { version });
@@ -5813,9 +5923,6 @@ This will be an error in the future. See https://tina.io/docs/errors/file-in-mut
5813
5923
  }
5814
5924
  }
5815
5925
  };
5816
- function hasOwnProperty(obj, prop) {
5817
- return obj.hasOwnProperty(prop);
5818
- }
5819
5926
  var _indexContent = async (database, level, documentPaths, enqueueOps, collection) => {
5820
5927
  let collectionIndexDefinitions;
5821
5928
  let collectionPath;
@@ -5835,29 +5942,17 @@ var _indexContent = async (database, level, documentPaths, enqueueOps, collectio
5835
5942
  const folderTreeBuilder = new FolderTreeBuilder();
5836
5943
  await sequential(documentPaths, async (filepath) => {
5837
5944
  try {
5838
- const dataString = await database.bridge.get((0, import_schema_tools3.normalizePath)(filepath));
5839
- const data = parseFile(
5840
- dataString,
5841
- import_path3.default.extname(filepath),
5842
- (yup3) => yup3.object({}),
5843
- {
5844
- frontmatterDelimiters: collection == null ? void 0 : collection.frontmatterDelimiters,
5845
- frontmatterFormat: collection == null ? void 0 : collection.frontmatterFormat
5846
- }
5945
+ const aliasedData = await loadAndParseWithAliases(
5946
+ database.bridge,
5947
+ filepath,
5948
+ collection,
5949
+ templateInfo
5847
5950
  );
5848
- const template = getTemplateForFile(templateInfo, data);
5849
- if (!template) {
5850
- console.warn(
5851
- `Document: ${filepath} has an ambiguous template, skipping from indexing`
5852
- );
5853
- return;
5854
- }
5855
5951
  const normalizedPath = (0, import_schema_tools3.normalizePath)(filepath);
5856
5952
  const folderKey = folderTreeBuilder.update(
5857
5953
  normalizedPath,
5858
5954
  collectionPath || ""
5859
5955
  );
5860
- const aliasedData = templateInfo ? replaceNameOverrides(template, data) : data;
5861
5956
  await enqueueOps([
5862
5957
  ...makeIndexOpsForDocument(
5863
5958
  normalizedPath,
@@ -5973,27 +6068,6 @@ var _deleteIndexContent = async (database, documentPaths, enqueueOps, collection
5973
6068
  );
5974
6069
  }
5975
6070
  };
5976
- var getTemplateForFile = (templateInfo, data) => {
5977
- if ((templateInfo == null ? void 0 : templateInfo.type) === "object") {
5978
- return templateInfo.template;
5979
- }
5980
- if ((templateInfo == null ? void 0 : templateInfo.type) === "union") {
5981
- if (hasOwnProperty(data, "_template")) {
5982
- const template = templateInfo.templates.find(
5983
- (t) => lastItem(t.namespace) === data._template
5984
- );
5985
- if (!template) {
5986
- throw new Error(
5987
- `Unable to find template "${data._template}". Possible templates are: ${templateInfo.templates.map((template2) => `"${template2.name}"`).join(", ")}.`
5988
- );
5989
- }
5990
- return template;
5991
- } else {
5992
- return void 0;
5993
- }
5994
- }
5995
- throw new Error(`Unable to determine template`);
5996
- };
5997
6071
 
5998
6072
  // src/level/tinaLevel.ts
5999
6073
  var import_many_level = require("many-level");
@@ -6019,7 +6093,7 @@ var TinaLevelClient = class extends import_many_level.ManyLevelGuest {
6019
6093
  // src/database/bridge/filesystem.ts
6020
6094
  var import_fs_extra = __toESM(require("fs-extra"));
6021
6095
  var import_fast_glob = __toESM(require("fast-glob"));
6022
- var import_path4 = __toESM(require("path"));
6096
+ var import_path5 = __toESM(require("path"));
6023
6097
  var import_normalize_path = __toESM(require("normalize-path"));
6024
6098
  var FilesystemBridge = class {
6025
6099
  constructor(rootPath, outputPath) {
@@ -6027,9 +6101,9 @@ var FilesystemBridge = class {
6027
6101
  this.outputPath = outputPath || rootPath;
6028
6102
  }
6029
6103
  async glob(pattern, extension) {
6030
- const basePath = import_path4.default.join(this.outputPath, ...pattern.split("/"));
6104
+ const basePath = import_path5.default.join(this.outputPath, ...pattern.split("/"));
6031
6105
  const items = await (0, import_fast_glob.default)(
6032
- import_path4.default.join(basePath, "**", `/*.${extension}`).replace(/\\/g, "/"),
6106
+ import_path5.default.join(basePath, "**", `/*.${extension}`).replace(/\\/g, "/"),
6033
6107
  {
6034
6108
  dot: true,
6035
6109
  ignore: ["**/node_modules/**"]
@@ -6041,14 +6115,14 @@ var FilesystemBridge = class {
6041
6115
  });
6042
6116
  }
6043
6117
  async delete(filepath) {
6044
- await import_fs_extra.default.remove(import_path4.default.join(this.outputPath, filepath));
6118
+ await import_fs_extra.default.remove(import_path5.default.join(this.outputPath, filepath));
6045
6119
  }
6046
6120
  async get(filepath) {
6047
- return import_fs_extra.default.readFileSync(import_path4.default.join(this.outputPath, filepath)).toString();
6121
+ return import_fs_extra.default.readFileSync(import_path5.default.join(this.outputPath, filepath)).toString();
6048
6122
  }
6049
6123
  async put(filepath, data, basePathOverride) {
6050
6124
  const basePath = basePathOverride || this.outputPath;
6051
- await import_fs_extra.default.outputFileSync(import_path4.default.join(basePath, filepath), data);
6125
+ await import_fs_extra.default.outputFileSync(import_path5.default.join(basePath, filepath), data);
6052
6126
  }
6053
6127
  };
6054
6128
  var AuditFileSystemBridge = class extends FilesystemBridge {
@@ -6073,7 +6147,7 @@ var import_fs_extra2 = __toESM(require("fs-extra"));
6073
6147
  var import_glob_parent = __toESM(require("glob-parent"));
6074
6148
  var import_normalize_path2 = __toESM(require("normalize-path"));
6075
6149
  var import_graphql6 = require("graphql");
6076
- var import_path5 = require("path");
6150
+ var import_path6 = require("path");
6077
6151
  var flat = typeof Array.prototype.flat === "undefined" ? (entries) => entries.reduce((acc, x) => acc.concat(x), []) : (entries) => entries.flat();
6078
6152
  var toUint8Array = (buf) => {
6079
6153
  const ab = new ArrayBuffer(buf.length);
@@ -6132,7 +6206,7 @@ var IsomorphicBridge = class {
6132
6206
  async listEntries({
6133
6207
  pattern,
6134
6208
  entry,
6135
- path: path5,
6209
+ path: path6,
6136
6210
  results
6137
6211
  }) {
6138
6212
  const treeResult = await import_isomorphic_git.default.readTree({
@@ -6142,7 +6216,7 @@ var IsomorphicBridge = class {
6142
6216
  });
6143
6217
  const children = [];
6144
6218
  for (const childEntry of treeResult.tree) {
6145
- const childPath = path5 ? `${path5}/${childEntry.path}` : childEntry.path;
6219
+ const childPath = path6 ? `${path6}/${childEntry.path}` : childEntry.path;
6146
6220
  if (childEntry.type === "tree") {
6147
6221
  children.push(childEntry);
6148
6222
  } else {
@@ -6152,7 +6226,7 @@ var IsomorphicBridge = class {
6152
6226
  }
6153
6227
  }
6154
6228
  for (const childEntry of children) {
6155
- const childPath = path5 ? `${path5}/${childEntry.path}` : childEntry.path;
6229
+ const childPath = path6 ? `${path6}/${childEntry.path}` : childEntry.path;
6156
6230
  await this.listEntries({
6157
6231
  pattern,
6158
6232
  entry: childEntry,
@@ -6161,17 +6235,17 @@ var IsomorphicBridge = class {
6161
6235
  });
6162
6236
  }
6163
6237
  }
6164
- async resolvePathEntries(path5, ref) {
6165
- let pathParts = path5.split("/");
6238
+ async resolvePathEntries(path6, ref) {
6239
+ let pathParts = path6.split("/");
6166
6240
  const result = await import_isomorphic_git.default.walk({
6167
6241
  ...this.isomorphicConfig,
6168
6242
  map: async (filepath, [head]) => {
6169
6243
  if (head._fullpath === ".") {
6170
6244
  return head;
6171
6245
  }
6172
- if (path5.startsWith(filepath)) {
6173
- if ((0, import_path5.dirname)(path5) === (0, import_path5.dirname)(filepath)) {
6174
- if (path5 === filepath) {
6246
+ if (path6.startsWith(filepath)) {
6247
+ if ((0, import_path6.dirname)(path6) === (0, import_path6.dirname)(filepath)) {
6248
+ if (path6 === filepath) {
6175
6249
  return head;
6176
6250
  }
6177
6251
  } else {
@@ -6191,7 +6265,7 @@ var IsomorphicBridge = class {
6191
6265
  }
6192
6266
  return { pathParts, pathEntries };
6193
6267
  }
6194
- async updateTreeHierarchy(existingOid, updatedOid, path5, type, pathEntries, pathParts) {
6268
+ async updateTreeHierarchy(existingOid, updatedOid, path6, type, pathEntries, pathParts) {
6195
6269
  const lastIdx = pathEntries.length - 1;
6196
6270
  const parentEntry = pathEntries[lastIdx];
6197
6271
  const parentPath = pathParts[lastIdx];
@@ -6206,7 +6280,7 @@ var IsomorphicBridge = class {
6206
6280
  cache: this.cache
6207
6281
  });
6208
6282
  tree = existingOid ? treeResult.tree.map((entry) => {
6209
- if (entry.path === path5) {
6283
+ if (entry.path === path6) {
6210
6284
  entry.oid = updatedOid;
6211
6285
  }
6212
6286
  return entry;
@@ -6215,7 +6289,7 @@ var IsomorphicBridge = class {
6215
6289
  {
6216
6290
  oid: updatedOid,
6217
6291
  type,
6218
- path: path5,
6292
+ path: path6,
6219
6293
  mode
6220
6294
  }
6221
6295
  ];
@@ -6224,7 +6298,7 @@ var IsomorphicBridge = class {
6224
6298
  {
6225
6299
  oid: updatedOid,
6226
6300
  type,
6227
- path: path5,
6301
+ path: path6,
6228
6302
  mode
6229
6303
  }
6230
6304
  ];
@@ -6325,7 +6399,7 @@ var IsomorphicBridge = class {
6325
6399
  path: parentPath,
6326
6400
  results
6327
6401
  });
6328
- return results.map((path5) => this.unqualifyPath(path5)).filter((path5) => path5.endsWith(extension));
6402
+ return results.map((path6) => this.unqualifyPath(path6)).filter((path6) => path6.endsWith(extension));
6329
6403
  }
6330
6404
  async delete(filepath) {
6331
6405
  const ref = await this.getRef();
@@ -6477,8 +6551,13 @@ var buildSchema = async (config, flags) => {
6477
6551
  createDatabase,
6478
6552
  createSchema,
6479
6553
  handleFetchErrorError,
6554
+ loadAndParseWithAliases,
6480
6555
  parseFile,
6481
6556
  resolve,
6557
+ scanAllContent,
6558
+ scanContentByPaths,
6482
6559
  sequential,
6483
- stringifyFile
6560
+ stringifyFile,
6561
+ transformDocument,
6562
+ transformDocumentIntoPayload
6484
6563
  });