mongodb-adapter.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714
  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 == null) return;
  306. if (Array.isArray(clause) === false) 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. 'The provided option "order" should be a non-empty String ' +
  314. 'or an Array of non-empty String, but %v given.',
  315. order,
  316. );
  317. const direction = order.match(/\s+(A|DE)SC$/);
  318. let field = order.replace(/\s+(A|DE)SC$/, '').trim();
  319. if (field === idPropName) {
  320. field = '_id';
  321. } else {
  322. try {
  323. field = utils.getColumnNameByPropertyName(modelName, field);
  324. } catch (error) {
  325. if (
  326. !(error instanceof InvalidArgumentError) ||
  327. error.message.indexOf('does not have the property') === -1
  328. ) {
  329. throw error;
  330. }
  331. }
  332. }
  333. acc[field] = direction && direction[1] === 'DE' ? -1 : 1;
  334. return acc;
  335. }, {});
  336. }
  337. /**
  338. * Build query.
  339. *
  340. * @param {string} modelName
  341. * @param {object} clause
  342. * @return {object}
  343. * @private
  344. */
  345. _buildQuery(modelName, clause) {
  346. const query = {};
  347. if (!clause || typeof clause !== 'object') return query;
  348. const idPropName = this._getIdPropName(modelName);
  349. Object.keys(clause).forEach(key => {
  350. let cond = clause[key];
  351. // and/or/nor clause
  352. if (key === 'and' || key === 'or' || key === 'nor') {
  353. if (Array.isArray(cond))
  354. cond = cond.map(c => this._buildQuery(modelName, c));
  355. query['$' + key] = cond;
  356. return;
  357. }
  358. // id
  359. if (key === idPropName) {
  360. key = '_id';
  361. } else {
  362. key = this._getColName(modelName, key);
  363. }
  364. // string
  365. if (typeof cond === 'string') {
  366. query[key] = this._coerceId(cond);
  367. return;
  368. }
  369. // ObjectId
  370. if (cond instanceof ObjectId) {
  371. query[key] = cond;
  372. return;
  373. }
  374. // operator
  375. if (cond && cond.constructor && cond.constructor.name === 'Object') {
  376. // eq
  377. if ('eq' in cond) {
  378. query[key] = this._coerceId(cond.eq);
  379. }
  380. // neq
  381. if ('neq' in cond) {
  382. query[key] = {$ne: this._coerceId(cond.neq)};
  383. }
  384. // gt
  385. if ('gt' in cond) {
  386. query[key] = {$gt: cond.gt};
  387. }
  388. // lt
  389. if ('lt' in cond) {
  390. query[key] = {$lt: cond.lt};
  391. }
  392. // gte
  393. if ('gte' in cond) {
  394. query[key] = {$gte: cond.gte};
  395. }
  396. // lte
  397. if ('lte' in cond) {
  398. query[key] = {$lte: cond.lte};
  399. }
  400. // inq
  401. if ('inq' in cond) {
  402. if (!cond.inq || !Array.isArray(cond.inq))
  403. throw new InvalidOperatorValueError(
  404. 'inq',
  405. 'an Array of possible values',
  406. cond.inq,
  407. );
  408. query[key] = {$in: cond.inq.map(v => this._coerceId(v))};
  409. }
  410. // nin
  411. if ('nin' in cond) {
  412. if (!cond.nin || !Array.isArray(cond.nin))
  413. throw new InvalidOperatorValueError(
  414. 'nin',
  415. 'an Array of possible values',
  416. cond,
  417. );
  418. query[key] = {$nin: cond.nin.map(v => this._coerceId(v))};
  419. }
  420. // between
  421. if ('between' in cond) {
  422. if (!Array.isArray(cond.between) || cond.between.length !== 2)
  423. throw new InvalidOperatorValueError(
  424. 'between',
  425. 'an Array of 2 elements',
  426. cond.between,
  427. );
  428. query[key] = {$gte: cond.between[0], $lte: cond.between[1]};
  429. }
  430. // exists
  431. if ('exists' in cond) {
  432. if (typeof cond.exists !== 'boolean')
  433. throw new InvalidOperatorValueError(
  434. 'exists',
  435. 'a Boolean',
  436. cond.exists,
  437. );
  438. query[key] = {$exists: cond.exists};
  439. }
  440. // like
  441. if ('like' in cond) {
  442. if (typeof cond.like !== 'string' && !(cond.like instanceof RegExp))
  443. throw new InvalidOperatorValueError(
  444. 'like',
  445. 'a String or RegExp',
  446. cond.like,
  447. );
  448. query[key] = {$regex: stringToRegexp(cond.like)};
  449. }
  450. // nlike
  451. if ('nlike' in cond) {
  452. if (typeof cond.nlike !== 'string' && !(cond.nlike instanceof RegExp))
  453. throw new InvalidOperatorValueError(
  454. 'nlike',
  455. 'a String or RegExp',
  456. cond.nlike,
  457. );
  458. query[key] = {$not: stringToRegexp(cond.nlike)};
  459. }
  460. // ilike
  461. if ('ilike' in cond) {
  462. if (typeof cond.ilike !== 'string' && !(cond.ilike instanceof RegExp))
  463. throw new InvalidOperatorValueError(
  464. 'ilike',
  465. 'a String or RegExp',
  466. cond.ilike,
  467. );
  468. query[key] = {$regex: stringToRegexp(cond.ilike, 'i')};
  469. }
  470. // nilike
  471. if ('nilike' in cond) {
  472. if (
  473. typeof cond.nilike !== 'string' &&
  474. !(cond.nilike instanceof RegExp)
  475. ) {
  476. throw new InvalidOperatorValueError(
  477. 'nilike',
  478. 'a String or RegExp',
  479. cond.nilike,
  480. );
  481. }
  482. query[key] = {$not: stringToRegexp(cond.nilike, 'i')};
  483. }
  484. // regexp and flags (optional)
  485. if ('regexp' in cond) {
  486. if (
  487. typeof cond.regexp !== 'string' &&
  488. !(cond.regexp instanceof RegExp)
  489. ) {
  490. throw new InvalidOperatorValueError(
  491. 'regexp',
  492. 'a String or RegExp',
  493. cond.regexp,
  494. );
  495. }
  496. const flags = cond.flags || undefined;
  497. if (flags && typeof flags !== 'string')
  498. throw new InvalidArgumentError(
  499. 'RegExp flags must be a String, but %v given.',
  500. cond.flags,
  501. );
  502. query[key] = {$regex: stringToRegexp(cond.regexp, flags)};
  503. }
  504. return;
  505. }
  506. // unknown
  507. query[key] = cond;
  508. });
  509. return query;
  510. }
  511. /**
  512. * Create.
  513. *
  514. * @param {string} modelName
  515. * @param {object} modelData
  516. * @param {object|undefined} filter
  517. * @return {Promise<object>}
  518. */
  519. async create(modelName, modelData, filter = undefined) {
  520. const idPropName = this._getIdPropName(modelName);
  521. const idValue = modelData[idPropName];
  522. if (idValue == null) {
  523. const pkType = this._getIdType(modelName);
  524. if (pkType !== DataType.STRING && pkType !== DataType.ANY)
  525. throw new InvalidArgumentError(
  526. 'MongoDB unable to generate primary keys of %s. ' +
  527. 'Do provide your own value for the %v property ' +
  528. 'or set property type to String.',
  529. capitalize(pkType),
  530. idPropName,
  531. );
  532. delete modelData[idPropName];
  533. }
  534. const tableData = this._toDatabase(modelName, modelData);
  535. const table = this._getCollection(modelName);
  536. const {insertedId} = await table.insertOne(tableData);
  537. const projection = this._buildProjection(
  538. modelName,
  539. filter && filter.fields,
  540. );
  541. const insertedData = await table.findOne({_id: insertedId}, {projection});
  542. return this._fromDatabase(modelName, insertedData);
  543. }
  544. /**
  545. * Replace by id.
  546. *
  547. * @param {string} modelName
  548. * @param {string|number} id
  549. * @param {object} modelData
  550. * @param {object|undefined} filter
  551. * @return {Promise<object>}
  552. */
  553. async replaceById(modelName, id, modelData, filter = undefined) {
  554. id = this._coerceId(id);
  555. const idPropName = this._getIdPropName(modelName);
  556. modelData[idPropName] = id;
  557. const tableData = this._toDatabase(modelName, modelData);
  558. const table = this._getCollection(modelName);
  559. const {modifiedCount} = await table.replaceOne({_id: id}, tableData);
  560. if (modifiedCount < 1)
  561. throw new InvalidArgumentError('Identifier %v is not found.', String(id));
  562. const projection = this._buildProjection(
  563. modelName,
  564. filter && filter.fields,
  565. );
  566. const replacedData = await table.findOne({_id: id}, {projection});
  567. return this._fromDatabase(modelName, replacedData);
  568. }
  569. /**
  570. * Patch by id.
  571. *
  572. * @param {string} modelName
  573. * @param {string|number} id
  574. * @param {object} modelData
  575. * @param {object|undefined} filter
  576. * @return {Promise<object>}
  577. */
  578. async patchById(modelName, id, modelData, filter = undefined) {
  579. id = this._coerceId(id);
  580. const idPropName = this._getIdPropName(modelName);
  581. delete modelData[idPropName];
  582. const tableData = this._toDatabase(modelName, modelData);
  583. const table = this._getCollection(modelName);
  584. const {modifiedCount} = await table.updateOne({_id: id}, {$set: tableData});
  585. if (modifiedCount < 1)
  586. throw new InvalidArgumentError('Identifier %v is not found.', String(id));
  587. const projection = this._buildProjection(
  588. modelName,
  589. filter && filter.fields,
  590. );
  591. const patchedData = await table.findOne({_id: id}, {projection});
  592. return this._fromDatabase(modelName, patchedData);
  593. }
  594. /**
  595. * Find.
  596. *
  597. * @param {string} modelName
  598. * @param {object|undefined} filter
  599. * @return {Promise<object[]>}
  600. */
  601. async find(modelName, filter = undefined) {
  602. filter = filter || {};
  603. const query = this._buildQuery(modelName, filter.where);
  604. const sort = this._buildSort(modelName, filter.order);
  605. const limit = filter.limit || undefined;
  606. const skip = filter.skip || undefined;
  607. const projection = this._buildProjection(modelName, filter.fields);
  608. const collection = this._getCollection(modelName);
  609. const options = {sort, limit, skip, projection};
  610. const tableItems = await collection.find(query, options).toArray();
  611. return tableItems.map(v => this._fromDatabase(modelName, v));
  612. }
  613. /**
  614. * Find by id.
  615. *
  616. * @param {string} modelName
  617. * @param {string|number} id
  618. * @param {object|undefined} filter
  619. * @return {Promise<object>}
  620. */
  621. async findById(modelName, id, filter = undefined) {
  622. id = this._coerceId(id);
  623. const table = this._getCollection(modelName);
  624. const projection = this._buildProjection(
  625. modelName,
  626. filter && filter.fields,
  627. );
  628. const patchedData = await table.findOne({_id: id}, {projection});
  629. if (!patchedData)
  630. throw new InvalidArgumentError('Identifier %v is not found.', String(id));
  631. return this._fromDatabase(modelName, patchedData);
  632. }
  633. /**
  634. * Delete.
  635. *
  636. * @param {string} modelName
  637. * @param {object|undefined} where
  638. * @return {Promise<number>}
  639. */
  640. async delete(modelName, where = undefined) {
  641. const table = this._getCollection(modelName);
  642. const query = this._buildQuery(modelName, where);
  643. const {deletedCount} = await table.deleteMany(query);
  644. return deletedCount;
  645. }
  646. /**
  647. * Delete by id.
  648. *
  649. * @param {string} modelName
  650. * @param {string|number} id
  651. * @return {Promise<boolean>}
  652. */
  653. async deleteById(modelName, id) {
  654. id = this._coerceId(id);
  655. const table = this._getCollection(modelName);
  656. const {deletedCount} = await table.deleteOne({_id: id});
  657. return deletedCount > 0;
  658. }
  659. /**
  660. * Exists.
  661. *
  662. * @param {string} modelName
  663. * @param {string|number} id
  664. * @return {Promise<boolean>}
  665. */
  666. async exists(modelName, id) {
  667. id = this._coerceId(id);
  668. const table = this._getCollection(modelName);
  669. const result = await table.findOne({_id: id}, {});
  670. return result != null;
  671. }
  672. /**
  673. * Count.
  674. *
  675. * @param {string} modelName
  676. * @param {object|undefined} where
  677. * @return {Promise<number>}
  678. */
  679. async count(modelName, where = undefined) {
  680. const query = this._buildQuery(modelName, where);
  681. const table = this._getCollection(modelName);
  682. return await table.count(query);
  683. }
  684. }