1.0.0 • Published 2 years ago

mysql-adonis-schema v1.0.0

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

mysql-adonis-schema

Generate adonis schema from MySQL database

Installation

Install mysql-adonis-schema with npm

npm install mysql-adonis-schema --save-dev

Usage/Examples

Create a file named mysql-adonis-schema.json and fill it as follows (adjust to your needs):

{
  "host": "127.0.0.1",
  "port": 3306,
  "user": "root",
  "password": "secret",
  "database": "myapp"
}

Create user table:

CREATE TABLE `user` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `name` varchar(255) NOT NULL,
  `username` varchar(255) NOT NULL,
  `password` varchar(255) NOT NULL,
  `profile_picture` varchar(255) DEFAULT NULL,
  `role` enum('admin','user') NOT NULL,
  PRIMARY KEY (`id`)
);

Then run the command:

npx mysql-adonis-schema

The above command will create a user.ts file with the following contents:

import { schema } from '@ioc:Adonis/Core/Validator'

export const user_schema = {
  id: schema.string,
  name: schema.string,
  username: schema.string,
  password: schema.string,
  profile_picture: schema.string,
  role: schema.enum,
}

Config

mysql-adonis-schema.json

{
  "host": "127.0.0.1",
  "port": 3306,
  "user": "root",
  "password": "secret",
  "database": "myapp",
  "tables": ["user", "log"],
  "ignore": ["adonis_schema_versions", "adonis_schema"],
  "folder": "app/Schema",
  "suffix": "schema",
  "camelCase": false
}
OptionDescription
tablesFilter the tables to include only those specified.
ignoreFilter the tables to exclude those specified.
folderSpecify the output directory.
suffixSuffix to the name of a generated file. (eg: user.schema.ts)
camelCaseConvert all table names and their properties to camelcase. (eg: profile_picture becomes profilePicture)