UNPKG

react-indexeddb-kit

Version:

A TypeScript-based React IndexedDB wrapper with CRUD operations.

137 lines (136 loc) 5.63 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.ReactIndexDBModel = void 0; const errors_1 = require("../errors"); const create_1 = require("./db-operations/create"); const delete_1 = require("./db-operations/delete"); const find_unique_1 = require("./db-operations/find-unique"); const findMany_1 = require("./db-operations/findMany"); const update_1 = require("./db-operations/update"); const validation_1 = require("./validation/validation"); class ReactIndexDBModel { constructor(dbClient, modelDef) { this.dbClient = dbClient; this.modelDef = modelDef; } /** * Ensure the database is connected before performing any operation. */ ensureDatabase() { return __awaiter(this, void 0, void 0, function* () { yield this.dbClient.ensureDatabase(); // ✅ Ensures the DB is connected const db = this.dbClient.getDatabase(); if (!db) { throw new errors_1.DatabaseError("Database is not initialized"); } return db; }); } /** * Create a new record in IndexedDB after validating the data. * @param data - The record data to insert. * @returns The inserted record ID. */ create(data) { return __awaiter(this, void 0, void 0, function* () { try { const db = yield this.ensureDatabase(); // ✅ Ensures DB before proceeding // Validate data before inserting const validatedData = (0, validation_1.validateData)(data, this.modelDef, this.dbClient.getSchema()); return yield (0, create_1.create)(db, this.modelDef, validatedData); } catch (error) { throw this.handleError(error); } }); } /** * Update an existing record after validating the data. * @param id - The ID of the record to update. * @param data - The updated record data. * @returns The updated record ID. */ update(id, data) { return __awaiter(this, void 0, void 0, function* () { try { const db = yield this.ensureDatabase(); // ✅ Ensures DB before proceeding // Validate data before updating const validatedData = (0, validation_1.validateData)(data, this.modelDef, this.dbClient.getSchema()); return yield (0, update_1.update)(db, this.modelDef, id, validatedData); } catch (error) { throw this.handleError(error); } }); } /** * Delete a record by ID. * @param id - The ID of the record to delete. * @returns A confirmation message. */ delete(id) { return __awaiter(this, void 0, void 0, function* () { try { const db = yield this.ensureDatabase(); // ✅ Ensures DB before proceeding return yield (0, delete_1.deleteRecord)(db, this.modelDef, id); } catch (error) { throw this.handleError(error); } }); } /** * Retrieve multiple records with optional query filters. * @param options - Query options such as filters, pagination, sorting, etc. * @returns An array of matching records. */ findMany(options) { return __awaiter(this, void 0, void 0, function* () { try { const db = yield this.ensureDatabase(); // ✅ Ensures DB before proceeding return yield (0, findMany_1.findMany)(db, this.modelDef, options); } catch (error) { throw this.handleError(error); } }); } /** * Find a single record by ID. * @param id - The ID of the record to retrieve. * @param options - Query options for selecting specific fields. * @returns The found record or null. */ findUnique(id, options) { return __awaiter(this, void 0, void 0, function* () { try { const db = yield this.ensureDatabase(); // ✅ Ensures DB before proceeding return yield (0, find_unique_1.findUnique)(db, this.modelDef, id, options); } catch (error) { throw this.handleError(error); } }); } /** * Handles unknown errors and ensures they are transformed into a proper `ReactIndexDBError`. * @param error - The caught error. * @returns {ReactIndexDBError} - A formatted error. */ handleError(error) { if (error instanceof errors_1.ValidationError || error instanceof errors_1.DatabaseError) { return error; // Already a known error, rethrow it } return new errors_1.DatabaseError(error instanceof Error ? error.message : "An unknown error occurred"); } } exports.ReactIndexDBModel = ReactIndexDBModel;