raven.js | |
|---|---|
ravenjs Guide | |
Getting Startedravenjs is available as a NPM module. Use npm to install the ravenjs package:
| |
| Import the ravenjs module | var raven = require('ravenjs'); |
Configuration | |
Using a connection string | raven.connectionString('Url=http://localhost:80;Database=FooDB;UserName=Foo;Password=**;ApiKey=**'); |
| Use the standard ravendb connection string format (case sensitive): | |
| |
| |
| |
| |
| |
| NOTE: If both User and Password and ApiKey is provided, the User and Password crdedentials are preferred. | |
Individual configuration options | |
| Configure the RavenDB Host to connect to. Accepts a fully qualified URL incuding port no. | raven.host('http://localhost:80'); |
| The database tennant to conenct to. | raven.database('FooDb'); |
| The Username credentials to use when connecting to the server. | raven.username('FooUser'); |
| The Password credentials to use when connecting to the server. | raven.password('*****'); |
| The ApiKey to use when connecting to the server. | raven.apiKey('****'); |
| The proxy server to use for conencting to the server. Only http and https URL schemes are supported. | raven.proxy('http://foooproxy:8080'); |
| Enable optimistic concurrency mode. Default is false. | raven.useOptimisticConcurrency(true); |
| Specify a custom id finder function. The document is provided as a single argument to the function and expects a string return value. | raven.idFinder(function(doc) {
if (doc.id) return doc.id;
if (doc.Id) return doc.Id;
}); |
| Specify a custom id generator function. The document and a callback function are provided as arguments to the function. The callback expects two arguments: Error, if any, and the generated id. NOTE: The default id generator uses the HiLoIdGenerator part of ravenjs to generate new ids. | raven.idGenerator(function(doc, callback) {
var id; //generate a new id...
callback(undefined, id);
}); |
Environment specific configuration | |
| ravenjs supports arbitary environment specific configuration ala Expressjs. Use the | raven.configure('development', function() {
raven.connectionString('Url=http://localhost:80')
.useOptimisticConcurrency(true);
}); |
Creating a ravenjs connection | |
| Once ravenjs is configured, you can use the | var client = raven.connect(); |
|
| var client = raven.connect({
database:'FooDb',
useOptimisticConcurrency: true
}); |
Document Operations | |
Get a document by id | client.get('foo', function(error, doc) {
console.log(doc);
}); |
| Use the | |
| |
Saving / Updating a document | |
| Use the | client.save('person/1', doc, function(error) {
console.log(error);
}); |
| The id parameter is optional, and if it's not provided ravenjs uses the default id finder to find the current id of the document. If ravenjs cannot find an id, and it's not an existing document, a new id is assigned to it using the configured id generator. | client.save(doc, function(error) {
console.log(error);
}); |
Deleting a document | |
| Use the | client.remove('person/1', function(error) {
console.log(error);
}); |
| Optionally you can pass in an existing document retrieved using the | client.remove(doc, function(error) {
console.log(error);
}); |
Index Operations | |
| Index operations are accessible by the | |
Creating a new index | |
| Use the | client.index('CityPopulation')
.map('from doc in docs select new {doc.Address.City, Count = 1}')
.reduce('from result in results group result by result.City into g select new {City = g.Key, Count = g.Sum(x => x.Count)}')
.create(function(error) {
console.log(error);
}); |
Deleting an existing index | |
| Use the | client.index('CityPopulation').remove(function(error) {
console.log(error);
}); |
Query Operations | |
| Query operations are accessible by the | |
Querying an index | |
| Specify the index name when invoking | client.query('CityPopulation').results(function(error, data) {
console.log('Total Results : ' + data.TotalResults);
}); |
Query filtering, sorting, projections and paging | |
| Use the | client.query('CityPopulation')
.where('City').is('Boston')
.results(function(error, data) {
console.log('Total Population of Boston: ' + data.Results[0].Count);
}); |
| The | client.query('CityPopulation')
.where('City').is(['Boston', 'New York'])
.results(function(error, data) {
console.log('Total Population of Boston and New York :' + data.Results[0].Count);
}); |
| The | client.query('Albums')
.where('Genre.Name').isEither(['Rock', 'Grunge'])
.results(function(error, data) {
for(var album in data.Results) {
console.log(album.Title);
}
}); |
| You can chain filters using additional | client.query('Albums')
.where('Title').is('*Great*')
.and('Genre.Name').is('Rock')
.results(function(error, data) {
console.log('Total Rock Albums where title contains Great:' + data.TotalResults);
}); |
| Use the | client.query('Albums')
.where('Genre.Name').is('Rock')
.orderBy('Title')
.results(function(error, data) {
for(var album in data.Results) {
console.log(album.Title);
}
}); |
| You can also sort descending | client.query('Albums')
.where('Genre.Name').is('Rock')
.orderByDescending('Title')
.results(function(error, data) {
for(var album in data.Results) {
console.log(album.Title);
}
}); |
| Use the | client.query('Albums')
.select('Title').is('Genre.Name')
.results(function(error, data) {
for(var album in data.Results) {
console.log(util.format('Album %s is of %s genre', album.Title, album.Name));
}
}); |
| Use the | client.query('Albums')
.skip(10)
.take(10)
.results(function(error, data) {
for(var album in data.Results) {
console.log(album.Title);
}
}); |
Dynamic Queries | |
| ravenjs also supports dynamic queries. Use the same | |
| Issue a dynamic query on a collection | client.query().collection('Persons')
.results(function(error, data) {
for(var person in data.Results) {
console.log(person.firstName);
}
}); |
| Issue a dynamic query using a where filter | client.query()
.where('Artist.Name').is('U2')
.results(function(error, data) {
console.log('Total Albums by U2: ' + data.TotalResults);
}); |
Attachment Operations | |
| Attachment operations are accessible using the | |
Getting an attachment | |
| Use the | client.attachment('IMG001.jpg', function(error, data) {
fs.writeFile('IMG001.jpg', data, function(err) {
console.log('Saved IMG001!');
});
}); |
Saving an attachment | |
| Use the | |
| fs.readFile('IMG001.jpg', function(err, data) {
if (err) throw err;
client.attachment('IMG001.jpg').save({ data: data}, function(error) {
console.log('Image saved!');
});
}); |
Deleting an attachment | |
| Use the | client.attachment('ING001.jpg', function(error) {
console.log('Image deleted!');
}); |
Utilities | |
| |
| Creates a new object and assigns the Raven-Entity-Name metadata to the newly created object. | var video = raven.create('Videos');
video.Title = 'Foo Bar';
client.save(video, function(error) {
console.log('Video Saved!');
}); |
| |
| Default id finder function used by ravenjs to discover id's of a document. | function defaultIdFinder(doc) {
if (!doc) return undefined;
if (doc['@metadata'] && doc['@metadata']['@id']) return doc['@metadata']['@id'];
if (doc.hasOwnProperty('id')) return doc.id;
if (doc.hasOwnProperty('Id')) return doc.Id;
} |
| |
| Default id generator function used by ravenjs to generate id's for a document when saving. The default id generator uses the built in HiLoIdGenerator to generate an id. | function defaultIdGenerator(settings, callback) {
if (!settings) throw Error('Expected a valid setings object.');
if (!settings.host) throw Error('Invalid settings. Expected host property.');
if (!callback || !_(callback).isFunction) throw Error('Exepected a valid callback function.');
var generator = new HiLoIdGenerator(settings);
generator.nextId(function(error, id) {
if (error) return callback(error);
return callback(undefined, id.toString());
});
}
|