UNPKG

@commodo/fields-storage-nedb

Version:

We're working hard to get all the docs in order. New articles will be added daily.

224 lines (178 loc) 5.99 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty")); var _isId = _interopRequireDefault(require("./isId")); var _Database = _interopRequireDefault(require("./Database")); function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; } function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { (0, _defineProperty2.default)(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; } class NeDbDriver { constructor({ collections, database } = {}) { this.database = database || new _Database.default(); this.collections = _objectSpread({ prefix: "", naming: null }, collections); } async create(items) { for (let i = 0; i < items.length; i++) { const { name, data } = items[i]; await this.getDatabase().collection(this.getCollectionName(name)).insert(data); } return true; } async update(items) { for (let i = 0; i < items.length; i++) { const { name, query, data } = items[i]; const collection = this.getCollectionName(name); await this.getDatabase().collection(collection).update(query, { $set: data }, { multi: true }); } return true; } // eslint-disable-next-line async delete({ name, options }) { const clonedOptions = _objectSpread({}, options); NeDbDriver.__prepareSearchOption(clonedOptions); await this.getDatabase().collection(this.getCollectionName(name)).remove(clonedOptions.query, { multi: true }); return true; } async find({ name, options }) { const clonedOptions = _objectSpread({ limit: 0, offset: 0 }, options); NeDbDriver.__prepareSearchOption(clonedOptions); NeDbDriver.__prepareProjectFields(clonedOptions); const projection = {}; if (Array.isArray(options.fields) && options.fields.length > 0) { for (let i = 0; i < options.fields.length; i++) { projection[options.fields[i]] = 1; } } const result = await this.getDatabase().collection(this.getCollectionName(name)).find(clonedOptions.query, projection).limit(clonedOptions.limit).skip(clonedOptions.offset).sort(clonedOptions.sort); return [result, {}]; } async findOne({ name, options }) { const clonedOptions = _objectSpread({}, options); NeDbDriver.__prepareSearchOption(clonedOptions); NeDbDriver.__prepareProjectFields(clonedOptions); const projection = {}; if (Array.isArray(options.fields) && options.fields.length > 0) { for (let i = 0; i < options.fields.length; i++) { projection[options.fields[i]] = 1; } } const [result] = await this.getDatabase().collection(this.getCollectionName(name)).find(clonedOptions.query, projection).limit(1).sort(clonedOptions.sort); return result; } async count({ name, options }) { const clonedOptions = _objectSpread({}, options); NeDbDriver.__prepareSearchOption(clonedOptions); return await this.getDatabase().collection(this.getCollectionName(name)).count(clonedOptions.query); } isId(value) { return (0, _isId.default)(value); } getDatabase() { return this.database; } setCollectionPrefix(collectionPrefix) { this.collections.prefix = collectionPrefix; return this; } getCollectionPrefix() { return this.collections.prefix; } setCollectionNaming(collectionNameValue) { this.collections.naming = collectionNameValue; return this; } getCollectionNaming() { return this.collections.naming; } getCollectionName(name) { const getCollectionName = this.getCollectionNaming(); if (typeof getCollectionName === "function") { return getCollectionName({ name, driver: this }); } return this.collections.prefix + name; } static __prepareSearchOption(options) { // Here we handle search (if passed) - we transform received arguments into linked LIKE statements. if (options.search && options.search.query) { const { query, operator, fields } = options.search; const searches = []; fields.forEach(field => { searches.push({ [field]: { $regex: `.*${query}.*`, $options: "i" } }); }); const search = { [operator === "and" ? "$and" : "$or"]: searches }; if (options.query instanceof Object) { options.query = { $and: [search, options.query] }; } else { options.query = search; } delete options.search; } } static __prepareProjectFields(options) { // Here we convert requested fields into a "project" parameter if (options.fields) { options.project = options.fields.reduce((acc, item) => { acc[item] = 1; return acc; }, { id: 1 }); delete options.fields; } } } var _default = NeDbDriver; exports.default = _default; //# sourceMappingURL=NeDbDriver.js.map