figment-cli v1.0.12
Figment-CLI
Figment CLI is a simple application that is used to create migration files, models, & queries in a more elegant manner. This package is heavily inspired by the excellent tools by Laravel: Artisan and Eloquent. While we currently lack many of the great features those packages have you can currently create migrations(tables), models(classes), run migrations, and generate queries. It is in it's early development stages and under goes massive changes daily thus rendering it a very unstable tool for production enviroments and we do not suggest you use it yet.
Get Started
npm i figment-cli --save-dev
tsconfig.json
"target": "es2020",
"module": "commonjs",
Commands
Start figment
<!-- Start Figment -->
npx fig
<!-- Publish migrations directory -->
fig init
Create Migrations
Creating a migration essentially creates a function that will be imported and executed into the migrate file. Good practices are to not use dashes, spaces, and singular words, however we handle that portion for you in case you for get
fig make:migration User
This will generate 1 file:
./migrations/users_migration.ts
export const UsersMigration = () => {
// Enter Your Tables
}
It will also edit the ./migrations/migrate.ts file:
// Call UsersMigration(); to migrate dogs table
import { UsersMigration } from './users_migration'
UsersMigration();
Running Migrations
To execute all of your migration files run:
fig migrate
If your migration function looked like the following:
export const UsersMigration = () => {
console.log('migrating user tables')
}
Creating Tables & Columns
The system for creating a table is using the Table class. For the most part, that is all you need to do to create a table. For now, we are mainly focusing on PostgreSQL due to this we will be using their documention as our system of record when it comes to nomenclature, strucutre, & guidelines.
Taking Full Advantage
To take full of advantage of these features we are using PG for our DB connection. You can simply put your DB variables in your .env files like so:
DB_HOST=DB_HOST
DB_USER=DB_USER
DB_PASSWORD=DB_PASSWORD
DB_NAME=DB_NAME
DB_PORT=DB_PORT
The Table class handles the connection internally which is enqued any time you run a migration.
Composing Columns
After generating your migration files you will need to configure your columns for the table. Navigate to your migration file:
- ./src/migrations/users_migration.ts
The following lines of code will be generated for you, however you can remove any of the defualt columns.
import { Table } from '../Table'
export const UsersMigration = () => {
const table = new Table('users', []);
table.id();
// Insert Your Columns
table.string('first_name')
table.timestamps();
table.migrate();
}
We will be giving a further in depth view of the data types we are offering, for now we have the basics:
- id - serial
- strings - varvhar
- numeric datatypes
- a few date types
- boolean
In order to make a field not nullable, you must pass false with the function call:
table.string('first_name', false)
Figment CLI also offers 3 default fields:
- id - auto incrementing
- timestamps
We are working out foreign keys & relationships but you can do so manually if you need to.
For more information around what columns look like from a structure point of view you may look at the Column Class below. This class is called in the Table class a data type function is called:
export class Column {
name: string;
type: string;
nullable?: boolean;
limit?: number;
def?: string;
increment?: number;
precision?: number;
scale?: number;
constructor(
name: string,
type: string,
nullable?: boolean | void,
limit?: number | void,
def?: string | void,
increment?: number | void,
precision?: number | void,
scale?: number | void
) {
...
}
More Examples
const table = new Table('users', []);
table.id();
table.string('name');
table.email();
table.limitChar('phone', 10, false);
table.integer('age');
table.smallint('favorite_number');
table.bigint('jersey_number');
table.numeric('grams', 1);
Migrating
As stated previously running:
fig migrate
Will call all of the migration files in ./src/migrations/migrate.ts which will then execute all of the table & column functions consecutively.
Extra Notes
One department that we are currently working on is error handling and messaging. Bare with us as we try to make this CLI better :)