0.0.6 • Published 3 years ago

@harnessflex/schema-builder v0.0.6

Weekly downloads
-
License
MIT
Repository
github
Last release
3 years ago

Schema Builder

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

Status npm GitHub

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

ColumnParamsTypeUnique Constraint Support
bigIncrementsname: stringColumn
bigIntegername: stringColumn
binaryname: stringColumn
blobname: stringColumn
booleanname: stringColumn
charname: stringColumn
datename: stringColumn
dateTimename: stringColumn
decimalname: stringColumn
foreignname: stringForeignColumn
idColumn
integername: stringColumn
longTextname: stringColumn
realname: stringColumn
smallIntegername: stringColumn
softDeletesColumn
stringname: stringColumn
textname: stringColumn
timename: stringColumn
timestampname: stringColumn
timestampscurrentTimeStamp: booleanobject

Other

MethodParamsDescription
addcolumn: Column or ForeignColumnAdd a new column to an existing table.
changecolumn: Column or ForeignColumnChange a column in an existing table.
columnscolumns: arrayA collection of columns.
schemacolumns: arrayA collection of columns.

API

ForeignColumn

MethodParamsDescription
referencescolumn: stringReference column of another table.
ontable: stringReference table.
onDeleterule: stringAdd onDelete rule.
onUpdaterule: stringAdd onUpdate rule.

JavaScript and Imba

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

Column

MethodParamsDescription
aftercolumn: stringAdd column after another column.
lengthlength: integerSet column length.
primaryprimary: booleanSet column as primary key.
autoIncrementincrement: booleanAdd auto increment attribute.
nullablenullable: booleanMark column nullable.
uniqueisUnique: booleanMark column unique.
unsignedisUnsigned: booleanMark column unsigned.
defaultvalue: mixedSet 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

MethodParamsDescription
wheretable: stringSet table name.
whereTabletable: stringSet table name.
usingdb: objectAdd/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.

0.0.6

3 years ago

0.0.5

4 years ago

0.0.4

4 years ago

0.0.3

4 years ago

0.0.2

4 years ago

0.0.1

4 years ago