# sukeru

> An Object Document Mapper for Riak

Latest version **0.1.0** (published 2015-03-08) · MIT license · 0 weekly downloads

## Install

```sh
npm install sukeru
pnpm add sukeru
yarn add sukeru
bun add sukeru
```

## Health

**Score 5/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: insecure dependencies; abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.1.0 |
| Published | 2015-03-08 |
| First published | 2015-03-08 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 8 |
| Known vulnerabilities | 0 (+9 in 3 direct dependencies) |
| Install scripts | no |
| GitHub stars | 2 |
| Author | Celrenheit |
| Maintainers | celrenheit |

## Links

- npm: https://www.npmjs.com/package/sukeru
- Repository: https://github.com/celrenheit/sukeru
- Issues: https://github.com/celrenheit/sukeru/issues
- npm.io page: https://npm.io/package/sukeru

## Dependencies (8)

- [lodash](https://npm.io/package/lodash.md) ^2.4.1
- [nodiak](https://npm.io/package/nodiak.md) git://github.com/celrenheit/nodiak.git#search_fixes
- [shortid](https://npm.io/package/shortid.md) ^2.1.3
- [inherits](https://npm.io/package/inherits.md) ~2.0.1
- [pluralize](https://npm.io/package/pluralize.md) ^1.1.0
- [utilities](https://npm.io/package/utilities.md) ^1.0.4
- [validator](https://npm.io/package/validator.md) ^3.26.0
- [merge-defaults](https://npm.io/package/merge-defaults.md) ^0.2.1

## Recent versions

- 0.1.0 (latest) — 2015-03-08

## 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)
});
```

---
_Source: https://npm.io/package/sukeru · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
