@tinacms/graphql 1.5.13 → 1.5.15
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/database/datalayer.d.ts +5 -1
- package/dist/database/index.d.ts +2 -0
- package/dist/index.d.ts +1 -32
- package/dist/index.js +369 -120
- package/dist/index.mjs +363 -118
- package/dist/resolver/index.d.ts +12 -1
- package/package.json +5 -5
package/dist/index.js
CHANGED
|
@@ -3090,7 +3090,7 @@ var validateField = async (field) => {
|
|
|
3090
3090
|
// package.json
|
|
3091
3091
|
var package_default = {
|
|
3092
3092
|
name: "@tinacms/graphql",
|
|
3093
|
-
version: "1.5.
|
|
3093
|
+
version: "1.5.15",
|
|
3094
3094
|
main: "dist/index.js",
|
|
3095
3095
|
module: "dist/index.mjs",
|
|
3096
3096
|
typings: "dist/index.d.ts",
|
|
@@ -3332,7 +3332,9 @@ var _buildSchema = async (builder, tinaSchema) => {
|
|
|
3332
3332
|
await builder.buildCreateCollectionFolderMutation()
|
|
3333
3333
|
);
|
|
3334
3334
|
await sequential(collections, async (collection) => {
|
|
3335
|
-
queryTypeDefinitionFields.push(
|
|
3335
|
+
queryTypeDefinitionFields.push(
|
|
3336
|
+
await builder.collectionDocument(collection)
|
|
3337
|
+
);
|
|
3336
3338
|
if (collection.isAuthCollection) {
|
|
3337
3339
|
queryTypeDefinitionFields.push(
|
|
3338
3340
|
await builder.authenticationCollectionDocument(collection)
|
|
@@ -3648,7 +3650,9 @@ var LevelProxyHandler = {
|
|
|
3648
3650
|
throw new Error(`The property, ${property.toString()}, doesn't exist`);
|
|
3649
3651
|
}
|
|
3650
3652
|
if (typeof target[property] !== "function") {
|
|
3651
|
-
throw new Error(
|
|
3653
|
+
throw new Error(
|
|
3654
|
+
`The property, ${property.toString()}, is not a function`
|
|
3655
|
+
);
|
|
3652
3656
|
}
|
|
3653
3657
|
if (property === "get") {
|
|
3654
3658
|
return async (...args) => {
|
|
@@ -4035,6 +4039,9 @@ var loadAndParseWithAliases = async (bridge, filepath, collection, templateInfo)
|
|
|
4035
4039
|
|
|
4036
4040
|
// src/database/datalayer.ts
|
|
4037
4041
|
var DEFAULT_COLLECTION_SORT_KEY = "__filepath__";
|
|
4042
|
+
var REFS_COLLECTIONS_SORT_KEY = "__refs__";
|
|
4043
|
+
var REFS_REFERENCE_FIELD = "__tina_ref__";
|
|
4044
|
+
var REFS_PATH_FIELD = "__tina_ref_path__";
|
|
4038
4045
|
var DEFAULT_NUMERIC_LPAD = 4;
|
|
4039
4046
|
var applyPadding = (input, pad) => {
|
|
4040
4047
|
if (pad) {
|
|
@@ -4608,6 +4615,57 @@ var makeIndexOpsForDocument = (filepath, collection, indexDefinitions, data, opT
|
|
|
4608
4615
|
}
|
|
4609
4616
|
return result;
|
|
4610
4617
|
};
|
|
4618
|
+
var makeRefOpsForDocument = (filepath, collection, references, data, opType, level) => {
|
|
4619
|
+
const result = [];
|
|
4620
|
+
if (collection) {
|
|
4621
|
+
for (const [c, referencePaths] of Object.entries(references || {})) {
|
|
4622
|
+
if (!referencePaths.length) {
|
|
4623
|
+
continue;
|
|
4624
|
+
}
|
|
4625
|
+
const collectionSublevel = level.sublevel(c, SUBLEVEL_OPTIONS);
|
|
4626
|
+
const refSublevel = collectionSublevel.sublevel(
|
|
4627
|
+
REFS_COLLECTIONS_SORT_KEY,
|
|
4628
|
+
SUBLEVEL_OPTIONS
|
|
4629
|
+
);
|
|
4630
|
+
const references2 = {};
|
|
4631
|
+
for (const path7 of referencePaths) {
|
|
4632
|
+
const ref = (0, import_jsonpath_plus.JSONPath)({ path: path7, json: data });
|
|
4633
|
+
if (!ref) {
|
|
4634
|
+
continue;
|
|
4635
|
+
}
|
|
4636
|
+
if (Array.isArray(ref)) {
|
|
4637
|
+
for (const r of ref) {
|
|
4638
|
+
if (!r) {
|
|
4639
|
+
continue;
|
|
4640
|
+
}
|
|
4641
|
+
if (references2[r]) {
|
|
4642
|
+
references2[r].push(path7);
|
|
4643
|
+
} else {
|
|
4644
|
+
references2[r] = [path7];
|
|
4645
|
+
}
|
|
4646
|
+
}
|
|
4647
|
+
} else {
|
|
4648
|
+
if (references2[ref]) {
|
|
4649
|
+
references2[ref].push(path7);
|
|
4650
|
+
} else {
|
|
4651
|
+
references2[ref] = [path7];
|
|
4652
|
+
}
|
|
4653
|
+
}
|
|
4654
|
+
}
|
|
4655
|
+
for (const ref of Object.keys(references2)) {
|
|
4656
|
+
for (const path7 of references2[ref]) {
|
|
4657
|
+
result.push({
|
|
4658
|
+
type: opType,
|
|
4659
|
+
key: `${ref}${INDEX_KEY_FIELD_SEPARATOR}${path7}${INDEX_KEY_FIELD_SEPARATOR}${filepath}`,
|
|
4660
|
+
sublevel: refSublevel,
|
|
4661
|
+
value: opType === "put" ? {} : void 0
|
|
4662
|
+
});
|
|
4663
|
+
}
|
|
4664
|
+
}
|
|
4665
|
+
}
|
|
4666
|
+
}
|
|
4667
|
+
return result;
|
|
4668
|
+
};
|
|
4611
4669
|
var makeStringEscaper = (regex, replacement) => {
|
|
4612
4670
|
return (input) => {
|
|
4613
4671
|
if (Array.isArray(input)) {
|
|
@@ -4829,24 +4887,33 @@ var transformDocumentIntoPayload = async (fullPath, rawData, tinaSchema, config,
|
|
|
4829
4887
|
throw e;
|
|
4830
4888
|
}
|
|
4831
4889
|
};
|
|
4832
|
-
var updateObjectWithJsonPath = (obj, path7, newValue) => {
|
|
4890
|
+
var updateObjectWithJsonPath = (obj, path7, oldValue, newValue) => {
|
|
4891
|
+
let updated = false;
|
|
4833
4892
|
if (!path7.includes(".") && !path7.includes("[")) {
|
|
4834
|
-
if (path7 in obj) {
|
|
4893
|
+
if (path7 in obj && obj[path7] === oldValue) {
|
|
4835
4894
|
obj[path7] = newValue;
|
|
4895
|
+
updated = true;
|
|
4836
4896
|
}
|
|
4837
|
-
return obj;
|
|
4838
|
-
}
|
|
4839
|
-
const parentPath = path7.replace(/\.[
|
|
4840
|
-
const keyToUpdate = path7.match(/[
|
|
4841
|
-
const parents = (0, import_jsonpath_plus2.JSONPath)({
|
|
4897
|
+
return { object: obj, updated };
|
|
4898
|
+
}
|
|
4899
|
+
const parentPath = path7.replace(/\.[^.\[\]]+$/, "");
|
|
4900
|
+
const keyToUpdate = path7.match(/[^.\[\]]+$/)[0];
|
|
4901
|
+
const parents = (0, import_jsonpath_plus2.JSONPath)({
|
|
4902
|
+
path: parentPath,
|
|
4903
|
+
json: obj,
|
|
4904
|
+
resultType: "value"
|
|
4905
|
+
});
|
|
4842
4906
|
if (parents.length > 0) {
|
|
4843
4907
|
parents.forEach((parent) => {
|
|
4844
4908
|
if (parent && typeof parent === "object" && keyToUpdate in parent) {
|
|
4845
|
-
parent[keyToUpdate]
|
|
4909
|
+
if (parent[keyToUpdate] === oldValue) {
|
|
4910
|
+
parent[keyToUpdate] = newValue;
|
|
4911
|
+
updated = true;
|
|
4912
|
+
}
|
|
4846
4913
|
}
|
|
4847
4914
|
});
|
|
4848
4915
|
}
|
|
4849
|
-
return obj;
|
|
4916
|
+
return { object: obj, updated };
|
|
4850
4917
|
};
|
|
4851
4918
|
var Resolver = class {
|
|
4852
4919
|
constructor(init) {
|
|
@@ -4863,7 +4930,9 @@ var Resolver = class {
|
|
|
4863
4930
|
};
|
|
4864
4931
|
this.getRaw = async (fullPath) => {
|
|
4865
4932
|
if (typeof fullPath !== "string") {
|
|
4866
|
-
throw new Error(
|
|
4933
|
+
throw new Error(
|
|
4934
|
+
`fullPath must be of type string for getDocument request`
|
|
4935
|
+
);
|
|
4867
4936
|
}
|
|
4868
4937
|
return this.database.get(fullPath);
|
|
4869
4938
|
};
|
|
@@ -4892,7 +4961,9 @@ var Resolver = class {
|
|
|
4892
4961
|
};
|
|
4893
4962
|
this.getDocument = async (fullPath, opts = {}) => {
|
|
4894
4963
|
if (typeof fullPath !== "string") {
|
|
4895
|
-
throw new Error(
|
|
4964
|
+
throw new Error(
|
|
4965
|
+
`fullPath must be of type string for getDocument request`
|
|
4966
|
+
);
|
|
4896
4967
|
}
|
|
4897
4968
|
const rawData = await this.getRaw(fullPath);
|
|
4898
4969
|
const hasReferences = (opts == null ? void 0 : opts.checkReferences) ? await this.hasReferences(fullPath, opts.collection) : void 0;
|
|
@@ -4907,7 +4978,9 @@ var Resolver = class {
|
|
|
4907
4978
|
};
|
|
4908
4979
|
this.deleteDocument = async (fullPath) => {
|
|
4909
4980
|
if (typeof fullPath !== "string") {
|
|
4910
|
-
throw new Error(
|
|
4981
|
+
throw new Error(
|
|
4982
|
+
`fullPath must be of type string for getDocument request`
|
|
4983
|
+
);
|
|
4911
4984
|
}
|
|
4912
4985
|
await this.database.delete(fullPath);
|
|
4913
4986
|
};
|
|
@@ -5111,7 +5184,11 @@ var Resolver = class {
|
|
|
5111
5184
|
collection,
|
|
5112
5185
|
doc == null ? void 0 : doc._rawData
|
|
5113
5186
|
);
|
|
5114
|
-
await this.database.put(
|
|
5187
|
+
await this.database.put(
|
|
5188
|
+
realPath,
|
|
5189
|
+
{ ...oldDoc, ...params },
|
|
5190
|
+
collection.name
|
|
5191
|
+
);
|
|
5115
5192
|
return this.getDocument(realPath);
|
|
5116
5193
|
};
|
|
5117
5194
|
/**
|
|
@@ -5225,17 +5302,35 @@ var Resolver = class {
|
|
|
5225
5302
|
await this.deleteDocument(realPath);
|
|
5226
5303
|
if (await this.hasReferences(realPath, collection)) {
|
|
5227
5304
|
const collRefs = await this.findReferences(realPath, collection);
|
|
5228
|
-
for (const [collection2,
|
|
5229
|
-
for (const [
|
|
5230
|
-
|
|
5231
|
-
|
|
5232
|
-
|
|
5305
|
+
for (const [collection2, docsWithRefs] of Object.entries(collRefs)) {
|
|
5306
|
+
for (const [pathToDocWithRef, referencePaths] of Object.entries(
|
|
5307
|
+
docsWithRefs
|
|
5308
|
+
)) {
|
|
5309
|
+
let refDoc = await this.getRaw(pathToDocWithRef);
|
|
5310
|
+
let hasUpdate = false;
|
|
5311
|
+
for (const path7 of referencePaths) {
|
|
5312
|
+
const { object: object2, updated } = updateObjectWithJsonPath(
|
|
5233
5313
|
refDoc,
|
|
5234
|
-
|
|
5314
|
+
path7,
|
|
5315
|
+
realPath,
|
|
5235
5316
|
null
|
|
5236
5317
|
);
|
|
5318
|
+
refDoc = object2;
|
|
5319
|
+
hasUpdate = updated || hasUpdate;
|
|
5320
|
+
}
|
|
5321
|
+
if (hasUpdate) {
|
|
5322
|
+
const collectionWithRef = this.tinaSchema.getCollectionByFullPath(pathToDocWithRef);
|
|
5323
|
+
if (!collectionWithRef) {
|
|
5324
|
+
throw new Error(
|
|
5325
|
+
`Unable to find collection for ${pathToDocWithRef}`
|
|
5326
|
+
);
|
|
5327
|
+
}
|
|
5328
|
+
await this.database.put(
|
|
5329
|
+
pathToDocWithRef,
|
|
5330
|
+
refDoc,
|
|
5331
|
+
collectionWithRef.name
|
|
5332
|
+
);
|
|
5237
5333
|
}
|
|
5238
|
-
await this.database.put(refPath, refDoc, collection2);
|
|
5239
5334
|
}
|
|
5240
5335
|
}
|
|
5241
5336
|
}
|
|
@@ -5261,23 +5356,43 @@ var Resolver = class {
|
|
|
5261
5356
|
await this.database.put(newRealPath, doc._rawData, collection.name);
|
|
5262
5357
|
await this.deleteDocument(realPath);
|
|
5263
5358
|
const collRefs = await this.findReferences(realPath, collection);
|
|
5264
|
-
for (const [collection2,
|
|
5265
|
-
for (const [
|
|
5266
|
-
|
|
5267
|
-
|
|
5268
|
-
|
|
5269
|
-
|
|
5270
|
-
|
|
5359
|
+
for (const [collection2, docsWithRefs] of Object.entries(collRefs)) {
|
|
5360
|
+
for (const [pathToDocWithRef, referencePaths] of Object.entries(
|
|
5361
|
+
docsWithRefs
|
|
5362
|
+
)) {
|
|
5363
|
+
let docWithRef = await this.getRaw(pathToDocWithRef);
|
|
5364
|
+
let hasUpdate = false;
|
|
5365
|
+
for (const path7 of referencePaths) {
|
|
5366
|
+
const { object: object2, updated } = updateObjectWithJsonPath(
|
|
5367
|
+
docWithRef,
|
|
5368
|
+
path7,
|
|
5369
|
+
realPath,
|
|
5271
5370
|
newRealPath
|
|
5272
5371
|
);
|
|
5372
|
+
docWithRef = object2;
|
|
5373
|
+
hasUpdate = updated || hasUpdate;
|
|
5374
|
+
}
|
|
5375
|
+
if (hasUpdate) {
|
|
5376
|
+
const collectionWithRef = this.tinaSchema.getCollectionByFullPath(pathToDocWithRef);
|
|
5377
|
+
if (!collectionWithRef) {
|
|
5378
|
+
throw new Error(
|
|
5379
|
+
`Unable to find collection for ${pathToDocWithRef}`
|
|
5380
|
+
);
|
|
5381
|
+
}
|
|
5382
|
+
await this.database.put(
|
|
5383
|
+
pathToDocWithRef,
|
|
5384
|
+
docWithRef,
|
|
5385
|
+
collectionWithRef.name
|
|
5386
|
+
);
|
|
5273
5387
|
}
|
|
5274
|
-
await this.database.put(refPath, refDoc, collection2);
|
|
5275
5388
|
}
|
|
5276
5389
|
}
|
|
5277
5390
|
return this.getDocument(newRealPath);
|
|
5278
5391
|
}
|
|
5279
5392
|
if (alreadyExists === false) {
|
|
5280
|
-
throw new Error(
|
|
5393
|
+
throw new Error(
|
|
5394
|
+
`Unable to update document, ${realPath} does not exist`
|
|
5395
|
+
);
|
|
5281
5396
|
}
|
|
5282
5397
|
return this.updateResolveDocument({
|
|
5283
5398
|
collection,
|
|
@@ -5403,35 +5518,30 @@ var Resolver = class {
|
|
|
5403
5518
|
*/
|
|
5404
5519
|
this.hasReferences = async (id, c) => {
|
|
5405
5520
|
let count = 0;
|
|
5406
|
-
|
|
5407
|
-
|
|
5408
|
-
|
|
5409
|
-
|
|
5410
|
-
|
|
5411
|
-
|
|
5412
|
-
|
|
5413
|
-
|
|
5414
|
-
|
|
5415
|
-
|
|
5416
|
-
|
|
5417
|
-
|
|
5418
|
-
|
|
5419
|
-
|
|
5420
|
-
|
|
5421
|
-
|
|
5422
|
-
|
|
5423
|
-
|
|
5424
|
-
|
|
5425
|
-
|
|
5426
|
-
(refId) => {
|
|
5427
|
-
count++;
|
|
5428
|
-
return refId;
|
|
5429
|
-
}
|
|
5430
|
-
);
|
|
5431
|
-
if (count) {
|
|
5432
|
-
return true;
|
|
5433
|
-
}
|
|
5521
|
+
await this.database.query(
|
|
5522
|
+
{
|
|
5523
|
+
collection: c.name,
|
|
5524
|
+
filterChain: makeFilterChain({
|
|
5525
|
+
conditions: [
|
|
5526
|
+
{
|
|
5527
|
+
filterPath: REFS_REFERENCE_FIELD,
|
|
5528
|
+
filterExpression: {
|
|
5529
|
+
_type: "string",
|
|
5530
|
+
_list: false,
|
|
5531
|
+
eq: id
|
|
5532
|
+
}
|
|
5533
|
+
}
|
|
5534
|
+
]
|
|
5535
|
+
}),
|
|
5536
|
+
sort: REFS_COLLECTIONS_SORT_KEY
|
|
5537
|
+
},
|
|
5538
|
+
(refId) => {
|
|
5539
|
+
count++;
|
|
5540
|
+
return refId;
|
|
5434
5541
|
}
|
|
5542
|
+
);
|
|
5543
|
+
if (count) {
|
|
5544
|
+
return true;
|
|
5435
5545
|
}
|
|
5436
5546
|
return false;
|
|
5437
5547
|
};
|
|
@@ -5439,46 +5549,41 @@ var Resolver = class {
|
|
|
5439
5549
|
* Finds references to a document
|
|
5440
5550
|
* @param id the id of the document to find references to
|
|
5441
5551
|
* @param c the collection to find references in
|
|
5442
|
-
* @returns
|
|
5552
|
+
* @returns a map of references to the document
|
|
5443
5553
|
*/
|
|
5444
5554
|
this.findReferences = async (id, c) => {
|
|
5445
5555
|
const references = {};
|
|
5446
|
-
|
|
5447
|
-
|
|
5448
|
-
|
|
5449
|
-
|
|
5450
|
-
|
|
5451
|
-
|
|
5452
|
-
|
|
5453
|
-
|
|
5454
|
-
|
|
5455
|
-
|
|
5456
|
-
|
|
5457
|
-
|
|
5458
|
-
_list: false,
|
|
5459
|
-
eq: id
|
|
5460
|
-
}
|
|
5461
|
-
}
|
|
5462
|
-
]
|
|
5463
|
-
}),
|
|
5464
|
-
sort: ref.field.name
|
|
5465
|
-
},
|
|
5466
|
-
(refId) => {
|
|
5467
|
-
if (!references[collection]) {
|
|
5468
|
-
references[collection] = {};
|
|
5469
|
-
}
|
|
5470
|
-
if (!references[collection][refId]) {
|
|
5471
|
-
references[collection][refId] = [];
|
|
5556
|
+
await this.database.query(
|
|
5557
|
+
{
|
|
5558
|
+
collection: c.name,
|
|
5559
|
+
filterChain: makeFilterChain({
|
|
5560
|
+
conditions: [
|
|
5561
|
+
{
|
|
5562
|
+
filterPath: REFS_REFERENCE_FIELD,
|
|
5563
|
+
filterExpression: {
|
|
5564
|
+
_type: "string",
|
|
5565
|
+
_list: false,
|
|
5566
|
+
eq: id
|
|
5567
|
+
}
|
|
5472
5568
|
}
|
|
5473
|
-
|
|
5474
|
-
|
|
5475
|
-
|
|
5476
|
-
|
|
5477
|
-
|
|
5478
|
-
|
|
5479
|
-
|
|
5569
|
+
]
|
|
5570
|
+
}),
|
|
5571
|
+
sort: REFS_COLLECTIONS_SORT_KEY
|
|
5572
|
+
},
|
|
5573
|
+
(refId, rawItem) => {
|
|
5574
|
+
if (!references[c.name]) {
|
|
5575
|
+
references[c.name] = {};
|
|
5576
|
+
}
|
|
5577
|
+
if (!references[c.name][refId]) {
|
|
5578
|
+
references[c.name][refId] = [];
|
|
5579
|
+
}
|
|
5580
|
+
const referencePath = rawItem == null ? void 0 : rawItem[REFS_PATH_FIELD];
|
|
5581
|
+
if (referencePath) {
|
|
5582
|
+
references[c.name][refId].push(referencePath);
|
|
5583
|
+
}
|
|
5584
|
+
return refId;
|
|
5480
5585
|
}
|
|
5481
|
-
|
|
5586
|
+
);
|
|
5482
5587
|
return references;
|
|
5483
5588
|
};
|
|
5484
5589
|
this.buildFieldMutations = async (fieldParams, template, existingData) => {
|
|
@@ -6214,6 +6319,7 @@ var Database = class {
|
|
|
6214
6319
|
}
|
|
6215
6320
|
};
|
|
6216
6321
|
this.addPendingDocument = async (filepath, data) => {
|
|
6322
|
+
var _a;
|
|
6217
6323
|
await this.initLevel();
|
|
6218
6324
|
const dataFields = await this.formatBodyOnPayload(filepath, data);
|
|
6219
6325
|
const collection = await this.collectionForPath(filepath);
|
|
@@ -6227,6 +6333,7 @@ var Database = class {
|
|
|
6227
6333
|
);
|
|
6228
6334
|
const indexDefinitions = await this.getIndexDefinitions(this.contentLevel);
|
|
6229
6335
|
const collectionIndexDefinitions = indexDefinitions == null ? void 0 : indexDefinitions[collection.name];
|
|
6336
|
+
const collectionReferences = (_a = await this.getCollectionReferences()) == null ? void 0 : _a[collection.name];
|
|
6230
6337
|
const normalizedPath = (0, import_schema_tools4.normalizePath)(filepath);
|
|
6231
6338
|
if (!(collection == null ? void 0 : collection.isDetached)) {
|
|
6232
6339
|
if (this.bridge) {
|
|
@@ -6255,6 +6362,14 @@ var Database = class {
|
|
|
6255
6362
|
let delOps = [];
|
|
6256
6363
|
if (!isGitKeep(normalizedPath, collection)) {
|
|
6257
6364
|
putOps = [
|
|
6365
|
+
...makeRefOpsForDocument(
|
|
6366
|
+
normalizedPath,
|
|
6367
|
+
collection == null ? void 0 : collection.name,
|
|
6368
|
+
collectionReferences,
|
|
6369
|
+
dataFields,
|
|
6370
|
+
"put",
|
|
6371
|
+
level
|
|
6372
|
+
),
|
|
6258
6373
|
...makeIndexOpsForDocument(
|
|
6259
6374
|
normalizedPath,
|
|
6260
6375
|
collection == null ? void 0 : collection.name,
|
|
@@ -6278,6 +6393,14 @@ var Database = class {
|
|
|
6278
6393
|
SUBLEVEL_OPTIONS
|
|
6279
6394
|
).get(normalizedPath);
|
|
6280
6395
|
delOps = existingItem ? [
|
|
6396
|
+
...makeRefOpsForDocument(
|
|
6397
|
+
normalizedPath,
|
|
6398
|
+
collection == null ? void 0 : collection.name,
|
|
6399
|
+
collectionReferences,
|
|
6400
|
+
existingItem,
|
|
6401
|
+
"del",
|
|
6402
|
+
level
|
|
6403
|
+
),
|
|
6281
6404
|
...makeIndexOpsForDocument(
|
|
6282
6405
|
normalizedPath,
|
|
6283
6406
|
collection == null ? void 0 : collection.name,
|
|
@@ -6313,7 +6436,7 @@ var Database = class {
|
|
|
6313
6436
|
await level.batch(ops);
|
|
6314
6437
|
};
|
|
6315
6438
|
this.put = async (filepath, data, collectionName) => {
|
|
6316
|
-
var _a, _b;
|
|
6439
|
+
var _a, _b, _c;
|
|
6317
6440
|
await this.initLevel();
|
|
6318
6441
|
try {
|
|
6319
6442
|
if (SYSTEM_FILES.includes(filepath)) {
|
|
@@ -6326,13 +6449,14 @@ var Database = class {
|
|
|
6326
6449
|
);
|
|
6327
6450
|
collectionIndexDefinitions = indexDefinitions == null ? void 0 : indexDefinitions[collectionName];
|
|
6328
6451
|
}
|
|
6452
|
+
const collectionReferences = (_a = await this.getCollectionReferences()) == null ? void 0 : _a[collectionName];
|
|
6329
6453
|
const normalizedPath = (0, import_schema_tools4.normalizePath)(filepath);
|
|
6330
6454
|
const dataFields = await this.formatBodyOnPayload(filepath, data);
|
|
6331
6455
|
const collection = await this.collectionForPath(filepath);
|
|
6332
6456
|
if (!collection) {
|
|
6333
6457
|
throw new import_graphql6.GraphQLError(`Unable to find collection for ${filepath}.`);
|
|
6334
6458
|
}
|
|
6335
|
-
if (((
|
|
6459
|
+
if (((_b = collection.match) == null ? void 0 : _b.exclude) || ((_c = collection.match) == null ? void 0 : _c.include)) {
|
|
6336
6460
|
const matches = this.tinaSchema.getMatches({ collection });
|
|
6337
6461
|
const match = import_micromatch2.default.isMatch(filepath, matches);
|
|
6338
6462
|
if (!match) {
|
|
@@ -6373,6 +6497,14 @@ var Database = class {
|
|
|
6373
6497
|
let delOps = [];
|
|
6374
6498
|
if (!isGitKeep(normalizedPath, collection)) {
|
|
6375
6499
|
putOps = [
|
|
6500
|
+
...makeRefOpsForDocument(
|
|
6501
|
+
normalizedPath,
|
|
6502
|
+
collectionName,
|
|
6503
|
+
collectionReferences,
|
|
6504
|
+
dataFields,
|
|
6505
|
+
"put",
|
|
6506
|
+
level
|
|
6507
|
+
),
|
|
6376
6508
|
...makeIndexOpsForDocument(
|
|
6377
6509
|
normalizedPath,
|
|
6378
6510
|
collectionName,
|
|
@@ -6396,6 +6528,14 @@ var Database = class {
|
|
|
6396
6528
|
SUBLEVEL_OPTIONS
|
|
6397
6529
|
).get(normalizedPath);
|
|
6398
6530
|
delOps = existingItem ? [
|
|
6531
|
+
...makeRefOpsForDocument(
|
|
6532
|
+
normalizedPath,
|
|
6533
|
+
collectionName,
|
|
6534
|
+
collectionReferences,
|
|
6535
|
+
existingItem,
|
|
6536
|
+
"del",
|
|
6537
|
+
level
|
|
6538
|
+
),
|
|
6399
6539
|
...makeIndexOpsForDocument(
|
|
6400
6540
|
normalizedPath,
|
|
6401
6541
|
collectionName,
|
|
@@ -6558,6 +6698,22 @@ var Database = class {
|
|
|
6558
6698
|
this.tinaSchema = await createSchema({ schema });
|
|
6559
6699
|
return this.tinaSchema;
|
|
6560
6700
|
};
|
|
6701
|
+
this.getCollectionReferences = async (level) => {
|
|
6702
|
+
if (this.collectionReferences) {
|
|
6703
|
+
return this.collectionReferences;
|
|
6704
|
+
}
|
|
6705
|
+
const result = {};
|
|
6706
|
+
const schema = await this.getSchema(level || this.contentLevel);
|
|
6707
|
+
const collections = schema.getCollections();
|
|
6708
|
+
for (const collection of collections) {
|
|
6709
|
+
const collectionReferences = this.tinaSchema.findReferencesFromCollection(
|
|
6710
|
+
collection.name
|
|
6711
|
+
);
|
|
6712
|
+
result[collection.name] = collectionReferences;
|
|
6713
|
+
}
|
|
6714
|
+
this.collectionReferences = result;
|
|
6715
|
+
return result;
|
|
6716
|
+
};
|
|
6561
6717
|
this.getIndexDefinitions = async (level) => {
|
|
6562
6718
|
if (!this.collectionIndexDefinitions) {
|
|
6563
6719
|
await new Promise(async (resolve2, reject) => {
|
|
@@ -6567,11 +6723,53 @@ var Database = class {
|
|
|
6567
6723
|
const collections = schema.getCollections();
|
|
6568
6724
|
for (const collection of collections) {
|
|
6569
6725
|
const indexDefinitions = {
|
|
6570
|
-
[DEFAULT_COLLECTION_SORT_KEY]: { fields: [] }
|
|
6726
|
+
[DEFAULT_COLLECTION_SORT_KEY]: { fields: [] },
|
|
6571
6727
|
// provide a default sort key which is the file sort
|
|
6728
|
+
// pseudo-index for the collection's references
|
|
6729
|
+
[REFS_COLLECTIONS_SORT_KEY]: {
|
|
6730
|
+
fields: [
|
|
6731
|
+
{
|
|
6732
|
+
name: REFS_REFERENCE_FIELD,
|
|
6733
|
+
type: "string",
|
|
6734
|
+
list: false
|
|
6735
|
+
},
|
|
6736
|
+
{
|
|
6737
|
+
name: REFS_PATH_FIELD,
|
|
6738
|
+
type: "string",
|
|
6739
|
+
list: false
|
|
6740
|
+
}
|
|
6741
|
+
]
|
|
6742
|
+
}
|
|
6572
6743
|
};
|
|
6573
|
-
|
|
6574
|
-
|
|
6744
|
+
let fields = [];
|
|
6745
|
+
if (collection.templates) {
|
|
6746
|
+
const templateFieldMap = {};
|
|
6747
|
+
const conflictedFields = /* @__PURE__ */ new Set();
|
|
6748
|
+
for (const template of collection.templates) {
|
|
6749
|
+
for (const field of template.fields) {
|
|
6750
|
+
if (!templateFieldMap[field.name]) {
|
|
6751
|
+
templateFieldMap[field.name] = field;
|
|
6752
|
+
} else {
|
|
6753
|
+
if (templateFieldMap[field.name].type !== field.type) {
|
|
6754
|
+
console.warn(
|
|
6755
|
+
`Field ${field.name} has conflicting types in templates - skipping index`
|
|
6756
|
+
);
|
|
6757
|
+
conflictedFields.add(field.name);
|
|
6758
|
+
}
|
|
6759
|
+
}
|
|
6760
|
+
}
|
|
6761
|
+
}
|
|
6762
|
+
for (const conflictedField in conflictedFields) {
|
|
6763
|
+
delete templateFieldMap[conflictedField];
|
|
6764
|
+
}
|
|
6765
|
+
for (const field of Object.values(templateFieldMap)) {
|
|
6766
|
+
fields.push(field);
|
|
6767
|
+
}
|
|
6768
|
+
} else if (collection.fields) {
|
|
6769
|
+
fields = collection.fields;
|
|
6770
|
+
}
|
|
6771
|
+
if (fields) {
|
|
6772
|
+
for (const field of fields) {
|
|
6575
6773
|
if (field.indexed !== void 0 && field.indexed === false || field.type === "object") {
|
|
6576
6774
|
continue;
|
|
6577
6775
|
}
|
|
@@ -6720,29 +6918,36 @@ var Database = class {
|
|
|
6720
6918
|
}
|
|
6721
6919
|
startKey = startKey || key || "";
|
|
6722
6920
|
endKey = key || "";
|
|
6723
|
-
edges = [...edges, { cursor: key, path: filepath }];
|
|
6921
|
+
edges = [...edges, { cursor: key, path: filepath, value: itemRecord }];
|
|
6724
6922
|
}
|
|
6725
6923
|
return {
|
|
6726
|
-
edges: await sequential(
|
|
6727
|
-
|
|
6728
|
-
|
|
6729
|
-
|
|
6730
|
-
|
|
6731
|
-
|
|
6732
|
-
|
|
6733
|
-
|
|
6734
|
-
|
|
6735
|
-
|
|
6736
|
-
|
|
6737
|
-
|
|
6738
|
-
|
|
6739
|
-
|
|
6740
|
-
|
|
6741
|
-
|
|
6924
|
+
edges: await sequential(
|
|
6925
|
+
edges,
|
|
6926
|
+
async ({
|
|
6927
|
+
cursor,
|
|
6928
|
+
path: path7,
|
|
6929
|
+
value
|
|
6930
|
+
}) => {
|
|
6931
|
+
try {
|
|
6932
|
+
const node = await hydrator(path7, value);
|
|
6933
|
+
return {
|
|
6934
|
+
node,
|
|
6935
|
+
cursor: btoa(cursor)
|
|
6936
|
+
};
|
|
6937
|
+
} catch (error) {
|
|
6938
|
+
console.log(error);
|
|
6939
|
+
if (error instanceof Error && (!path7.includes(".tina/__generated__/_graphql.json") || !path7.includes("tina/__generated__/_graphql.json"))) {
|
|
6940
|
+
throw new TinaQueryError({
|
|
6941
|
+
originalError: error,
|
|
6942
|
+
file: path7,
|
|
6943
|
+
collection: collection.name,
|
|
6944
|
+
stack: error.stack
|
|
6945
|
+
});
|
|
6946
|
+
}
|
|
6947
|
+
throw error;
|
|
6742
6948
|
}
|
|
6743
|
-
throw error;
|
|
6744
6949
|
}
|
|
6745
|
-
|
|
6950
|
+
),
|
|
6746
6951
|
pageInfo: {
|
|
6747
6952
|
hasPreviousPage,
|
|
6748
6953
|
hasNextPage,
|
|
@@ -6882,12 +7087,14 @@ var Database = class {
|
|
|
6882
7087
|
}
|
|
6883
7088
|
};
|
|
6884
7089
|
this.delete = async (filepath) => {
|
|
7090
|
+
var _a;
|
|
6885
7091
|
await this.initLevel();
|
|
6886
7092
|
const collection = await this.collectionForPath(filepath);
|
|
6887
7093
|
if (!collection) {
|
|
6888
7094
|
throw new Error(`No collection found for path: ${filepath}`);
|
|
6889
7095
|
}
|
|
6890
7096
|
const indexDefinitions = await this.getIndexDefinitions(this.contentLevel);
|
|
7097
|
+
const collectionReferences = (_a = await this.getCollectionReferences()) == null ? void 0 : _a[collection.name];
|
|
6891
7098
|
const collectionIndexDefinitions = indexDefinitions == null ? void 0 : indexDefinitions[collection.name];
|
|
6892
7099
|
let level = this.contentLevel;
|
|
6893
7100
|
if (collection == null ? void 0 : collection.isDetached) {
|
|
@@ -6906,6 +7113,14 @@ var Database = class {
|
|
|
6906
7113
|
collection.path || ""
|
|
6907
7114
|
);
|
|
6908
7115
|
await this.contentLevel.batch([
|
|
7116
|
+
...makeRefOpsForDocument(
|
|
7117
|
+
normalizedPath,
|
|
7118
|
+
collection.name,
|
|
7119
|
+
collectionReferences,
|
|
7120
|
+
item,
|
|
7121
|
+
"del",
|
|
7122
|
+
level
|
|
7123
|
+
),
|
|
6909
7124
|
...makeIndexOpsForDocument(
|
|
6910
7125
|
normalizedPath,
|
|
6911
7126
|
collection.name,
|
|
@@ -6983,7 +7198,13 @@ var Database = class {
|
|
|
6983
7198
|
);
|
|
6984
7199
|
}
|
|
6985
7200
|
} else {
|
|
6986
|
-
await _indexContent(
|
|
7201
|
+
await _indexContent(
|
|
7202
|
+
this,
|
|
7203
|
+
level,
|
|
7204
|
+
contentPaths,
|
|
7205
|
+
enqueueOps,
|
|
7206
|
+
collection
|
|
7207
|
+
);
|
|
6987
7208
|
}
|
|
6988
7209
|
}
|
|
6989
7210
|
);
|
|
@@ -7123,6 +7344,7 @@ var hashPasswordValues = async (data, passwordFields) => Promise.all(
|
|
|
7123
7344
|
);
|
|
7124
7345
|
var isGitKeep = (filepath, collection) => filepath.endsWith(`.gitkeep.${(collection == null ? void 0 : collection.format) || "md"}`);
|
|
7125
7346
|
var _indexContent = async (database, level, documentPaths, enqueueOps, collection, passwordFields) => {
|
|
7347
|
+
var _a;
|
|
7126
7348
|
let collectionIndexDefinitions;
|
|
7127
7349
|
let collectionPath;
|
|
7128
7350
|
if (collection) {
|
|
@@ -7133,6 +7355,7 @@ var _indexContent = async (database, level, documentPaths, enqueueOps, collectio
|
|
|
7133
7355
|
}
|
|
7134
7356
|
collectionPath = collection.path;
|
|
7135
7357
|
}
|
|
7358
|
+
const collectionReferences = (_a = await database.getCollectionReferences()) == null ? void 0 : _a[collection == null ? void 0 : collection.name];
|
|
7136
7359
|
const tinaSchema = await database.getSchema();
|
|
7137
7360
|
let templateInfo = null;
|
|
7138
7361
|
if (collection) {
|
|
@@ -7165,6 +7388,14 @@ var _indexContent = async (database, level, documentPaths, enqueueOps, collectio
|
|
|
7165
7388
|
const item = await rootSublevel.get(normalizedPath);
|
|
7166
7389
|
if (item) {
|
|
7167
7390
|
await database.contentLevel.batch([
|
|
7391
|
+
...makeRefOpsForDocument(
|
|
7392
|
+
normalizedPath,
|
|
7393
|
+
collection == null ? void 0 : collection.name,
|
|
7394
|
+
collectionReferences,
|
|
7395
|
+
item,
|
|
7396
|
+
"del",
|
|
7397
|
+
level
|
|
7398
|
+
),
|
|
7168
7399
|
...makeIndexOpsForDocument(
|
|
7169
7400
|
normalizedPath,
|
|
7170
7401
|
collection.name,
|
|
@@ -7191,6 +7422,14 @@ var _indexContent = async (database, level, documentPaths, enqueueOps, collectio
|
|
|
7191
7422
|
}
|
|
7192
7423
|
if (!isGitKeep(filepath, collection)) {
|
|
7193
7424
|
await enqueueOps([
|
|
7425
|
+
...makeRefOpsForDocument(
|
|
7426
|
+
normalizedPath,
|
|
7427
|
+
collection == null ? void 0 : collection.name,
|
|
7428
|
+
collectionReferences,
|
|
7429
|
+
aliasedData,
|
|
7430
|
+
"put",
|
|
7431
|
+
level
|
|
7432
|
+
),
|
|
7194
7433
|
...makeIndexOpsForDocument(
|
|
7195
7434
|
normalizedPath,
|
|
7196
7435
|
collection == null ? void 0 : collection.name,
|
|
@@ -7241,6 +7480,7 @@ var _indexContent = async (database, level, documentPaths, enqueueOps, collectio
|
|
|
7241
7480
|
}
|
|
7242
7481
|
};
|
|
7243
7482
|
var _deleteIndexContent = async (database, documentPaths, enqueueOps, collection) => {
|
|
7483
|
+
var _a;
|
|
7244
7484
|
if (!documentPaths.length) {
|
|
7245
7485
|
return;
|
|
7246
7486
|
}
|
|
@@ -7254,6 +7494,7 @@ var _deleteIndexContent = async (database, documentPaths, enqueueOps, collection
|
|
|
7254
7494
|
throw new Error(`No indexDefinitions for collection ${collection.name}`);
|
|
7255
7495
|
}
|
|
7256
7496
|
}
|
|
7497
|
+
const collectionReferences = (_a = await database.getCollectionReferences()) == null ? void 0 : _a[collection == null ? void 0 : collection.name];
|
|
7257
7498
|
const tinaSchema = await database.getSchema();
|
|
7258
7499
|
let templateInfo = null;
|
|
7259
7500
|
if (collection) {
|
|
@@ -7277,6 +7518,14 @@ var _deleteIndexContent = async (database, documentPaths, enqueueOps, collection
|
|
|
7277
7518
|
item
|
|
7278
7519
|
) : item;
|
|
7279
7520
|
await enqueueOps([
|
|
7521
|
+
...makeRefOpsForDocument(
|
|
7522
|
+
itemKey,
|
|
7523
|
+
collection == null ? void 0 : collection.name,
|
|
7524
|
+
collectionReferences,
|
|
7525
|
+
aliasedData,
|
|
7526
|
+
"del",
|
|
7527
|
+
database.contentLevel
|
|
7528
|
+
),
|
|
7280
7529
|
...makeIndexOpsForDocument(
|
|
7281
7530
|
itemKey,
|
|
7282
7531
|
collection.name,
|