1.2.16 • Published 4 years ago
koi-cli v1.2.16
Koi-cli (KOA Typescript API - Command Line Interface)
A simple CLI made to generate base files and migration files.
Prerequisite:
Clone the Koi project here:
Install:
npm i -g koi-cli
Usage:
1. Run to generate JSON file for structuring table attributes.
koi-generate:config $tableName
example: koi-generate:config user
2. Modify the JSON file in /src/db/config.
- Config:
# /src/db/config/user.json
# Sample table configuration only
{
"schema": "public",
"table": {
"tableName": "user",
"attributes" :{
"id": {
"primaryKey": true,
"dataType": "INTEGER", # Or "integer"
"allowNull": false,
},
"name": {
"dataType": "STRING",
"allowNull": false,
},
"password": {
"dataType": "STRING",
"allowNull": false,
},
"createdAt": {
"dataType": "DATE",
"allowNull": false,
},
"updatedAt": {
"dataType": "DATE",
"allowNull": false,
},
}
}
}
3. Run to generate migration files from config/ .json for sequelize to use.
koi-generate:migration
example: koi-generate:migration
4. Modify the migration file that just have been created.
- Migrations:
# /src/db/migrations/create-migration-user.js
# Sample migration only
"use strict";
module.exports = {
up: (queryInterface, Sequelize) => {
const DataTypes = Sequelize;
return queryInterface.createSchema("public")
.then(() => {
const tableConfig = {
schema: "public",
tableName: "user"
};
const tableProps = {
id: {
primaryKey: true,
type: DataTypes.INTEGER,
allowNull: false,
},
name: {
type: DataTypes.STRING,
allowNull: false,
},
password: {
type: DataTypes.STRING,
allowNull: false,
},
createdAt: {
type: DataTypes.DATE,
allowNull: false,
},
updatedAt: {
type: DataTypes.DATE,
allowNull: false,
},
};
return queryInterface.createTable(tableConfig, tableProps);
});
},
down: (queryInterface, Sequelize) => {
const tableConfig = {
schema: "public",
tableName: "user"
};
return queryInterface.dropTable(tableConfig).then(() => {
return queryInterface.dropTable(tableConfig);
});
}
};
5. Run to generate base files (models, controllers, routes, transformers and services).
koi-generate:base $tableName
example: koi-generate:base user
6. Modify the recently created base files.
- Models:
# /src/models/user.ts
# Sample model only
import {
DataTypes,
ModelAttributes,
} from 'sequelize';
import { BaseModel, ITableConfig } from '../baseModel';
export interface IUser {
id?: number;
name: string;
password: string;
}
export class User extends BaseModel implements IUser {
public id: number;
public name: string;
public password: string;
static getAttributes(): ModelAttributes {
return {
id: {
primaryKey: true,
type: DataTypes.INTEGER,
allowNull: false,
},
name: {
type: DataTypes.STRING,
allowNull: false,
},
password: {
type: DataTypes.STRING,
allowNull: false,
},
createdAt: {
type: DataTypes.DATE,
allowNull: false,
},
updatedAt: {
type: DataTypes.DATE,
allowNull: false,
},
};
}
public static getValidationRules(): any {
# Guide for validation Rules https://validatejs.org
return {};
}
static getTableNameConfig(): ITableConfig {
return {
schema: 'public',
tableName: 'users',
};
}
public static initAssociations(): void {
# Define your model association here
}
}
- Controllers:
# /src/controllers/v1/userController.ts
# Sample controller only
import { Context } from 'koa';
import { ICustomAppContext } from './../../typings';
import { User } from './../../models/core';
import { BaseBreadController } from '../baseBreadController';
import { UserTransformer } from './../../transformers';
import { UserService } from './../../services/dbService';
import { ValidationService } from './../../services';
# type alias for shortcut to trigger intellisense
type CustomContext = Context & ICustomAppContext;
export class UserController extends BaseBreadController {
constructor() {
super(
new UserTransformer(),
new UserService,
new ValidationService(User)
);
# set any of these variables to false as needed
this.hasBrowse = false;
this.hasRead = true;
this.hasEdit = true;
this.hasAdd = true;
this.hasDelete = true;
}
}
- Routes:
# /src/routes/v1/user.ts
# Sample route only
import * as Router from 'koa-router';
import { UserController } from '../../controllers/v1';
const route = new Router({ prefix: '/user' });
const userCtrl = new UserController();
route.get('/', userCtrl.browse);
route.get('/:id', userCtrl.read);
route.patch('/:id', userCtrl.edit);
route.post('/', userCtrl.add);
route.delete('/:id', userCtrl.delete);
export { route as userRoute };
- Transformers:
# /src/transformers/userTransformer.ts
# Sample transformer only
import { TransformableObjectAbstract } from './transformableObjectAbstract';
export class UserTransformer extends TransformableObjectAbstract {
protected objectType = 'user';
protected visibleFields = [
'username',
'firstName',
'middleName',
'lastName',
'phone',
'mobile',
];
}
License:
- MIT
Copyright 2019 © 98Labs, Inc
1.2.16
4 years ago
1.2.15
4 years ago
1.2.14
4 years ago
1.2.13
4 years ago
1.2.12
4 years ago
1.2.11
4 years ago
1.2.10
4 years ago
1.2.9
4 years ago
1.2.8
4 years ago
1.2.7
4 years ago
1.2.6
4 years ago
1.2.5
4 years ago
1.2.4
4 years ago
1.2.3
4 years ago
1.2.2
4 years ago
1.2.1
4 years ago
1.2.0
4 years ago
1.1.9
4 years ago
1.1.8
4 years ago
1.1.7
4 years ago
1.1.6
4 years ago
1.1.5
4 years ago
1.1.4
4 years ago
1.1.3
4 years ago
1.1.2
4 years ago
1.1.1
4 years ago
1.1.0
5 years ago
1.0.9
5 years ago
1.0.8
5 years ago
1.0.7
5 years ago
1.0.6
5 years ago
1.0.5
5 years ago
1.0.4
5 years ago
1.0.3
5 years ago
1.0.2
5 years ago
1.0.1
5 years ago
1.0.0
5 years ago