EJDB Nodejs API

EJDB.open(dbFile[, openMode][, cb])

Open a database specified by dbFile using the openMode.

Execution of this function will be asynchronous if cb is provided, otherwise it will executed in synchronous mode.

The openMode argument is a bitmask of the following flags:

  • JBOREADER Open database as a reader.
  • JBOWRITER Open database as a writer.
  • JBOCREAT Create new database if it not exists.
  • JBOTRUNC Trancated database on opening.
Arguments:
  • dbFile (string) – The database file path.
  • [openMode] (number) – Open mode. Default to: JBOWRITER | JBOCREAT
  • [cb] (function) – Callback called with an error and EJDB object arguments
Returns:

EJDB database instance object db.

EJDB.isValidOID(oid)

Return true if argument is a valid ObjectId string.

Arguments:
  • oid (string) – Object id
Returns:

Boolean value

db.close([cb])

Close a database. If database is closed this function does nothing.

Arguments:
  • cb (function) – Callback called with an error.
db.isOpen()

Check if database is in opened state.

db.ensureCollection(cname[, copts][, cb])

Return existing collection or create new collection if it was not created before. Collection options copts are applied only for newly created collection. For existing collections copts takes no effect.

Execution of this function will be asynchronous if cb is provided, otherwise it will executed in synchronous mode.

copts:

{
  //Max number of cached records in shared memory segment. Default: 0
  'cachedrecords': number,

  //Estimated number of records in this collection. Default: 65535.
  'records': number,

   //If `true` the size of a database can be larger than 2GB. Default: false
  'large': false,

  //If `true` collection's records will be compressed
  // with DEFLATE compression. Default: false
  'compressed': false
}
Arguments:
  • cname (string) – Collection name
  • [copts] (map) – Options to be used when creating the collection
  • [cb] (function) – Callback called with an error argument
db.dropCollection(cname[, prune=false][, cb])

Drop the collection specified by cname. If prune set to true the collection data will be removed from disk.

Arguments:
  • cname (string) – Collection name
  • [prune=false] (boolean) – If prune set to true the collection data will be removed from disk
  • [cb] (function) – Callback called with an error argument
db.save(cname, jsarr[, opts={}][, cb])

Save/update specified in jsarr JSON objects in the collection are identified by cname. If a collection with cname does not exists it will be created.

Every persistent object may have a unique ObjectId placed in the _id property. If a saved object does not have _id it will be autogenerated. To identify and update object it should contains the _id property.

Execution of this function will be asynchronous if cb is provided, otherwise it will be executed in synchronous mode.

A save options opts:

{
    //A saved object will be merged with who's
    // already persisted in db.
    merge: false
}

Calling variations:

save(cname, <json object>|<Array of json objects>)
save(cname, <json object>|<Array of json objects>, options)
save(cname, <json object>|<Array of json objects>, cb)
save(cname, <json object>|<Array of json objects>, options, cb)

Note

Field names of passed JSON objects may not contain $ and . characters, error condition will be fired in this case.

Arguments:
  • cname (string) – Collection name.
  • jsarr (array|object) – Signle JSON object or array of JSON objects to save.
  • [opts] (object) – Save options.
  • [cb] (function) – Callback function with arguments: (error, {Array} of OIDs of saved objects)
Returns:

An OID array of saved object in synchronous mode otherwise return undefined.

db.load(cname, oid[, cb])

Retrieve JSON object identified by OID.

Execution of this function will be asynchronous if cb is provided, otherwise it will be executed in synchronous mode.

Return JSON object or null if it is not found.

Arguments:
  • cname (string) – Collection name
  • oid (string) – Object identifier (OID)
  • [cb] (function) – Callback function
Returns:

JSON Object or null if it is not found in synchronous mode otherwise return undefined.

db.remove(cname, oid[, cb])

Remove document object identified by ObjectId from the collection cname.

Execution of this function will be asynchronous if cb is provided, otherwise it will be executed in synchronous mode.

Arguments:
  • cname (string) – Collection name
  • [oid] (string) – ObjectId
Returns:

undefined

db.find(cname, ...)

Retrieve a set of documents matched to the specified query. See the complete query language specification

Execution of this function will be asynchronous if cb is provided, otherwise it will be executed in synchronous mode.

A callback function cb will be called with the following arguments: error, cursor, count where:

  • error: Error object
  • cursor: Cursor object to traverse documents found
  • count: Total number of matched documents.

Calling variations:

