Relation
This is meant to be the best ORM in node. Taking inspiration from knex and sequelize, but the end goal to to completely mimick Laravel's Eloquent package. In order to achieve the best syntax possible, we are using ES6 Proxies, which is now supported in the latest version of node. Currently, only mysql is supported, but adding a new driver is trivial.
npm install relation --save
//if using mysql driver
npm install mysql --save
Setup
You must set the following environment variables in your app. We recommend creating a .env
file and using dotenv
DB_DRIVER=mysql
DB_HOST=localhost
DB_USERNAME=test
DB_PASSWORD=secret
DB_NAME=blah
Create a Model
chat.js
/* overwrite table name, this function is optional static tableName() { return 'dashboard_chats' } */
Using the Model
As long as the plural version of the model is available in the database (you can overwrite this), you can query the database.
console.log(chats)}
Supported methods
.all()
returns everything in the table.where({ fieldName: 'value' })
returns any matching results.create({ field: 'value'})
create a new row.select('column', 'column2')
contrain rows to select.first()
returns first results.limit(5)
limits the query
Query Building
Chat Chat Chat Chat
Relationships
This is a huge WIP, feel free to contribute :)
Supported:
- One To One
- One To Many
Todo:
- One To Many (Inverse)
- Many To Many
- Has Many Through
- Polymorphic Relations
- Many To Many Polymorphic Relations
One to One Example
{ return this } let chat = await Chat //any relationship will return a promise with the resultlet user = await chatuser tobe
One to Many Example
{ return this } let user = await User //has many results return a query builder instancelet chats = await userchats
Migrations
Will go over this soon...
CLI
If you install relation globally (npm install relation -g
) you can access the CLI methods to help create migrations, models, etc.
Migrations
relation make:migration User -m
-m will create a model as well
This will create a migration file that will allow you to build out tables.
Models
relation make:model User
Creates a file in your current directory /models/user.js
with a default model