UNPKG

@tucmc/hazel

Version:
120 lines (119 loc) 5.2 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.Collection = void 0; const path_1 = __importDefault(require("path")); const process = __importStar(require("process")); const DMap_1 = require("../data/DMap"); const LiveDMap_1 = require("../data/LiveDMap"); const ReferableEntity_1 = require("../data/ReferableEntity"); const Colour_1 = require("../debugger/Colour"); const Debugger_1 = require("../debugger/Debugger"); const Files_1 = require("../io/Files"); class Collection { name; debug = new Debugger_1.Debugger(this.constructor.name || 'Collection'); rootPath = `resource/collection/${process.env.RUNTIME_MODE}`; resourcePath; dbInstance; constructor(name) { this.name = name; this.resourcePath = path_1.default.join(this.rootPath, `${this.name}.json`); this.dbInstance = this.initInstance(name); } setDefaultMutator(mutator) { this.collectionMutator = mutator; return this; } async pushChanges(changes, strict = true) { if (!changes.isLive && strict) { throw Error(`${Colour_1.ConsoleColour.BGRED}${Colour_1.ConsoleColour.BOLD}only changes from LiveDMap are pushable by default.${Colour_1.ConsoleColour.RESET} \n${Colour_1.ConsoleColour.BGYELLOW}Please do not push cached data to the database.${Colour_1.ConsoleColour.RESET}`); } return await this.verifyChanges(await this.handleChanges(changes.changes)); } makeReferableEntities(data) { const refMap = new DMap_1.DMap(data); const nMap = {}; refMap.iterateSync((k, v) => { const id = v._docID; delete v._docID; const entity = new ReferableEntity_1.ReferableMapEntity(v, id); entity.setSynthesized(false); nMap[k] = entity; }); return nMap; } async fetch(mutator = this.collectionMutator) { const loader = this.debug.loadingInfo(`fetching collection ${this.name} from database.`); const data = await this.retrieveCollection(); loader.succeed(); const mutated = mutator(data); Files_1.Files.writeFile(mutator(data), this.resourcePath); return new LiveDMap_1.LiveDMap(this.makeReferableEntities(mutated)); } async readFromCache(autoFetch = false) { this.debug.info(`reading collection ${this.name} from cache.`); const data = Files_1.Files.readFile(this.resourcePath); if (!data) { this.debug.warn(`cached collection ${this.name} is not presented. autoFetch: ${autoFetch ? 'true' : 'false'}`); if (!autoFetch) return null; return await this.fetch(); } return new DMap_1.DMap(this.makeReferableEntities(data.content)); } async readFromCacheNoRef(autoFetch = false) { this.debug.info(`reading collection ${this.name} from cache.`); const data = Files_1.Files.readFile(this.resourcePath); if (!data) { this.debug.warn(`cached collection ${this.name} is not presented. autoFetch: ${autoFetch ? 'true' : 'false'}`); if (!autoFetch) return null; return await this.fetchNoRef(); } return new DMap_1.DMap(this.clearEntityReference(data.content)); } async fetchNoRef(mutator = this.collectionMutator) { const loader = this.debug.loadingInfo(`fetching collection ${this.name} from database.`); const data = await this.retrieveCollection(); loader.succeed(); const mutated = mutator(data); Files_1.Files.writeFile(mutator(data), this.resourcePath); return new LiveDMap_1.LiveDMap(this.clearEntityReference(mutated)); } clearEntityReference(data) { const clearedMap = new DMap_1.DMap(data); clearedMap.iterateSync((k, v) => { if (v._docID) { delete v._docID; } }); return clearedMap.getRecord(); } } exports.Collection = Collection;