react-indexeddb-kit
Version:
A TypeScript-based React IndexedDB wrapper with CRUD operations.
38 lines (37 loc) • 1.91 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.findMany = findMany;
const errors_1 = require("../../errors");
// Find multiple records with optional filtering and selection
function findMany(db, modelDef, options) {
return new Promise((resolve, reject) => {
const transaction = db.transaction(modelDef.name, "readonly");
const store = transaction.objectStore(modelDef.name);
const request = store.getAll();
request.onsuccess = () => {
let results = request.result;
// Apply filtering
if (options === null || options === void 0 ? void 0 : options.where) {
results = results.filter((item) => Object.entries(options.where).every(([key, value]) => item[key] === value));
}
// Apply sorting
if (options === null || options === void 0 ? void 0 : options.orderBy) {
const { field, direction } = options.orderBy;
results.sort((a, b) => (a[field] < b[field] ? -1 : 1) * (direction === "asc" ? 1 : -1));
}
// Apply pagination
if ((options === null || options === void 0 ? void 0 : options.skip) !== undefined) {
results = results.slice(options.skip);
}
if ((options === null || options === void 0 ? void 0 : options.limit) !== undefined) {
results = results.slice(0, options.limit);
}
// Apply selection based on { fieldName: true } format
if (options === null || options === void 0 ? void 0 : options.select) {
results = results.map((item) => Object.fromEntries(Object.entries(item).filter(([key]) => options.select[key])));
}
resolve(results);
};
request.onerror = () => reject(new errors_1.DatabaseError("Failed to fetch records"));
});
}