@tinacms/graphql 0.0.0-db8aa8e-20250228034006 → 0.0.0-dbef36f-20250616005720

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.mjs CHANGED
@@ -1847,7 +1847,7 @@ var Builder = class {
1847
1847
  * ```
1848
1848
  *
1849
1849
  * @public
1850
- * @param collection a Tina Cloud collection
1850
+ * @param collection a TinaCloud collection
1851
1851
  */
1852
1852
  this.collectionFragment = async (collection) => {
1853
1853
  const name = NAMER.dataTypeName(collection.namespace);
@@ -3019,7 +3019,7 @@ var validateField = async (field) => {
3019
3019
  // package.json
3020
3020
  var package_default = {
3021
3021
  name: "@tinacms/graphql",
3022
- version: "1.5.13",
3022
+ version: "1.5.18",
3023
3023
  main: "dist/index.js",
3024
3024
  module: "dist/index.mjs",
3025
3025
  typings: "dist/index.d.ts",
@@ -3045,7 +3045,6 @@ var package_default = {
3045
3045
  types: "pnpm tsc",
3046
3046
  build: "tinacms-scripts build",
3047
3047
  docs: "pnpm typedoc",
3048
- serve: "pnpm nodemon dist/server.js",
3049
3048
  test: "vitest run",
3050
3049
  "test-watch": "vitest"
3051
3050
  },
@@ -3100,7 +3099,6 @@ var package_default = {
3100
3099
  "@types/yup": "^0.29.14",
3101
3100
  "jest-file-snapshot": "^0.5.0",
3102
3101
  "memory-level": "^1.0.0",
3103
- nodemon: "3.1.4",
3104
3102
  typescript: "^5.7.3",
3105
3103
  vite: "^4.5.9",
3106
3104
  vitest: "^0.32.4",
@@ -3260,7 +3258,9 @@ var _buildSchema = async (builder, tinaSchema) => {
3260
3258
  await builder.buildCreateCollectionFolderMutation()
3261
3259
  );
3262
3260
  await sequential(collections, async (collection) => {
3263
- queryTypeDefinitionFields.push(await builder.collectionDocument(collection));
3261
+ queryTypeDefinitionFields.push(
3262
+ await builder.collectionDocument(collection)
3263
+ );
3264
3264
  if (collection.isAuthCollection) {
3265
3265
  queryTypeDefinitionFields.push(
3266
3266
  await builder.authenticationCollectionDocument(collection)
@@ -3310,250 +3310,11 @@ import { graphql, buildASTSchema, getNamedType, GraphQLError as GraphQLError4 }
3310
3310
  // src/resolver/index.ts
3311
3311
  import path3 from "path";
3312
3312
  import isValid from "date-fns/isValid/index.js";
3313
+ import { JSONPath as JSONPath2 } from "jsonpath-plus";
3313
3314
 
3314
3315
  // src/mdx/index.ts
3315
3316
  import { parseMDX, stringifyMDX } from "@tinacms/mdx";
3316
3317
 
3317
- // src/resolver/index.ts
3318
- import { JSONPath as JSONPath2 } from "jsonpath-plus";
3319
-
3320
- // src/resolver/error.ts
3321
- var TinaGraphQLError = class extends Error {
3322
- constructor(message, extensions) {
3323
- super(message);
3324
- if (!this.name) {
3325
- Object.defineProperty(this, "name", { value: "TinaGraphQLError" });
3326
- }
3327
- this.extensions = { ...extensions };
3328
- }
3329
- };
3330
- var TinaFetchError = class extends Error {
3331
- constructor(message, args) {
3332
- super(message);
3333
- this.name = "TinaFetchError";
3334
- this.collection = args.collection;
3335
- this.stack = args.stack;
3336
- this.file = args.file;
3337
- this.originalError = args.originalError;
3338
- }
3339
- };
3340
- var TinaQueryError = class extends TinaFetchError {
3341
- constructor(args) {
3342
- super(
3343
- `Error querying file ${args.file} from collection ${args.collection}. ${auditMessage(args.includeAuditMessage)}`,
3344
- args
3345
- );
3346
- }
3347
- };
3348
- var TinaParseDocumentError = class extends TinaFetchError {
3349
- constructor(args) {
3350
- super(
3351
- `Error parsing file ${args.file} from collection ${args.collection}. ${auditMessage(args.includeAuditMessage)}`,
3352
- args
3353
- );
3354
- }
3355
- toString() {
3356
- return super.toString() + "\n OriginalError: \n" + this.originalError.toString();
3357
- }
3358
- };
3359
- var auditMessage = (includeAuditMessage = true) => includeAuditMessage ? `Please run "tinacms audit" or add the --verbose option for more info` : "";
3360
- var handleFetchErrorError = (e, verbose) => {
3361
- if (e instanceof Error) {
3362
- if (e instanceof TinaFetchError) {
3363
- if (verbose) {
3364
- console.log(e.toString());
3365
- console.log(e);
3366
- console.log(e.stack);
3367
- }
3368
- }
3369
- } else {
3370
- console.error(e);
3371
- }
3372
- throw e;
3373
- };
3374
-
3375
- // src/resolver/filter-utils.ts
3376
- var resolveReferences = async (filter, fields, resolver) => {
3377
- for (const fieldKey of Object.keys(filter)) {
3378
- const fieldDefinition = fields.find(
3379
- (f) => f.name === fieldKey
3380
- );
3381
- if (fieldDefinition) {
3382
- if (fieldDefinition.type === "reference") {
3383
- const { edges, values } = await resolver(filter, fieldDefinition);
3384
- if (edges.length === 1) {
3385
- filter[fieldKey] = {
3386
- eq: values[0]
3387
- };
3388
- } else if (edges.length > 1) {
3389
- filter[fieldKey] = {
3390
- in: values
3391
- };
3392
- } else {
3393
- filter[fieldKey] = {
3394
- eq: "___null___"
3395
- };
3396
- }
3397
- } else if (fieldDefinition.type === "object") {
3398
- if (fieldDefinition.templates) {
3399
- for (const templateName of Object.keys(filter[fieldKey])) {
3400
- const template = fieldDefinition.templates.find(
3401
- (template2) => !(typeof template2 === "string") && template2.name === templateName
3402
- );
3403
- if (template) {
3404
- await resolveReferences(
3405
- filter[fieldKey][templateName],
3406
- template.fields,
3407
- resolver
3408
- );
3409
- } else {
3410
- throw new Error(`Template ${templateName} not found`);
3411
- }
3412
- }
3413
- } else {
3414
- await resolveReferences(
3415
- filter[fieldKey],
3416
- fieldDefinition.fields,
3417
- resolver
3418
- );
3419
- }
3420
- }
3421
- } else {
3422
- throw new Error(`Unable to find field ${fieldKey}`);
3423
- }
3424
- }
3425
- };
3426
- var collectConditionsForChildFields = (filterNode, fields, pathExpression, collectCondition) => {
3427
- for (const childFieldName of Object.keys(filterNode)) {
3428
- const childField = fields.find((field) => field.name === childFieldName);
3429
- if (!childField) {
3430
- throw new Error(`Unable to find type for field ${childFieldName}`);
3431
- }
3432
- collectConditionsForField(
3433
- childFieldName,
3434
- childField,
3435
- filterNode[childFieldName],
3436
- pathExpression,
3437
- collectCondition
3438
- );
3439
- }
3440
- };
3441
- var collectConditionsForObjectField = (fieldName, field, filterNode, pathExpression, collectCondition) => {
3442
- if (field.list && field.templates) {
3443
- for (const [filterKey, childFilterNode] of Object.entries(filterNode)) {
3444
- const template = field.templates.find(
3445
- (template2) => !(typeof template2 === "string") && template2.name === filterKey
3446
- );
3447
- const jsonPath = `${fieldName}[?(@._template=="${filterKey}")]`;
3448
- const filterPath = pathExpression ? `${pathExpression}.${jsonPath}` : jsonPath;
3449
- collectConditionsForChildFields(
3450
- childFilterNode,
3451
- template.fields,
3452
- filterPath,
3453
- collectCondition
3454
- );
3455
- }
3456
- } else {
3457
- const jsonPath = `${fieldName}${field.list ? "[*]" : ""}`;
3458
- const filterPath = pathExpression ? `${pathExpression}.${jsonPath}` : `${jsonPath}`;
3459
- collectConditionsForChildFields(
3460
- filterNode,
3461
- field.fields,
3462
- filterPath,
3463
- collectCondition
3464
- );
3465
- }
3466
- };
3467
- var collectConditionsForField = (fieldName, field, filterNode, pathExpression, collectCondition) => {
3468
- if (field.type === "object") {
3469
- collectConditionsForObjectField(
3470
- fieldName,
3471
- field,
3472
- filterNode,
3473
- pathExpression,
3474
- collectCondition
3475
- );
3476
- } else {
3477
- collectCondition({
3478
- filterPath: pathExpression ? `${pathExpression}.${fieldName}` : fieldName,
3479
- filterExpression: {
3480
- _type: field.type,
3481
- _list: !!field.list,
3482
- ...filterNode
3483
- }
3484
- });
3485
- }
3486
- };
3487
-
3488
- // src/resolver/media-utils.ts
3489
- var resolveMediaCloudToRelative = (value, config = { useRelativeMedia: true }, schema) => {
3490
- if (config && value) {
3491
- if (config.useRelativeMedia === true) {
3492
- return value;
3493
- }
3494
- if (hasTinaMediaConfig(schema) === true) {
3495
- const assetsURL = `https://${config.assetsHost}/${config.clientId}`;
3496
- if (typeof value === "string" && value.includes(assetsURL)) {
3497
- const cleanMediaRoot = cleanUpSlashes(
3498
- schema.config.media.tina.mediaRoot
3499
- );
3500
- const strippedURL = value.replace(assetsURL, "");
3501
- return `${cleanMediaRoot}${strippedURL}`;
3502
- }
3503
- if (Array.isArray(value)) {
3504
- return value.map((v) => {
3505
- if (!v || typeof v !== "string") return v;
3506
- const cleanMediaRoot = cleanUpSlashes(
3507
- schema.config.media.tina.mediaRoot
3508
- );
3509
- const strippedURL = v.replace(assetsURL, "");
3510
- return `${cleanMediaRoot}${strippedURL}`;
3511
- });
3512
- }
3513
- return value;
3514
- }
3515
- return value;
3516
- } else {
3517
- return value;
3518
- }
3519
- };
3520
- var resolveMediaRelativeToCloud = (value, config = { useRelativeMedia: true }, schema) => {
3521
- if (config && value) {
3522
- if (config.useRelativeMedia === true) {
3523
- return value;
3524
- }
3525
- if (hasTinaMediaConfig(schema) === true) {
3526
- const cleanMediaRoot = cleanUpSlashes(schema.config.media.tina.mediaRoot);
3527
- if (typeof value === "string") {
3528
- const strippedValue = value.replace(cleanMediaRoot, "");
3529
- return `https://${config.assetsHost}/${config.clientId}${strippedValue}`;
3530
- }
3531
- if (Array.isArray(value)) {
3532
- return value.map((v) => {
3533
- if (!v || typeof v !== "string") return v;
3534
- const strippedValue = v.replace(cleanMediaRoot, "");
3535
- return `https://${config.assetsHost}/${config.clientId}${strippedValue}`;
3536
- });
3537
- }
3538
- }
3539
- return value;
3540
- } else {
3541
- return value;
3542
- }
3543
- };
3544
- var cleanUpSlashes = (path7) => {
3545
- if (path7) {
3546
- return `/${path7.replace(/^\/+|\/+$/gm, "")}`;
3547
- }
3548
- return "";
3549
- };
3550
- var hasTinaMediaConfig = (schema) => {
3551
- if (!schema.config?.media?.tina) return false;
3552
- if (typeof schema.config?.media?.tina?.publicFolder !== "string" && typeof schema.config?.media?.tina?.mediaRoot !== "string")
3553
- return false;
3554
- return true;
3555
- };
3556
-
3557
3318
  // src/resolver/index.ts