find(cname, [cb])
find(cname, qobj, [cb])
find(cname, qobj, hints, [cb])
find(cname, qobj, qobjarr, [cb])
find(cname, qobj, qobjarr, hints, [cb])
Arguments:
  • cname (string) – Collection name
  • [qobj] (object) – Main JSON query object
  • [hints] (object) – JSON object specifies the query hints
  • [qobjarr] (array) – Array of extra OR joined query objects
  • [cb] (function) – Callback accepting arguments: error, cursor, count
Returns:

undefined if cb is provided. If cb is not provided and $onlycount query hint is set returns count number. If cb is not provided and no $onlycount query hint returns cursor object.

db.findOne(cname, ...)

Retrieve a first found document matched to the specified query. See the complete query language specification

Execution of this function will be asynchronous if cb is provided, otherwise it will be executed in synchronous mode.

A callback function cb will be called with the following arguments: error, object where:

  • error: Error object
  • document: Found document or null if no matched documents.

Calling variations:

findOne(cname, [cb])
findOne(cname, qobj, [cb])
findOne(cname, qobj, hints, [cb])
findOne(cname, qobj, qobjarr, [cb])
findOne(cname, qobj, qobjarr, hints, [cb])
Arguments:
  • cname (string) – Collection name
  • [qobj] (object) – Main JSON query object
  • [hints] (object) – JSON object specifies the query hints
  • [qobjarr] (array) – Array of extra OR joined query objects
  • [cb] (function) – Callback accepting arguments: error, document
Returns:

undefined if cb is provided, otherwise return a found document or null if no documents found.

db.command(cmd[, cb])

Execute ejdb database command. See the database commands documentation

Execution of this function will be asynchronous if cb is provided, otherwise it will be executed in synchronous mode.

A callback function cb will be called with the following arguments: error, object where:

Arguments:
  • cmd (json) – Command specification
  • [cb] (function) – Callback function arguments: error, response
db.update(cname, ...)

Convenient method to execute update queries.

Execution of this function will be asynchronous if cb is provided, otherwise it will be executed in synchronous mode.

A callback function cb will be called with the following arguments: error, count where:

  • error: Error object
  • count: Number of documents updated.

Calling variations:

update(cname, qobj, [cb])
update(cname, qobj, hints, [cb])
update(cname, qobj, qobjarr, [cb])
update(cname, qobj, qobjarr, hints, [cb])
Arguments:
  • cname (string) – Collection name
  • [qobj] (object) – Main JSON query object
  • [hints] (object) – JSON object specifies the query hints
  • [qobjarr] (array) – Array of extra OR joined query objects
  • [cb] (function) – Callback function accepting arguments: error, count
Returns:

Number of updated documents if cb is not provided otherwise undefined

db.count(cname, ...)

Convenient method to execute count(*) queries.

Execution of this function will be asynchronous if cb is provided, otherwise it will be executed in synchronous mode.

A callback function cb will be called with the following arguments: error, count where:

  • error: Error object
  • count: Number of documents matched the query.

Calling variations:

count(cname, qobj, [cb])
count(cname, qobj, hints, [cb])
count(cname, qobj, qobjarr, [cb])
count(cname, qobj, qobjarr, hints, [cb])
Arguments:
  • cname (string) – Collection name
  • [qobj] (object) – Main JSON query object
  • [hints] (object) – JSON object specifies the query hints
  • [qobjarr] (array) – Array of extra OR joined query objects
  • [cb] (function) – Callback function accepting arguments: error, count
Returns:

Number of documents matched the query if cb is not provided otherwise undefined

db.sync([cb])

Synchronize entire database and all of its collections with disk.

Execution of this function will be asynchronous if cb is provided, otherwise it will be executed in synchronous mode.

Arguments:
  • [cb] (function) – Callback function accepting error
db.dropIndexes(cname, fieldpath[, cb])

DROP indexes of all types for JSON fieldpath.

Execution of this function will be asynchronous if cb is provided, otherwise it will be executed in synchronous mode.

Arguments:
  • cname (string) – Collection name
  • fieldpath (string) – Documents fieldpath
  • [cb] (function) – Callback function accepting error
db.dropIndexes(cname, fieldpath[, cb])

OPTIMIZE indexes of all types for JSON fieldpath. Performs B+ tree index file optimization.

Execution of this function will be asynchronous if cb is provided, otherwise it will be executed in synchronous mode.

Arguments:
  • cname (string) – Collection name
  • fieldpath (string) – Documents fieldpath
  • [cb] (function) – Callback function accepting error
db.ensureStringIndex(cname, fieldpath[, cb])

Ensure index presence of string type for JSON fieldpath.

