1.0.5 • Published 3 years ago
@thanathip/mysql-orm v1.0.5
mysql-orm
Simple mysql object relation model.
Install
Install with npm:
npm install --save @thanathip/mysql-orm
Usage
const { DB , Model } = require('@thanathip/mysql-orm');
// ES6
import { DB , Model } from '@thanathip/mysql-orm'
const db = await DB.table('users').all()
// simple
class User extends Model {}
await new User()
.select('id','username')
.whereIn('id',[1,2,3])
.orwhere('id',4)
.latest('id')
.get()
await new User()
.select('id')
.oldest('id')
.first()
Insert Update , Delete
class User extends Model {}
// insert
await new User()
.create({
'id' : 1,
'username': 'users'
}).save()
// update
await new User()
.where('id',1)
.update({
'username': 'users1'
}).save()
// delete
await new User().where('id',1).delete()
Relation
// support 'hasOne' ,'hasMany' ,'belongsTo','belongsToMany'
class User extends Model {}
class Car extends Model {
constructor(){
super()
this.RELATION = [
{
name : 'user',
relation:'belongsTo',
model: User
},
]
}
}
// #######################
class Car extends Model {}
class User extends Model{
constructor(){
super()
this.RELATION = [
{
name : 'cars',
relation:'hasMany',
model: Car
},
]
}
}
await new User().with('cars').get() // hasMany
await new Car().with('user').first() // belongsTo
Method
where(column , operator , value)
orwhere(column , operator , value)
whereIn(column , [])
whereNotIn(column , [])
whereNull(column)
whereNotNull(column)
whereBetween (column , [value1 , value2])
whereSubQuery(colmn , rawSql)
select(column1 ,column2 ,...)
join (primary key , table.foreign key)
limit (value)
orderBy (column ,'ASC' || 'DSCE')
latest (column)
oldest (column)
groupBy (column)
// exc
all() // array object all data
get() // array object with condition
first() // object
firstOrFail() // object if null throw 404
toArray(column) // array
count(column) // number
raw(rawSQL) // array object with raw sql
pagination(limit,page) // current page total & array object
// relation
with(name relation1, name relation2 ,....)
Cli
npm install @thanathip/mysql-orm -g
// use cmd
thanathip make:model FOLDER/MODEL
// ex
thanathip make:model Models/User
const { Model } = require ('@thanathip/mysql-orm')
// es6
// import Model from '@thanathip/mysql-orm'
class User extends Model{
constructor(){
super()
/*
add relation to model
this.RELATION = [
{
name : ...,
relation : ... ,
model : ... ,
pk: ... ,
fk: ... ,
select : ...,
hidden : ...,
freezeTable : ...
}
required name , relation , modal
optional pk ,fk , select, hidden , freezeTable
]
this.RELATION_SUB = []
*/
/*
config model
this.DEFAULT_ORDER_BY = false // default order by id
this.DEFAULT_SCOPE = {
where: {}, // column : value
whereNot : {},
whereNull : {},
whereNotNull : {}
}
this.TIMESTAMP = true
this.SOFT_DELETE = false
this.freezeTable('your table')
this.PATTERN = 'snake_case' // 'camelCase' or 'snake_case'
*/
}
}
// es6
// export default User()
module.exports= User()
env
NODE_ENV = development
DB_HOST = localhost
DB_PORT = 3306
DB_USERNAME = root
DB_PASSWORD = password
DB_DATABASE = database
DB_HOST_PROD =
DB_PORT_PROD =
DB_USERNAME_PROD =
DB_PASSWORD_PROD =
DB_DATABASE_PROD =