3558
3319
  import { GraphQLError as GraphQLError2 } from "graphql";
3559
3320
 
@@ -3575,7 +3336,9 @@ var LevelProxyHandler = {
3575
3336
  throw new Error(`The property, ${property.toString()}, doesn't exist`);
3576
3337
  }
3577
3338
  if (typeof target[property] !== "function") {
3578
- throw new Error(`The property, ${property.toString()}, is not a function`);
3339
+ throw new Error(
3340
+ `The property, ${property.toString()}, is not a function`
3341
+ );
3579
3342
  }
3580
3343
  if (property === "get") {
3581
3344
  return async (...args) => {
@@ -3613,13 +3376,13 @@ import path2 from "path";
3613
3376
 
3614
3377
  // src/database/util.ts
3615
3378
  import toml from "@iarna/toml";
3616
- import yaml from "js-yaml";
3617
- import matter from "gray-matter";
3618
3379
  import {
3619
3380
  normalizePath
3620
3381
  } from "@tinacms/schema-tools";
3621
- import micromatch from "micromatch";
3382
+ import matter from "gray-matter";
3383
+ import yaml from "js-yaml";
3622
3384
  import path from "path";
3385
+ import micromatch from "micromatch";
3623
3386
 
3624
3387
  // src/database/alias-utils.ts
3625
3388
  var replaceBlockAliases = (template, item) => {
@@ -4506,100 +4269,343 @@ var makeFolderOpsForCollection = (folderTree, collection, indexDefinitions, opTy
4506
4269
  });
4507
4270
  }
4508
4271
  }
