{"_id":"@bonjourjohn/dbhelper","_rev":"2-7a9761844c208a3fbc2557201021a76d","name":"@bonjourjohn/dbhelper","dist-tags":{"latest":"1.0.1"},"versions":{"1.0.0":{"name":"@bonjourjohn/dbhelper","version":"1.0.0","description":"Abstract Model and Collection classes for native NodeJS Mongodb driver with cache","main":"index.js","directories":{"lib":"lib","test":"test"},"dependencies":{"@bonjourjohn/utils":"^1.0.0","ioredis":"^3.2.2","mongodb":"^3.0.4"},"devDependencies":{"mocha":"^5.0.4","should":"^13.2.1"},"scripts":{"test":"mocha -b"},"repository":{"type":"git","url":"git+https://github.com/nicolasespiau/dbhelper.git"},"keywords":["mongodb","ioredis","cache"],"author":{"name":"ESPIAU Nicolas"},"license":"ISC","bugs":{"url":"https://github.com/nicolasespiau/dbhelper/issues"},"homepage":"https://github.com/nicolasespiau/dbhelper#readme","gitHead":"c6a34f8fa0d4acc6b6dbde17c8c7c5bd2b7525bd","_id":"@bonjourjohn/dbhelper@1.0.0","_npmVersion":"5.6.0","_nodeVersion":"9.9.0","_npmUser":{"name":"bonjourjohn","email":"nicolas.espiau@gmail.com"},"dist":{"integrity":"sha512-iY7XewsylKEK5uIuUioDPTTK7ud4YRg/5qBDJymcVU3J8bI3Vl+ANenGwKHhYkdR1zQAv+4zxJcbEfkGMBdX0w==","shasum":"9b6081189507ac347b99d0601b8436e2a3160636","tarball":"https://registry.npmjs.org/@bonjourjohn/dbhelper/-/dbhelper-1.0.0.tgz","fileCount":13,"unpackedSize":37654,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.4\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJbYCw0CRA9TVsSAnZWagAAUqgP+gJgRPx5MngD85IYREEL\nixIE5sOLJROXVf09BDGA2w4sMpdMnQdGzrvIZeHBMuI9M9Sdz2F/bKpGClwq\nkO/vVKcUIfhvGV8GiJ4JvWC4e8XSYzPXu2XWPvE/Cw8Ure+JDQxF9/aUtNoa\nSKZVINFLkzfWfrVcEf7jXvlgU/uzi8dwwwRwWLPED2HCxtoG43O26IxHwuGn\nmAJaCk2Z3T7aeZaOmMsbLx2fuOSl7iMSgEowZ1odX8wntCz4X803voU3iCEq\nRYyGF4J0gbUEjN5Sqb7a2lWHRSUvbfjHcd5MbwQ1tISitBnfYF2AwYSmWY0x\nK5Xr0j4XwCXQi/XGoxSisOup0lIDRI/b8k9bLepzzw4R5cApT+Vv4WlqTwxR\nsK4l6AyNKTI0hW8pb0m26WBBY4i69N244CdHIitOND42BVcma1RgcH6iYwld\n3QoGtfBCDrWjkzGSSWmjBOn4CdOK0/WO6SwJ9KDFbj9mW1fvyNf7KfKIn2ry\nhNFxCh0MpJmogHFS4qnRjRpakp9O1Eql+HVx9m7zUpsQ3zYv/ZxEPVXPQh44\ndn7u352kHoKcwDw0tk9rH0kE2gkO7mmE3gjR6xHTPdzgE8QA4hI3+u+rDRWm\n7RjHi2Iso9ZUs4xt7Xu1c+EcA6YvHnpS/Vo92YM+9+FGKHJxzSPrKosXG9aQ\n4bGy\r\n=KaIa\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIC3GByG8fmGsa0rakUsLime+1k9PrY/dEeFqbClR0b2wAiEAtmNZbGWv6MRCnmbc61EMdXZViFWFi/BC7X0PfPdK080="}]},"maintainers":[{"name":"bonjourjohn","email":"nicolas.espiau@gmail.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/dbhelper_1.0.0_1533029427547_0.780993792878087"},"_hasShrinkwrap":false},"1.0.1":{"name":"@bonjourjohn/dbhelper","version":"1.0.1","description":"Abstract Model and Collection classes for native NodeJS Mongodb driver with cache","main":"index.js","directories":{"lib":"lib","test":"test"},"dependencies":{"@bonjourjohn/utils":"^1.0.0","ioredis":"^3.2.2","mongodb":"^3.5.9"},"devDependencies":{"mocha":"^8.0.1","should":"^13.2.1"},"scripts":{"test":"mocha -b"},"repository":{"type":"git","url":"git+https://github.com/nicolasespiau/dbhelper.git"},"keywords":["mongodb","ioredis","cache"],"author":{"name":"ESPIAU Nicolas"},"license":"ISC","bugs":{"url":"https://github.com/nicolasespiau/dbhelper/issues"},"homepage":"https://github.com/nicolasespiau/dbhelper#readme","gitHead":"baf0e7dd3d75876c6980e76baa2c4d1d4b7fffb7","_id":"@bonjourjohn/dbhelper@1.0.1","_nodeVersion":"12.6.0","_npmVersion":"6.14.5","dist":{"integrity":"sha512-XXEGtOl2/KenpC3kdmo/qoV3/Td2l9AqgHalbFOiPQtCJXSCSxwzn81p8vk192HsNgYmpuqY06oZ0I/W/lO0jA==","shasum":"cda11d684f7e433ec99d5b47e2b860a4f93f7022","tarball":"https://registry.npmjs.org/@bonjourjohn/dbhelper/-/dbhelper-1.0.1.tgz","fileCount":14,"unpackedSize":39896,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.4\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJe7lNzCRA9TVsSAnZWagAA8ksP/jZPCpmT1ywexIIOCMP/\nB5YLyx7GxxsP9QKYXESJA0dkd8yYrGorTfLOxqpMJdfQ5vdY1T1rgHnLgc7f\n/XxejrJn+dLHiSF1rXNyy+Q2zwFkhwvcmKBmhxqEorH2fDYRjTsF+fVm3Mbj\nQsMA749+XiCAHKklwfxkKutiGdP85K8DSZtB/otGmdyrDlGMMFF9LVJngLwi\n8m3oOnwFndCmihOY5l7LmUUBKcSTjgCqIgex+H8p7S6QG2aJc32mLwf88AxK\nDIbrAMeYfiCKNR1NXfaYEeYj3CT+DtWNyxWN86RcfAu5pDbsZ1oZtHkpQtBq\nPCBgFk7aYGJRycXOet8GHMhgrlSILUqFbEfG2N6ef8UhMHFjJaxvdblh8CjH\ng67ag573Y2iV636HWVDW3XIIGwFCoNQmuikwKzmAuWF6DT7NDISjK5xbrAqV\n/yQDPez6C9cJaBFKxEeOf0pr4bqJyGG2ZM6HZlxYzLWrMPL1onzfrr0cnUAT\nhUEY340LLRp5tlnyxuKWRIr1Oe9PbXkBvwTeXQzLds1QTyO4xAyfpIvyv+PR\n+XueAvydJSQB+roaPEp0om9fqJvPnsRLqwKg4TTvStsv6wRXfmaXRB28Byfs\nxCkURVFeqRTTVtnYUa17fbO6MNOrW2cTXB8b3sA+BdU7EASPoUN30VgoQdh8\nBtSk\r\n=slrY\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEQCIGAbjUu70zJCZqR4UT5KmGZj970p3B9x3ziiRRjLQdgFAiBBSaViln/sAP6vcKPdrPGE8rOwQTRP/6pBOX3OQc0SFw=="}]},"maintainers":[{"name":"bonjourjohn","email":"nicolas.espiau@gmail.com"}],"_npmUser":{"name":"bonjourjohn","email":"nicolas.espiau@gmail.com"},"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/dbhelper_1.0.1_1592677234378_0.8620835770815782"},"_hasShrinkwrap":false}},"time":{"created":"2018-07-31T09:30:27.339Z","1.0.0":"2018-07-31T09:30:28.091Z","modified":"2022-04-04T20:06:28.592Z","1.0.1":"2020-06-20T18:20:34.589Z"},"maintainers":[{"name":"bonjourjohn","email":"nicolas.espiau@gmail.com"}],"description":"Abstract Model and Collection classes for native NodeJS Mongodb driver with cache","homepage":"https://github.com/nicolasespiau/dbhelper#readme","keywords":["mongodb","ioredis","cache"],"repository":{"type":"git","url":"git+https://github.com/nicolasespiau/dbhelper.git"},"author":{"name":"ESPIAU Nicolas"},"bugs":{"url":"https://github.com/nicolasespiau/dbhelper/issues"},"license":"ISC","readme":"# DB HELPER\n\n## Purpose\n\nThis present model class is designed to simplify the development of\nobject oriented projects using the [native MongoDB Node JS driver](https://mongodb.github.io/node-mongodb-native/).\n\nIt provides abstract classes for models and collection.\nIt provides overrides for main insert, find and update functions.\n\nIt also allows developer to set up a cache and to define specific pre insert, pre update and post save behaviors.\n\nThis module also provides a cache client generator based on [ioredis](https://github.com/luin/ioredis).\n\nProvided to Model constructor, it allows you to use a Redis cache to store responses to Mongo queries in order to improve performances.\n\n##  Quick start\n\nFirst you need to create a class extending the `Model`.\n\nThe constructor needs to be overridden. The native constructor takes 3 arguments:\n\n- `dbInstance` is the instance of a Mongo Db\n- `foo` is the collection name this class is related to\n- `cacheClient` is the ioredis cache client this object is going to use. This param is not mandatory\n\n_Foo.class_:\n```javascript\nconst Model = require('@bonjourjohn/dbhelper').Model;\n\nmodule.exports = class Foo extends Model {\n  constructor(dbInstance, cacheClient) {\n    super(dbInstance, 'foo', cacheClient);\n  }\n};\n```\n\nIn your code, where you need your object instance:\n\nwithout cache:\n\n```javascript\nconst MongoClient = require('mongodb').MongoClient;\n\nconst FooClass = require('./path/to/Foo.class');\nconst Mclient = new MongoClient(SERVER, OPTIONS).connect();\nconst MDB = Mclient.db(DBNAME);\n\nconst Foo = new FooClass(MDB);\nFoo.init(); //loads the dedicated MongoCollection into object instance\n```\n\nwith cache:\n\n```javascript\nconst MongoClient = require('mongodb').MongoClient;\n\nconst FooClass = require('./path/to/Foo.class');\nconst Mclient = new MongoClient(SERVER, OPTIONS).connect();\nconst cacheClient = require('@bonjourjohn/dbhelper').Cache(CACHEOPTS);\n\nconst MDB = Mclient.db(DBNAME);\n\nconst Foo = new FooClass(MDB, cacheClient);\nFoo.init(); //loads the dedicated MongoCollection into object instance\n```\n\n## Model native properties list\n\nHere are the properties you can find in an instaciated Model object once it's been initiated (it's ready to work):\n\n- `this.dbInstance` instance of Mongodb Db\n- `this.collection` instance of Mongodb Collection\n- `this.collecionName` string, equivalent to `this.collection.collectionName`\n- `this.cacheClient` instance of Redis client _(if cacheClient set, not mandatory)_\n- `this.useCache` bool, true if `this.cacheClient` is set and ready, false otherwise. It can be set to `false` if you want to skip cache.\n\nTemporary properties:\n\n- `this.doc` JSON object, document that is going to be inserted in collection. It will exist only during insert process (from _preInsert to _postSave)\n- `this.docs` JSON object, documents that are going to be inserted in collection. It will exist only during insert process (from _preInsert to _postSave)\n- `this.update` JSON object, update query that will be used by an update method. It will exist only during update process (from _preUpdate to _postSave)\n\n## Model methods list\n\nHere are the methods provided by this Model class, and the desciption of their usefulness.\n\n### Constructor\n\n`constructor(dbInstance, collectionName, cacheClient)`\n\nCreates a new Model object.\n\n#### Params:\n\n- `dbInstance` Mongodb Db object\n- `collectionName` name of the collection the current instance has to be linked to\n- `cacheClient` *not mandatory* Redis cache client\n\n### init\n\n`init()`\n\nGet the `collectionName` collection in given Db and set it as an object instance local var `this.collection`.\n\n### setCacheClient\n\n`setCacheClient(cacheClient)`\n\nStore given cache client into `this.cacheClient`.\nSet `this.useCache` to true if cache client is ready.\nAttach listeners:\n\n- `on('ready')` to set `this.useCache` to `true`\n- `on('end')` to set `this.useCache` to `false`\n- `on('error')` to set `this.useCache` to `false`\n\n#### Params\n\n- `cacheClient` instance of Redis client\n\n### _preInsert()\n\nCalled in `insertOne` and `insertMany` methods.\nExecute all pre insert actions.\n\nIt can be overriden. Native behavior consist in adding timestamps fields `createdAt` and `updatedAt` to document(s).\n\n### _preUpdate()\n\nCalled in `findOneAndUpdate`, `updateMany` and `updateOne` methods.\nExecute all pre update actions.\n\nIt can be overriden. Native behavior consist in added the update of `updatedAt` field in update query if it's not already present.\n\n### _postSave()\n\nCalled at then end of all saving process: `insertOne`, `insertMany`, `findOneAndUpdate`, `updateMany` and `updateOne`.\nExecute all post save actions.\n\nIt can be overriden. Native behavior consist in clearing cache if cache is on.\n\n### _postDelete()\n\nCalled at then end of all deleting process: `remove`, `findOneAndDelete`.\nExecute all post delete actions.\n\nIt can be overriden. Native behavior consist in clearing cache if cache is on.\n\n### find(), findOne(), findOneAndDelete(), insertOne(), insertMany(), updateOne(), findOneAndUpdate(), updateMany()\n\nThese methods will do the exact same thing as they do when their called on Mongodb Collection objects, except two things:\n\n- they will execute _pre and _post methods before and after process\n- they will try to read in cache and/or write result in cache, or flush cache according to the nature of the operation (read, write, delete)\n\nThey all take the same arguments their analogue in Mongo Collection object, but you can add this option field:\n\n- `skipCache` bool, tell method to skip reading in cache and to go straigth to database.\n\nExample:\n\n```javascript\nconst results = await Foo.findOne({\"fieldName\": \"value\"}, {\"skipCache\": true});\n```\n\n### setupTimestamps()\n\nAdd fields `createdAt` and `updatedAt` into document stored in `this.doc` or into documents stored in `this.docs`.\n\n### updateTimestamps()\n\nAdd `updatedAt` to `$set` part of the query stored in `this.update`. Create the `$set` part if it does not exist.\n\n### findInCache(query, options)\n\nCheck if given query already has a result in cache with given options and return them if it has.\nReturn false otherwise.\n\nCalled by read methods when `this.useCache` is `true`.\n\n\n### storeInCache(query, options, value)\n\nStores `value` into cache under a key generated from given `query` and `options`.\n\nCalled by read methods when `this.useCache` is `true`.\n\n### clearCache()\n\nClears the cache for the current collection name. IE. flush all keys corresponding to the pattern `COLLECTIONNAME*`\n\n## Testing\n\n### Requirements\n\nYou need a MongoDB and a Redis server running.\n\nUse these Docker images:\n - [MongoDB](https://hub.docker.com/_/mongo/)\n - [Redis](https://hub.docker.com/_/redis/)\n\n```shell\ndocker run --name database -p 27017:27017 -d mongo\ndocker run --name cache -p 6379:6379 -d redis:3.0.6-32bit\n```\n\nRegarding your needs, you can use your own Redis and Mongo applications/servers/containers.\n\nOnce everything is running, just run the tests:\n\n```shell\nnpm test\n```\n","readmeFilename":"readme.md"}