NodeJS NEDB Package and Usage Method Example

This example tells the NodeJS NEDB package library and the method of use. Share to everyone for reference, specifically:

libs / nedb.js

const Datastore = Require (‘Nedb’); Function DB (Database ) {let options = {filename: Database, AutoLoad: true,}; this.db = new datastore (options);} db.prototype.limit = function (offset, limited) {this.offset = offset || 0; this .limit = limited || 15; return this;} db.prototype.sort = function (orderby) {this.orderby = orderby; return this;} db.prototype.find = function (query, select) {Return New Promise (resolve, reject) => {let stmt = this.db.find (query || {}); if (this.Orderby! == undefined) {stmt.sort (this.Orderby);} if (this.offset ! == Undefined) {stmt.skip (this.offset) .limit (this.limit);} if (select! = undefined) {Stmt.Projection;} stmt.exec ((ERR, DOCS) => {if (err) {return reject (ERR);} resolve (docs);}}}; db.prototype .findone = function (query, select) {return new promise => {let stmt = this.db.findone (query || {}); if (this.sort! == undefined) {stmt . Sort (this.Sort);} if (select! = undefined) {stmt.projection (select || {});} stmt.exec ((Err, DOC) => {if (err) {RETURN REJECT (ERR) } resolve (doc);})}} DB.Prototype.Insert = function (value) {Return New Promise ((Resolve, Reject) => {this.db.insert (Values, (Err, NewDoc) = > {if (err) {return reject (ERR);} resolve (newdoc);})}} DB.Prototype.Update= Function (Query, Values, Options) {Return New Promise ((Resolve, Reject) => {this.db.Update (query || {}, values ​​|| {}, options || {}, (err, numaffected) ) => {if (err) {RETURN REJECT (ERR);} resolve (numaffected);}});} db.prototype.remove = function (query, options) {Return new promise (resolve, reject) = > {this.db.remove (query || {}, options || {}, (err, numaffected) => {if (err) {return reject (ERR);} resolve (numaffected);}); } Module.exports = (Database) => {Return New DB (Database);}
  Test.js  

Const db = Require (‘./ libs / nedb’) (‘TestDB’); (async function () {AWAIT DB.INSERT ({Number: 12}); Await Db.insert ({Number: 11}) AWAIT DB.INSERT ({NUMBER: 13}); let res = await db.sort ({Number: -1}). Limit (0, 2) .find (); console.log (res);} ();

   

 It is desirable to be described herein. Help for everyone node.js programming.                      
© Copyright Notice
THE END
Just support it if you like
like0
share
comment Grab the couch

Please log in to comment