4509
- return result;
4272
+ return result;
4273
+ };
4274
+ var makeIndexOpsForDocument = (filepath, collection, indexDefinitions, data, opType, level, escapeStr = stringEscaper) => {
4275
+ const result = [];
4276
+ if (collection) {
4277
+ const collectionSublevel = level.sublevel(collection, SUBLEVEL_OPTIONS);
4278
+ for (const [sort, definition] of Object.entries(indexDefinitions)) {
4279
+ const indexedValue = makeKeyForField(definition, data, escapeStr);
4280
+ const indexSublevel = collectionSublevel.sublevel(sort, SUBLEVEL_OPTIONS);
4281
+ if (sort === DEFAULT_COLLECTION_SORT_KEY) {
4282
+ result.push({
4283
+ type: opType,
4284
+ key: filepath,
4285
+ sublevel: indexSublevel,
4286
+ value: opType === "put" ? {} : void 0
4287
+ });
4288
+ } else {
4289
+ if (indexedValue) {
4290
+ result.push({
4291
+ type: opType,
4292
+ key: `${indexedValue}${INDEX_KEY_FIELD_SEPARATOR}${filepath}`,
4293
+ sublevel: indexSublevel,
4294
+ value: opType === "put" ? {} : void 0
4295
+ });
4296
+ }
4297
+ }
4298
+ }
4299
+ }
4300
+ return result;
4301
+ };
4302
+ var makeRefOpsForDocument = (filepath, collection, references, data, opType, level) => {
4303
+ const result = [];
4304
+ if (collection) {
4305
+ for (const [c, referencePaths] of Object.entries(references || {})) {
4306
+ if (!referencePaths.length) {
4307
+ continue;
4308
+ }
4309
+ const collectionSublevel = level.sublevel(c, SUBLEVEL_OPTIONS);
4310
+ const refSublevel = collectionSublevel.sublevel(
4311
+ REFS_COLLECTIONS_SORT_KEY,
4312
+ SUBLEVEL_OPTIONS
4313
+ );
4314
+ const references2 = {};
4315
+ for (const path7 of referencePaths) {
4316
+ const ref = JSONPath({ path: path7, json: data });
4317
+ if (!ref) {
4318
+ continue;
4319
+ }
4320
+ if (Array.isArray(ref)) {
4321
+ for (const r of ref) {
4322
+ if (!r) {
4323
+ continue;
4324
+ }
4325
+ if (references2[r]) {
4326
+ references2[r].push(path7);
4327
+ } else {
4328
+ references2[r] = [path7];
4329
+ }
4330
+ }
4331
+ } else {
4332
+ if (references2[ref]) {
4333
+ references2[ref].push(path7);
4334
+ } else {
4335
+ references2[ref] = [path7];
4336
+ }
4337
+ }
4338
+ }
4339
+ for (const ref of Object.keys(references2)) {
4340
+ for (const path7 of references2[ref]) {
4341
+ result.push({
4342
+ type: opType,
4343
+ key: `${ref}${INDEX_KEY_FIELD_SEPARATOR}${path7}${INDEX_KEY_FIELD_SEPARATOR}${filepath}`,
4344
+ sublevel: refSublevel,
4345
+ value: opType === "put" ? {} : void 0
4346
+ });
4347
+ }
4348
+ }
4349
+ }
4350
+ }
4351
+ return result;
4352
+ };
4353
+ var makeStringEscaper = (regex, replacement) => {
4354
+ return (input) => {
4355
+ if (Array.isArray(input)) {
4356
+ return input.map(
4357
+ (val) => val.replace(regex, replacement)
4358
+ );
4359
+ } else {
4360
+ if (typeof input === "string") {
4361
+ return input.replace(regex, replacement);
4362
+ } else {
4363
+ return input;
4364
+ }
4365
+ }
4366
+ };
4367
+ };
4368
+ var stringEscaper = makeStringEscaper(
4369
+ new RegExp(INDEX_KEY_FIELD_SEPARATOR, "gm"),
4370
+ encodeURIComponent(INDEX_KEY_FIELD_SEPARATOR)
4371
+ );
4372
+
4373
+ // src/resolver/error.ts
4374
+ var TinaGraphQLError = class extends Error {
4375
+ constructor(message, extensions) {
4376
+ super(message);
4377
+ if (!this.name) {
4378
+ Object.defineProperty(this, "name", { value: "TinaGraphQLError" });
4379
+ }
4380
+ this.extensions = { ...extensions };
4381
+ }
4382
+ };
4383
+ var TinaFetchError = class extends Error {
4384
+ constructor(message, args) {
4385
+ super(message);
4386
+ this.name = "TinaFetchError";
4387
+ this.collection = args.collection;
4388
+ this.stack = args.stack;
4389
+ this.file = args.file;
4390
+ this.originalError = args.originalError;
4391
+ }
4392
+ };
4393
+ var TinaQueryError = class extends TinaFetchError {
4394
+ constructor(args) {
4395
+ super(
4396
+ `Error querying file ${args.file} from collection ${args.collection}. ${auditMessage(args.includeAuditMessage)}`,
4397
+ args
4398
+ );
4399
+ }
4400
+ };
4401
+ var TinaParseDocumentError = class extends TinaFetchError {
4402
+ constructor(args) {
4403
+ super(
4404
+ `Error parsing file ${args.file} from collection ${args.collection}. ${auditMessage(args.includeAuditMessage)}`,
4405
+ args
4406
+ );
4407
+ }
4408
+ toString() {
4409
+ return super.toString() + "\n OriginalError: \n" + this.originalError.toString();
4410
+ }
4411
+ };
4412
+ var auditMessage = (includeAuditMessage = true) => includeAuditMessage ? `Please run "tinacms audit" or add the --verbose option for more info` : "";
4413
+ var handleFetchErrorError = (e, verbose) => {
4414
+ if (e instanceof Error) {
4415
+ if (e instanceof TinaFetchError) {
4416
+ if (verbose) {
4417
+ console.log(e.toString());
4418
+ console.log(e);
4419
+ console.log(e.stack);
4420
+ }
4421
+ }
4422
+ } else {
4423
+ console.error(e);
4424
+ }
4425
+ throw e;
4426
+ };
4427
+
4428
+ // src/resolver/filter-utils.ts
4429
+ var resolveReferences = async (filter, fields, resolver) => {
4430
+ for (const fieldKey of Object.keys(filter)) {
4431
+ const fieldDefinition = fields.find(
4432
+ (f) => f.name === fieldKey
4433
+ );
4434
+ if (fieldDefinition) {
4435
+ if (fieldDefinition.type === "reference") {
4436
+ const { edges, values } = await resolver(filter, fieldDefinition);
4437
+ if (edges.length === 1) {
4438
+ filter[fieldKey] = {
4439
+ eq: values[0]
4440
+ };
4441
+ } else if (edges.length > 1) {
4442
+ filter[fieldKey] = {
4443
+ in: values
4444
+ };
4445
+ } else {
4446
+ filter[fieldKey] = {
4447
+ eq: "___null___"
4448
+ };
4449
+ }
4450
+ } else if (fieldDefinition.type === "object") {
4451
+ if (fieldDefinition.templates) {
4452
+ for (const templateName of Object.keys(filter[fieldKey])) {
4453
+ const template = fieldDefinition.templates.find(
4454
+ (template2) => !(typeof template2 === "string") && template2.name === templateName
4455
+ );
4456
+ if (template) {
4457
+ await resolveReferences(
4458
+ filter[fieldKey][templateName],
4459
+ template.fields,
4460
+ resolver
4461
+ );
4462
+ } else {
4463
+ throw new Error(`Template ${templateName} not found`);
4464
+ }
4465
+ }
4466
+ } else {
4467
+ await resolveReferences(
4468
+ filter[fieldKey],
4469
+ fieldDefinition.fields,
4470
+ resolver
4471
+ );
4472
+ }
4473
+ }
4474
+ } else {
4475
+ throw new Error(`Unable to find field ${fieldKey}`);
4476
+ }
4477
+ }
4478
+ };
4479
+ var collectConditionsForChildFields = (filterNode, fields, pathExpression, collectCondition) => {
4480
+ for (const childFieldName of Object.keys(filterNode)) {
4481
+ const childField = fields.find((field) => field.name === childFieldName);
4482
+ if (!childField) {
4483
+ throw new Error(`Unable to find type for field ${childFieldName}`);
4484
+ }
4485
+ collectConditionsForField(
4486
+ childFieldName,
4487
+ childField,
4488
+ filterNode[childFieldName],
4489
+ pathExpression,
4490
+ collectCondition
4491
+ );
4492
+ }
4493
+ };
4494
+ var collectConditionsForObjectField = (fieldName, field, filterNode, pathExpression, collectCondition) => {
4495
+ if (field.list && field.templates) {
4496
+ for (const [filterKey, childFilterNode] of Object.entries(filterNode)) {
4497
+ const template = field.templates.find(
4498
+ (template2) => !(typeof template2 === "string") && template2.name === filterKey
4499
+ );
4500
+ const jsonPath = `${fieldName}[?(@._template=="${filterKey}")]`;
4501
+ const filterPath = pathExpression ? `${pathExpression}.${jsonPath}` : jsonPath;
4502
+ collectConditionsForChildFields(
4503
+ childFilterNode,
4504
+ template.fields,
4505
+ filterPath,
4506
+ collectCondition
4507
+ );
4508
+ }
4509
+ } else {
4510
+ const jsonPath = `${fieldName}${field.list ? "[*]" : ""}`;
4511
+ const filterPath = pathExpression ? `${pathExpression}.${jsonPath}` : `${jsonPath}`;
4512
+ collectConditionsForChildFields(
4513
+ filterNode,
4514
+ field.fields,
4515
+ filterPath,
4516
+ collectCondition
4517
+ );
4518
+ }
4510
4519
  };
