@tinacms/datalayer 0.0.1 → 0.1.1

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
@@ -51,112 +51,80 @@ var __toModule = (module2) => {
51
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);
52
52
  };
53
53
 
54
- // pnp:/Users/jeffsee/code/tinacms/packages/@tinacms/datalayer/src/index.ts
54
+ // pnp:/home/runner/work/tinacms/tinacms/packages/@tinacms/datalayer/src/index.ts
55
55
  __export(exports, {
56
+ AuditFileSystemBridge: () => AuditFileSystemBridge,
57
+ AuditFilesystemStore: () => AuditFilesystemStore,
58
+ DEFAULT_COLLECTION_SORT_KEY: () => DEFAULT_COLLECTION_SORT_KEY,
59
+ DEFAULT_NUMERIC_LPAD: () => DEFAULT_NUMERIC_LPAD,
56
60
  FilesystemBridge: () => FilesystemBridge,
57
61
  FilesystemStore: () => FilesystemStore,
58
- GithubBridge: () => GithubBridge,
59
- GithubStore: () => GithubStore,
62
+ INDEX_KEY_FIELD_SEPARATOR: () => INDEX_KEY_FIELD_SEPARATOR,
60
63
  LevelStore: () => LevelStore,
61
- MemoryStore: () => MemoryStore
64
+ OP: () => OP,
65
+ atob: () => atob,
66
+ btoa: () => btoa,
67
+ coerceFilterChainOperands: () => coerceFilterChainOperands,
68
+ makeFilter: () => makeFilter,
69
+ makeFilterChain: () => makeFilterChain,
70
+ makeFilterSuffixes: () => makeFilterSuffixes,
71
+ makeKeyForField: () => makeKeyForField,
72
+ makeStringEscaper: () => makeStringEscaper
62
73
  });
63
74
 
64
- // pnp:/Users/jeffsee/code/tinacms/packages/@tinacms/datalayer/src/database/bridge/github.ts
65
- var import_lodash = __toModule(require("lodash"));
75
+ // pnp:/home/runner/work/tinacms/tinacms/packages/@tinacms/datalayer/src/database/bridge/filesystem.ts
76
+ var import_fs_extra = __toModule(require("fs-extra"));
77
+ var import_fast_glob = __toModule(require("fast-glob"));
66
78
  var import_path = __toModule(require("path"));
