├── .gitignore ├── LICENSE ├── README.md ├── index.js ├── lib ├── ReadPreference.js ├── binary.js ├── collection.js ├── connection.js ├── index.js └── objectid.js └── package.json /.gitignore: -------------------------------------------------------------------------------- 1 | *.log 2 | npm-debug.log* 3 | node_modules 4 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2016 Warren Seine 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # mongoose-nedb 2 | 3 | A Mongoose driver for NeDB. 4 | 5 | ## Installation 6 | 7 | ``` 8 | npm install mongoose-nedb --save 9 | ``` 10 | 11 | ## Usage 12 | 13 | ```javascript 14 | require('mongoose-nedb').install(); 15 | ``` 16 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | install: function() { 3 | global.MONGOOSE_DRIVER_PATH = __dirname + '/lib'; 4 | } 5 | }; 6 | -------------------------------------------------------------------------------- /lib/ReadPreference.js: -------------------------------------------------------------------------------- 1 | var ReadPreference = function(mode, tags, options) { 2 | if(!(this instanceof ReadPreference)) { 3 | return new ReadPreference(mode, tags, options); 4 | } 5 | 6 | this._type = 'ReadPreference'; 7 | this.mode = mode; 8 | this.tags = tags; 9 | this.options = options; 10 | 11 | // If no tags were passed in 12 | if(tags && typeof tags == 'object' && !Array.isArray(tags)) { 13 | this.options = tags; 14 | this.tags = null; 15 | } 16 | } 17 | 18 | ReadPreference.prototype.isValid = function(mode) { 19 | return true; 20 | } 21 | 22 | ReadPreference.prototype.toObject = function() { 23 | return {}; 24 | } 25 | 26 | ReadPreference.prototype.toJSON = function() { 27 | return this.toObject(); 28 | } 29 | 30 | module.exports = function readPreference(pref, tags) { 31 | return new ReadPreference(); 32 | }; 33 | -------------------------------------------------------------------------------- /lib/binary.js: -------------------------------------------------------------------------------- 1 | 2 | /*! 3 | * Module dependencies. 4 | */ 5 | 6 | var Binary = require('bson').Binary; 7 | 8 | module.exports = exports = Binary; 9 | -------------------------------------------------------------------------------- /lib/collection.js: -------------------------------------------------------------------------------- 1 | /*! 2 | * Module dependencies. 3 | */ 4 | 5 | var path = require('path'); 6 | var bson = require('bson'); 7 | var MongooseCollection = require(path.join(path.dirname(require.resolve('mongoose')), 'lib/collection')); 8 | var DataStore = require('nedb'); 9 | 10 | // Override the function to generate new _id MongoDB suitable random string 11 | DataStore.prototype.createNewId = function() { 12 | return new bson.ObjectID().toString(); 13 | } 14 | 15 | /** 16 | * A [node-mongodb-native](https://github.com/mongodb/node-mongodb-native) collection implementation. 17 | * 18 | * All methods methods from the [node-mongodb-native](https://github.com/mongodb/node-mongodb-native) driver are copied and wrapped in queue management. 19 | * 20 | * @inherits Collection 21 | * @api private 22 | */ 23 | 24 | function NeDBCollection() { 25 | this.collection = null; 26 | MongooseCollection.apply(this, arguments); 27 | } 28 | 29 | /*! 30 | * Inherit from abstract Collection. 31 | */ 32 | 33 | NeDBCollection.prototype.__proto__ = MongooseCollection.prototype; 34 | 35 | /** 36 | * Called when the connection opens. 37 | * 38 | * @api private 39 | */ 40 | 41 | NeDBCollection.prototype.onOpen = function() { 42 | var _this = this; 43 | var options = _this.conn.options.nedbOptions; 44 | 45 | var collection = new DataStore(options); 46 | 47 | collection.loadDatabase(function (err) { 48 | if (err) { 49 | _this.conn.emit('error', err); 50 | } else { 51 | _this.collection = collection; 52 | MongooseCollection.prototype.onOpen.call(_this); 53 | } 54 | }); 55 | }; 56 | 57 | /** 58 | * Called when the connection closes 59 | * 60 | * @api private 61 | */ 62 | 63 | NeDBCollection.prototype.onClose = function() { 64 | MongooseCollection.prototype.onClose.call(this); 65 | }; 66 | 67 | 68 | /*! 69 | * Copy the collection methods and make them subject to queues 70 | */ 71 | 72 | function iter(i) { 73 | NeDBCollection.prototype[i] = function() { 74 | if (this.buffer) { 75 | this.addQueue(i, arguments); 76 | return; 77 | } 78 | 79 | var collection = this.collection, 80 | args = arguments, 81 | _this = this; 82 | 83 | return collection[i].apply(collection, args); 84 | }; 85 | } 86 | 87 | for (var i in DataStore.prototype) { 88 | try { 89 | if (typeof DataStore.prototype[i] !== 'function') { 90 | continue; 91 | } 92 | } catch (e) { 93 | continue; 94 | } 95 | 96 | iter(i); 97 | } 98 | 99 | 100 | /** 101 | * Retreives information about this collections indexes. 102 | * 103 | * @param {Function} callback 104 | * @method getIndexes 105 | * @api public 106 | */ 107 | 108 | NeDBCollection.prototype.getIndexes = NeDBCollection.prototype.indexInformation; 109 | 110 | /*! 111 | * Module exports. 112 | */ 113 | 114 | module.exports = NeDBCollection; 115 | -------------------------------------------------------------------------------- /lib/connection.js: -------------------------------------------------------------------------------- 1 | /*! 2 | * Module dependencies. 3 | */ 4 | 5 | var path = require('path'); 6 | var MongooseConnection = require(path.join(path.dirname(require.resolve('mongoose')), 'lib/connection')); 7 | var STATES = require(path.join(path.dirname(require.resolve('mongoose')), 'lib/connectionstate')); 8 | var fs = require('fs'); 9 | 10 | /** 11 | * A [node-mongodb-native](https://github.com/mongodb/node-mongodb-native) connection implementation. 12 | * 13 | * @inherits Connection 14 | * @api private 15 | */ 16 | 17 | function NeDBConnection() { 18 | MongooseConnection.apply(this, arguments); 19 | this._listening = false; 20 | } 21 | 22 | /** 23 | * Expose the possible connection states. 24 | * @api public 25 | */ 26 | 27 | NeDBConnection.STATES = STATES; 28 | 29 | /*! 30 | * Inherits from Connection. 31 | */ 32 | 33 | NeDBConnection.prototype.__proto__ = MongooseConnection.prototype; 34 | 35 | /** 36 | * Opens the connection to MongoDB. 37 | * 38 | * @param {Function} fn 39 | * @return {Connection} this 40 | * @api private 41 | */ 42 | 43 | NeDBConnection.prototype.doOpen = function(fn) { 44 | var _this = this; 45 | 46 | if (_this.options && !_this.options.dbpath) 47 | _this.options.dbpath = '.'; 48 | 49 | fs.access(_this.options.dbpath, fs.W_OK, function(err) { 50 | if (err) return fn(err); 51 | 52 | fn(); 53 | }); 54 | 55 | return this; 56 | }; 57 | 58 | /** 59 | * Switches to a different database using the same connection pool. 60 | * 61 | * Returns a new connection object, with the new db. 62 | * 63 | * @param {String} name The database name 64 | * @return {Connection} New Connection Object 65 | * @api public 66 | */ 67 | 68 | NeDBConnection.prototype.useDb = function(name) { 69 | throw new Error("Not implemented"); 70 | }; 71 | 72 | /** 73 | * Opens a connection to a MongoDB ReplicaSet. 74 | * 75 | * See description of [doOpen](#NeDBConnection-doOpen) for server options. In this case `options.replset` is also passed to ReplSetServers. 76 | * 77 | * @param {Function} fn 78 | * @api private 79 | * @return {Connection} this 80 | */ 81 | 82 | NeDBConnection.prototype.doOpenSet = function(fn) { 83 | throw new Error("Not implemented"); 84 | }; 85 | 86 | /** 87 | * Closes the connection 88 | * 89 | * @param {Function} fn 90 | * @return {Connection} this 91 | * @api private 92 | */ 93 | 94 | NeDBConnection.prototype.doClose = function(fn) { 95 | throw new Error("Not implemented"); 96 | }; 97 | 98 | /** 99 | * Prepares default connection options for the node-mongodb-native driver. 100 | * 101 | * _NOTE: `passed` options take precedence over connection string options._ 102 | * 103 | * @param {Object} passed options that were passed directly during connection 104 | * @param {Object} [connStrOptions] options that were passed in the connection string 105 | * @api private 106 | */ 107 | 108 | NeDBConnection.prototype.parseOptions = function(passed, connStrOpts) { 109 | var o = passed || {}; 110 | return o; 111 | }; 112 | 113 | /*! 114 | * Module exports. 115 | */ 116 | 117 | module.exports = NeDBConnection; 118 | -------------------------------------------------------------------------------- /lib/index.js: -------------------------------------------------------------------------------- 1 | /*! 2 | * Module exports. 3 | */ 4 | 5 | exports.Binary = require('./binary'); 6 | exports.ObjectId = require('./objectid'); 7 | exports.ReadPreference = require('./ReadPreference'); 8 | -------------------------------------------------------------------------------- /lib/objectid.js: -------------------------------------------------------------------------------- 1 | 2 | /*! 3 | * [node-mongodb-native](https://github.com/mongodb/node-mongodb-native) ObjectId 4 | * @constructor NodeMongoDbObjectId 5 | * @see ObjectId 6 | */ 7 | 8 | var ObjectId = require('bson').ObjectID; 9 | 10 | /*! 11 | * ignore 12 | */ 13 | 14 | module.exports = exports = ObjectId; 15 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "mongoose-nedb", 3 | "version": "0.0.1", 4 | "description": "A Mongoose driver for NeDB.", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "repository": { 10 | "type": "git", 11 | "url": "git+https://github.com/warrenseine/mongoose-nedb.git" 12 | }, 13 | "keywords": [ 14 | "nedb", 15 | "mongoose" 16 | ], 17 | "author": "Warren Seine", 18 | "license": "MIT", 19 | "bugs": { 20 | "url": "https://github.com/warrenseine/mongoose-nedb/issues" 21 | }, 22 | "homepage": "https://github.com/warrenseine/mongoose-nedb#readme", 23 | "peerDependencies": { 24 | "nedb": "^1.8.0", 25 | "mongoose": "^4.6.3", 26 | "bson": "~0.5.4" 27 | } 28 | } 29 | --------------------------------------------------------------------------------