4511
- var makeIndexOpsForDocument = (filepath, collection, indexDefinitions, data, opType, level, escapeStr = stringEscaper) => {
4512
- const result = [];
4513
- if (collection) {
4514
- const collectionSublevel = level.sublevel(collection, SUBLEVEL_OPTIONS);
4515
- for (const [sort, definition] of Object.entries(indexDefinitions)) {
4516
- const indexedValue = makeKeyForField(definition, data, escapeStr);
4517
- const indexSublevel = collectionSublevel.sublevel(sort, SUBLEVEL_OPTIONS);
4518
- if (sort === DEFAULT_COLLECTION_SORT_KEY) {
4519
- result.push({
4520
- type: opType,
4521
- key: filepath,
4522
- sublevel: indexSublevel,
4523
- value: opType === "put" ? {} : void 0
4524
- });
4525
- } else {
4526
- if (indexedValue) {
4527
- result.push({
4528
- type: opType,
4529
- key: `${indexedValue}${INDEX_KEY_FIELD_SEPARATOR}${filepath}`,
4530
- sublevel: indexSublevel,
4531
- value: opType === "put" ? {} : void 0
4532
- });
4533
- }
4520
+ var collectConditionsForField = (fieldName, field, filterNode, pathExpression, collectCondition) => {
4521
+ if (field.type === "object") {
4522
+ collectConditionsForObjectField(
4523
+ fieldName,
4524
+ field,
4525
+ filterNode,
4526
+ pathExpression,
4527
+ collectCondition
4528
+ );
4529
+ } else {
4530
+ collectCondition({
4531
+ filterPath: pathExpression ? `${pathExpression}.${fieldName}` : fieldName,
4532
+ filterExpression: {
4533
+ _type: field.type,
4534
+ _list: !!field.list,
4535
+ ...filterNode
4534
4536
  }
4535
- }
4537
+ });
4536
4538
  }
4537
- return result;
4538
4539
  };
4539
- var makeRefOpsForDocument = (filepath, collection, references, data, opType, level) => {
4540
- const result = [];
4541
- if (collection) {
4542
- for (const [c, referencePaths] of Object.entries(references || {})) {
4543
- if (!referencePaths.length) {
4544
- continue;
4545
- }
4546
- const collectionSublevel = level.sublevel(c, SUBLEVEL_OPTIONS);
4547
- const refSublevel = collectionSublevel.sublevel(
4548
- REFS_COLLECTIONS_SORT_KEY,
4549
- SUBLEVEL_OPTIONS
4550
- );
4551
- const references2 = {};
4552
- for (const path7 of referencePaths) {
4553
- const ref = JSONPath({ path: path7, json: data });
4554
- if (Array.isArray(ref)) {
4555
- for (const r of ref) {
4556
- if (references2[r]) {
4557
- references2[r].push(path7);
4558
- } else {
4559
- references2[r] = [path7];
4560
- }
4561
- }
4562
- } else {
4563
- if (references2[ref]) {
4564
- references2[ref].push(path7);
4565
- } else {
4566
- references2[ref] = [path7];
4567
- }
4568
- }
4540
+
4541
+ // src/resolver/media-utils.ts
4542
+ var resolveMediaCloudToRelative = (value, config = { useRelativeMedia: true }, schema) => {
4543
+ if (config && value) {
4544
+ if (config.useRelativeMedia === true) {
4545
+ return value;
4546
+ }
4547
+ if (hasTinaMediaConfig(schema) === true) {
4548
+ const assetsURL = `https://${config.assetsHost}/${config.clientId}`;
4549
+ if (typeof value === "string" && value.includes(assetsURL)) {
4550
+ const cleanMediaRoot = cleanUpSlashes(
4551
+ schema.config.media.tina.mediaRoot
4552
+ );
4553
+ const strippedURL = value.replace(assetsURL, "");
4554
+ return `${cleanMediaRoot}${strippedURL}`;
4569
4555
  }
4570
- for (const ref of Object.keys(references2)) {
4571
- for (const path7 of references2[ref]) {
4572
- result.push({
4573
- type: opType,
4574
- key: `${ref}${INDEX_KEY_FIELD_SEPARATOR}${path7}${INDEX_KEY_FIELD_SEPARATOR}${filepath}`,
4575
- sublevel: refSublevel,
4576
- value: opType === "put" ? {} : void 0
4577
- });
4578
- }
4556
+ if (Array.isArray(value)) {
4557
+ return value.map((v) => {
4558
+ if (!v || typeof v !== "string") return v;
4559
+ const cleanMediaRoot = cleanUpSlashes(
4560
+ schema.config.media.tina.mediaRoot
4561
+ );
4562
+ const strippedURL = v.replace(assetsURL, "");
4563
+ return `${cleanMediaRoot}${strippedURL}`;
4564
+ });
4579
4565
  }
4566
+ return value;
4580
4567
  }
4568
+ return value;
4569
+ } else {
4570
+ return value;
4581
4571
  }
4582
- return result;
4583
4572
  };
4584
- var makeStringEscaper = (regex, replacement) => {
4585
- return (input) => {
4586
- if (Array.isArray(input)) {
4587
- return input.map(
4588
- (val) => val.replace(regex, replacement)
4589
- );
4590
- } else {
4591
- if (typeof input === "string") {
4592
- return input.replace(regex, replacement);
4593
- } else {
4594
- return input;
4573
+ var resolveMediaRelativeToCloud = (value, config = { useRelativeMedia: true }, schema) => {
4574
+ if (config && value) {
4575
+ if (config.useRelativeMedia === true) {
4576
+ return value;
4577
+ }
4578
+ if (hasTinaMediaConfig(schema) === true) {
4579
+ const cleanMediaRoot = cleanUpSlashes(schema.config.media.tina.mediaRoot);
4580
+ if (typeof value === "string") {
4581
+ const strippedValue = value.replace(cleanMediaRoot, "");
4582
+ return `https://${config.assetsHost}/${config.clientId}${strippedValue}`;
4583
+ }
4584
+ if (Array.isArray(value)) {
4585
+ return value.map((v) => {
4586
+ if (!v || typeof v !== "string") return v;
4587
+ const strippedValue = v.replace(cleanMediaRoot, "");
4588
+ return `https://${config.assetsHost}/${config.clientId}${strippedValue}`;
4589
+ });
4595
4590
  }
4596
4591
  }
4597
- };
4592
+ return value;
4593
+ } else {
4594
+ return value;
4595
+ }
4596
+ };
4597
+ var cleanUpSlashes = (path7) => {
4598
+ if (path7) {
4599
+ return `/${path7.replace(/^\/+|\/+$/gm, "")}`;
4600
+ }
4601
+ return "";
4602
+ };
4603
+ var hasTinaMediaConfig = (schema) => {
4604
+ if (!schema.config?.media?.tina) return false;
4605
+ if (typeof schema.config?.media?.tina?.publicFolder !== "string" && typeof schema.config?.media?.tina?.mediaRoot !== "string")
4606
+ return false;
4607
+ return true;
4598
4608
  };
4599
- var stringEscaper = makeStringEscaper(
4600
- new RegExp(INDEX_KEY_FIELD_SEPARATOR, "gm"),
4601
- encodeURIComponent(INDEX_KEY_FIELD_SEPARATOR)
4602
- );
4603
4609
 
4604
4610
  // src/resolver/index.ts
4605
4611
  var createResolver = (args) => {
@@ -4802,25 +4808,32 @@ var transformDocumentIntoPayload = async (fullPath, rawData, tinaSchema, config,
4802
4808
  }
4803
4809
  };
4804
4810
  var updateObjectWithJsonPath = (obj, path7, oldValue, newValue) => {
4811
+ let updated = false;
4805
4812
  if (!path7.includes(".") && !path7.includes("[")) {
4806
4813
  if (path7 in obj && obj[path7] === oldValue) {
4807
4814
  obj[path7] = newValue;
4815
+ updated = true;
4808
4816
  }
4809
- return obj;
4817
+ return { object: obj, updated };
4810
4818
  }
4811
4819
  const parentPath = path7.replace(/\.[^.\[\]]+$/, "");
4812
4820
  const keyToUpdate = path7.match(/[^.\[\]]+$/)[0];
4813
- const parents = JSONPath2({ path: parentPath, json: obj, resultType: "value" });
4821
+ const parents = JSONPath2({
4822
+ path: parentPath,
4823
+ json: obj,
4824
+ resultType: "value"
4825
+ });
4814
4826
  if (parents.length > 0) {
4815
4827
  parents.forEach((parent) => {
4816
4828
  if (parent && typeof parent === "object" && keyToUpdate in parent) {
4817
4829
  if (parent[keyToUpdate] === oldValue) {
4818
4830
  parent[keyToUpdate] = newValue;
4831
+ updated = true;
4819
4832
  }
4820
4833
  }
4821
4834
  });
4822
4835
  }
4823
- return obj;
4836
+ return { object: obj, updated };
4824
4837
  };
4825
4838
  var Resolver = class {
4826
4839
  constructor(init) {
@@ -4837,7 +4850,9 @@ var Resolver = class {
4837
4850
  };
4838
4851
  this.getRaw = async (fullPath) => {
4839
4852
  if (typeof fullPath !== "string") {
4840
- throw new Error(`fullPath must be of type string for getDocument request`);
4853
+ throw new Error(
4854
+ `fullPath must be of type string for getDocument request`
4855
+ );
4841
4856
  }
4842
4857
  return this.database.get(fullPath);
4843
4858
  };
@@ -4866,7 +4881,9 @@ var Resolver = class {
4866
4881
  };
4867
4882
  this.getDocument = async (fullPath, opts = {}) => {
4868
4883
  if (typeof fullPath !== "string") {
4869
- throw new Error(`fullPath must be of type string for getDocument request`);
4884
+ throw new Error(
4885
+ `fullPath must be of type string for getDocument request`
4886
+ );
4870
4887
  }
4871
4888
  const rawData = await this.getRaw(fullPath);
4872
4889
  const hasReferences = opts?.checkReferences ? await this.hasReferences(fullPath, opts.collection) : void 0;
@@ -4881,7 +4898,9 @@ var Resolver = class {
4881
4898
  };
4882
4899
  this.deleteDocument = async (fullPath) => {
4883
4900
  if (typeof fullPath !== "string") {
4884
- throw new Error(`fullPath must be of type string for getDocument request`);
4901
+ throw new Error(
4902
+ `fullPath must be of type string for getDocument request`
4903
+ );
4885
4904
  }
4886
4905
  await this.database.delete(fullPath);
4887
4906
  };
@@ -5085,7 +5104,11 @@ var Resolver = class {
5085
5104
  collection,
5086
5105
  doc?._rawData
5087
5106
  );
5088
- await this.database.put(realPath, { ...oldDoc, ...params }, collection.name);
5107
+ await this.database.put(
5108
+ realPath,
5109
+ { ...oldDoc, ...params },
5110
+ collection.name
5111
+ );
5089
5112
  return this.getDocument(realPath);
5090
5113
  };
5091
5114
  /**
@@ -5203,10 +5226,30 @@ var Resolver = class {
5203
5226
  docsWithRefs
5204
5227
  )) {
5205
5228
  let refDoc = await this.getRaw(pathToDocWithRef);
5229
+ let hasUpdate = false;
5206
5230
  for (const path7 of referencePaths) {
5207
- refDoc = updateObjectWithJsonPath(refDoc, path7, realPath, null);
5231
+ const { object: object2, updated } = updateObjectWithJsonPath(
5232
+ refDoc,
5233
+ path7,
5234
+ realPath,
5235
+ null
5236
+ );
5237
+ refDoc = object2;
5238
+ hasUpdate = updated || hasUpdate;
5239
+ }
5240
+ if (hasUpdate) {
5241
+ const collectionWithRef = this.tinaSchema.getCollectionByFullPath(pathToDocWithRef);
5242
+ if (!collectionWithRef) {
5243
+ throw new Error(
5244
+ `Unable to find collection for ${pathToDocWithRef}`
5245
+ );
5246
+ }
5247
+ await this.database.put(
5248
+ pathToDocWithRef,
5249
+ refDoc,
5250
+ collectionWithRef.name
5251
+ );
5208
5252
  }
5209
- await this.database.put(pathToDocWithRef, refDoc, collection2);
5210
5253
  }
5211
5254
  }
5212
5255
  }
@@ -5237,21 +5280,38 @@ var Resolver = class {
5237
5280
  docsWithRefs
5238
5281
  )) {
5239
5282
  let docWithRef = await this.getRaw(pathToDocWithRef);
5283
+ let hasUpdate = false;
5240
5284
  for (const path7 of referencePaths) {
5241
- docWithRef = updateObjectWithJsonPath(
5285
+ const { object: object2, updated } = updateObjectWithJsonPath(
5242
5286
  docWithRef,
5243
5287
  path7,
5244
5288
  realPath,
5245
5289
  newRealPath
5246
5290
  );
5291
+ docWithRef = object2;
5292
+ hasUpdate = updated || hasUpdate;
5293
+ }
5294
+ if (hasUpdate) {
5295
+ const collectionWithRef = this.tinaSchema.getCollectionByFullPath(pathToDocWithRef);
5296
+ if (!collectionWithRef) {
5297
+ throw new Error(
5298
+ `Unable to find collection for ${pathToDocWithRef}`
5299
+ );
5300
+ }
5301
+ await this.database.put(
5302
+ pathToDocWithRef,
5303
+ docWithRef,
5304
+ collectionWithRef.name
5305
+ );
5247
5306
  }
5248
- await this.database.put(pathToDocWithRef, docWithRef, collection2);
5249
5307
  }
5250
5308
  }
5251
5309
  return this.getDocument(newRealPath);
5252
5310
  }
5253
5311
  if (alreadyExists === false) {
5254
- throw new Error(`Unable to update document, ${realPath} does not exist`);
5312
+ throw new Error(
5313
+ `Unable to update document, ${realPath} does not exist`
5314
+ );
5255
5315
  }
5256
5316
  return this.updateResolveDocument({
5257
5317
  collection,
@@ -6593,8 +6653,35 @@ var Database = class {
6593
6653
  ]
6594
6654
  }
6595
6655
  };
6596
- if (collection.fields) {
6597
- for (const field of collection.fields) {
6656
+ let fields = [];
6657
+ if (collection.templates) {
6658
+ const templateFieldMap = {};
6659
+ const conflictedFields = /* @__PURE__ */ new Set();
6660
+ for (const template of collection.templates) {
6661
+ for (const field of template.fields) {
6662
+ if (!templateFieldMap[field.name]) {
6663
+ templateFieldMap[field.name] = field;
6664
+ } else {
6665
+ if (templateFieldMap[field.name].type !== field.type) {
6666
+ console.warn(
6667
+ `Field ${field.name} has conflicting types in templates - skipping index`
6668
+ );
6669
+ conflictedFields.add(field.name);
6670
+ }
6671
+ }
6672
+ }
6673
+ }
6674
+ for (const conflictedField in conflictedFields) {
6675
+ delete templateFieldMap[conflictedField];
6676
+ }
6677
+ for (const field of Object.values(templateFieldMap)) {
6678
+ fields.push(field);
6679
+ }
6680
+ } else if (collection.fields) {
6681
+ fields = collection.fields;
6682
+ }
6683
+ if (fields) {
6684
+ for (const field of fields) {
6598
6685
  if (field.indexed !== void 0 && field.indexed === false || field.type === "object") {
6599
6686
  continue;
6600
6687
  }
@@ -6895,13 +6982,14 @@ var Database = class {
6895
6982
  documentPaths,
6896
6983
  async (collection, documentPaths2) => {
6897
6984
  if (collection && !collection.isDetached) {
6898
- await _indexContent(
6899
- this,
6900
- this.contentLevel,
6901
- documentPaths2,
6985
+ await _indexContent({
6986
+ database: this,
6987
+ level: this.contentLevel,
6988
+ documentPaths: documentPaths2,
6902
6989
  enqueueOps,
6903
- collection
6904
- );
6990
+ collection,
6991
+ isPartialReindex: true
6992
+ });
6905
6993
  }
6906
6994
  }
6907
6995
  );
@@ -7008,20 +7096,26 @@ var Database = class {
7008
7096
  );
7009
7097
  const doc = await level2.keys({ limit: 1 }).next();
7010
7098
  if (!doc) {
7011
- await _indexContent(
7012
- this,
7013
- level2,
7014
- contentPaths,
7099
+ await _indexContent({
7100
+ database: this,
7101
+ level: level2,
7102
+ documentPaths: contentPaths,
7015
7103
  enqueueOps,
7016
7104
  collection,
7017
- userFields.map((field) => [
7105
+ passwordFields: userFields.map((field) => [
7018
7106
  ...field.path,
7019
7107
  field.passwordFieldName
7020
7108
  ])
7021
- );
7109
+ });
7022
7110
  }
7023
7111
  } else {
7024
- await _indexContent(this, level, contentPaths, enqueueOps, collection);
7112
+ await _indexContent({
7113
+ database: this,
7114
+ level,
7115
+ documentPaths: contentPaths,
7116
+ enqueueOps,
7117
+ collection
7118
+ });
7025
7119
  }
7026
7120
  }
7027
7121
  );
@@ -7160,7 +7254,15 @@ var hashPasswordValues = async (data, passwordFields) => Promise.all(
7160
7254
  )
7161
7255
  );
7162
7256
  var isGitKeep = (filepath, collection) => filepath.endsWith(`.gitkeep.${collection?.format || "md"}`);
7163
- var _indexContent = async (database, level, documentPaths, enqueueOps, collection, passwordFields) => {
7257
+ var _indexContent = async ({
7258
+ database,
7259
+ level,
7260
+ documentPaths,
7261
+ enqueueOps,
7262
+ collection,
7263
+ passwordFields,
7264
+ isPartialReindex
7265
+ }) => {
7164
7266
  let collectionIndexDefinitions;
7165
7267
  let collectionPath;
7166
7268
  if (collection) {
@@ -7201,40 +7303,42 @@ var _indexContent = async (database, level, documentPaths, enqueueOps, collectio
7201
7303
  normalizedPath,
7202
7304
  collectionPath || ""
7203
7305
  );
7204
- const item = await rootSublevel.get(normalizedPath);
7205
- if (item) {
7206
- await database.contentLevel.batch([
7207
- ...makeRefOpsForDocument(
7208
- normalizedPath,
7209
- collection?.name,
7210
- collectionReferences,
7211
- item,
7212
- "del",
7213
- level
7214
- ),
7215
- ...makeIndexOpsForDocument(
7216
- normalizedPath,
7217
- collection.name,
7218
- collectionIndexDefinitions,
7219
- item,
7220
- "del",
7221
- level
7222
- ),
7223
- // folder indices
7224
- ...makeIndexOpsForDocument(
7225
- normalizedPath,
7226
- `${collection.name}_${folderKey}`,
7227
- collectionIndexDefinitions,
7228
- item,
7229
- "del",
7230
- level
7231
- ),
7232
- {
7233
- type: "del",
7234
- key: normalizedPath,
7235
- sublevel: rootSublevel
7236
- }
7237
- ]);
7306
+ if (isPartialReindex) {
7307
+ const item = await rootSublevel.get(normalizedPath);
7308
+ if (item) {
7309
+ await database.contentLevel.batch([
7310
+ ...makeRefOpsForDocument(
7311
+ normalizedPath,
7312
+ collection?.name,
7313
+ collectionReferences,
7314
+ item,
7315
+ "del",
7316
+ level
7317
+ ),
7318
+ ...makeIndexOpsForDocument(
7319
+ normalizedPath,
7320
+ collection.name,
7321
+ collectionIndexDefinitions,
7322
+ item,
7323
+ "del",
7324
+ level
7325
+ ),
7326
+ // folder indices
7327
+ ...makeIndexOpsForDocument(
7328
+ normalizedPath,
7329
+ `${collection.name}_${folderKey}`,
7330
+ collectionIndexDefinitions,
7331
+ item,
7332
+ "del",
7333
+ level
7334
+ ),
7335
+ {
7336
+ type: "del",
7337
+ key: normalizedPath,
7338
+ sublevel: rootSublevel
7339
+ }
7340
+ ]);
7341
+ }
7238
7342
  }
7239
7343
  if (!isGitKeep(filepath, collection)) {
7240
7344
  await enqueueOps([
@@ -7470,8 +7574,8 @@ import path6 from "path";
7470
7574
  import normalize from "normalize-path";
7471
7575
  var FilesystemBridge = class {
7472
7576
  constructor(rootPath, outputPath) {
7473
- this.rootPath = rootPath || "";
7474
- this.outputPath = outputPath || rootPath;
7577
+ this.rootPath = path6.resolve(rootPath);
7578
+ this.outputPath = outputPath ? path6.resolve(outputPath) : this.rootPath;
7475
7579
  }
7476
7580
  async glob(pattern, extension) {
7477
7581
  const basePath = path6.join(this.outputPath, ...pattern.split("/"));
@@ -7483,19 +7587,19 @@ var FilesystemBridge = class {
7483
7587
  }
7484
7588
  );
7485
7589
  const posixRootPath = normalize(this.outputPath);
7486
- return items.map((item) => {
7487
- return item.replace(posixRootPath, "").replace(/^\/|\/$/g, "");
7488
- });
7590
+ return items.map(
7591
+ (item) => item.substring(posixRootPath.length).replace(/^\/|\/$/g, "")
7592
+ );
7489
7593
  }
7490
7594
  async delete(filepath) {
7491
7595
  await fs2.remove(path6.join(this.outputPath, filepath));
7492
7596
  }
7493
7597
  async get(filepath) {
7494
- return fs2.readFileSync(path6.join(this.outputPath, filepath)).toString();
7598
+ return (await fs2.readFile(path6.join(this.outputPath, filepath))).toString();
7495
7599
  }
7496
7600
  async put(filepath, data, basePathOverride) {
7497
7601
  const basePath = basePathOverride || this.outputPath;
7498
- await fs2.outputFileSync(path6.join(basePath, filepath), data);
7602
+ await fs2.outputFile(path6.join(basePath, filepath), data);
7499
7603
  }
7500
7604
  };
7501
7605
  var AuditFileSystemBridge = class extends FilesystemBridge {