@tinacms/graphql 0.0.0-d9672bc-20250218033222 → 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.12",
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
  },
@@ -3055,12 +3054,12 @@ var package_default = {
3055
3054
  "@tinacms/schema-tools": "workspace:*",
3056
3055
  "abstract-level": "^1.0.4",
3057
3056
  "date-fns": "^2.30.0",
3058
- "fast-glob": "^3.3.2",
3059
- "fs-extra": "^11.2.0",
3057
+ "fast-glob": "^3.3.3",
3058
+ "fs-extra": "^11.3.0",
3060
3059
  "glob-parent": "^6.0.2",
3061
3060
  graphql: "15.8.0",
3062
3061
  "gray-matter": "^4.0.3",
3063
- "isomorphic-git": "^1.27.1",
3062
+ "isomorphic-git": "^1.29.0",
3064
3063
  "js-sha1": "^0.6.0",
3065
3064
  "js-yaml": "^3.14.1",
3066
3065
  "jsonpath-plus": "10.1.0",
@@ -3070,7 +3069,7 @@ var package_default = {
3070
3069
  "many-level": "^2.0.0",
3071
3070
  micromatch: "4.0.8",
3072
3071
  "normalize-path": "^3.0.0",
3073
- "readable-stream": "^4.5.2",
3072
+ "readable-stream": "^4.7.0",
3074
3073
  scmp: "^2.1.0",
3075
3074
  yup: "^0.32.11"
3076
3075
  },
@@ -3094,17 +3093,16 @@ var package_default = {
3094
3093
  "@types/lru-cache": "^5.1.1",
3095
3094
  "@types/mdast": "^3.0.15",
3096
3095
  "@types/micromatch": "^4.0.9",
3097
- "@types/node": "^22.9.0",
3096
+ "@types/node": "^22.13.1",
3098
3097
  "@types/normalize-path": "^3.0.2",
3099
3098
  "@types/ws": "^7.4.7",
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
- typescript: "^5.6.3",
3105
- vite: "^4.3.9",
3106
- vitest: "^0.32.2",
3107
- zod: "^3.23.8"
3102
+ typescript: "^5.7.3",
3103
+ vite: "^4.5.9",
3104
+ vitest: "^0.32.4",
3105
+ zod: "^3.24.2"
3108
3106
  }
3109
3107
  };
3110
3108
 
@@ -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) => {
@@ -4534,62 +4297,315 @@ var makeIndexOpsForDocument = (filepath, collection, indexDefinitions, data, opT
4534
4297
  }
4535
4298
  }
4536
4299
  }
4537
- return result;
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
+ }
4538
4478
  };
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
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
4550
4499
  );
4551
- const references2 = {};
4552
- for (const path7 of referencePaths) {
4553
- const ref = JSONPath({ path: path7, json: data });
4554
- if (references2[ref]) {
4555
- references2[ref].push(path7);
4556
- } else {
4557
- references2[ref] = [path7];
4558
- }
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
+ }
4519
+ };
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
4559
4536
  }
