UNPKG

@berish/orm-rethinkdb-file-adapter

Version:

Адаптер файловой базы данных RethinkDB для @berish/orm

113 lines 4.61 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 }); const emitter_1 = require("@berish/emitter"); const orm_1 = require("@berish/orm"); const r = require("rethinkdb"); class RethinkFileAdapter extends orm_1.BaseFileAdapter { constructor() { super(...arguments); this.connection = null; this._cacheEmitter = new emitter_1.CacheEmitter(); this.tables = []; } // private tablesInWait: { [tableName: string]: Promise<void> } = {}; close() { return __awaiter(this, void 0, void 0, function* () { if (this.connection && this.connection.open) { yield this.connection.close(); } this.params = null; this.connection = null; this.tables = []; }); } initialize(params) { return __awaiter(this, void 0, void 0, function* () { params = params || {}; this.params = params; this.connection = yield r.connect({ db: params.dbName, host: params.host, password: params.password, port: params.port, ssl: params.ssl, user: params.user, }); yield this.db(params.dbName); }); } get(ids, fetchData) { return __awaiter(this, void 0, void 0, function* () { const table = yield this.table(this.params.tableName); let seq = table.getAll(...ids); if (!fetchData) seq = seq.without('data'); const cursor = yield seq.run(this.connection); return cursor.toArray(); }); } create(items) { return __awaiter(this, void 0, void 0, function* () { const table = yield this.table(this.params.tableName); items.forEach(item => (item.createdAt = +new Date())); yield table.insert(items, { conflict: 'replace' }).run(this.connection); }); } delete(ids) { return __awaiter(this, void 0, void 0, function* () { const table = yield this.table(this.params.tableName); yield table .getAll(...ids) .delete() .run(this.connection); }); } db(dbName) { const _db = (dbName) => __awaiter(this, void 0, void 0, function* () { const dbList = yield r.dbList().run(this.connection); if (!dbList.includes(dbName)) { yield r.dbCreate(dbName).run(this.connection); yield r .db(dbName) .wait() .run(this.connection); } return r.db(dbName); }); return this._cacheEmitter.call(dbName, () => _db(dbName)); } table(tableName) { const _table = (tableName) => __awaiter(this, void 0, void 0, function* () { const db = yield this.db(this.params.dbName); if (this.tables.includes(tableName)) { const tableList = yield db.tableList().run(this.connection); if (tableList.includes(tableName)) return db.table(tableName); this.tables.splice(this.tables.indexOf(tableName), 1); return _table(tableName); } const tableList = yield db.tableList().run(this.connection); if (!tableList.includes(tableName)) { yield db.tableCreate(tableName).run(this.connection); yield db .table(tableName) .wait() .run(this.connection); } this.tables.push(tableName); return db.table(tableName); }); return this._cacheEmitter.call(tableName, () => _table(tableName)); } } exports.default = RethinkFileAdapter; //# sourceMappingURL=index.js.map