符合中小企业对网站设计、功能常规化式的企业展示型网站建设
本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...
商城网站建设因基本功能的需求不同费用上面也有很大的差别...
手机微信网站开发、微信官网、微信商城网站...
本文实例讲述了nodejs nedb 封装库与使用方法。分享给大家供大家参考,具体如下:
我们提供的服务有:成都网站设计、成都网站建设、微信公众号开发、网站优化、网站认证、禹城ssl等。为上千多家企事业单位解决了网站和推广的问题。提供周到的售前咨询和贴心的售后服务,是有科学管理、有技术的禹城网站制作公司
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, limit) { this.offset = offset || 0; this.limit = limit || 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(select || {}); } stmt.exec((err, docs) => { if (err) { return reject(err); } resolve(docs); }) }) }; DB.prototype.findOne = function(query, select) { return new Promise((resolve, reject) => { 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(values) { 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); })();
执行命令
nodemon test.js
希望本文所述对大家node.js程序设计有所帮助。