Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
400 additions
and
141 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -1,142 +1,9 @@ | |||
#examples | #examples | ||
h1 Examples | h1 Examples | ||
|
|
||
h2 Create a Key Map | |||
p | p | ||
pre | ul | ||
code | li | ||
| var alfred = require('alfred'); | a(href="/examples/odm") Examples using the Object Data Model (ODM) | ||
| alfred.open('path/to/db/dir', function(err, db) { | li | ||
| if (err) { throw err; } | a(href="/examples/raw") Raw access | ||
| db.ensure('users', function(err, users_key_map) { | |||
| console.log('users key map attached'); | |||
| }); | |||
| }); | |||
|
|||
h2 Populate | |||
p Populate a Key Map with some values | |||
pre | |||
code | |||
| 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'} | |||
| }; | |||
pre | |||
code | |||
| 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.'); | |||
| }); | |||
| } | |||
| } | |||
| }); | |||
|
|||
h2 Fetch | |||
p Fetch a value from a key map based on key. | |||
p Assuming we have db open and db.users attached, let's find user with key 4 | |||
p | |||
pre | |||
code | |||
| db.users.get(4, function(err, user) { | |||
| if (err) { throw err; } | |||
| console.log(user.name); // -> 'Sandra' | |||
| }); | |||
|
|||
h2 Create index | |||
p Assuming we have db open and db.users attached, let's add the 'age' and 'sex' indexes': | |||
p | |||
pre | |||
code | |||
| // 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'); | |||
| }); | |||
|
|||
p You can add a 'sex' index in a similar way. | |||
|
|||
h2 Find | |||
p Assuming we have db open and db.users attached, and that db.users has indexes 'age' and 'sex': | |||
p | |||
pre | |||
code | |||
| // find the users with age = 35 | |||
| db.users.find({age: {$eq: 35}}) (function(err, key, user) { | |||
| if (err) { throw err; } | |||
| console.log(user); | |||
| }); | |||
|
|||
p | |||
pre | |||
code | |||
| // 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); | |||
| }); | |||
|
|||
p | |||
pre | |||
code | |||
| // 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); | |||
| }); | |||
|
|||
h2 Bulk find | |||
p Do the same thing, but get all the results on the same callback. | |||
p Assuming we have db open and db.users attached, and that db.users has indexes 'age' and 'sex': | |||
p | |||
pre | |||
code | |||
| // 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); | |||
| }); | |||
| }); | |||
|
|||
h2 Find stream | |||
p Do the same thing, but get all the results as a stream. | |||
p Assuming we have db open and db.users attached, and that db.users has indexes 'age' and 'sex': | |||
p | |||
pre | |||
code | |||
| // 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'); | |||
| }; | |||
|
|||
h2 Activity stream | |||
p Stream filtered <i>puts</i> on a key map. And let it run... | |||
p Assuming we have db open and db.users attached: | |||
p | |||
pre | |||
code | |||
| 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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,53 @@ | |||
#examples | |||
h1 Examples (raw accesss) | |||
|
|||
h2 Define a User model | |||
p | |||
pre | |||
code | |||
| var User = db.define('User'); | |||
| User.property('name'); | |||
| User.property('address'); | |||
| User.property('age', Number); | |||
| User.property('married', 'boolean'); | |||
| User.property('email', 'string'); | |||
| User.index('age', function(user) { | |||
| return user.age; | |||
| }); | |||
|
|||
h2 Create a new User document | |||
p | |||
pre | |||
code | |||
| var user = User.new(USER); | |||
| user.save(function(validationErrors) { | |||
| assert.ok(errors === null, 'errors is not null'); | |||
| console.log('User saved'); | |||
| }); | |||
|
|||
h2 Find User document by ID | |||
p | |||
pre | |||
code | |||
| var id = 'abc'; | |||
| User.get(id, function(user) { | |||
| console.log('found user: ' + user.inspect()); | |||
| }); | |||
|
|||
h2 Find | |||
p | |||
pre | |||
code | |||
| User.find({age: {$lte: 30}}) (function(gotUser) { | |||
| assert.ok(user.equal(gotUser), 'user ('+user.toString()+') and gotten user ('+gotUser.toString()+') are different'); | |||
| done(); | |||
| }).all(function(users) { | |||
| assert.equal(1, users.length); | |||
| assert.ok(user.equal(users[0]), 'user ('+user.toString()+') and gotten user ('+users[0].toString()+') are different'); | |||
| done(); | |||
| }).where({age: {$gt: 30}}).all(function(users) { | |||
| assert.equal(0, users.length); | |||
| done(); | |||
| }); | |||
| }); | |||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,142 @@ | |||
#examples | |||
h1 Examples (raw accesss) | |||
|
|||
h2 Create a Key Map | |||
p | |||
pre | |||
code | |||
| 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'); | |||
| }); | |||
| }); | |||
|
|||
h2 Populate | |||
p Populate a Key Map with some values | |||
pre | |||
code | |||
| 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'} | |||
| }; | |||
pre | |||
code | |||
| 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.'); | |||
| }); | |||
| } | |||
| } | |||
| }); | |||
|
|||
h2 Fetch | |||
p Fetch a value from a key map based on key. | |||
p Assuming we have db open and db.users attached, let's find user with key 4 | |||
p | |||
pre | |||
code | |||
| db.users.get(4, function(err, user) { | |||
| if (err) { throw err; } | |||
| console.log(user.name); // -> 'Sandra' | |||
| }); | |||
|
|||
h2 Create index | |||
p Assuming we have db open and db.users attached, let's add the 'age' and 'sex' indexes': | |||
p | |||
pre | |||
code | |||
| // 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'); | |||
| }); | |||
|
|||
p You can add a 'sex' index in a similar way. | |||
|
|||
h2 Find | |||
p Assuming we have db open and db.users attached, and that db.users has indexes 'age' and 'sex': | |||
p | |||
pre | |||
code | |||
| // find the users with age = 35 | |||
| db.users.find({age: {$eq: 35}}) (function(err, key, user) { | |||
| if (err) { throw err; } | |||
| console.log(user); | |||
| }); | |||
|
|||
p | |||
pre | |||
code | |||
| // 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); | |||
| }); | |||
|
|||
p | |||
pre | |||
code | |||
| // 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); | |||
| }); | |||
|
|||
h2 Bulk find | |||
p Do the same thing, but get all the results on the same callback. | |||
p Assuming we have db open and db.users attached, and that db.users has indexes 'age' and 'sex': | |||
p | |||
pre | |||
code | |||
| // 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); | |||
| }); | |||
| }); | |||
|
|||
h2 Find stream | |||
p Do the same thing, but get all the results as a stream. | |||
p Assuming we have db open and db.users attached, and that db.users has indexes 'age' and 'sex': | |||
p | |||
pre | |||
code | |||
| // 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'); | |||
| }; | |||
|
|||
h2 Activity stream | |||
p Stream filtered <i>puts</i> on a key map. And let it run... | |||
p Assuming we have db open and db.users attached: | |||
p | |||
pre | |||
code | |||
| 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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.