UNPKG

@bitblit/ratchet-aws

Version:

Common tools for use with AWS browser and node

89 lines 3.79 kB
import { DateTime } from 'luxon'; import { StringRatchet } from '@bitblit/ratchet-common/lang/string-ratchet'; import { RequireRatchet } from '@bitblit/ratchet-common/lang/require-ratchet'; import { MapRatchet } from '@bitblit/ratchet-common/lang/map-ratchet'; export class PrototypeDao { provider; cfg; static defaultDaoConfig() { return { guidCreateFunction: StringRatchet.createType4Guid, guidFieldName: 'guid', createdEpochMSFieldName: 'createdEpochMS', updatedEpochMSFieldName: 'updatedEpochMS', createdUtcTimestampFieldName: null, updatedUtcTimestampFieldName: null, }; } constructor(provider, cfg = PrototypeDao.defaultDaoConfig()) { this.provider = provider; this.cfg = cfg; RequireRatchet.notNullOrUndefined(provider, 'provider'); RequireRatchet.notNullOrUndefined(cfg, 'cfg'); RequireRatchet.notNullOrUndefined(cfg.guidCreateFunction, 'cfg.guidCreateFunction'); RequireRatchet.notNullOrUndefined(cfg.guidFieldName, 'cfg.guidFieldName'); } async fetchAll() { const db = await this.provider.loadDatabase(); return db.items || []; } async resetDatabase() { await this.provider.storeDatabase({ items: [], lastModifiedEpochMS: Date.now() }); } async removeItems(guids) { let old = await this.fetchAll(); if (guids) { old = old.filter((t) => !guids.includes(t[this.cfg.guidFieldName])); await this.provider.storeDatabase({ items: old, lastModifiedEpochMS: Date.now() }); } return old; } async store(value) { let old = await this.fetchAll(); if (value) { value[this.cfg.guidFieldName] = value[this.cfg.guidFieldName] || this.cfg.guidCreateFunction(); if (this.cfg.createdEpochMSFieldName) { value[this.cfg.createdEpochMSFieldName] = value[this.cfg.createdEpochMSFieldName] || Date.now(); } if (this.cfg.createdUtcTimestampFieldName) { value[this.cfg.createdUtcTimestampFieldName] = value[this.cfg.createdUtcTimestampFieldName] || DateTime.utc().toISO(); } if (this.cfg.updatedEpochMSFieldName) { value[this.cfg.updatedEpochMSFieldName] = Date.now(); } if (this.cfg.updatedUtcTimestampFieldName) { value[this.cfg.updatedUtcTimestampFieldName] = DateTime.utc().toISO(); } old = old.filter((t) => t[this.cfg.guidFieldName] !== value[this.cfg.guidFieldName]); old.push(value); await this.provider.storeDatabase({ items: old, lastModifiedEpochMS: Date.now() }); } return value; } async fetchById(guid) { const old = await this.fetchAll(); return old.find((t) => t[this.cfg.guidFieldName] === guid); } async searchByField(fieldDotPath, fieldValue) { RequireRatchet.notNullOrUndefined(fieldDotPath, 'fieldDotPath'); RequireRatchet.notNullOrUndefined(fieldValue, 'fieldValue'); const map = {}; map[fieldDotPath] = fieldValue; return this.searchByFieldMap(map); } async searchByFieldMap(input) { RequireRatchet.notNullOrUndefined(input, 'input'); let old = await this.fetchAll(); old = old.filter((t) => { let matchAll = true; Object.keys(input).forEach((k) => { const val = MapRatchet.findValueDotPath(t, k); const fieldValue = input[k]; matchAll = matchAll && val === fieldValue; }); return matchAll; }); return old; } } //# sourceMappingURL=prototype-dao.js.map