67
- var import_rest = __toModule(require("@octokit/rest"));
68
- var import_graphql = __toModule(require("graphql"));
69
- var GithubBridge = class {
70
- constructor({ rootPath, accessToken, owner, repo, ref }) {
71
- this.rootPath = rootPath;
72
- this.repoConfig = {
73
- owner,
74
- repo,
75
- ref
76
- };
77
- this.appOctoKit = new import_rest.Octokit({
78
- auth: accessToken
79
- });
79
+ var import_normalize_path = __toModule(require("normalize-path"));
80
+ var FilesystemBridge = class {
81
+ constructor(rootPath) {
82
+ this.rootPath = rootPath || "";
80
83
  }
81
- async readDir(filepath) {
82
- const fullPath = import_path.default.join(this.rootPath, filepath);
83
- const repos = await this.appOctoKit.repos.getContent(__spreadProps(__spreadValues({}, this.repoConfig), {
84
- path: fullPath
85
- })).then(async (response) => {
86
- if (Array.isArray(response.data)) {
87
- return await Promise.all(await response.data.map(async (d) => {
88
- if (d.type === "dir") {
89
- const nestedItems = await this.readDir(d.path);
90
- if (Array.isArray(nestedItems)) {
91
- return nestedItems.map((nestedItem) => {
92
- return import_path.default.join(d.path, nestedItem);
93
- });
94
- } else {
95
- throw new Error(`Expected items to be an array of strings for readDir at ${d.path}`);
96
- }
97
- }
98
- return d.path;
99
- }));
100
- }
101
- throw new Error(`Expected to return an array from Github directory ${import_path.default}`);
84
+ async glob(pattern) {
85
+ const basePath = import_path.default.join(this.rootPath, ...pattern.split("/"));
86
+ const items = await (0, import_fast_glob.default)(import_path.default.join(basePath, "**", "/*").replace(/\\/g, "/"), {
87
+ dot: true
88
+ });
89
+ const posixRootPath = (0, import_normalize_path.default)(this.rootPath);
90
+ return items.map((item) => {
91
+ return item.replace(posixRootPath, "").replace(/^\/|\/$/g, "");
102
92
  });
103
- return import_lodash.default.flatten(repos);
104
93
  }
105
94
  supportsBuilding() {
106
- return false;
95
+ return true;
107
96
  }
108
- async glob(pattern) {
109
- const results = await this.readDir(pattern);
110
- return results.map((item) => item.replace(this.rootPath, "").replace(/^\/|\/$/g, ""));
97
+ async delete(filepath) {
98
+ await import_fs_extra.default.remove(import_path.default.join(this.rootPath, filepath));
111
99
  }
112
100
  async get(filepath) {
113
- const realpath = import_path.default.join(this.rootPath, filepath);
114
- return this.appOctoKit.repos.getContent(__spreadProps(__spreadValues({}, this.repoConfig), {
115
- path: realpath
116
- })).then((response) => {
117
- return Buffer.from(response.data.content, "base64").toString();
118
- }).catch((e) => {
119
- if (e.status === 401) {
120
- throw new import_graphql.GraphQLError(`Unauthorized request to Github Repository: '${this.repoConfig.owner}/${this.repoConfig.repo}', please ensure your access token is valid.`, null, null, null, null, e, { status: e.status });
121
- }
122
- throw new import_graphql.GraphQLError(`Unable to find record '${filepath}' in Github Repository: '${this.repoConfig.owner}/${this.repoConfig.repo}', Ref: '${this.repoConfig.ref}'`, null, null, null, null, e, { status: e.status });
123
- });
101
+ return import_fs_extra.default.readFileSync(import_path.default.join(this.rootPath, filepath)).toString();
124
102
  }
125
103
  async putConfig(filepath, data) {
126
- throw new Error(`Config files cannot be changed by the Github bridge`);
104
+ await this.put(filepath, data);
127
105
  }
128
106
  async put(filepath, data) {
129
- const realpath = import_path.default.join(this.rootPath, filepath);
130
- let fileSha = void 0;
131
- try {
132
- const fileContent = await this.appOctoKit.repos.getContent(__spreadProps(__spreadValues({}, this.repoConfig), {
133
- path: realpath
134
- }));
135
- fileSha = fileContent.data.sha;
136
- } catch (e) {
137
- console.log("No file exists, creating new one");
138
- }
139
- await this.appOctoKit.repos.createOrUpdateFileContents(__spreadProps(__spreadValues({}, this.repoConfig), {
140
- branch: this.repoConfig.ref,
141
- path: realpath,
142
- message: "Update from GraphQL client",
143
- content: new Buffer(data).toString("base64"),
144
- sha: fileSha
145
- }));
107
+ await import_fs_extra.default.outputFileSync(import_path.default.join(this.rootPath, filepath), data);
108
+ }
109
+ };
110
+ var AuditFileSystemBridge = class extends FilesystemBridge {
111
+ async put(_filepath, _data) {
112
+ return;
146
113
  }
147
114
  };
148
115
 
149
- // pnp:/Users/jeffsee/code/tinacms/packages/@tinacms/datalayer/src/database/store/github.ts
150
- var import_lodash2 = __toModule(require("lodash"));
116
+ // pnp:/home/runner/work/tinacms/tinacms/packages/@tinacms/datalayer/src/database/store/filesystem.ts
117
+ var import_fs_extra2 = __toModule(require("fs-extra"));
118
+ var import_fast_glob2 = __toModule(require("fast-glob"));
151
119
  var import_path2 = __toModule(require("path"));
152
- var import_rest2 = __toModule(require("@octokit/rest"));
120
+ var import_normalize_path2 = __toModule(require("normalize-path"));
153
121
 
154
- // pnp:/Users/jeffsee/code/tinacms/packages/@tinacms/datalayer/src/database/util.ts
122
+ // pnp:/home/runner/work/tinacms/tinacms/packages/@tinacms/datalayer/src/database/util.ts
155
123
  var import_gray_matter = __toModule(require("gray-matter"));
156
124
 
157
- // pnp:/Users/jeffsee/code/tinacms/packages/@tinacms/datalayer/src/util.ts
125
+ // pnp:/home/runner/work/tinacms/tinacms/packages/@tinacms/datalayer/src/util.ts
158
126
  var yup = __toModule(require("yup"));
159
- var import_graphql2 = __toModule(require("graphql"));
127
+ var import_graphql = __toModule(require("graphql"));
160
128
  var sequential = async (items, callback) => {
161
129
  const accum = [];
162
130
  if (!items) {
@@ -181,13 +149,19 @@ function assertShape(value, yupSchema, errorMessage) {
181
149
  shape.validateSync(value);
182
150
  } catch (e) {
183
151
  const message = errorMessage || `Failed to assertShape - ${e.message}`;
184
- throw new import_graphql2.GraphQLError(message, null, null, null, null, null, {
152
+ throw new import_graphql.GraphQLError(message, null, null, null, null, null, {
185
153
  stack: e.stack
186
154
  });
187
155
  }
188
156
  }
157
+ var atob = (b64Encoded) => {
158
+ return Buffer.from(b64Encoded, "base64").toString();
159
+ };
160
+ var btoa = (string) => {
161
+ return Buffer.from(string).toString("base64");
162
+ };
189
163
 
190
- // pnp:/Users/jeffsee/code/tinacms/packages/@tinacms/datalayer/src/database/util.ts
164
+ // pnp:/home/runner/work/tinacms/tinacms/packages/@tinacms/datalayer/src/database/util.ts
191
165
  var stringifyFile = (content, format, keepTemplateKey) => {
192
166
  switch (format) {
193
167
  case ".markdown":
@@ -242,144 +216,7 @@ var parseFile = (content, format, yupSchema) => {
242
216
  }
243
217
  };
244
218
 
245
- // pnp:/Users/jeffsee/code/tinacms/packages/@tinacms/datalayer/src/database/store/github.ts
246
- var import_graphql3 = __toModule(require("graphql"));
247
- var GithubStore = class {
248
- async clear() {
249
- }
250
- async print() {
251
- }
252
- constructor({ rootPath, accessToken, owner, repo, ref }) {
253
- this.rootPath = rootPath;
254
- this.repoConfig = {
255
- owner,
256
- repo,
257
- ref
258
- };
259
- this.appOctoKit = new import_rest2.Octokit({
260
- auth: accessToken
261
- });
262
- }
263
- async query(queryStrings) {
264
- throw new Error(`Unable to perform query for GithubStore`);
265
- }
266
- supportsSeeding() {
267
- return false;
268
- }
269
- async seed() {
270
- throw new Error(`Seeding data is not possible for Github data store`);
271
- }
272
- async readDir(filepath) {
273
- const fullPath = import_path2.default.join(this.rootPath, filepath);
274
- const repos = await this.appOctoKit.repos.getContent(__spreadProps(__spreadValues({}, this.repoConfig), {
275
- path: fullPath
276
- })).then(async (response) => {
277
- if (Array.isArray(response.data)) {
278
- return await Promise.all(await response.data.map(async (d) => {
279
- if (d.type === "dir") {
280
- const nestedItems = await this.readDir(d.path);
281
- if (Array.isArray(nestedItems)) {
282
- return nestedItems.map((nestedItem) => {
283
- return import_path2.default.join(d.path, nestedItem);
284
- });
285
- } else {
286
- throw new Error(`Expected items to be an array of strings for readDir at ${d.path}`);
287
- }
288
- }
289
- return d.path;
290
- }));
291
- }
292
- throw new Error(`Expected to return an array from Github directory ${import_path2.default}`);
293
- });
294
- return import_lodash2.default.flatten(repos);
295
- }
296
- async glob(pattern, callback) {
297
- const results = await this.readDir(pattern);
298
- const items = results.map((item) => item.replace(this.rootPath, "").replace(/^\/|\/$/g, ""));
299
- if (callback) {
300
- return sequential(items, async (item) => {
301
- return callback(item);
302
- });
303
- } else {
304
- return items;
305
- }
306
- }
307
- async get(filepath) {
308
- const realpath = import_path2.default.join(this.rootPath, filepath);
309
- return this.appOctoKit.repos.getContent(__spreadProps(__spreadValues({}, this.repoConfig), {
310
- path: realpath
311
- })).then((response) => {
312
- const responseString = Buffer.from(response.data.content, "base64").toString();
313
- return parseFile(responseString, import_path2.default.extname(filepath), (yup2) => yup2.object());
314
- }).catch((e) => {
315
- if (e.status === 401) {
316
- throw new import_graphql3.GraphQLError(`Unauthorized request to Github Repository: '${this.repoConfig.owner}/${this.repoConfig.repo}', please ensure your access token is valid.`, null, null, null, null, e, { status: e.status });
317
- }
318
- throw new import_graphql3.GraphQLError(`Unable to find record '${filepath}' in Github Repository: '${this.repoConfig.owner}/${this.repoConfig.repo}', Ref: '${this.repoConfig.ref}'`, null, null, null, null, e, { status: e.status });
319
- });
320
- }
321
- supportsIndexing() {
322
- return false;
323
- }
324
- async put(filepath, data) {
325
- const realpath = import_path2.default.join(this.rootPath, filepath);
326
- let fileSha = void 0;
327
- try {
328
- const fileContent = await this.appOctoKit.repos.getContent(__spreadProps(__spreadValues({}, this.repoConfig), {
329
- path: realpath
330
- }));
331
- fileSha = fileContent.data.sha;
332
- } catch (e) {
333
- console.log("No file exists, creating new one");
334
- }
335
- await this.appOctoKit.repos.createOrUpdateFileContents(__spreadProps(__spreadValues({}, this.repoConfig), {
336
- branch: this.repoConfig.ref,
337
- path: realpath,
338
- message: "Update from GraphQL client",
339
- content: new Buffer(stringifyFile(data, import_path2.default.extname(filepath), false)).toString("base64"),
340
- sha: fileSha
341
- }));
342
- }
343
- };
344
-
345
- // pnp:/Users/jeffsee/code/tinacms/packages/@tinacms/datalayer/src/database/bridge/filesystem.ts
346
- var import_fs_extra = __toModule(require("fs-extra"));
347
- var import_fast_glob = __toModule(require("fast-glob"));
348
- var import_path3 = __toModule(require("path"));
349
- var import_normalize_path = __toModule(require("normalize-path"));
350
- var FilesystemBridge = class {
351
- constructor(rootPath) {
352
- this.rootPath = rootPath || "";
353
- }
354
- async glob(pattern) {
355
- const basePath = import_path3.default.join(this.rootPath, ...pattern.split("/"));
356
- const items = await (0, import_fast_glob.default)(import_path3.default.join(basePath, "**", "/*").replace(/\\/g, "/"), {
357
- dot: true
358
- });
359
- const posixRootPath = (0, import_normalize_path.default)(this.rootPath);
360
- return items.map((item) => {
361
- return item.replace(posixRootPath, "").replace(/^\/|\/$/g, "");
362
- });
363
- }
364
- supportsBuilding() {
365
- return true;
366
- }
367
- async get(filepath) {
368
- return import_fs_extra.default.readFileSync(import_path3.default.join(this.rootPath, filepath)).toString();
369
- }
370
- async putConfig(filepath, data) {
371
- await this.put(filepath, data);
372
- }
373
- async put(filepath, data) {
374
- await import_fs_extra.default.outputFileSync(import_path3.default.join(this.rootPath, filepath), data);
375
- }
376
- };
377
-
378
- // pnp:/Users/jeffsee/code/tinacms/packages/@tinacms/datalayer/src/database/store/filesystem.ts
379
- var import_fs_extra2 = __toModule(require("fs-extra"));
380
- var import_fast_glob2 = __toModule(require("fast-glob"));
381
- var import_path4 = __toModule(require("path"));
382
- var import_normalize_path2 = __toModule(require("normalize-path"));
219
+ // pnp:/home/runner/work/tinacms/tinacms/packages/@tinacms/datalayer/src/database/store/filesystem.ts
383
220
  var FilesystemStore = class {
384
221
  async clear() {
385
222
  }
@@ -388,14 +225,14 @@ var FilesystemStore = class {
388
225
  constructor({ rootPath }) {
389
226
  this.rootPath = rootPath || "";
390
227
  }
391
- async query(queryStrings) {
228
+ async query(queryOptions) {
392
229
  throw new Error(`Unable to perform query for Filesystem store`);
393
230
  }
394
231
  async seed() {
395
232
  throw new Error(`Seeding data is not possible for Filesystem store`);
396
233
  }
397
234
  async get(filepath) {
398
- return parseFile(await import_fs_extra2.default.readFileSync(import_path4.default.join(this.rootPath, filepath)).toString(), import_path4.default.extname(filepath), (yup2) => yup2.object());
235
+ return parseFile(await import_fs_extra2.default.readFileSync(import_path2.default.join(this.rootPath, filepath)).toString(), import_path2.default.extname(filepath), (yup2) => yup2.object());
399
236
  }
400
237
  supportsSeeding() {
401
238
  return false;
@@ -404,8 +241,8 @@ var FilesystemStore = class {
404
241
  return false;
405
242
  }
406
243
  async glob(pattern, callback) {
407
- const basePath = import_path4.default.join(this.rootPath, ...pattern.split("/"));
408
- const itemsRaw = await (0, import_fast_glob2.default)(import_path4.default.join(basePath, "**", "/*").replace(/\\/g, "/"), {
244
+ const basePath = import_path2.default.join(this.rootPath, ...pattern.split("/"));
245
+ const itemsRaw = await (0, import_fast_glob2.default)(import_path2.default.join(basePath, "**", "/*").replace(/\\/g, "/"), {
409
246
  dot: true
410
247
  });
411
248
  const posixRootPath = (0, import_normalize_path2.default)(this.rootPath);
@@ -420,125 +257,454 @@ var FilesystemStore = class {
420
257
  return items;
421
258
  }
422
259
  }
423
- async put(filepath, data) {
424
- await import_fs_extra2.default.outputFileSync(import_path4.default.join(this.rootPath, filepath), stringifyFile(data, import_path4.default.extname(filepath), false));
425
- }
426
- };
427
-
428
- // pnp:/Users/jeffsee/code/tinacms/packages/@tinacms/datalayer/src/database/store/memory.ts
429
- var MemoryStore = class {
430
- constructor(rootPath, object = {}) {
431
- this.map = object;
432
- this.rootPath = rootPath || "";
433
- this.db = {
434
- get: async (filepath) => {
435
- return this.map[filepath];
436
- },
437
- put: async (filepath, content) => {
438
- this.map[filepath] = content;
439
- await this.print();
440
- }
441
- };
260
+ async put(filepath, data, options) {
261
+ await import_fs_extra2.default.outputFileSync(import_path2.default.join(this.rootPath, filepath), stringifyFile(data, import_path2.default.extname(filepath), options.keepTemplateKey));
442
262
  }
443
- async query(queryStrings, hydrator) {
444
- const resultSets = await sequential(queryStrings, async (queryString) => {
445
- const res = await this.get(queryString);
446
- return res || [];
447
- });
448
- let items = [];
449
- if (resultSets.length > 0) {
450
- items = resultSets.reduce((p, c) => p.filter((e) => c.includes(e)));
451
- }
452
- return sequential(items, async (documentString) => {
453
- return hydrator(documentString);
454
- });
263
+ async open() {
455
264
  }
456
- async seed(filepath, data) {
457
- await this.put(filepath, data);
265
+ async close() {
458
266
  }
459
- supportsSeeding() {
460
- return true;
267
+ async delete(filepath) {
268
+ await import_fs_extra2.default.remove(import_path2.default.join(this.rootPath, filepath));
461
269
  }
462
- supportsIndexing() {
463
- return true;
270
+ };
271
+ var AuditFilesystemStore = class extends FilesystemStore {
272
+ async put(_filepath, _data) {
273
+ return;
464
274
  }
465
- async print() {
275
+ };
276
+
277
+ // pnp:/home/runner/work/tinacms/tinacms/packages/@tinacms/datalayer/src/database/store/index.ts
278
+ var import_jsonpath_plus = __toModule(require("jsonpath-plus"));
279
+ var DEFAULT_COLLECTION_SORT_KEY = "__filepath__";
280
+ var INDEX_KEY_FIELD_SEPARATOR = "#";
281
+ var DEFAULT_NUMERIC_LPAD = 4;
282
+ var OP;
283
+ (function(OP2) {
284
+ OP2["EQ"] = "eq";
285
+ OP2["GT"] = "gt";
286
+ OP2["LT"] = "lt";
287
+ OP2["GTE"] = "gte";
288
+ OP2["LTE"] = "lte";
289
+ OP2["STARTS_WITH"] = "startsWith";
290
+ OP2["IN"] = "in";
291
+ })(OP || (OP = {}));
292
+ var inferOperatorFromFilter = (filterOperator) => {
293
+ switch (filterOperator) {
294
+ case "after":
295
+ return OP.GT;
296
+ case "before":
297
+ return OP.LT;
298
+ case "eq":
299
+ return OP.EQ;
300
+ case "startsWith":
301
+ return OP.STARTS_WITH;
302
+ case "lt":
303
+ return OP.LT;
304
+ case "lte":
305
+ return OP.LTE;
306
+ case "gt":
307
+ return OP.GT;
308
+ case "gte":
309
+ return OP.GTE;
310
+ case "in":
311
+ return OP.IN;
312
+ default:
313
+ throw new Error(`unsupported filter condition: '${filterOperator}'`);
466
314
  }
467
- async clear() {
468
- this.map = {};
315
+ };
316
+ var getFilterOperator = (expression, operand) => {
317
+ return (expression[operand] || expression[operand] === 0) && operand;
318
+ };
319
+ var makeFilterChain = ({
320
+ conditions
321
+ }) => {
322
+ const filterChain = [];
323
+ if (!conditions) {
324
+ return filterChain;
325
+ }
326
+ for (const condition of conditions) {
327
+ const { filterPath, filterExpression } = condition;
328
+ const _a = filterExpression, { _type } = _a, keys = __objRest(_a, ["_type"]);
329
+ const [key1, key2, ...extraKeys] = Object.keys(keys);
330
+ if (extraKeys.length) {
331
+ throw new Error(`Unexpected keys: [${extraKeys.join(",")}] in filter expression`);
332
+ }
333
+ if (key1 && !key2) {
334
+ filterChain.push({
335
+ pathExpression: filterPath,
336
+ rightOperand: filterExpression[key1],
337
+ operator: inferOperatorFromFilter(key1),
338
+ type: _type,
339
+ pad: _type === "number" ? { fillString: "0", maxLength: DEFAULT_NUMERIC_LPAD } : void 0
340
+ });
341
+ } else if (key1 && key2) {
342
+ const leftFilterOperator = getFilterOperator(filterExpression, "gt") || getFilterOperator(filterExpression, "gte") || getFilterOperator(filterExpression, "after") || void 0;
343
+ const rightFilterOperator = getFilterOperator(filterExpression, "lt") || getFilterOperator(filterExpression, "lte") || getFilterOperator(filterExpression, "before") || void 0;
344
+ let leftOperand;
345
+ let rightOperand;
346
+ if (rightFilterOperator && leftFilterOperator) {
347
+ if (key1 === leftFilterOperator) {
348
+ leftOperand = filterExpression[key1];
349
+ rightOperand = filterExpression[key2];
350
+ } else {
351
+ rightOperand = filterExpression[key1];
352
+ leftOperand = filterExpression[key2];
353
+ }
354
+ filterChain.push({
355
+ pathExpression: filterPath,
356
+ rightOperand,
357
+ leftOperand,
358
+ leftOperator: inferOperatorFromFilter(leftFilterOperator),
359
+ rightOperator: inferOperatorFromFilter(rightFilterOperator),
360
+ type: _type,
361
+ pad: _type === "number" ? { fillString: "0", maxLength: DEFAULT_NUMERIC_LPAD } : void 0
362
+ });
363
+ } else {
364
+ throw new Error(`Filter on field '${filterPath}' has invalid combination of conditions: '${key1}, ${key2}'`);
365
+ }
366
+ }
469
367
  }
470
- async glob(pattern, callback) {
471
- const strings = Object.keys(this.map).filter((key) => {
472
- if (key.startsWith(pattern)) {
473
- return true;
368
+ return filterChain;
369
+ };
370
+ var makeFilter = ({
371
+ filterChain
372
+ }) => {
373
+ return (values) => {
374
+ for (const filter of filterChain) {
375
+ const dataType = filter.type;
376
+ const resolvedValues = (0, import_jsonpath_plus.JSONPath)({
377
+ path: filter.pathExpression,
378
+ json: values
379
+ });
380
+ if (!resolvedValues || !resolvedValues.length) {
381
+ return false;
382
+ }
383
+ let operands;
384
+ if (dataType === "string" || dataType === "reference") {
385
+ operands = resolvedValues;
386
+ } else if (dataType === "number") {
387
+ operands = resolvedValues.map((resolvedValue) => Number(resolvedValue));
388
+ } else if (dataType === "datetime") {
389
+ operands = resolvedValues.map((resolvedValue) => {
390
+ const coerced = new Date(resolvedValue).getTime();
391
+ return isNaN(coerced) ? Number(resolvedValue) : coerced;
392
+ });
393
+ } else if (dataType === "boolean") {
394
+ operands = resolvedValues.map((resolvedValue) => typeof resolvedValue === "boolean" && resolvedValue || resolvedValue === "true" || resolvedValue === "1");
474
395
  } else {
396
+ throw new Error(`Unexpected datatype ${dataType}`);
397
+ }
398
+ const { operator } = filter;
399
+ let matches = false;
400
+ if (operator) {
401
+ switch (operator) {
402
+ case OP.EQ:
403
+ if (operands.findIndex((operand) => operand === filter.rightOperand) >= 0) {
404
+ matches = true;
405
+ }
406
+ break;
407
+ case OP.GT:
408
+ for (const operand of operands) {
409
+ if (operand > filter.rightOperand) {
410
+ matches = true;
411
+ break;
412
+ }
413
+ }
414
+ break;
415
+ case OP.LT:
416
+ for (const operand of operands) {
417
+ if (operand < filter.rightOperand) {
418
+ matches = true;
419
+ break;
420
+ }
421
+ }
422
+ break;
423
+ case OP.GTE:
424
+ for (const operand of operands) {
425
+ if (operand >= filter.rightOperand) {
426
+ matches = true;
427
+ break;
428
+ }
429
+ }
430
+ break;
431
+ case OP.LTE:
432
+ for (const operand of operands) {
433
+ if (operand <= filter.rightOperand) {
434
+ matches = true;
435
+ break;
436
+ }
437
+ }
438
+ break;
439
+ case OP.IN:
440
+ for (const operand of operands) {
441
+ if (filter.rightOperand.indexOf(operand) >= 0) {
442
+ matches = true;
443
+ break;
444
+ }
445
+ }
446
+ break;
447
+ case OP.STARTS_WITH:
448
+ for (const operand of operands) {
449
+ if (operand.startsWith(filter.rightOperand)) {
450
+ matches = true;
451
+ break;
452
+ }
453
+ }
454
+ break;
455
+ default:
456
+ throw new Error(`unexpected operator ${operator}`);
457
+ }
458
+ } else {
459
+ const { rightOperator, leftOperator, rightOperand, leftOperand } = filter;
460
+ for (const operand of operands) {
461
+ let rightMatches = false;
462
+ let leftMatches = false;
463
+ if (rightOperator === OP.LTE && operand <= rightOperand) {
464
+ rightMatches = true;
465
+ } else if (rightOperator === OP.LT && operand < rightOperand) {
466
+ rightMatches = true;
467
+ }
468
+ if (leftOperator === OP.GTE && operand >= leftOperand) {
469
+ leftMatches = true;
470
+ } else if (leftOperator === OP.GT && operand > leftOperand) {
471
+ leftMatches = true;
472
+ }
473
+ if (rightMatches && leftMatches) {
474
+ matches = true;
475
+ break;
476
+ }
477
+ }
478
+ }
479
+ if (!matches) {
475
480
  return false;
476
481
  }
477
- });
478
- if (callback) {
479
- return sequential(strings, async (item) => {
480
- return callback(item);
481
- });
482
+ }
483
+ return true;
484
+ };
485
+ };
486
+ var makeStringEscaper = (regex, replacement) => {
487
+ return (input) => {
488
+ if (Array.isArray(input)) {
489
+ return input.map((val) => val.replace(regex, replacement));
482
490
  } else {
483
- return strings;
491
+ return input.replace(regex, replacement);
492
+ }
493
+ };
494
+ };
495
+ var applyPadding = (input, pad) => {
496
+ if (pad) {
497
+ if (Array.isArray(input)) {
498
+ return input.map((val) => String(val).padStart(pad.maxLength, pad.fillString));
499
+ } else {
500
+ return String(input).padStart(pad.maxLength, pad.fillString);
484
501
  }
485
502
  }
486
- async get(filepath) {
487
- const content = await this.db.get(filepath);
488
- return content;
503
+ return input;
504
+ };
505
+ var coerceFilterChainOperands = (filterChain, stringEscaper) => {
506
+ const result = [];
507
+ if (filterChain.length) {
508
+ for (const filter of filterChain) {
509
+ const dataType = filter.type;
510
+ if (dataType === "datetime") {
511
+ if (filter.leftOperand !== void 0) {
512
+ result.push(__spreadProps(__spreadValues({}, filter), {
513
+ rightOperand: new Date(filter.rightOperand).getTime(),
514
+ leftOperand: new Date(filter.leftOperand).getTime()
515
+ }));
516
+ } else {
517
+ if (Array.isArray(filter.rightOperand)) {
518
+ result.push(__spreadProps(__spreadValues({}, filter), {
519
+ rightOperand: filter.rightOperand.map((operand) => new Date(operand).getTime())
520
+ }));
521
+ } else {
522
+ result.push(__spreadProps(__spreadValues({}, filter), {
523
+ rightOperand: new Date(filter.rightOperand).getTime()
524
+ }));
525
+ }
526
+ }
527
+ } else if (dataType === "string") {
528
+ if (filter.leftOperand !== void 0) {
529
+ result.push(__spreadProps(__spreadValues({}, filter), {
530
+ rightOperand: applyPadding(stringEscaper(filter.rightOperand), filter.pad),
531
+ leftOperand: applyPadding(stringEscaper(filter.leftOperand), filter.pad)
532
+ }));
533
+ } else {
534
+ result.push(__spreadProps(__spreadValues({}, filter), {
535
+ rightOperand: applyPadding(stringEscaper(filter.rightOperand), filter.pad)
536
+ }));
537
+ }
538
+ } else {
539
+ result.push(__spreadValues({}, filter));
540
+ }
541
+ }
489
542
  }
490
- async put(filepath, data) {
491
- await this.db.put(filepath, data);
543
+ return result;
544
+ };
545
+ var makeFilterSuffixes = (filterChain, index) => {
546
+ if (filterChain && filterChain.length) {
547
+ const indexFields = index.fields.map((field) => field.name);
548
+ const orderedFilterChain = [];
549
+ for (const filter of filterChain) {
550
+ const idx = indexFields.indexOf(filter.pathExpression);
551
+ if (idx === -1) {
552
+ return;
553
+ }
554
+ if (filter.operator && filter.operator === OP.IN) {
555
+ return;
556
+ }
557
+ orderedFilterChain[idx] = filter;
558
+ }
559
+ const baseFragments = [];
560
+ let rightSuffix;
561
+ let leftSuffix;
562
+ let ternaryFilter = false;
563
+ if (orderedFilterChain[filterChain.length - 1] && !orderedFilterChain[filterChain.length - 1].operator) {
564
+ ternaryFilter = true;
565
+ }
566
+ for (let i = 0; i < orderedFilterChain.length; i++) {
567
+ const filter = orderedFilterChain[i];
568
+ if (!filter) {
569
+ return;
570
+ }
571
+ if (Number(i) < indexFields.length - 1) {
572
+ if (!filter.operator) {
573
+ return;
574
+ }
575
+ const binaryFilter = filter;
576
+ if (binaryFilter.operator !== OP.EQ) {
577
+ return;
578
+ }
579
+ baseFragments.push(applyPadding(orderedFilterChain[i].rightOperand, orderedFilterChain[i].pad));
580
+ } else {
581
+ if (ternaryFilter) {
582
+ leftSuffix = applyPadding(orderedFilterChain[i].leftOperand, orderedFilterChain[i].pad);
583
+ rightSuffix = applyPadding(orderedFilterChain[i].rightOperand, orderedFilterChain[i].pad);
584
+ } else {
585
+ const op = orderedFilterChain[i].operator;
586
+ const operand = applyPadding(orderedFilterChain[i].rightOperand, orderedFilterChain[i].pad);
587
+ if (op === OP.LT || op === OP.LTE) {
588
+ rightSuffix = operand;
589
+ } else if (op === OP.GT || op === OP.GTE) {
590
+ leftSuffix = operand;
591
+ } else {
592
+ rightSuffix = operand;
593
+ leftSuffix = operand;
594
+ }
595
+ }
596
+ }
597
+ }
598
+ return {
599
+ left: leftSuffix && [...baseFragments, leftSuffix].join(INDEX_KEY_FIELD_SEPARATOR) || void 0,
600
+ right: rightSuffix && [...baseFragments, rightSuffix].join(INDEX_KEY_FIELD_SEPARATOR) || void 0
601
+ };
602
+ } else {
603
+ return {};
604
+ }
605
+ };
606
+ var makeKeyForField = (definition, data, stringEscaper) => {
607
+ const valueParts = [];
608
+ for (const field of definition.fields) {
609
+ if (field.name in data) {
610
+ const resolvedValue = String(field.type === "datetime" ? new Date(data[field.name]).getTime() : field.type === "string" ? stringEscaper(data[field.name]) : data[field.name]);
611
+ valueParts.push(applyPadding(resolvedValue, field.pad));
612
+ } else {
613
+ return null;
614
+ }
492
615
  }
616
+ return valueParts.join(INDEX_KEY_FIELD_SEPARATOR);
493
617
  };
494
618
 
495
- // pnp:/Users/jeffsee/code/tinacms/packages/@tinacms/datalayer/src/database/store/level.ts
496
- var import_path5 = __toModule(require("path"));
619
+ // pnp:/home/runner/work/tinacms/tinacms/packages/@tinacms/datalayer/src/database/store/level.ts
620
+ var import_path3 = __toModule(require("path"));
497
621
  var import_level = __toModule(require("level"));
498
622
  var import_levelup = __toModule(require("levelup"));
499
623
  var import_memdown = __toModule(require("memdown"));
500
624
  var import_encoding_down = __toModule(require("encoding-down"));
625
+ var defaultPrefix = "_ROOT_";
626
+ var escapeStr = makeStringEscaper(new RegExp(INDEX_KEY_FIELD_SEPARATOR, "gm"), encodeURIComponent(INDEX_KEY_FIELD_SEPARATOR));
501
627
  var LevelStore = class {
502
628
  constructor(rootPath, useMemory = false) {
503
629
  this.rootPath = rootPath || "";
630
+ this.useMemory = useMemory;
504
631
  if (useMemory) {
505
- const db = (0, import_levelup.default)((0, import_encoding_down.default)((0, import_memdown.default)(), { valueEncoding: "json" }));
506
- this.db = db;
632
+ this.db = (0, import_levelup.default)((0, import_encoding_down.default)((0, import_memdown.default)(), { valueEncoding: "json" }));
507
633
  } else {
508
- const db = (0, import_level.default)(import_path5.default.join(rootPath, ".tina/__generated__/db"), {
634
+ this.db = (0, import_level.default)(import_path3.default.join(rootPath, ".tina/__generated__/db"), {
509
635
  valueEncoding: "json"
510
636
  });
511
- this.db = db;
512
637
  }
513
638
  }
514
- async query(queryStrings, hydrator) {
515
- const resultSets = await sequential(queryStrings, async (queryString) => {
516
- let strings = [];
517
- const p = new Promise((resolve, reject) => {
518
- this.db.createReadStream({
519
- gte: queryString,
520
- lte: queryString + "\xFF"
521
- }).on("data", (data) => {
522
- strings = [...strings, ...data.value];
523
- }).on("error", (message) => {
524
- reject(message);
525
- }).on("end", function() {
526
- resolve();
527
- });
528
- });
529
- await p;
530
- return strings || [];
531
- });
532
- let items = [];
533
- if (resultSets.length > 0) {
534
- items = resultSets.reduce((p, c) => p.filter((e) => c.includes(e)));
639
+ async query(queryOptions) {
640
+ var _b;
641
+ const _a = queryOptions, {
642
+ filterChain: rawFilterChain,
643
+ sort = DEFAULT_COLLECTION_SORT_KEY,
644
+ collection,
645
+ indexDefinitions,
646
+ limit = 10
647
+ } = _a, query = __objRest(_a, [
648
+ "filterChain",
649
+ "sort",
650
+ "collection",
651
+ "indexDefinitions",
652
+ "limit"
653
+ ]);
654
+ const filterChain = coerceFilterChainOperands(rawFilterChain, escapeStr);
655
+ const indexDefinition = sort && (indexDefinitions == null ? void 0 : indexDefinitions[sort]);
656
+ const filterSuffixes = indexDefinition && makeFilterSuffixes(filterChain, indexDefinition);
657
+ const indexPrefix = indexDefinition ? `${collection}${INDEX_KEY_FIELD_SEPARATOR}${sort}` : `${defaultPrefix}${INDEX_KEY_FIELD_SEPARATOR}`;
658
+ if (!query.gt && !query.gte) {
659
+ query.gte = (filterSuffixes == null ? void 0 : filterSuffixes.left) ? `${indexPrefix}${INDEX_KEY_FIELD_SEPARATOR}${filterSuffixes.left}` : indexPrefix;
535
660
  }
536
- return sequential(items, async (documentString) => {
537
- return hydrator(documentString);
538
- });
661
+ if (!query.lt && !query.lte) {
662
+ query.lte = (filterSuffixes == null ? void 0 : filterSuffixes.right) ? `${indexPrefix}${INDEX_KEY_FIELD_SEPARATOR}${filterSuffixes.right}\xFF` : `${indexPrefix}\xFF`;
663
+ }
664
+ let edges = [];
665
+ let startKey = "";
666
+ let endKey = "";
667
+ let hasPreviousPage = false;
668
+ let hasNextPage = false;
669
+ const fieldsPattern = ((_b = indexDefinition == null ? void 0 : indexDefinition.fields) == null ? void 0 : _b.length) ? `${indexDefinition.fields.map((p) => `${INDEX_KEY_FIELD_SEPARATOR}(?<${p.name}>.+)`).join("")}${INDEX_KEY_FIELD_SEPARATOR}` : INDEX_KEY_FIELD_SEPARATOR;
670
+ const valuesRegex = indexDefinition ? new RegExp(`^${indexPrefix}${fieldsPattern}(?<_filepath_>.+)`) : new RegExp(`^${indexPrefix}(?<_filepath_>.+)`);
671
+ const itemFilter = makeFilter({ filterChain });
672
+ for await (const [key, value] of this.db.iterator(query)) {
673
+ const matcher = valuesRegex.exec(key);
674
+ if (!matcher || indexDefinition && matcher.length !== indexDefinition.fields.length + 2) {
675
+ continue;
676
+ }
677
+ const filepath = matcher.groups["_filepath_"];
678
+ if (!itemFilter(filterSuffixes ? matcher.groups : indexDefinition ? await this.db.get(`${defaultPrefix}${INDEX_KEY_FIELD_SEPARATOR}${filepath}`) : value)) {
679
+ continue;
680
+ }
681
+ if (limit !== -1 && edges.length >= limit) {
682
+ if (query.reverse) {
683
+ hasPreviousPage = true;
684
+ } else {
685
+ hasNextPage = true;
686
+ }
687
+ break;
688
+ }
689
+ startKey = startKey || key || "";
690
+ endKey = key || "";
691
+ edges = [...edges, { cursor: key, path: filepath }];
692
+ }
693
+ return {
694
+ edges,
695
+ pageInfo: {
696
+ hasPreviousPage,
697
+ hasNextPage,
698
+ startCursor: startKey,
699
+ endCursor: endKey
700
+ }
701
+ };
539
702
  }
540
- async seed(filepath, data) {
541
- await this.put(filepath, data);
703
+ async seed(filepath, data, options) {
704
+ await this.put(filepath, data, __spreadValues({
705
+ keepTemplateKey: false,
706
+ seed: true
707
+ }, options));
542
708
  }
543
709
  supportsSeeding() {
544
710
  return true;
@@ -546,6 +712,27 @@ var LevelStore = class {
546
712
  supportsIndexing() {
547
713
  return true;
548
714
  }
715
+ async delete(filepath, options) {
716
+ const data = await this.db.get(`${defaultPrefix}${INDEX_KEY_FIELD_SEPARATOR}${filepath}`);
717
+ if (!data) {
718
+ return;
719
+ }
720
+ if (options == null ? void 0 : options.indexDefinitions) {
721
+ for (const [sort, definition] of Object.entries(options.indexDefinitions)) {
722
+ const indexedValue = makeKeyForField(definition, data, escapeStr);
723
+ let indexKey;
724
+ if (sort === DEFAULT_COLLECTION_SORT_KEY) {
725
+ indexKey = `${options.collection}${INDEX_KEY_FIELD_SEPARATOR}${sort}${INDEX_KEY_FIELD_SEPARATOR}${filepath}`;
726
+ } else {
727
+ indexKey = indexedValue ? `${options.collection}${INDEX_KEY_FIELD_SEPARATOR}${sort}${INDEX_KEY_FIELD_SEPARATOR}${indexedValue}${INDEX_KEY_FIELD_SEPARATOR}${filepath}` : null;
728
+ }
729
+ if (indexKey) {
730
+ await this.db.del(indexKey);
731
+ }
732
+ }
733
+ }
734
+ await this.db.del(`${defaultPrefix}${INDEX_KEY_FIELD_SEPARATOR}${filepath}`);
735
+ }
549
736
  async print() {
550
737
  this.db.createReadStream().on("data", function(data) {
551
738
  console.log(data.key, "=", data.value);
@@ -567,10 +754,10 @@ var LevelStore = class {
567
754
  const strings = [];
568
755
  const p = new Promise((resolve, reject) => {
569
756
  this.db.createKeyStream({
570
- gte: pattern,
571
- lte: pattern + "\xFF"
757
+ gte: `${defaultPrefix}${INDEX_KEY_FIELD_SEPARATOR}${pattern}`,
758
+ lte: `${defaultPrefix}${INDEX_KEY_FIELD_SEPARATOR}${pattern}\xFF`
572
759
  }).on("data", (data) => {
573
- strings.push(data);
760
+ strings.push(data.split(`${defaultPrefix}${INDEX_KEY_FIELD_SEPARATOR}`)[1]);
574
761
  }).on("error", (message) => {
575
762
  reject(message);
576
763
  }).on("end", function() {
@@ -588,22 +775,64 @@ var LevelStore = class {
588
775
  }
589
776
  async get(filepath) {
590
777
  try {
591
- const content = await this.db.get(filepath);
592
- return content;
778
+ return await this.db.get(`${defaultPrefix}${INDEX_KEY_FIELD_SEPARATOR}${filepath}`);
593
779
  } catch (e) {
594
780
  return void 0;
595
781
  }
596
782
  }
597
- async put(filepath, data) {
598
- await this.db.put(filepath, data);
783
+ async close() {
784
+ await this.db.close();
785
+ }
786
+ async put(filepath, data, options) {
787
+ let existingData;
788
+ try {
789
+ existingData = options && !options.seed ? await this.db.get(`${defaultPrefix}${INDEX_KEY_FIELD_SEPARATOR}${filepath}`) : null;
790
+ } catch (err) {
791
+ if (!err.notFound) {
792
+ throw err;
793
+ }
794
+ }
795
+ await this.db.put(`${defaultPrefix}${INDEX_KEY_FIELD_SEPARATOR}${filepath}`, data);
796
+ if (options == null ? void 0 : options.indexDefinitions) {
797
+ for (const [sort, definition] of Object.entries(options.indexDefinitions)) {
798
+ const indexedValue = makeKeyForField(definition, data, escapeStr);
799
+ const existingIndexedValue = existingData ? makeKeyForField(definition, existingData, escapeStr) : null;
800
+ let indexKey;
801
+ let existingIndexKey = null;
802
+ if (sort === DEFAULT_COLLECTION_SORT_KEY) {
803
+ indexKey = `${options.collection}${INDEX_KEY_FIELD_SEPARATOR}${sort}${INDEX_KEY_FIELD_SEPARATOR}${filepath}`;
804
+ existingIndexKey = indexKey;
805
+ } else {
806
+ indexKey = indexedValue ? `${options.collection}${INDEX_KEY_FIELD_SEPARATOR}${sort}${INDEX_KEY_FIELD_SEPARATOR}${indexedValue}${INDEX_KEY_FIELD_SEPARATOR}${filepath}` : null;
807
+ existingIndexKey = existingIndexedValue ? `${options.collection}${INDEX_KEY_FIELD_SEPARATOR}${sort}${INDEX_KEY_FIELD_SEPARATOR}${existingIndexedValue}${INDEX_KEY_FIELD_SEPARATOR}${filepath}` : null;
808
+ }
809
+ if (indexKey) {
810
+ if (existingIndexKey && indexKey != existingIndexKey) {
811
+ await this.db.del(existingIndexKey);
812
+ }
813
+ await this.db.put(indexKey, "");
814
+ }
815
+ }
816
+ }
599
817
  }
600
818
  };
601
819
  // Annotate the CommonJS export names for ESM import in node:
602
820
  0 && (module.exports = {
821
+ AuditFileSystemBridge,
822
+ AuditFilesystemStore,
823
+ DEFAULT_COLLECTION_SORT_KEY,
824
+ DEFAULT_NUMERIC_LPAD,
603
825
  FilesystemBridge,
604
826
  FilesystemStore,
605
- GithubBridge,
606
- GithubStore,
827
+ INDEX_KEY_FIELD_SEPARATOR,
607
828
  LevelStore,
608
- MemoryStore
829
+ OP,
830
+ atob,
831
+ btoa,
832
+ coerceFilterChainOperands,
833
+ makeFilter,
834
+ makeFilterChain,
835
+ makeFilterSuffixes,
836
+ makeKeyForField,
837
+ makeStringEscaper
609
838
  });