UNPKG

@jsstudio/development-api-interceptor

Version:
140 lines 6.32 kB
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()); }); }; import { openDB } from 'idb'; import find from 'lodash/find'; import { AUTO_INCREMENT, INDEXED_DB_TRANSACTIONS, INDEXED_DB_VERSION, RESPONSE_CODE, VALIDATION } from '../constants'; import { ApiError } from '../error-handling'; import jsonSchemaObject from '../utils/json-schema'; import { logger } from '../utils/logger'; class IndexedDb { constructor() { this.database = ''; } setDatabase(database) { this.database = database; } initialize(json) { return __awaiter(this, void 0, void 0, function* () { const tableNames = json.tables.map((item) => item.table_name); try { const autoIncrementCols = this.getAutoIncrementColumns(json.tables); yield this.createDatabase(tableNames, autoIncrementCols); jsonSchemaObject.set(json); } catch (err) { logger('IndexedDb validation error', err); } }); } createDatabase(tableNames, autoIncrementCols) { return __awaiter(this, void 0, void 0, function* () { try { this.db = yield openDB(this.database, INDEXED_DB_VERSION, { upgrade(db) { for (const tableName of tableNames) { const autoIncrementIndex = autoIncrementCols.findIndex((item) => item.tableName === tableName); autoIncrementIndex > -1 ? db.createObjectStore(tableName, autoIncrementCols[autoIncrementIndex]) : db.createObjectStore(tableName); } }, }); } catch (error) { return false; } Object.freeze(indexedDB); }); } getAutoIncrementColumns(tables) { const autoIncrementColObj = []; for (const { table_name: tableName, columns } of tables) { for (const { entity_name: keyPath, constraints } of columns) { if ((!this.hasConstraint(AUTO_INCREMENT, constraints)) || (this.hasConstraint(AUTO_INCREMENT, constraints) && !(constraints === null || constraints === void 0 ? void 0 : constraints.AUTO_INCREMENT))) { continue; } autoIncrementColObj.push({ keyPath, tableName, autoIncrement: true }); } } return autoIncrementColObj; } getPrimaryKey(tableName) { const jsonSchema = jsonSchemaObject.get(); const tableObject = find(jsonSchema.tables, (e) => e.table_name === tableName); const columnObject = find(tableObject === null || tableObject === void 0 ? void 0 : tableObject.columns, { constraints: { PRIMARY_KEY: true } }); if (!(columnObject === null || columnObject === void 0 ? void 0 : columnObject.entity_name)) { throw new ApiError(VALIDATION.PRIMARY_KEY_NOT_PRESENT_IN_SCHEMA, RESPONSE_CODE.INVALID_INPUT); } return columnObject.entity_name; } hasConstraint(constraintValue, constraints) { var _a; return (_a = constraints === null || constraints === void 0 ? void 0 : constraints.hasOwnProperty(constraintValue)) !== null && _a !== void 0 ? _a : false; } getById(tableName, id) { return __awaiter(this, void 0, void 0, function* () { const tx = this.db.transaction(tableName, INDEXED_DB_TRANSACTIONS.READ_ONLY); const store = tx.objectStore(tableName); return yield store.get(id); }); } getAll(tableName) { return __awaiter(this, void 0, void 0, function* () { const tx = this.db.transaction(tableName, INDEXED_DB_TRANSACTIONS.READ_ONLY); const store = tx.objectStore(tableName); return yield store.getAll(); }); } create(tableName, value) { return __awaiter(this, void 0, void 0, function* () { const tx = this.db.transaction(tableName, INDEXED_DB_TRANSACTIONS.READ_WRITE); const store = tx.objectStore(tableName); if (store.autoIncrement) { return yield store.put(value); } if (!value[this.getPrimaryKey(tableName)]) { throw new ApiError(VALIDATION.PRIMARY_KEY_NOT_PRESENT, RESPONSE_CODE.INVALID_INPUT); } return yield store.put(value, value[this.getPrimaryKey(tableName)]); }); } update(tableName, value, id) { return __awaiter(this, void 0, void 0, function* () { const tx = this.db.transaction(tableName, INDEXED_DB_TRANSACTIONS.READ_WRITE); const store = tx.objectStore(tableName); const result = yield store.get(id); if (!result) { return result; } value[this.getPrimaryKey(tableName)] = id; if (store.autoIncrement) { return yield store.put(value); } return yield store.put(value, id); }); } delete(tableName, id) { return __awaiter(this, void 0, void 0, function* () { const tx = this.db.transaction(tableName, INDEXED_DB_TRANSACTIONS.READ_WRITE); const store = tx.objectStore(tableName); const result = yield store.get(id); if (!result) { return result; } yield store.delete(id); return id; }); } } const indexedDb = new IndexedDb(); export default indexedDb; //# sourceMappingURL=index.js.map