@tinacms/graphql 0.0.0-e0ddb8c-20241004065742 → 0.0.0-e27c017-20250619233313

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
@@ -68,6 +68,15 @@ var SysFieldDefinition = {
68
68
  selectionSet: {
69
69
  kind: "SelectionSet",
70
70
  selections: [
71
+ // {
72
+ // kind: 'Field' as const,
73
+ // name: {
74
+ // kind: 'Name' as const,
75
+ // value: 'title',
76
+ // },
77
+ // arguments: [],
78
+ // directives: [],
79
+ // },
71
80
  {
72
81
  kind: "Field",
73
82
  name: {
@@ -86,6 +95,15 @@ var SysFieldDefinition = {
86
95
  arguments: [],
87
96
  directives: []
88
97
  },
98
+ {
99
+ kind: "Field",
100
+ name: {
101
+ kind: "Name",
102
+ value: "hasReferences"
103
+ },
104
+ arguments: [],
105
+ directives: []
106
+ },
89
107
  {
90
108
  kind: "Field",
91
109
  name: {
@@ -126,6 +144,10 @@ var SysFieldDefinition = {
126
144
  }
127
145
  };
128
146
  var astBuilder = {
147
+ /**
148
+ * `FormFieldBuilder` acts as a shortcut to building an entire `ObjectTypeDefinition`, we use this
149
+ * because all Tina field objects share a common set of fields ('name', 'label', 'component')
150
+ */
129
151
  FormFieldBuilder: ({
130
152
  name,
131
153
  additionalFields
@@ -349,6 +371,8 @@ var astBuilder = {
349
371
  kind: "Name",
350
372
  value: name
351
373
  },
374
+ // @ts-ignore FIXME; this is being handled properly but we're lying to
375
+ // ts and then fixing it in the `extractInlineTypes` function
352
376
  fields
353
377
  }),
354
378
  UnionTypeDefinition: ({
@@ -361,6 +385,8 @@ var astBuilder = {
361
385
  value: name
362
386
  },
363
387
  directives: [],
388
+ // @ts-ignore FIXME; this is being handled properly but we're lying to
389
+ // ts and then fixing it in the `extractInlineTypes` function
364
390
  types: types.map((name2) => ({
365
391
  kind: "NamedType",
366
392
  name: {
@@ -457,8 +483,11 @@ var astBuilder = {
457
483
  string: "String",
458
484
  boolean: "Boolean",
459
485
  number: "Float",
486
+ // FIXME - needs to be float or int
460
487
  datetime: "String",
488
+ // FIXME
461
489
  image: "String",
490
+ // FIXME
462
491
  text: "String"
463
492
  };
464
493
  return scalars[type];
@@ -957,8 +986,7 @@ var astBuilder = {
957
986
  }
958
987
  };
959
988
  var capitalize = (s) => {
960
- if (typeof s !== "string")
961
- return "";
989
+ if (typeof s !== "string") return "";
962
990
  return s.charAt(0).toUpperCase() + s.slice(1);
963
991
  };
964
992
  var extractInlineTypes = (item) => {
@@ -1001,41 +1029,6 @@ function* walk(maybeNode, visited = /* @__PURE__ */ new WeakSet()) {
1001
1029
  yield maybeNode;
1002
1030
  visited.add(maybeNode);
1003
1031
  }
1004
- function addNamespaceToSchema(maybeNode, namespace = []) {
1005
- if (typeof maybeNode === "string") {
1006
- return maybeNode;
1007
- }
1008
- if (typeof maybeNode === "boolean") {
1009
- return maybeNode;
1010
- }
1011
- const newNode = maybeNode;
1012
- const keys = Object.keys(maybeNode);
1013
- Object.values(maybeNode).map((m, index) => {
1014
- const key = keys[index];
1015
- if (Array.isArray(m)) {
1016
- newNode[key] = m.map((element) => {
1017
- if (!element) {
1018
- return;
1019
- }
1020
- if (!element.hasOwnProperty("name")) {
1021
- return element;
1022
- }
1023
- const value = element.name || element.value;
1024
- return addNamespaceToSchema(element, [...namespace, value]);
1025
- });
1026
- } else {
1027
- if (!m) {
1028
- return;
1029
- }
1030
- if (!m.hasOwnProperty("name")) {
1031
- newNode[key] = m;
1032
- } else {
1033
- newNode[key] = addNamespaceToSchema(m, [...namespace, m.name]);
1034
- }
1035
- }
1036
- });
1037
- return { ...newNode, namespace };
1038
- }
1039
1032
  var generateNamespacedFieldName = (names, suffix = "") => {
1040
1033
  return (suffix ? [...names, suffix] : names).map(capitalize).join("");
1041
1034
  };
@@ -1195,6 +1188,11 @@ var scalarDefinitions = [
1195
1188
  required: true,
1196
1189
  type: astBuilder.TYPES.String
1197
1190
  }),
1191
+ astBuilder.FieldDefinition({
1192
+ name: "hasReferences",
1193
+ required: false,
1194
+ type: astBuilder.TYPES.Boolean
1195
+ }),
1198
1196
  astBuilder.FieldDefinition({
1199
1197
  name: "breadcrumbs",
1200
1198
  required: true,
@@ -1408,6 +1406,19 @@ var Builder = class {
1408
1406
  this.addToLookupMap = (lookup) => {
1409
1407
  this.lookupMap[lookup.type] = lookup;
1410
1408
  };
1409
+ /**
1410
+ * ```graphql
1411
+ * # ex.
1412
+ * {
1413
+ * getCollection(collection: $collection) {
1414
+ * name
1415
+ * documents {...}
1416
+ * }
1417
+ * }
1418
+ * ```
1419
+ *
1420
+ * @param collections
1421
+ */
1411
1422
  this.buildCollectionDefinition = async (collections) => {
1412
1423
  const name = "collection";
1413
1424
  const typeName = "Collection";
@@ -1478,6 +1489,19 @@ var Builder = class {
1478
1489
  required: true
1479
1490
  });
1480
1491
  };
1492
+ /**
1493
+ * ```graphql
1494
+ * # ex.
1495
+ * {
1496
+ * getCollections {
1497
+ * name
1498
+ * documents {...}
1499
+ * }
1500
+ * }
1501
+ * ```
1502
+ *
1503
+ * @param collections
1504
+ */
1481
1505
  this.buildMultiCollectionDefinition = async (collections) => {
1482
1506
  const name = "collections";
1483
1507
  const typeName = "Collection";
@@ -1488,6 +1512,17 @@ var Builder = class {
1488
1512
  required: true
1489
1513
  });
1490
1514
  };
1515
+ /**
1516
+ * ```graphql
1517
+ * # ex.
1518
+ * {
1519
+ * node(id: $id) {
1520
+ * id
1521
+ * data {...}
1522
+ * }
1523
+ * }
1524
+ * ```
1525
+ */
1491
1526
  this.multiNodeDocument = async () => {
1492
1527
  const name = "node";
1493
1528
  const args = [
@@ -1508,6 +1543,19 @@ var Builder = class {
1508
1543
  required: true
1509
1544
  });
1510
1545
  };
1546
+ /**
1547
+ * ```graphql
1548
+ * # ex.
1549
+ * {
1550
+ * getDocument(collection: $collection, relativePath: $relativePath) {
1551
+ * id
1552
+ * data {...}
1553
+ * }
1554
+ * }
1555
+ * ```
1556
+ *
1557
+ * @param collections
1558
+ */
1511
1559
  this.multiCollectionDocument = async (collections) => {
1512
1560
  const name = "document";
1513
1561
  const args = [
@@ -1533,6 +1581,19 @@ var Builder = class {
1533
1581
  required: true
1534
1582
  });
1535
1583
  };
1584
+ /**
1585
+ * ```graphql
1586
+ * # ex.
1587
+ * {
1588
+ * addPendingDocument(collection: $collection, relativePath: $relativePath, params: $params) {
1589
+ * id
1590
+ * data {...}
1591
+ * }
1592
+ * }
1593
+ * ```
1594
+ *
1595
+ * @param collections
1596
+ */
1536
1597
  this.addMultiCollectionDocumentMutation = async () => {
1537
1598
  return astBuilder.FieldDefinition({
1538
1599
  name: "addPendingDocument",
@@ -1557,6 +1618,19 @@ var Builder = class {
1557
1618
  type: astBuilder.TYPES.MultiCollectionDocument
1558
1619
  });
1559
1620
  };
1621
+ /**
1622
+ * ```graphql
1623
+ * # ex.
1624
+ * {
1625
+ * createDocument(relativePath: $relativePath, params: $params) {
1626
+ * id
1627
+ * data {...}
1628
+ * }
1629
+ * }
1630
+ * ```
1631
+ *
1632
+ * @param collections
1633
+ */
1560
1634
  this.buildCreateCollectionDocumentMutation = async (collections) => {
1561
1635
  return astBuilder.FieldDefinition({
1562
1636
  name: "createDocument",
@@ -1584,6 +1658,19 @@ var Builder = class {
1584
1658
  type: astBuilder.TYPES.MultiCollectionDocument
1585
1659
  });
1586
1660
  };
1661
+ /**
1662
+ * ```graphql
1663
+ * # ex.
1664
+ * {
1665
+ * updateDocument(relativePath: $relativePath, params: $params) {
1666
+ * id
1667
+ * data {...}
1668
+ * }
1669
+ * }
1670
+ * ```
1671
+ *
1672
+ * @param collections
1673
+ */
1587
1674
  this.buildUpdateCollectionDocumentMutation = async (collections) => {
1588
1675
  return astBuilder.FieldDefinition({
1589
1676
  name: "updateDocument",
@@ -1611,6 +1698,19 @@ var Builder = class {
1611
1698
  type: astBuilder.TYPES.MultiCollectionDocument
1612
1699
  });
1613
1700
  };
1701
+ /**
1702
+ * ```graphql
1703
+ * # ex.
1704
+ * {
1705
+ * deleteDocument(relativePath: $relativePath, params: $params) {
1706
+ * id
1707
+ * data {...}
1708
+ * }
1709
+ * }
1710
+ * ```
1711
+ *
1712
+ * @param collections
1713
+ */
1614
1714
  this.buildDeleteCollectionDocumentMutation = async (collections) => {
1615
1715
  return astBuilder.FieldDefinition({
1616
1716
  name: "deleteDocument",
@@ -1630,6 +1730,19 @@ var Builder = class {
1630
1730
  type: astBuilder.TYPES.MultiCollectionDocument
1631
1731
  });
1632
1732
  };
1733
+ /**
1734
+ * ```graphql
1735
+ * # ex.
1736
+ * {
1737
+ * createFolder(folderName: $folderName, params: $params) {
1738
+ * id
1739
+ * data {...}
1740
+ * }
1741
+ * }
1742
+ * ```
1743
+ *
1744
+ * @param collections
1745
+ */
1633
1746
  this.buildCreateCollectionFolderMutation = async () => {
1634
1747
  return astBuilder.FieldDefinition({
1635
1748
  name: "createFolder",
@@ -1649,6 +1762,19 @@ var Builder = class {
1649
1762
  type: astBuilder.TYPES.MultiCollectionDocument
1650
1763
  });
1651
1764
  };
1765
+ /**
1766
+ * ```graphql
1767
+ * # ex.
1768
+ * {
1769
+ * getPostDocument(relativePath: $relativePath) {
1770
+ * id
1771
+ * data {...}
1772
+ * }
1773
+ * }
1774
+ * ```
1775
+ *
1776
+ * @param collection
1777
+ */
1652
1778
  this.collectionDocument = async (collection) => {
1653
1779
  const name = NAMER.queryName([collection.name]);
1654
1780
  const type = await this._buildCollectionDocumentType(collection);
@@ -1709,6 +1835,20 @@ var Builder = class {
1709
1835
  const args = [];
1710
1836
  return astBuilder.FieldDefinition({ type, name, args, required: false });
1711
1837
  };
1838
+ /**
1839
+ * Turns a collection into a fragment that gets updated on build. This fragment does not resolve references
1840
+ * ```graphql
1841
+ * # ex.
1842
+ * fragment AuthorsParts on Authors {
1843
+ * name
1844
+ * avatar
1845
+ * ...
1846
+ * }
1847
+ * ```
1848
+ *
1849
+ * @public
1850
+ * @param collection a TinaCloud collection
1851
+ */
1712
1852
  this.collectionFragment = async (collection) => {
1713
1853
  const name = NAMER.dataTypeName(collection.namespace);
1714
1854
  const fragmentName = NAMER.fragmentName(collection.namespace);
@@ -1722,6 +1862,20 @@ var Builder = class {
1722
1862
  selections: filterSelections(selections)
1723
1863
  });
1724
1864
  };
1865
+ /**
1866
+ * Given a collection this function returns its selections set. For example for Post this would return
1867
+ *
1868
+ * "
1869
+ * body
1870
+ * title
1871
+ * ... on Author {
1872
+ * name
1873
+ * heroImg
1874
+ * }
1875
+ *
1876
+ * But in the AST format
1877
+ *
1878
+ * */
1725
1879
  this._getCollectionFragmentSelections = async (collection, depth) => {
1726
1880
  const selections = [];
1727
1881
  selections.push({
@@ -1803,9 +1957,9 @@ var Builder = class {
1803
1957
  ]
1804
1958
  });
1805
1959
  }
1960
+ // TODO: Should we throw here?
1806
1961
  case "reference":
1807
- if (depth >= this.maxDepth)
1808
- return false;
1962
+ if (depth >= this.maxDepth) return false;
1809
1963
  if (!("collections" in field)) {
1810
1964
  return false;
1811
1965
  }
@@ -1837,6 +1991,7 @@ var Builder = class {
1837
1991
  name: field.name,
1838
1992
  selections: [
1839
1993
  ...selections,
1994
+ // This is ... on Document { id }
1840
1995
  {
1841
1996
  kind: "InlineFragment",
1842
1997
  typeCondition: {
@@ -1867,6 +2022,19 @@ var Builder = class {
1867
2022
  });
1868
2023
  }
1869
2024
  };
2025
+ /**
2026
+ * ```graphql
2027
+ * # ex.
2028
+ * mutation {
2029
+ * updatePostDocument(relativePath: $relativePath, params: $params) {
2030
+ * id
2031
+ * data {...}
2032
+ * }
2033
+ * }
2034
+ * ```
2035
+ *
2036
+ * @param collection
2037
+ */
1870
2038
  this.updateCollectionDocumentMutation = async (collection) => {
1871
2039
  return astBuilder.FieldDefinition({
1872
2040
  type: await this._buildCollectionDocumentType(collection),
@@ -1886,6 +2054,19 @@ var Builder = class {
1886
2054
  ]
1887
2055
  });
1888
2056
  };
2057
+ /**
2058
+ * ```graphql
2059
+ * # ex.
2060
+ * mutation {
2061
+ * createPostDocument(relativePath: $relativePath, params: $params) {
2062
+ * id
2063
+ * data {...}
2064
+ * }
2065
+ * }
2066
+ * ```
2067
+ *
2068
+ * @param collection
2069
+ */
1889
2070
  this.createCollectionDocumentMutation = async (collection) => {
1890
2071
  return astBuilder.FieldDefinition({
1891
2072
  type: await this._buildCollectionDocumentType(collection),
@@ -1905,6 +2086,22 @@ var Builder = class {
1905
2086
  ]
1906
2087
  });
1907
2088
  };
2089
+ /**
2090
+ * ```graphql
2091
+ * # ex.
2092
+ * {
2093
+ * getPostList(first: 10) {
2094
+ * edges {
2095
+ * node {
2096
+ * id
2097
+ * }
2098
+ * }
2099
+ * }
2100
+ * }
2101
+ * ```
2102
+ *
2103
+ * @param collection
2104
+ */
1908
2105
  this.collectionDocumentList = async (collection) => {
1909
2106
  const connectionName = NAMER.referenceConnectionType(collection.namespace);
1910
2107
  this.addToLookupMap({
@@ -1920,6 +2117,10 @@ var Builder = class {
1920
2117
  collection
1921
2118
  });
1922
2119
  };
2120
+ /**
2121
+ * GraphQL type definitions which remain unchanged regardless
2122
+ * of the supplied Tina schema. Ex. "node" interface
2123
+ */
1923
2124
  this.buildStaticDefinitions = () => staticDefinitions;
1924
2125
  this._buildCollectionDocumentType = async (collection, suffix = "", extraFields = [], extraInterfaces = []) => {
1925
2126
  const documentTypeName = NAMER.documentTypeName(collection.namespace);
@@ -2424,6 +2625,7 @@ var Builder = class {
2424
2625
  name: NAMER.dataFilterTypeName(namespace),
2425
2626
  fields: await sequential(collections, async (collection2) => {
2426
2627
  return astBuilder.InputValueDefinition({
2628
+ // @ts-ignore
2427
2629
  name: collection2.name,
2428
2630
  type: NAMER.dataFilterTypeName(collection2.namespace)
2429
2631
  });
@@ -2612,7 +2814,8 @@ Visit https://tina.io/docs/errors/ui-not-supported/ for more information
2612
2814
  ]
2613
2815
  });
2614
2816
  };
2615
- this.maxDepth = config?.tinaSchema.schema?.config?.client?.referenceDepth ?? 2;
2817
+ this.maxDepth = // @ts-ignore
2818
+ config?.tinaSchema.schema?.config?.client?.referenceDepth ?? 2;
2616
2819
  this.tinaSchema = config.tinaSchema;
2617
2820
  this.lookupMap = {};
2618
2821
  }
@@ -2623,8 +2826,7 @@ Visit https://tina.io/docs/errors/ui-not-supported/ for more information
2623
2826
  selections.push(field);
2624
2827
  });
2625
2828
  const filteredSelections = filterSelections(selections);
2626
- if (!filteredSelections.length)
2627
- return false;
2829
+ if (!filteredSelections.length) return false;
2628
2830
  return astBuilder.InlineFragmentDefinition({
2629
2831
  selections: filteredSelections,
2630
2832
  name: NAMER.dataTypeName(template.namespace)
@@ -2661,6 +2863,7 @@ var filterSelections = (arr) => {
2661
2863
  import { TinaSchema } from "@tinacms/schema-tools";
2662
2864
 
2663
2865
  // src/schema/validate.ts
2866
+ import { addNamespaceToSchema } from "@tinacms/schema-tools";
2664
2867
  import deepClone from "lodash.clonedeep";
2665
2868
  import * as yup2 from "yup";
2666
2869
  import {
@@ -2707,6 +2910,7 @@ var validationCollectionsPathAndMatch = (collections) => {
2707
2910
  }).map((x) => `${x.path}${x.format || "md"}`);
2708
2911
  if (noMatchCollections.length !== new Set(noMatchCollections).size) {
2709
2912
  throw new Error(
2913
+ // TODO: add a link to the docs
2710
2914
  "Two collections without match can not have the same `path`. Please make the `path` unique or add a matches property to the collection."
2711
2915
  );
2712
2916
  }
@@ -2815,7 +3019,7 @@ var validateField = async (field) => {
2815
3019
  // package.json
2816
3020
  var package_default = {
2817
3021
  name: "@tinacms/graphql",
2818
- version: "1.5.4",
3022
+ version: "1.5.18",
2819
3023
  main: "dist/index.js",
2820
3024
  module: "dist/index.mjs",
2821
3025
  typings: "dist/index.d.ts",
@@ -2841,9 +3045,8 @@ var package_default = {
2841
3045
  types: "pnpm tsc",
2842
3046
  build: "tinacms-scripts build",
2843
3047
  docs: "pnpm typedoc",
2844
- serve: "pnpm nodemon dist/server.js",
2845
- test: "jest",
2846
- "test-watch": "jest --watch"
3048
+ test: "vitest run",
3049
+ "test-watch": "vitest"
2847
3050
  },
2848
3051
  dependencies: {
2849
3052
  "@iarna/toml": "^2.2.5",
@@ -2851,22 +3054,22 @@ var package_default = {
2851
3054
  "@tinacms/schema-tools": "workspace:*",
2852
3055
  "abstract-level": "^1.0.4",
2853
3056
  "date-fns": "^2.30.0",
2854
- "fast-glob": "^3.3.2",
2855
- "fs-extra": "^11.2.0",
3057
+ "fast-glob": "^3.3.3",
3058
+ "fs-extra": "^11.3.0",
2856
3059
  "glob-parent": "^6.0.2",
2857
3060
  graphql: "15.8.0",
2858
3061
  "gray-matter": "^4.0.3",
2859
- "isomorphic-git": "^1.27.1",
3062
+ "isomorphic-git": "^1.29.0",
2860
3063
  "js-sha1": "^0.6.0",
2861
3064
  "js-yaml": "^3.14.1",
2862
- "jsonpath-plus": "^6.0.1",
3065
+ "jsonpath-plus": "10.1.0",
2863
3066
  "lodash.clonedeep": "^4.5.0",
2864
3067
  "lodash.set": "^4.3.2",
2865
3068
  "lodash.uniqby": "^4.7.0",
2866
3069
  "many-level": "^2.0.0",
2867
3070
  micromatch: "4.0.8",
2868
3071
  "normalize-path": "^3.0.0",
2869
- "readable-stream": "^4.5.2",
3072
+ "readable-stream": "^4.7.0",
2870
3073
  scmp: "^2.1.0",
2871
3074
  yup: "^0.32.11"
2872
3075
  },
@@ -2884,24 +3087,22 @@ var package_default = {
2884
3087
  "@types/estree": "^0.0.50",
2885
3088
  "@types/express": "^4.17.21",
2886
3089
  "@types/fs-extra": "^9.0.13",
2887
- "@types/jest": "^26.0.24",
2888
3090
  "@types/js-yaml": "^3.12.10",
2889
3091
  "@types/lodash.camelcase": "^4.3.9",
2890
3092
  "@types/lodash.upperfirst": "^4.3.9",
2891
3093
  "@types/lru-cache": "^5.1.1",
2892
3094
  "@types/mdast": "^3.0.15",
2893
3095
  "@types/micromatch": "^4.0.9",
2894
- "@types/node": "^22.7.4",
3096
+ "@types/node": "^22.13.1",
2895
3097
  "@types/normalize-path": "^3.0.2",
2896
3098
  "@types/ws": "^7.4.7",
2897
3099
  "@types/yup": "^0.29.14",
2898
- jest: "^29.7.0",
2899
- "jest-diff": "^29.7.0",
2900
3100
  "jest-file-snapshot": "^0.5.0",
2901
- "jest-matcher-utils": "^29.7.0",
2902
3101
  "memory-level": "^1.0.0",
2903
- nodemon: "3.1.4",
2904
- typescript: "^5.6.2"
3102
+ typescript: "^5.7.3",
3103
+ vite: "^4.5.9",
3104
+ vitest: "^0.32.4",
3105
+ zod: "^3.24.2"
2905
3106
  }
2906
3107
  };
2907
3108
 
@@ -2972,6 +3173,7 @@ var _buildFragments = async (builder, tinaSchema) => {
2972
3173
  const fragDoc = {
2973
3174
  kind: "Document",
2974
3175
  definitions: uniqBy2(
3176
+ // @ts-ignore
2975
3177
  extractInlineTypes(fragmentDefinitionsFields),
2976
3178
  (node) => node.name.value
2977
3179
  )
@@ -2994,6 +3196,7 @@ var _buildQueries = async (builder, tinaSchema) => {
2994
3196
  fragName,
2995
3197
  queryName: queryListName,
2996
3198
  filterType: queryFilterTypeName,
3199
+ // look for flag to see if the data layer is enabled
2997
3200
  dataLayer: Boolean(
2998
3201
  tinaSchema.config?.meta?.flags?.find((x) => x === "experimentalData")
2999
3202
  )
@@ -3003,6 +3206,7 @@ var _buildQueries = async (builder, tinaSchema) => {
3003
3206
  const queryDoc = {
3004
3207
  kind: "Document",
3005
3208
  definitions: uniqBy2(
3209
+ // @ts-ignore
3006
3210
  extractInlineTypes(operationsDefinitions),
3007
3211
  (node) => node.name.value
3008
3212
  )
@@ -3054,7 +3258,9 @@ var _buildSchema = async (builder, tinaSchema) => {
3054
3258
  await builder.buildCreateCollectionFolderMutation()
3055
3259
  );
3056
3260
  await sequential(collections, async (collection) => {
3057
- queryTypeDefinitionFields.push(await builder.collectionDocument(collection));
3261
+ queryTypeDefinitionFields.push(
3262
+ await builder.collectionDocument(collection)
3263
+ );
3058
3264
  if (collection.isAuthCollection) {
3059
3265
  queryTypeDefinitionFields.push(
3060
3266
  await builder.authenticationCollectionDocument(collection)
@@ -3091,6 +3297,7 @@ var _buildSchema = async (builder, tinaSchema) => {
3091
3297
  return {
3092
3298
  kind: "Document",
3093
3299
  definitions: uniqBy2(
3300
+ // @ts-ignore
3094
3301
  extractInlineTypes(definitions),
3095
3302
  (node) => node.name.value
3096
3303
  )
@@ -3103,394 +3310,158 @@ import { graphql, buildASTSchema, getNamedType, GraphQLError as GraphQLError4 }
3103
3310
  // src/resolver/index.ts
3104
3311
  import path3 from "path";
3105
3312
  import isValid from "date-fns/isValid/index.js";
3313
+ import { JSONPath as JSONPath2 } from "jsonpath-plus";
3106
3314
 
3107
3315
  // src/mdx/index.ts
3108
- import { parseMDX, stringifyMDX } from "@tinacms/mdx";
3316
+ import { parseMDX, serializeMDX } from "@tinacms/mdx";
3109
3317
 
3110
- // src/resolver/error.ts
3111
- var TinaGraphQLError = class extends Error {
3112
- constructor(message, extensions) {
3113
- super(message);
3114
- if (!this.name) {
3115
- Object.defineProperty(this, "name", { value: "TinaGraphQLError" });
3116
- }
3117
- this.extensions = { ...extensions };
3118
- }
3318
+ // src/resolver/index.ts
3319
+ import { GraphQLError as GraphQLError2 } from "graphql";
3320
+
3321
+ // src/database/datalayer.ts
3322
+ import { JSONPath } from "jsonpath-plus";
3323
+ import sha from "js-sha1";
3324
+
3325
+ // src/database/level.ts
3326
+ var ARRAY_ITEM_VALUE_SEPARATOR = ",";
3327
+ var INDEX_KEY_FIELD_SEPARATOR = "";
3328
+ var CONTENT_ROOT_PREFIX = "~";
3329
+ var SUBLEVEL_OPTIONS = {
3330
+ separator: INDEX_KEY_FIELD_SEPARATOR,
3331
+ valueEncoding: "json"
3119
3332
  };
3120
- var TinaFetchError = class extends Error {
3121
- constructor(message, args) {
3122
- super(message);
3123
- this.name = "TinaFetchError";
3124
- this.collection = args.collection;
3125
- this.stack = args.stack;
3126
- this.file = args.file;
3127
- this.originalError = args.originalError;
3333
+ var LevelProxyHandler = {
3334
+ get: function(target, property) {
3335
+ if (!target[property]) {
3336
+ throw new Error(`The property, ${property.toString()}, doesn't exist`);
3337
+ }
3338
+ if (typeof target[property] !== "function") {
3339
+ throw new Error(
3340
+ `The property, ${property.toString()}, is not a function`
3341
+ );
3342
+ }
3343
+ if (property === "get") {
3344
+ return async (...args) => {
3345
+ let result;
3346
+ try {
3347
+ result = await target[property].apply(target, args);
3348
+ } catch (e) {
3349
+ if (e.code !== "LEVEL_NOT_FOUND") {
3350
+ throw e;
3351
+ }
3352
+ }
3353
+ return result;
3354
+ };
3355
+ } else if (property === "sublevel") {
3356
+ return (...args) => {
3357
+ return new Proxy(
3358
+ // eslint-disable-next-line prefer-spread
3359
+ target[property].apply(target, args),
3360
+ LevelProxyHandler
3361
+ );
3362
+ };
3363
+ } else {
3364
+ return (...args) => target[property].apply(target, args);
3365
+ }
3128
3366
  }
3129
3367
  };
3130
- var TinaQueryError = class extends TinaFetchError {
3131
- constructor(args) {
3132
- super(
3133
- `Error querying file ${args.file} from collection ${args.collection}. ${auditMessage(args.includeAuditMessage)}`,
3134
- args
3135
- );
3368
+ var LevelProxy = class {
3369
+ constructor(level) {
3370
+ return new Proxy(level, LevelProxyHandler);
3136
3371
  }
3137
3372
  };
3138
- var TinaParseDocumentError = class extends TinaFetchError {
3139
- constructor(args) {
3140
- super(
3141
- `Error parsing file ${args.file} from collection ${args.collection}. ${auditMessage(args.includeAuditMessage)}`,
3142
- args
3373
+
3374
+ // src/database/datalayer.ts
3375
+ import path2 from "path";
3376
+
3377
+ // src/database/util.ts
3378
+ import toml from "@iarna/toml";
3379
+ import {
3380
+ normalizePath
3381
+ } from "@tinacms/schema-tools";
3382
+ import matter from "gray-matter";
3383
+ import yaml from "js-yaml";
3384
+ import path from "path";
3385
+ import micromatch from "micromatch";
3386
+
3387
+ // src/database/alias-utils.ts
3388
+ var replaceBlockAliases = (template, item) => {
3389
+ const output = { ...item };
3390
+ const templateKey = template.templateKey || "_template";
3391
+ const templateName = output[templateKey];
3392
+ const matchingTemplate = template.templates.find(
3393
+ (t) => t.nameOverride == templateName || t.name == templateName
3394
+ );
3395
+ if (!matchingTemplate) {
3396
+ throw new Error(
3397
+ `Block template "${templateName}" is not defined for field "${template.name}"`
3143
3398
  );
3144
3399
  }
3145
- toString() {
3146
- return super.toString() + "\n OriginalError: \n" + this.originalError.toString();
3400
+ output._template = matchingTemplate.name;
3401
+ if (templateKey != "_template") {
3402
+ delete output[templateKey];
3147
3403
  }
3404
+ return output;
3148
3405
  };
3149
- var auditMessage = (includeAuditMessage = true) => includeAuditMessage ? `Please run "tinacms audit" or add the --verbose option for more info` : "";
3150
- var handleFetchErrorError = (e, verbose) => {
3151
- if (e instanceof Error) {
3152
- if (e instanceof TinaFetchError) {
3153
- if (verbose) {
3154
- console.log(e.toString());
3155
- console.log(e);
3156
- console.log(e.stack);
3406
+ var replaceNameOverrides = (template, obj) => {
3407
+ if (template.list) {
3408
+ return obj.map((item) => {
3409
+ if (isBlockField(template)) {
3410
+ item = replaceBlockAliases(template, item);
3157
3411
  }
3158
- }
3412
+ return _replaceNameOverrides(
3413
+ getTemplateForData(template, item).fields,
3414
+ item
3415
+ );
3416
+ });
3159
3417
  } else {
3160
- console.error(e);
3418
+ return _replaceNameOverrides(getTemplateForData(template, obj).fields, obj);
3161
3419
  }
3162
- throw e;
3163
3420
  };
3164
-
3165
- // src/resolver/filter-utils.ts
3166
- var resolveReferences = async (filter, fields, resolver) => {
3167
- for (const fieldKey of Object.keys(filter)) {
3168
- const fieldDefinition = fields.find(
3169
- (f) => f.name === fieldKey
3421
+ function isBlockField(field) {
3422
+ return field && field.type === "object" && field.templates?.length > 0;
3423
+ }
3424
+ var _replaceNameOverrides = (fields, obj) => {
3425
+ const output = {};
3426
+ Object.keys(obj).forEach((key) => {
3427
+ const field = fields.find(
3428
+ (fieldWithMatchingAlias) => (fieldWithMatchingAlias?.nameOverride || fieldWithMatchingAlias?.name) === key
3170
3429
  );
3171
- if (fieldDefinition) {
3172
- if (fieldDefinition.type === "reference") {
3173
- const { edges, values } = await resolver(filter, fieldDefinition);
3174
- if (edges.length === 1) {
3175
- filter[fieldKey] = {
3176
- eq: values[0]
3177
- };
3178
- } else if (edges.length > 1) {
3179
- filter[fieldKey] = {
3180
- in: values
3181
- };
3182
- } else {
3183
- filter[fieldKey] = {
3184
- eq: "___null___"
3185
- };
3186
- }
3187
- } else if (fieldDefinition.type === "object") {
3188
- if (fieldDefinition.templates) {
3189
- for (const templateName of Object.keys(filter[fieldKey])) {
3190
- const template = fieldDefinition.templates.find(
3191
- (template2) => !(typeof template2 === "string") && template2.name === templateName
3192
- );
3193
- if (template) {
3194
- await resolveReferences(
3195
- filter[fieldKey][templateName],
3196
- template.fields,
3197
- resolver
3198
- );
3199
- } else {
3200
- throw new Error(`Template ${templateName} not found`);
3201
- }
3202
- }
3203
- } else {
3204
- await resolveReferences(
3205
- filter[fieldKey],
3206
- fieldDefinition.fields,
3207
- resolver
3208
- );
3209
- }
3430
+ output[field?.name || key] = field?.type == "object" ? replaceNameOverrides(field, obj[key]) : obj[key];
3431
+ });
3432
+ return output;
3433
+ };
3434
+ var getTemplateForData = (field, data) => {
3435
+ if (field.templates?.length) {
3436
+ const templateKey = "_template";
3437
+ if (data[templateKey]) {
3438
+ const result = field.templates.find(
3439
+ (template) => template.nameOverride === data[templateKey] || template.name === data[templateKey]
3440
+ );
3441
+ if (result) {
3442
+ return result;
3210
3443
  }
3211
- } else {
3212
- throw new Error(`Unable to find field ${fieldKey}`);
3444
+ throw new Error(
3445
+ `Template "${data[templateKey]}" is not defined for field "${field.name}"`
3446
+ );
3213
3447
  }
3448
+ throw new Error(
3449
+ `Missing required key "${templateKey}" on field "${field.name}"`
3450
+ );
3451
+ } else {
3452
+ return field;
3214
3453
  }
3215
3454
  };
3216
- var collectConditionsForChildFields = (filterNode, fields, pathExpression, collectCondition) => {
3217
- for (const childFieldName of Object.keys(filterNode)) {
3218
- const childField = fields.find((field) => field.name === childFieldName);
3219
- if (!childField) {
3220
- throw new Error(`Unable to find type for field ${childFieldName}`);
3221
- }
3222
- collectConditionsForField(
3223
- childFieldName,
3224
- childField,
3225
- filterNode[childFieldName],
3226
- pathExpression,
3227
- collectCondition
3228
- );
3229
- }
3230
- };
3231
- var collectConditionsForObjectField = (fieldName, field, filterNode, pathExpression, collectCondition) => {
3232
- if (field.list && field.templates) {
3233
- for (const [filterKey, childFilterNode] of Object.entries(filterNode)) {
3234
- const template = field.templates.find(
3235
- (template2) => !(typeof template2 === "string") && template2.name === filterKey
3236
- );
3237
- const jsonPath = `${fieldName}[?(@._template=="${filterKey}")]`;
3238
- const filterPath = pathExpression ? `${pathExpression}.${jsonPath}` : jsonPath;
3239
- collectConditionsForChildFields(
3240
- childFilterNode,
3241
- template.fields,
3242
- filterPath,
3243
- collectCondition
3244
- );
3245
- }
3246
- } else {
3247
- const jsonPath = `${fieldName}${field.list ? "[*]" : ""}`;
3248
- const filterPath = pathExpression ? `${pathExpression}.${jsonPath}` : `${jsonPath}`;
3249
- collectConditionsForChildFields(
3250
- filterNode,
3251
- field.fields,
3252
- filterPath,
3253
- collectCondition
3254
- );
3255
- }
3256
- };
3257
- var collectConditionsForField = (fieldName, field, filterNode, pathExpression, collectCondition) => {
3258
- if (field.type === "object") {
3259
- collectConditionsForObjectField(
3260
- fieldName,
3261
- field,
3262
- filterNode,
3263
- pathExpression,
3264
- collectCondition
3265
- );
3266
- } else {
3267
- collectCondition({
3268
- filterPath: pathExpression ? `${pathExpression}.${fieldName}` : fieldName,
3269
- filterExpression: {
3270
- _type: field.type,
3271
- _list: !!field.list,
3272
- ...filterNode
3273
- }
3274
- });
3275
- }
3276
- };
3277
-
3278
- // src/resolver/media-utils.ts
3279
- var resolveMediaCloudToRelative = (value, config = { useRelativeMedia: true }, schema) => {
3280
- if (config && value) {
3281
- if (config.useRelativeMedia === true) {
3282
- return value;
3283
- }
3284
- if (hasTinaMediaConfig(schema) === true) {
3285
- const assetsURL = `https://${config.assetsHost}/${config.clientId}`;
3286
- if (typeof value === "string" && value.includes(assetsURL)) {
3287
- const cleanMediaRoot = cleanUpSlashes(
3288
- schema.config.media.tina.mediaRoot
3289
- );
3290
- const strippedURL = value.replace(assetsURL, "");
3291
- return `${cleanMediaRoot}${strippedURL}`;
3292
- }
3293
- if (Array.isArray(value)) {
3294
- return value.map((v) => {
3295
- if (!v || typeof v !== "string")
3296
- return v;
3297
- const cleanMediaRoot = cleanUpSlashes(
3298
- schema.config.media.tina.mediaRoot
3299
- );
3300
- const strippedURL = v.replace(assetsURL, "");
3301
- return `${cleanMediaRoot}${strippedURL}`;
3302
- });
3303
- }
3304
- return value;
3305
- }
3306
- return value;
3307
- } else {
3308
- return value;
3309
- }
3310
- };
3311
- var resolveMediaRelativeToCloud = (value, config = { useRelativeMedia: true }, schema) => {
3312
- if (config && value) {
3313
- if (config.useRelativeMedia === true) {
3314
- return value;
3315
- }
3316
- if (hasTinaMediaConfig(schema) === true) {
3317
- const cleanMediaRoot = cleanUpSlashes(schema.config.media.tina.mediaRoot);
3318
- if (typeof value === "string") {
3319
- const strippedValue = value.replace(cleanMediaRoot, "");
3320
- return `https://${config.assetsHost}/${config.clientId}${strippedValue}`;
3321
- }
3322
- if (Array.isArray(value)) {
3323
- return value.map((v) => {
3324
- if (!v || typeof v !== "string")
3325
- return v;
3326
- const strippedValue = v.replace(cleanMediaRoot, "");
3327
- return `https://${config.assetsHost}/${config.clientId}${strippedValue}`;
3328
- });
3329
- }
3330
- }
3331
- return value;
3332
- } else {
3333
- return value;
3334
- }
3335
- };
3336
- var cleanUpSlashes = (path7) => {
3337
- if (path7) {
3338
- return `/${path7.replace(/^\/+|\/+$/gm, "")}`;
3339
- }
3340
- return "";
3341
- };
3342
- var hasTinaMediaConfig = (schema) => {
3343
- if (!schema.config?.media?.tina)
3344
- return false;
3345
- if (typeof schema.config?.media?.tina?.publicFolder !== "string" && typeof schema.config?.media?.tina?.mediaRoot !== "string")
3346
- return false;
3347
- return true;
3348
- };
3349
-
3350
- // src/resolver/index.ts
3351
- import { GraphQLError as GraphQLError2 } from "graphql";
3352
-
3353
- // src/database/datalayer.ts
3354
- import { JSONPath } from "jsonpath-plus";
3355
- import sha from "js-sha1";
3356
-
3357
- // src/database/level.ts
3358
- var ARRAY_ITEM_VALUE_SEPARATOR = ",";
3359
- var INDEX_KEY_FIELD_SEPARATOR = "";
3360
- var CONTENT_ROOT_PREFIX = "~";
3361
- var SUBLEVEL_OPTIONS = {
3362
- separator: INDEX_KEY_FIELD_SEPARATOR,
3363
- valueEncoding: "json"
3364
- };
3365
- var LevelProxyHandler = {
3366
- get: function(target, property) {
3367
- if (!target[property]) {
3368
- throw new Error(`The property, ${property.toString()}, doesn't exist`);
3369
- }
3370
- if (typeof target[property] !== "function") {
3371
- throw new Error(`The property, ${property.toString()}, is not a function`);
3372
- }
3373
- if (property === "get") {
3374
- return async (...args) => {
3375
- let result;
3376
- try {
3377
- result = await target[property].apply(target, args);
3378
- } catch (e) {
3379
- if (e.code !== "LEVEL_NOT_FOUND") {
3380
- throw e;
3381
- }
3382
- }
3383
- return result;
3384
- };
3385
- } else if (property === "sublevel") {
3386
- return (...args) => {
3387
- return new Proxy(
3388
- target[property].apply(target, args),
3389
- LevelProxyHandler
3390
- );
3391
- };
3392
- } else {
3393
- return (...args) => target[property].apply(target, args);
3394
- }
3395
- }
3396
- };
3397
- var LevelProxy = class {
3398
- constructor(level) {
3399
- return new Proxy(level, LevelProxyHandler);
3400
- }
3401
- };
3402
-
3403
- // src/database/datalayer.ts
3404
- import path2 from "path";
3405
-
3406
- // src/database/util.ts
3407
- import toml from "@iarna/toml";
3408
- import yaml from "js-yaml";
3409
- import matter from "gray-matter";
3410
- import {
3411
- normalizePath
3412
- } from "@tinacms/schema-tools";
3413
- import micromatch from "micromatch";
3414
- import path from "path";
3415
-
3416
- // src/database/alias-utils.ts
3417
- var replaceBlockAliases = (template, item) => {
3418
- const output = { ...item };
3419
- const templateKey = template.templateKey || "_template";
3420
- const templateName = output[templateKey];
3421
- const matchingTemplate = template.templates.find(
3422
- (t) => t.nameOverride == templateName || t.name == templateName
3423
- );
3424
- if (!matchingTemplate) {
3425
- throw new Error(
3426
- `Block template "${templateName}" is not defined for field "${template.name}"`
3427
- );
3428
- }
3429
- output._template = matchingTemplate.name;
3430
- if (templateKey != "_template") {
3431
- delete output[templateKey];
3432
- }
3433
- return output;
3434
- };
3435
- var replaceNameOverrides = (template, obj) => {
3436
- if (template.list) {
3437
- return obj.map((item) => {
3438
- if (isBlockField(template)) {
3439
- item = replaceBlockAliases(template, item);
3440
- }
3441
- return _replaceNameOverrides(
3442
- getTemplateForData(template, item).fields,
3443
- item
3444
- );
3445
- });
3446
- } else {
3447
- return _replaceNameOverrides(getTemplateForData(template, obj).fields, obj);
3448
- }
3449
- };
3450
- function isBlockField(field) {
3451
- return field && field.type === "object" && field.templates?.length > 0;
3452
- }
3453
- var _replaceNameOverrides = (fields, obj) => {
3454
- const output = {};
3455
- Object.keys(obj).forEach((key) => {
3456
- const field = fields.find(
3457
- (fieldWithMatchingAlias) => (fieldWithMatchingAlias?.nameOverride || fieldWithMatchingAlias?.name) === key
3458
- );
3459
- output[field?.name || key] = field?.type == "object" ? replaceNameOverrides(field, obj[key]) : obj[key];
3460
- });
3461
- return output;
3462
- };
3463
- var getTemplateForData = (field, data) => {
3464
- if (field.templates?.length) {
3465
- const templateKey = "_template";
3466
- if (data[templateKey]) {
3467
- const result = field.templates.find(
3468
- (template) => template.nameOverride === data[templateKey] || template.name === data[templateKey]
3469
- );
3470
- if (result) {
3471
- return result;
3472
- }
3473
- throw new Error(
3474
- `Template "${data[templateKey]}" is not defined for field "${field.name}"`
3475
- );
3476
- }
3477
- throw new Error(
3478
- `Missing required key "${templateKey}" on field "${field.name}"`
3479
- );
3480
- } else {
3481
- return field;
3482
- }
3483
- };
3484
- var applyBlockAliases = (template, item) => {
3485
- const output = { ...item };
3486
- const templateKey = template.templateKey || "_template";
3487
- const templateName = output._template;
3488
- const matchingTemplate = template.templates.find(
3489
- (t) => t.nameOverride == templateName || t.name == templateName
3490
- );
3491
- if (!matchingTemplate) {
3492
- throw new Error(
3493
- `Block template "${templateName}" is not defined for field "${template.name}"`
3455
+ var applyBlockAliases = (template, item) => {
3456
+ const output = { ...item };
3457
+ const templateKey = template.templateKey || "_template";
3458
+ const templateName = output._template;
3459
+ const matchingTemplate = template.templates.find(
3460
+ (t) => t.nameOverride == templateName || t.name == templateName
3461
+ );
3462
+ if (!matchingTemplate) {
3463
+ throw new Error(
3464
+ `Block template "${templateName}" is not defined for field "${template.name}"`
3494
3465
  );
3495
3466
  }
3496
3467
  output[templateKey] = matchingTemplate.nameOverride || matchingTemplate.name;
@@ -3752,6 +3723,9 @@ var loadAndParseWithAliases = async (bridge, filepath, collection, templateInfo)
3752
3723
 
3753
3724
  // src/database/datalayer.ts
3754
3725
  var DEFAULT_COLLECTION_SORT_KEY = "__filepath__";
3726
+ var REFS_COLLECTIONS_SORT_KEY = "__refs__";
3727
+ var REFS_REFERENCE_FIELD = "__tina_ref__";
3728
+ var REFS_PATH_FIELD = "__tina_ref_path__";
3755
3729
  var DEFAULT_NUMERIC_LPAD = 4;
3756
3730
  var applyPadding = (input, pad) => {
3757
3731
  if (pad) {
@@ -4261,6 +4235,7 @@ var makeFolderOpsForCollection = (folderTree, collection, indexDefinitions, opTy
4261
4235
  result.push({
4262
4236
  type: opType,
4263
4237
  key: `${collection.path}/${subFolderKey}.${collection.format}`,
4238
+ // replace the root with the collection path
4264
4239
  sublevel: indexSublevel,
4265
4240
  value: {}
4266
4241
  });
@@ -4294,55 +4269,343 @@ var makeFolderOpsForCollection = (folderTree, collection, indexDefinitions, opTy
4294
4269
  });
4295
4270
  }
4296
4271
  }
4297
- 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
+ }
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
4536
+ }
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
+ });
4565
+ }
4566
+ return value;
4567
+ }
4568
+ return value;
4569
+ } else {
4570
+ return value;
4571
+ }
4298
4572
  };
4299
- var makeIndexOpsForDocument = (filepath, collection, indexDefinitions, data, opType, level, escapeStr = stringEscaper) => {
4300
- const result = [];
4301
- if (collection) {
4302
- const collectionSublevel = level.sublevel(collection, SUBLEVEL_OPTIONS);
4303
- for (const [sort, definition] of Object.entries(indexDefinitions)) {
4304
- const indexedValue = makeKeyForField(definition, data, escapeStr);
4305
- const indexSublevel = collectionSublevel.sublevel(sort, SUBLEVEL_OPTIONS);
4306
- if (sort === DEFAULT_COLLECTION_SORT_KEY) {
4307
- result.push({
4308
- type: opType,
4309
- key: filepath,
4310
- sublevel: indexSublevel,
4311
- value: opType === "put" ? {} : void 0
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}`;
4312
4589
  });
4313
- } else {
4314
- if (indexedValue) {
4315
- result.push({
4316
- type: opType,
4317
- key: `${indexedValue}${INDEX_KEY_FIELD_SEPARATOR}${filepath}`,
4318
- sublevel: indexSublevel,
4319
- value: opType === "put" ? {} : void 0
4320
- });
4321
- }
4322
4590
  }
4323
4591
  }
4592
+ return value;
4593
+ } else {
4594
+ return value;
4324
4595
  }
4325
- return result;
4326
4596
  };
4327
- var makeStringEscaper = (regex, replacement) => {
4328
- return (input) => {
4329
- if (Array.isArray(input)) {
4330
- return input.map(
4331
- (val) => val.replace(regex, replacement)
4332
- );
4333
- } else {
4334
- if (typeof input === "string") {
4335
- return input.replace(regex, replacement);
4336
- } else {
4337
- return input;
4338
- }
4339
- }
4340
- };
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;
4341
4608
  };
4342
- var stringEscaper = makeStringEscaper(
4343
- new RegExp(INDEX_KEY_FIELD_SEPARATOR, "gm"),
4344
- encodeURIComponent(INDEX_KEY_FIELD_SEPARATOR)
4345
- );
4346
4609
 
4347
4610
  // src/resolver/index.ts
4348
4611
  var createResolver = (args) => {
@@ -4375,6 +4638,7 @@ var resolveFieldData = async ({ namespace, ...field }, rawData, accumulator, tin
4375
4638
  case "password":
4376
4639
  accumulator[field.name] = {
4377
4640
  value: void 0,
4641
+ // never resolve the password hash
4378
4642
  passwordChangeRequired: value["passwordChangeRequired"] ?? false
4379
4643
  };
4380
4644
  break;
@@ -4469,7 +4733,7 @@ var resolveFieldData = async ({ namespace, ...field }, rawData, accumulator, tin
4469
4733
  }
4470
4734
  return accumulator;
4471
4735
  };
4472
- var transformDocumentIntoPayload = async (fullPath, rawData, tinaSchema, config, isAudit) => {
4736
+ var transformDocumentIntoPayload = async (fullPath, rawData, tinaSchema, config, isAudit, hasReferences) => {
4473
4737
  const collection = tinaSchema.getCollection(rawData._collection);
4474
4738
  try {
4475
4739
  const template = tinaSchema.getTemplateForData({
@@ -4523,6 +4787,7 @@ var transformDocumentIntoPayload = async (fullPath, rawData, tinaSchema, config,
4523
4787
  basename,
4524
4788
  filename,
4525
4789
  extension,
4790
+ hasReferences,
4526
4791
  path: fullPath,
4527
4792
  relativePath,
4528
4793
  breadcrumbs,
@@ -4542,6 +4807,34 @@ var transformDocumentIntoPayload = async (fullPath, rawData, tinaSchema, config,
4542
4807
  throw e;
4543
4808
  }
4544
4809
  };
4810
+ var updateObjectWithJsonPath = (obj, path7, oldValue, newValue) => {
4811
+ let updated = false;
4812
+ if (!path7.includes(".") && !path7.includes("[")) {
4813
+ if (path7 in obj && obj[path7] === oldValue) {
4814
+ obj[path7] = newValue;
4815
+ updated = true;
4816
+ }
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
+ });
4826
+ if (parents.length > 0) {
4827
+ parents.forEach((parent) => {
4828
+ if (parent && typeof parent === "object" && keyToUpdate in parent) {
4829
+ if (parent[keyToUpdate] === oldValue) {
4830
+ parent[keyToUpdate] = newValue;
4831
+ updated = true;
4832
+ }
4833
+ }
4834
+ });
4835
+ }
4836
+ return { object: obj, updated };
4837
+ };
4545
4838
  var Resolver = class {
4546
4839
  constructor(init) {
4547
4840
  this.init = init;
@@ -4549,6 +4842,7 @@ var Resolver = class {
4549
4842
  const collection = this.tinaSchema.getCollection(collectionName);
4550
4843
  const extraFields = {};
4551
4844
  return {
4845
+ // return the collection and hasDocuments to resolve documents at a lower level
4552
4846
  documents: { collection, hasDocuments },
4553
4847
  ...collection,
4554
4848
  ...extraFields
@@ -4556,7 +4850,9 @@ var Resolver = class {
4556
4850
  };
4557
4851
  this.getRaw = async (fullPath) => {
4558
4852
  if (typeof fullPath !== "string") {
4559
- 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
+ );
4560
4856
  }
4561
4857
  return this.database.get(fullPath);
4562
4858
  };
@@ -4583,22 +4879,28 @@ var Resolver = class {
4583
4879
  );
4584
4880
  }
4585
4881
  };
4586
- this.getDocument = async (fullPath) => {
4882
+ this.getDocument = async (fullPath, opts = {}) => {
4587
4883
  if (typeof fullPath !== "string") {
4588
- 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
+ );
4589
4887
  }
4590
4888
  const rawData = await this.getRaw(fullPath);
4889
+ const hasReferences = opts?.checkReferences ? await this.hasReferences(fullPath, opts.collection) : void 0;
4591
4890
  return transformDocumentIntoPayload(
4592
4891
  fullPath,
4593
4892
  rawData,
4594
4893
  this.tinaSchema,
4595
4894
  this.config,
4596
- this.isAudit
4895
+ this.isAudit,
4896
+ hasReferences
4597
4897
  );
4598
4898
  };
4599
4899
  this.deleteDocument = async (fullPath) => {
4600
4900
  if (typeof fullPath !== "string") {
4601
- 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
+ );
4602
4904
  }
4603
4905
  await this.database.delete(fullPath);
4604
4906
  };
@@ -4633,7 +4935,9 @@ var Resolver = class {
4633
4935
  );
4634
4936
  } else {
4635
4937
  return this.buildFieldMutations(
4938
+ // @ts-ignore FIXME Argument of type 'string | object' is not assignable to parameter of type '{ [fieldName: string]: string | object | (string | object)[]; }'
4636
4939
  fieldValue,
4940
+ //@ts-ignore
4637
4941
  objectTemplate,
4638
4942
  existingData
4639
4943
  );
@@ -4645,6 +4949,7 @@ var Resolver = class {
4645
4949
  fieldValue.map(async (item) => {
4646
4950
  if (typeof item === "string") {
4647
4951
  throw new Error(
4952
+ //@ts-ignore
4648
4953
  `Expected object for template value for field ${field.name}`
4649
4954
  );
4650
4955
  }
@@ -4653,16 +4958,19 @@ var Resolver = class {
4653
4958
  });
4654
4959
  const [templateName] = Object.entries(item)[0];
4655
4960
  const template = templates.find(
4961
+ //@ts-ignore
4656
4962
  (template2) => template2.name === templateName
4657
4963
  );
4658
4964
  if (!template) {
4659
4965
  throw new Error(`Expected to find template ${templateName}`);
4660
4966
  }
4661
4967
  return {
4968
+ // @ts-ignore FIXME Argument of type 'unknown' is not assignable to parameter of type '{ [fieldName: string]: string | { [key: string]: unknown; } | (string | { [key: string]: unknown; })[]; }'
4662
4969
  ...await this.buildFieldMutations(
4663
4970
  item[template.name],
4664
4971
  template
4665
4972
  ),
4973
+ //@ts-ignore
4666
4974
  _template: template.name
4667
4975
  };
4668
4976
  })
@@ -4670,6 +4978,7 @@ var Resolver = class {
4670
4978
  } else {
4671
4979
  if (typeof fieldValue === "string") {
4672
4980
  throw new Error(
4981
+ //@ts-ignore
4673
4982
  `Expected object for template value for field ${field.name}`
4674
4983
  );
4675
4984
  }
@@ -4678,16 +4987,19 @@ var Resolver = class {
4678
4987
  });
4679
4988
  const [templateName] = Object.entries(fieldValue)[0];
4680
4989
  const template = templates.find(
4990
+ //@ts-ignore
4681
4991
  (template2) => template2.name === templateName
4682
4992
  );
4683
4993
  if (!template) {
4684
4994
  throw new Error(`Expected to find template ${templateName}`);
4685
4995
  }
4686
4996
  return {
4997
+ // @ts-ignore FIXME Argument of type 'unknown' is not assignable to parameter of type '{ [fieldName: string]: string | { [key: string]: unknown; } | (string | { [key: string]: unknown; })[]; }'
4687
4998
  ...await this.buildFieldMutations(
4688
4999
  fieldValue[template.name],
4689
5000
  template
4690
5001
  ),
5002
+ //@ts-ignore
4691
5003
  _template: template.name
4692
5004
  };
4693
5005
  }
@@ -4727,6 +5039,7 @@ var Resolver = class {
4727
5039
  return this.getDocument(realPath);
4728
5040
  }
4729
5041
  const params = await this.buildObjectMutations(
5042
+ // @ts-ignore
4730
5043
  args.params[collection.name],
4731
5044
  collection
4732
5045
  );
@@ -4772,6 +5085,7 @@ var Resolver = class {
4772
5085
  const values = {
4773
5086
  ...oldDoc,
4774
5087
  ...await this.buildFieldMutations(
5088
+ // @ts-ignore FIXME: failing on unknown, which we don't need to know because it's recursive
4775
5089
  templateParams,
4776
5090
  template,
4777
5091
  doc?._rawData
@@ -4785,13 +5099,22 @@ var Resolver = class {
4785
5099
  return this.getDocument(realPath);
4786
5100
  }
4787
5101
  const params = await this.buildObjectMutations(
5102
+ //@ts-ignore
4788
5103
  isCollectionSpecific ? args.params : args.params[collection.name],
4789
5104
  collection,
4790
5105
  doc?._rawData
4791
5106
  );
4792
- await this.database.put(realPath, { ...oldDoc, ...params }, collection.name);
5107
+ await this.database.put(
5108
+ realPath,
5109
+ { ...oldDoc, ...params },
5110
+ collection.name
5111
+ );
4793
5112
  return this.getDocument(realPath);
4794
5113
  };
5114
+ /**
5115
+ * Returns top-level fields which are not defined in the collection, so their
5116
+ * values are not eliminated from Tina when new values are saved
5117
+ */
4795
5118
  this.resolveLegacyValues = (oldDoc, collection) => {
4796
5119
  const legacyValues = {};
4797
5120
  Object.entries(oldDoc).forEach(([key, value]) => {
@@ -4896,6 +5219,40 @@ var Resolver = class {
4896
5219
  if (isDeletion) {
4897
5220
  const doc = await this.getDocument(realPath);
4898
5221
  await this.deleteDocument(realPath);
5222
+ if (await this.hasReferences(realPath, collection)) {
5223
+ const collRefs = await this.findReferences(realPath, collection);
5224
+ for (const [collection2, docsWithRefs] of Object.entries(collRefs)) {
5225
+ for (const [pathToDocWithRef, referencePaths] of Object.entries(
5226
+ docsWithRefs
5227
+ )) {
5228
+ let refDoc = await this.getRaw(pathToDocWithRef);
5229
+ let hasUpdate = false;
5230
+ for (const path7 of referencePaths) {
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
+ );
5252
+ }
5253
+ }
5254
+ }
5255
+ }
4899
5256
  return doc;
4900
5257
  }
4901
5258
  if (isUpdateName) {
@@ -4912,12 +5269,49 @@ var Resolver = class {
4912
5269
  collection?.path,
4913
5270
  args.params.relativePath
4914
5271
  );
5272
+ if (newRealPath === realPath) {
5273
+ return doc;
5274
+ }
4915
5275
  await this.database.put(newRealPath, doc._rawData, collection.name);
4916
5276
  await this.deleteDocument(realPath);
5277
+ const collRefs = await this.findReferences(realPath, collection);
5278
+ for (const [collection2, docsWithRefs] of Object.entries(collRefs)) {
5279
+ for (const [pathToDocWithRef, referencePaths] of Object.entries(
5280
+ docsWithRefs
5281
+ )) {
5282
+ let docWithRef = await this.getRaw(pathToDocWithRef);
5283
+ let hasUpdate = false;
5284
+ for (const path7 of referencePaths) {
5285
+ const { object: object2, updated } = updateObjectWithJsonPath(
5286
+ docWithRef,
5287
+ path7,
5288
+ realPath,
5289
+ newRealPath
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
+ );
5306
+ }
5307
+ }
5308
+ }
4917
5309
  return this.getDocument(newRealPath);
4918
5310
  }
4919
5311
  if (alreadyExists === false) {
4920
- 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
+ );
4921
5315
  }
4922
5316
  return this.updateResolveDocument({
4923
5317
  collection,
@@ -4927,7 +5321,10 @@ var Resolver = class {
4927
5321
  isCollectionSpecific
4928
5322
  });
4929
5323
  } else {
4930
- return this.getDocument(realPath);
5324
+ return this.getDocument(realPath, {
5325
+ collection,
5326
+ checkReferences: true
5327
+ });
4931
5328
  }
4932
5329
  };
4933
5330
  this.resolveCollectionConnections = async ({ ids }) => {
@@ -4964,6 +5361,7 @@ var Resolver = class {
4964
5361
  },
4965
5362
  collection: referencedCollection,
4966
5363
  hydrator: (path7) => path7
5364
+ // just return the path
4967
5365
  }
4968
5366
  );
4969
5367
  const { edges } = resolvedCollectionConnection;
@@ -5031,6 +5429,82 @@ var Resolver = class {
5031
5429
  }
5032
5430
  };
5033
5431
  };
5432
+ /**
5433
+ * Checks if a document has references to it
5434
+ * @param id The id of the document to check for references
5435
+ * @param c The collection to check for references
5436
+ * @returns true if the document has references, false otherwise
5437
+ */
5438
+ this.hasReferences = async (id, c) => {
5439
+ let count = 0;
5440
+ await this.database.query(
5441
+ {
5442
+ collection: c.name,
5443
+ filterChain: makeFilterChain({
5444
+ conditions: [
5445
+ {
5446
+ filterPath: REFS_REFERENCE_FIELD,
5447
+ filterExpression: {
5448
+ _type: "string",
5449
+ _list: false,
5450
+ eq: id
5451
+ }
5452
+ }
5453
+ ]
5454
+ }),
5455
+ sort: REFS_COLLECTIONS_SORT_KEY
5456
+ },
5457
+ (refId) => {
5458
+ count++;
5459
+ return refId;
5460
+ }
5461
+ );
5462
+ if (count) {
5463
+ return true;
5464
+ }
5465
+ return false;
5466
+ };
5467
+ /**
5468
+ * Finds references to a document
5469
+ * @param id the id of the document to find references to
5470
+ * @param c the collection to find references in
5471
+ * @returns a map of references to the document
5472
+ */
5473
+ this.findReferences = async (id, c) => {
5474
+ const references = {};
5475
+ await this.database.query(
5476
+ {
5477
+ collection: c.name,
5478
+ filterChain: makeFilterChain({
5479
+ conditions: [
5480
+ {
5481
+ filterPath: REFS_REFERENCE_FIELD,
5482
+ filterExpression: {
5483
+ _type: "string",
5484
+ _list: false,
5485
+ eq: id
5486
+ }
5487
+ }
5488
+ ]
5489
+ }),
5490
+ sort: REFS_COLLECTIONS_SORT_KEY
5491
+ },
5492
+ (refId, rawItem) => {
5493
+ if (!references[c.name]) {
5494
+ references[c.name] = {};
5495
+ }
5496
+ if (!references[c.name][refId]) {
5497
+ references[c.name][refId] = [];
5498
+ }
5499
+ const referencePath = rawItem?.[REFS_PATH_FIELD];
5500
+ if (referencePath) {
5501
+ references[c.name][refId].push(referencePath);
5502
+ }
5503
+ return refId;
5504
+ }
5505
+ );
5506
+ return references;
5507
+ };
5034
5508
  this.buildFieldMutations = async (fieldParams, template, existingData) => {
5035
5509
  const accum = {};
5036
5510
  for (const passwordField of template.fields.filter(
@@ -5098,7 +5572,7 @@ var Resolver = class {
5098
5572
  }
5099
5573
  break;
5100
5574
  case "rich-text":
5101
- accum[fieldName] = stringifyMDX(
5575
+ accum[fieldName] = serializeMDX(
5102
5576
  fieldValue,
5103
5577
  field,
5104
5578
  (fieldValue2) => resolveMediaCloudToRelative(
@@ -5117,6 +5591,27 @@ var Resolver = class {
5117
5591
  }
5118
5592
  return accum;
5119
5593
  };
5594
+ /**
5595
+ * A mutation looks nearly identical between updateDocument:
5596
+ * ```graphql
5597
+ * updateDocument(collection: $collection,relativePath: $path, params: {
5598
+ * post: {
5599
+ * title: "Hello, World"
5600
+ * }
5601
+ * })`
5602
+ * ```
5603
+ * and `updatePostDocument`:
5604
+ * ```graphql
5605
+ * updatePostDocument(relativePath: $path, params: {
5606
+ * title: "Hello, World"
5607
+ * })
5608
+ * ```
5609
+ * The problem here is that we don't know whether the payload came from `updateDocument`
5610
+ * or `updatePostDocument` (we could, but for now it's easier not to pipe those details through),
5611
+ * But we do know that when given a `args.collection` value, we can assume that
5612
+ * this was a `updateDocument` request, and thus - should grab the data
5613
+ * from the corresponding field name in the key
5614
+ */
5120
5615
  this.buildParams = (args) => {
5121
5616
  try {
5122
5617
  assertShape(
@@ -5216,7 +5711,10 @@ var resolve = async ({
5216
5711
  const graphQLSchema = buildASTSchema(graphQLSchemaAst);
5217
5712
  const tinaConfig = await database.getTinaSchema();
5218
5713
  const tinaSchema = await createSchema({
5714
+ // TODO: please update all the types to import from @tinacms/schema-tools
5715
+ // @ts-ignore
5219
5716
  schema: tinaConfig,
5717
+ // @ts-ignore
5220
5718
  flags: tinaConfig?.meta?.flags
5221
5719
  });
5222
5720
  const resolver = createResolver({
@@ -5233,8 +5731,7 @@ var resolve = async ({
5233
5731
  database
5234
5732
  },
5235
5733
  typeResolver: async (source, _args, info) => {
5236
- if (source.__typename)
5237
- return source.__typename;
5734
+ if (source.__typename) return source.__typename;
5238
5735
  const namedType = getNamedType(info.returnType).toString();
5239
5736
  const lookup = await database.getLookup(namedType);
5240
5737
  if (lookup.resolveType === "unionData") {
@@ -5383,11 +5880,13 @@ var resolve = async ({
5383
5880
  set(
5384
5881
  params,
5385
5882
  userField.path.slice(1),
5883
+ // remove _rawData from users path
5386
5884
  users.map((u) => {
5387
5885
  if (user[idFieldName] === u[idFieldName]) {
5388
5886
  return user;
5389
5887
  }
5390
5888
  return {
5889
+ // don't overwrite other users' passwords
5391
5890
  ...u,
5392
5891
  [passwordFieldName]: {
5393
5892
  ...u[passwordFieldName],
@@ -5410,6 +5909,9 @@ var resolve = async ({
5410
5909
  }
5411
5910
  const isCreation = lookup[info.fieldName] === "create";
5412
5911
  switch (lookup.resolveType) {
5912
+ /**
5913
+ * `node(id: $id)`
5914
+ */
5413
5915
  case "nodeDocument":
5414
5916
  assertShape(
5415
5917
  args,
@@ -5441,6 +5943,7 @@ var resolve = async ({
5441
5943
  collection: args.collection,
5442
5944
  isMutation,
5443
5945
  isCreation,
5946
+ // Right now this is the only case for deletion
5444
5947
  isDeletion: info.fieldName === "deleteDocument",
5445
5948
  isFolderCreation: info.fieldName === "createFolder",
5446
5949
  isUpdateName: Boolean(args?.params?.relativePath),
@@ -5450,6 +5953,9 @@ var resolve = async ({
5450
5953
  return result;
5451
5954
  }
5452
5955
  return value;
5956
+ /**
5957
+ * eg `getMovieDocument.data.actors`
5958
+ */
5453
5959
  case "multiCollectionDocumentList":
5454
5960
  if (Array.isArray(value)) {
5455
5961
  return {
@@ -5461,7 +5967,15 @@ var resolve = async ({
5461
5967
  }
5462
5968
  if (info.fieldName === "documents" && value?.collection && value?.hasDocuments) {
5463
5969
  let filter = args.filter;
5464
- if (typeof args?.filter !== "undefined" && args?.filter !== null && typeof value?.collection?.name === "string" && Object.keys(args.filter).includes(value?.collection?.name) && typeof args.filter[value?.collection?.name] !== "undefined") {
5970
+ if (
5971
+ // 1. Make sure that the filter exists
5972
+ typeof args?.filter !== "undefined" && args?.filter !== null && // 2. Make sure that the collection name exists
5973
+ // @ts-ignore
5974
+ typeof value?.collection?.name === "string" && // 3. Make sure that the collection name is in the filter and is not undefined
5975
+ // @ts-ignore
5976
+ Object.keys(args.filter).includes(value?.collection?.name) && // @ts-ignore
5977
+ typeof args.filter[value?.collection?.name] !== "undefined"
5978
+ ) {
5465
5979
  filter = args.filter[value.collection.name];
5466
5980
  }
5467
5981
  return resolver.resolveCollectionConnection({
@@ -5469,12 +5983,20 @@ var resolve = async ({
5469
5983
  ...args,
5470
5984
  filter
5471
5985
  },
5986
+ // @ts-ignore
5472
5987
  collection: value.collection
5473
5988
  });
5474
5989
  }
5475
5990
  throw new Error(
5476
5991
  `Expected an array for result of ${info.fieldName} at ${info.path}`
5477
5992
  );
5993
+ /**
5994
+ * Collections-specific getter
5995
+ * eg. `getPostDocument`/`createPostDocument`/`updatePostDocument`
5996
+ *
5997
+ * if coming from a query result
5998
+ * the field will be `node`
5999
+ */
5478
6000
  case "collectionDocument": {
5479
6001
  if (value) {
5480
6002
  return value;
@@ -5489,11 +6011,32 @@ var resolve = async ({
5489
6011
  });
5490
6012
  return result;
5491
6013
  }
6014
+ /**
6015
+ * Collections-specific list getter
6016
+ * eg. `getPageList`
6017
+ */
5492
6018
  case "collectionDocumentList":
5493
6019
  return resolver.resolveCollectionConnection({
5494
6020
  args,
5495
6021
  collection: tinaSchema.getCollection(lookup.collection)
5496
6022
  });
6023
+ /**
6024
+ * A polymorphic data set, it can be from a document's data
6025
+ * of any nested object which can be one of many shapes
6026
+ *
6027
+ * ```graphql
6028
+ * getPostDocument(relativePath: $relativePath) {
6029
+ * data {...} <- this part
6030
+ * }
6031
+ * ```
6032
+ * ```graphql
6033
+ * getBlockDocument(relativePath: $relativePath) {
6034
+ * data {
6035
+ * blocks {...} <- or this part
6036
+ * }
6037
+ * }
6038
+ * ```
6039
+ */
5497
6040
  case "unionData":
5498
6041
  if (!value) {
5499
6042
  if (args.relativePath) {
@@ -5558,8 +6101,7 @@ var TinaLevelClient = class extends ManyLevelGuest {
5558
6101
  this.port = port || 9e3;
5559
6102
  }
5560
6103
  openConnection() {
5561
- if (this._connected)
5562
- return;
6104
+ if (this._connected) return;
5563
6105
  const socket = connect(this.port);
5564
6106
  pipeline(socket, this.createRpcStream(), socket, () => {
5565
6107
  this._connected = false;
@@ -5569,7 +6111,7 @@ var TinaLevelClient = class extends ManyLevelGuest {
5569
6111
  };
5570
6112
 
5571
6113
  // src/database/index.ts
5572
- import path4 from "path";
6114
+ import path4 from "node:path";
5573
6115
  import { GraphQLError as GraphQLError5 } from "graphql";
5574
6116
  import micromatch2 from "micromatch";
5575
6117
  import sha2 from "js-sha1";
@@ -5704,6 +6246,7 @@ var Database = class {
5704
6246
  );
5705
6247
  const indexDefinitions = await this.getIndexDefinitions(this.contentLevel);
5706
6248
  const collectionIndexDefinitions = indexDefinitions?.[collection.name];
6249
+ const collectionReferences = (await this.getCollectionReferences())?.[collection.name];
5707
6250
  const normalizedPath = normalizePath(filepath);
5708
6251
  if (!collection?.isDetached) {
5709
6252
  if (this.bridge) {
@@ -5732,6 +6275,14 @@ var Database = class {
5732
6275
  let delOps = [];
5733
6276
  if (!isGitKeep(normalizedPath, collection)) {
5734
6277
  putOps = [
6278
+ ...makeRefOpsForDocument(
6279
+ normalizedPath,
6280
+ collection?.name,
6281
+ collectionReferences,
6282
+ dataFields,
6283
+ "put",
6284
+ level
6285
+ ),
5735
6286
  ...makeIndexOpsForDocument(
5736
6287
  normalizedPath,
5737
6288
  collection?.name,
@@ -5740,6 +6291,7 @@ var Database = class {
5740
6291
  "put",
5741
6292
  level
5742
6293
  ),
6294
+ // folder indices
5743
6295
  ...makeIndexOpsForDocument(
5744
6296
  normalizedPath,
5745
6297
  `${collection?.name}_${folderKey}`,
@@ -5754,6 +6306,14 @@ var Database = class {
5754
6306
  SUBLEVEL_OPTIONS
5755
6307
  ).get(normalizedPath);
5756
6308
  delOps = existingItem ? [
6309
+ ...makeRefOpsForDocument(
6310
+ normalizedPath,
6311
+ collection?.name,
6312
+ collectionReferences,
6313
+ existingItem,
6314
+ "del",
6315
+ level
6316
+ ),
5757
6317
  ...makeIndexOpsForDocument(
5758
6318
  normalizedPath,
5759
6319
  collection?.name,
@@ -5762,6 +6322,7 @@ var Database = class {
5762
6322
  "del",
5763
6323
  level
5764
6324
  ),
6325
+ // folder indices
5765
6326
  ...makeIndexOpsForDocument(
5766
6327
  normalizedPath,
5767
6328
  `${collection?.name}_${folderKey}`,
@@ -5800,6 +6361,7 @@ var Database = class {
5800
6361
  );
5801
6362
  collectionIndexDefinitions = indexDefinitions?.[collectionName];
5802
6363
  }
6364
+ const collectionReferences = (await this.getCollectionReferences())?.[collectionName];
5803
6365
  const normalizedPath = normalizePath(filepath);
5804
6366
  const dataFields = await this.formatBodyOnPayload(filepath, data);
5805
6367
  const collection = await this.collectionForPath(filepath);
@@ -5847,6 +6409,14 @@ var Database = class {
5847
6409
  let delOps = [];
5848
6410
  if (!isGitKeep(normalizedPath, collection)) {
5849
6411
  putOps = [
6412
+ ...makeRefOpsForDocument(
6413
+ normalizedPath,
6414
+ collectionName,
6415
+ collectionReferences,
6416
+ dataFields,
6417
+ "put",
6418
+ level
6419
+ ),
5850
6420
  ...makeIndexOpsForDocument(
5851
6421
  normalizedPath,
5852
6422
  collectionName,
@@ -5855,6 +6425,7 @@ var Database = class {
5855
6425
  "put",
5856
6426
  level
5857
6427
  ),
6428
+ // folder indices
5858
6429
  ...makeIndexOpsForDocument(
5859
6430
  normalizedPath,
5860
6431
  `${collection?.name}_${folderKey}`,
@@ -5869,6 +6440,14 @@ var Database = class {
5869
6440
  SUBLEVEL_OPTIONS
5870
6441
  ).get(normalizedPath);
5871
6442
  delOps = existingItem ? [
6443
+ ...makeRefOpsForDocument(
6444
+ normalizedPath,
6445
+ collectionName,
6446
+ collectionReferences,
6447
+ existingItem,
6448
+ "del",
6449
+ level
6450
+ ),
5872
6451
  ...makeIndexOpsForDocument(
5873
6452
  normalizedPath,
5874
6453
  collectionName,
@@ -5877,6 +6456,7 @@ var Database = class {
5877
6456
  "del",
5878
6457
  level
5879
6458
  ),
6459
+ // folder indices
5880
6460
  ...makeIndexOpsForDocument(
5881
6461
  normalizedPath,
5882
6462
  `${collection?.name}_${folderKey}`,
@@ -5954,6 +6534,7 @@ var Database = class {
5954
6534
  aliasedData,
5955
6535
  extension,
5956
6536
  writeTemplateKey,
6537
+ //templateInfo.type === 'union',
5957
6538
  {
5958
6539
  frontmatterFormat: collection?.frontmatterFormat,
5959
6540
  frontmatterDelimiters: collection?.frontmatterDelimiters
@@ -5992,6 +6573,7 @@ var Database = class {
5992
6573
  SUBLEVEL_OPTIONS
5993
6574
  ).get(graphqlPath);
5994
6575
  };
6576
+ //TODO - is there a reason why the database fetches some config with "bridge.get", and some with "store.get"?
5995
6577
  this.getGraphQLSchemaFromBridge = async () => {
5996
6578
  if (!this.bridge) {
5997
6579
  throw new Error(`No bridge configured`);
@@ -6028,6 +6610,22 @@ var Database = class {
6028
6610
  this.tinaSchema = await createSchema({ schema });
6029
6611
  return this.tinaSchema;
6030
6612
  };
6613
+ this.getCollectionReferences = async (level) => {
6614
+ if (this.collectionReferences) {
6615
+ return this.collectionReferences;
6616
+ }
6617
+ const result = {};
6618
+ const schema = await this.getSchema(level || this.contentLevel);
6619
+ const collections = schema.getCollections();
6620
+ for (const collection of collections) {
6621
+ const collectionReferences = this.tinaSchema.findReferencesFromCollection(
6622
+ collection.name
6623
+ );
6624
+ result[collection.name] = collectionReferences;
6625
+ }
6626
+ this.collectionReferences = result;
6627
+ return result;
6628
+ };
6031
6629
  this.getIndexDefinitions = async (level) => {
6032
6630
  if (!this.collectionIndexDefinitions) {
6033
6631
  await new Promise(async (resolve2, reject) => {
@@ -6037,10 +6635,53 @@ var Database = class {
6037
6635
  const collections = schema.getCollections();
6038
6636
  for (const collection of collections) {
6039
6637
  const indexDefinitions = {
6040
- [DEFAULT_COLLECTION_SORT_KEY]: { fields: [] }
6638
+ [DEFAULT_COLLECTION_SORT_KEY]: { fields: [] },
6639
+ // provide a default sort key which is the file sort
6640
+ // pseudo-index for the collection's references
6641
+ [REFS_COLLECTIONS_SORT_KEY]: {
6642
+ fields: [
6643
+ {
6644
+ name: REFS_REFERENCE_FIELD,
6645
+ type: "string",
6646
+ list: false
6647
+ },
6648
+ {
6649
+ name: REFS_PATH_FIELD,
6650
+ type: "string",
6651
+ list: false
6652
+ }
6653
+ ]
6654
+ }
6041
6655
  };
6042
- if (collection.fields) {
6043
- 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) {
6044
6685
  if (field.indexed !== void 0 && field.indexed === false || field.type === "object") {
6045
6686
  continue;
6046
6687
  }
@@ -6188,29 +6829,36 @@ var Database = class {
6188
6829
  }
6189
6830
  startKey = startKey || key || "";
6190
6831
  endKey = key || "";
6191
- edges = [...edges, { cursor: key, path: filepath }];
6832
+ edges = [...edges, { cursor: key, path: filepath, value: itemRecord }];
6192
6833
  }
6193
6834
  return {
6194
- edges: await sequential(edges, async (edge) => {
6195
- try {
6196
- const node = await hydrator(edge.path);
6197
- return {
6198
- node,
6199
- cursor: btoa(edge.cursor)
6200
- };
6201
- } catch (error) {
6202
- console.log(error);
6203
- if (error instanceof Error && (!edge.path.includes(".tina/__generated__/_graphql.json") || !edge.path.includes("tina/__generated__/_graphql.json"))) {
6204
- throw new TinaQueryError({
6205
- originalError: error,
6206
- file: edge.path,
6207
- collection: collection.name,
6208
- stack: error.stack
6209
- });
6835
+ edges: await sequential(
6836
+ edges,
6837
+ async ({
6838
+ cursor,
6839
+ path: path7,
6840
+ value
6841
+ }) => {
6842
+ try {
6843
+ const node = await hydrator(path7, value);
6844
+ return {
6845
+ node,
6846
+ cursor: btoa(cursor)
6847
+ };
6848
+ } catch (error) {
6849
+ console.log(error);
6850
+ if (error instanceof Error && (!path7.includes(".tina/__generated__/_graphql.json") || !path7.includes("tina/__generated__/_graphql.json"))) {
6851
+ throw new TinaQueryError({
6852
+ originalError: error,
6853
+ file: path7,
6854
+ collection: collection.name,
6855
+ stack: error.stack
6856
+ });
6857
+ }
6858
+ throw error;
6210
6859
  }
6211
- throw error;
6212
6860
  }
6213
- }),
6861
+ ),
6214
6862
  pageInfo: {
6215
6863
  hasPreviousPage,
6216
6864
  hasNextPage,
@@ -6334,13 +6982,14 @@ var Database = class {
6334
6982
  documentPaths,
6335
6983
  async (collection, documentPaths2) => {
6336
6984
  if (collection && !collection.isDetached) {
6337
- await _indexContent(
6338
- this,
6339
- this.contentLevel,
6340
- documentPaths2,
6985
+ await _indexContent({
6986
+ database: this,
6987
+ level: this.contentLevel,
6988
+ documentPaths: documentPaths2,
6341
6989
  enqueueOps,
6342
- collection
6343
- );
6990
+ collection,
6991
+ isPartialReindex: true
6992
+ });
6344
6993
  }
6345
6994
  }
6346
6995
  );
@@ -6356,17 +7005,18 @@ var Database = class {
6356
7005
  throw new Error(`No collection found for path: ${filepath}`);
6357
7006
  }
6358
7007
  const indexDefinitions = await this.getIndexDefinitions(this.contentLevel);
7008
+ const collectionReferences = (await this.getCollectionReferences())?.[collection.name];
6359
7009
  const collectionIndexDefinitions = indexDefinitions?.[collection.name];
6360
7010
  let level = this.contentLevel;
6361
7011
  if (collection?.isDetached) {
6362
7012
  level = this.appLevel.sublevel(collection?.name, SUBLEVEL_OPTIONS);
6363
7013
  }
6364
- const itemKey = normalizePath(filepath);
7014
+ const normalizedPath = normalizePath(filepath);
6365
7015
  const rootSublevel = level.sublevel(
6366
7016
  CONTENT_ROOT_PREFIX,
6367
7017
  SUBLEVEL_OPTIONS
6368
7018
  );
6369
- const item = await rootSublevel.get(itemKey);
7019
+ const item = await rootSublevel.get(normalizedPath);
6370
7020
  if (item) {
6371
7021
  const folderTreeBuilder = new FolderTreeBuilder();
6372
7022
  const folderKey = folderTreeBuilder.update(
@@ -6374,16 +7024,25 @@ var Database = class {
6374
7024
  collection.path || ""
6375
7025
  );
6376
7026
  await this.contentLevel.batch([
7027
+ ...makeRefOpsForDocument(
7028
+ normalizedPath,
7029
+ collection.name,
7030
+ collectionReferences,
7031
+ item,
7032
+ "del",
7033
+ level
7034
+ ),
6377
7035
  ...makeIndexOpsForDocument(
6378
- filepath,
7036
+ normalizedPath,
6379
7037
  collection.name,
6380
7038
  collectionIndexDefinitions,
6381
7039
  item,
6382
7040
  "del",
6383
7041
  level
6384
7042
  ),
7043
+ // folder indices
6385
7044
  ...makeIndexOpsForDocument(
6386
- filepath,
7045
+ normalizedPath,
6387
7046
  `${collection.name}_${folderKey}`,
6388
7047
  collectionIndexDefinitions,
6389
7048
  item,
@@ -6392,17 +7051,17 @@ var Database = class {
6392
7051
  ),
6393
7052
  {
6394
7053
  type: "del",
6395
- key: itemKey,
7054
+ key: normalizedPath,
6396
7055
  sublevel: rootSublevel
6397
7056
  }
6398
7057
  ]);
6399
7058
  }
6400
7059
  if (!collection?.isDetached) {
6401
7060
  if (this.bridge) {
6402
- await this.bridge.delete(normalizePath(filepath));
7061
+ await this.bridge.delete(normalizedPath);
6403
7062
  }
6404
7063
  try {
6405
- await this.onDelete(normalizePath(filepath));
7064
+ await this.onDelete(normalizedPath);
6406
7065
  } catch (e) {
6407
7066
  throw new GraphQLError5(
6408
7067
  `Error running onDelete hook for ${filepath}: ${e}`,
@@ -6437,20 +7096,26 @@ var Database = class {
6437
7096
  );
6438
7097
  const doc = await level2.keys({ limit: 1 }).next();
6439
7098
  if (!doc) {
6440
- await _indexContent(
6441
- this,
6442
- level2,
6443
- contentPaths,
7099
+ await _indexContent({
7100
+ database: this,
7101
+ level: level2,
7102
+ documentPaths: contentPaths,
6444
7103
  enqueueOps,
6445
7104
  collection,
6446
- userFields.map((field) => [
7105
+ passwordFields: userFields.map((field) => [
6447
7106
  ...field.path,
6448
7107
  field.passwordFieldName
6449
7108
  ])
6450
- );
7109
+ });
6451
7110
  }
6452
7111
  } else {
6453
- await _indexContent(this, level, contentPaths, enqueueOps, collection);
7112
+ await _indexContent({
7113
+ database: this,
7114
+ level,
7115
+ documentPaths: contentPaths,
7116
+ enqueueOps,
7117
+ collection
7118
+ });
6454
7119
  }
6455
7120
  }
6456
7121
  );
@@ -6536,6 +7201,9 @@ var Database = class {
6536
7201
  info: templateInfo
6537
7202
  };
6538
7203
  }
7204
+ /**
7205
+ * Clears the internal cache of the tinaSchema and the lookup file. This allows the state to be reset
7206
+ */
6539
7207
  clearCache() {
6540
7208
  this.tinaSchema = null;
6541
7209
  this._lookup = null;
@@ -6586,7 +7254,15 @@ var hashPasswordValues = async (data, passwordFields) => Promise.all(
6586
7254
  )
6587
7255
  );
6588
7256
  var isGitKeep = (filepath, collection) => filepath.endsWith(`.gitkeep.${collection?.format || "md"}`);
6589
- 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
+ }) => {
6590
7266
  let collectionIndexDefinitions;
6591
7267
  let collectionPath;
6592
7268
  if (collection) {
@@ -6597,6 +7273,7 @@ var _indexContent = async (database, level, documentPaths, enqueueOps, collectio
6597
7273
  }
6598
7274
  collectionPath = collection.path;
6599
7275
  }
7276
+ const collectionReferences = (await database.getCollectionReferences())?.[collection?.name];
6600
7277
  const tinaSchema = await database.getSchema();
6601
7278
  let templateInfo = null;
6602
7279
  if (collection) {
@@ -6618,12 +7295,61 @@ var _indexContent = async (database, level, documentPaths, enqueueOps, collectio
6618
7295
  await hashPasswordValues(aliasedData, passwordFields);
6619
7296
  }
6620
7297
  const normalizedPath = normalizePath(filepath);
7298
+ const rootSublevel = level.sublevel(
7299
+ CONTENT_ROOT_PREFIX,
7300
+ SUBLEVEL_OPTIONS
7301
+ );
6621
7302
  const folderKey = folderTreeBuilder.update(
6622
7303
  normalizedPath,
6623
7304
  collectionPath || ""
6624
7305
  );
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
+ }
7342
+ }
6625
7343
  if (!isGitKeep(filepath, collection)) {
6626
7344
  await enqueueOps([
7345
+ ...makeRefOpsForDocument(
7346
+ normalizedPath,
7347
+ collection?.name,
7348
+ collectionReferences,
7349
+ aliasedData,
7350
+ "put",
7351
+ level
7352
+ ),
6627
7353
  ...makeIndexOpsForDocument(
6628
7354
  normalizedPath,
6629
7355
  collection?.name,
@@ -6632,6 +7358,7 @@ var _indexContent = async (database, level, documentPaths, enqueueOps, collectio
6632
7358
  "put",
6633
7359
  level
6634
7360
  ),
7361
+ // folder indexes
6635
7362
  ...makeIndexOpsForDocument(
6636
7363
  normalizedPath,
6637
7364
  `${collection?.name}_${folderKey}`,
@@ -6686,6 +7413,7 @@ var _deleteIndexContent = async (database, documentPaths, enqueueOps, collection
6686
7413
  throw new Error(`No indexDefinitions for collection ${collection.name}`);
6687
7414
  }
6688
7415
  }
7416
+ const collectionReferences = (await database.getCollectionReferences())?.[collection?.name];
6689
7417
  const tinaSchema = await database.getSchema();
6690
7418
  let templateInfo = null;
6691
7419
  if (collection) {
@@ -6709,6 +7437,14 @@ var _deleteIndexContent = async (database, documentPaths, enqueueOps, collection
6709
7437
  item
6710
7438
  ) : item;
6711
7439
  await enqueueOps([
7440
+ ...makeRefOpsForDocument(
7441
+ itemKey,
7442
+ collection?.name,
7443
+ collectionReferences,
7444
+ aliasedData,
7445
+ "del",
7446
+ database.contentLevel
7447
+ ),
6712
7448
  ...makeIndexOpsForDocument(
6713
7449
  itemKey,
6714
7450
  collection.name,
@@ -6717,6 +7453,7 @@ var _deleteIndexContent = async (database, documentPaths, enqueueOps, collection
6717
7453
  "del",
6718
7454
  database.contentLevel
6719
7455
  ),
7456
+ // folder indexes
6720
7457
  ...makeIndexOpsForDocument(
6721
7458
  itemKey,
6722
7459
  `${collection?.name}_${folderKey}`,
@@ -6837,8 +7574,8 @@ import path6 from "path";
6837
7574
  import normalize from "normalize-path";
6838
7575
  var FilesystemBridge = class {
6839
7576
  constructor(rootPath, outputPath) {
6840
- this.rootPath = rootPath || "";
6841
- this.outputPath = outputPath || rootPath;
7577
+ this.rootPath = path6.resolve(rootPath);
7578
+ this.outputPath = outputPath ? path6.resolve(outputPath) : this.rootPath;
6842
7579
  }
6843
7580
  async glob(pattern, extension) {
6844
7581
  const basePath = path6.join(this.outputPath, ...pattern.split("/"));
@@ -6850,19 +7587,19 @@ var FilesystemBridge = class {
6850
7587
  }
6851
7588
  );
6852
7589
  const posixRootPath = normalize(this.outputPath);
6853
- return items.map((item) => {
6854
- return item.replace(posixRootPath, "").replace(/^\/|\/$/g, "");
6855
- });
7590
+ return items.map(
7591
+ (item) => item.substring(posixRootPath.length).replace(/^\/|\/$/g, "")
7592
+ );
6856
7593
  }
6857
7594
  async delete(filepath) {
6858
7595
  await fs2.remove(path6.join(this.outputPath, filepath));
6859
7596
  }
6860
7597
  async get(filepath) {
6861
- return fs2.readFileSync(path6.join(this.outputPath, filepath)).toString();
7598
+ return (await fs2.readFile(path6.join(this.outputPath, filepath))).toString();
6862
7599
  }
6863
7600
  async put(filepath, data, basePathOverride) {
6864
7601
  const basePath = basePathOverride || this.outputPath;
6865
- await fs2.outputFileSync(path6.join(basePath, filepath), data);
7602
+ await fs2.outputFile(path6.join(basePath, filepath), data);
6866
7603
  }
6867
7604
  };
6868
7605
  var AuditFileSystemBridge = class extends FilesystemBridge {
@@ -6932,17 +7669,26 @@ var IsomorphicBridge = class {
6932
7669
  getAuthor() {
6933
7670
  return {
6934
7671
  ...this.author,
6935
- timestamp: Math.round(new Date().getTime() / 1e3),
7672
+ timestamp: Math.round((/* @__PURE__ */ new Date()).getTime() / 1e3),
6936
7673
  timezoneOffset: 0
6937
7674
  };
6938
7675
  }
6939
7676
  getCommitter() {
6940
7677
  return {
6941
7678
  ...this.committer,
6942
- timestamp: Math.round(new Date().getTime() / 1e3),
7679
+ timestamp: Math.round((/* @__PURE__ */ new Date()).getTime() / 1e3),
6943
7680
  timezoneOffset: 0
6944
7681
  };
6945
7682
  }
7683
+ /**
7684
+ * Recursively populate paths matching `pattern` for the given `entry`
7685
+ *
7686
+ * @param pattern - pattern to filter paths by
7687
+ * @param entry - TreeEntry to start building list from
7688
+ * @param path - base path
7689
+ * @param results
7690
+ * @private
7691
+ */
6946
7692
  async listEntries({
6947
7693
  pattern,
6948
7694
  entry,
@@ -6975,6 +7721,15 @@ var IsomorphicBridge = class {
6975
7721
  });
6976
7722
  }
6977
7723
  }
7724
+ /**
7725
+ * For the specified path, returns an object with an array containing the parts of the path (pathParts)
7726
+ * and an array containing the WalkerEntry objects for the path parts (pathEntries). Any null elements in the
7727
+ * pathEntries are placeholders for non-existent entries.
7728
+ *
7729
+ * @param path - path being resolved
7730
+ * @param ref - ref to resolve path entries for
7731
+ * @private
7732
+ */
6978
7733
  async resolvePathEntries(path7, ref) {
6979
7734
  let pathParts = path7.split("/");
6980
7735
  const result = await git2.walk({
@@ -7005,6 +7760,17 @@ var IsomorphicBridge = class {
7005
7760
  }
7006
7761
  return { pathParts, pathEntries };
7007
7762
  }
7763
+ /**
7764
+ * Updates tree entry and associated parent tree entries
7765
+ *
7766
+ * @param existingOid - the existing OID
7767
+ * @param updatedOid - the updated OID
7768
+ * @param path - the path of the entry being updated
7769
+ * @param type - the type of the entry being updated (blob or tree)
7770
+ * @param pathEntries - parent path entries
7771
+ * @param pathParts - parent path parts
7772
+ * @private
7773
+ */
7008
7774
  async updateTreeHierarchy(existingOid, updatedOid, path7, type, pathEntries, pathParts) {
7009
7775
  const lastIdx = pathEntries.length - 1;
7010
7776
  const parentEntry = pathEntries[lastIdx];
@@ -7060,6 +7826,13 @@ var IsomorphicBridge = class {
7060
7826
  );
7061
7827
  }
7062
7828
  }
7829
+ /**
7830
+ * Creates a commit for the specified tree and updates the specified ref to point to the commit
7831
+ *
7832
+ * @param treeSha - sha of the new tree
7833
+ * @param ref - the ref that should be updated
7834
+ * @private
7835
+ */
7063
7836
  async commitTree(treeSha, ref) {
7064
7837
  const commitSha = await git2.writeCommit({
7065
7838
  ...this.isomorphicConfig,
@@ -7072,6 +7845,7 @@ var IsomorphicBridge = class {
7072
7845
  })
7073
7846
  ],
7074
7847
  message: this.commitMessage,
7848
+ // TODO these should be configurable
7075
7849
  author: this.getAuthor(),
7076
7850
  committer: this.getCommitter()
7077
7851
  }
@@ -7309,5 +8083,5 @@ export {
7309
8083
  transformDocument,
7310
8084
  transformDocumentIntoPayload
7311
8085
  };
7312
- //! Replaces _.flattenDeep()
7313
8086
  //! Replaces _.get()
8087
+ //! Replaces _.flattenDeep()