Skip to content
This repository was archived by the owner on Sep 24, 2021. It is now read-only.
harnessflexPublic archive

About

A Eloquent inspired schema builder for Harness Flex and DB Migrate.

Topics

Resources

Stars

7 stars

Watchers

1 watching

Forks

Repository files navigation

Schema Builder

A Laravel inspired schema builder for Harness Flex and DB Migrate.

Status npm GitHub

Project is no longer maintained. Please use Formidablejs' Schema Builder instead.

Install

This package is automatically installed with the Harness Flex Framework.

npm i @harnessflex/schema-builder

Basic Usage

Schema Builder works on both JavaScript based projects and imba based projects.

JavaScript

const { columns, id, foreign, string, longText, timestamps, softDeletes, timestamp } = require('@harnessflex/schema-builder')

exports.up = function (db) {
    return db.createTable('posts', columns([
        id(),
        foreign('user_id').references('id').on('users').onDelete('cascade'),
        string('title'),
        longText('body'),
        string('slug').unique(),
        timestamp('published_at').nullable(),
        softDeletes(),
        timestamps(),
    ])
}

Imba

const { columns, id, foreign, string, longText, timestamps, softDeletes, timestamp } = require '@harnessflex/schema-builder'

exports.up = do(db)
    db.createTable 'posts', columns [
        id!
        foreign('user_id').references('id').on('users').onDelete 'cascade'
        string 'title'
        longText 'body'
        string('slug').unique!
        timestamp('published_at').nullable!
        softDeletes!
        timestamps!
    ]

Modifying Tables

To add a new column to an existing table, use the add helper method:

const { string, add } = require('@harnessflex/schema-builder')

...

exports.up = function (db) {
    return add( string('api_key').nullable().after('password') )
        .whereTable('users')
        .using(db)
};

And to change a column in an existing table, use the change helper method:

const { change, longText } = require('@harnessflex/schema-builder')

...

exports.up = function (db) {
    return change( longText('api_key').nullable() )
        .whereTable('users')
        .using(db)
};

Both, the add and change methods, return a ChangeColumn instance.

Columns

Column Params Type Unique Constraint Support
bigIncrements name: string Column ☑
bigInteger name: string Column ☑
binary name: string Column ☑
blob name: string Column ☐
boolean name: string Column ☑
char name: string Column ☑
date name: string Column ☑
dateTime name: string Column ☑
decimal name: string Column ☑
foreign name: string ForeignColumn ☐
id Column ☑
integer name: string Column ☑
longText name: string Column ☐
real name: string Column ☑
smallInteger name: string Column ☑
softDeletes Column ☑
string name: string Column ☑
text name: string Column ☐
time name: string Column ☑
timestamp name: string Column ☑
timestamps currentTimeStamp: boolean object ☑

Other

Method Params Description
add column: Column or ForeignColumn Add a new column to an existing table.
change column: Column or ForeignColumn Change a column in an existing table.
columns columns: array A collection of columns.
schema columns: array A collection of columns.

API


ForeignColumn

Method Params Description
references column: string Reference column of another table.
on table: string Reference table.
onDelete rule: string Add onDelete rule.
onUpdate rule: string Add onUpdate rule.

JavaScript and Imba

foreign('user_id').references('id').on('users').onDelete('cascade')

Column

Method Params Description
after column: string Add column after another column.
length length: integer Set column length.
primary primary: boolean Set column as primary key.
autoIncrement increment: boolean Add auto increment attribute.
nullable nullable: boolean Mark column nullable.
unique isUnique: boolean Mark column unique.
unsigned isUnsigned: boolean Mark column unsigned.
default value: mixed Set column default value.

JavaScript

return db.createTable('users', columns([
	id(),
	string('name'),
	string('email').unique(),
	string('password'),
	timestamp('email_verified_at').nullable(),
	timetamps(),
]))

Imba

db.createTable 'users', columns [
	id!
	string 'name'
	string('email').unique!
	string 'password'
	timestamp('email_verified_at').nullable!
	timetamps!
]

ChangeColumn

Method Params Description
where table: string Set table name.
whereTable table: string Set table name.
using db: object Add/change column and return db-migrate db instance.

JavaScript

return change( longText('api_key').nullable() )
    .whereTable('users')
    .using(db)

Imba

change( longText('api_key').nullable! )
    .whereTable('users')
    .using db

Example (JavaScript)

Before running the example project, edit the database.json config file located under the example folder.

When done, run the following command:

db-migrate up -m=example/migrations

This command will create 2 new tables named users and posts in your database.

Note: db-migrate and db-migrate-mysql must be installed globally.

Security

If you discover any security related issues, please email donaldpakkies@gmail.com instead of using the issue tracker.

License

The MIT License (MIT). Please see License File for more information.

About

A Eloquent inspired schema builder for Harness Flex and DB Migrate.

Topics

Resources

Stars

7 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages