lincd-jsonld-store
Version:
90 lines • 4.71 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 __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var JSONLDStore_1;
Object.defineProperty(exports, "__esModule", { value: true });
exports.JSONLDStore = void 0;
const LinkedStorage_1 = require("lincd/utils/LinkedStorage");
const package_js_1 = require("../package.js");
const jsonld_store_js_1 = require("../ontologies/jsonld-store.js");
const InMemoryStore_1 = require("lincd-storage/shapes/InMemoryStore");
const JSONLDWriter_1 = require("lincd-jsonld/utils/JSONLDWriter");
const JSONLD_1 = require("lincd-jsonld/utils/JSONLD");
const QuadSet_1 = require("lincd/collections/QuadSet");
let JSONLDStore = JSONLDStore_1 = class JSONLDStore extends InMemoryStore_1.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_1.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_1.LinkedStorage.getGraphForStore(this) || this.targetGraph;
return JSONLD_1.JSONLD.parseString(jsonld, true, null, graph).then((parseResult) => {
this.contents = parseResult.quads;
return this.contents;
});
}
loadContents() {
return __awaiter(this, void 0, void 0, function* () {
// 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_1.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({}));
}
onContentsUpdated() {
return __awaiter(this, void 0, void 0, function* () {
let jsonld = yield this.getJSONLDFromContents();
return this.setPermanentStorageContents(jsonld);
});
}
getMinStoreDelay() {
return JSONLDStore_1.MINIMUM_STORE_DELAY_MS;
}
};
exports.JSONLDStore = JSONLDStore;
/**
* indicates that instances of this shape need to have this rdf.type
*/
JSONLDStore.targetClass = jsonld_store_js_1.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;
exports.JSONLDStore = JSONLDStore = JSONLDStore_1 = __decorate([
package_js_1.linkedShape
], JSONLDStore);
//# sourceMappingURL=JSONLDStore.js.map