0.7.0 • Published 7 years ago

adonis-generators v0.7.0

Weekly downloads
6
License
MIT
Repository
github
Last release
7 years ago

Adonis Generators

This repo contains generators for Adonis Models and Migrations ala Rails.

Installation

To install this package run:

npm install --save adonis-generators

Then in bootstrap/app.js make the following changes:

  • In the aceProviders array add: 'adonis-generators/providers/GeneratorsProvider',
  • In the commands array add: 'AdonisGenerators/Generate:Migration',
  • In the commands array add: 'AdonisGenerators/Generate:Model',
  • In the commands array add: 'AdonisGenerators/Generate:Controller',
  • In the commands array add: 'AdonisGenerators/Generate:JsonApiView',
  • In the commands array add: 'AdonisGenerators/Generate:JsonApiResource',

Use - JSON API Resource

This ONE command will build all of the boiler plate for a full JSON API resource for adonis-jsonapi applications:

./ace g:jsonapiresource User email:string password:string profile:hasOne

This is the same as running:

./ace g:jsonapiview User email:string password:string profile:hasOne
./ace g:controller User email:string password:string profile:hasOne -j
./ace g:migration User email:string password:string profile:hasOne
./ace g:model User email:string password:string profile:hasOne

Use - JSON API View

To generate a JsonApiView for a resource for adonis-jsonapi use the command g:jsonapiview:

./ace g:jsonapiview User email:string password:string profile:hasOne

This will create a file app/JsonApiViews/User.js:

const JsonApiView = require('adonis-jsonapi/src/JsonApiView');

class User extends JsonApiView {
  get attributes() {
    return ['email', 'password'];
  }

  profile() {
    return this.belongsTo('App/Http/JsonApiViews/Profile', true);
  }
}

module.exports = User;

Use - Controllers

To generate a new controller use the following command (note the -a which will create an API controller):

./ace g:controller User -a email:string password:string profile:hasOne

This will create a full RESTful controller:

'use strict'

const User = require('App/Model/User');

class UserController {

  * index(request, response) {
    const users = yield User.with('profile').fetch();

    response.send(users);
  }

  * store(request, response) {
    const input = request.only('email', 'password', 'user_id');
    const user = yield User.create(input);

    response.send(user);
  }

  * show(request, response) {
    const id = request.param('id');
    const user = yield User.with('profile').where({ id }).firstOrFail();

    response.send(user);
  }

  * update(request, response) {
    const input = request.only('email', 'password', 'user_id');
    const id = request.param('id');

    const user = yield User.with('profile').where({ id }).firstOrFail();
    yield user.update(input);

    response.send(user);
  }

  * destroy(request, response) {
    const id = request.param('id');
    const user = yield User.query().where({ id }).firstOrFail();
    yield user.delete();

    response.status(204).send();
  }

}

module.exports = UserController;

The g:controller command also supports JSON APIs using adonis-jsonapi by using the -j option:

./ace g:controller User email:string password:string profile:hasOne -j

This will create a full RESTful controller for JSON API:

'use strict';

const User = use('App/Model/User');
const attributes = ['email', 'password'];

class UserController {

  * index(request, response) {
    const users = yield User.with('profile').fetch();

    response.jsonApi('User', users);
  }

  * store(request, response) {
    const input = request.jsonApi.getAttributesSnakeCase(attributes);
    const foreignKeys = {
    };
    const user = yield User.create(Object.assign({}, input, foreignKeys));

    response.jsonApi('User', user);
  }

  * show(request, response) {
    const id = request.param('id');
    const user = yield User.with('profile').where({ id }).firstOrFail();

    response.jsonApi('User', user);
  }

  * update(request, response) {
    const id = request.param('id');
    request.jsonApi.assertId(id);

    const input = request.jsonApi.getAttributesSnakeCase(attributes);
    const foreignKeys = {
    };

    const user = yield User.with('profile').where({ id }).firstOrFail();
    yield user.update(Object.assign({}, input, foreignKeys));

    response.jsonApi('User', user);
  }

  * destroy(request, response) {
    const id = request.param('id');

    const user = yield User.query().where({ id }).firstOrFail();
    yield user.delete();

    response.status(204).send();
  }

}

module.exports = UserController;

Use - Migrations

To generate a new migration use the following command:

./ace g:migration User email:string password:string

This will create a migration for a User model (users table) with string columns for email and password.

To see all of the available field types and relations, check out the field types table.

Use - Models

To generate a new model use the following command:

./ace g:migration User email:string password:string profile:hasOne:Profile

This will generate a User model in the app/Model directory. It will also include the indicated profile relation:

profile() {
  return this.hasOne('App/Model/Profile', 'user_id');
}

NOTE All relations generated with this pacakge will include the full foreign key.

To see all of the available field types and relations, check out the field types table.

Using the -m flag will also generate a migration to match the specified model.

Field Types

StructureMigration OutputModel Output
field_name:integertable.integer('field_name')null
field_name:bigIntegertable.bigInteger('field_name')null
field_name:texttable.text('field_name')null
field_name:stringtable.string('field_name')null
field_name:booleantable.boolean('field_name')null
field_name:datetable.date('field_name')null
field_name:dateTimetable.dateTime('field_name')null
field_name:timetable.time('field_name')null
field_name:timestamptable.timestamp('field_name')null
field_name:binarytable.binary('field_name')null
field_name:jsontable.json('field_name')null
field_name:jsonbtable.jsonb('field_name')null
field_name:uuidtable.uuid('field_name')null
field_name:floattable.float('field_name')null
field_name:decimaltable.decimal('field_name')null
relationName:belongsTo:ParentModeltable.integer('foreign_key').reference('parent_model.id')return this.belongsTo('App/Model/ParentModel', 'foreign_key')
relationName:hasMany:ChildModelnullreturn this.hasMany('App/Model/ChildModel', 'foreign_key')
relationName:hasOne:ChildModelnullreturn this.hasOne('App/Model/ChildModel', 'foreign_key')
0.7.0

7 years ago

0.6.1

7 years ago

0.6.0

7 years ago

0.5.4

7 years ago

0.5.3

7 years ago

0.5.2

7 years ago

0.5.1

7 years ago

0.5.0

7 years ago

0.4.8

7 years ago

0.4.7

8 years ago

0.4.6

8 years ago

0.4.5

8 years ago

0.4.4

8 years ago

0.4.3

8 years ago

0.4.2

8 years ago

0.4.1

8 years ago

0.4.0

8 years ago

0.3.0

8 years ago

0.2.0

8 years ago

0.1.0

8 years ago