UNPKG

redis-orm-node

Version:

`redis-orm` is a lightweight Object-Relational Mapping (ORM) library for Node.js.

76 lines 3.88 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.getRepository = void 0; const uuid_1 = require("uuid"); const internalStorage_1 = require("../internalStorage"); const where_1 = require("../mappers/where"); const utils_1 = require("../utils"); function getRepository(base) { return class Repository extends base { static get(id) { return __awaiter(this, void 0, void 0, function* () { const { connection } = internalStorage_1.internalStorage.getEntityDefinition(this); const result = yield connection.hGetAll(id); if (!(0, utils_1.isEmptyObject)(result)) return undefined; return { id, value: result }; }); } static set(id, value) { return __awaiter(this, void 0, void 0, function* () { const { connection } = internalStorage_1.internalStorage.getEntityDefinition(this); const result = yield connection.hSet(id, Object.entries(JSON.parse(JSON.stringify(value)))); if (!result) return undefined; return { id, value }; }); } static generateId(partitionKey) { const { schema } = internalStorage_1.internalStorage.getEntityDefinition(this); const hKey = `${schema.name}:${partitionKey || (0, uuid_1.v4)()}`; return hKey; } static create(value, id) { return __awaiter(this, void 0, void 0, function* () { const hKey = id || this.generateId(); return yield Repository.set.call(this, hKey, value); }); } static delete(id) { return __awaiter(this, void 0, void 0, function* () { const { connection } = internalStorage_1.internalStorage.getEntityDefinition(this); return yield connection.del(id); }); } static find({ where, options }) { return __awaiter(this, void 0, void 0, function* () { const { connection, schema } = internalStorage_1.internalStorage.getEntityDefinition(this); const query = where && (0, where_1.convertWhere)(schema, ...(Array.isArray(where) ? where : [where])); const { documents } = yield connection.ft.search(schema.index, query || '*', { SORTBY: options === null || options === void 0 ? void 0 : options.sortby, LIMIT: options === null || options === void 0 ? void 0 : options.limit }); return documents; }); } static findOne({ where, options }) { return __awaiter(this, void 0, void 0, function* () { const [result] = yield Repository.find.call(this, { where, options: Object.assign(Object.assign({}, options), { limit: { from: (options === null || options === void 0 ? void 0 : options.limit.from) || 0, size: 1 } }) }); return result; }); } }; } exports.getRepository = getRepository; //# sourceMappingURL=Repository.js.map