A Laravel inspired schema builder for Harness Flex and DB Migrate.
Project is no longer maintained. Please use Formidablejs' Schema Builder instead.
This package is automatically installed with the Harness Flex Framework.
npm i @harnessflex/schema-builder
Schema Builder works on both JavaScript based projects and imba based projects.
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(),
])
}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!
]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
addandchangemethods, return aChangeColumninstance.
| 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 |
☑ |
| 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. |
| 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')| 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. |
return db.createTable('users', columns([
id(),
string('name'),
string('email').unique(),
string('password'),
timestamp('email_verified_at').nullable(),
timetamps(),
]))db.createTable 'users', columns [
id!
string 'name'
string('email').unique!
string 'password'
timestamp('email_verified_at').nullable!
timetamps!
]| 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. |
return change( longText('api_key').nullable() )
.whereTable('users')
.using(db)change( longText('api_key').nullable! )
.whereTable('users')
.using dbBefore 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/migrationsThis command will create 2 new tables named users and posts in your database.
Note:
db-migrateanddb-migrate-mysqlmust be installed globally.
If you discover any security related issues, please email donaldpakkies@gmail.com instead of using the issue tracker.
The MIT License (MIT). Please see License File for more information.