Fork me on GitHub

Everything here is experimental, and the API is no exception, so expect things to change in future releases.

Alfred.open (path_to_database, [options, ] callback)

Opens a new or an existing database that resides in path_to_database.

  • path_to_database: directory where the database files (will) reside. Must already exist.
  • callback (err, db): invoked when there is an error or the database is open. Use db to access / create / remove key maps.
  • options:
    • meta_compact_interval: default average compact interval for the metadata key map, in miliseconds. Defaults to 1 hour (1000 * 60 * 60).
    • replication_master: true if this database is going to act as a replication master. Defaults to false.
    • replication_port: replication server port for this database. Defaults to 5293.
    • replication_max_file_size_kb: maximum file size for replication temporary files on master. Defaults to 10000 KBytes.
    • replication_max_keep_ms: maximum time a replication temporary file should be kept around. Defaults to 15 days.

Example:

var Alfred = require('alfred');
Alfred.open('path/to/database/dir', function(err, db) {
  if (err) { throw err; }
  // now I have the db object to do stuff
});

database.attach (key_map_name [, options], callback)

Attaches a key map to this database.

  • key_map_name: name of this key map. Will be available as a named property of database (database.<key_map_name> or database[key_map_name]).
  • callback (err, key_map): invoked when there is an error or the key map is attached.
  • options:
    • buffered: if true, writes are buffered (flushes are scheduled every second after the last one by default). If false, key_map.put only callsback when data is written to disk. Defaults to true.
    • flush_interval: This determines the frequency of flushes. A flush is scheduled flush_interval miliseconds after the last one finished. In miliseconds. Defaults to 1000 (1 second).
    • type: can be 'cached_key_map' or 'indexed_key_map'. Defaults to 'cached_key_map'. ('cached_key_map' is also indexed)
    • compact_interval: average compact interval, in miliseconds. Defaults to 1 hour (1000 * 60 * 60).
    • cache_slots: Maximum number of objects to cache (only valid if type is 'cached_key_map'). Defaults to 1000.

Example:

var Alfred = require('alfred');
Alfred.open ('path/to/database/dir', function(err, db) {
  if (err) { throw err; }
  console.log(db.users); // -> undefined
  db.attach ('users', function(err, users_key_map) {
    if (err) { throw err; }
    // now you can use users_key_map
  })
});

database.ensure (key_map_name [, options], callback)

If not attached, attaches this key map.

If already attached does nothing. If you need to change the options on this key_map, you must detach and then attach.

Example:

var Alfred = require('alfred');
Alfred.open('path/to/database/dir', function(err, db) {
  if (err) { throw err; }
  db.ensure ('users', function(err, users_key_map) {
    if (err) { throw err; }
    // now you can use users_key_map
  })
});

database.detach (key_map_name, callback)

Detaches a key map from this database.

database.<key_map_name> must already be attached.

  • key_map_name: name of the key map to be detached.
  • callback (err): invoked when an error occurs or when key map is detached.

Example:

var Alfred = require('alfred');
Alfred.open('path/to/database/dir', function(err, db) {
  if (err) { throw err; }
  console.log('users' in db); // -> true
  db.detach('users', function(err) {
    if (err) { throw err; }
    console.log(db.users); // -> undefined
  })
});

database.close (callback)

Closes all key maps and meta data.

You should call this before leaving.

  • callback (err): when an error occurs or the database ends.

Events

Bind to events like this:

db.on('error', function(err) {
  // handle error
});

'error'

Emitted when there is an unhandled error. You should catch these.

callback (err)

'key_map_attached'

Emitted when a key map is attached into this database.

callback (key_map_name)

'index_added'

Emitted when an index is added into a key map attached to this db.

callback (key_map_name, index_name)

'index_dropped'

Emitted when an index is removed from a key map attached to this db.

callback (key_map_name, index_name)