@tinacms/graphql 1.4.14 → 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.14",
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(
@@ -4572,6 +4860,9 @@ var resolve = async ({
4572
4860
  try {
4573
4861
  const verboseValue = verbose != null ? verbose : true;
4574
4862
  const graphQLSchemaAst = await database.getGraphQLSchema();
4863
+ if (!graphQLSchemaAst) {
4864
+ throw new import_graphql4.GraphQLError(`GraphQL schema not found`);
4865
+ }
4575
4866
  const graphQLSchema = (0, import_graphql4.buildASTSchema)(graphQLSchemaAst);
4576
4867
  const tinaConfig = await database.getTinaSchema();
4577
4868
  const tinaSchema = await createSchema({
@@ -4803,125 +5094,9 @@ var resolve = async ({
4803
5094
  };
4804
5095
 
4805
5096
  // src/database/index.ts
4806
- var import_path3 = __toESM(require("path"));
5097
+ var import_path4 = __toESM(require("path"));
4807
5098
  var import_graphql5 = require("graphql");
4808
- var import_micromatch = __toESM(require("micromatch"));
4809
-
4810
- // src/database/alias-utils.ts
4811
- var replaceBlockAliases = (template, item) => {
4812
- const output = { ...item };
4813
- const templateKey = template.templateKey || "_template";
4814
- const templateName = output[templateKey];
4815
- const matchingTemplate = template.templates.find(
4816
- (t) => t.nameOverride == templateName || t.name == templateName
4817
- );
4818
- if (!matchingTemplate) {
4819
- throw new Error(
4820
- `Block template "${templateName}" is not defined for field "${template.name}"`
4821
- );
4822
- }
4823
- output._template = matchingTemplate.name;
4824
- if (templateKey != "_template") {
4825
- delete output[templateKey];
4826
- }
4827
- return output;
4828
- };
4829
- var replaceNameOverrides = (template, obj) => {
4830
- if (template.list) {
4831
- return obj.map((item) => {
4832
- if (isBlockField(template)) {
4833
- item = replaceBlockAliases(template, item);
4834
- }
4835
- return _replaceNameOverrides(
4836
- getTemplateForData(template, item).fields,
4837
- item
4838
- );
4839
- });
4840
- } else {
4841
- return _replaceNameOverrides(getTemplateForData(template, obj).fields, obj);
4842
- }
4843
- };
4844
- function isBlockField(field) {
4845
- var _a;
4846
- return field && field.type === "object" && ((_a = field.templates) == null ? void 0 : _a.length) > 0;
4847
- }
4848
- var _replaceNameOverrides = (fields, obj) => {
4849
- const output = {};
4850
- Object.keys(obj).forEach((key) => {
4851
- const field = fields.find(
4852
- (fieldWithMatchingAlias) => ((fieldWithMatchingAlias == null ? void 0 : fieldWithMatchingAlias.nameOverride) || (fieldWithMatchingAlias == null ? void 0 : fieldWithMatchingAlias.name)) === key
4853
- );
4854
- output[(field == null ? void 0 : field.name) || key] = (field == null ? void 0 : field.type) == "object" ? replaceNameOverrides(field, obj[key]) : obj[key];
4855
- });
4856
- return output;
4857
- };
4858
- var getTemplateForData = (field, data) => {
4859
- var _a;
4860
- if ((_a = field.templates) == null ? void 0 : _a.length) {
4861
- const templateKey = "_template";
4862
- if (data[templateKey]) {
4863
- const result = field.templates.find(
4864
- (template) => template.nameOverride === data[templateKey] || template.name === data[templateKey]
4865
- );
4866
- if (result) {
4867
- return result;
4868
- }
4869
- throw new Error(
4870
- `Template "${data[templateKey]}" is not defined for field "${field.name}"`
4871
- );
4872
- }
4873
- throw new Error(
4874
- `Missing required key "${templateKey}" on field "${field.name}"`
4875
- );
4876
- } else {
4877
- return field;
4878
- }
4879
- };
4880
- var applyBlockAliases = (template, item) => {
4881
- const output = { ...item };
4882
- const templateKey = template.templateKey || "_template";
4883
- const templateName = output._template;
4884
- const matchingTemplate = template.templates.find(
4885
- (t) => t.nameOverride == templateName || t.name == templateName
4886
- );
4887
- if (!matchingTemplate) {
4888
- throw new Error(
4889
- `Block template "${templateName}" is not defined for field "${template.name}"`
4890
- );
4891
- }
4892
- output[templateKey] = matchingTemplate.nameOverride || matchingTemplate.name;
4893
- if (templateKey != "_template") {
4894
- delete output._template;
4895
- }
4896
- return output;
4897
- };
4898
- var applyNameOverrides = (template, obj) => {
4899
- if (template.list) {
4900
- return obj.map((item) => {
4901
- let result = _applyNameOverrides(
4902
- getTemplateForData(template, item).fields,
4903
- item
4904
- );
4905
- if (isBlockField(template)) {
4906
- result = applyBlockAliases(template, result);
4907
- }
4908
- return result;
4909
- });
4910
- } else {
4911
- return _applyNameOverrides(getTemplateForData(template, obj).fields, obj);
4912
- }
4913
- };
4914
- var _applyNameOverrides = (fields, obj) => {
4915
- const output = {};
4916
- Object.keys(obj).forEach((key) => {
4917
- const field = fields.find((field2) => field2.name === key);
4918
- const outputKey = (field == null ? void 0 : field.nameOverride) || key;
4919
- output[outputKey] = (field == null ? void 0 : field.type) === "object" ? applyNameOverrides(field, obj[key]) : obj[key];
4920
- });
4921
- return output;
4922
- };
4923
-
4924
- // src/database/index.ts
5099
+ var import_micromatch2 = __toESM(require("micromatch"));
4925
5100
  var import_js_sha12 = __toESM(require("js-sha1"));
4926
5101
  var createDatabase = (config) => {
4927
5102
  return new Database({
@@ -4939,16 +5114,17 @@ var Database = class {
4939
5114
  this.config = config;
4940
5115
  this.collectionForPath = async (filepath) => {
4941
5116
  const tinaSchema = await this.getSchema(this.level);
4942
- return tinaSchema.getCollectionByFullPath(filepath);
5117
+ try {
5118
+ return tinaSchema.getCollectionByFullPath(filepath);
5119
+ } catch (e) {
5120
+ }
4943
5121
  };
4944
- this.getGeneratedFolder = () => import_path3.default.join(this.tinaDirectory, "__generated__");
5122
+ this.getGeneratedFolder = () => import_path4.default.join(this.tinaDirectory, "__generated__");
4945
5123
  this.get = async (filepath) => {
4946
5124
  await this.initLevel();
4947
5125
  if (SYSTEM_FILES.includes(filepath)) {
4948
5126
  throw new Error(`Unexpected get for config file ${filepath}`);
4949
5127
  } else {
4950
- const tinaSchema = await this.getSchema(this.level);
4951
- const extension = import_path3.default.extname(filepath);
4952
5128
  const contentObject = await this.level.sublevel(
4953
5129
  CONTENT_ROOT_PREFIX,
4954
5130
  SUBLEVEL_OPTIONS
@@ -4956,56 +5132,27 @@ var Database = class {
4956
5132
  if (!contentObject) {
4957
5133
  throw new import_graphql5.GraphQLError(`Unable to find record ${filepath}`);
4958
5134
  }
4959
- const templateName = hasOwnProperty(contentObject, "_template") && typeof contentObject._template === "string" ? contentObject._template : void 0;
4960
- const { collection, template } = hasOwnProperty(
5135
+ return transformDocument(
5136
+ filepath,
4961
5137
  contentObject,
4962
- "__collection"
4963
- ) ? {
4964
- collection: tinaSchema.getCollection(
4965
- contentObject["__collection"]
4966
- ),
4967
- template: void 0
4968
- } : tinaSchema.getCollectionAndTemplateByFullPath(filepath, templateName);
4969
- const field = template == null ? void 0 : template.fields.find((field2) => {
4970
- if (field2.type === "string" || field2.type === "rich-text") {
4971
- if (field2.isBody) {
4972
- return true;
4973
- }
4974
- }
4975
- return false;
4976
- });
4977
- let data = contentObject;
4978
- if ((extension === ".md" || extension === ".mdx") && field) {
4979
- if (hasOwnProperty(contentObject, "$_body")) {
4980
- const { $_body, ...rest } = contentObject;
4981
- data = rest;
4982
- data[field.name] = $_body;
4983
- }
4984
- }
4985
- return {
4986
- ...data,
4987
- _collection: collection.name,
4988
- _keepTemplateKey: !!collection.templates,
4989
- _template: (template == null ? void 0 : template.namespace) ? lastItem(template == null ? void 0 : template.namespace) : void 0,
4990
- _relativePath: filepath.replace(collection.path, "").replace(/^\/|\/$/g, ""),
4991
- _id: filepath
4992
- };
5138
+ await this.getSchema(this.level)
5139
+ );
4993
5140
  }
4994
5141
  };
4995
5142
  this.addPendingDocument = async (filepath, data) => {
4996
5143
  await this.initLevel();
4997
5144
  const dataFields = await this.formatBodyOnPayload(filepath, data);
4998
5145
  const collection = await this.collectionForPath(filepath);
5146
+ if (!collection) {
5147
+ throw new import_graphql5.GraphQLError(`Unable to find collection for ${filepath}`);
5148
+ }
4999
5149
  const stringifiedFile = await this.stringifyFile(
5000
5150
  filepath,
5001
5151
  dataFields,
5002
5152
  collection
5003
5153
  );
5004
- let collectionIndexDefinitions;
5005
- if (collection) {
5006
- const indexDefinitions = await this.getIndexDefinitions(this.level);
5007
- collectionIndexDefinitions = indexDefinitions == null ? void 0 : indexDefinitions[collection.name];
5008
- }
5154
+ const indexDefinitions = await this.getIndexDefinitions(this.level);
5155
+ const collectionIndexDefinitions = indexDefinitions == null ? void 0 : indexDefinitions[collection.name];
5009
5156
  const normalizedPath = (0, import_schema_tools3.normalizePath)(filepath);
5010
5157
  if (this.bridge) {
5011
5158
  await this.bridge.put(normalizedPath, stringifiedFile);
@@ -5083,9 +5230,12 @@ var Database = class {
5083
5230
  const normalizedPath = (0, import_schema_tools3.normalizePath)(filepath);
5084
5231
  const dataFields = await this.formatBodyOnPayload(filepath, data);
5085
5232
  const collection = await this.collectionForPath(filepath);
5233
+ if (!collection) {
5234
+ throw new import_graphql5.GraphQLError(`Unable to find collection for ${filepath}.`);
5235
+ }
5086
5236
  if (((_a = collection.match) == null ? void 0 : _a.exclude) || ((_b = collection.match) == null ? void 0 : _b.include)) {
5087
5237
  const matches = this.tinaSchema.getMatches({ collection });
5088
- const match = import_micromatch.default.isMatch(filepath, matches);
5238
+ const match = import_micromatch2.default.isMatch(filepath, matches);
5089
5239
  if (!match) {
5090
5240
  throw new import_graphql5.GraphQLError(
5091
5241
  `File ${filepath} does not match collection ${collection.name} glob ${matches.join(
@@ -5174,8 +5324,10 @@ var Database = class {
5174
5324
  }
5175
5325
  };
5176
5326
  this.formatBodyOnPayload = async (filepath, data) => {
5177
- const tinaSchema = await this.getSchema(this.level);
5178
- const collection = tinaSchema.getCollectionByFullPath(filepath);
5327
+ const collection = await this.collectionForPath(filepath);
5328
+ if (!collection) {
5329
+ throw new Error(`Unable to find collection for path ${filepath}`);
5330
+ }
5179
5331
  const { template } = await this.getTemplateDetailsForFile(collection, data);
5180
5332
  const bodyField = template.fields.find((field) => {
5181
5333
  if (field.type === "string" || field.type === "rich-text") {
@@ -5205,7 +5357,7 @@ var Database = class {
5205
5357
  );
5206
5358
  const writeTemplateKey = templateDetails.info.type === "union";
5207
5359
  const aliasedData = applyNameOverrides(templateDetails.template, payload);
5208
- const extension = import_path3.default.extname(filepath);
5360
+ const extension = import_path4.default.extname(filepath);
5209
5361
  const stringifiedFile = stringifyFile(
5210
5362
  aliasedData,
5211
5363
  extension,
@@ -5221,6 +5373,9 @@ var Database = class {
5221
5373
  const data = await this.get(filepath);
5222
5374
  const dataFields = await this.formatBodyOnPayload(filepath, data);
5223
5375
  const collection = await this.collectionForPath(filepath);
5376
+ if (!collection) {
5377
+ throw new Error(`Unable to find collection for path ${filepath}`);
5378
+ }
5224
5379
  const stringifiedFile = await this.stringifyFile(
5225
5380
  filepath,
5226
5381
  dataFields,
@@ -5231,7 +5386,7 @@ var Database = class {
5231
5386
  this.getLookup = async (returnType) => {
5232
5387
  await this.initLevel();
5233
5388
  const lookupPath = (0, import_schema_tools3.normalizePath)(
5234
- import_path3.default.join(this.getGeneratedFolder(), `_lookup.json`)
5389
+ import_path4.default.join(this.getGeneratedFolder(), `_lookup.json`)
5235
5390
  );
5236
5391
  if (!this._lookup) {
5237
5392
  const _lookup = await this.level.sublevel(
@@ -5245,7 +5400,7 @@ var Database = class {
5245
5400
  this.getGraphQLSchema = async () => {
5246
5401
  await this.initLevel();
5247
5402
  const graphqlPath = (0, import_schema_tools3.normalizePath)(
5248
- import_path3.default.join(this.getGeneratedFolder(), `_graphql.json`)
5403
+ import_path4.default.join(this.getGeneratedFolder(), `_graphql.json`)
5249
5404
  );
5250
5405
  return await this.level.sublevel(
5251
5406
  CONTENT_ROOT_PREFIX,
@@ -5257,7 +5412,7 @@ var Database = class {
5257
5412
  throw new Error(`No bridge configured`);
5258
5413
  }
5259
5414
  const graphqlPath = (0, import_schema_tools3.normalizePath)(
5260
- import_path3.default.join(this.getGeneratedFolder(), `_graphql.json`)
5415
+ import_path4.default.join(this.getGeneratedFolder(), `_graphql.json`)
5261
5416
  );
5262
5417
  const _graphql = await this.bridge.get(graphqlPath);
5263
5418
  return JSON.parse(_graphql);
@@ -5265,7 +5420,7 @@ var Database = class {
5265
5420
  this.getTinaSchema = async (level) => {
5266
5421
  await this.initLevel();
5267
5422
  const schemaPath = (0, import_schema_tools3.normalizePath)(
5268
- import_path3.default.join(this.getGeneratedFolder(), `_schema.json`)
5423
+ import_path4.default.join(this.getGeneratedFolder(), `_schema.json`)
5269
5424
  );
5270
5425
  return await (level || this.level).sublevel(
5271
5426
  CONTENT_ROOT_PREFIX,
@@ -5281,7 +5436,7 @@ var Database = class {
5281
5436
  if (!schema) {
5282
5437
  throw new Error(
5283
5438
  `Unable to get schema from level db: ${(0, import_schema_tools3.normalizePath)(
5284
- import_path3.default.join(this.getGeneratedFolder(), `_schema.json`)
5439
+ import_path4.default.join(this.getGeneratedFolder(), `_schema.json`)
5285
5440
  )}`
5286
5441
  );
5287
5442
  }
@@ -5496,7 +5651,7 @@ var Database = class {
5496
5651
  lookup = lookupFromLockFile || JSON.parse(
5497
5652
  await this.bridge.get(
5498
5653
  (0, import_schema_tools3.normalizePath)(
5499
- import_path3.default.join(this.getGeneratedFolder(), "_lookup.json")
5654
+ import_path4.default.join(this.getGeneratedFolder(), "_lookup.json")
5500
5655
  )
5501
5656
  )
5502
5657
  );
@@ -5520,15 +5675,15 @@ var Database = class {
5520
5675
  }
5521
5676
  const contentRootLevel = nextLevel.sublevel(CONTENT_ROOT_PREFIX, SUBLEVEL_OPTIONS);
5522
5677
  await contentRootLevel.put(
5523
- (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")),
5524
5679
  graphQLSchema
5525
5680
  );
5526
5681
  await contentRootLevel.put(
5527
- (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")),
5528
5683
  tinaSchema.schema
5529
5684
  );
5530
5685
  await contentRootLevel.put(
5531
- (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")),
5532
5687
  lookup
5533
5688
  );
5534
5689
  const result = await this._indexAllContent(
@@ -5559,8 +5714,9 @@ var Database = class {
5559
5714
  await this.level.batch(operations.splice(0, 25));
5560
5715
  }
5561
5716
  };
5717
+ const tinaSchema = await this.getSchema(this.level);
5562
5718
  await this.indexStatusCallbackWrapper(async () => {
5563
- const { pathsByCollection, nonCollectionPaths, collections } = await this.partitionPathsByCollection(documentPaths);
5719
+ const { pathsByCollection, nonCollectionPaths, collections } = await partitionPathsByCollection(tinaSchema, documentPaths);
5564
5720
  for (const collection of Object.keys(pathsByCollection)) {
5565
5721
  await _deleteIndexContent(
5566
5722
  this,
@@ -5586,18 +5742,25 @@ var Database = class {
5586
5742
  await this.level.batch(operations.splice(0, 25));
5587
5743
  }
5588
5744
  };
5745
+ const tinaSchema = await this.getSchema(this.level);
5589
5746
  await this.indexStatusCallbackWrapper(async () => {
5590
- const { pathsByCollection, nonCollectionPaths, collections } = await this.partitionPathsByCollection(documentPaths);
5591
- for (const collection of Object.keys(pathsByCollection)) {
5592
- await _indexContent(
5593
- this,
5594
- this.level,
5595
- pathsByCollection[collection],
5596
- enqueueOps,
5597
- collections[collection]
5598
- );
5599
- }
5600
- 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
+ );
5601
5764
  });
5602
5765
  while (operations.length) {
5603
5766
  await this.level.batch(operations.splice(0, 25));
@@ -5606,11 +5769,11 @@ var Database = class {
5606
5769
  this.delete = async (filepath) => {
5607
5770
  await this.initLevel();
5608
5771
  const collection = await this.collectionForPath(filepath);
5609
- let collectionIndexDefinitions;
5610
- if (collection) {
5611
- const indexDefinitions = await this.getIndexDefinitions(this.level);
5612
- collectionIndexDefinitions = indexDefinitions == null ? void 0 : indexDefinitions[collection.name];
5772
+ if (!collection) {
5773
+ throw new Error(`No collection found for path: ${filepath}`);
5613
5774
  }
5775
+ const indexDefinitions = await this.getIndexDefinitions(this.level);
5776
+ const collectionIndexDefinitions = indexDefinitions == null ? void 0 : indexDefinitions[collection.name];
5614
5777
  this.level.sublevel(
5615
5778
  CONTENT_ROOT_PREFIX,
5616
5779
  SUBLEVEL_OPTIONS
@@ -5657,7 +5820,6 @@ var Database = class {
5657
5820
  await this.onDelete((0, import_schema_tools3.normalizePath)(filepath));
5658
5821
  };
5659
5822
  this._indexAllContent = async (level, schema) => {
5660
- const warnings = [];
5661
5823
  const tinaSchema = await this.getSchema(level, schema);
5662
5824
  const operations = [];
5663
5825
  const enqueueOps = async (ops) => {
@@ -5667,33 +5829,13 @@ var Database = class {
5667
5829
  await level.batch(batchOps);
5668
5830
  }
5669
5831
  };
5670
- const filesSeen = /* @__PURE__ */ new Map();
5671
- const duplicateFiles = /* @__PURE__ */ new Set();
5672
- await sequential(tinaSchema.getCollections(), async (collection) => {
5673
- const normalPath = (0, import_schema_tools3.normalizePath)(collection.path);
5674
- const format = collection.format || "md";
5675
- const documentPaths = await this.bridge.glob(normalPath, format);
5676
- const matches = this.tinaSchema.getMatches({ collection });
5677
- const filteredPaths = matches.length > 0 ? (0, import_micromatch.default)(documentPaths, matches) : documentPaths;
5678
- filteredPaths.forEach((path5) => {
5679
- if (filesSeen.has(path5)) {
5680
- filesSeen.get(path5).push(collection.name);
5681
- duplicateFiles.add(path5);
5682
- } else {
5683
- filesSeen.set(path5, [collection.name]);
5684
- }
5685
- });
5686
- duplicateFiles.forEach((path5) => {
5687
- warnings.push(
5688
- `"${path5}" Found in multiple collections: ${filesSeen.get(path5).map((collection2) => `"${collection2}"`).join(
5689
- ", "
5690
- )}. This can cause unexpected behavior. We recommend updating the \`match\` property of those collections so that each file is in only one collection.
5691
- This will be an error in the future. See https://tina.io/docs/errors/file-in-mutpliple-collections/
5692
- `
5693
- );
5694
- });
5695
- await _indexContent(this, level, filteredPaths, enqueueOps, collection);
5696
- });
5832
+ const warnings = await scanAllContent(
5833
+ tinaSchema,
5834
+ this.bridge,
5835
+ async (collection, contentPaths) => {
5836
+ await _indexContent(this, level, contentPaths, enqueueOps, collection);
5837
+ }
5838
+ );
5697
5839
  while (operations.length) {
5698
5840
  await level.batch(operations.splice(0, 25));
5699
5841
  }
@@ -5706,24 +5848,6 @@ This will be an error in the future. See https://tina.io/docs/errors/file-in-mut
5706
5848
  this.onPut = config.onPut || defaultOnPut;
5707
5849
  this.onDelete = config.onDelete || defaultOnDelete;
5708
5850
  }
5709
- async partitionPathsByCollection(documentPaths) {
5710
- const pathsByCollection = {};
5711
- const nonCollectionPaths = [];
5712
- const collections = {};
5713
- for (const documentPath of documentPaths) {
5714
- const collection = await this.collectionForPath(documentPath);
5715
- if (collection) {
5716
- if (!pathsByCollection[collection.name]) {
5717
- pathsByCollection[collection.name] = [];
5718
- }
5719
- collections[collection.name] = collection;
5720
- pathsByCollection[collection.name].push(documentPath);
5721
- } else {
5722
- nonCollectionPaths.push(documentPath);
5723
- }
5724
- }
5725
- return { pathsByCollection, nonCollectionPaths, collections };
5726
- }
5727
5851
  async updateDatabaseVersion(version) {
5728
5852
  const metadataLevel = this.rootLevel.sublevel("_metadata", SUBLEVEL_OPTIONS);
5729
5853
  await metadataLevel.put("metadata", { version });
@@ -5743,7 +5867,10 @@ This will be an error in the future. See https://tina.io/docs/errors/file-in-mut
5743
5867
  let version = await this.getDatabaseVersion();
5744
5868
  if (!version) {
5745
5869
  version = "";
5746
- await this.updateDatabaseVersion(version);
5870
+ try {
5871
+ await this.updateDatabaseVersion(version);
5872
+ } catch (e) {
5873
+ }
5747
5874
  }
5748
5875
  this.level = this.rootLevel.sublevel(version, SUBLEVEL_OPTIONS);
5749
5876
  }
@@ -5796,9 +5923,6 @@ This will be an error in the future. See https://tina.io/docs/errors/file-in-mut
5796
5923
  }
5797
5924
  }
5798
5925
  };
5799
- function hasOwnProperty(obj, prop) {
5800
- return obj.hasOwnProperty(prop);
5801
- }
5802
5926
  var _indexContent = async (database, level, documentPaths, enqueueOps, collection) => {
5803
5927
  let collectionIndexDefinitions;
5804
5928
  let collectionPath;
@@ -5818,29 +5942,17 @@ var _indexContent = async (database, level, documentPaths, enqueueOps, collectio
5818
5942
  const folderTreeBuilder = new FolderTreeBuilder();
5819
5943
  await sequential(documentPaths, async (filepath) => {
5820
5944
  try {
5821
- const dataString = await database.bridge.get((0, import_schema_tools3.normalizePath)(filepath));
5822
- const data = parseFile(
5823
- dataString,
5824
- import_path3.default.extname(filepath),
5825
- (yup3) => yup3.object({}),
5826
- {
5827
- frontmatterDelimiters: collection == null ? void 0 : collection.frontmatterDelimiters,
5828
- frontmatterFormat: collection == null ? void 0 : collection.frontmatterFormat
5829
- }
5945
+ const aliasedData = await loadAndParseWithAliases(
5946
+ database.bridge,
5947
+ filepath,
5948
+ collection,
5949
+ templateInfo
5830
5950
  );
5831
- const template = getTemplateForFile(templateInfo, data);
5832
- if (!template) {
5833
- console.warn(
5834
- `Document: ${filepath} has an ambiguous template, skipping from indexing`
5835
- );
5836
- return;
5837
- }
5838
5951
  const normalizedPath = (0, import_schema_tools3.normalizePath)(filepath);
5839
5952
  const folderKey = folderTreeBuilder.update(
5840
5953
  normalizedPath,
5841
5954
  collectionPath || ""
5842
5955
  );
5843
- const aliasedData = templateInfo ? replaceNameOverrides(template, data) : data;
5844
5956
  await enqueueOps([
5845
5957
  ...makeIndexOpsForDocument(
5846
5958
  normalizedPath,
@@ -5872,7 +5984,7 @@ var _indexContent = async (database, level, documentPaths, enqueueOps, collectio
5872
5984
  throw new TinaFetchError(`Unable to seed ${filepath}`, {
5873
5985
  originalError: error,
5874
5986
  file: filepath,
5875
- collection: collection.name,
5987
+ collection: collection == null ? void 0 : collection.name,
5876
5988
  stack: error.stack
5877
5989
  });
5878
5990
  }
@@ -5956,27 +6068,6 @@ var _deleteIndexContent = async (database, documentPaths, enqueueOps, collection
5956
6068
  );
5957
6069
  }
5958
6070
  };
5959
- var getTemplateForFile = (templateInfo, data) => {
5960
- if (templateInfo.type === "object") {
5961
- return templateInfo.template;
5962
- }
5963
- if (templateInfo.type === "union") {
5964
- if (hasOwnProperty(data, "_template")) {
5965
- const template = templateInfo.templates.find(
5966
- (t) => lastItem(t.namespace) === data._template
5967
- );
5968
- if (!template) {
5969
- throw new Error(
5970
- `Unable to find template "${data._template}". Possible templates are: ${templateInfo.templates.map((template2) => `"${template2.name}"`).join(", ")}.`
5971
- );
5972
- }
5973
- return template;
5974
- } else {
5975
- return void 0;
5976
- }
5977
- }
5978
- throw new Error(`Unable to determine template`);
5979
- };
5980
6071
 
5981
6072
  // src/level/tinaLevel.ts
5982
6073
  var import_many_level = require("many-level");
@@ -6002,7 +6093,7 @@ var TinaLevelClient = class extends import_many_level.ManyLevelGuest {
6002
6093
  // src/database/bridge/filesystem.ts
6003
6094
  var import_fs_extra = __toESM(require("fs-extra"));
6004
6095
  var import_fast_glob = __toESM(require("fast-glob"));
6005
- var import_path4 = __toESM(require("path"));
6096
+ var import_path5 = __toESM(require("path"));
6006
6097
  var import_normalize_path = __toESM(require("normalize-path"));
6007
6098
  var FilesystemBridge = class {
6008
6099
  constructor(rootPath, outputPath) {
@@ -6010,9 +6101,9 @@ var FilesystemBridge = class {
6010
6101
  this.outputPath = outputPath || rootPath;
6011
6102
  }
6012
6103
  async glob(pattern, extension) {
6013
- const basePath = import_path4.default.join(this.outputPath, ...pattern.split("/"));
6104
+ const basePath = import_path5.default.join(this.outputPath, ...pattern.split("/"));
6014
6105
  const items = await (0, import_fast_glob.default)(
6015
- import_path4.default.join(basePath, "**", `/*.${extension}`).replace(/\\/g, "/"),
6106
+ import_path5.default.join(basePath, "**", `/*.${extension}`).replace(/\\/g, "/"),
6016
6107
  {
6017
6108
  dot: true,
6018
6109
  ignore: ["**/node_modules/**"]
@@ -6024,14 +6115,14 @@ var FilesystemBridge = class {
6024
6115
  });
6025
6116
  }
6026
6117
  async delete(filepath) {
6027
- 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));
6028
6119
  }
6029
6120
  async get(filepath) {
6030
- 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();
6031
6122
  }
6032
6123
  async put(filepath, data, basePathOverride) {
6033
6124
  const basePath = basePathOverride || this.outputPath;
6034
- 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);
6035
6126
  }
6036
6127
  };
6037
6128
  var AuditFileSystemBridge = class extends FilesystemBridge {
@@ -6056,7 +6147,7 @@ var import_fs_extra2 = __toESM(require("fs-extra"));
6056
6147
  var import_glob_parent = __toESM(require("glob-parent"));
6057
6148
  var import_normalize_path2 = __toESM(require("normalize-path"));
6058
6149
  var import_graphql6 = require("graphql");
6059
- var import_path5 = require("path");
6150
+ var import_path6 = require("path");
6060
6151
  var flat = typeof Array.prototype.flat === "undefined" ? (entries) => entries.reduce((acc, x) => acc.concat(x), []) : (entries) => entries.flat();
6061
6152
  var toUint8Array = (buf) => {
6062
6153
  const ab = new ArrayBuffer(buf.length);
@@ -6115,7 +6206,7 @@ var IsomorphicBridge = class {
6115
6206
  async listEntries({
6116
6207
  pattern,
6117
6208
  entry,
6118
- path: path5,
6209
+ path: path6,
6119
6210
  results
6120
6211
  }) {
6121
6212
  const treeResult = await import_isomorphic_git.default.readTree({
@@ -6125,7 +6216,7 @@ var IsomorphicBridge = class {
6125
6216
  });
6126
6217
  const children = [];
6127
6218
  for (const childEntry of treeResult.tree) {
6128
- const childPath = path5 ? `${path5}/${childEntry.path}` : childEntry.path;
6219
+ const childPath = path6 ? `${path6}/${childEntry.path}` : childEntry.path;
6129
6220
  if (childEntry.type === "tree") {
6130
6221
  children.push(childEntry);
6131
6222
  } else {
@@ -6135,7 +6226,7 @@ var IsomorphicBridge = class {
6135
6226
  }
6136
6227
  }
6137
6228
  for (const childEntry of children) {
6138
- const childPath = path5 ? `${path5}/${childEntry.path}` : childEntry.path;
6229
+ const childPath = path6 ? `${path6}/${childEntry.path}` : childEntry.path;
6139
6230
  await this.listEntries({
6140
6231
  pattern,
6141
6232
  entry: childEntry,
@@ -6144,17 +6235,17 @@ var IsomorphicBridge = class {
6144
6235
  });
6145
6236
  }
6146
6237
  }
6147
- async resolvePathEntries(path5, ref) {
6148
- let pathParts = path5.split("/");
6238
+ async resolvePathEntries(path6, ref) {
6239
+ let pathParts = path6.split("/");
6149
6240
  const result = await import_isomorphic_git.default.walk({
6150
6241
  ...this.isomorphicConfig,
6151
6242
  map: async (filepath, [head]) => {
6152
6243
  if (head._fullpath === ".") {
6153
6244
  return head;
6154
6245
  }
6155
- if (path5.startsWith(filepath)) {
6156
- if ((0, import_path5.dirname)(path5) === (0, import_path5.dirname)(filepath)) {
6157
- if (path5 === filepath) {
6246
+ if (path6.startsWith(filepath)) {
6247
+ if ((0, import_path6.dirname)(path6) === (0, import_path6.dirname)(filepath)) {
6248
+ if (path6 === filepath) {
6158
6249
  return head;
6159
6250
  }
6160
6251
  } else {
@@ -6174,7 +6265,7 @@ var IsomorphicBridge = class {
6174
6265
  }
6175
6266
  return { pathParts, pathEntries };
6176
6267
  }
6177
- async updateTreeHierarchy(existingOid, updatedOid, path5, type, pathEntries, pathParts) {
6268
+ async updateTreeHierarchy(existingOid, updatedOid, path6, type, pathEntries, pathParts) {
6178
6269
  const lastIdx = pathEntries.length - 1;
6179
6270
  const parentEntry = pathEntries[lastIdx];
6180
6271
  const parentPath = pathParts[lastIdx];
@@ -6189,7 +6280,7 @@ var IsomorphicBridge = class {
6189
6280
  cache: this.cache
6190
6281
  });
6191
6282
  tree = existingOid ? treeResult.tree.map((entry) => {
6192
- if (entry.path === path5) {
6283
+ if (entry.path === path6) {
6193
6284
  entry.oid = updatedOid;
6194
6285
  }
6195
6286
  return entry;
@@ -6198,7 +6289,7 @@ var IsomorphicBridge = class {
6198
6289
  {
6199
6290
  oid: updatedOid,
6200
6291
  type,
6201
- path: path5,
6292
+ path: path6,
6202
6293
  mode
6203
6294
  }
6204
6295
  ];
@@ -6207,7 +6298,7 @@ var IsomorphicBridge = class {
6207
6298
  {
6208
6299
  oid: updatedOid,
6209
6300
  type,
6210
- path: path5,
6301
+ path: path6,
6211
6302
  mode
6212
6303
  }
6213
6304
  ];
@@ -6308,7 +6399,7 @@ var IsomorphicBridge = class {
6308
6399
  path: parentPath,
6309
6400
  results
6310
6401
  });
6311
- 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));
6312
6403
  }
6313
6404
  async delete(filepath) {
6314
6405
  const ref = await this.getRef();
@@ -6460,8 +6551,13 @@ var buildSchema = async (config, flags) => {
6460
6551
  createDatabase,
6461
6552
  createSchema,
6462
6553
  handleFetchErrorError,
6554
+ loadAndParseWithAliases,
6463
6555
  parseFile,
6464
6556
  resolve,
6557
+ scanAllContent,
6558
+ scanContentByPaths,
6465
6559
  sequential,
6466
- stringifyFile
6560
+ stringifyFile,
6561
+ transformDocument,
6562
+ transformDocumentIntoPayload
6467
6563
  });