UNPKG

@stoqey/sofa

Version:
294 lines 12.7 kB
"use strict"; var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; 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()); }); }; var __generator = (this && this.__generator) || function (thisArg, body) { var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; function verb(n) { return function (v) { return step([n, v]); }; } function step(op) { if (f) throw new TypeError("Generator is already executing."); while (_) try { if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; if (y = 0, t) op = [op[0] & 2, t.value]; switch (op[0]) { case 0: case 1: t = op; break; case 4: _.label++; return { value: op[1], done: false }; case 5: _.label++; y = op[1]; op = [0]; continue; case 7: op = _.ops.pop(); _.trys.pop(); continue; default: if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } if (t[2]) _.ops.pop(); _.trys.pop(); continue; } op = body.call(thisArg, _); } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; } }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.Model = void 0; var connection_1 = __importDefault(require("./connection")); var pagination_1 = require("./pagination"); var uuid_1 = require("./uuid"); var utils_schema_1 = require("./utils/utils.schema"); var search_1 = require("./search"); var Model = /** @class */ (function () { function Model(name, options) { this.scope = '_default'; this.schema = { createdAt: 'date', updatedAt: 'date', }; // this.collection = SofaConnection.Instance.getCollection(); this.collectionName = name; if (options) { this.scope = (options && options.scope) || '_default'; this.schema = __assign(__assign({}, ((options && options.schema) || {})), { createdAt: 'date', updatedAt: 'date' }); } } /** * Refresh and get default collection from SofaConnection * Because SofaConnection is a singleton, sometimes it might be undefined depending when model was created * So we have to call it from all model methods * to avoid error `Cannot read property 'defaultCollection' of null` */ Model.prototype.fresh = function () { this.collection = connection_1.default.Instance.getCollection(); }; /** Get this collection * getCollection */ Model.prototype.getCollection = function () { this.fresh(); return this.collection; }; /** * create */ Model.prototype.create = function (data) { return __awaiter(this, void 0, void 0, function () { var id, createdData, error_1; return __generator(this, function (_a) { switch (_a.label) { case 0: this.fresh(); id = (0, uuid_1.generateUUID)(); createdData = __assign(__assign({ id: id }, data), { createdAt: new Date(), updatedAt: new Date(), _type: this.collectionName, _scope: this.scope }); _a.label = 1; case 1: _a.trys.push([1, 3, , 4]); return [4 /*yield*/, this.collection.upsert(createdData.id, createdData)]; case 2: _a.sent(); return [2 /*return*/, (0, utils_schema_1.parseSchema)(this.schema, createdData)]; case 3: error_1 = _a.sent(); throw error_1; case 4: return [2 /*return*/]; } }); }); }; /** * findById */ Model.prototype.findById = function (id) { return __awaiter(this, void 0, void 0, function () { var data, error_2; return __generator(this, function (_a) { switch (_a.label) { case 0: this.fresh(); _a.label = 1; case 1: _a.trys.push([1, 3, , 4]); return [4 /*yield*/, this.collection.get(id)]; case 2: data = _a.sent(); return [2 /*return*/, (0, utils_schema_1.parseSchema)(this.schema, data.content)]; case 3: error_2 = _a.sent(); throw error_2; case 4: return [2 /*return*/]; } }); }); }; /** * update */ Model.prototype.updateById = function (id, data) { return __awaiter(this, void 0, void 0, function () { var updatedDocument, error_3; return __generator(this, function (_a) { switch (_a.label) { case 0: this.fresh(); updatedDocument = __assign(__assign({}, data), { id: id, updatedAt: new Date(), _type: this.collectionName, _scope: this.scope }); _a.label = 1; case 1: _a.trys.push([1, 3, , 4]); return [4 /*yield*/, this.collection.replace(id, updatedDocument)]; case 2: _a.sent(); return [2 /*return*/, (0, utils_schema_1.parseSchema)(this.schema, updatedDocument)]; case 3: error_3 = _a.sent(); throw error_3; case 4: return [2 /*return*/]; } }); }); }; /** * save */ Model.prototype.save = function (data) { return __awaiter(this, void 0, void 0, function () { var id, updatedDocument, error_4; return __generator(this, function (_a) { switch (_a.label) { case 0: this.fresh(); id = data && data.id; updatedDocument = __assign(__assign({}, data), { id: id, updatedAt: new Date(), _type: this.collectionName, _scope: this.scope }); _a.label = 1; case 1: _a.trys.push([1, 3, , 4]); if (!id) { throw new Error('document must have id'); } return [4 /*yield*/, this.collection.replace(id, updatedDocument)]; case 2: _a.sent(); return [2 /*return*/, (0, utils_schema_1.parseSchema)(this.schema, updatedDocument)]; case 3: error_4 = _a.sent(); console.error(error_4); throw error_4; case 4: return [2 /*return*/]; } }); }); }; Model.prototype.delete = function (id) { return __awaiter(this, void 0, void 0, function () { var error_5; return __generator(this, function (_a) { switch (_a.label) { case 0: this.fresh(); _a.label = 1; case 1: _a.trys.push([1, 3, , 4]); return [4 /*yield*/, this.collection.remove(id)]; case 2: _a.sent(); return [2 /*return*/, true]; case 3: error_5 = _a.sent(); throw error_5; case 4: return [2 /*return*/]; } }); }); }; /** * Pagination * select = ['id', 'createdAt'] where = { where: { owner: { $eq: "stoqey" }, _type: { $eq: "Trade" } }, }, page = 0, limit = 10, orderBy = { createdAt: "DESC" }, * @param args PaginationArgs */ Model.prototype.pagination = function (_a) { var select = _a.select, where = _a.where, orderBy = _a.orderBy, limit = _a.limit, page = _a.page, _b = _a.customQuery, customQuery = _b === void 0 ? {} : _b; return __awaiter(this, void 0, void 0, function () { var whereEx, bucketName, rows; var _this = this; return __generator(this, function (_c) { switch (_c.label) { case 0: this.fresh(); // refresh whereEx = { _type: { $eq: this.collectionName } }; if (where) { whereEx = __assign(__assign({}, whereEx), where); } bucketName = connection_1.default.Instance.bucketName; return [4 /*yield*/, (0, pagination_1.Pagination)({ bucketName: bucketName, select: select, where: __assign({ where: whereEx }, customQuery), limit: limit, page: page, orderBy: orderBy, })]; case 1: rows = _c.sent(); return [2 /*return*/, rows.map(function (r) { return (0, utils_schema_1.parseSchema)(_this.schema, r); })]; } }); }); }; /** * Run a custom query with model parsing * @param { select, query } * @returns */ Model.prototype.customQuery = function (_a) { var params = _a.params, limit = _a.limit, query = _a.query; return __awaiter(this, void 0, void 0, function () { var response; return __generator(this, function (_b) { switch (_b.label) { case 0: this.fresh(); // refresh return [4 /*yield*/, (0, search_1.CustomQuery)({ limit: limit, params: params, query: query, })]; case 1: response = _b.sent(); return [2 /*return*/, response]; } }); }); }; Model.prototype.parse = function (data) { this.fresh(); // refresh return (0, utils_schema_1.parseSchema)(this.schema, data); }; return Model; }()); exports.Model = Model; exports.default = Model; //# sourceMappingURL=model.js.map