figment-orm v1.0.7
Figment-ORM
Figment-ORM 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 provided 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 alter tables. 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-orm
OR
npm i figment-orm -g
Commands
Start figment
<!-- Start Figment -->
npx fig // if you did not install it globally
<!-- 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 forget
fig make:migration User
This will generate 1 file:
./migrations/users_migration.ts
export const UsersMigration = () => {
// Enter Your Tables
}
UsersMigration();
Running Migrations
To execute all of your migration files run:
fig migrate
Creating Datbase, 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.
Database - Dev
If you need to, you may also create a database by running
fig make:database myApp
To use this feature, name your database in your env file. Fig will attempt to create a database using this connection however it will fail. When it fails, we default to the postgres connection on your local machine to create the db. We currently only use this for development purposes.
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 'figment-orm'
export const UsersMigration = () => {
const table = new Table('users', []);
table.id();
// Insert Your Columns
table.string('first_name')
table.timestamps();
table.migrate();
}
UsersMigration()
Adding Columns
Adding new columns is quite simple. Run the following command.
fig make:migration add_phone_to_users
The important things to note is including "add" and the trailing "_table_name". This allows fig to distguish a new table from a table alteration. The content between "add" and "_table_name" does not matter, however we do suggest to be clear for ease of understanding.
Running this command will generate a new file in migrations './src/migration/add_phone_to_users_migration.ts' which will look very similar to a standard migration file with one acception. The acception is that a table alteration creates an instance of AlterTable instead of Table.
import { AlterTable } from 'figment-orm'
export const AddTailsToFishMigration = () => {
const table = new AlterTable('fish', []);
table.string('tails');
table.alter_table();
}
AddTailsToFishMigration()
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 orm also offers 3 default or prewritten fields:
- id - auto incrementing
- timestamps
We are working out foreign keys & relationships but you can do so manually if you need to.
User Table Example
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);