Model Component Generator
A Node.js library and CLI for generating Objection.js Models and their supporting files. It helps to save time creating Models and their migrations, so that you can focus on implementing the business logic for your Node.js server-side project.
Features
- Creates an Objection.js model file via the commandline
- Creates the model's knex.js migration file for the model's database table
- Create a test model file in the tests folder by default
- Create a test seed data file in the tests/data folder by default
Dependencies
Install
npm i @anephenix/mcg
Usage
You can use MCG via the command line:
npx mcg Post
This will do the following:
- Create a folder called models, unless it already exists
- Create a model file called Post.js in the models folder
- Create a folder called migrations, unless it already exists
- Generate a migration file to create the posts table with the created_at and updated_at timestamp fields.
You can pass the following optional arguments:
- --testFolder - specify a custom folder name to put the test model file and seed data file in (e.g. test, spec)
- --mainDir - specify a custom folder path to generate all of the files in, in case your app's code is not in the current working directory
- --tableName - specify a custom database table name for the model and migration file (.e.g. blog_posts)
- --attributes - specify the fields to add to the model, migration and test seed data file, as a space-separated list of name:type pairs (e.g. title:string description:text published:boolean)
The supported attribute types are: string, text, integer, float, boolean, date, datetime and json. For example:
npx mcg Post --attributes title:string description:text published:boolean
This will add title, description and published columns to the migration file, matching properties to the model's jsonSchema, and sample values for each of them in the test seed data file.
There is also the option of setting these optional arguments in a mcg.config.js file that looks like this:
module.exports = {
testFolder: 'spec',
mainDir: 'app',
};
You can also use MCG programmatically, like this:
const mcg = require('@anephenix/mcg');
(async () => {
await mcg('Post');
})();
The mainDir and testFolder are optional 2nd and 3rd arguments to that command, and tableName and attributes are optional 4th and 5th arguments:
const path = require('path');
const mcg = require('@anephenix/mcg');
(async () => {
const mainDir = path.join(process.cwd(), 'app');
const testFolder = 'spec';
const tableName = undefined;
const attributes = ['title:string', 'description:text', 'published:boolean'];
await mcg('Post', mainDir, testFolder, tableName, attributes);
})();
License and Credits
2026 Anephenix Ltd. MCG is licensed under the MIT license.