tspace-mgorm
Query builder object relation mapping
Install
Install with npm:
npm install tspace-mgorm --save
Usage
DB
import { DB } from 'tspace-mgorm'
(async () => {
await new DB().raw((connect,db) => {
const result = await db.collection('users').find().toArray()
connect.close()
return result
}})
await new DB().collection('users').whereIn('id',[1,2,3]).get()
await new DB().collection('users').first()
await new DB().find(1)
await new DB()
.create({
name : 'name',
username: 'users'
}).save()
await new DB()
.whereUser(1)
.update({
name: 'users12345'
}).save()
await new DB()
.where('id',1)
.delete()
})()
Model for relationship
support hasOne ,hasMany,belongsTo
import { Model } from 'tspace-mgorm'
import Role from '../Role'
import Project from '../Project'
import Role from '../Role'
class User extends Model {
constructor(){
super()
this.hasMany({name : 'projects', model: Project })
this.hasOne({name : 'project', model: Project})
this.belongsTo({name : 'role', model: Role , localField : 'roleId' ,foreignField : '_id' })
}
}
export default User
import User from '../User'
(async () => {
await new User().with('car','phones').get()
})()
Method chaining
method chaining for query data
where(column , operator , value)
whereIn(column , [])
whereNotIn(column , [])
whereNull(column)
whereNotNull(column)
whereBetween (column , [value1 , value2])
join ({from,localField,foreignField,as,isObject})
limit (limit)
latest (...column)
oldest (...column)
insert(objects)
create(objects)
update (objects)
with(name1 , name2,...nameN)
get()
first()
find(id)
save()
Setup
.env connection to database
NODE_ENV = development
MONGODB_HOST = localhost
MONGODB_PORT = 3306
MONGODB_DATABASE = database
MONGODB_HOST_PROD = localhost
MONGODB_PORT_PROD = 3306
MONGODB_DATABASE_PROD = database