https://github.com/celrenheit/sukeru
Sukeru - Riak Object Document Mapper
https://github.com/celrenheit/sukeru
Last synced: 9 months ago
JSON representation
Sukeru - Riak Object Document Mapper
- Host: GitHub
- URL: https://github.com/celrenheit/sukeru
- Owner: celrenheit
- License: mit
- Created: 2015-02-16T09:09:09.000Z (over 11 years ago)
- Default Branch: master
- Last Pushed: 2015-03-09T21:44:21.000Z (over 11 years ago)
- Last Synced: 2025-08-16T15:27:20.994Z (10 months ago)
- Language: JavaScript
- Homepage:
- Size: 188 KB
- Stars: 2
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: Readme.md
- License: LICENSE
Awesome Lists containing this project
README
# Overview
Sukeru is an Object Document Mapper for Riak.
# Installation
```shell
$ npm install sukeru
```
# Usage
First we need to connect to the database (localhost by default):
```javascript
sukeru.connect(function() {
// Inside this we can define our models and do our queries...
});
```
## Model definition
Let's define a basic User model:
```javascript
var User = sukeru.model('User', function() {
this.string('name');
this.string('email');
this.string('password');
});
```
## Data types
The currently available data types are:
* string
* boolean
* date
## Accessing a Model
```javascript
var User = sukeru.model('User');
```
## Methods
Now that the User model is created, we can create a new user and than save it to the database:
```javascript
var john = new User();
john.name = "John";
john.email = "john@example.com";
john.password = "my secret password";
john.save(function(err) {
if(err)
return console.log(err);
console.log("The user has been successfully created", john);
console.log("It should have a new generated id:", john.id);
});
```
Let's find a user by id:
```javascript
User.findById("aNIdToFind", function(err, user) {
if(err)
return console.log(err);
console.log("A user was found", user);
});
```
To delete this user:
```javascript
john.remove(function(err) {
console.log("John should not exist anymore in the database");
});
```
To delete a user by its id:
```javascript
User.delete("aNIdToDelete", function(err) {
console.log("This user should not exist anymore in the database");
});
```
## Validation
You can specify validation rules for each field this way:
```javascript
var User = sukeru.model('User', function() {
this.string('name').required();
this.string('email').required()
.email();
this.string('password').required()
.minLength(6)
.maxLength(16);
this.date('birthday').required()
.after(new Date(2012, 01, 01))
.before(new Date(2015, 01, 01));
});
```
You can also pass a default value as a second argument of the property type:
```javascript
this.date('birthday', new Date());
this.string('name', 'John');
```
## Defining methods and statics
```javascript
var User = sukeru.model('User', function() {
this.string('name_s');
this.string('email');
this.string('password');
this.methods.comparePassword = function(password) {
return (this.password === password);
};
this.statics.findByName = function(name, callback) {
this.search({
q: "name_s:"+name
}, cb);
};
});
// Example usage
// Static function
User.findByName("John", function(err, users) {
// Do something here with your users
});
// Instance level methods
var user = new User();
user.email = "test";
user.password = "secret";
user.comparePassword("falsepassword"); // should be false
user.comparePassword("secret"); // should be true
```
## Search
### Configuring Riak
Create a search index (replace **user** by the name you choose for your search index):
```shell
$ curl -XPUT $RIAK_HOST/search/index/user -H 'Content-Type: application/json' -d '{"schema":"_yz_default"}'
```
Create a bucket-type (if it is not already):
```shell
$ riak-admin bucket-type create searchable '{"props": {"search_index":"user"}}'
```
Activate this bucket-type:
```shell
$ riak-admin bucket-type activate searchable
```
### Using search
Riak has a built-in Solr search engine.
Solr needs a schema to define each fields name and types. By default, Riak provides us a default schema.
For strings to be indexed (using default schema) we need to add the suffixe: "_s".
> ***Note :*** For now you are only able to use the default search schema for Solr provided by Riak's Team (thus the suffix "_s")
> You case use a custom schema but you will have to create it yourself.
Let's say we want to be able to search for the name of a user. We can modify the model definition like this:
```javascript
var User = sukeru.model('User', function() {
this.string('name_s')
this.string('email');
this.string('password');
});
```
To search for an object we can run a Solr query:
```javascript
User.search({
q: "name_s:John",
rows: 10
}, function(err, users) {
console.log("Here we have our results", users)
});
```