mongodb-adapter.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713
  1. /* eslint no-unused-vars: 0 */
  2. import {ObjectId} from 'mongodb';
  3. import {MongoClient} from 'mongodb';
  4. import {isIsoDate} from './utils/index.js';
  5. import {isObjectId} from './utils/index.js';
  6. import {Adapter} from '@e22m4u/js-repository';
  7. import {DataType} from '@e22m4u/js-repository';
  8. import {capitalize} from '@e22m4u/js-repository';
  9. import {createMongodbUrl} from './utils/index.js';
  10. import {ServiceContainer} from '@e22m4u/js-service';
  11. import {transformValuesDeep} from './utils/index.js';
  12. import {stringToRegexp} from '@e22m4u/js-repository';
  13. import {selectObjectKeys} from '@e22m4u/js-repository';
  14. import {ModelDefinitionUtils} from '@e22m4u/js-repository';
  15. import {InvalidArgumentError} from '@e22m4u/js-repository';
  16. import {InvalidOperatorValueError} from '@e22m4u/js-repository';
  17. /**
  18. * Mongodb option names.
  19. * 5.8.1
  20. *
  21. * @type {string[]}
  22. */
  23. const MONGODB_OPTION_NAMES = [
  24. 'appname',
  25. 'authMechanism',
  26. 'authMechanismProperties',
  27. 'authSource',
  28. 'compressors',
  29. 'connectTimeoutMS',
  30. 'directConnection',
  31. 'heartbeatFrequencyMS',
  32. 'journal',
  33. 'loadBalanced',
  34. 'localThresholdMS',
  35. 'maxIdleTimeMS',
  36. 'maxPoolSize',
  37. 'maxConnecting',
  38. 'maxStalenessSeconds',
  39. 'minPoolSize',
  40. 'proxyHost',
  41. 'proxyPort',
  42. 'proxyUsername',
  43. 'proxyPassword',
  44. 'readConcernLevel',
  45. 'readPreference',
  46. 'readPreferenceTags',
  47. 'replicaSet',
  48. 'retryReads',
  49. 'retryWrites',
  50. 'serverSelectionTimeoutMS',
  51. 'serverSelectionTryOnce',
  52. 'socketTimeoutMS',
  53. 'srvMaxHosts',
  54. 'srvServiceName',
  55. 'ssl',
  56. 'timeoutMS',
  57. 'tls',
  58. 'tlsAllowInvalidCertificates',
  59. 'tlsAllowInvalidHostnames',
  60. 'tlsCAFile',
  61. 'tlsCertificateKeyFile',
  62. 'tlsCertificateKeyFilePassword',
  63. 'tlsInsecure',
  64. 'w',
  65. 'waitQueueTimeoutMS',
  66. 'wTimeoutMS',
  67. 'zlibCompressionLevel',
  68. ];
  69. /**
  70. * Default settings.
  71. *
  72. * @type {object}
  73. */
  74. const DEFAULT_SETTINGS = {
  75. // connectTimeoutMS: 2500,
  76. // serverSelectionTimeoutMS: 2500,
  77. };
  78. /**
  79. * Mongodb adapter.
  80. */
  81. export class MongodbAdapter extends Adapter {
  82. /**
  83. * Mongodb instance.
  84. *
  85. * @type {MongoClient}
  86. * @private
  87. */
  88. _client;
  89. /**
  90. * Client.
  91. *
  92. * @returns {MongoClient}
  93. */
  94. get client() {
  95. return this._client;
  96. }
  97. /**
  98. * Collections.
  99. *
  100. * @type {Map<any, any>}
  101. * @private
  102. */
  103. _collections = new Map();
  104. /**
  105. * Constructor.
  106. *
  107. * @param {ServiceContainer} container
  108. * @param settings
  109. */
  110. constructor(container, settings) {
  111. settings = Object.assign({}, DEFAULT_SETTINGS, settings || {});
  112. settings.protocol = settings.protocol || 'mongodb';
  113. settings.hostname = settings.hostname || settings.host || '127.0.0.1';
  114. settings.port = settings.port || 27017;
  115. settings.database = settings.database || settings.db || 'database';
  116. super(container, settings);
  117. const options = selectObjectKeys(this.settings, MONGODB_OPTION_NAMES);
  118. const url = createMongodbUrl(this.settings);
  119. this._client = new MongoClient(url, options);
  120. }
  121. /**
  122. * Get id prop name.
  123. *
  124. * @param modelName
  125. */
  126. _getIdPropName(modelName) {
  127. return this.getService(ModelDefinitionUtils).getPrimaryKeyAsPropertyName(
  128. modelName,
  129. );
  130. }
  131. /**
  132. * Get id col name.
  133. *
  134. * @param modelName
  135. */
  136. _getIdColName(modelName) {
  137. return this.getService(ModelDefinitionUtils).getPrimaryKeyAsColumnName(
  138. modelName,
  139. );
  140. }
  141. /**
  142. * Coerce id.
  143. *
  144. * @param value
  145. * @return {ObjectId|*}
  146. * @private
  147. */
  148. _coerceId(value) {
  149. if (value == null) return value;
  150. if (isObjectId(value)) return new ObjectId(value);
  151. return value;
  152. }
  153. /**
  154. * To database.
  155. *
  156. * @param {string} modelName
  157. * @param {object} modelData
  158. * @return {object}
  159. * @private
  160. */
  161. _toDatabase(modelName, modelData) {
  162. const tableData = this.getService(
  163. ModelDefinitionUtils,
  164. ).convertPropertyNamesToColumnNames(modelName, modelData);
  165. const idColName = this._getIdColName(modelName);
  166. if (idColName !== 'id' && idColName !== '_id')
  167. throw new InvalidArgumentError(
  168. 'MongoDB is not supporting custom names of the primary key. ' +
  169. 'Do use "id" as a primary key instead of %v.',
  170. idColName,
  171. );
  172. if (idColName in tableData && idColName !== '_id') {
  173. tableData._id = tableData[idColName];
  174. delete tableData[idColName];
  175. }
  176. return transformValuesDeep(tableData, value => {
  177. if (value instanceof ObjectId) return value;
  178. if (value instanceof Date) return value;
  179. if (isObjectId(value)) return new ObjectId(value);
  180. if (isIsoDate(value)) return new Date(value);
  181. return value;
  182. });
  183. }
  184. /**
  185. * From database.
  186. *
  187. * @param {string} modelName
  188. * @param {object} tableData
  189. * @return {object}
  190. * @private
  191. */
  192. _fromDatabase(modelName, tableData) {
  193. if ('_id' in tableData) {
  194. const idColName = this._getIdColName(modelName);
  195. if (idColName !== 'id' && idColName !== '_id')
  196. throw new InvalidArgumentError(
  197. 'MongoDB is not supporting custom names of the primary key. ' +
  198. 'Do use "id" as a primary key instead of %v.',
  199. idColName,
  200. );
  201. if (idColName !== '_id') {
  202. tableData[idColName] = tableData._id;
  203. delete tableData._id;
  204. }
  205. }
  206. const modelData = this.getService(
  207. ModelDefinitionUtils,
  208. ).convertColumnNamesToPropertyNames(modelName, tableData);
  209. return transformValuesDeep(modelData, value => {
  210. if (value instanceof ObjectId) return String(value);
  211. if (value instanceof Date) return value.toISOString();
  212. return value;
  213. });
  214. }
  215. /**
  216. * Get collection.
  217. *
  218. * @param {string} modelName
  219. * @return {*}
  220. * @private
  221. */
  222. _getCollection(modelName) {
  223. let collection = this._collections.get(modelName);
  224. if (collection) return collection;
  225. const tableName =
  226. this.getService(ModelDefinitionUtils).getTableNameByModelName(modelName);
  227. collection = this.client.db(this.settings.database).collection(tableName);
  228. this._collections.set(modelName, collection);
  229. return collection;
  230. }
  231. /**
  232. * Get id type.
  233. *
  234. * @param modelName
  235. * @return {string|*}
  236. * @private
  237. */
  238. _getIdType(modelName) {
  239. const utils = this.getService(ModelDefinitionUtils);
  240. const pkPropName = utils.getPrimaryKeyAsPropertyName(modelName);
  241. return utils.getDataTypeByPropertyName(modelName, pkPropName);
  242. }
  243. /**
  244. * Build projection.
  245. *
  246. * @param {string} modelName
  247. * @param {string|string[]} fields
  248. * @return {Record<string, number>|undefined}
  249. * @private
  250. */
  251. _buildProjection(modelName, fields) {
  252. if (fields == null) return;
  253. if (Array.isArray(fields) === false) fields = [fields];
  254. if (!fields.length) return;
  255. if (fields.indexOf('_id') === -1) fields.push('_id');
  256. return fields.reduce((acc, field) => {
  257. if (!field || typeof field !== 'string')
  258. throw new InvalidArgumentError(
  259. 'The provided option "fields" should be a non-empty String ' +
  260. 'or an Array of non-empty String, but %v given.',
  261. field,
  262. );
  263. let colName = this._getColName(modelName, field);
  264. acc[colName] = 1;
  265. return acc;
  266. }, {});
  267. }
  268. /**
  269. * Get col name.
  270. *
  271. * @param {string} modelName
  272. * @param {string} propName
  273. * @return {string}
  274. * @private
  275. */
  276. _getColName(modelName, propName) {
  277. if (!propName || typeof propName !== 'string')
  278. throw new InvalidArgumentError(
  279. 'A property name must be a non-empty String, but %v given.',
  280. propName,
  281. );
  282. const utils = this.getService(ModelDefinitionUtils);
  283. let colName = propName;
  284. try {
  285. colName = utils.getColumnNameByPropertyName(modelName, propName);
  286. } catch (error) {
  287. if (
  288. !(error instanceof InvalidArgumentError) ||
  289. error.message.indexOf('does not have the property') === -1
  290. ) {
  291. throw error;
  292. }
  293. }
  294. return colName;
  295. }
  296. /**
  297. * Build sort.
  298. *
  299. * @param {string} modelName
  300. * @param {string|string[]} clause
  301. * @return {object|undefined}
  302. * @private
  303. */
  304. _buildSort(modelName, clause) {
  305. if (!clause) return;
  306. clause = Array.isArray(clause) ? clause : [clause];
  307. if (!clause.length) return;
  308. const utils = this.getService(ModelDefinitionUtils);
  309. const idPropName = this._getIdPropName(modelName);
  310. return clause.reduce((acc, order) => {
  311. if (!order || typeof order !== 'string')
  312. throw new InvalidArgumentError(
  313. 'A field order must be a non-empty String, but %v given.',
  314. order,
  315. );
  316. const direction = order.match(/\s+(A|DE)SC$/);
  317. let key = order.replace(/\s+(A|DE)SC$/, '').trim();
  318. if (key === idPropName) {
  319. key = '_id';
  320. } else {
  321. try {
  322. key = utils.getColumnNameByPropertyName(modelName, key);
  323. } catch (error) {
  324. if (
  325. !(error instanceof InvalidArgumentError) ||
  326. error.message.indexOf('does not have the property') === -1
  327. ) {
  328. throw error;
  329. }
  330. }
  331. }
  332. acc[key] = direction && direction[1] === 'DE' ? -1 : 1;
  333. return acc;
  334. }, {});
  335. }
  336. /**
  337. * Build query.
  338. *
  339. * @param {string} modelName
  340. * @param {object} clause
  341. * @return {object}
  342. * @private
  343. */
  344. _buildQuery(modelName, clause) {
  345. const query = {};
  346. if (!clause || typeof clause !== 'object') return query;
  347. const idPropName = this._getIdPropName(modelName);
  348. Object.keys(clause).forEach(key => {
  349. let cond = clause[key];
  350. // and/or/nor clause
  351. if (key === 'and' || key === 'or' || key === 'nor') {
  352. if (Array.isArray(cond))
  353. cond = cond.map(c => this._buildQuery(modelName, c));
  354. query['$' + key] = cond;
  355. return;
  356. }
  357. // id
  358. if (key === idPropName) {
  359. key = '_id';
  360. } else {
  361. key = this._getColName(modelName, key);
  362. }
  363. // string
  364. if (typeof cond === 'string') {
  365. query[key] = this._coerceId(cond);
  366. return;
  367. }
  368. // ObjectId
  369. if (cond instanceof ObjectId) {
  370. query[key] = cond;
  371. return;
  372. }
  373. // operator
  374. if (cond && cond.constructor && cond.constructor.name === 'Object') {
  375. // eq
  376. if ('eq' in cond) {
  377. query[key] = this._coerceId(cond.eq);
  378. }
  379. // neq
  380. if ('neq' in cond) {
  381. query[key] = {$ne: this._coerceId(cond.neq)};
  382. }
  383. // gt
  384. if ('gt' in cond) {
  385. query[key] = {$gt: cond.gt};
  386. }
  387. // lt
  388. if ('lt' in cond) {
  389. query[key] = {$lt: cond.lt};
  390. }
  391. // gte
  392. if ('gte' in cond) {
  393. query[key] = {$gte: cond.gte};
  394. }
  395. // lte
  396. if ('lte' in cond) {
  397. query[key] = {$lte: cond.lte};
  398. }
  399. // inq
  400. if ('inq' in cond) {
  401. if (!cond.inq || !Array.isArray(cond.inq))
  402. throw new InvalidOperatorValueError(
  403. 'inq',
  404. 'an Array of possible values',
  405. cond.inq,
  406. );
  407. query[key] = {$in: cond.inq.map(v => this._coerceId(v))};
  408. }
  409. // nin
  410. if ('nin' in cond) {
  411. if (!cond.nin || !Array.isArray(cond.nin))
  412. throw new InvalidOperatorValueError(
  413. 'nin',
  414. 'an Array of possible values',
  415. cond,
  416. );
  417. query[key] = {$nin: cond.nin.map(v => this._coerceId(v))};
  418. }
  419. // between
  420. if ('between' in cond) {
  421. if (!Array.isArray(cond.between) || cond.between.length !== 2)
  422. throw new InvalidOperatorValueError(
  423. 'between',
  424. 'an Array of 2 elements',
  425. cond.between,
  426. );
  427. query[key] = {$gte: cond.between[0], $lte: cond.between[1]};
  428. }
  429. // exists
  430. if ('exists' in cond) {
  431. if (typeof cond.exists !== 'boolean')
  432. throw new InvalidOperatorValueError(
  433. 'exists',
  434. 'a Boolean',
  435. cond.exists,
  436. );
  437. query[key] = {$exists: cond.exists};
  438. }
  439. // like
  440. if ('like' in cond) {
  441. if (typeof cond.like !== 'string' && !(cond.like instanceof RegExp))
  442. throw new InvalidOperatorValueError(
  443. 'like',
  444. 'a String or RegExp',
  445. cond.like,
  446. );
  447. query[key] = {$regex: stringToRegexp(cond.like)};
  448. }
  449. // nlike
  450. if ('nlike' in cond) {
  451. if (typeof cond.nlike !== 'string' && !(cond.nlike instanceof RegExp))
  452. throw new InvalidOperatorValueError(
  453. 'nlike',
  454. 'a String or RegExp',
  455. cond.nlike,
  456. );
  457. query[key] = {$not: stringToRegexp(cond.nlike)};
  458. }
  459. // ilike
  460. if ('ilike' in cond) {
  461. if (typeof cond.ilike !== 'string' && !(cond.ilike instanceof RegExp))
  462. throw new InvalidOperatorValueError(
  463. 'ilike',
  464. 'a String or RegExp',
  465. cond.ilike,
  466. );
  467. query[key] = {$regex: stringToRegexp(cond.ilike, 'i')};
  468. }
  469. // nilike
  470. if ('nilike' in cond) {
  471. if (
  472. typeof cond.nilike !== 'string' &&
  473. !(cond.nilike instanceof RegExp)
  474. ) {
  475. throw new InvalidOperatorValueError(
  476. 'nilike',
  477. 'a String or RegExp',
  478. cond.nilike,
  479. );
  480. }
  481. query[key] = {$not: stringToRegexp(cond.nilike, 'i')};
  482. }
  483. // regexp and flags (optional)
  484. if ('regexp' in cond) {
  485. if (
  486. typeof cond.regexp !== 'string' &&
  487. !(cond.regexp instanceof RegExp)
  488. ) {
  489. throw new InvalidOperatorValueError(
  490. 'regexp',
  491. 'a String or RegExp',
  492. cond.regexp,
  493. );
  494. }
  495. const flags = cond.flags || undefined;
  496. if (flags && typeof flags !== 'string')
  497. throw new InvalidArgumentError(
  498. 'RegExp flags must be a String, but %v given.',
  499. cond.flags,
  500. );
  501. query[key] = {$regex: stringToRegexp(cond.regexp, flags)};
  502. }
  503. return;
  504. }
  505. // unknown
  506. query[key] = cond;
  507. });
  508. return query;
  509. }
  510. /**
  511. * Create.
  512. *
  513. * @param {string} modelName
  514. * @param {object} modelData
  515. * @param {object|undefined} filter
  516. * @return {Promise<object>}
  517. */
  518. async create(modelName, modelData, filter = undefined) {
  519. const idPropName = this._getIdPropName(modelName);
  520. const idValue = modelData[idPropName];
  521. if (idValue == null) {
  522. const pkType = this._getIdType(modelName);
  523. if (pkType !== DataType.STRING && pkType !== DataType.ANY)
  524. throw new InvalidArgumentError(
  525. 'MongoDB unable to generate primary keys of %s. ' +
  526. 'Do provide your own value for the %v property ' +
  527. 'or set property type to String.',
  528. capitalize(pkType),
  529. idPropName,
  530. );
  531. delete modelData[idPropName];
  532. }
  533. const tableData = this._toDatabase(modelName, modelData);
  534. const table = this._getCollection(modelName);
  535. const {insertedId} = await table.insertOne(tableData);
  536. const projection = this._buildProjection(
  537. modelName,
  538. filter && filter.fields,
  539. );
  540. const insertedData = await table.findOne({_id: insertedId}, {projection});
  541. return this._fromDatabase(modelName, insertedData);
  542. }
  543. /**
  544. * Replace by id.
  545. *
  546. * @param {string} modelName
  547. * @param {string|number} id
  548. * @param {object} modelData
  549. * @param {object|undefined} filter
  550. * @return {Promise<object>}
  551. */
  552. async replaceById(modelName, id, modelData, filter = undefined) {
  553. id = this._coerceId(id);
  554. const idPropName = this._getIdPropName(modelName);
  555. modelData[idPropName] = id;
  556. const tableData = this._toDatabase(modelName, modelData);
  557. const table = this._getCollection(modelName);
  558. const {modifiedCount} = await table.replaceOne({_id: id}, tableData);
  559. if (modifiedCount < 1)
  560. throw new InvalidArgumentError('Identifier %v is not found.', String(id));
  561. const projection = this._buildProjection(
  562. modelName,
  563. filter && filter.fields,
  564. );
  565. const replacedData = await table.findOne({_id: id}, {projection});
  566. return this._fromDatabase(modelName, replacedData);
  567. }
  568. /**
  569. * Patch by id.
  570. *
  571. * @param {string} modelName
  572. * @param {string|number} id
  573. * @param {object} modelData
  574. * @param {object|undefined} filter
  575. * @return {Promise<object>}
  576. */
  577. async patchById(modelName, id, modelData, filter = undefined) {
  578. id = this._coerceId(id);
  579. const idPropName = this._getIdPropName(modelName);
  580. delete modelData[idPropName];
  581. const tableData = this._toDatabase(modelName, modelData);
  582. const table = this._getCollection(modelName);
  583. const {modifiedCount} = await table.updateOne({_id: id}, {$set: tableData});
  584. if (modifiedCount < 1)
  585. throw new InvalidArgumentError('Identifier %v is not found.', String(id));
  586. const projection = this._buildProjection(
  587. modelName,
  588. filter && filter.fields,
  589. );
  590. const patchedData = await table.findOne({_id: id}, {projection});
  591. return this._fromDatabase(modelName, patchedData);
  592. }
  593. /**
  594. * Find.
  595. *
  596. * @param {string} modelName
  597. * @param {object|undefined} filter
  598. * @return {Promise<object[]>}
  599. */
  600. async find(modelName, filter = undefined) {
  601. filter = filter || {};
  602. const query = this._buildQuery(modelName, filter.where);
  603. const sort = this._buildSort(modelName, filter.order);
  604. const limit = filter.limit || undefined;
  605. const skip = filter.skip || undefined;
  606. const projection = this._buildProjection(modelName, filter.fields);
  607. const collection = this._getCollection(modelName);
  608. const options = {sort, limit, skip, projection};
  609. const tableItems = await collection.find(query, options).toArray();
  610. return tableItems.map(v => this._fromDatabase(modelName, v));
  611. }
  612. /**
  613. * Find by id.
  614. *
  615. * @param {string} modelName
  616. * @param {string|number} id
  617. * @param {object|undefined} filter
  618. * @return {Promise<object>}
  619. */
  620. async findById(modelName, id, filter = undefined) {
  621. id = this._coerceId(id);
  622. const table = this._getCollection(modelName);
  623. const projection = this._buildProjection(
  624. modelName,
  625. filter && filter.fields,
  626. );
  627. const patchedData = await table.findOne({_id: id}, {projection});
  628. if (!patchedData)
  629. throw new InvalidArgumentError('Identifier %v is not found.', String(id));
  630. return this._fromDatabase(modelName, patchedData);
  631. }
  632. /**
  633. * Delete.
  634. *
  635. * @param {string} modelName
  636. * @param {object|undefined} where
  637. * @return {Promise<number>}
  638. */
  639. async delete(modelName, where = undefined) {
  640. const table = this._getCollection(modelName);
  641. const query = this._buildQuery(modelName, where);
  642. const {deletedCount} = await table.deleteMany(query);
  643. return deletedCount;
  644. }
  645. /**
  646. * Delete by id.
  647. *
  648. * @param {string} modelName
  649. * @param {string|number} id
  650. * @return {Promise<boolean>}
  651. */
  652. async deleteById(modelName, id) {
  653. id = this._coerceId(id);
  654. const table = this._getCollection(modelName);
  655. const {deletedCount} = await table.deleteOne({_id: id});
  656. return deletedCount > 0;
  657. }
  658. /**
  659. * Exists.
  660. *
  661. * @param {string} modelName
  662. * @param {string|number} id
  663. * @return {Promise<boolean>}
  664. */
  665. async exists(modelName, id) {
  666. id = this._coerceId(id);
  667. const table = this._getCollection(modelName);
  668. const result = await table.findOne({_id: id}, {});
  669. return result != null;
  670. }
  671. /**
  672. * Count.
  673. *
  674. * @param {string} modelName
  675. * @param {object|undefined} where
  676. * @return {Promise<number>}
  677. */
  678. async count(modelName, where = undefined) {
  679. const query = this._buildQuery(modelName, where);
  680. const table = this._getCollection(modelName);
  681. return await table.count(query);
  682. }
  683. }