UNPKG

@evolplus/evo-utils

Version:
116 lines (115 loc) 5.12 kB
"use strict"; 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()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.StaticRepository = exports.EntityRepository = void 0; const cache_1 = require("./cache"); const naming_1 = require("./naming"); const DEFAULT_CACHE_SIZE = 1000; class EntityRepository { constructor(conn, table, fields, primaryKey, cacheSize = DEFAULT_CACHE_SIZE) { this.conn = conn; this.table = table; this.primaryKey = primaryKey; this.cache = new cache_1.LRUCache(cacheSize); this.dbPK = (0, naming_1.convertName)(primaryKey, naming_1.NamingConvention.CamelCase, naming_1.NamingConvention.SnakeCase); this.data2Db = (0, naming_1.createConverter)(fields, naming_1.NamingConvention.CamelCase, naming_1.NamingConvention.SnakeCase); this.db2Data = (0, naming_1.createConverter)(fields, naming_1.NamingConvention.CamelCase, naming_1.NamingConvention.SnakeCase, true); } get(id) { return __awaiter(this, void 0, void 0, function* () { if (this.cache.contains(id)) { return this.cache.get(id); } const [rows] = yield this.conn.query(`SELECT * FROM ${this.table} WHERE ${this.dbPK} = ?`, [id]); if (rows.length) { const entity = this.db2Data(rows[0]); this.cache.put(id, entity); return entity; } }); } put(entity) { return __awaiter(this, void 0, void 0, function* () { let id = entity[this.primaryKey], data = this.data2Db(entity); for (var k in data) { if (data[k] === undefined) { delete data[k]; } } let dataWithoutKey = Object.assign({}, data); delete dataWithoutKey[this.dbPK]; yield this.conn.query(`INSERT INTO ${this.table} SET ? ON DUPLICATE KEY UPDATE ?`, [data, dataWithoutKey]); this.cache.put(id, entity); }); } delete(id) { return __awaiter(this, void 0, void 0, function* () { yield this.conn.query(`DELETE FROM ${this.table} WHERE ${this.dbPK} = ?`, [id]); this.cache.delete(id); }); } query(field, operator, value) { return __awaiter(this, void 0, void 0, function* () { const [rows] = yield this.conn.query(`SELECT * FROM ${this.table} WHERE ${(0, naming_1.convertName)(field, naming_1.NamingConvention.CamelCase, naming_1.NamingConvention.SnakeCase)} ${operator} ?`, [value]); return rows.map((row) => { let data = this.db2Data(row); this.cache.put(data[this.primaryKey], data); return data; }); }); } } exports.EntityRepository = EntityRepository; class StaticRepository { constructor(conn, table, fields, primaryKey) { this.conn = conn; this.table = table; this.primaryKey = primaryKey; this.values = {}; this.promiseReady = this.fetchAll(fields); } waitForReady() { return __awaiter(this, void 0, void 0, function* () { yield Promise.all([this.promiseReady]); }); } get(id) { let val = this.values[id]; if (val) { return Object.assign({}, val); } } getAll() { return Object.values(this.values).map((entity) => Object.assign({}, entity)); } fetchAll(fields) { return __awaiter(this, void 0, void 0, function* () { const [rows] = yield this.conn.query(`SELECT * FROM ${this.table}`), db2Data = (0, naming_1.createConverter)(fields, naming_1.NamingConvention.CamelCase, naming_1.NamingConvention.SnakeCase, true); for (let row of rows) { let data = db2Data(row); this.values[data[this.primaryKey]] = data; } }); } query(field, operator, value) { return Object.values(this.values).filter((entity) => { switch (operator) { case '=': return entity[field] === value; case '<': return entity[field] < value; case '<=': return entity[field] <= value; case '>': return entity[field] > value; case '>=': return entity[field] >= value; case '!=': return entity[field] !== value; } }); } } exports.StaticRepository = StaticRepository;