lincd-jsonld-store
Version:
74 lines • 3.58 kB
JavaScript
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var JSONLDStore_1;
import { LinkedStorage } from 'lincd/utils/LinkedStorage';
import { linkedShape } from '../package.js';
import { jsonldStore } from '../ontologies/jsonld-store.js';
import { InMemoryStore } from 'lincd-storage/shapes/InMemoryStore';
import { JSONLDWriter } from 'lincd-jsonld/utils/JSONLDWriter';
import { JSONLD } from 'lincd-jsonld/utils/JSONLD';
import { QuadSet } from 'lincd/collections/QuadSet';
let JSONLDStore = JSONLDStore_1 = class JSONLDStore extends InMemoryStore {
getJSONLDFromContents() {
//extra context for nodes that match with the URI of this store and nodes that match with the default DATA_ROOT
return JSONLDWriter.fromQuads(this.contents, true, true, {
this: this.namedNode.uri + '/',
local: process.env.DATA_ROOT + '/',
}).then((obj) => {
//return pretty JSON if not in production
let prod = process.env.NODE_ENV === 'production';
return prod ? JSON.stringify(obj) : JSON.stringify(obj, null, 2);
});
}
setContentsFromJSONLD(jsonld) {
let graph = LinkedStorage.getGraphForStore(this) || this.targetGraph;
return JSONLD.parseString(jsonld, true, null, graph).then((parseResult) => {
this.contents = parseResult.quads;
return this.contents;
});
}
async loadContents() {
// console.log('Loading contents of ' + this.toString() + ' from permanent storage.');
return this.getPermanentStorageContents()
.then((jsonld) => {
return this.setContentsFromJSONLD(jsonld || '{}').then((res) => {
// console.log('Loaded contents of ' + this.toString() + ' from permanent storage.');
return res;
});
})
.catch((err) => {
console.warn('Could not load contents of JSONLDStore: ', err);
return Promise.resolve(new QuadSet());
});
}
setPermanentStorageContents(jsonld) {
//by default not implemented. extending classes can define where permanent copy is stored.
return Promise.reject(false);
}
getPermanentStorageContents() {
//by default starts empty. extending classes can define where permanent copy is stored.
return Promise.resolve(JSON.stringify({}));
}
async onContentsUpdated() {
let jsonld = await this.getJSONLDFromContents();
return this.setPermanentStorageContents(jsonld);
}
getMinStoreDelay() {
return JSONLDStore_1.MINIMUM_STORE_DELAY_MS;
}
};
/**
* indicates that instances of this shape need to have this rdf.type
*/
JSONLDStore.targetClass = jsonldStore.JSONLD_Store;
//wait at least this many milliseconds before storing the data in this store again (used to prevent IO blocking up)
JSONLDStore.MINIMUM_STORE_DELAY_MS = 10 * 1000;
JSONLDStore = JSONLDStore_1 = __decorate([
linkedShape
], JSONLDStore);
export { JSONLDStore };
//# sourceMappingURL=JSONLDStore.js.map