DAO
=========

A modelling tool for RealtimeDatabase(firebase) & Dynamodb(Amazon) & Redis

inspired by dynamoose

## Installation

  `npm install @eyugame/dao`

## Usage
    const daoutils = require('@eyugame/dao/utils');

    // Set up firebase realtime database
    // npm install firebase-admin
    daoutils.setupDB({
        db: 'firebase',
        tablePrefix: 'books'
    });

    // Set up AWS dynamodb
    // npm install dynamodb
    daoutils.setupDB({
        db: 'aws',
        tablePrefix: 'books',
        throughput: {
            readCapacity: 0,
            writeCapacity: 0
        }
    })

    // Set up redis
    // npm install redis
    daoutils.setupDB({
        db: 'redis',
        host: 'localhost',
        port: 6379,
        tablePrefix: 'books',
        //password: '123321' //optional
    });

    const dao = require('@eyugame/dao');

    const User = dao.define('users', {
        hashKey: 'id',
        schema: {
            id: joi.string().required(),
            name: joi.string().required(),
            age: joi.number().default(18)
        },
        tableName: 'test_users',
        FIREBASE_IGNORED: ['id'],       //optional, default: [hashKey]
        methods: {
            toVO() {
                const result = {
                    ...this._entity
                };
                delete result.id;
                return result;
            }
        }
    });

    const Book = dao.define('books', {
        hashKey: 'isbn',
        rangeKey: 'author',
        schema: {
            isbn: joi.string().required(),
            author: joi.string().required(),
            title: joi.string().required()
        },
        tableName: 'books',
        FIREBASE_IGNORED: ['isbn', 'author'],       //optional, default: [hashKey,rangeKey]
        methods: {
            toVO() {
                const result = {};
                Object.assign(result, this._entity);
                return result;
            }
        }
    });


    const id = 'hp';

    //----------------------static methods----------------------

    let entity = User.loadOrCreate({id});
    User.load({id});
    User.create({id,name:'Harry Potter'});
    User.update({id,name:'EyuGame Tech', age: 12});
    User.destroy({id});
    User.exists({id});
    //User.listAll({platform:'IOS'});
    User.alter({id,{age: 1}});
    User.batchGet(['hp']);

    const keys = [{isbn: isbn1, author}, {isbn: isbn2, author}]
    let books = await Book.batchGet(keys);
    for (let e of books) {
        console.log('RangeKey BatchGet:::', e.toVO())
    }

    //----------------------instance methods----------------------
    entity.name = 'hello';
    entity.save({transaction:true});
    entity.remove();
    entity.toVO();
    
  
  
  


## Tests

  `npm test`

## Contributing

^_^