4560
- for (const ref of Object.keys(references2)) {
4561
- for (const path7 of references2[ref]) {
4562
- result.push({
4563
- type: opType,
4564
- key: `${ref}${INDEX_KEY_FIELD_SEPARATOR}${path7}${INDEX_KEY_FIELD_SEPARATOR}${filepath}`,
4565
- sublevel: refSublevel,
4566
- value: opType === "put" ? {} : void 0
4567
- });
4568
- }
4537
+ });
4538
+ }
4539
+ };
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}`;
4555
+ }
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
+ });
4569
4565
  }
4566
+ return value;
4570
4567
  }
4568
+ return value;
4569
+ } else {
4570
+ return value;
4571
4571
  }
4572
- return result;
4573
4572
  };
4574
- var makeStringEscaper = (regex, replacement) => {
4575
- return (input) => {
4576
- if (Array.isArray(input)) {
4577
- return input.map(
4578
- (val) => val.replace(regex, replacement)
4579
- );
4580
- } else {
4581
- if (typeof input === "string") {
4582
- return input.replace(regex, replacement);
4583
- } else {
4584
- 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
+ });
4585
4590
  }
4586
4591
  }
4587
- };
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;
4588
4608
  };
4589
- var stringEscaper = makeStringEscaper(
4590
- new RegExp(INDEX_KEY_FIELD_SEPARATOR, "gm"),
4591
- encodeURIComponent(INDEX_KEY_FIELD_SEPARATOR)
4592
- );
4593
4609
 
4594
4610
  // src/resolver/index.ts
4595
4611
  var createResolver = (args) => {
@@ -4791,24 +4807,33 @@ var transformDocumentIntoPayload = async (fullPath, rawData, tinaSchema, config,
4791
4807
  throw e;
4792
4808
  }
4793
4809
  };
4794
- var updateObjectWithJsonPath = (obj, path7, newValue) => {
4810
+ var updateObjectWithJsonPath = (obj, path7, oldValue, newValue) => {
4811
+ let updated = false;
4795
4812
  if (!path7.includes(".") && !path7.includes("[")) {
4796
- if (path7 in obj) {
4813
+ if (path7 in obj && obj[path7] === oldValue) {
4797
4814
  obj[path7] = newValue;
4815
+ updated = true;
4798
4816
  }
4799
- return obj;
4800
- }
4801
- const parentPath = path7.replace(/\.[^.]+$/, "");
4802
- const keyToUpdate = path7.match(/[^.]+$/)[0];
4803
- const parents = JSONPath2({ path: parentPath, json: obj, resultType: "value" });
4817
+ return { object: obj, updated };
4818
+ }
4819
+ const parentPath = path7.replace(/\.[^.\[\]]+$/, "");
4820
+ const keyToUpdate = path7.match(/[^.\[\]]+$/)[0];
4821
+ const parents = JSONPath2({
4822
+ path: parentPath,
4823
+ json: obj,
4824
+ resultType: "value"
4825
+ });
4804
4826
  if (parents.length > 0) {
4805
4827
  parents.forEach((parent) => {
4806
4828
  if (parent && typeof parent === "object" && keyToUpdate in parent) {
4807
- parent[keyToUpdate] = newValue;
4829
+ if (parent[keyToUpdate] === oldValue) {
4830
+ parent[keyToUpdate] = newValue;
4831
+ updated = true;
4832
+ }
4808
4833
  }
4809
4834
  });
4810
4835
  }
4811
- return obj;
4836
+ return { object: obj, updated };
4812
4837
  };
4813
4838
  var Resolver = class {
4814
4839
  constructor(init) {
@@ -4825,7 +4850,9 @@ var Resolver = class {
4825
4850
  };
4826
4851
  this.getRaw = async (fullPath) => {
4827
4852
  if (typeof fullPath !== "string") {
4828
- 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
+ );
4829
4856
  }
4830
4857
  return this.database.get(fullPath);
4831
4858
  };
@@ -4854,7 +4881,9 @@ var Resolver = class {
4854
4881
  };
4855
4882
  this.getDocument = async (fullPath, opts = {}) => {
4856
4883
  if (typeof fullPath !== "string") {
4857
- 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
+ );
4858
4887
  }
4859
4888
  const rawData = await this.getRaw(fullPath);
4860
4889
  const hasReferences = opts?.checkReferences ? await this.hasReferences(fullPath, opts.collection) : void 0;
@@ -4869,7 +4898,9 @@ var Resolver = class {
4869
4898
  };
4870
4899
  this.deleteDocument = async (fullPath) => {
4871
4900
  if (typeof fullPath !== "string") {
4872
- 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
+ );
4873
4904
  }
4874
4905
  await this.database.delete(fullPath);
4875
4906
  };
@@ -5073,7 +5104,11 @@ var Resolver = class {
5073
5104
  collection,
5074
5105
  doc?._rawData
5075
5106
  );
5076
- await this.database.put(realPath, { ...oldDoc, ...params }, collection.name);
5107
+ await this.database.put(
5108
+ realPath,
5109
+ { ...oldDoc, ...params },
5110
+ collection.name
5111
+ );
5077
5112
  return this.getDocument(realPath);
5078
5113
  };
5079
5114
  /**
@@ -5191,10 +5226,30 @@ var Resolver = class {
5191
5226
  docsWithRefs
5192
5227
  )) {
5193
5228
  let refDoc = await this.getRaw(pathToDocWithRef);
5229
+ let hasUpdate = false;
5194
5230
  for (const path7 of referencePaths) {
5195
- refDoc = updateObjectWithJsonPath(refDoc, path7, 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
+ );
5196
5252
  }
5197
- await this.database.put(pathToDocWithRef, refDoc, collection2);
5198
5253
  }
5199
5254
  }
5200
5255
  }
@@ -5214,6 +5269,9 @@ var Resolver = class {
5214
5269
  collection?.path,
5215
5270
  args.params.relativePath
5216
5271
  );
5272
+ if (newRealPath === realPath) {
5273
+ return doc;
5274
+ }
5217
5275
  await this.database.put(newRealPath, doc._rawData, collection.name);
5218
5276
  await this.deleteDocument(realPath);
5219
5277
  const collRefs = await this.findReferences(realPath, collection);
@@ -5222,20 +5280,38 @@ var Resolver = class {
5222
5280
  docsWithRefs
5223
5281
  )) {
5224
5282
  let docWithRef = await this.getRaw(pathToDocWithRef);
5283
+ let hasUpdate = false;
5225
5284
  for (const path7 of referencePaths) {
5226
- docWithRef = updateObjectWithJsonPath(
5285
+ const { object: object2, updated } = updateObjectWithJsonPath(
5227
5286
  docWithRef,
5228
5287
  path7,
5288
+ realPath,
5229
5289
  newRealPath
5230
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
+ );
5231
5306
  }
5232
- await this.database.put(pathToDocWithRef, docWithRef, collection2);
5233
5307
  }
5234
5308
  }
5235
5309
  return this.getDocument(newRealPath);
5236
5310
  }
5237
5311
  if (alreadyExists === false) {
5238
- 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
+ );
5239
5315
  }
5240
5316
  return this.updateResolveDocument({
5241
5317
  collection,
@@ -6577,8 +6653,35 @@ var Database = class {
6577
6653
  ]
6578
6654
  }
6579
6655
  };
6580
- if (collection.fields) {
6581
- 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) {
6582
6685
  if (field.indexed !== void 0 && field.indexed === false || field.type === "object") {
6583
6686
  continue;
6584
6687
  }
@@ -6879,13 +6982,14 @@ var Database = class {
6879
6982
  documentPaths,
6880
6983
  async (collection, documentPaths2) => {
6881
6984
  if (collection && !collection.isDetached) {
6882
- await _indexContent(
6883
- this,
6884
- this.contentLevel,
6885
- documentPaths2,
6985
+ await _indexContent({
6986
+ database: this,
6987
+ level: this.contentLevel,
6988
+ documentPaths: documentPaths2,
6886
6989
  enqueueOps,
6887
- collection
6888
- );
6990
+ collection,
6991
+ isPartialReindex: true
6992
+ });
6889
6993
  }
6890
6994
  }
6891
6995
  );
@@ -6992,20 +7096,26 @@ var Database = class {
6992
7096
  );
6993
7097
  const doc = await level2.keys({ limit: 1 }).next();
6994
7098
  if (!doc) {
6995
- await _indexContent(
6996
- this,
6997
- level2,
6998
- contentPaths,
7099
+ await _indexContent({
7100
+ database: this,
7101
+ level: level2,
7102
+ documentPaths: contentPaths,
6999
7103
  enqueueOps,
7000
7104
  collection,
7001
- userFields.map((field) => [
7105
+ passwordFields: userFields.map((field) => [
7002
7106
  ...field.path,
7003
7107
  field.passwordFieldName
7004
7108
  ])
7005
- );
7109
+ });
7006
7110
  }
7007
7111
  } else {
7008
- await _indexContent(this, level, contentPaths, enqueueOps, collection);
7112
+ await _indexContent({
7113
+ database: this,
7114
+ level,
7115
+ documentPaths: contentPaths,
7116
+ enqueueOps,
7117
+ collection
7118
+ });
7009
7119
  }
7010
7120
  }
7011
7121
  );
@@ -7144,7 +7254,15 @@ var hashPasswordValues = async (data, passwordFields) => Promise.all(
7144
7254
  )
7145
7255
  );
7146
7256
  var isGitKeep = (filepath, collection) => filepath.endsWith(`.gitkeep.${collection?.format || "md"}`);
7147
- 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
+ }) => {
7148
7266
  let collectionIndexDefinitions;
7149
7267
  let collectionPath;
7150
7268
  if (collection) {
@@ -7185,40 +7303,42 @@ var _indexContent = async (database, level, documentPaths, enqueueOps, collectio
7185
7303
  normalizedPath,
7186
7304
  collectionPath || ""
7187
7305
  );
7188
- const item = await rootSublevel.get(normalizedPath);
7189
- if (item) {
7190
- await database.contentLevel.batch([
7191
- ...makeRefOpsForDocument(
7192
- normalizedPath,
7193
- collection?.name,
7194
- collectionReferences,
7195
- item,
7196
- "del",
7197
- level
7198
- ),
7199
- ...makeIndexOpsForDocument(
7200
- normalizedPath,
7201
- collection.name,
7202
- collectionIndexDefinitions,
7203
- item,
7204
- "del",
7205
- level
7206
- ),
7207
- // folder indices
7208
- ...makeIndexOpsForDocument(
7209
- normalizedPath,
7210
- `${collection.name}_${folderKey}`,
7211
- collectionIndexDefinitions,
7212
- item,
7213
- "del",
7214
- level
7215
- ),
7216
- {
7217
- type: "del",
7218
- key: normalizedPath,
7219
- sublevel: rootSublevel
7220
- }
7221
- ]);
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
+ }
7222
7342
  }
7223
7343
  if (!isGitKeep(filepath, collection)) {
7224
7344
  await enqueueOps([
@@ -7454,8 +7574,8 @@ import path6 from "path";
7454
7574
  import normalize from "normalize-path";
7455
7575
  var FilesystemBridge = class {
7456
7576
  constructor(rootPath, outputPath) {
7457
- this.rootPath = rootPath || "";
7458
- this.outputPath = outputPath || rootPath;
7577
+ this.rootPath = path6.resolve(rootPath);
7578
+ this.outputPath = outputPath ? path6.resolve(outputPath) : this.rootPath;
7459
7579
  }
7460
7580
  async glob(pattern, extension) {
7461
7581
  const basePath = path6.join(this.outputPath, ...pattern.split("/"));
@@ -7467,19 +7587,19 @@ var FilesystemBridge = class {
7467
7587
  }
7468
7588
  );
7469
7589
  const posixRootPath = normalize(this.outputPath);
7470
- return items.map((item) => {
7471
- return item.replace(posixRootPath, "").replace(/^\/|\/$/g, "");
7472
- });
7590
+ return items.map(
7591
+ (item) => item.substring(posixRootPath.length).replace(/^\/|\/$/g, "")
7592
+ );
7473
7593
  }
7474
7594
  async delete(filepath) {
7475
7595
  await fs2.remove(path6.join(this.outputPath, filepath));
7476
7596
  }
7477
7597
  async get(filepath) {
7478
- return fs2.readFileSync(path6.join(this.outputPath, filepath)).toString();
7598
+ return (await fs2.readFile(path6.join(this.outputPath, filepath))).toString();
7479
7599
  }
7480
7600
  async put(filepath, data, basePathOverride) {
7481
7601
  const basePath = basePathOverride || this.outputPath;
7482
- await fs2.outputFileSync(path6.join(basePath, filepath), data);
7602
+ await fs2.outputFile(path6.join(basePath, filepath), data);
7483
7603
  }
7484
7604
  };
7485
7605
  var AuditFileSystemBridge = class extends FilesystemBridge {