UNPKG

@x5e/gink

Version:

an eventually consistent database

94 lines 3.33 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Vertex = void 0; const Database_1 = require("./Database"); const Container_1 = require("./Container"); const builders_1 = require("./builders"); const utils_1 = require("./utils"); const Edge_1 = require("./Edge"); class Vertex extends Container_1.Container { constructor(database, address) { super(database, address, builders_1.Behavior.VERTEX); } static get(database, muid) { database = database || Database_1.Database.recent; if (!muid) { muid = { timestamp: -1, medallion: -1, offset: builders_1.Behavior.VERTEX }; } return new Vertex(database, muid); } static async create(database, meta) { database = database || Database_1.Database.recent; const muid = await Container_1.Container.addContainer({ behavior: builders_1.Behavior.VERTEX, database, meta, }); return new Vertex(database, muid); } toJson(indent, asOf, seen) { throw new Error("toJson not implemented for Vertex"); } /** * Returns a promise that resolves to true showing if this placeholder is/was visible at the * specified time (default now), or false if it was softly deleted. * @returns undefined, a basic value, or a container */ async isAlive(asOf) { const entry = await this.database.store.getEntryByKey(this.address, undefined, asOf); return entry === undefined || !entry.deletion; } async size(asOf) { return (await this.isAlive(asOf)) ? 1 : 0; } /** * Performs a soft delete of this graph node. */ async remove(meta) { return this.addEntry(undefined, Container_1.Container.DELETION, meta); } async revive(meta) { return this.addEntry(undefined, Container_1.Container.INCLUSION, meta); } async reset(toTime, recurse, meta) { const bundler = await this.database.startBundle(meta); if (!toTime) { await this.remove(meta); } else { const aliveThen = await this.isAlive(toTime); const aliveNow = await this.isAlive(); if (aliveThen !== aliveNow) { if (aliveThen) { await this.revive({ bundler }); } else { await this.remove({ bundler }); } } } if (!meta?.bundler) { await bundler.commit(); } } async getEdgesFrom(asOf) { return this.getEdges(true, asOf); } async getEdgesTo(asOf) { return this.getEdges(false, asOf); } async getEdges(source, asOf) { const entries = await this.database.store.getEntriesBySourceOrTarget(this.address, source, asOf); const edges = []; for (let i = 0; i < entries.length; i++) { const entry = entries[i]; if (entry.behavior !== builders_1.Behavior.EDGE_TYPE) continue; const edge = Edge_1.Edge.get(this.database, (0, utils_1.muidTupleToMuid)(entry.entryId), (0, utils_1.entryToEdgeData)(entry)); edges.push(edge); } return edges; } } exports.Vertex = Vertex; //# sourceMappingURL=Vertex.js.map