{"id":13787169,"url":"https://github.com/rtablada/adonis-generators","last_synced_at":"2025-04-12T10:22:29.066Z","repository":{"id":57173566,"uuid":"65164098","full_name":"rtablada/adonis-generators","owner":"rtablada","description":null,"archived":false,"fork":false,"pushed_at":"2016-11-20T00:33:57.000Z","size":79,"stargazers_count":16,"open_issues_count":2,"forks_count":4,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-26T05:16:22.025Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/rtablada.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-08-08T01:49:24.000Z","updated_at":"2024-06-24T16:06:29.000Z","dependencies_parsed_at":"2022-08-24T14:40:44.452Z","dependency_job_id":null,"html_url":"https://github.com/rtablada/adonis-generators","commit_stats":null,"previous_names":[],"tags_count":15,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rtablada%2Fadonis-generators","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rtablada%2Fadonis-generators/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rtablada%2Fadonis-generators/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rtablada%2Fadonis-generators/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/rtablada","download_url":"https://codeload.github.com/rtablada/adonis-generators/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248550877,"owners_count":21122981,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-03T20:00:30.726Z","updated_at":"2025-04-12T10:22:29.034Z","avatar_url":"https://github.com/rtablada.png","language":"JavaScript","readme":"# Adonis Generators\n\nThis repo contains generators for Adonis Models and Migrations ala Rails.\n\n## Installation\n\nTo install this package run:\n\n```bash\nnpm install --save adonis-generators\n```\n\nThen in `bootstrap/app.js` make the following changes:\n\n* In the `aceProviders` array add: `'adonis-generators/providers/GeneratorsProvider',`\n* In the `commands` array add: `'AdonisGenerators/Generate:Migration',`\n* In the `commands` array add: `'AdonisGenerators/Generate:Model',`\n* In the `commands` array add: `'AdonisGenerators/Generate:Controller',`\n* In the `commands` array add: `'AdonisGenerators/Generate:JsonApiView',`\n* In the `commands` array add: `'AdonisGenerators/Generate:JsonApiResource',`\n\n## Use - JSON API Resource\n\nThis **ONE** command will build all of the boiler plate for a full JSON API resource for `adonis-jsonapi` applications:\n\n```bash\n./ace g:jsonapiresource User email:string password:string profile:hasOne\n```\n\nThis is the same as running:\n\n```bash\n./ace g:jsonapiview User email:string password:string profile:hasOne\n./ace g:controller User email:string password:string profile:hasOne -j\n./ace g:migration User email:string password:string profile:hasOne\n./ace g:model User email:string password:string profile:hasOne\n```\n\n## Use - JSON API View\n\nTo generate a JsonApiView for a resource for `adonis-jsonapi` use the command `g:jsonapiview`:\n\n```bash\n./ace g:jsonapiview User email:string password:string profile:hasOne\n```\n\nThis will create a file `app/JsonApiViews/User.js`:\n\n```js\nconst JsonApiView = require('adonis-jsonapi/src/JsonApiView');\n\nclass User extends JsonApiView {\n  get attributes() {\n    return ['email', 'password'];\n  }\n\n  profile() {\n    return this.belongsTo('App/Http/JsonApiViews/Profile', true);\n  }\n}\n\nmodule.exports = User;\n```\n\n## Use - Controllers\n\nTo generate a new controller use the following command (note the `-a` which will create an API controller):\n\n```bash\n./ace g:controller User -a email:string password:string profile:hasOne\n```\n\nThis will create a full RESTful controller:\n\n```js\n'use strict'\n\nconst User = require('App/Model/User');\n\nclass UserController {\n\n  * index(request, response) {\n    const users = yield User.with('profile').fetch();\n\n    response.send(users);\n  }\n\n  * store(request, response) {\n    const input = request.only('email', 'password', 'user_id');\n    const user = yield User.create(input);\n\n    response.send(user);\n  }\n\n  * show(request, response) {\n    const id = request.param('id');\n    const user = yield User.with('profile').where({ id }).firstOrFail();\n\n    response.send(user);\n  }\n\n  * update(request, response) {\n    const input = request.only('email', 'password', 'user_id');\n    const id = request.param('id');\n\n    const user = yield User.with('profile').where({ id }).firstOrFail();\n    yield user.update(input);\n\n    response.send(user);\n  }\n\n  * destroy(request, response) {\n    const id = request.param('id');\n    const user = yield User.query().where({ id }).firstOrFail();\n    yield user.delete();\n\n    response.status(204).send();\n  }\n\n}\n\nmodule.exports = UserController;\n```\n\nThe `g:controller` command also supports JSON APIs using `adonis-jsonapi` by using the `-j` option:\n\n```bash\n./ace g:controller User email:string password:string profile:hasOne -j\n```\n\nThis will create a full RESTful controller for JSON API:\n\n```js\n'use strict';\n\nconst User = use('App/Model/User');\nconst attributes = ['email', 'password'];\n\nclass UserController {\n\n  * index(request, response) {\n    const users = yield User.with('profile').fetch();\n\n    response.jsonApi('User', users);\n  }\n\n  * store(request, response) {\n    const input = request.jsonApi.getAttributesSnakeCase(attributes);\n    const foreignKeys = {\n    };\n    const user = yield User.create(Object.assign({}, input, foreignKeys));\n\n    response.jsonApi('User', user);\n  }\n\n  * show(request, response) {\n    const id = request.param('id');\n    const user = yield User.with('profile').where({ id }).firstOrFail();\n\n    response.jsonApi('User', user);\n  }\n\n  * update(request, response) {\n    const id = request.param('id');\n    request.jsonApi.assertId(id);\n\n    const input = request.jsonApi.getAttributesSnakeCase(attributes);\n    const foreignKeys = {\n    };\n\n    const user = yield User.with('profile').where({ id }).firstOrFail();\n    yield user.update(Object.assign({}, input, foreignKeys));\n\n    response.jsonApi('User', user);\n  }\n\n  * destroy(request, response) {\n    const id = request.param('id');\n\n    const user = yield User.query().where({ id }).firstOrFail();\n    yield user.delete();\n\n    response.status(204).send();\n  }\n\n}\n\nmodule.exports = UserController;\n```\n\n## Use - Migrations\n\nTo generate a new migration use the following command:\n\n```bash\n./ace g:migration User email:string password:string\n```\n\nThis will create a migration for a `User` model (`users` table) with string columns for `email` and `password`.\n\nTo see all of the available field types and relations, check out the [field types table](#field-types).\n\n## Use - Models\n\nTo generate a new model use the following command:\n\n```bash\n./ace g:migration User email:string password:string profile:hasOne:Profile\n```\n\nThis will generate a `User` model in the `app/Model` directory.\nIt will also include the indicated `profile` relation:\n\n```js\nprofile() {\n  return this.hasOne('App/Model/Profile', 'user_id');\n}\n```\n\n\u003e **NOTE** All relations generated with this pacakge will include the full foreign key.\n\nTo see all of the available field types and relations, check out the [field types table](#field-types).\n\nUsing the `-m` flag will also generate a migration to match the specified model.\n\n\n## Field Types\n\n| Structure                                          | Migration Output                                          | Model Output                                                  |\n|----------------------------------------------------|-----------------------------------------------------------|---------------------------------------------------------------|\n| field_name:integer                                 | table.integer('field_name')                               | null                                                          |\n| field_name:bigInteger                              | table.bigInteger('field_name')                            | null                                                          |\n| field_name:text                                    | table.text('field_name')                                  | null                                                          |\n| field_name:string                                  | table.string('field_name')                                | null                                                          |\n| field_name:boolean                                 | table.boolean('field_name')                               | null                                                          |\n| field_name:date                                    | table.date('field_name')                                  | null                                                          |\n| field_name:dateTime                                | table.dateTime('field_name')                              | null                                                          |\n| field_name:time                                    | table.time('field_name')                                  | null                                                          |\n| field_name:timestamp                               | table.timestamp('field_name')                             | null                                                          |\n| field_name:binary                                  | table.binary('field_name')                                | null                                                          |\n| field_name:json                                    | table.json('field_name')                                  | null                                                          |\n| field_name:jsonb                                   | table.jsonb('field_name')                                 | null                                                          |\n| field_name:uuid                                    | table.uuid('field_name')                                  | null                                                          |\n| field_name:float                                   | table.float('field_name')                                 | null                                                          |\n| field_name:decimal                                 | table.decimal('field_name')                               | null                                                          |\n| relationName:belongsTo[:ParentModel][:foreign_key] | table.integer('foreign_key').reference('parent_model.id') | return this.belongsTo('App/Model/ParentModel', 'foreign_key') |\n| relationName:hasMany[:ChildModel][:foreign_key]    | null                                                      | return this.hasMany('App/Model/ChildModel', 'foreign_key')   |\n| relationName:hasOne[:ChildModel][:foreign_key]     | null                                                      | return this.hasOne('App/Model/ChildModel', 'foreign_key')    |\n","funding_links":[],"categories":["Databases, ORMs, Migrations \u0026 Seeding"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frtablada%2Fadonis-generators","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frtablada%2Fadonis-generators","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frtablada%2Fadonis-generators/lists"}