@tinacms/datalayer 1.0.1 → 1.1.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/LICENSE +37 -176
- package/dist/index.d.ts +2 -20
- package/dist/index.es.js +19 -0
- package/dist/index.js +12 -1152
- package/package.json +9 -23
- package/dist/database/bridge/filesystem.d.ts +0 -36
- package/dist/database/bridge/index.d.ts +0 -30
- package/dist/database/bridge/isomorphic.d.ts +0 -108
- package/dist/database/store/filesystem.d.ts +0 -34
- package/dist/database/store/index.d.ts +0 -144
- package/dist/database/store/level.d.ts +0 -32
- package/dist/database/util.d.ts +0 -16
- package/dist/util.d.ts +0 -28
package/dist/index.js
CHANGED
|
@@ -1,39 +1,10 @@
|
|
|
1
1
|
var __create = Object.create;
|
|
2
2
|
var __defProp = Object.defineProperty;
|
|
3
|
-
var __defProps = Object.defineProperties;
|
|
4
3
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
-
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
|
|
6
4
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
7
|
-
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
8
5
|
var __getProtoOf = Object.getPrototypeOf;
|
|
9
6
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
10
|
-
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
11
|
-
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
12
|
-
var __spreadValues = (a, b) => {
|
|
13
|
-
for (var prop in b || (b = {}))
|
|
14
|
-
if (__hasOwnProp.call(b, prop))
|
|
15
|
-
__defNormalProp(a, prop, b[prop]);
|
|
16
|
-
if (__getOwnPropSymbols)
|
|
17
|
-
for (var prop of __getOwnPropSymbols(b)) {
|
|
18
|
-
if (__propIsEnum.call(b, prop))
|
|
19
|
-
__defNormalProp(a, prop, b[prop]);
|
|
20
|
-
}
|
|
21
|
-
return a;
|
|
22
|
-
};
|
|
23
|
-
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
24
7
|
var __markAsModule = (target) => __defProp(target, "__esModule", { value: true });
|
|
25
|
-
var __objRest = (source, exclude) => {
|
|
26
|
-
var target = {};
|
|
27
|
-
for (var prop in source)
|
|
28
|
-
if (__hasOwnProp.call(source, prop) && exclude.indexOf(prop) < 0)
|
|
29
|
-
target[prop] = source[prop];
|
|
30
|
-
if (source != null && __getOwnPropSymbols)
|
|
31
|
-
for (var prop of __getOwnPropSymbols(source)) {
|
|
32
|
-
if (exclude.indexOf(prop) < 0 && __propIsEnum.call(source, prop))
|
|
33
|
-
target[prop] = source[prop];
|
|
34
|
-
}
|
|
35
|
-
return target;
|
|
36
|
-
};
|
|
37
8
|
var __export = (target, all) => {
|
|
38
9
|
__markAsModule(target);
|
|
39
10
|
for (var name in all)
|
|
@@ -53,1133 +24,22 @@ var __toModule = (module2) => {
|
|
|
53
24
|
|
|
54
25
|
// src/index.ts
|
|
55
26
|
__export(exports, {
|
|
56
|
-
AuditFileSystemBridge: () => AuditFileSystemBridge,
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
IsomorphicBridge: () => IsomorphicBridge,
|
|
64
|
-
LevelStore: () => LevelStore,
|
|
65
|
-
OP: () => OP,
|
|
66
|
-
atob: () => atob,
|
|
67
|
-
btoa: () => btoa,
|
|
68
|
-
coerceFilterChainOperands: () => coerceFilterChainOperands,
|
|
69
|
-
makeFilter: () => makeFilter,
|
|
70
|
-
makeFilterChain: () => makeFilterChain,
|
|
71
|
-
makeFilterSuffixes: () => makeFilterSuffixes,
|
|
72
|
-
makeKeyForField: () => makeKeyForField,
|
|
73
|
-
makeStringEscaper: () => makeStringEscaper
|
|
27
|
+
AuditFileSystemBridge: () => import_graphql.AuditFileSystemBridge,
|
|
28
|
+
Database: () => import_graphql.Database,
|
|
29
|
+
FilesystemBridge: () => import_graphql.FilesystemBridge,
|
|
30
|
+
IsomorphicBridge: () => import_graphql.IsomorphicBridge,
|
|
31
|
+
TinaLevelClient: () => import_graphql.TinaLevelClient,
|
|
32
|
+
createDatabase: () => import_graphql.createDatabase,
|
|
33
|
+
resolve: () => import_graphql.resolve
|
|
74
34
|
});
|
|
75
|
-
|
|
76
|
-
// src/database/bridge/filesystem.ts
|
|
77
|
-
var import_fs_extra = __toModule(require("fs-extra"));
|
|
78
|
-
var import_fast_glob = __toModule(require("fast-glob"));
|
|
79
|
-
var import_path = __toModule(require("path"));
|
|
80
|
-
var import_normalize_path = __toModule(require("normalize-path"));
|
|
81
|
-
var FilesystemBridge = class {
|
|
82
|
-
constructor(rootPath) {
|
|
83
|
-
this.rootPath = rootPath || "";
|
|
84
|
-
this.outputPath = rootPath || "";
|
|
85
|
-
}
|
|
86
|
-
addOutputPath(outputPath) {
|
|
87
|
-
this.outputPath = outputPath;
|
|
88
|
-
}
|
|
89
|
-
async glob(pattern, extension) {
|
|
90
|
-
const basePath = import_path.default.join(this.outputPath, ...pattern.split("/"));
|
|
91
|
-
const items = await (0, import_fast_glob.default)(import_path.default.join(basePath, "**", `/*${extension}`).replace(/\\/g, "/"), {
|
|
92
|
-
dot: true
|
|
93
|
-
});
|
|
94
|
-
const posixRootPath = (0, import_normalize_path.default)(this.outputPath);
|
|
95
|
-
return items.map((item) => {
|
|
96
|
-
return item.replace(posixRootPath, "").replace(/^\/|\/$/g, "");
|
|
97
|
-
});
|
|
98
|
-
}
|
|
99
|
-
supportsBuilding() {
|
|
100
|
-
return true;
|
|
101
|
-
}
|
|
102
|
-
async delete(filepath) {
|
|
103
|
-
await import_fs_extra.default.remove(import_path.default.join(this.outputPath, filepath));
|
|
104
|
-
}
|
|
105
|
-
async get(filepath) {
|
|
106
|
-
return import_fs_extra.default.readFileSync(import_path.default.join(this.outputPath, filepath)).toString();
|
|
107
|
-
}
|
|
108
|
-
async putConfig(filepath, data) {
|
|
109
|
-
if (this.rootPath !== this.outputPath) {
|
|
110
|
-
await this.put(filepath, data);
|
|
111
|
-
await this.put(filepath, data, this.rootPath);
|
|
112
|
-
} else {
|
|
113
|
-
await this.put(filepath, data);
|
|
114
|
-
}
|
|
115
|
-
}
|
|
116
|
-
async put(filepath, data, basePathOverride) {
|
|
117
|
-
const basePath = basePathOverride || this.outputPath;
|
|
118
|
-
await import_fs_extra.default.outputFileSync(import_path.default.join(basePath, filepath), data);
|
|
119
|
-
}
|
|
120
|
-
};
|
|
121
|
-
var AuditFileSystemBridge = class extends FilesystemBridge {
|
|
122
|
-
async put(filepath, data) {
|
|
123
|
-
if ([
|
|
124
|
-
".tina/__generated__/_lookup.json",
|
|
125
|
-
".tina/__generated__/_schema.json",
|
|
126
|
-
".tina/__generated__/_graphql.json"
|
|
127
|
-
].includes(filepath)) {
|
|
128
|
-
return super.put(filepath, data);
|
|
129
|
-
}
|
|
130
|
-
return;
|
|
131
|
-
}
|
|
132
|
-
};
|
|
133
|
-
|
|
134
|
-
// src/database/bridge/isomorphic.ts
|
|
135
|
-
var import_isomorphic_git = __toModule(require("isomorphic-git"));
|
|
136
|
-
var import_fs_extra2 = __toModule(require("fs-extra"));
|
|
137
|
-
var import_glob_parent = __toModule(require("glob-parent"));
|
|
138
|
-
var import_normalize_path2 = __toModule(require("normalize-path"));
|
|
139
|
-
var import_graphql = __toModule(require("graphql"));
|
|
140
|
-
var import_path2 = __toModule(require("path"));
|
|
141
|
-
var flat = typeof Array.prototype.flat === "undefined" ? (entries) => entries.reduce((acc, x) => acc.concat(x), []) : (entries) => entries.flat();
|
|
142
|
-
var toUint8Array = (buf) => {
|
|
143
|
-
const ab = new ArrayBuffer(buf.length);
|
|
144
|
-
const view = new Uint8Array(ab);
|
|
145
|
-
for (let i = 0; i < buf.length; ++i) {
|
|
146
|
-
view[i] = buf[i];
|
|
147
|
-
}
|
|
148
|
-
return view;
|
|
149
|
-
};
|
|
150
|
-
var IsomorphicBridge = class {
|
|
151
|
-
constructor(rootPath, {
|
|
152
|
-
gitRoot,
|
|
153
|
-
author,
|
|
154
|
-
committer,
|
|
155
|
-
fsModule = import_fs_extra2.default,
|
|
156
|
-
commitMessage = "Update from GraphQL client",
|
|
157
|
-
ref,
|
|
158
|
-
onPut,
|
|
159
|
-
onDelete
|
|
160
|
-
}) {
|
|
161
|
-
this.cache = {};
|
|
162
|
-
this.rootPath = rootPath;
|
|
163
|
-
this.gitRoot = gitRoot;
|
|
164
|
-
this.relativePath = rootPath.slice(this.gitRoot.length).replace(/\\/g, "/");
|
|
165
|
-
if (this.relativePath.startsWith("/")) {
|
|
166
|
-
this.relativePath = this.relativePath.slice(1);
|
|
167
|
-
}
|
|
168
|
-
this.fsModule = fsModule;
|
|
169
|
-
this.author = author;
|
|
170
|
-
this.committer = committer || author;
|
|
171
|
-
this.isomorphicConfig = {
|
|
172
|
-
dir: (0, import_normalize_path2.default)(this.gitRoot),
|
|
173
|
-
fs: this.fsModule
|
|
174
|
-
};
|
|
175
|
-
this.ref = ref;
|
|
176
|
-
this.commitMessage = commitMessage;
|
|
177
|
-
this.onPut = onPut || (() => {
|
|
178
|
-
});
|
|
179
|
-
this.onDelete = onDelete || (() => {
|
|
180
|
-
});
|
|
181
|
-
}
|
|
182
|
-
getAuthor() {
|
|
183
|
-
return __spreadProps(__spreadValues({}, this.author), {
|
|
184
|
-
timestamp: Math.round(new Date().getTime() / 1e3),
|
|
185
|
-
timezoneOffset: 0
|
|
186
|
-
});
|
|
187
|
-
}
|
|
188
|
-
getCommitter() {
|
|
189
|
-
return __spreadProps(__spreadValues({}, this.committer), {
|
|
190
|
-
timestamp: Math.round(new Date().getTime() / 1e3),
|
|
191
|
-
timezoneOffset: 0
|
|
192
|
-
});
|
|
193
|
-
}
|
|
194
|
-
async listEntries({
|
|
195
|
-
pattern,
|
|
196
|
-
entry,
|
|
197
|
-
path: path4,
|
|
198
|
-
results
|
|
199
|
-
}) {
|
|
200
|
-
const treeResult = await import_isomorphic_git.default.readTree(__spreadProps(__spreadValues({}, this.isomorphicConfig), {
|
|
201
|
-
oid: entry.oid,
|
|
202
|
-
cache: this.cache
|
|
203
|
-
}));
|
|
204
|
-
const children = [];
|
|
205
|
-
for (const childEntry of treeResult.tree) {
|
|
206
|
-
const childPath = path4 ? `${path4}/${childEntry.path}` : childEntry.path;
|
|
207
|
-
if (childEntry.type === "tree") {
|
|
208
|
-
children.push(childEntry);
|
|
209
|
-
} else {
|
|
210
|
-
if (childPath.startsWith(pattern)) {
|
|
211
|
-
results.push(childPath);
|
|
212
|
-
}
|
|
213
|
-
}
|
|
214
|
-
}
|
|
215
|
-
for (const childEntry of children) {
|
|
216
|
-
const childPath = path4 ? `${path4}/${childEntry.path}` : childEntry.path;
|
|
217
|
-
await this.listEntries({
|
|
218
|
-
pattern,
|
|
219
|
-
entry: childEntry,
|
|
220
|
-
path: childPath,
|
|
221
|
-
results
|
|
222
|
-
});
|
|
223
|
-
}
|
|
224
|
-
}
|
|
225
|
-
async resolvePathEntries(path4, ref) {
|
|
226
|
-
let pathParts = path4.split("/");
|
|
227
|
-
const result = await import_isomorphic_git.default.walk(__spreadProps(__spreadValues({}, this.isomorphicConfig), {
|
|
228
|
-
map: async (filepath, [head]) => {
|
|
229
|
-
if (head._fullpath === ".") {
|
|
230
|
-
return head;
|
|
231
|
-
}
|
|
232
|
-
if (path4.startsWith(filepath)) {
|
|
233
|
-
if ((0, import_path2.dirname)(path4) === (0, import_path2.dirname)(filepath)) {
|
|
234
|
-
if (path4 === filepath) {
|
|
235
|
-
return head;
|
|
236
|
-
}
|
|
237
|
-
} else {
|
|
238
|
-
return head;
|
|
239
|
-
}
|
|
240
|
-
}
|
|
241
|
-
},
|
|
242
|
-
cache: this.cache,
|
|
243
|
-
trees: [import_isomorphic_git.default.TREE({ ref })]
|
|
244
|
-
}));
|
|
245
|
-
const pathEntries = flat(result);
|
|
246
|
-
if (pathParts.indexOf(".") === -1) {
|
|
247
|
-
pathParts = [".", ...pathParts];
|
|
248
|
-
}
|
|
249
|
-
while (pathParts.length > pathEntries.length) {
|
|
250
|
-
pathEntries.push(null);
|
|
251
|
-
}
|
|
252
|
-
return { pathParts, pathEntries };
|
|
253
|
-
}
|
|
254
|
-
async updateTreeHierarchy(existingOid, updatedOid, path4, type, pathEntries, pathParts) {
|
|
255
|
-
const lastIdx = pathEntries.length - 1;
|
|
256
|
-
const parentEntry = pathEntries[lastIdx];
|
|
257
|
-
const parentPath = pathParts[lastIdx];
|
|
258
|
-
let parentOid;
|
|
259
|
-
let tree;
|
|
260
|
-
const mode = type === "blob" ? "100644" : "040000";
|
|
261
|
-
if (parentEntry) {
|
|
262
|
-
parentOid = await parentEntry.oid();
|
|
263
|
-
const treeResult = await import_isomorphic_git.default.readTree(__spreadProps(__spreadValues({}, this.isomorphicConfig), {
|
|
264
|
-
oid: parentOid,
|
|
265
|
-
cache: this.cache
|
|
266
|
-
}));
|
|
267
|
-
tree = existingOid ? treeResult.tree.map((entry) => {
|
|
268
|
-
if (entry.path === path4) {
|
|
269
|
-
entry.oid = updatedOid;
|
|
270
|
-
}
|
|
271
|
-
return entry;
|
|
272
|
-
}) : [
|
|
273
|
-
...treeResult.tree,
|
|
274
|
-
{
|
|
275
|
-
oid: updatedOid,
|
|
276
|
-
type,
|
|
277
|
-
path: path4,
|
|
278
|
-
mode
|
|
279
|
-
}
|
|
280
|
-
];
|
|
281
|
-
} else {
|
|
282
|
-
tree = [
|
|
283
|
-
{
|
|
284
|
-
oid: updatedOid,
|
|
285
|
-
type,
|
|
286
|
-
path: path4,
|
|
287
|
-
mode
|
|
288
|
-
}
|
|
289
|
-
];
|
|
290
|
-
}
|
|
291
|
-
const updatedParentOid = await import_isomorphic_git.default.writeTree(__spreadProps(__spreadValues({}, this.isomorphicConfig), {
|
|
292
|
-
tree
|
|
293
|
-
}));
|
|
294
|
-
if (lastIdx === 0) {
|
|
295
|
-
return updatedParentOid;
|
|
296
|
-
} else {
|
|
297
|
-
return await this.updateTreeHierarchy(parentOid, updatedParentOid, parentPath, "tree", pathEntries.slice(0, lastIdx), pathParts.slice(0, lastIdx));
|
|
298
|
-
}
|
|
299
|
-
}
|
|
300
|
-
async commitTree(treeSha, ref) {
|
|
301
|
-
const commitSha = await import_isomorphic_git.default.writeCommit(__spreadProps(__spreadValues({}, this.isomorphicConfig), {
|
|
302
|
-
commit: {
|
|
303
|
-
tree: treeSha,
|
|
304
|
-
parent: [
|
|
305
|
-
await import_isomorphic_git.default.resolveRef(__spreadProps(__spreadValues({}, this.isomorphicConfig), {
|
|
306
|
-
ref
|
|
307
|
-
}))
|
|
308
|
-
],
|
|
309
|
-
message: this.commitMessage,
|
|
310
|
-
author: this.getAuthor(),
|
|
311
|
-
committer: this.getCommitter()
|
|
312
|
-
}
|
|
313
|
-
}));
|
|
314
|
-
await import_isomorphic_git.default.writeRef(__spreadProps(__spreadValues({}, this.isomorphicConfig), {
|
|
315
|
-
ref,
|
|
316
|
-
value: commitSha,
|
|
317
|
-
force: true
|
|
318
|
-
}));
|
|
319
|
-
}
|
|
320
|
-
async getRef() {
|
|
321
|
-
if (this.ref) {
|
|
322
|
-
return this.ref;
|
|
323
|
-
}
|
|
324
|
-
const ref = await import_isomorphic_git.default.currentBranch(__spreadProps(__spreadValues({}, this.isomorphicConfig), {
|
|
325
|
-
fullname: true
|
|
326
|
-
}));
|
|
327
|
-
if (!ref) {
|
|
328
|
-
throw new import_graphql.GraphQLError(`Unable to determine current branch from HEAD`, null, null, null, null, null, {});
|
|
329
|
-
}
|
|
330
|
-
this.ref = ref;
|
|
331
|
-
return ref;
|
|
332
|
-
}
|
|
333
|
-
async glob(pattern, extension) {
|
|
334
|
-
const ref = await this.getRef();
|
|
335
|
-
const parent = (0, import_glob_parent.default)(this.qualifyPath(pattern));
|
|
336
|
-
const { pathParts, pathEntries } = await this.resolvePathEntries(parent, ref);
|
|
337
|
-
const leafEntry = pathEntries[pathEntries.length - 1];
|
|
338
|
-
const entryPath = pathParts[pathParts.length - 1];
|
|
339
|
-
const parentEntry = pathEntries[pathEntries.length - 2];
|
|
340
|
-
let treeEntry;
|
|
341
|
-
let parentPath;
|
|
342
|
-
if (parentEntry) {
|
|
343
|
-
const treeResult = await import_isomorphic_git.default.readTree(__spreadProps(__spreadValues({}, this.isomorphicConfig), {
|
|
344
|
-
oid: await parentEntry.oid(),
|
|
345
|
-
cache: this.cache
|
|
346
|
-
}));
|
|
347
|
-
treeEntry = treeResult.tree.find((entry) => entry.path === entryPath);
|
|
348
|
-
parentPath = pathParts.slice(1, pathParts.length).join("/");
|
|
349
|
-
} else {
|
|
350
|
-
treeEntry = {
|
|
351
|
-
type: "tree",
|
|
352
|
-
oid: await leafEntry.oid()
|
|
353
|
-
};
|
|
354
|
-
parentPath = "";
|
|
355
|
-
}
|
|
356
|
-
const results = [];
|
|
357
|
-
await this.listEntries({
|
|
358
|
-
pattern: this.qualifyPath(pattern),
|
|
359
|
-
entry: treeEntry,
|
|
360
|
-
path: parentPath,
|
|
361
|
-
results
|
|
362
|
-
});
|
|
363
|
-
return results.map((path4) => this.unqualifyPath(path4)).filter((path4) => path4.endsWith(extension));
|
|
364
|
-
}
|
|
365
|
-
supportsBuilding() {
|
|
366
|
-
return true;
|
|
367
|
-
}
|
|
368
|
-
async delete(filepath) {
|
|
369
|
-
const ref = await this.getRef();
|
|
370
|
-
const { pathParts, pathEntries } = await this.resolvePathEntries(this.qualifyPath(filepath), ref);
|
|
371
|
-
let oidToRemove;
|
|
372
|
-
let ptr = pathEntries.length - 1;
|
|
373
|
-
while (ptr >= 1) {
|
|
374
|
-
const leafEntry = pathEntries[ptr];
|
|
375
|
-
const nodePath = pathParts[ptr];
|
|
376
|
-
if (leafEntry) {
|
|
377
|
-
oidToRemove = oidToRemove || await leafEntry.oid();
|
|
378
|
-
const parentEntry = pathEntries[ptr - 1];
|
|
379
|
-
const existingOid = await parentEntry.oid();
|
|
380
|
-
const treeResult = await import_isomorphic_git.default.readTree(__spreadProps(__spreadValues({}, this.isomorphicConfig), {
|
|
381
|
-
oid: existingOid,
|
|
382
|
-
cache: this.cache
|
|
383
|
-
}));
|
|
384
|
-
const updatedTree = treeResult.tree.filter((value) => value.path !== nodePath);
|
|
385
|
-
if (updatedTree.length === 0) {
|
|
386
|
-
ptr -= 1;
|
|
387
|
-
continue;
|
|
388
|
-
}
|
|
389
|
-
const updatedTreeOid = await import_isomorphic_git.default.writeTree(__spreadProps(__spreadValues({}, this.isomorphicConfig), {
|
|
390
|
-
tree: updatedTree
|
|
391
|
-
}));
|
|
392
|
-
const updatedRootTreeOid = await this.updateTreeHierarchy(existingOid, updatedTreeOid, pathParts[ptr - 1], "tree", pathEntries.slice(0, ptr - 1), pathParts.slice(0, ptr - 1));
|
|
393
|
-
await this.commitTree(updatedRootTreeOid, ref);
|
|
394
|
-
break;
|
|
395
|
-
} else {
|
|
396
|
-
throw new import_graphql.GraphQLError(`Unable to resolve path: ${filepath}`, null, null, null, null, null, { status: 404 });
|
|
397
|
-
}
|
|
398
|
-
}
|
|
399
|
-
if (oidToRemove) {
|
|
400
|
-
await import_isomorphic_git.default.updateIndex(__spreadProps(__spreadValues({}, this.isomorphicConfig), {
|
|
401
|
-
filepath: this.qualifyPath(filepath),
|
|
402
|
-
force: true,
|
|
403
|
-
remove: true,
|
|
404
|
-
oid: oidToRemove,
|
|
405
|
-
cache: this.cache
|
|
406
|
-
}));
|
|
407
|
-
}
|
|
408
|
-
await this.onDelete(filepath);
|
|
409
|
-
}
|
|
410
|
-
qualifyPath(filepath) {
|
|
411
|
-
return this.relativePath ? `${this.relativePath}/${filepath}` : filepath;
|
|
412
|
-
}
|
|
413
|
-
unqualifyPath(filepath) {
|
|
414
|
-
return this.relativePath ? filepath.slice(this.relativePath.length + 1) : filepath;
|
|
415
|
-
}
|
|
416
|
-
async get(filepath) {
|
|
417
|
-
const ref = await this.getRef();
|
|
418
|
-
const oid = await import_isomorphic_git.default.resolveRef(__spreadProps(__spreadValues({}, this.isomorphicConfig), {
|
|
419
|
-
ref
|
|
420
|
-
}));
|
|
421
|
-
const { blob } = await import_isomorphic_git.default.readBlob(__spreadProps(__spreadValues({}, this.isomorphicConfig), {
|
|
422
|
-
oid,
|
|
423
|
-
filepath: this.qualifyPath(filepath),
|
|
424
|
-
cache: this.cache
|
|
425
|
-
}));
|
|
426
|
-
return Buffer.from(blob).toString("utf8");
|
|
427
|
-
}
|
|
428
|
-
async putConfig(filepath, data) {
|
|
429
|
-
await this.put(filepath, data);
|
|
430
|
-
}
|
|
431
|
-
async put(filepath, data) {
|
|
432
|
-
const ref = await this.getRef();
|
|
433
|
-
const { pathParts, pathEntries } = await this.resolvePathEntries(this.qualifyPath(filepath), ref);
|
|
434
|
-
const blobUpdate = toUint8Array(Buffer.from(data));
|
|
435
|
-
let existingOid;
|
|
436
|
-
const leafEntry = pathEntries[pathEntries.length - 1];
|
|
437
|
-
const nodePath = pathParts[pathParts.length - 1];
|
|
438
|
-
if (leafEntry) {
|
|
439
|
-
existingOid = await leafEntry.oid();
|
|
440
|
-
const hash = await import_isomorphic_git.default.hashBlob({ object: blobUpdate });
|
|
441
|
-
if (hash.oid === existingOid) {
|
|
442
|
-
await this.onPut(filepath, data);
|
|
443
|
-
return;
|
|
444
|
-
}
|
|
445
|
-
}
|
|
446
|
-
const updatedOid = await import_isomorphic_git.default.writeBlob(__spreadProps(__spreadValues({}, this.isomorphicConfig), {
|
|
447
|
-
blob: blobUpdate
|
|
448
|
-
}));
|
|
449
|
-
const updatedRootSha = await this.updateTreeHierarchy(existingOid, updatedOid, nodePath, "blob", pathEntries.slice(0, pathEntries.length - 1), pathParts.slice(0, pathParts.length - 1));
|
|
450
|
-
await this.commitTree(updatedRootSha, ref);
|
|
451
|
-
await import_isomorphic_git.default.updateIndex(__spreadProps(__spreadValues({}, this.isomorphicConfig), {
|
|
452
|
-
filepath: this.qualifyPath(filepath),
|
|
453
|
-
add: true,
|
|
454
|
-
oid: updatedOid,
|
|
455
|
-
cache: this.cache
|
|
456
|
-
}));
|
|
457
|
-
await this.onPut(filepath, data);
|
|
458
|
-
}
|
|
459
|
-
};
|
|
460
|
-
|
|
461
|
-
// src/database/store/filesystem.ts
|
|
462
|
-
var import_fs_extra3 = __toModule(require("fs-extra"));
|
|
463
|
-
var import_fast_glob2 = __toModule(require("fast-glob"));
|
|
464
|
-
var import_path3 = __toModule(require("path"));
|
|
465
|
-
var import_normalize_path3 = __toModule(require("normalize-path"));
|
|
466
|
-
|
|
467
|
-
// src/database/util.ts
|
|
468
|
-
var import_gray_matter = __toModule(require("gray-matter"));
|
|
469
|
-
|
|
470
|
-
// src/util.ts
|
|
471
|
-
var yup = __toModule(require("yup"));
|
|
472
|
-
var import_graphql2 = __toModule(require("graphql"));
|
|
473
|
-
var sequential = async (items, callback) => {
|
|
474
|
-
const accum = [];
|
|
475
|
-
if (!items) {
|
|
476
|
-
return [];
|
|
477
|
-
}
|
|
478
|
-
const reducePromises = async (previous, endpoint) => {
|
|
479
|
-
const prev = await previous;
|
|
480
|
-
if (prev) {
|
|
481
|
-
accum.push(prev);
|
|
482
|
-
}
|
|
483
|
-
return callback(endpoint, accum.length);
|
|
484
|
-
};
|
|
485
|
-
const result = await items.reduce(reducePromises, Promise.resolve());
|
|
486
|
-
if (result) {
|
|
487
|
-
accum.push(result);
|
|
488
|
-
}
|
|
489
|
-
return accum;
|
|
490
|
-
};
|
|
491
|
-
function assertShape(value, yupSchema, errorMessage) {
|
|
492
|
-
const shape = yupSchema(yup);
|
|
493
|
-
try {
|
|
494
|
-
shape.validateSync(value);
|
|
495
|
-
} catch (e) {
|
|
496
|
-
const message = errorMessage || `Failed to assertShape - ${e.message}`;
|
|
497
|
-
throw new import_graphql2.GraphQLError(message, null, null, null, null, null, {
|
|
498
|
-
stack: e.stack
|
|
499
|
-
});
|
|
500
|
-
}
|
|
501
|
-
}
|
|
502
|
-
var atob = (b64Encoded) => {
|
|
503
|
-
return Buffer.from(b64Encoded, "base64").toString();
|
|
504
|
-
};
|
|
505
|
-
var btoa = (string) => {
|
|
506
|
-
return Buffer.from(string).toString("base64");
|
|
507
|
-
};
|
|
508
|
-
|
|
509
|
-
// src/database/util.ts
|
|
510
|
-
var stringifyFile = (content, format, keepTemplateKey) => {
|
|
511
|
-
switch (format) {
|
|
512
|
-
case ".markdown":
|
|
513
|
-
case ".mdx":
|
|
514
|
-
case ".md":
|
|
515
|
-
const _a = content, {
|
|
516
|
-
_relativePath,
|
|
517
|
-
_keepTemplateKey,
|
|
518
|
-
_id,
|
|
519
|
-
_template,
|
|
520
|
-
_collection,
|
|
521
|
-
$_body
|
|
522
|
-
} = _a, rest = __objRest(_a, [
|
|
523
|
-
"_relativePath",
|
|
524
|
-
"_keepTemplateKey",
|
|
525
|
-
"_id",
|
|
526
|
-
"_template",
|
|
527
|
-
"_collection",
|
|
528
|
-
"$_body"
|
|
529
|
-
]);
|
|
530
|
-
const extra = {};
|
|
531
|
-
if (keepTemplateKey) {
|
|
532
|
-
extra["_template"] = _template;
|
|
533
|
-
}
|
|
534
|
-
const ok = import_gray_matter.default.stringify(typeof $_body === "undefined" ? "" : `
|
|
535
|
-
${$_body}`, __spreadValues(__spreadValues({}, rest), extra));
|
|
536
|
-
return ok;
|
|
537
|
-
case ".json":
|
|
538
|
-
return JSON.stringify(content, null, 2);
|
|
539
|
-
default:
|
|
540
|
-
throw new Error(`Must specify a valid format, got ${format}`);
|
|
541
|
-
}
|
|
542
|
-
};
|
|
543
|
-
var parseFile = (content, format, yupSchema) => {
|
|
544
|
-
switch (format) {
|
|
545
|
-
case ".markdown":
|
|
546
|
-
case ".mdx":
|
|
547
|
-
case ".md":
|
|
548
|
-
const contentJSON = (0, import_gray_matter.default)(content || "");
|
|
549
|
-
const markdownData = __spreadProps(__spreadValues({}, contentJSON.data), {
|
|
550
|
-
$_body: contentJSON.content
|
|
551
|
-
});
|
|
552
|
-
assertShape(markdownData, yupSchema);
|
|
553
|
-
return markdownData;
|
|
554
|
-
case ".json":
|
|
555
|
-
if (!content) {
|
|
556
|
-
return {};
|
|
557
|
-
}
|
|
558
|
-
return JSON.parse(content);
|
|
559
|
-
default:
|
|
560
|
-
throw new Error(`Must specify a valid format, got ${format}`);
|
|
561
|
-
}
|
|
562
|
-
};
|
|
563
|
-
|
|
564
|
-
// src/database/store/filesystem.ts
|
|
565
|
-
var FilesystemStore = class {
|
|
566
|
-
async clear() {
|
|
567
|
-
}
|
|
568
|
-
async print() {
|
|
569
|
-
}
|
|
570
|
-
constructor({ rootPath }) {
|
|
571
|
-
this.rootPath = rootPath || "";
|
|
572
|
-
}
|
|
573
|
-
async query(_queryOptions) {
|
|
574
|
-
throw new Error(`Unable to perform query for Filesystem store`);
|
|
575
|
-
}
|
|
576
|
-
async seed() {
|
|
577
|
-
throw new Error(`Seeding data is not possible for Filesystem store`);
|
|
578
|
-
}
|
|
579
|
-
async get(filepath) {
|
|
580
|
-
return parseFile(await import_fs_extra3.default.readFileSync(import_path3.default.join(this.rootPath, filepath)).toString(), import_path3.default.extname(filepath), (yup2) => yup2.object());
|
|
581
|
-
}
|
|
582
|
-
supportsSeeding() {
|
|
583
|
-
return false;
|
|
584
|
-
}
|
|
585
|
-
supportsIndexing() {
|
|
586
|
-
return false;
|
|
587
|
-
}
|
|
588
|
-
async glob(pattern, callback, extension) {
|
|
589
|
-
const basePath = import_path3.default.join(this.rootPath, ...pattern.split("/"));
|
|
590
|
-
const itemsRaw = await (0, import_fast_glob2.default)(import_path3.default.join(basePath, "**", `/*${extension}`).replace(/\\/g, "/"), {
|
|
591
|
-
dot: true
|
|
592
|
-
});
|
|
593
|
-
const posixRootPath = (0, import_normalize_path3.default)(this.rootPath);
|
|
594
|
-
const items = itemsRaw.map((item) => {
|
|
595
|
-
return item.replace(posixRootPath, "").replace(/^\/|\/$/g, "");
|
|
596
|
-
});
|
|
597
|
-
if (callback) {
|
|
598
|
-
return sequential(items, async (item) => {
|
|
599
|
-
return callback(item);
|
|
600
|
-
});
|
|
601
|
-
} else {
|
|
602
|
-
return items;
|
|
603
|
-
}
|
|
604
|
-
}
|
|
605
|
-
async put(filepath, data, options) {
|
|
606
|
-
await import_fs_extra3.default.outputFileSync(import_path3.default.join(this.rootPath, filepath), stringifyFile(data, import_path3.default.extname(filepath), options.keepTemplateKey));
|
|
607
|
-
}
|
|
608
|
-
async open() {
|
|
609
|
-
}
|
|
610
|
-
async close() {
|
|
611
|
-
}
|
|
612
|
-
async delete(filepath) {
|
|
613
|
-
await import_fs_extra3.default.remove(import_path3.default.join(this.rootPath, filepath));
|
|
614
|
-
}
|
|
615
|
-
};
|
|
616
|
-
var AuditFilesystemStore = class extends FilesystemStore {
|
|
617
|
-
async put(_filepath, _data) {
|
|
618
|
-
return;
|
|
619
|
-
}
|
|
620
|
-
};
|
|
621
|
-
|
|
622
|
-
// src/database/store/index.ts
|
|
623
|
-
var import_jsonpath_plus = __toModule(require("jsonpath-plus"));
|
|
624
|
-
var DEFAULT_COLLECTION_SORT_KEY = "__filepath__";
|
|
625
|
-
var INDEX_KEY_FIELD_SEPARATOR = "#";
|
|
626
|
-
var DEFAULT_NUMERIC_LPAD = 4;
|
|
627
|
-
var OP;
|
|
628
|
-
(function(OP2) {
|
|
629
|
-
OP2["EQ"] = "eq";
|
|
630
|
-
OP2["GT"] = "gt";
|
|
631
|
-
OP2["LT"] = "lt";
|
|
632
|
-
OP2["GTE"] = "gte";
|
|
633
|
-
OP2["LTE"] = "lte";
|
|
634
|
-
OP2["STARTS_WITH"] = "startsWith";
|
|
635
|
-
OP2["IN"] = "in";
|
|
636
|
-
})(OP || (OP = {}));
|
|
637
|
-
var inferOperatorFromFilter = (filterOperator) => {
|
|
638
|
-
switch (filterOperator) {
|
|
639
|
-
case "after":
|
|
640
|
-
return OP.GT;
|
|
641
|
-
case "before":
|
|
642
|
-
return OP.LT;
|
|
643
|
-
case "eq":
|
|
644
|
-
return OP.EQ;
|
|
645
|
-
case "startsWith":
|
|
646
|
-
return OP.STARTS_WITH;
|
|
647
|
-
case "lt":
|
|
648
|
-
return OP.LT;
|
|
649
|
-
case "lte":
|
|
650
|
-
return OP.LTE;
|
|
651
|
-
case "gt":
|
|
652
|
-
return OP.GT;
|
|
653
|
-
case "gte":
|
|
654
|
-
return OP.GTE;
|
|
655
|
-
case "in":
|
|
656
|
-
return OP.IN;
|
|
657
|
-
default:
|
|
658
|
-
throw new Error(`unsupported filter condition: '${filterOperator}'`);
|
|
659
|
-
}
|
|
660
|
-
};
|
|
661
|
-
var getFilterOperator = (expression, operand) => {
|
|
662
|
-
return (expression[operand] || expression[operand] === 0) && operand;
|
|
663
|
-
};
|
|
664
|
-
var makeFilterChain = ({
|
|
665
|
-
conditions
|
|
666
|
-
}) => {
|
|
667
|
-
const filterChain = [];
|
|
668
|
-
if (!conditions) {
|
|
669
|
-
return filterChain;
|
|
670
|
-
}
|
|
671
|
-
for (const condition of conditions) {
|
|
672
|
-
const { filterPath, filterExpression } = condition;
|
|
673
|
-
const _a = filterExpression, { _type } = _a, keys = __objRest(_a, ["_type"]);
|
|
674
|
-
const [key1, key2, ...extraKeys] = Object.keys(keys);
|
|
675
|
-
if (extraKeys.length) {
|
|
676
|
-
throw new Error(`Unexpected keys: [${extraKeys.join(",")}] in filter expression`);
|
|
677
|
-
}
|
|
678
|
-
if (key1 && !key2) {
|
|
679
|
-
filterChain.push({
|
|
680
|
-
pathExpression: filterPath,
|
|
681
|
-
rightOperand: filterExpression[key1],
|
|
682
|
-
operator: inferOperatorFromFilter(key1),
|
|
683
|
-
type: _type,
|
|
684
|
-
pad: _type === "number" ? { fillString: "0", maxLength: DEFAULT_NUMERIC_LPAD } : void 0
|
|
685
|
-
});
|
|
686
|
-
} else if (key1 && key2) {
|
|
687
|
-
const leftFilterOperator = getFilterOperator(filterExpression, "gt") || getFilterOperator(filterExpression, "gte") || getFilterOperator(filterExpression, "after") || void 0;
|
|
688
|
-
const rightFilterOperator = getFilterOperator(filterExpression, "lt") || getFilterOperator(filterExpression, "lte") || getFilterOperator(filterExpression, "before") || void 0;
|
|
689
|
-
let leftOperand;
|
|
690
|
-
let rightOperand;
|
|
691
|
-
if (rightFilterOperator && leftFilterOperator) {
|
|
692
|
-
if (key1 === leftFilterOperator) {
|
|
693
|
-
leftOperand = filterExpression[key1];
|
|
694
|
-
rightOperand = filterExpression[key2];
|
|
695
|
-
} else {
|
|
696
|
-
rightOperand = filterExpression[key1];
|
|
697
|
-
leftOperand = filterExpression[key2];
|
|
698
|
-
}
|
|
699
|
-
filterChain.push({
|
|
700
|
-
pathExpression: filterPath,
|
|
701
|
-
rightOperand,
|
|
702
|
-
leftOperand,
|
|
703
|
-
leftOperator: inferOperatorFromFilter(leftFilterOperator),
|
|
704
|
-
rightOperator: inferOperatorFromFilter(rightFilterOperator),
|
|
705
|
-
type: _type,
|
|
706
|
-
pad: _type === "number" ? { fillString: "0", maxLength: DEFAULT_NUMERIC_LPAD } : void 0
|
|
707
|
-
});
|
|
708
|
-
} else {
|
|
709
|
-
throw new Error(`Filter on field '${filterPath}' has invalid combination of conditions: '${key1}, ${key2}'`);
|
|
710
|
-
}
|
|
711
|
-
}
|
|
712
|
-
}
|
|
713
|
-
return filterChain;
|
|
714
|
-
};
|
|
715
|
-
var makeFilter = ({
|
|
716
|
-
filterChain
|
|
717
|
-
}) => {
|
|
718
|
-
return (values) => {
|
|
719
|
-
for (const filter of filterChain) {
|
|
720
|
-
const dataType = filter.type;
|
|
721
|
-
const resolvedValues = (0, import_jsonpath_plus.JSONPath)({
|
|
722
|
-
path: filter.pathExpression,
|
|
723
|
-
json: values
|
|
724
|
-
});
|
|
725
|
-
if (!resolvedValues || !resolvedValues.length) {
|
|
726
|
-
return false;
|
|
727
|
-
}
|
|
728
|
-
let operands;
|
|
729
|
-
if (dataType === "string" || dataType === "reference") {
|
|
730
|
-
operands = resolvedValues;
|
|
731
|
-
} else if (dataType === "number") {
|
|
732
|
-
operands = resolvedValues.map((resolvedValue) => Number(resolvedValue));
|
|
733
|
-
} else if (dataType === "datetime") {
|
|
734
|
-
operands = resolvedValues.map((resolvedValue) => {
|
|
735
|
-
const coerced = new Date(resolvedValue).getTime();
|
|
736
|
-
return isNaN(coerced) ? Number(resolvedValue) : coerced;
|
|
737
|
-
});
|
|
738
|
-
} else if (dataType === "boolean") {
|
|
739
|
-
operands = resolvedValues.map((resolvedValue) => typeof resolvedValue === "boolean" && resolvedValue || resolvedValue === "true" || resolvedValue === "1");
|
|
740
|
-
} else {
|
|
741
|
-
throw new Error(`Unexpected datatype ${dataType}`);
|
|
742
|
-
}
|
|
743
|
-
const { operator } = filter;
|
|
744
|
-
let matches = false;
|
|
745
|
-
if (operator) {
|
|
746
|
-
switch (operator) {
|
|
747
|
-
case OP.EQ:
|
|
748
|
-
if (operands.findIndex((operand) => operand === filter.rightOperand) >= 0) {
|
|
749
|
-
matches = true;
|
|
750
|
-
}
|
|
751
|
-
break;
|
|
752
|
-
case OP.GT:
|
|
753
|
-
for (const operand of operands) {
|
|
754
|
-
if (operand > filter.rightOperand) {
|
|
755
|
-
matches = true;
|
|
756
|
-
break;
|
|
757
|
-
}
|
|
758
|
-
}
|
|
759
|
-
break;
|
|
760
|
-
case OP.LT:
|
|
761
|
-
for (const operand of operands) {
|
|
762
|
-
if (operand < filter.rightOperand) {
|
|
763
|
-
matches = true;
|
|
764
|
-
break;
|
|
765
|
-
}
|
|
766
|
-
}
|
|
767
|
-
break;
|
|
768
|
-
case OP.GTE:
|
|
769
|
-
for (const operand of operands) {
|
|
770
|
-
if (operand >= filter.rightOperand) {
|
|
771
|
-
matches = true;
|
|
772
|
-
break;
|
|
773
|
-
}
|
|
774
|
-
}
|
|
775
|
-
break;
|
|
776
|
-
case OP.LTE:
|
|
777
|
-
for (const operand of operands) {
|
|
778
|
-
if (operand <= filter.rightOperand) {
|
|
779
|
-
matches = true;
|
|
780
|
-
break;
|
|
781
|
-
}
|
|
782
|
-
}
|
|
783
|
-
break;
|
|
784
|
-
case OP.IN:
|
|
785
|
-
for (const operand of operands) {
|
|
786
|
-
if (filter.rightOperand.indexOf(operand) >= 0) {
|
|
787
|
-
matches = true;
|
|
788
|
-
break;
|
|
789
|
-
}
|
|
790
|
-
}
|
|
791
|
-
break;
|
|
792
|
-
case OP.STARTS_WITH:
|
|
793
|
-
for (const operand of operands) {
|
|
794
|
-
if (operand.startsWith(filter.rightOperand)) {
|
|
795
|
-
matches = true;
|
|
796
|
-
break;
|
|
797
|
-
}
|
|
798
|
-
}
|
|
799
|
-
break;
|
|
800
|
-
default:
|
|
801
|
-
throw new Error(`unexpected operator ${operator}`);
|
|
802
|
-
}
|
|
803
|
-
} else {
|
|
804
|
-
const { rightOperator, leftOperator, rightOperand, leftOperand } = filter;
|
|
805
|
-
for (const operand of operands) {
|
|
806
|
-
let rightMatches = false;
|
|
807
|
-
let leftMatches = false;
|
|
808
|
-
if (rightOperator === OP.LTE && operand <= rightOperand) {
|
|
809
|
-
rightMatches = true;
|
|
810
|
-
} else if (rightOperator === OP.LT && operand < rightOperand) {
|
|
811
|
-
rightMatches = true;
|
|
812
|
-
}
|
|
813
|
-
if (leftOperator === OP.GTE && operand >= leftOperand) {
|
|
814
|
-
leftMatches = true;
|
|
815
|
-
} else if (leftOperator === OP.GT && operand > leftOperand) {
|
|
816
|
-
leftMatches = true;
|
|
817
|
-
}
|
|
818
|
-
if (rightMatches && leftMatches) {
|
|
819
|
-
matches = true;
|
|
820
|
-
break;
|
|
821
|
-
}
|
|
822
|
-
}
|
|
823
|
-
}
|
|
824
|
-
if (!matches) {
|
|
825
|
-
return false;
|
|
826
|
-
}
|
|
827
|
-
}
|
|
828
|
-
return true;
|
|
829
|
-
};
|
|
830
|
-
};
|
|
831
|
-
var makeStringEscaper = (regex, replacement) => {
|
|
832
|
-
return (input) => {
|
|
833
|
-
if (Array.isArray(input)) {
|
|
834
|
-
return input.map((val) => val.replace(regex, replacement));
|
|
835
|
-
} else {
|
|
836
|
-
return input.replace(regex, replacement);
|
|
837
|
-
}
|
|
838
|
-
};
|
|
839
|
-
};
|
|
840
|
-
var applyPadding = (input, pad) => {
|
|
841
|
-
if (pad) {
|
|
842
|
-
if (Array.isArray(input)) {
|
|
843
|
-
return input.map((val) => String(val).padStart(pad.maxLength, pad.fillString));
|
|
844
|
-
} else {
|
|
845
|
-
return String(input).padStart(pad.maxLength, pad.fillString);
|
|
846
|
-
}
|
|
847
|
-
}
|
|
848
|
-
return input;
|
|
849
|
-
};
|
|
850
|
-
var coerceFilterChainOperands = (filterChain, stringEscaper) => {
|
|
851
|
-
const result = [];
|
|
852
|
-
if (filterChain.length) {
|
|
853
|
-
for (const filter of filterChain) {
|
|
854
|
-
const dataType = filter.type;
|
|
855
|
-
if (dataType === "datetime") {
|
|
856
|
-
if (filter.leftOperand !== void 0) {
|
|
857
|
-
result.push(__spreadProps(__spreadValues({}, filter), {
|
|
858
|
-
rightOperand: new Date(filter.rightOperand).getTime(),
|
|
859
|
-
leftOperand: new Date(filter.leftOperand).getTime()
|
|
860
|
-
}));
|
|
861
|
-
} else {
|
|
862
|
-
if (Array.isArray(filter.rightOperand)) {
|
|
863
|
-
result.push(__spreadProps(__spreadValues({}, filter), {
|
|
864
|
-
rightOperand: filter.rightOperand.map((operand) => new Date(operand).getTime())
|
|
865
|
-
}));
|
|
866
|
-
} else {
|
|
867
|
-
result.push(__spreadProps(__spreadValues({}, filter), {
|
|
868
|
-
rightOperand: new Date(filter.rightOperand).getTime()
|
|
869
|
-
}));
|
|
870
|
-
}
|
|
871
|
-
}
|
|
872
|
-
} else if (dataType === "string") {
|
|
873
|
-
if (filter.leftOperand !== void 0) {
|
|
874
|
-
result.push(__spreadProps(__spreadValues({}, filter), {
|
|
875
|
-
rightOperand: applyPadding(stringEscaper(filter.rightOperand), filter.pad),
|
|
876
|
-
leftOperand: applyPadding(stringEscaper(filter.leftOperand), filter.pad)
|
|
877
|
-
}));
|
|
878
|
-
} else {
|
|
879
|
-
result.push(__spreadProps(__spreadValues({}, filter), {
|
|
880
|
-
rightOperand: applyPadding(stringEscaper(filter.rightOperand), filter.pad)
|
|
881
|
-
}));
|
|
882
|
-
}
|
|
883
|
-
} else {
|
|
884
|
-
result.push(__spreadValues({}, filter));
|
|
885
|
-
}
|
|
886
|
-
}
|
|
887
|
-
}
|
|
888
|
-
return result;
|
|
889
|
-
};
|
|
890
|
-
var makeFilterSuffixes = (filterChain, index) => {
|
|
891
|
-
if (filterChain && filterChain.length) {
|
|
892
|
-
const indexFields = index.fields.map((field) => field.name);
|
|
893
|
-
const orderedFilterChain = [];
|
|
894
|
-
for (const filter of filterChain) {
|
|
895
|
-
const idx = indexFields.indexOf(filter.pathExpression);
|
|
896
|
-
if (idx === -1) {
|
|
897
|
-
return;
|
|
898
|
-
}
|
|
899
|
-
if (filter.operator && filter.operator === OP.IN) {
|
|
900
|
-
return;
|
|
901
|
-
}
|
|
902
|
-
orderedFilterChain[idx] = filter;
|
|
903
|
-
}
|
|
904
|
-
const baseFragments = [];
|
|
905
|
-
let rightSuffix;
|
|
906
|
-
let leftSuffix;
|
|
907
|
-
let ternaryFilter = false;
|
|
908
|
-
if (orderedFilterChain[filterChain.length - 1] && !orderedFilterChain[filterChain.length - 1].operator) {
|
|
909
|
-
ternaryFilter = true;
|
|
910
|
-
}
|
|
911
|
-
for (let i = 0; i < orderedFilterChain.length; i++) {
|
|
912
|
-
const filter = orderedFilterChain[i];
|
|
913
|
-
if (!filter) {
|
|
914
|
-
return;
|
|
915
|
-
}
|
|
916
|
-
if (Number(i) < indexFields.length - 1) {
|
|
917
|
-
if (!filter.operator) {
|
|
918
|
-
return;
|
|
919
|
-
}
|
|
920
|
-
const binaryFilter = filter;
|
|
921
|
-
if (binaryFilter.operator !== OP.EQ) {
|
|
922
|
-
return;
|
|
923
|
-
}
|
|
924
|
-
baseFragments.push(applyPadding(orderedFilterChain[i].rightOperand, orderedFilterChain[i].pad));
|
|
925
|
-
} else {
|
|
926
|
-
if (ternaryFilter) {
|
|
927
|
-
leftSuffix = applyPadding(orderedFilterChain[i].leftOperand, orderedFilterChain[i].pad);
|
|
928
|
-
rightSuffix = applyPadding(orderedFilterChain[i].rightOperand, orderedFilterChain[i].pad);
|
|
929
|
-
} else {
|
|
930
|
-
const op = orderedFilterChain[i].operator;
|
|
931
|
-
const operand = applyPadding(orderedFilterChain[i].rightOperand, orderedFilterChain[i].pad);
|
|
932
|
-
if (op === OP.LT || op === OP.LTE) {
|
|
933
|
-
rightSuffix = operand;
|
|
934
|
-
} else if (op === OP.GT || op === OP.GTE) {
|
|
935
|
-
leftSuffix = operand;
|
|
936
|
-
} else {
|
|
937
|
-
rightSuffix = operand;
|
|
938
|
-
leftSuffix = operand;
|
|
939
|
-
}
|
|
940
|
-
}
|
|
941
|
-
}
|
|
942
|
-
}
|
|
943
|
-
return {
|
|
944
|
-
left: leftSuffix && [...baseFragments, leftSuffix].join(INDEX_KEY_FIELD_SEPARATOR) || void 0,
|
|
945
|
-
right: rightSuffix && [...baseFragments, rightSuffix].join(INDEX_KEY_FIELD_SEPARATOR) || void 0
|
|
946
|
-
};
|
|
947
|
-
} else {
|
|
948
|
-
return {};
|
|
949
|
-
}
|
|
950
|
-
};
|
|
951
|
-
var makeKeyForField = (definition, data, stringEscaper, maxStringLength = 100) => {
|
|
952
|
-
const valueParts = [];
|
|
953
|
-
for (const field of definition.fields) {
|
|
954
|
-
if (field.name in data && data[field.name] !== void 0 && data[field.name] !== null) {
|
|
955
|
-
const rawValue = data[field.name];
|
|
956
|
-
const resolvedValue = String(field.type === "datetime" ? new Date(rawValue).getTime() : field.type === "string" ? stringEscaper(rawValue) : rawValue).substring(0, maxStringLength);
|
|
957
|
-
valueParts.push(applyPadding(resolvedValue, field.pad));
|
|
958
|
-
} else {
|
|
959
|
-
return null;
|
|
960
|
-
}
|
|
961
|
-
}
|
|
962
|
-
return valueParts.join(INDEX_KEY_FIELD_SEPARATOR);
|
|
963
|
-
};
|
|
964
|
-
|
|
965
|
-
// src/database/store/level.ts
|
|
966
|
-
var import_path4 = __toModule(require("path"));
|
|
967
|
-
var import_level = __toModule(require("level"));
|
|
968
|
-
var import_levelup = __toModule(require("levelup"));
|
|
969
|
-
var import_memdown = __toModule(require("memdown"));
|
|
970
|
-
var import_encoding_down = __toModule(require("encoding-down"));
|
|
971
|
-
var defaultPrefix = "_ROOT_";
|
|
972
|
-
var escapeStr = makeStringEscaper(new RegExp(INDEX_KEY_FIELD_SEPARATOR, "gm"), encodeURIComponent(INDEX_KEY_FIELD_SEPARATOR));
|
|
973
|
-
var LevelStore = class {
|
|
974
|
-
constructor(rootPath, useMemory = false) {
|
|
975
|
-
this.rootPath = rootPath || "";
|
|
976
|
-
this.useMemory = useMemory;
|
|
977
|
-
if (useMemory) {
|
|
978
|
-
this.db = (0, import_levelup.default)((0, import_encoding_down.default)((0, import_memdown.default)(), { valueEncoding: "json" }));
|
|
979
|
-
} else {
|
|
980
|
-
this.db = (0, import_level.default)(import_path4.default.join(rootPath, ".tina/__generated__/db"), {
|
|
981
|
-
valueEncoding: "json"
|
|
982
|
-
});
|
|
983
|
-
}
|
|
984
|
-
}
|
|
985
|
-
async query(queryOptions) {
|
|
986
|
-
var _b;
|
|
987
|
-
const _a = queryOptions, {
|
|
988
|
-
filterChain: rawFilterChain,
|
|
989
|
-
sort = DEFAULT_COLLECTION_SORT_KEY,
|
|
990
|
-
collection,
|
|
991
|
-
indexDefinitions,
|
|
992
|
-
limit = 50
|
|
993
|
-
} = _a, query = __objRest(_a, [
|
|
994
|
-
"filterChain",
|
|
995
|
-
"sort",
|
|
996
|
-
"collection",
|
|
997
|
-
"indexDefinitions",
|
|
998
|
-
"limit"
|
|
999
|
-
]);
|
|
1000
|
-
const filterChain = coerceFilterChainOperands(rawFilterChain, escapeStr);
|
|
1001
|
-
const indexDefinition = sort && (indexDefinitions == null ? void 0 : indexDefinitions[sort]);
|
|
1002
|
-
const filterSuffixes = indexDefinition && makeFilterSuffixes(filterChain, indexDefinition);
|
|
1003
|
-
const indexPrefix = indexDefinition ? `${collection}${INDEX_KEY_FIELD_SEPARATOR}${sort}` : `${defaultPrefix}${INDEX_KEY_FIELD_SEPARATOR}`;
|
|
1004
|
-
if (!query.gt && !query.gte) {
|
|
1005
|
-
query.gte = (filterSuffixes == null ? void 0 : filterSuffixes.left) ? `${indexPrefix}${INDEX_KEY_FIELD_SEPARATOR}${filterSuffixes.left}` : indexPrefix;
|
|
1006
|
-
}
|
|
1007
|
-
if (!query.lt && !query.lte) {
|
|
1008
|
-
query.lte = (filterSuffixes == null ? void 0 : filterSuffixes.right) ? `${indexPrefix}${INDEX_KEY_FIELD_SEPARATOR}${filterSuffixes.right}\xFF` : `${indexPrefix}\xFF`;
|
|
1009
|
-
}
|
|
1010
|
-
let edges = [];
|
|
1011
|
-
let startKey = "";
|
|
1012
|
-
let endKey = "";
|
|
1013
|
-
let hasPreviousPage = false;
|
|
1014
|
-
let hasNextPage = false;
|
|
1015
|
-
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;
|
|
1016
|
-
const valuesRegex = indexDefinition ? new RegExp(`^${indexPrefix}${fieldsPattern}(?<_filepath_>.+)`) : new RegExp(`^${indexPrefix}(?<_filepath_>.+)`);
|
|
1017
|
-
const itemFilter = makeFilter({ filterChain });
|
|
1018
|
-
for await (const [key, value] of this.db.iterator(query)) {
|
|
1019
|
-
const matcher = valuesRegex.exec(key);
|
|
1020
|
-
if (!matcher || indexDefinition && matcher.length !== indexDefinition.fields.length + 2) {
|
|
1021
|
-
continue;
|
|
1022
|
-
}
|
|
1023
|
-
const filepath = matcher.groups["_filepath_"];
|
|
1024
|
-
if (!itemFilter(filterSuffixes ? matcher.groups : indexDefinition ? await this.db.get(`${defaultPrefix}${INDEX_KEY_FIELD_SEPARATOR}${filepath}`) : value)) {
|
|
1025
|
-
continue;
|
|
1026
|
-
}
|
|
1027
|
-
if (limit !== -1 && edges.length >= limit) {
|
|
1028
|
-
if (query.reverse) {
|
|
1029
|
-
hasPreviousPage = true;
|
|
1030
|
-
} else {
|
|
1031
|
-
hasNextPage = true;
|
|
1032
|
-
}
|
|
1033
|
-
break;
|
|
1034
|
-
}
|
|
1035
|
-
startKey = startKey || key || "";
|
|
1036
|
-
endKey = key || "";
|
|
1037
|
-
edges = [...edges, { cursor: key, path: filepath }];
|
|
1038
|
-
}
|
|
1039
|
-
return {
|
|
1040
|
-
edges,
|
|
1041
|
-
pageInfo: {
|
|
1042
|
-
hasPreviousPage,
|
|
1043
|
-
hasNextPage,
|
|
1044
|
-
startCursor: startKey,
|
|
1045
|
-
endCursor: endKey
|
|
1046
|
-
}
|
|
1047
|
-
};
|
|
1048
|
-
}
|
|
1049
|
-
async seed(filepath, data, options) {
|
|
1050
|
-
await this.put(filepath, data, __spreadValues({
|
|
1051
|
-
keepTemplateKey: false,
|
|
1052
|
-
seed: true
|
|
1053
|
-
}, options));
|
|
1054
|
-
}
|
|
1055
|
-
supportsSeeding() {
|
|
1056
|
-
return true;
|
|
1057
|
-
}
|
|
1058
|
-
supportsIndexing() {
|
|
1059
|
-
return true;
|
|
1060
|
-
}
|
|
1061
|
-
async delete(filepath, options) {
|
|
1062
|
-
const data = await this.db.get(`${defaultPrefix}${INDEX_KEY_FIELD_SEPARATOR}${filepath}`);
|
|
1063
|
-
if (!data) {
|
|
1064
|
-
return;
|
|
1065
|
-
}
|
|
1066
|
-
if (options == null ? void 0 : options.indexDefinitions) {
|
|
1067
|
-
for (const [sort, definition] of Object.entries(options.indexDefinitions)) {
|
|
1068
|
-
const indexedValue = makeKeyForField(definition, data, escapeStr);
|
|
1069
|
-
let indexKey;
|
|
1070
|
-
if (sort === DEFAULT_COLLECTION_SORT_KEY) {
|
|
1071
|
-
indexKey = `${options.collection}${INDEX_KEY_FIELD_SEPARATOR}${sort}${INDEX_KEY_FIELD_SEPARATOR}${filepath}`;
|
|
1072
|
-
} else {
|
|
1073
|
-
indexKey = indexedValue ? `${options.collection}${INDEX_KEY_FIELD_SEPARATOR}${sort}${INDEX_KEY_FIELD_SEPARATOR}${indexedValue}${INDEX_KEY_FIELD_SEPARATOR}${filepath}` : null;
|
|
1074
|
-
}
|
|
1075
|
-
if (indexKey) {
|
|
1076
|
-
await this.db.del(indexKey);
|
|
1077
|
-
}
|
|
1078
|
-
}
|
|
1079
|
-
}
|
|
1080
|
-
await this.db.del(`${defaultPrefix}${INDEX_KEY_FIELD_SEPARATOR}${filepath}`);
|
|
1081
|
-
}
|
|
1082
|
-
async print() {
|
|
1083
|
-
this.db.createReadStream().on("data", function(data) {
|
|
1084
|
-
console.log(data.key, "=", data.value);
|
|
1085
|
-
}).on("error", function(err) {
|
|
1086
|
-
console.log("Oh my!", err);
|
|
1087
|
-
}).on("close", function() {
|
|
1088
|
-
console.log("Stream closed");
|
|
1089
|
-
}).on("end", function() {
|
|
1090
|
-
console.log("Stream ended");
|
|
1091
|
-
});
|
|
1092
|
-
}
|
|
1093
|
-
async open() {
|
|
1094
|
-
await this.db.open();
|
|
1095
|
-
}
|
|
1096
|
-
async clear() {
|
|
1097
|
-
await this.db.clear();
|
|
1098
|
-
}
|
|
1099
|
-
async glob(pattern, callback) {
|
|
1100
|
-
const strings = [];
|
|
1101
|
-
const p = new Promise((resolve, reject) => {
|
|
1102
|
-
this.db.createKeyStream({
|
|
1103
|
-
gte: `${defaultPrefix}${INDEX_KEY_FIELD_SEPARATOR}${pattern}`,
|
|
1104
|
-
lte: `${defaultPrefix}${INDEX_KEY_FIELD_SEPARATOR}${pattern}\xFF`
|
|
1105
|
-
}).on("data", (data) => {
|
|
1106
|
-
strings.push(data.split(`${defaultPrefix}${INDEX_KEY_FIELD_SEPARATOR}`)[1]);
|
|
1107
|
-
}).on("error", (message) => {
|
|
1108
|
-
reject(message);
|
|
1109
|
-
}).on("end", function() {
|
|
1110
|
-
resolve();
|
|
1111
|
-
});
|
|
1112
|
-
});
|
|
1113
|
-
await p;
|
|
1114
|
-
if (callback) {
|
|
1115
|
-
return sequential(strings, async (item) => {
|
|
1116
|
-
return callback(item);
|
|
1117
|
-
});
|
|
1118
|
-
} else {
|
|
1119
|
-
return strings;
|
|
1120
|
-
}
|
|
1121
|
-
}
|
|
1122
|
-
async get(filepath) {
|
|
1123
|
-
try {
|
|
1124
|
-
return await this.db.get(`${defaultPrefix}${INDEX_KEY_FIELD_SEPARATOR}${filepath}`);
|
|
1125
|
-
} catch (e) {
|
|
1126
|
-
return void 0;
|
|
1127
|
-
}
|
|
1128
|
-
}
|
|
1129
|
-
async close() {
|
|
1130
|
-
await this.db.close();
|
|
1131
|
-
}
|
|
1132
|
-
async put(filepath, data, options) {
|
|
1133
|
-
let existingData;
|
|
1134
|
-
try {
|
|
1135
|
-
existingData = options && !options.seed ? await this.db.get(`${defaultPrefix}${INDEX_KEY_FIELD_SEPARATOR}${filepath}`) : null;
|
|
1136
|
-
} catch (err) {
|
|
1137
|
-
if (!err.notFound) {
|
|
1138
|
-
throw err;
|
|
1139
|
-
}
|
|
1140
|
-
}
|
|
1141
|
-
await this.db.put(`${defaultPrefix}${INDEX_KEY_FIELD_SEPARATOR}${filepath}`, data);
|
|
1142
|
-
if (options == null ? void 0 : options.indexDefinitions) {
|
|
1143
|
-
for (const [sort, definition] of Object.entries(options.indexDefinitions)) {
|
|
1144
|
-
const indexedValue = makeKeyForField(definition, data, escapeStr);
|
|
1145
|
-
const existingIndexedValue = existingData ? makeKeyForField(definition, existingData, escapeStr) : null;
|
|
1146
|
-
let indexKey;
|
|
1147
|
-
let existingIndexKey = null;
|
|
1148
|
-
if (sort === DEFAULT_COLLECTION_SORT_KEY) {
|
|
1149
|
-
indexKey = `${options.collection}${INDEX_KEY_FIELD_SEPARATOR}${sort}${INDEX_KEY_FIELD_SEPARATOR}${filepath}`;
|
|
1150
|
-
existingIndexKey = indexKey;
|
|
1151
|
-
} else {
|
|
1152
|
-
indexKey = indexedValue ? `${options.collection}${INDEX_KEY_FIELD_SEPARATOR}${sort}${INDEX_KEY_FIELD_SEPARATOR}${indexedValue}${INDEX_KEY_FIELD_SEPARATOR}${filepath}` : null;
|
|
1153
|
-
existingIndexKey = existingIndexedValue ? `${options.collection}${INDEX_KEY_FIELD_SEPARATOR}${sort}${INDEX_KEY_FIELD_SEPARATOR}${existingIndexedValue}${INDEX_KEY_FIELD_SEPARATOR}${filepath}` : null;
|
|
1154
|
-
}
|
|
1155
|
-
if (indexKey) {
|
|
1156
|
-
if (existingIndexKey && indexKey != existingIndexKey) {
|
|
1157
|
-
await this.db.del(existingIndexKey);
|
|
1158
|
-
}
|
|
1159
|
-
await this.db.put(indexKey, "");
|
|
1160
|
-
}
|
|
1161
|
-
}
|
|
1162
|
-
}
|
|
1163
|
-
}
|
|
1164
|
-
};
|
|
35
|
+
var import_graphql = __toModule(require("@tinacms/graphql"));
|
|
1165
36
|
// Annotate the CommonJS export names for ESM import in node:
|
|
1166
37
|
0 && (module.exports = {
|
|
1167
38
|
AuditFileSystemBridge,
|
|
1168
|
-
|
|
1169
|
-
DEFAULT_COLLECTION_SORT_KEY,
|
|
1170
|
-
DEFAULT_NUMERIC_LPAD,
|
|
39
|
+
Database,
|
|
1171
40
|
FilesystemBridge,
|
|
1172
|
-
FilesystemStore,
|
|
1173
|
-
INDEX_KEY_FIELD_SEPARATOR,
|
|
1174
41
|
IsomorphicBridge,
|
|
1175
|
-
|
|
1176
|
-
|
|
1177
|
-
|
|
1178
|
-
btoa,
|
|
1179
|
-
coerceFilterChainOperands,
|
|
1180
|
-
makeFilter,
|
|
1181
|
-
makeFilterChain,
|
|
1182
|
-
makeFilterSuffixes,
|
|
1183
|
-
makeKeyForField,
|
|
1184
|
-
makeStringEscaper
|
|
42
|
+
TinaLevelClient,
|
|
43
|
+
createDatabase,
|
|
44
|
+
resolve
|
|
1185
45
|
});
|