apemandb
==========

<!---
This file is generated by ape-tmpl. Do not update manually.
--->

<!-- Badge Start -->
<a name="badges"></a>

[![Build Status][bd_travis_shield_url]][bd_travis_url]
[![Code Climate][bd_codeclimate_shield_url]][bd_codeclimate_url]
[![Code Coverage][bd_codeclimate_coverage_shield_url]][bd_codeclimate_url]
[![npm Version][bd_npm_shield_url]][bd_npm_url]
[![JS Standard][bd_standard_shield_url]][bd_standard_url]

[bd_repo_url]: https://github.com/apeman-labo/apemandb
[bd_travis_url]: http://travis-ci.org/apeman-labo/apemandb
[bd_travis_shield_url]: http://img.shields.io/travis/apeman-labo/apemandb.svg?style=flat
[bd_travis_com_url]: http://travis-ci.com/apeman-labo/apemandb
[bd_travis_com_shield_url]: https://api.travis-ci.com/apeman-labo/apemandb.svg?token=
[bd_license_url]: https://github.com/apeman-labo/apemandb/blob/master/LICENSE
[bd_codeclimate_url]: http://codeclimate.com/github/apeman-labo/apemandb
[bd_codeclimate_shield_url]: http://img.shields.io/codeclimate/github/apeman-labo/apemandb.svg?style=flat
[bd_codeclimate_coverage_shield_url]: http://img.shields.io/codeclimate/coverage/github/apeman-labo/apemandb.svg?style=flat
[bd_gemnasium_url]: https://gemnasium.com/apeman-labo/apemandb
[bd_gemnasium_shield_url]: https://gemnasium.com/apeman-labo/apemandb.svg
[bd_npm_url]: http://www.npmjs.org/package/apemandb
[bd_npm_shield_url]: http://img.shields.io/npm/v/apemandb.svg?style=flat
[bd_standard_url]: http://standardjs.com/
[bd_standard_shield_url]: https://img.shields.io/badge/code%20style-standard-brightgreen.svg

<!-- Badge End -->


<!-- Description Start -->
<a name="description"></a>

Database for apeman project.

<!-- Description End -->


<!-- Overview Start -->
<a name="overview"></a>


apemandb is thin wrapper of [Sequelize][sequelize_url], and optimized for apeman web apps.

[sequelize_url]: http://docs.sequelizejs.com

<!-- Overview End -->


<!-- Sections Start -->
<a name="sections"></a>

<!-- Section from "doc/guides/01.Installation.md.hbs" Start -->

<a name="section-doc-guides-01-installation-md"></a>

Installation
-----

```bash
$ npm install apemandb --save
```


<!-- Section from "doc/guides/01.Installation.md.hbs" End -->

<!-- Section from "doc/guides/02.Setup.md.hbs" Start -->

<a name="section-doc-guides-02-setup-md"></a>

Setup
---------

### Create Configuration File.

Use [apemanenv][apemanenv_url] to configure for each environment.

1. Create database.json in env directory
2. Exports env directory with apemanenv

**env/database.json**
```javascript
{
  "default": {
    "DIALECT": "mysql",
    "SCHEMA": "apeman-demo-web",
    "PORT": 3306,
    "HOST": "localhost",
    "USERNAME": "apeman-demo-web",
    "PASSWORD": "apeman-demo-web"
  },
  "production": {
    "SCHEMA": "apeman-demo-web",
    "USERNAME": "xxxxxxxxxx",
    "PASSWORD": "xxxxxxxxxx"
  },
  "development": {
    "SCHEMA": "apeman-demo-web_dev",
    "USERNAME": "apeman-demo-web_dev",
    "PASSWORD": "apeman-demo-web_dev"
  },
  "test": {
    "DIALECT": "sqlite",
    "SCHEMA": "apeman-demo-web_test",
    "USERNAME": "apeman-demo-web_test",
    "PASSWORD": "apeman-demo-web_test",
    "STORAGE": "tmp/test-database.db"
  }
}
```

**env/index.js**
```javascript
'use strict'

const apemanenv = require('apemanenv')

module.exports = apemanenv(__dirname) // Exports all settings in dir.

```

### Define Model

Create &lt;model_name&gt;.json at db/models directory

**db/models/user.json**
```json
{
  "$name": "ExampleModel",
  "$description": "Some example",
  "$inherits": [
    "apemandb/models/abstract/ap_keyed.json"
  ],
  "$attributes": {
    "username": {
      "$type": "STRING",
      "$unique": true
    },
    "introText": {
      "$type": "STRING(1024)",
      "$nullable": true
    },
    "profileData": {
      "$type": "TEXT",
      "$convert": {
        "$json": true
      }
    }
  },
  "$indices": []
}
```

### Exports Database module

Create database instance from env and models

**db/index.js**
```javascript
'use strict'

const apemandb = require('apemandb')

let db = apemandb({
  env: require('../env')('database'),
  models: `${__dirname}/models/*.json`,
  addons: `${__dirname}/addons/*.addon.js`,
  seeds: `/seeds/${process.env.NODE_ENV}/*.seed.js`
})

// Models are exposed as db.models.<ModelName> (like db.models.User)
module.exports = db

```


[apemanenv_url]: https://github.com/apeman-labo/apemanenv
[sequelize_url]: http://docs.sequelizejs.com

<!-- Section from "doc/guides/02.Setup.md.hbs" End -->

<!-- Section from "doc/guides/03.Usage.md.hbs" Start -->

<a name="section-doc-guides-03-usage-md"></a>

Usage
---------

Basic usage is same as [Sequelize Models][sequelize_model_usage_url]

### Create a New Record

```javascript
'use strict'

const db = require('./db')
let { User } = db.models

User.create({
  username: 'John'
}).then((result) => {
  /* ... */
})

```

[sequelize_model_usage_url]: http://docs.sequelizejs.com/en/latest/docs/models-usage/


<!-- Section from "doc/guides/03.Usage.md.hbs" End -->


<!-- Sections Start -->


<!-- LICENSE Start -->
<a name="license"></a>

License
-------
This software is released under the [MIT License](https://github.com/apeman-labo/apemandb/blob/master/LICENSE).

<!-- LICENSE End -->


<!-- Links Start -->
<a name="links"></a>

Links
------

+ [apeman][apeman_url]
+ [apemanenv][apemanenv_url]
+ [sequelize][sequelize_url]

[apeman_url]: https://github.com/apeman-labo/apeman
[apemanenv_url]: https://github.com/apeman-labo/apemanenv
[sequelize_url]: http://docs.sequelizejs.com

<!-- Links End -->
