@tinacms/graphql 1.1.0 → 1.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -39,22 +39,26 @@ var __export = (target, all) => {
39
39
  for (var name2 in all)
40
40
  __defProp(target, name2, { get: all[name2], enumerable: true });
41
41
  };
42
- var __reExport = (target, module2, desc) => {
43
- if (module2 && typeof module2 === "object" || typeof module2 === "function") {
44
- for (let key of __getOwnPropNames(module2))
42
+ var __reExport = (target, module3, desc) => {
43
+ if (module3 && typeof module3 === "object" || typeof module3 === "function") {
44
+ for (let key of __getOwnPropNames(module3))
45
45
  if (!__hasOwnProp.call(target, key) && key !== "default")
46
- __defProp(target, key, { get: () => module2[key], enumerable: !(desc = __getOwnPropDesc(module2, key)) || desc.enumerable });
46
+ __defProp(target, key, { get: () => module3[key], enumerable: !(desc = __getOwnPropDesc(module3, key)) || desc.enumerable });
47
47
  }
48
48
  return target;
49
49
  };
50
- var __toModule = (module2) => {
51
- return __reExport(__markAsModule(__defProp(module2 != null ? __create(__getProtoOf(module2)) : {}, "default", module2 && module2.__esModule && "default" in module2 ? { get: () => module2.default, enumerable: true } : { value: module2, enumerable: true })), module2);
50
+ var __toModule = (module3) => {
51
+ return __reExport(__markAsModule(__defProp(module3 != null ? __create(__getProtoOf(module3)) : {}, "default", module3 && module3.__esModule && "default" in module3 ? { get: () => module3.default, enumerable: true } : { value: module3, enumerable: true })), module3);
52
52
  };
53
53
 
54
54
  // src/index.ts
55
55
  __export(exports, {
56
+ AuditFileSystemBridge: () => AuditFileSystemBridge,
57
+ FilesystemBridge: () => FilesystemBridge,
58
+ IsomorphicBridge: () => IsomorphicBridge,
56
59
  TinaFetchError: () => TinaFetchError,
57
60
  TinaGraphQLError: () => TinaGraphQLError,
61
+ TinaLevelClient: () => TinaLevelClient,
58
62
  TinaParseDocumentError: () => TinaParseDocumentError,
59
63
  TinaQueryError: () => TinaQueryError,
60
64
  assertShape: () => assertShape,
@@ -69,9 +73,9 @@ __export(exports, {
69
73
  sequential: () => sequential,
70
74
  stringifyFile: () => stringifyFile
71
75
  });
72
- var import_fs_extra2 = __toModule(require("fs-extra"));
73
- var import_path4 = __toModule(require("path"));
74
- var import_graphql6 = __toModule(require("graphql"));
76
+ var import_fs_extra4 = __toModule(require("fs-extra"));
77
+ var import_path6 = __toModule(require("path"));
78
+ var import_graphql7 = __toModule(require("graphql"));
75
79
 
76
80
  // src/build.ts
77
81
  var import_lodash3 = __toModule(require("lodash"));
@@ -113,6 +117,12 @@ function assertShape(value, yupSchema, errorMessage) {
113
117
  });
114
118
  }
115
119
  }
120
+ var atob = (b64Encoded) => {
121
+ return Buffer.from(b64Encoded, "base64").toString();
122
+ };
123
+ var btoa = (string2) => {
124
+ return Buffer.from(string2).toString("base64");
125
+ };
116
126
  var lastItem = (arr) => {
117
127
  return arr[arr.length - 1];
118
128
  };
@@ -2186,14 +2196,14 @@ var Builder = class {
2186
2196
  collection,
2187
2197
  collections
2188
2198
  }) => {
2189
- const extra = this.database.store.supportsIndexing() ? [
2199
+ const extra = [
2190
2200
  await this._connectionFilterBuilder({
2191
2201
  fieldName,
2192
2202
  namespace,
2193
2203
  collection,
2194
2204
  collections
2195
2205
  })
2196
- ] : [];
2206
+ ];
2197
2207
  return astBuilder.FieldDefinition({
2198
2208
  name: fieldName,
2199
2209
  required: true,
@@ -2473,14 +2483,19 @@ var validateField = async (field) => {
2473
2483
 
2474
2484
  // package.json
2475
2485
  var name = "@tinacms/graphql";
2476
- var version = "1.1.0";
2486
+ var version = "1.3.0";
2477
2487
  var main = "dist/index.js";
2488
+ var module2 = "dist/index.es.js";
2478
2489
  var typings = "dist/index.d.ts";
2479
2490
  var files = [
2480
2491
  "package.json",
2481
2492
  "dist"
2482
2493
  ];
2483
- var license = "Apache-2.0";
2494
+ var exports = {
2495
+ import: "./dist/index.es.js",
2496
+ require: "./dist/index.js"
2497
+ };
2498
+ var license = "SEE LICENSE IN LICENSE";
2484
2499
  var buildConfig = {
2485
2500
  entryPoints: [
2486
2501
  {
@@ -2501,9 +2516,9 @@ var scripts = {
2501
2516
  var dependencies = {
2502
2517
  "@graphql-tools/relay-operation-optimizer": "^6.4.1",
2503
2518
  "@iarna/toml": "^2.2.5",
2504
- "@tinacms/datalayer": "workspace:*",
2505
2519
  "@tinacms/mdx": "workspace:*",
2506
2520
  "@tinacms/schema-tools": "workspace:*",
2521
+ "abstract-level": "^1.0.3",
2507
2522
  "body-parser": "^1.19.0",
2508
2523
  cors: "^2.8.5",
2509
2524
  dataloader: "^2.0.0",
@@ -2515,12 +2530,16 @@ var dependencies = {
2515
2530
  "fast-glob": "^3.2.5",
2516
2531
  flat: "^5.0.2",
2517
2532
  "fs-extra": "^9.0.1",
2533
+ "glob-parent": "^6.0.2",
2518
2534
  graphql: "15.8.0",
2519
2535
  "graphql-type-json": "^0.3.2",
2520
2536
  "gray-matter": "^4.0.2",
2537
+ "isomorphic-git": "^1.21.0",
2521
2538
  "js-yaml": "^3.14.1",
2539
+ "jsonpath-plus": "^6.0.1",
2522
2540
  leveldown: "^6.1.0",
2523
2541
  lodash: "^4.17.20",
2542
+ "many-level": "^2.0.0",
2524
2543
  mdast: "^3.0.0",
2525
2544
  "mdast-util-from-markdown": "^1.0.0",
2526
2545
  "mdast-util-mdx": "^1.1.0",
@@ -2529,6 +2548,7 @@ var dependencies = {
2529
2548
  "micromark-extension-mdxjs": "^1.0.0",
2530
2549
  "normalize-path": "^3.0.0",
2531
2550
  prettier: "^2.2.1",
2551
+ "readable-stream": "^4.3.0",
2532
2552
  "rehype-format": "^3.1.0",
2533
2553
  "rehype-stringify": "^8.0.0",
2534
2554
  remark: "^13.0.0",
@@ -2553,7 +2573,6 @@ var repository = {
2553
2573
  directory: "packages/tina-graphql"
2554
2574
  };
2555
2575
  var devDependencies = {
2556
- "@tinacms/datalayer": "workspace:*",
2557
2576
  "@tinacms/schema-tools": "workspace:*",
2558
2577
  "@tinacms/scripts": "workspace:*",
2559
2578
  "@types/cors": "^2.8.7",
@@ -2575,6 +2594,7 @@ var devDependencies = {
2575
2594
  "jest-diff": "27.0.6",
2576
2595
  "jest-file-snapshot": "^0.5.0",
2577
2596
  "jest-matcher-utils": "27.0.6",
2597
+ "memory-level": "^1.0.0",
2578
2598
  nodemon: "2.0.19",
2579
2599
  typescript: "4.3.5"
2580
2600
  };
@@ -2582,8 +2602,10 @@ var package_default = {
2582
2602
  name,
2583
2603
  version,
2584
2604
  main,
2605
+ module: module2,
2585
2606
  typings,
2586
2607
  files,
2608
+ exports,
2587
2609
  license,
2588
2610
  buildConfig,
2589
2611
  scripts,
@@ -2623,10 +2645,8 @@ var buildDotTinaFiles = async ({
2623
2645
  flags = [],
2624
2646
  buildSDK = true
2625
2647
  }) => {
2626
- if (database.store.supportsIndexing()) {
2627
- if (flags.indexOf("experimentalData") === -1) {
2628
- flags.push("experimentalData");
2629
- }
2648
+ if (flags.indexOf("experimentalData") === -1) {
2649
+ flags.push("experimentalData");
2630
2650
  }
2631
2651
  const tinaSchema = await createSchema({ schema: config, flags });
2632
2652
  const builder = await createBuilder({
@@ -2778,7 +2798,7 @@ var TinaQueryError = class extends TinaFetchError {
2778
2798
  };
2779
2799
  var TinaParseDocumentError = class extends TinaFetchError {
2780
2800
  constructor(args) {
2781
- super(`Error Parsing file ${args.file} from collection ${args.collection}. ${auditMessage(args.includeAuditMessage)}`, args);
2801
+ super(`Error parsing file ${args.file} from collection ${args.collection}. ${auditMessage(args.includeAuditMessage)}`, args);
2782
2802
  }
2783
2803
  toString() {
2784
2804
  return super.toString() + "\n OriginalError: \n" + this.originalError.toString();
@@ -2800,9 +2820,6 @@ var handleFetchErrorError = (e, verbose) => {
2800
2820
  throw e;
2801
2821
  };
2802
2822
 
2803
- // src/resolver/index.ts
2804
- var import_datalayer = __toModule(require("@tinacms/datalayer"));
2805
-
2806
2823
  // src/resolver/filter-utils.ts
2807
2824
  var resolveReferences = async (filter, fields, resolver) => {
2808
2825
  for (const fieldKey of Object.keys(filter)) {
@@ -2931,9 +2948,9 @@ var resolveMediaRelativeToCloud = (value, config = { useRelativeMedia: true }, s
2931
2948
  return value;
2932
2949
  }
2933
2950
  };
2934
- var cleanUpSlashes = (path5) => {
2935
- if (path5) {
2936
- return `/${path5.replace(/^\/+|\/+$/gm, "")}`;
2951
+ var cleanUpSlashes = (path6) => {
2952
+ if (path6) {
2953
+ return `/${path6.replace(/^\/+|\/+$/gm, "")}`;
2937
2954
  }
2938
2955
  return "";
2939
2956
  };
@@ -2947,6 +2964,389 @@ var hasTinaMediaConfig = (schema) => {
2947
2964
 
2948
2965
  // src/resolver/index.ts
2949
2966
  var import_graphql3 = __toModule(require("graphql"));
2967
+
2968
+ // src/database/datalayer.ts
2969
+ var import_jsonpath_plus = __toModule(require("jsonpath-plus"));
2970
+
2971
+ // src/database/level.ts
2972
+ var INDEX_KEY_FIELD_SEPARATOR = "";
2973
+ var CONTENT_ROOT_PREFIX = "~";
2974
+ var SUBLEVEL_OPTIONS = {
2975
+ separator: INDEX_KEY_FIELD_SEPARATOR,
2976
+ valueEncoding: "json"
2977
+ };
2978
+
2979
+ // src/database/datalayer.ts
2980
+ var OP;
2981
+ (function(OP2) {
2982
+ OP2["EQ"] = "eq";
2983
+ OP2["GT"] = "gt";
2984
+ OP2["LT"] = "lt";
2985
+ OP2["GTE"] = "gte";
2986
+ OP2["LTE"] = "lte";
2987
+ OP2["STARTS_WITH"] = "startsWith";
2988
+ OP2["IN"] = "in";
2989
+ })(OP || (OP = {}));
2990
+ var DEFAULT_COLLECTION_SORT_KEY = "__filepath__";
2991
+ var DEFAULT_NUMERIC_LPAD = 4;
2992
+ var applyPadding = (input, pad) => {
2993
+ if (pad) {
2994
+ if (Array.isArray(input)) {
2995
+ return input.map((val) => String(val).padStart(pad.maxLength, pad.fillString));
2996
+ } else {
2997
+ return String(input).padStart(pad.maxLength, pad.fillString);
2998
+ }
2999
+ }
3000
+ return input;
3001
+ };
3002
+ var getFilterOperator = (expression, operand) => {
3003
+ return (expression[operand] || expression[operand] === 0) && operand;
3004
+ };
3005
+ var inferOperatorFromFilter = (filterOperator) => {
3006
+ switch (filterOperator) {
3007
+ case "after":
3008
+ return OP.GT;
3009
+ case "before":
3010
+ return OP.LT;
3011
+ case "eq":
3012
+ return OP.EQ;
3013
+ case "startsWith":
3014
+ return OP.STARTS_WITH;
3015
+ case "lt":
3016
+ return OP.LT;
3017
+ case "lte":
3018
+ return OP.LTE;
3019
+ case "gt":
3020
+ return OP.GT;
3021
+ case "gte":
3022
+ return OP.GTE;
3023
+ case "in":
3024
+ return OP.IN;
3025
+ default:
3026
+ throw new Error(`unsupported filter condition: '${filterOperator}'`);
3027
+ }
3028
+ };
3029
+ var makeKeyForField = (definition, data, stringEscaper2, maxStringLength = 100) => {
3030
+ const valueParts = [];
3031
+ for (const field of definition.fields) {
3032
+ if (field.name in data && data[field.name] !== void 0 && data[field.name] !== null) {
3033
+ const rawValue = data[field.name];
3034
+ const resolvedValue = String(field.type === "datetime" ? new Date(rawValue).getTime() : field.type === "string" ? stringEscaper2(rawValue) : rawValue).substring(0, maxStringLength);
3035
+ valueParts.push(applyPadding(resolvedValue, field.pad));
3036
+ } else {
3037
+ return null;
3038
+ }
3039
+ }
3040
+ return valueParts.join(INDEX_KEY_FIELD_SEPARATOR);
3041
+ };
3042
+ var coerceFilterChainOperands = (filterChain, escapeString = stringEscaper) => {
3043
+ const result = [];
3044
+ if (filterChain.length) {
3045
+ for (const filter of filterChain) {
3046
+ const dataType = filter.type;
3047
+ if (dataType === "datetime") {
3048
+ if (filter.leftOperand !== void 0) {
3049
+ result.push(__spreadProps(__spreadValues({}, filter), {
3050
+ rightOperand: new Date(filter.rightOperand).getTime(),
3051
+ leftOperand: new Date(filter.leftOperand).getTime()
3052
+ }));
3053
+ } else {
3054
+ if (Array.isArray(filter.rightOperand)) {
3055
+ result.push(__spreadProps(__spreadValues({}, filter), {
3056
+ rightOperand: filter.rightOperand.map((operand) => new Date(operand).getTime())
3057
+ }));
3058
+ } else {
3059
+ result.push(__spreadProps(__spreadValues({}, filter), {
3060
+ rightOperand: new Date(filter.rightOperand).getTime()
3061
+ }));
3062
+ }
3063
+ }
3064
+ } else if (dataType === "string") {
3065
+ if (filter.leftOperand !== void 0) {
3066
+ result.push(__spreadProps(__spreadValues({}, filter), {
3067
+ rightOperand: applyPadding(escapeString(filter.rightOperand), filter.pad),
3068
+ leftOperand: applyPadding(escapeString(filter.leftOperand), filter.pad)
3069
+ }));
3070
+ } else {
3071
+ result.push(__spreadProps(__spreadValues({}, filter), {
3072
+ rightOperand: applyPadding(escapeString(filter.rightOperand), filter.pad)
3073
+ }));
3074
+ }
3075
+ } else {
3076
+ result.push(__spreadValues({}, filter));
3077
+ }
3078
+ }
3079
+ }
3080
+ return result;
3081
+ };
3082
+ var makeFilter = ({
3083
+ filterChain
3084
+ }) => {
3085
+ return (values) => {
3086
+ for (const filter of filterChain) {
3087
+ const dataType = filter.type;
3088
+ const resolvedValues = (0, import_jsonpath_plus.JSONPath)({
3089
+ path: filter.pathExpression,
3090
+ json: values
3091
+ });
3092
+ if (!resolvedValues || !resolvedValues.length) {
3093
+ return false;
3094
+ }
3095
+ let operands;
3096
+ if (dataType === "string" || dataType === "reference") {
3097
+ operands = resolvedValues;
3098
+ } else if (dataType === "number") {
3099
+ operands = resolvedValues.map((resolvedValue) => Number(resolvedValue));
3100
+ } else if (dataType === "datetime") {
3101
+ operands = resolvedValues.map((resolvedValue) => {
3102
+ const coerced = new Date(resolvedValue).getTime();
3103
+ return isNaN(coerced) ? Number(resolvedValue) : coerced;
3104
+ });
3105
+ } else if (dataType === "boolean") {
3106
+ operands = resolvedValues.map((resolvedValue) => typeof resolvedValue === "boolean" && resolvedValue || resolvedValue === "true" || resolvedValue === "1");
3107
+ } else {
3108
+ throw new Error(`Unexpected datatype ${dataType}`);
3109
+ }
3110
+ const { operator } = filter;
3111
+ let matches = false;
3112
+ if (operator) {
3113
+ switch (operator) {
3114
+ case OP.EQ:
3115
+ if (operands.findIndex((operand) => operand === filter.rightOperand) >= 0) {
3116
+ matches = true;
3117
+ }
3118
+ break;
3119
+ case OP.GT:
3120
+ for (const operand of operands) {
3121
+ if (operand > filter.rightOperand) {
3122
+ matches = true;
3123
+ break;
3124
+ }
3125
+ }
3126
+ break;
3127
+ case OP.LT:
3128
+ for (const operand of operands) {
3129
+ if (operand < filter.rightOperand) {
3130
+ matches = true;
3131
+ break;
3132
+ }
3133
+ }
3134
+ break;
3135
+ case OP.GTE:
3136
+ for (const operand of operands) {
3137
+ if (operand >= filter.rightOperand) {
3138
+ matches = true;
3139
+ break;
3140
+ }
3141
+ }
3142
+ break;
3143
+ case OP.LTE:
3144
+ for (const operand of operands) {
3145
+ if (operand <= filter.rightOperand) {
3146
+ matches = true;
3147
+ break;
3148
+ }
3149
+ }
3150
+ break;
3151
+ case OP.IN:
3152
+ for (const operand of operands) {
3153
+ if (filter.rightOperand.indexOf(operand) >= 0) {
3154
+ matches = true;
3155
+ break;
3156
+ }
3157
+ }
3158
+ break;
3159
+ case OP.STARTS_WITH:
3160
+ for (const operand of operands) {
3161
+ if (operand.startsWith(filter.rightOperand)) {
3162
+ matches = true;
3163
+ break;
3164
+ }
3165
+ }
3166
+ break;
3167
+ default:
3168
+ throw new Error(`unexpected operator ${operator}`);
3169
+ }
3170
+ } else {
3171
+ const { rightOperator, leftOperator, rightOperand, leftOperand } = filter;
3172
+ for (const operand of operands) {
3173
+ let rightMatches = false;
3174
+ let leftMatches = false;
3175
+ if (rightOperator === OP.LTE && operand <= rightOperand) {
3176
+ rightMatches = true;
3177
+ } else if (rightOperator === OP.LT && operand < rightOperand) {
3178
+ rightMatches = true;
3179
+ }
3180
+ if (leftOperator === OP.GTE && operand >= leftOperand) {
3181
+ leftMatches = true;
3182
+ } else if (leftOperator === OP.GT && operand > leftOperand) {
3183
+ leftMatches = true;
3184
+ }
3185
+ if (rightMatches && leftMatches) {
3186
+ matches = true;
3187
+ break;
3188
+ }
3189
+ }
3190
+ }
3191
+ if (!matches) {
3192
+ return false;
3193
+ }
3194
+ }
3195
+ return true;
3196
+ };
3197
+ };
3198
+ var makeFilterChain = ({
3199
+ conditions
3200
+ }) => {
3201
+ const filterChain = [];
3202
+ if (!conditions) {
3203
+ return filterChain;
3204
+ }
3205
+ for (const condition of conditions) {
3206
+ const { filterPath, filterExpression } = condition;
3207
+ const _a = filterExpression, { _type } = _a, keys = __objRest(_a, ["_type"]);
3208
+ const [key1, key2, ...extraKeys] = Object.keys(keys);
3209
+ if (extraKeys.length) {
3210
+ throw new Error(`Unexpected keys: [${extraKeys.join(",")}] in filter expression`);
3211
+ }
3212
+ if (key1 && !key2) {
3213
+ filterChain.push({
3214
+ pathExpression: filterPath,
3215
+ rightOperand: filterExpression[key1],
3216
+ operator: inferOperatorFromFilter(key1),
3217
+ type: _type,
3218
+ pad: _type === "number" ? { fillString: "0", maxLength: DEFAULT_NUMERIC_LPAD } : void 0
3219
+ });
3220
+ } else if (key1 && key2) {
3221
+ const leftFilterOperator = getFilterOperator(filterExpression, "gt") || getFilterOperator(filterExpression, "gte") || getFilterOperator(filterExpression, "after") || void 0;
3222
+ const rightFilterOperator = getFilterOperator(filterExpression, "lt") || getFilterOperator(filterExpression, "lte") || getFilterOperator(filterExpression, "before") || void 0;
3223
+ let leftOperand;
3224
+ let rightOperand;
3225
+ if (rightFilterOperator && leftFilterOperator) {
3226
+ if (key1 === leftFilterOperator) {
3227
+ leftOperand = filterExpression[key1];
3228
+ rightOperand = filterExpression[key2];
3229
+ } else {
3230
+ rightOperand = filterExpression[key1];
3231
+ leftOperand = filterExpression[key2];
3232
+ }
3233
+ filterChain.push({
3234
+ pathExpression: filterPath,
3235
+ rightOperand,
3236
+ leftOperand,
3237
+ leftOperator: inferOperatorFromFilter(leftFilterOperator),
3238
+ rightOperator: inferOperatorFromFilter(rightFilterOperator),
3239
+ type: _type,
3240
+ pad: _type === "number" ? { fillString: "0", maxLength: DEFAULT_NUMERIC_LPAD } : void 0
3241
+ });
3242
+ } else {
3243
+ throw new Error(`Filter on field '${filterPath}' has invalid combination of conditions: '${key1}, ${key2}'`);
3244
+ }
3245
+ }
3246
+ }
3247
+ return filterChain;
3248
+ };
3249
+ var makeFilterSuffixes = (filterChain, index) => {
3250
+ if (filterChain && filterChain.length) {
3251
+ const indexFields = index.fields.map((field) => field.name);
3252
+ const orderedFilterChain = [];
3253
+ for (const filter of filterChain) {
3254
+ const idx = indexFields.indexOf(filter.pathExpression);
3255
+ if (idx === -1) {
3256
+ return;
3257
+ }
3258
+ if (filter.operator && filter.operator === OP.IN) {
3259
+ return;
3260
+ }
3261
+ orderedFilterChain[idx] = filter;
3262
+ }
3263
+ const baseFragments = [];
3264
+ let rightSuffix;
3265
+ let leftSuffix;
3266
+ let ternaryFilter = false;
3267
+ if (orderedFilterChain[filterChain.length - 1] && !orderedFilterChain[filterChain.length - 1].operator) {
3268
+ ternaryFilter = true;
3269
+ }
3270
+ for (let i = 0; i < orderedFilterChain.length; i++) {
3271
+ const filter = orderedFilterChain[i];
3272
+ if (!filter) {
3273
+ return;
3274
+ }
3275
+ if (Number(i) < indexFields.length - 1) {
3276
+ if (!filter.operator) {
3277
+ return;
3278
+ }
3279
+ const binaryFilter = filter;
3280
+ if (binaryFilter.operator !== OP.EQ) {
3281
+ return;
3282
+ }
3283
+ baseFragments.push(applyPadding(orderedFilterChain[i].rightOperand, orderedFilterChain[i].pad));
3284
+ } else {
3285
+ if (ternaryFilter) {
3286
+ leftSuffix = applyPadding(orderedFilterChain[i].leftOperand, orderedFilterChain[i].pad);
3287
+ rightSuffix = applyPadding(orderedFilterChain[i].rightOperand, orderedFilterChain[i].pad);
3288
+ } else {
3289
+ const op = orderedFilterChain[i].operator;
3290
+ const operand = applyPadding(orderedFilterChain[i].rightOperand, orderedFilterChain[i].pad);
3291
+ if (op === OP.LT || op === OP.LTE) {
3292
+ rightSuffix = operand;
3293
+ } else if (op === OP.GT || op === OP.GTE) {
3294
+ leftSuffix = operand;
3295
+ } else {
3296
+ rightSuffix = operand;
3297
+ leftSuffix = operand;
3298
+ }
3299
+ }
3300
+ }
3301
+ }
3302
+ return {
3303
+ left: leftSuffix && [...baseFragments, leftSuffix].join(INDEX_KEY_FIELD_SEPARATOR) || void 0,
3304
+ right: rightSuffix && [...baseFragments, rightSuffix].join(INDEX_KEY_FIELD_SEPARATOR) || void 0
3305
+ };
3306
+ } else {
3307
+ return {};
3308
+ }
3309
+ };
3310
+ var makeIndexOpsForDocument = (filepath, collection, indexDefinitions, data, opType, level, escapeStr = stringEscaper) => {
3311
+ const result = [];
3312
+ if (collection) {
3313
+ const collectionSublevel = level.sublevel(collection, SUBLEVEL_OPTIONS);
3314
+ for (const [sort, definition] of Object.entries(indexDefinitions)) {
3315
+ const indexedValue = makeKeyForField(definition, data, escapeStr);
3316
+ const indexSublevel = collectionSublevel.sublevel(sort, SUBLEVEL_OPTIONS);
3317
+ if (sort === DEFAULT_COLLECTION_SORT_KEY) {
3318
+ result.push({
3319
+ type: opType,
3320
+ key: filepath,
3321
+ sublevel: indexSublevel,
3322
+ value: opType === "put" ? {} : void 0
3323
+ });
3324
+ } else {
3325
+ if (indexedValue) {
3326
+ result.push({
3327
+ type: opType,
3328
+ key: `${indexedValue}${INDEX_KEY_FIELD_SEPARATOR}${filepath}`,
3329
+ sublevel: indexSublevel,
3330
+ value: opType === "put" ? {} : void 0
3331
+ });
3332
+ }
3333
+ }
3334
+ }
3335
+ }
3336
+ return result;
3337
+ };
3338
+ var makeStringEscaper = (regex, replacement) => {
3339
+ return (input) => {
3340
+ if (Array.isArray(input)) {
3341
+ return input.map((val) => val.replace(regex, replacement));
3342
+ } else {
3343
+ return input.replace(regex, replacement);
3344
+ }
3345
+ };
3346
+ };
3347
+ var stringEscaper = makeStringEscaper(new RegExp(INDEX_KEY_FIELD_SEPARATOR, "gm"), encodeURIComponent(INDEX_KEY_FIELD_SEPARATOR));
3348
+
3349
+ // src/resolver/index.ts
2950
3350
  var createResolver = (args) => {
2951
3351
  return new Resolver(args);
2952
3352
  };
@@ -3250,7 +3650,7 @@ var Resolver = class {
3250
3650
  first: -1
3251
3651
  },
3252
3652
  collection: referencedCollection,
3253
- hydrator: (path5) => path5
3653
+ hydrator: (path6) => path6
3254
3654
  });
3255
3655
  const { edges } = resolvedCollectionConnection;
3256
3656
  const values = edges.map((edge) => edge.node);
@@ -3277,7 +3677,7 @@ var Resolver = class {
3277
3677
  }
3278
3678
  }
3279
3679
  const queryOptions = {
3280
- filterChain: (0, import_datalayer.makeFilterChain)({
3680
+ filterChain: makeFilterChain({
3281
3681
  conditions: conditions || []
3282
3682
  }),
3283
3683
  collection: collection.name,
@@ -3518,7 +3918,7 @@ var resolve = async ({
3518
3918
  }
3519
3919
  },
3520
3920
  fieldResolver: async (source = {}, _args = {}, _context, info) => {
3521
- var _a2;
3921
+ var _a2, _b, _c, _d;
3522
3922
  try {
3523
3923
  const args = JSON.parse(JSON.stringify(_args));
3524
3924
  const returnType = (0, import_graphql4.getNamedType)(info.returnType).toString();
@@ -3607,8 +4007,14 @@ var resolve = async ({
3607
4007
  })
3608
4008
  };
3609
4009
  } else if (info.fieldName === "documents" && (value == null ? void 0 : value.collection) && (value == null ? void 0 : value.hasDocuments)) {
4010
+ let filter = args.filter;
4011
+ if (typeof (args == null ? void 0 : args.filter) !== "undefined" && (args == null ? void 0 : args.filter) !== null && typeof ((_b = value == null ? void 0 : value.collection) == null ? void 0 : _b.name) === "string" && Object.keys(args.filter).includes((_c = value == null ? void 0 : value.collection) == null ? void 0 : _c.name) && typeof args.filter[(_d = value == null ? void 0 : value.collection) == null ? void 0 : _d.name] !== "undefined") {
4012
+ filter = args.filter[value.collection.name];
4013
+ }
3610
4014
  return resolver.resolveCollectionConnection({
3611
- args,
4015
+ args: __spreadProps(__spreadValues({}, args), {
4016
+ filter
4017
+ }),
3612
4018
  collection: value.collection
3613
4019
  });
3614
4020
  } else {
@@ -3732,6 +4138,7 @@ ${$_body}`, strippedContent, {
3732
4138
  case ".json":
3733
4139
  return JSON.stringify(strippedContent, null, 2);
3734
4140
  case ".yaml":
4141
+ case ".yml":
3735
4142
  return import_js_yaml.default.safeDump(strippedContent);
3736
4143
  case ".toml":
3737
4144
  return import_toml.default.stringify(strippedContent);
@@ -3765,6 +4172,7 @@ var parseFile = (content, format, yupSchema, markdownParseConfig) => {
3765
4172
  }
3766
4173
  return import_toml.default.parse(content);
3767
4174
  case ".yaml":
4175
+ case ".yml":
3768
4176
  if (!content) {
3769
4177
  return {};
3770
4178
  }
@@ -3776,31 +4184,39 @@ var parseFile = (content, format, yupSchema, markdownParseConfig) => {
3776
4184
  var normalizePath = (filepath) => filepath.replace(/\\/g, "/");
3777
4185
 
3778
4186
  // src/database/index.ts
3779
- var import_datalayer2 = __toModule(require("@tinacms/datalayer"));
3780
- var createDatabase = async (config) => {
4187
+ var createDatabase = (config) => {
3781
4188
  return new Database(__spreadProps(__spreadValues({}, config), {
3782
4189
  bridge: config.bridge,
3783
- store: config.store
4190
+ level: config.level
3784
4191
  }));
3785
4192
  };
3786
4193
  var SYSTEM_FILES = ["_schema", "_graphql", "_lookup"];
3787
4194
  var defaultStatusCallback = () => Promise.resolve();
4195
+ var defaultOnPut = () => Promise.resolve();
4196
+ var defaultOnDelete = () => Promise.resolve();
3788
4197
  var Database = class {
3789
4198
  constructor(config) {
3790
4199
  this.config = config;
3791
4200
  this.collectionForPath = async (filepath) => {
3792
- const tinaSchema = await this.getSchema();
3793
- const collection = tinaSchema.getCollectionByFullPath(filepath);
3794
- return collection;
4201
+ const tinaSchema = await this.getSchema(this.level);
4202
+ return tinaSchema.getCollectionByFullPath(filepath);
3795
4203
  };
3796
4204
  this.getGeneratedFolder = () => import_path3.default.join(this.tinaDirectory, "__generated__");
3797
4205
  this.get = async (filepath) => {
4206
+ await this.initLevel();
3798
4207
  if (SYSTEM_FILES.includes(filepath)) {
3799
4208
  throw new Error(`Unexpected get for config file ${filepath}`);
3800
4209
  } else {
3801
- const tinaSchema = await this.getSchema();
4210
+ const tinaSchema = await this.getSchema(this.level);
3802
4211
  const extension = import_path3.default.extname(filepath);
3803
- const contentObject = await this.store.get(normalizePath(filepath));
4212
+ let contentObject;
4213
+ try {
4214
+ contentObject = await this.level.sublevel(CONTENT_ROOT_PREFIX, SUBLEVEL_OPTIONS).get(normalizePath(filepath));
4215
+ } catch (e) {
4216
+ if (e.code !== "LEVEL_NOT_FOUND") {
4217
+ throw e;
4218
+ }
4219
+ }
3804
4220
  if (!contentObject) {
3805
4221
  throw new import_graphql5.GraphQLError(`Unable to find record ${filepath}`);
3806
4222
  }
@@ -3832,41 +4248,79 @@ var Database = class {
3832
4248
  }
3833
4249
  };
3834
4250
  this.addPendingDocument = async (filepath, data) => {
3835
- const { stringifiedFile, payload, keepTemplateKey } = await this.stringifyFile(filepath, data);
4251
+ await this.initLevel();
4252
+ const { stringifiedFile, payload } = await this.stringifyFile(filepath, data);
3836
4253
  const collection = await this.collectionForPath(filepath);
3837
4254
  let collectionIndexDefinitions;
3838
4255
  if (collection) {
3839
- const indexDefinitions = await this.getIndexDefinitions();
4256
+ const indexDefinitions = await this.getIndexDefinitions(this.level);
3840
4257
  collectionIndexDefinitions = indexDefinitions == null ? void 0 : indexDefinitions[collection.name];
3841
4258
  }
3842
- if (this.store.supportsSeeding()) {
3843
- await this.bridge.put(normalizePath(filepath), stringifiedFile);
4259
+ const normalizedPath = normalizePath(filepath);
4260
+ if (this.bridge) {
4261
+ await this.bridge.put(normalizedPath, stringifiedFile);
3844
4262
  }
3845
- await this.store.put(normalizePath(filepath), payload, {
3846
- keepTemplateKey,
3847
- collection: collection == null ? void 0 : collection.name,
3848
- indexDefinitions: collectionIndexDefinitions
3849
- });
4263
+ await this.onPut(normalizedPath, stringifiedFile);
4264
+ const putOps = makeIndexOpsForDocument(normalizedPath, collection == null ? void 0 : collection.name, collectionIndexDefinitions, payload, "put", this.level);
4265
+ let existingItem;
4266
+ try {
4267
+ existingItem = await this.level.sublevel(CONTENT_ROOT_PREFIX, SUBLEVEL_OPTIONS).get(normalizedPath);
4268
+ } catch (e) {
4269
+ if (e.code !== "LEVEL_NOT_FOUND") {
4270
+ throw e;
4271
+ }
4272
+ }
4273
+ const delOps = existingItem ? makeIndexOpsForDocument(normalizedPath, collection == null ? void 0 : collection.name, collectionIndexDefinitions, existingItem, "del", this.level) : [];
4274
+ const ops = [
4275
+ ...delOps,
4276
+ ...putOps,
4277
+ {
4278
+ type: "put",
4279
+ key: normalizedPath,
4280
+ value: payload,
4281
+ sublevel: this.level.sublevel(CONTENT_ROOT_PREFIX, SUBLEVEL_OPTIONS)
4282
+ }
4283
+ ];
4284
+ await this.level.batch(ops);
3850
4285
  };
3851
4286
  this.put = async (filepath, data, collection) => {
4287
+ await this.initLevel();
3852
4288
  try {
3853
4289
  if (SYSTEM_FILES.includes(filepath)) {
3854
4290
  throw new Error(`Unexpected put for config file ${filepath}`);
3855
4291
  } else {
3856
4292
  let collectionIndexDefinitions;
3857
4293
  if (collection) {
3858
- const indexDefinitions = await this.getIndexDefinitions();
4294
+ const indexDefinitions = await this.getIndexDefinitions(this.level);
3859
4295
  collectionIndexDefinitions = indexDefinitions == null ? void 0 : indexDefinitions[collection];
3860
4296
  }
3861
- const { stringifiedFile, payload, keepTemplateKey } = await this.stringifyFile(filepath, data);
3862
- if (this.store.supportsSeeding()) {
3863
- await this.bridge.put(normalizePath(filepath), stringifiedFile);
4297
+ const normalizedPath = normalizePath(filepath);
4298
+ const { stringifiedFile, payload } = await this.stringifyFile(filepath, data);
4299
+ if (this.bridge) {
4300
+ await this.bridge.put(normalizedPath, stringifiedFile);
3864
4301
  }
3865
- await this.store.put(normalizePath(filepath), payload, {
3866
- keepTemplateKey,
3867
- collection,
3868
- indexDefinitions: collectionIndexDefinitions
3869
- });
4302
+ await this.onPut(normalizedPath, stringifiedFile);
4303
+ const putOps = makeIndexOpsForDocument(normalizedPath, collection, collectionIndexDefinitions, payload, "put", this.level);
4304
+ let existingItem;
4305
+ try {
4306
+ existingItem = await this.level.sublevel(CONTENT_ROOT_PREFIX, SUBLEVEL_OPTIONS).get(normalizedPath);
4307
+ } catch (e) {
4308
+ if (e.code !== "LEVEL_NOT_FOUND") {
4309
+ throw e;
4310
+ }
4311
+ }
4312
+ const delOps = existingItem ? makeIndexOpsForDocument(normalizedPath, collection, collectionIndexDefinitions, existingItem, "del", this.level) : [];
4313
+ const ops = [
4314
+ ...delOps,
4315
+ ...putOps,
4316
+ {
4317
+ type: "put",
4318
+ key: normalizedPath,
4319
+ value: payload,
4320
+ sublevel: this.level.sublevel(CONTENT_ROOT_PREFIX, SUBLEVEL_OPTIONS)
4321
+ }
4322
+ ];
4323
+ await this.level.batch(ops);
3870
4324
  }
3871
4325
  return true;
3872
4326
  } catch (error) {
@@ -3882,7 +4336,7 @@ var Database = class {
3882
4336
  if (SYSTEM_FILES.includes(filepath)) {
3883
4337
  throw new Error(`Unexpected put for config file ${filepath}`);
3884
4338
  } else {
3885
- const tinaSchema = await this.getSchema();
4339
+ const tinaSchema = await this.getSchema(this.level);
3886
4340
  const collection = tinaSchema.getCollectionByFullPath(filepath);
3887
4341
  const templateInfo = await tinaSchema.getTemplatesForCollectable(collection);
3888
4342
  let template;
@@ -3936,43 +4390,51 @@ var Database = class {
3936
4390
  return stringifiedFile;
3937
4391
  };
3938
4392
  this.getLookup = async (returnType) => {
3939
- const lookupPath = import_path3.default.join(this.getGeneratedFolder(), `_lookup.json`);
4393
+ await this.initLevel();
4394
+ const lookupPath = normalizePath(import_path3.default.join(this.getGeneratedFolder(), `_lookup.json`));
3940
4395
  if (!this._lookup) {
3941
- const _lookup = await this.store.get(normalizePath(lookupPath));
4396
+ const _lookup = await this.level.sublevel(CONTENT_ROOT_PREFIX, SUBLEVEL_OPTIONS).get(lookupPath);
3942
4397
  this._lookup = _lookup;
3943
4398
  }
3944
4399
  return this._lookup[returnType];
3945
4400
  };
3946
4401
  this.getGraphQLSchema = async () => {
3947
- const graphqlPath = import_path3.default.join(this.getGeneratedFolder(), `_graphql.json`);
3948
- return this.store.get(normalizePath(graphqlPath));
4402
+ await this.initLevel();
4403
+ const graphqlPath = normalizePath(import_path3.default.join(this.getGeneratedFolder(), `_graphql.json`));
4404
+ return await this.level.sublevel(CONTENT_ROOT_PREFIX, SUBLEVEL_OPTIONS).get(graphqlPath);
3949
4405
  };
3950
4406
  this.getGraphQLSchemaFromBridge = async () => {
3951
- const graphqlPath = import_path3.default.join(this.getGeneratedFolder(), `_graphql.json`);
3952
- const _graphql = await this.bridge.get(normalizePath(graphqlPath));
4407
+ if (!this.bridge) {
4408
+ throw new Error(`No bridge configured`);
4409
+ }
4410
+ const graphqlPath = normalizePath(import_path3.default.join(this.getGeneratedFolder(), `_graphql.json`));
4411
+ const _graphql = await this.bridge.get(graphqlPath);
3953
4412
  return JSON.parse(_graphql);
3954
4413
  };
3955
- this.getTinaSchema = async () => {
3956
- const schemaPath = import_path3.default.join(this.getGeneratedFolder(), `_schema.json`);
3957
- return this.store.get(normalizePath(schemaPath));
4414
+ this.getTinaSchema = async (level) => {
4415
+ await this.initLevel();
4416
+ const schemaPath = normalizePath(import_path3.default.join(this.getGeneratedFolder(), `_schema.json`));
4417
+ return await (level || this.level).sublevel(CONTENT_ROOT_PREFIX, SUBLEVEL_OPTIONS).get(schemaPath);
3958
4418
  };
3959
- this.getSchema = async () => {
4419
+ this.getSchema = async (level) => {
3960
4420
  if (this.tinaSchema) {
3961
4421
  return this.tinaSchema;
3962
4422
  }
3963
- const schema = await this.getTinaSchema();
4423
+ await this.initLevel();
4424
+ const schema = await this.getTinaSchema(level || this.level);
3964
4425
  this.tinaSchema = await createSchema({ schema });
3965
4426
  return this.tinaSchema;
3966
4427
  };
3967
- this.getIndexDefinitions = async () => {
4428
+ this.getIndexDefinitions = async (level) => {
3968
4429
  if (!this.collectionIndexDefinitions) {
3969
4430
  await new Promise(async (resolve2, reject) => {
4431
+ await this.initLevel();
3970
4432
  try {
3971
- const schema = await this.getSchema();
4433
+ const schema = await this.getSchema(level || this.level);
3972
4434
  const collections = schema.getCollections();
3973
4435
  for (const collection of collections) {
3974
4436
  const indexDefinitions = {
3975
- [import_datalayer2.DEFAULT_COLLECTION_SORT_KEY]: { fields: [] }
4437
+ [DEFAULT_COLLECTION_SORT_KEY]: { fields: [] }
3976
4438
  };
3977
4439
  if (collection.fields) {
3978
4440
  for (const field of collection.fields) {
@@ -3984,7 +4446,7 @@ var Database = class {
3984
4446
  {
3985
4447
  name: field.name,
3986
4448
  type: field.type,
3987
- pad: field.type === "number" ? { fillString: "0", maxLength: import_datalayer2.DEFAULT_NUMERIC_LPAD } : void 0
4449
+ pad: field.type === "number" ? { fillString: "0", maxLength: DEFAULT_NUMERIC_LPAD } : void 0
3988
4450
  }
3989
4451
  ]
3990
4452
  };
@@ -4023,43 +4485,82 @@ var Database = class {
4023
4485
  return true;
4024
4486
  };
4025
4487
  this.query = async (queryOptions, hydrator) => {
4026
- const { first, after, last, before, sort, collection, filterChain } = queryOptions;
4027
- const storeQueryOptions = {
4028
- sort,
4488
+ var _a;
4489
+ await this.initLevel();
4490
+ const {
4491
+ first,
4492
+ after,
4493
+ last,
4494
+ before,
4495
+ sort = DEFAULT_COLLECTION_SORT_KEY,
4029
4496
  collection,
4030
- filterChain
4031
- };
4497
+ filterChain: rawFilterChain
4498
+ } = queryOptions;
4499
+ let limit = 50;
4032
4500
  if (first) {
4033
- storeQueryOptions.limit = first;
4501
+ limit = first;
4034
4502
  } else if (last) {
4035
- storeQueryOptions.limit = last;
4036
- } else {
4037
- storeQueryOptions.limit = 50;
4503
+ limit = last;
4038
4504
  }
4505
+ const query = { reverse: !!last };
4039
4506
  if (after) {
4040
- storeQueryOptions.gt = (0, import_datalayer2.atob)(after);
4507
+ query.gt = atob(after);
4041
4508
  } else if (before) {
4042
- storeQueryOptions.lt = (0, import_datalayer2.atob)(before);
4509
+ query.lt = atob(before);
4043
4510
  }
4044
- if (last) {
4045
- storeQueryOptions.reverse = true;
4046
- }
4047
- const indexDefinitions = await this.getIndexDefinitions();
4048
- storeQueryOptions.indexDefinitions = indexDefinitions == null ? void 0 : indexDefinitions[queryOptions.collection];
4049
- if (!storeQueryOptions.indexDefinitions) {
4511
+ const allIndexDefinitions = await this.getIndexDefinitions(this.level);
4512
+ const indexDefinitions = allIndexDefinitions == null ? void 0 : allIndexDefinitions[queryOptions.collection];
4513
+ if (!indexDefinitions) {
4050
4514
  throw new Error(`No indexDefinitions for collection ${queryOptions.collection}`);
4051
4515
  }
4052
- const {
4053
- edges,
4054
- pageInfo: { hasPreviousPage, hasNextPage, startCursor, endCursor }
4055
- } = await this.store.query(storeQueryOptions);
4516
+ const filterChain = coerceFilterChainOperands(rawFilterChain);
4517
+ const indexDefinition = sort && (indexDefinitions == null ? void 0 : indexDefinitions[sort]);
4518
+ const filterSuffixes = indexDefinition && makeFilterSuffixes(filterChain, indexDefinition);
4519
+ const rootLevel = this.level.sublevel(CONTENT_ROOT_PREFIX, SUBLEVEL_OPTIONS);
4520
+ const sublevel = indexDefinition ? this.level.sublevel(collection, SUBLEVEL_OPTIONS).sublevel(sort, SUBLEVEL_OPTIONS) : rootLevel;
4521
+ if (!query.gt && !query.gte) {
4522
+ query.gte = (filterSuffixes == null ? void 0 : filterSuffixes.left) ? filterSuffixes.left : "";
4523
+ }
4524
+ if (!query.lt && !query.lte) {
4525
+ query.lte = (filterSuffixes == null ? void 0 : filterSuffixes.right) ? `${filterSuffixes.right}\xFF` : "\xFF";
4526
+ }
4527
+ let edges = [];
4528
+ let startKey = "";
4529
+ let endKey = "";
4530
+ let hasPreviousPage = false;
4531
+ let hasNextPage = false;
4532
+ const fieldsPattern = ((_a = indexDefinition == null ? void 0 : indexDefinition.fields) == null ? void 0 : _a.length) ? `${indexDefinition.fields.map((p) => `(?<${p.name}>.+)${INDEX_KEY_FIELD_SEPARATOR}`).join("")}` : "";
4533
+ const valuesRegex = indexDefinition ? new RegExp(`^${fieldsPattern}(?<_filepath_>.+)`) : new RegExp(`^(?<_filepath_>.+)`);
4534
+ const itemFilter = makeFilter({ filterChain });
4535
+ const iterator = sublevel.iterator(query);
4536
+ for await (const [key, value] of iterator) {
4537
+ const matcher = valuesRegex.exec(key);
4538
+ if (!matcher || indexDefinition && matcher.length !== indexDefinition.fields.length + 2) {
4539
+ continue;
4540
+ }
4541
+ const filepath = matcher.groups["_filepath_"];
4542
+ if (!itemFilter(filterSuffixes ? matcher.groups : indexDefinition ? await rootLevel.get(filepath) : value)) {
4543
+ continue;
4544
+ }
4545
+ if (limit !== -1 && edges.length >= limit) {
4546
+ if (query.reverse) {
4547
+ hasPreviousPage = true;
4548
+ } else {
4549
+ hasNextPage = true;
4550
+ }
4551
+ break;
4552
+ }
4553
+ startKey = startKey || key || "";
4554
+ endKey = key || "";
4555
+ edges = [...edges, { cursor: key, path: filepath }];
4556
+ }
4056
4557
  return {
4057
4558
  edges: await sequential(edges, async (edge) => {
4058
4559
  try {
4059
4560
  const node = await hydrator(edge.path);
4060
4561
  return {
4061
4562
  node,
4062
- cursor: (0, import_datalayer2.btoa)(edge.cursor)
4563
+ cursor: btoa(edge.cursor)
4063
4564
  };
4064
4565
  } catch (error) {
4065
4566
  if (error instanceof Error && (!edge.path.includes(".tina/__generated__/_graphql.json") || !edge.path.includes("tina/__generated__/_graphql.json"))) {
@@ -4077,8 +4578,8 @@ var Database = class {
4077
4578
  pageInfo: {
4078
4579
  hasPreviousPage,
4079
4580
  hasNextPage,
4080
- startCursor: (0, import_datalayer2.btoa)(startCursor),
4081
- endCursor: (0, import_datalayer2.btoa)(endCursor)
4581
+ startCursor: btoa(startKey),
4582
+ endCursor: btoa(endKey)
4082
4583
  }
4083
4584
  };
4084
4585
  };
@@ -4086,7 +4587,7 @@ var Database = class {
4086
4587
  graphQLSchema,
4087
4588
  tinaSchema
4088
4589
  }) => {
4089
- if (this.bridge.supportsBuilding()) {
4590
+ if (this.bridge && this.bridge.supportsBuilding()) {
4090
4591
  await this.bridge.putConfig(normalizePath(import_path3.default.join(this.getGeneratedFolder(), `_graphql.json`)), JSON.stringify(graphQLSchema));
4091
4592
  await this.bridge.putConfig(normalizePath(import_path3.default.join(this.getGeneratedFolder(), `_schema.json`)), JSON.stringify(tinaSchema.schema));
4092
4593
  }
@@ -4096,60 +4597,125 @@ var Database = class {
4096
4597
  tinaSchema,
4097
4598
  lookup: lookupFromLockFile
4098
4599
  }) => {
4600
+ if (!this.bridge) {
4601
+ throw new Error("No bridge configured");
4602
+ }
4603
+ await this.initLevel();
4099
4604
  await this.indexStatusCallbackWrapper(async () => {
4100
4605
  const lookup = lookupFromLockFile || JSON.parse(await this.bridge.get(normalizePath(import_path3.default.join(this.getGeneratedFolder(), "_lookup.json"))));
4101
- if (this.store.supportsSeeding()) {
4102
- await this.store.clear();
4103
- await this.store.seed(normalizePath(import_path3.default.join(this.getGeneratedFolder(), "_graphql.json")), graphQLSchema);
4104
- await this.store.seed(normalizePath(import_path3.default.join(this.getGeneratedFolder(), "_schema.json")), tinaSchema.schema);
4105
- await this.store.seed(normalizePath(import_path3.default.join(this.getGeneratedFolder(), "_lookup.json")), lookup);
4106
- await this._indexAllContent();
4606
+ let nextLevel;
4607
+ let nextVersion;
4608
+ if (!this.config.version) {
4609
+ await this.level.clear();
4610
+ nextLevel = this.level;
4107
4611
  } else {
4108
- if (this.store.supportsIndexing()) {
4109
- throw new Error(`Schema must be indexed with provided Store`);
4612
+ const version2 = await this.getDatabaseVersion();
4613
+ nextVersion = version2 ? `${parseInt(version2) + 1}` : "0";
4614
+ nextLevel = this.rootLevel.sublevel(nextVersion, SUBLEVEL_OPTIONS);
4615
+ }
4616
+ const contentRootLevel = nextLevel.sublevel(CONTENT_ROOT_PREFIX, SUBLEVEL_OPTIONS);
4617
+ await contentRootLevel.put(normalizePath(import_path3.default.join(this.getGeneratedFolder(), "_graphql.json")), graphQLSchema);
4618
+ await contentRootLevel.put(normalizePath(import_path3.default.join(this.getGeneratedFolder(), "_schema.json")), tinaSchema.schema);
4619
+ await contentRootLevel.put(normalizePath(import_path3.default.join(this.getGeneratedFolder(), "_lookup.json")), lookup);
4620
+ await this._indexAllContent(nextLevel);
4621
+ if (this.config.version) {
4622
+ await this.updateDatabaseVersion(nextVersion);
4623
+ if (this.level) {
4624
+ await this.level.clear();
4110
4625
  }
4626
+ this.level = nextLevel;
4111
4627
  }
4112
4628
  });
4113
4629
  };
4114
4630
  this.deleteContentByPaths = async (documentPaths) => {
4631
+ await this.initLevel();
4632
+ const operations = [];
4633
+ const enqueueOps = async (ops) => {
4634
+ operations.push(...ops);
4635
+ while (operations.length >= 25) {
4636
+ await this.level.batch(operations.splice(0, 25));
4637
+ }
4638
+ };
4115
4639
  await this.indexStatusCallbackWrapper(async () => {
4116
4640
  const { pathsByCollection, nonCollectionPaths, collections } = await this.partitionPathsByCollection(documentPaths);
4117
4641
  for (const collection of Object.keys(pathsByCollection)) {
4118
- await _deleteIndexContent(this, pathsByCollection[collection], collections[collection]);
4642
+ await _deleteIndexContent(this, pathsByCollection[collection], enqueueOps, collections[collection]);
4119
4643
  }
4120
- await _deleteIndexContent(this, nonCollectionPaths, null);
4644
+ await _deleteIndexContent(this, nonCollectionPaths, enqueueOps, null);
4121
4645
  });
4646
+ while (operations.length) {
4647
+ await this.level.batch(operations.splice(0, 25));
4648
+ }
4122
4649
  };
4123
4650
  this.indexContentByPaths = async (documentPaths) => {
4651
+ await this.initLevel();
4652
+ const operations = [];
4653
+ const enqueueOps = async (ops) => {
4654
+ operations.push(...ops);
4655
+ while (operations.length >= 25) {
4656
+ await this.level.batch(operations.splice(0, 25));
4657
+ }
4658
+ };
4124
4659
  await this.indexStatusCallbackWrapper(async () => {
4125
4660
  const { pathsByCollection, nonCollectionPaths, collections } = await this.partitionPathsByCollection(documentPaths);
4126
4661
  for (const collection of Object.keys(pathsByCollection)) {
4127
- await _indexContent(this, pathsByCollection[collection], collections[collection]);
4662
+ await _indexContent(this, this.level, pathsByCollection[collection], enqueueOps, collections[collection]);
4128
4663
  }
4129
- await _indexContent(this, nonCollectionPaths);
4664
+ await _indexContent(this, this.level, nonCollectionPaths, enqueueOps);
4130
4665
  });
4666
+ while (operations.length) {
4667
+ await this.level.batch(operations.splice(0, 25));
4668
+ }
4131
4669
  };
4132
4670
  this.delete = async (filepath) => {
4671
+ await this.initLevel();
4133
4672
  const collection = await this.collectionForPath(filepath);
4134
4673
  let collectionIndexDefinitions;
4135
4674
  if (collection) {
4136
- const indexDefinitions = await this.getIndexDefinitions();
4675
+ const indexDefinitions = await this.getIndexDefinitions(this.level);
4137
4676
  collectionIndexDefinitions = indexDefinitions == null ? void 0 : indexDefinitions[collection.name];
4138
4677
  }
4139
- await this.store.delete(normalizePath(filepath), {
4140
- collection: collection.name,
4141
- indexDefinitions: collectionIndexDefinitions
4142
- });
4143
- await this.bridge.delete(normalizePath(filepath));
4678
+ this.level.sublevel(CONTENT_ROOT_PREFIX, SUBLEVEL_OPTIONS);
4679
+ const itemKey = normalizePath(filepath);
4680
+ const rootSublevel = this.level.sublevel(CONTENT_ROOT_PREFIX, SUBLEVEL_OPTIONS);
4681
+ const item = await rootSublevel.get(itemKey);
4682
+ if (item) {
4683
+ await this.level.batch([
4684
+ ...makeIndexOpsForDocument(filepath, collection.name, collectionIndexDefinitions, item, "del", this.level),
4685
+ {
4686
+ type: "del",
4687
+ key: itemKey,
4688
+ sublevel: rootSublevel
4689
+ }
4690
+ ]);
4691
+ }
4692
+ if (this.bridge) {
4693
+ await this.bridge.delete(normalizePath(filepath));
4694
+ }
4695
+ await this.onDelete(normalizePath(filepath));
4144
4696
  };
4145
- this._indexAllContent = async () => {
4146
- const tinaSchema = await this.getSchema();
4697
+ this._indexAllContent = async (level) => {
4698
+ const tinaSchema = await this.getSchema(level);
4699
+ const operations = [];
4700
+ const enqueueOps = async (ops) => {
4701
+ operations.push(...ops);
4702
+ while (operations.length >= 25) {
4703
+ const batchOps = operations.splice(0, 25);
4704
+ await level.batch(batchOps);
4705
+ }
4706
+ };
4147
4707
  await sequential(tinaSchema.getCollections(), async (collection) => {
4148
4708
  const documentPaths = await this.bridge.glob(normalizePath(collection.path), collection.format || "md");
4149
- await _indexContent(this, documentPaths, collection);
4709
+ await _indexContent(this, level, documentPaths, enqueueOps, collection);
4150
4710
  });
4711
+ while (operations.length) {
4712
+ await level.batch(operations.splice(0, 25));
4713
+ }
4151
4714
  };
4152
4715
  this.addToLookupMap = async (lookup) => {
4716
+ if (!this.bridge) {
4717
+ throw new Error("No bridge configured");
4718
+ }
4153
4719
  const lookupPath = import_path3.default.join(this.getGeneratedFolder(), `_lookup.json`);
4154
4720
  let lookupMap;
4155
4721
  try {
@@ -4164,8 +4730,10 @@ var Database = class {
4164
4730
  };
4165
4731
  this.tinaDirectory = config.tinaDirectory || ".tina";
4166
4732
  this.bridge = config.bridge;
4167
- this.store = config.store;
4733
+ this.rootLevel = config.level;
4168
4734
  this.indexStatusCallback = config.indexStatusCallback || defaultStatusCallback;
4735
+ this.onPut = config.onPut || defaultOnPut;
4736
+ this.onDelete = config.onDelete || defaultOnDelete;
4169
4737
  }
4170
4738
  async partitionPathsByCollection(documentPaths) {
4171
4739
  const pathsByCollection = {};
@@ -4185,6 +4753,42 @@ var Database = class {
4185
4753
  }
4186
4754
  return { pathsByCollection, nonCollectionPaths, collections };
4187
4755
  }
4756
+ async updateDatabaseVersion(version2) {
4757
+ const metadataLevel = await this.rootLevel.sublevel("_metadata", SUBLEVEL_OPTIONS);
4758
+ await metadataLevel.put("metadata", { version: version2 });
4759
+ }
4760
+ async getDatabaseVersion() {
4761
+ const metadataLevel = await this.rootLevel.sublevel("_metadata", SUBLEVEL_OPTIONS);
4762
+ let version2;
4763
+ try {
4764
+ const metadata = await metadataLevel.get("metadata");
4765
+ version2 = metadata.version || version2;
4766
+ } catch (e) {
4767
+ if (e.code !== "LEVEL_NOT_FOUND") {
4768
+ throw e;
4769
+ }
4770
+ if (version2) {
4771
+ await metadataLevel.put("metadata", { version: version2 });
4772
+ }
4773
+ }
4774
+ return version2;
4775
+ }
4776
+ async initLevel() {
4777
+ if (this.level) {
4778
+ return;
4779
+ }
4780
+ if (!this.config.version) {
4781
+ this.level = this.rootLevel;
4782
+ } else {
4783
+ const version2 = await this.getDatabaseVersion();
4784
+ if (version2) {
4785
+ this.level = this.rootLevel.sublevel(version2, SUBLEVEL_OPTIONS);
4786
+ }
4787
+ }
4788
+ if (!this.level) {
4789
+ throw new import_graphql5.GraphQLError("Error initializing LevelDB instance");
4790
+ }
4791
+ }
4188
4792
  clearCache() {
4189
4793
  this.tinaSchema = null;
4190
4794
  this._lookup = null;
@@ -4203,22 +4807,14 @@ var Database = class {
4203
4807
  function hasOwnProperty(obj, prop) {
4204
4808
  return obj.hasOwnProperty(prop);
4205
4809
  }
4206
- var _indexContent = async (database, documentPaths, collection) => {
4207
- let seedOptions = void 0;
4810
+ var _indexContent = async (database, level, documentPaths, enqueueOps, collection) => {
4811
+ let collectionIndexDefinitions;
4208
4812
  if (collection) {
4209
- const indexDefinitions = await database.getIndexDefinitions();
4210
- const collectionIndexDefinitions = indexDefinitions == null ? void 0 : indexDefinitions[collection.name];
4813
+ const indexDefinitions = await database.getIndexDefinitions(level);
4814
+ collectionIndexDefinitions = indexDefinitions == null ? void 0 : indexDefinitions[collection.name];
4211
4815
  if (!collectionIndexDefinitions) {
4212
4816
  throw new Error(`No indexDefinitions for collection ${collection.name}`);
4213
4817
  }
4214
- const numIndexes = Object.keys(collectionIndexDefinitions).length;
4215
- if (numIndexes > 20) {
4216
- throw new Error(`A maximum of 20 indexes are allowed per field. Currently collection ${collection.name} has ${numIndexes} indexes. Add 'indexed: false' to exclude a field from indexing.`);
4217
- }
4218
- seedOptions = {
4219
- collection: collection.name,
4220
- indexDefinitions: collectionIndexDefinitions
4221
- };
4222
4818
  }
4223
4819
  await sequential(documentPaths, async (filepath) => {
4224
4820
  try {
@@ -4227,9 +4823,16 @@ var _indexContent = async (database, documentPaths, collection) => {
4227
4823
  frontmatterDelimiters: collection == null ? void 0 : collection.frontmatterDelimiters,
4228
4824
  frontmatterFormat: collection == null ? void 0 : collection.frontmatterFormat
4229
4825
  });
4230
- if (database.store.supportsSeeding()) {
4231
- await database.store.seed(normalizePath(filepath), data, seedOptions);
4232
- }
4826
+ const normalizedPath = normalizePath(filepath);
4827
+ await enqueueOps([
4828
+ ...makeIndexOpsForDocument(normalizedPath, collection == null ? void 0 : collection.name, collectionIndexDefinitions, data, "put", level),
4829
+ {
4830
+ type: "put",
4831
+ key: normalizedPath,
4832
+ value: data,
4833
+ sublevel: level.sublevel(CONTENT_ROOT_PREFIX, SUBLEVEL_OPTIONS)
4834
+ }
4835
+ ]);
4233
4836
  } catch (error) {
4234
4837
  throw new TinaFetchError(`Unable to seed ${filepath}`, {
4235
4838
  originalError: error,
@@ -4240,29 +4843,444 @@ var _indexContent = async (database, documentPaths, collection) => {
4240
4843
  }
4241
4844
  });
4242
4845
  };
4243
- var _deleteIndexContent = async (database, documentPaths, collection) => {
4244
- let deleteOptions = void 0;
4846
+ var _deleteIndexContent = async (database, documentPaths, enequeueOps, collection) => {
4847
+ let collectionIndexDefinitions;
4245
4848
  if (collection) {
4246
- const indexDefinitions = await database.getIndexDefinitions();
4247
- const collectionIndexDefinitions = indexDefinitions == null ? void 0 : indexDefinitions[collection.name];
4849
+ const indexDefinitions = await database.getIndexDefinitions(database.level);
4850
+ collectionIndexDefinitions = indexDefinitions == null ? void 0 : indexDefinitions[collection.name];
4248
4851
  if (!collectionIndexDefinitions) {
4249
4852
  throw new Error(`No indexDefinitions for collection ${collection.name}`);
4250
4853
  }
4251
- deleteOptions = {
4252
- collection: collection.name,
4253
- indexDefinitions: collectionIndexDefinitions
4254
- };
4255
4854
  }
4855
+ const rootLevel = database.level.sublevel(CONTENT_ROOT_PREFIX, SUBLEVEL_OPTIONS);
4256
4856
  await sequential(documentPaths, async (filepath) => {
4257
- database.store.delete(filepath, deleteOptions);
4857
+ const itemKey = normalizePath(filepath);
4858
+ try {
4859
+ const item = await rootLevel.get(itemKey);
4860
+ if (item) {
4861
+ await enequeueOps([
4862
+ ...makeIndexOpsForDocument(itemKey, collection.name, collectionIndexDefinitions, item, "del", database.level),
4863
+ { type: "del", key: itemKey, sublevel: rootLevel }
4864
+ ]);
4865
+ }
4866
+ } catch (e) {
4867
+ if (e.code !== "LEVEL_NOT_FOUND") {
4868
+ throw e;
4869
+ }
4870
+ }
4258
4871
  });
4259
4872
  };
4260
4873
 
4874
+ // src/level/tinaLevel.ts
4875
+ var import_many_level = __toModule(require("many-level"));
4876
+ var import_readable_stream = __toModule(require("readable-stream"));
4877
+ var import_net = __toModule(require("net"));
4878
+ var TinaLevelClient = class extends import_many_level.ManyLevelGuest {
4879
+ constructor() {
4880
+ super(...arguments);
4881
+ this._connected = false;
4882
+ }
4883
+ openConnection() {
4884
+ if (this._connected)
4885
+ return;
4886
+ const socket = (0, import_net.connect)(9e3);
4887
+ (0, import_readable_stream.pipeline)(socket, this.createRpcStream(), socket, () => {
4888
+ this._connected = false;
4889
+ });
4890
+ this._connected = true;
4891
+ }
4892
+ };
4893
+
4894
+ // src/database/bridge/filesystem.ts
4895
+ var import_fs_extra2 = __toModule(require("fs-extra"));
4896
+ var import_fast_glob = __toModule(require("fast-glob"));
4897
+ var import_path4 = __toModule(require("path"));
4898
+ var import_normalize_path = __toModule(require("normalize-path"));
4899
+ var FilesystemBridge = class {
4900
+ constructor(rootPath) {
4901
+ this.rootPath = rootPath || "";
4902
+ this.outputPath = rootPath || "";
4903
+ }
4904
+ addOutputPath(outputPath) {
4905
+ this.outputPath = outputPath;
4906
+ }
4907
+ async glob(pattern, extension) {
4908
+ const basePath = import_path4.default.join(this.outputPath, ...pattern.split("/"));
4909
+ const items = await (0, import_fast_glob.default)(import_path4.default.join(basePath, "**", `/*${extension}`).replace(/\\/g, "/"), {
4910
+ dot: true
4911
+ });
4912
+ const posixRootPath = (0, import_normalize_path.default)(this.outputPath);
4913
+ return items.map((item) => {
4914
+ return item.replace(posixRootPath, "").replace(/^\/|\/$/g, "");
4915
+ });
4916
+ }
4917
+ supportsBuilding() {
4918
+ return true;
4919
+ }
4920
+ async delete(filepath) {
4921
+ await import_fs_extra2.default.remove(import_path4.default.join(this.outputPath, filepath));
4922
+ }
4923
+ async get(filepath) {
4924
+ return import_fs_extra2.default.readFileSync(import_path4.default.join(this.outputPath, filepath)).toString();
4925
+ }
4926
+ async putConfig(filepath, data) {
4927
+ if (this.rootPath !== this.outputPath) {
4928
+ await this.put(filepath, data);
4929
+ await this.put(filepath, data, this.rootPath);
4930
+ } else {
4931
+ await this.put(filepath, data);
4932
+ }
4933
+ }
4934
+ async put(filepath, data, basePathOverride) {
4935
+ const basePath = basePathOverride || this.outputPath;
4936
+ await import_fs_extra2.default.outputFileSync(import_path4.default.join(basePath, filepath), data);
4937
+ }
4938
+ };
4939
+ var AuditFileSystemBridge = class extends FilesystemBridge {
4940
+ async put(filepath, data) {
4941
+ if ([
4942
+ ".tina/__generated__/_lookup.json",
4943
+ ".tina/__generated__/_schema.json",
4944
+ ".tina/__generated__/_graphql.json"
4945
+ ].includes(filepath)) {
4946
+ return super.put(filepath, data);
4947
+ }
4948
+ return;
4949
+ }
4950
+ };
4951
+
4952
+ // src/database/bridge/isomorphic.ts
4953
+ var import_isomorphic_git = __toModule(require("isomorphic-git"));
4954
+ var import_fs_extra3 = __toModule(require("fs-extra"));
4955
+ var import_glob_parent = __toModule(require("glob-parent"));
4956
+ var import_normalize_path2 = __toModule(require("normalize-path"));
4957
+ var import_graphql6 = __toModule(require("graphql"));
4958
+ var import_path5 = __toModule(require("path"));
4959
+ var flat = typeof Array.prototype.flat === "undefined" ? (entries) => entries.reduce((acc, x) => acc.concat(x), []) : (entries) => entries.flat();
4960
+ var toUint8Array = (buf) => {
4961
+ const ab = new ArrayBuffer(buf.length);
4962
+ const view = new Uint8Array(ab);
4963
+ for (let i = 0; i < buf.length; ++i) {
4964
+ view[i] = buf[i];
4965
+ }
4966
+ return view;
4967
+ };
4968
+ var IsomorphicBridge = class {
4969
+ constructor(rootPath, {
4970
+ gitRoot,
4971
+ author,
4972
+ committer,
4973
+ fsModule = import_fs_extra3.default,
4974
+ commitMessage = "Update from GraphQL client",
4975
+ ref,
4976
+ onPut,
4977
+ onDelete
4978
+ }) {
4979
+ this.cache = {};
4980
+ this.rootPath = rootPath;
4981
+ this.gitRoot = gitRoot;
4982
+ this.relativePath = rootPath.slice(this.gitRoot.length).replace(/\\/g, "/");
4983
+ if (this.relativePath.startsWith("/")) {
4984
+ this.relativePath = this.relativePath.slice(1);
4985
+ }
4986
+ this.fsModule = fsModule;
4987
+ this.author = author;
4988
+ this.committer = committer || author;
4989
+ this.isomorphicConfig = {
4990
+ dir: (0, import_normalize_path2.default)(this.gitRoot),
4991
+ fs: this.fsModule
4992
+ };
4993
+ this.ref = ref;
4994
+ this.commitMessage = commitMessage;
4995
+ this.onPut = onPut || (() => {
4996
+ });
4997
+ this.onDelete = onDelete || (() => {
4998
+ });
4999
+ }
5000
+ getAuthor() {
5001
+ return __spreadProps(__spreadValues({}, this.author), {
5002
+ timestamp: Math.round(new Date().getTime() / 1e3),
5003
+ timezoneOffset: 0
5004
+ });
5005
+ }
5006
+ getCommitter() {
5007
+ return __spreadProps(__spreadValues({}, this.committer), {
5008
+ timestamp: Math.round(new Date().getTime() / 1e3),
5009
+ timezoneOffset: 0
5010
+ });
5011
+ }
5012
+ async listEntries({
5013
+ pattern,
5014
+ entry,
5015
+ path: path6,
5016
+ results
5017
+ }) {
5018
+ const treeResult = await import_isomorphic_git.default.readTree(__spreadProps(__spreadValues({}, this.isomorphicConfig), {
5019
+ oid: entry.oid,
5020
+ cache: this.cache
5021
+ }));
5022
+ const children = [];
5023
+ for (const childEntry of treeResult.tree) {
5024
+ const childPath = path6 ? `${path6}/${childEntry.path}` : childEntry.path;
5025
+ if (childEntry.type === "tree") {
5026
+ children.push(childEntry);
5027
+ } else {
5028
+ if (childPath.startsWith(pattern)) {
5029
+ results.push(childPath);
5030
+ }
5031
+ }
5032
+ }
5033
+ for (const childEntry of children) {
5034
+ const childPath = path6 ? `${path6}/${childEntry.path}` : childEntry.path;
5035
+ await this.listEntries({
5036
+ pattern,
5037
+ entry: childEntry,
5038
+ path: childPath,
5039
+ results
5040
+ });
5041
+ }
5042
+ }
5043
+ async resolvePathEntries(path6, ref) {
5044
+ let pathParts = path6.split("/");
5045
+ const result = await import_isomorphic_git.default.walk(__spreadProps(__spreadValues({}, this.isomorphicConfig), {
5046
+ map: async (filepath, [head]) => {
5047
+ if (head._fullpath === ".") {
5048
+ return head;
5049
+ }
5050
+ if (path6.startsWith(filepath)) {
5051
+ if ((0, import_path5.dirname)(path6) === (0, import_path5.dirname)(filepath)) {
5052
+ if (path6 === filepath) {
5053
+ return head;
5054
+ }
5055
+ } else {
5056
+ return head;
5057
+ }
5058
+ }
5059
+ },
5060
+ cache: this.cache,
5061
+ trees: [import_isomorphic_git.default.TREE({ ref })]
5062
+ }));
5063
+ const pathEntries = flat(result);
5064
+ if (pathParts.indexOf(".") === -1) {
5065
+ pathParts = [".", ...pathParts];
5066
+ }
5067
+ while (pathParts.length > pathEntries.length) {
5068
+ pathEntries.push(null);
5069
+ }
5070
+ return { pathParts, pathEntries };
5071
+ }
5072
+ async updateTreeHierarchy(existingOid, updatedOid, path6, type, pathEntries, pathParts) {
5073
+ const lastIdx = pathEntries.length - 1;
5074
+ const parentEntry = pathEntries[lastIdx];
5075
+ const parentPath = pathParts[lastIdx];
5076
+ let parentOid;
5077
+ let tree;
5078
+ const mode = type === "blob" ? "100644" : "040000";
5079
+ if (parentEntry) {
5080
+ parentOid = await parentEntry.oid();
5081
+ const treeResult = await import_isomorphic_git.default.readTree(__spreadProps(__spreadValues({}, this.isomorphicConfig), {
5082
+ oid: parentOid,
5083
+ cache: this.cache
5084
+ }));
5085
+ tree = existingOid ? treeResult.tree.map((entry) => {
5086
+ if (entry.path === path6) {
5087
+ entry.oid = updatedOid;
5088
+ }
5089
+ return entry;
5090
+ }) : [
5091
+ ...treeResult.tree,
5092
+ {
5093
+ oid: updatedOid,
5094
+ type,
5095
+ path: path6,
5096
+ mode
5097
+ }
5098
+ ];
5099
+ } else {
5100
+ tree = [
5101
+ {
5102
+ oid: updatedOid,
5103
+ type,
5104
+ path: path6,
5105
+ mode
5106
+ }
5107
+ ];
5108
+ }
5109
+ const updatedParentOid = await import_isomorphic_git.default.writeTree(__spreadProps(__spreadValues({}, this.isomorphicConfig), {
5110
+ tree
5111
+ }));
5112
+ if (lastIdx === 0) {
5113
+ return updatedParentOid;
5114
+ } else {
5115
+ return await this.updateTreeHierarchy(parentOid, updatedParentOid, parentPath, "tree", pathEntries.slice(0, lastIdx), pathParts.slice(0, lastIdx));
5116
+ }
5117
+ }
5118
+ async commitTree(treeSha, ref) {
5119
+ const commitSha = await import_isomorphic_git.default.writeCommit(__spreadProps(__spreadValues({}, this.isomorphicConfig), {
5120
+ commit: {
5121
+ tree: treeSha,
5122
+ parent: [
5123
+ await import_isomorphic_git.default.resolveRef(__spreadProps(__spreadValues({}, this.isomorphicConfig), {
5124
+ ref
5125
+ }))
5126
+ ],
5127
+ message: this.commitMessage,
5128
+ author: this.getAuthor(),
5129
+ committer: this.getCommitter()
5130
+ }
5131
+ }));
5132
+ await import_isomorphic_git.default.writeRef(__spreadProps(__spreadValues({}, this.isomorphicConfig), {
5133
+ ref,
5134
+ value: commitSha,
5135
+ force: true
5136
+ }));
5137
+ }
5138
+ async getRef() {
5139
+ if (this.ref) {
5140
+ return this.ref;
5141
+ }
5142
+ const ref = await import_isomorphic_git.default.currentBranch(__spreadProps(__spreadValues({}, this.isomorphicConfig), {
5143
+ fullname: true
5144
+ }));
5145
+ if (!ref) {
5146
+ throw new import_graphql6.GraphQLError(`Unable to determine current branch from HEAD`, null, null, null, null, null, {});
5147
+ }
5148
+ this.ref = ref;
5149
+ return ref;
5150
+ }
5151
+ async glob(pattern, extension) {
5152
+ const ref = await this.getRef();
5153
+ const parent = (0, import_glob_parent.default)(this.qualifyPath(pattern));
5154
+ const { pathParts, pathEntries } = await this.resolvePathEntries(parent, ref);
5155
+ const leafEntry = pathEntries[pathEntries.length - 1];
5156
+ const entryPath = pathParts[pathParts.length - 1];
5157
+ const parentEntry = pathEntries[pathEntries.length - 2];
5158
+ let treeEntry;
5159
+ let parentPath;
5160
+ if (parentEntry) {
5161
+ const treeResult = await import_isomorphic_git.default.readTree(__spreadProps(__spreadValues({}, this.isomorphicConfig), {
5162
+ oid: await parentEntry.oid(),
5163
+ cache: this.cache
5164
+ }));
5165
+ treeEntry = treeResult.tree.find((entry) => entry.path === entryPath);
5166
+ parentPath = pathParts.slice(1, pathParts.length).join("/");
5167
+ } else {
5168
+ treeEntry = {
5169
+ type: "tree",
5170
+ oid: await leafEntry.oid()
5171
+ };
5172
+ parentPath = "";
5173
+ }
5174
+ const results = [];
5175
+ await this.listEntries({
5176
+ pattern: this.qualifyPath(pattern),
5177
+ entry: treeEntry,
5178
+ path: parentPath,
5179
+ results
5180
+ });
5181
+ return results.map((path6) => this.unqualifyPath(path6)).filter((path6) => path6.endsWith(extension));
5182
+ }
5183
+ supportsBuilding() {
5184
+ return true;
5185
+ }
5186
+ async delete(filepath) {
5187
+ const ref = await this.getRef();
5188
+ const { pathParts, pathEntries } = await this.resolvePathEntries(this.qualifyPath(filepath), ref);
5189
+ let oidToRemove;
5190
+ let ptr = pathEntries.length - 1;
5191
+ while (ptr >= 1) {
5192
+ const leafEntry = pathEntries[ptr];
5193
+ const nodePath = pathParts[ptr];
5194
+ if (leafEntry) {
5195
+ oidToRemove = oidToRemove || await leafEntry.oid();
5196
+ const parentEntry = pathEntries[ptr - 1];
5197
+ const existingOid = await parentEntry.oid();
5198
+ const treeResult = await import_isomorphic_git.default.readTree(__spreadProps(__spreadValues({}, this.isomorphicConfig), {
5199
+ oid: existingOid,
5200
+ cache: this.cache
5201
+ }));
5202
+ const updatedTree = treeResult.tree.filter((value) => value.path !== nodePath);
5203
+ if (updatedTree.length === 0) {
5204
+ ptr -= 1;
5205
+ continue;
5206
+ }
5207
+ const updatedTreeOid = await import_isomorphic_git.default.writeTree(__spreadProps(__spreadValues({}, this.isomorphicConfig), {
5208
+ tree: updatedTree
5209
+ }));
5210
+ const updatedRootTreeOid = await this.updateTreeHierarchy(existingOid, updatedTreeOid, pathParts[ptr - 1], "tree", pathEntries.slice(0, ptr - 1), pathParts.slice(0, ptr - 1));
5211
+ await this.commitTree(updatedRootTreeOid, ref);
5212
+ break;
5213
+ } else {
5214
+ throw new import_graphql6.GraphQLError(`Unable to resolve path: ${filepath}`, null, null, null, null, null, { status: 404 });
5215
+ }
5216
+ }
5217
+ if (oidToRemove) {
5218
+ await import_isomorphic_git.default.updateIndex(__spreadProps(__spreadValues({}, this.isomorphicConfig), {
5219
+ filepath: this.qualifyPath(filepath),
5220
+ force: true,
5221
+ remove: true,
5222
+ oid: oidToRemove,
5223
+ cache: this.cache
5224
+ }));
5225
+ }
5226
+ await this.onDelete(filepath);
5227
+ }
5228
+ qualifyPath(filepath) {
5229
+ return this.relativePath ? `${this.relativePath}/${filepath}` : filepath;
5230
+ }
5231
+ unqualifyPath(filepath) {
5232
+ return this.relativePath ? filepath.slice(this.relativePath.length + 1) : filepath;
5233
+ }
5234
+ async get(filepath) {
5235
+ const ref = await this.getRef();
5236
+ const oid = await import_isomorphic_git.default.resolveRef(__spreadProps(__spreadValues({}, this.isomorphicConfig), {
5237
+ ref
5238
+ }));
5239
+ const { blob } = await import_isomorphic_git.default.readBlob(__spreadProps(__spreadValues({}, this.isomorphicConfig), {
5240
+ oid,
5241
+ filepath: this.qualifyPath(filepath),
5242
+ cache: this.cache
5243
+ }));
5244
+ return Buffer.from(blob).toString("utf8");
5245
+ }
5246
+ async putConfig(filepath, data) {
5247
+ await this.put(filepath, data);
5248
+ }
5249
+ async put(filepath, data) {
5250
+ const ref = await this.getRef();
5251
+ const { pathParts, pathEntries } = await this.resolvePathEntries(this.qualifyPath(filepath), ref);
5252
+ const blobUpdate = toUint8Array(Buffer.from(data));
5253
+ let existingOid;
5254
+ const leafEntry = pathEntries[pathEntries.length - 1];
5255
+ const nodePath = pathParts[pathParts.length - 1];
5256
+ if (leafEntry) {
5257
+ existingOid = await leafEntry.oid();
5258
+ const hash = await import_isomorphic_git.default.hashBlob({ object: blobUpdate });
5259
+ if (hash.oid === existingOid) {
5260
+ await this.onPut(filepath, data);
5261
+ return;
5262
+ }
5263
+ }
5264
+ const updatedOid = await import_isomorphic_git.default.writeBlob(__spreadProps(__spreadValues({}, this.isomorphicConfig), {
5265
+ blob: blobUpdate
5266
+ }));
5267
+ const updatedRootSha = await this.updateTreeHierarchy(existingOid, updatedOid, nodePath, "blob", pathEntries.slice(0, pathEntries.length - 1), pathParts.slice(0, pathParts.length - 1));
5268
+ await this.commitTree(updatedRootSha, ref);
5269
+ await import_isomorphic_git.default.updateIndex(__spreadProps(__spreadValues({}, this.isomorphicConfig), {
5270
+ filepath: this.qualifyPath(filepath),
5271
+ add: true,
5272
+ oid: updatedOid,
5273
+ cache: this.cache
5274
+ }));
5275
+ await this.onPut(filepath, data);
5276
+ }
5277
+ };
5278
+
4261
5279
  // src/index.ts
4262
5280
  var buildSchema = async (rootPath, database, flags) => {
4263
- const tempConfig = import_path4.default.join(rootPath, ".tina", "__generated__", "config");
4264
- const config = await import_fs_extra2.default.readFileSync(import_path4.default.join(tempConfig, "schema.json")).toString();
4265
- await import_fs_extra2.default.remove(tempConfig);
5281
+ const tempConfig = import_path6.default.join(rootPath, ".tina", "__generated__", "config");
5282
+ const config = await import_fs_extra4.default.readFileSync(import_path6.default.join(tempConfig, "schema.json")).toString();
5283
+ await import_fs_extra4.default.remove(tempConfig);
4266
5284
  const { graphQLSchema, tinaSchema } = await buildDotTinaFiles({
4267
5285
  database,
4268
5286
  config: JSON.parse(config),
@@ -4272,12 +5290,16 @@ var buildSchema = async (rootPath, database, flags) => {
4272
5290
  };
4273
5291
  var getASTSchema = async (database) => {
4274
5292
  const gqlAst = await database.getGraphQLSchemaFromBridge();
4275
- return (0, import_graphql6.buildASTSchema)(gqlAst);
5293
+ return (0, import_graphql7.buildASTSchema)(gqlAst);
4276
5294
  };
4277
5295
  // Annotate the CommonJS export names for ESM import in node:
4278
5296
  0 && (module.exports = {
5297
+ AuditFileSystemBridge,
5298
+ FilesystemBridge,
5299
+ IsomorphicBridge,
4279
5300
  TinaFetchError,
4280
5301
  TinaGraphQLError,
5302
+ TinaLevelClient,
4281
5303
  TinaParseDocumentError,
4282
5304
  TinaQueryError,
4283
5305
  assertShape,