Examples (raw accesss)
Create a Key Map
var alfred = require('alfred');
alfred.open('path/to/db/dir', function(err, db) {
if (err) { throw err; }
db.ensure('users', function(err, users_key_map) {
console.log('users key map attached');
});
});
Populate
Populate a Key Map with some values
var USERS = {
1: {name: 'Pedro', age: 35, sex: 'm'}
, 2: {name: 'John', age: 32, sex: 'm'}
, 3: {name: 'Bruno', age: 28, sex: 'm'}
, 4: {name: 'Sandra', age: 35, sex: 'f'}
, 5: {name: 'Patricia', age: 42, sex: 'f'}
, 6: {name: 'Joana', age: 29, sex: 'f'}
, 7: {name: 'Susana', age: 30, sex: 'f'}
};
alfred.open('path/to/db/dir', function(err, db) {
if (err) { throw err; }
for (id in USERS) {
if (USERS.hasOwnProperty(id)) {
db.users.put(id, USERS[id], function(err) {
if (err) { throw err; }
console.log('user with id ' + id + ' inserted.');
});
}
}
});
Fetch
Fetch a value from a key map based on key.
Assuming we have db open and db.users attached, let's find user with key 4
db.users.get(4, function(err, user) {
if (err) { throw err; }
console.log(user.name); // -> 'Sandra'
});
Create index
Assuming we have db open and db.users attached, let's add the 'age' and 'sex' indexes':
// add 'age' index to users
db.users.addIndex('age', function(user) {
return user.age;
}, function(err) {
if (err) { throw err; }
console.log('age index added to users');
});
You can add a 'sex' index in a similar way.
Find
Assuming we have db open and db.users attached, and that db.users has indexes 'age' and 'sex':
// find the users with age = 35
db.users.find({age: {$eq: 35}}) (function(err, key, user) {
if (err) { throw err; }
console.log(user);
});
// find the feminine users with age > 28 and <= 35
db.users.find({age: {$gt: 28, $lte: 35}, sex: 'f'}) (function(err, key, user) {
if (err) { throw err; }
console.log(user);
});
// find the with age > 28 and <= 35 OR that are female
db.users.find({age: {$gt: 28, $lte: 35}}).or({sex: {$eq: 'f'}}) (function(err, key, user) {
if (err) { throw err; }
console.log(user);
});
Bulk find
Do the same thing, but get all the results on the same callback.
Assuming we have db open and db.users attached, and that db.users has indexes 'age' and 'sex':
// find the with age > 28 and <= 35 OR that are female
db.users.find({age: {$gt: 28, $lte: 35}}).or({sex: {$eq: 'f'}}).all(function(err, users) {
if (err) { throw err; }
users.forEach(function(user) {
console.log(user);
});
});
Find stream
Do the same thing, but get all the results as a stream.
Assuming we have db open and db.users attached, and that db.users has indexes 'age' and 'sex':
// find the with age > 28 and <= 35 OR that are female
var stream = db.users.find({age: {$gt: 28, $lte: 35}}).or({sex: {$eq: 'f'}}).stream();
stream.on('record', function(user) {
console.log(user);
});
stream.on('error' function(err) {
throw err;
});
stream.on('end', function() {
console.log('ended');
};
Activity stream
Stream filtered puts on a key map. And let it run...
Assuming we have db open and db.users attached:
var activity_stream = db.users.startStream(function(key, value) {
return value.age <= 30;
}, function(key, user) {
console.log('here is a young user: ' + require('util').inspect(user));
});
// eventually, stop the stream
db.users.stopStream(activity_stream);