apemandb

8.7.4 • Public • Published

apemandb

Build Status Code Climate Code Coverage npm Version JS Standard

Database for apeman project.

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

Installation

$ npm install apemandb --save

Setup

Create Configuration File.

Use apemanenv to configure for each environment.

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

env/database.json

{
  "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

'use strict'
 
const apemanenv = require('apemanenv')
 
module.exports = apemanenv(__dirname) // Exports all settings in dir.
 

Define Model

Create <model_name>.json at db/models directory

db/models/user.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

'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
 

Usage

Basic usage is same as Sequelize Models

Create a New Record

'use strict'
 
const db = require('./db')
let { User } = db.models
 
User.create({
  username: 'John'
}).then((result) => {
  /* ... */
})
 

License

This software is released under the MIT License.

Links

Package Sidebar

Install

npm i apemandb

Weekly Downloads

87

Version

8.7.4

License

MIT

Last publish

Collaborators

  • okunishinishi