Moin Uddin edited this page Jan 24, 2016 · 4 revisions

Following events are available in ChotaDB.

You can use the on method on ChotaDB instance to subscribe to any event.

e.g.:

  var DB = ChotaDB();

  DB.on('error', function(err) {
    console.error(err);
  }).on('created', function(res) {
    console.log(res);
  }).on('inserted', function(res) {
    console.log(res);
  }).on('updated', function(res) {
    console.log(res);
  }).on('removed', function(res) {
    console.log(res);
  }).on('dropped', function(res) {
    console.log(res);
  });

Callback to every event is passed an object related to that event.

error

Fires when any error occurs. An object related to that event is passed to callback. Callback object:

{
  code: errorCode,
  reason: errorReason
}

code would be an integer from error codes. reason would be a human-friendly string explanation of error.

created

Fired when any collection is created. Object passed to callback would contain the name of newly created collection.

Callback object:

{
  collection: name
}

dropped

Fired when any collection is dropped/deleted. Object passed to callback would contain the name of dropped collection.

Callback object:

{
  collection: name
}

inserted

Fired when a new record is inserted to any collection. Callback object:

{
  collection: collectionName,
  data: dataInsrted
}

updated

Fired when a record is changed in any collection. Callback object:

{
  collection: collectionName,
  change: newData,
  affected: affected
}

In this callback object, change is an object that contain only the newly added data in key/value format. affected is an array that contains _ids of the all records this change was applied to.

removed

Fired when a record is removed in any collection. Callback object e.g.:

{
  collection: collectionName,
  removed: removed
}

removed in callback object is an array that contains _ids of the all records that have been removed and are no longer available.

Error codes

  • 1: When create is called but the name passed to it is not allowed or a collection with that name already exists. A collection cannot be created with the name that is either a property or a method of ChotaDB instance.
  • 2: When select is called and passed a name of collection that does not exists.
  • 3: When ChotaDB is not able to store data. Probably due to reaching browser's storage quota limits.

Multiple Triggers

inserted, updated and removed can be fired multiple times.

inserted would be fired multiple times if bulkInsert is used.

update would be fired multiple times if update is called on record individually. i.e: Used in each.

removed would be fired multiple times if remove is called on record individually. i.e: Used in each.

Clone this wiki locally
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.
Press h to open a hovercard with more details.