Skip to content
david-pfx edited this page Nov 21, 2018 · 2 revisions
<style> .docApiDiv { font-family: Helvetical Neue, Helvetica, Arial, sans-serif; } .docApiDiv a { color: #55f; font-weight: bold; text-decoration: none; } .docApiDiv a:hover { text-decoration: underline; } .docApiSectionDiv { border-top: 1px solid; margin-top: 20px; } .docApiCodeCommentSpan { background-color: #bbf; color: #000; display: block; } .docApiCodeKeywordSpan { color: #f00; font-weight: bold; } .docApiCodePre { background-color: #eef; border: 1px solid; border-radius: 5px; color: #777; padding: 5px; white-space: pre-wrap; } .docApiSignatureSpan { color: #777; } </style>

Api reference

(as of commit https://github.com/louischatriot/nedb/tree/08f37b9cc21a357f7d39c2533f696fea751cda8f on Feb 15, 2016)

table of contents

  • module Nedb
    <li><a class="docApiElementLiA" href="#element.Nedb.super_">
    function <span class="docApiSignatureSpan">Nedb.</span>super_
    <span class="docApiSignatureSpan">()</span>
    </a></li>
    
  • module Nedb.prototype
    <li><a class="docApiElementLiA" href="#element.Nedb.prototype.addToIndexes">
    function <span class="docApiSignatureSpan">Nedb.prototype.</span>addToIndexes
    <span class="docApiSignatureSpan">(doc)</span>
    </a></li>
    
    <li><a class="docApiElementLiA" href="#element.Nedb.prototype.count">
    function <span class="docApiSignatureSpan">Nedb.prototype.</span>count
    <span class="docApiSignatureSpan">(query, callback)</span>
    </a></li>
    
    <li><a class="docApiElementLiA" href="#element.Nedb.prototype.createNewId">
    function <span class="docApiSignatureSpan">Nedb.prototype.</span>createNewId
    <span class="docApiSignatureSpan">()</span>
    </a></li>
    
    <li><a class="docApiElementLiA" href="#element.Nedb.prototype.ensureIndex">
    function <span class="docApiSignatureSpan">Nedb.prototype.</span>ensureIndex
    <span class="docApiSignatureSpan">(options, cb)</span>
    </a></li>
    
    <li><a class="docApiElementLiA" href="#element.Nedb.prototype.find">
    function <span class="docApiSignatureSpan">Nedb.prototype.</span>find
    <span class="docApiSignatureSpan">(query, projection, callback)</span>
    </a></li>
    
    <li><a class="docApiElementLiA" href="#element.Nedb.prototype.findOne">
    function <span class="docApiSignatureSpan">Nedb.prototype.</span>findOne
    <span class="docApiSignatureSpan">(query, projection, callback)</span>
    </a></li>
    
    <li><a class="docApiElementLiA" href="#element.Nedb.prototype.getAllData">
    function <span class="docApiSignatureSpan">Nedb.prototype.</span>getAllData
    <span class="docApiSignatureSpan">()</span>
    </a></li>
    
    <li><a class="docApiElementLiA" href="#element.Nedb.prototype.getCandidates">
    function <span class="docApiSignatureSpan">Nedb.prototype.</span>getCandidates
    <span class="docApiSignatureSpan">(query, dontExpireStaleDocs, callback)</span>
    </a></li>
    
    <li><a class="docApiElementLiA" href="#element.Nedb.prototype.insert">
    function <span class="docApiSignatureSpan">Nedb.prototype.</span>insert
    <span class="docApiSignatureSpan">()</span>
    </a></li>
    
    <li><a class="docApiElementLiA" href="#element.Nedb.prototype.loadDatabase">
    function <span class="docApiSignatureSpan">Nedb.prototype.</span>loadDatabase
    <span class="docApiSignatureSpan">()</span>
    </a></li>
    
    <li><a class="docApiElementLiA" href="#element.Nedb.prototype.prepareDocumentForInsertion">
    function <span class="docApiSignatureSpan">Nedb.prototype.</span>prepareDocumentForInsertion
    <span class="docApiSignatureSpan">(newDoc)</span>
    </a></li>
    
    <li><a class="docApiElementLiA" href="#element.Nedb.prototype.remove">
    function <span class="docApiSignatureSpan">Nedb.prototype.</span>remove
    <span class="docApiSignatureSpan">()</span>
    </a></li>
    
    <li><a class="docApiElementLiA" href="#element.Nedb.prototype.removeFromIndexes">
    function <span class="docApiSignatureSpan">Nedb.prototype.</span>removeFromIndexes
    <span class="docApiSignatureSpan">(doc)</span>
    </a></li>
    
    <li><a class="docApiElementLiA" href="#element.Nedb.prototype.removeIndex">
    function <span class="docApiSignatureSpan">Nedb.prototype.</span>removeIndex
    <span class="docApiSignatureSpan">(fieldName, cb)</span>
    </a></li>
    
    <li><a class="docApiElementLiA" href="#element.Nedb.prototype.resetIndexes">
    function <span class="docApiSignatureSpan">Nedb.prototype.</span>resetIndexes
    <span class="docApiSignatureSpan">(newData)</span>
    </a></li>
    
    <li><a class="docApiElementLiA" href="#element.Nedb.prototype.update">
    function <span class="docApiSignatureSpan">Nedb.prototype.</span>update
    <span class="docApiSignatureSpan">()</span>
    </a></li>
    
    <li><a class="docApiElementLiA" href="#element.Nedb.prototype.updateIndexes">
    function <span class="docApiSignatureSpan">Nedb.prototype.</span>updateIndexes
    <span class="docApiSignatureSpan">(oldDoc, newDoc)</span>
    </a></li>
    
  • <h2><a href="#element.Nedb.super_" id="element.Nedb.super_">
        function <span class="docApiSignatureSpan">Nedb.</span>super_
        <span class="docApiSignatureSpan">()</span>
    </a></h2>
    <ul>
    <li>description and source code<pre class="docApiCodePre">super_ = function () {}</pre></li>
    <li>example usage<pre class="docApiCodePre">n/a</pre></li>
    </ul>
    
    <h2><a href="#element.Nedb.prototype.addToIndexes" id="element.Nedb.prototype.addToIndexes">
        function <span class="docApiSignatureSpan">Nedb.prototype.</span>addToIndexes
        <span class="docApiSignatureSpan">(doc)</span>
    </a></h2>
    <ul>
    <li>description and source code<pre class="docApiCodePre">addToIndexes = function (doc) {
    

    var i, failingIndex, error , keys = Object.keys(this.indexes) ;

    for (i = 0; i < keys.length; i += 1) { try { this.indexes[keys[i]].insert(doc); } catch (e) { failingIndex = i; error = e; break; } }

    // If an error happened, we need to rollback the insert on all other indexes if (error) { for (i = 0; i < failingIndex; i += 1) { this.indexes[keys[i]].remove(doc); }

    throw error;
    

    } }

  • example usage
    n/a
  • <h2><a href="#element.Nedb.prototype.count" id="element.Nedb.prototype.count">
        function <span class="docApiSignatureSpan">Nedb.prototype.</span>count
        <span class="docApiSignatureSpan">(query, callback)</span>
    </a></h2>
    <ul>
    <li>description and source code<pre class="docApiCodePre">count = function (query, callback) {
    

    var cursor = new Cursor(this, query, function(err, docs, callback) { if (err) { return callback(err); } return callback(null, docs.length); });

    if (typeof callback === 'function') { cursor.exec(callback); } else { return cursor; } }

  • example usage
    n/a
  • <h2><a href="#element.Nedb.prototype.createNewId" id="element.Nedb.prototype.createNewId">
        function <span class="docApiSignatureSpan">Nedb.prototype.</span>createNewId
        <span class="docApiSignatureSpan">()</span>
    </a></h2>
    <ul>
    <li>description and source code<pre class="docApiCodePre">createNewId = function () {
    

    var tentativeId = customUtils.uid(16); // Try as many times as needed to get an unused _id. As explained in customUtils, the probability of this ever happening is ex
    tremely small, so this is O(1) if (this.indexes._id.getMatching(tentativeId).length > 0) { tentativeId = this.createNewId(); } return tentativeId; }

  • example usage
    n/a
  • <h2><a href="#element.Nedb.prototype.ensureIndex" id="element.Nedb.prototype.ensureIndex">
        function <span class="docApiSignatureSpan">Nedb.prototype.</span>ensureIndex
        <span class="docApiSignatureSpan">(options, cb)</span>
    </a></h2>
    <ul>
    <li>description and source code<pre class="docApiCodePre">ensureIndex = function (options, cb) {
    

    var err , callback = cb || function () {};

    options = options || {};

    if (!options.fieldName) { err = new Error("Cannot create an index without a fieldName"); err.missingFieldName = true; return callback(err); } if (this.indexes[options.fieldName]) { return callback(null); }

    this.indexes[options.fieldName] = new Index(options); if (options.expireAfterSeconds !== undefined) { this.ttlIndexes[options.fieldName] = options.expireAfterSeconds; } // With t
    his implementation index creation is not necessary to ensure TTL but we stick with MongoDB's API here

    try { this.indexes[options.fieldName].insert(this.getAllData()); } catch (e) { delete this.indexes[options.fieldName]; return callback(e); }

    // We may want to force all options to be persisted including defaults, not just the ones passed the index creation function this.persistence.persistNewState([{ $$indexCreated: options }], function (err) { if (err) { return callback(err); } return callback(null); }); }

  • example usage
    n/a
  • <h2><a href="#element.Nedb.prototype.find" id="element.Nedb.prototype.find">
        function <span class="docApiSignatureSpan">Nedb.prototype.</span>find
        <span class="docApiSignatureSpan">(query, projection, callback)</span>
    </a></h2>
    <ul>
    <li>description and source code<pre class="docApiCodePre">find = function (query, projection, callback) {
    

    switch (arguments.length) { case 1: projection = {}; // callback is undefined, will return a cursor break; case 2: if (typeof projection === 'function') { callback = projection; projection = {}; } // If not assume projection is an object and callback undefined break; }

    var cursor = new Cursor(this, query, function(err, docs, callback) { var res = [], i;

    if (err) { return callback(err); }
    
    for (i = 0; i &lt; docs.length; i += 1) {
      res.push(model.deepCopy(docs[i]));
    }
    return callback(null, res);
    

    });

    cursor.projection(projection); if (typeof callback === 'function') { cursor.exec(callback); } else { return cursor; } }

  • example usage
    n/a
  • <h2><a href="#element.Nedb.prototype.findOne" id="element.Nedb.prototype.findOne">
        function <span class="docApiSignatureSpan">Nedb.prototype.</span>findOne
        <span class="docApiSignatureSpan">(query, projection, callback)</span>
    </a></h2>
    <ul>
    <li>description and source code<pre class="docApiCodePre">findOne = function (query, projection, callback) {
    

    switch (arguments.length) { case 1: projection = {}; // callback is undefined, will return a cursor break; case 2: if (typeof projection === 'function') { callback = projection; projection = {}; } // If not assume projection is an object and callback undefined break; }

    var cursor = new Cursor(this, query, function(err, docs, callback) { if (err) { return callback(err); } if (docs.length === 1) { return callback(null, model.deepCopy(docs[0])); } else { return callback(null, null); } });

    cursor.projection(projection).limit(1); if (typeof callback === 'function') { cursor.exec(callback); } else { return cursor; } }

  • example usage
    n/a
  • <h2><a href="#element.Nedb.prototype.getAllData" id="element.Nedb.prototype.getAllData">
        function <span class="docApiSignatureSpan">Nedb.prototype.</span>getAllData
        <span class="docApiSignatureSpan">()</span>
    </a></h2>
    <ul>
    <li>description and source code<pre class="docApiCodePre">getAllData = function () {
    

    return this.indexes._id.getAll(); }

  • example usage
    n/a
  • <h2><a href="#element.Nedb.prototype.getCandidates" id="element.Nedb.prototype.getCandidates">
        function <span class="docApiSignatureSpan">Nedb.prototype.</span>getCandidates
        <span class="docApiSignatureSpan">(query, dontExpireStaleDocs, callback)</span>
    </a></h2>
    <ul>
    <li>description and source code<pre class="docApiCodePre">getCandidates = function (query, dontExpireStaleDocs, callback) {
    

    var indexNames = Object.keys(this.indexes) , self = this , usableQueryKeys;

    if (typeof dontExpireStaleDocs === 'function') { callback = dontExpireStaleDocs; dontExpireStaleDocs = false; }

    async.waterfall([ // STEP 1: get candidates list by checking indexes from most to least frequent usecase function (cb) { // For a basic match usableQueryKeys = []; Object.keys(query).forEach(function (k) { if (typeof query[k] === 'string' || typeof query[k] === 'number' || typeof query[k] === 'boolean' || util.isDate(query[k])
    || query[k] === null) { usableQueryKeys.push(k); } }); usableQueryKeys = _.intersection(usableQueryKeys, indexNames); if (usableQueryKeys.length > 0) { return cb(null, self.indexes[usableQueryKeys[0]].getMatching(query[usableQueryKeys[0]])); }

    // For a $in match
    usableQueryKeys = [];
    Object.keys(query).forEach(function (k) {
      if (query[k] && query[k].hasOwnProperty('$in')) {
        usableQueryKeys.push(k);
      }
    });
    usableQueryKeys = _.intersection(usableQueryKeys, indexNames);
    if (usableQueryKeys.length &gt; 0) {
      return cb(null, self.indexes[usableQueryKeys[0]].getMatching(query[usableQueryKeys[0]].$in));
    }
    
    // For a comparison match
    usableQueryKeys = [];
    Object.keys(query).forEach(function (k) {
      if (query[k] && (query[k].hasOwnProperty('$lt') || query[k].hasOwnProperty('$lte') || query[k].hasOwnProperty('$gt') || qu\
    

    ery[k].hasOwnProperty('$gte'))) { usableQueryKeys.push(k); } }); usableQueryKeys = _.intersection(usableQueryKeys, indexNames); if (usableQueryKeys.length > 0) { return cb(null, self.indexes[usableQueryKeys[0]].getBetweenBounds(query[usableQueryKeys[0]])); }

    // By default, return all the DB data
    return cb(null, self.getAllData());
    

    } // STEP 2: remove all expired documents , function (docs) { if (dontExpireStaleDocs) { return callback(null, docs); }

    var expiredDocsIds = [], validDocs = [], ttlIndexesFieldNames = Object.keys(self.ttlIndexes);
    
    docs.forEach(function (doc) {
      var valid = true;
      ttlIndexesFieldNames.forEach(function (i) {
        if (doc[i] !== undefined && util.isDate(doc[i]) && Date.now() &gt; doc[i].getTime() + self.ttlIndexes[i] * 1000) {
          valid = false;
        }
      });
      if (valid) { validDocs.push(doc); } else { expiredDocsIds.push(doc._id); }
    });
    
    async.eachSeries(expiredDocsIds, function (_id, cb) {
      self._remove({ _id: _id }, {}, function (err) {
        if (err) { return callback(err); }
        return cb();
      });
    }, function (err) {
      return callback(null, validDocs);
    });
    

    }]); }

  • example usage
    n/a
  • <h2><a href="#element.Nedb.prototype.insert" id="element.Nedb.prototype.insert">
        function <span class="docApiSignatureSpan">Nedb.prototype.</span>insert
        <span class="docApiSignatureSpan">()</span>
    </a></h2>
    <ul>
    <li>description and source code<pre class="docApiCodePre">insert = function () {
    

    this.executor.push({ this: this, fn: this._insert, arguments: arguments }); }

  • example usage
    n/a
  • <h2><a href="#element.Nedb.prototype.loadDatabase" id="element.Nedb.prototype.loadDatabase">
        function <span class="docApiSignatureSpan">Nedb.prototype.</span>loadDatabase
        <span class="docApiSignatureSpan">()</span>
    </a></h2>
    <ul>
    <li>description and source code<pre class="docApiCodePre">loadDatabase = function () {
    

    this.executor.push({ this: this.persistence, fn: this.persistence.loadDatabase, arguments: arguments }, true); }

  • example usage
    n/a
  • <h2><a href="#element.Nedb.prototype.prepareDocumentForInsertion" id="element.Nedb.prototype.prepareDocumentForInsertion">
        function <span class="docApiSignatureSpan">Nedb.prototype.</span>prepareDocumentForInsertion
        <span class="docApiSignatureSpan">(newDoc)</span>
    </a></h2>
    <ul>
    <li>description and source code<pre class="docApiCodePre">prepareDocumentForInsertion = function (newDoc) {
    

    var preparedDoc, self = this;

    if (util.isArray(newDoc)) { preparedDoc = []; newDoc.forEach(function (doc) { preparedDoc.push(self.prepareDocumentForInsertion(doc)); }); } else { preparedDoc = model.deepCopy(newDoc); if (preparedDoc._id === undefined) { preparedDoc._id = this.createNewId(); } var now = new Date(); if (this.timestampData && preparedDoc.createdAt === undefined) { preparedDoc.createdAt = now; } if (this.timestampData && preparedDoc.updatedAt === undefined) { preparedDoc.updatedAt = now; } model.checkObject(preparedDoc); }

    return preparedDoc; }

  • example usage
    n/a
  • <h2><a href="#element.Nedb.prototype.remove" id="element.Nedb.prototype.remove">
        function <span class="docApiSignatureSpan">Nedb.prototype.</span>remove
        <span class="docApiSignatureSpan">()</span>
    </a></h2>
    <ul>
    <li>description and source code<pre class="docApiCodePre">remove = function () {
    

    this.executor.push({ this: this, fn: this._remove, arguments: arguments }); }

  • example usage
    n/a
  • <h2><a href="#element.Nedb.prototype.removeFromIndexes" id="element.Nedb.prototype.removeFromIndexes">
        function <span class="docApiSignatureSpan">Nedb.prototype.</span>removeFromIndexes
        <span class="docApiSignatureSpan">(doc)</span>
    </a></h2>
    <ul>
    <li>description and source code<pre class="docApiCodePre">removeFromIndexes = function (doc) {
    

    var self = this;

    Object.keys(this.indexes).forEach(function (i) { self.indexes[i].remove(doc); }); }

  • example usage
    n/a
  • <h2><a href="#element.Nedb.prototype.removeIndex" id="element.Nedb.prototype.removeIndex">
        function <span class="docApiSignatureSpan">Nedb.prototype.</span>removeIndex
        <span class="docApiSignatureSpan">(fieldName, cb)</span>
    </a></h2>
    <ul>
    <li>description and source code<pre class="docApiCodePre">removeIndex = function (fieldName, cb) {
    

    var callback = cb || function () {};

    delete this.indexes[fieldName];

    this.persistence.persistNewState([{ $$indexRemoved: fieldName }], function (err) { if (err) { return callback(err); } return callback(null); }); }

  • example usage
    n/a
  • <h2><a href="#element.Nedb.prototype.resetIndexes" id="element.Nedb.prototype.resetIndexes">
        function <span class="docApiSignatureSpan">Nedb.prototype.</span>resetIndexes
        <span class="docApiSignatureSpan">(newData)</span>
    </a></h2>
    <ul>
    <li>description and source code<pre class="docApiCodePre">resetIndexes = function (newData) {
    

    var self = this;

    Object.keys(this.indexes).forEach(function (i) { self.indexes[i].reset(newData); }); }

  • example usage
    n/a
  • <h2><a href="#element.Nedb.prototype.update" id="element.Nedb.prototype.update">
        function <span class="docApiSignatureSpan">Nedb.prototype.</span>update
        <span class="docApiSignatureSpan">()</span>
    </a></h2>
    <ul>
    <li>description and source code<pre class="docApiCodePre">update = function () {
    

    this.executor.push({ this: this, fn: this._update, arguments: arguments }); }

  • example usage
    n/a
  • <h2><a href="#element.Nedb.prototype.updateIndexes" id="element.Nedb.prototype.updateIndexes">
        function <span class="docApiSignatureSpan">Nedb.prototype.</span>updateIndexes
        <span class="docApiSignatureSpan">(oldDoc, newDoc)</span>
    </a></h2>
    <ul>
    <li>description and source code<pre class="docApiCodePre">updateIndexes = function (oldDoc, newDoc) {
    

    var i, failingIndex, error , keys = Object.keys(this.indexes) ;

    for (i = 0; i < keys.length; i += 1) { try { this.indexes[keys[i]].update(oldDoc, newDoc); } catch (e) { failingIndex = i; error = e; break; } }

    // If an error happened, we need to rollback the update on all other indexes if (error) { for (i = 0; i < failingIndex; i += 1) { this.indexes[keys[i]].revertUpdate(oldDoc, newDoc); }

    throw error;
    

    } }

  • example usage
    n/a