Execution of this function will be asynchronous if cb is provided, otherwise it will be executed in synchronous mode.

Arguments:
  • cname (string) – Collection name :param string fieldpath: Documents fieldpath :param function [cb]: Callback function accepting error
db.rebuildStringIndex(cname, fieldpath[, cb])

Rebuild index of string type for JSON fieldpath.

Execution of this function will be asynchronous if cb is provided, otherwise it will be executed in synchronous mode.

Arguments:
  • cname (string) – Collection name
  • fieldpath (string) – Documents fieldpath
  • [cb] (function) – Callback function accepting error
db.dropStringIndex(cname, fieldpath[, cb])

Drop index of string type for JSON fieldpath.

Execution of this function will be asynchronous if cb is provided, otherwise it will be executed in synchronous mode.

Arguments:
  • cname (string) – Collection name
  • fieldpath (string) – Documents fieldpath
  • [cb] (function) – Callback function accepting error
db.ensureIStringIndex(cname, fieldpath[, cb])

Ensure case insensitive index presence of string type for JSON fieldpath.

Execution of this function will be asynchronous if cb is provided, otherwise it will be executed in synchronous mode.

Arguments:
  • cname (string) – Collection name
  • fieldpath (string) – Documents fieldpath
  • [cb] (function) – Callback function accepting error
db.rebuildIStringIndex(cname, fieldpath[, cb])

Rebuild case insensitive index of string type for JSON fieldpath.

Execution of this function will be asynchronous if cb is provided, otherwise it will be executed in synchronous mode.

Arguments:
  • cname (string) – Collection name
  • fieldpath (string) – Documents fieldpath
  • [cb] (function) – Callback function accepting error
db.dropIStringIndex(cname, fieldpath[, cb])

Drop case insensitive index of string type for JSON fieldpath.

Execution of this function will be asynchronous if cb is provided, otherwise it will be executed in synchronous mode.

Arguments:
  • cname (string) – Collection name
  • fieldpath (string) – Documents fieldpath
  • [cb] (function) – Callback function accepting error
db.ensureNumberIndex(cname, fieldpath[, cb])

Ensure index presence of number type for JSON fieldpath.

Execution of this function will be asynchronous if cb is provided, otherwise it will be executed in synchronous mode.

Arguments:
  • cname (string) – Collection name
  • fieldpath (string) – Documents fieldpath
  • [cb] (function) – Callback function accepting error
db.rebuildNumberIndex(cname, fieldpath[, cb])

Rebuild index of number type for JSON fieldpath.

Execution of this function will be asynchronous if cb is provided, otherwise it will be executed in synchronous mode.

Arguments:
  • cname (string) – Collection name
  • fieldpath (string) – Documents fieldpath
  • [cb] (function) – Callback function accepting error
db.dropNumberIndex(cname, fieldpath[, cb])

Drop index of number type for JSON fieldpath.

Execution of this function will be asynchronous if cb is provided, otherwise it will be executed in synchronous mode.

Arguments:
  • cname (string) – Collection name
  • fieldpath (string) – Documents fieldpath
  • [cb] (function) – Callback function accepting error
db.ensureArrayIndex(cname, fieldpath[, cb])

Ensure index presence of array type for JSON fieldpath.

Execution of this function will be asynchronous if cb is provided, otherwise it will be executed in synchronous mode.

Arguments:
  • cname (string) – Collection name
  • fieldpath (string) – Documents fieldpath
  • [cb] (function) – Callback function accepting error
db.rebuildArrayIndex(cname, fieldpath[, cb])

Rebuild index of array type for JSON fieldpath.

Execution of this function will be asynchronous if cb is provided, otherwise it will be executed in synchronous mode.

Arguments:
  • cname (string) – Collection name
  • fieldpath (string) – Documents fieldpath
  • [cb] (function) – Callback function accepting error
db.dropArrayIndex(cname, fieldpath[, cb])

Drop index of array type for JSON fieldpath.

Execution of this function will be asynchronous if cb is provided, otherwise it will be executed in synchronous mode.

Arguments:
  • cname (string) – Collection name
  • fieldpath (string) – Documents fieldpath
  • [cb] (function) – Callback function accepting error
db.getDBMeta()

Get description of EJDB database and its collections.

db.beginTransaction(cname[, cb])

Begin collection transaction.

db.commitTransaction(cname[, cb])

Commit collection transaction.

db.rollbackTransaction(cname[, cb])

Rollback collection transaction.

db.getTransactionStatus(cname[, cb])

Get collection transaction status.