PG ORM
JS ORM for PostgreSQL
This package has moved to @5no/pg-model
Install
@5no/pg-model requires Node version 8 or above.
npm install --save @5no/pg-model
.env
DATABASE_URL=postgres://test:123123@127.0.0.1:5432/testDB?ssl=falseDATABASE_QUERY_LOG=true
Schema
table: schema: 'public' //database schema name name: 'users' //table name columns: // describe columns name: // column name type: String // String, Number, Boolean, Date, Object, Array primaryKey: true // only for primaryKey defaultValue: null // default value required: true // validate values prefilled: true // filling field by default schema: {} // Can describe object schema name: // Auto filling column name type: Date created: true // Fill in create moment format: 'YYYY-MM-DD HH:mm:ss' // Format date name: // Auto filling column name type: Date updated: true // Fill in update moment format: 'YYYY-MM-DD HH:mm:ss' // Format date relations: Info: // name model: UsersInfo // model join: model: Roles // Join model local: 'role_id' // local column foreign: 'id' // external table field type: 'one' // one, many // join model local: 'id' // local column foreign: 'user_id' // external table field type: 'one' // one, many, join cascade: // cascade methods "save", "delete" 'save' 'delete'
Model Methods
async // Save changesasync // set changes by Json dataasync // set changes by Json dataasync // Save changes by Json dataasync delete // Delete entriesasync // Return to JSON format
Manager.build(model, json = false)
Builder uses @5no/pg-builder
// get a Builder for customing query // get one row by primary keys // get one row by filter // get all rows by filter // get count rows
Model Relations Type "many" Methods
await // Model data JSON // get rows by filter // get row by filter
Model Relations Type "join" Methods
await // Model data | Json | ID // get rows by filter if type many // get row by filter if type many
Examples
const Manager Model = static schema = table: schema: 'public' name: 'roles' columns: id: type: String primaryKey: true defaultValue: null role: type: String defaultValue: null created_at: type: Date created: true format: 'YYYY-MM-DD HH:mm:ss' updated_at: type: Date updated: true format: 'YYYY-MM-DD HH:mm:ss' relations: {} static schema = table: schema: 'public' name: 'settings' columns: id: type: String primaryKey: true defaultValue: null name: type: String defaultValue: null created_at: type: Date created: true format: 'YYYY-MM-DD HH:mm:ss' updated_at: type: Date updated: true format: 'YYYY-MM-DD HH:mm:ss' relations: {} static schema = table: schema: 'public' name: 'user_roles' columns: user_id: type: String defaultValue: null primaryKey: true role_id: type: String defaultValue: null primaryKey: true relations: {} static schema = table: schema: 'public' name: 'user_setting' columns: user_id: type: String defaultValue: null primaryKey: true setting_id: type: String defaultValue: null primaryKey: true relations: {} static schema = table: schema: 'public' name: 'users_address' columns: id: type: String primaryKey: true defaultValue: null user_id: type: String defaultValue: null required: true street_name: type: String defaultValue: null postcode: type: String defaultValue: null created_at: type: Date created: true format: 'YYYY-MM-DD HH:mm:ss' updated_at: type: Date updated: true format: 'YYYY-MM-DD HH:mm:ss' relations: {} static schema = table: schema: 'public' name: 'users_info' columns: id: type: String primaryKey: true defaultValue: null user_id: type: String defaultValue: null required: true first_name: type: String defaultValue: null last_name: type: String defaultValue: null created_at: type: Date created: true format: 'YYYY-MM-DD HH:mm:ss' updated_at: type: Date updated: true format: 'YYYY-MM-DD HH:mm:ss' relations: {} static schema = table: schema: 'public' name: 'users' columns: id: type: String primaryKey: true defaultValue: null email: type: String required: true public_key: type: String required: true secret_key: type: String defaultValue: '' required: true personalised: type: Object prefilled: true defaultValue: test: 100 properties: type: Array defaultValue: schema: name: type: String required: true filters: 'lowerCase' value: type: String required: true created_at: type: Date created: true format: 'YYYY-MM-DD HH:mm:ss' updated_at: type: Date updated: true format: 'YYYY-MM-DD HH:mm:ss' relations: Info: model: UsersInfo local: 'id' foreign: 'user_id' type: 'one' cascade: 'save' 'delete' Addresses: model: UsersAddresses local: 'id' foreign: 'user_id' type: 'many' cascade: 'save' 'delete' Roles: model: UserRoles join: model: Roles local: 'role_id' foreign: 'id' type: 'many' local: 'id' foreign: 'user_id' type: 'join' cascade: 'save' 'delete' Setting: model: UserSetting join: model: Settings local: 'setting_id' foreign: 'id' type: 'one' local: 'id' foreign: 'user_id' type: 'join' cascade: 'save' 'delete'
CREATE NEW ENTRY
const roleModel = roleModelrole = 'Admin'await roleModel const settingModel = settingModelname = 'AdminSetting'await settingModel const testNewUser = testNewUseremail = 'test@test.me'testNewUserpublic_key = 'test_123'testNewUsersecret_key = 'test_333' testNewUserproperties = name: 'Test' value: 'OK' await testNewUserAddressesawait testNewUserAddresses testNewUserInfofirst_name = 'Aleks2'testNewUserInfolast_name = 'Sokol2' await testNewUserRoles await testNewUserSetting const returnData = await testNewUser /* If all correct function return boolean "true" otherwise array errorsError: [ 'duplicate key value violates unique constraint "users_email_index"', ]*/
BUILDER
const builder = await Manager builder const data = await builderrows
GET
const dataJson = await Manager
return
id: '7852468e-ac99-4f5e-9ee3-d506b0c4424e' secret_key: 'test_333' email: 'test@test.me' public_key: 'test_123' created_at: '2018-12-20 17:10:31' updated_at: '2018-12-20 17:10:31' personalised: test: 100 properties: name: 'test' value: 'OK' Info: id: '0320dc4f-4ca7-4b65-bd42-52f286a0b9db' user_id: '7852468e-ac99-4f5e-9ee3-d506b0c4424e' first_name: 'Aleks2' last_name: 'Sokol2' created_at: '2018-12-20 17:10:31' updated_at: '2018-12-20 17:10:31' Addresses: id: 'be40ccb3-3a33-4b6e-9467-6907b0c4396b' user_id: '7852468e-ac99-4f5e-9ee3-d506b0c4424e' street_name: 'Test' postcode: '100500' created_at: '2018-12-20 17:10:31' updated_at: '2018-12-20 17:10:31' id: 'f5bae3e9-290b-451e-a0e2-1ec2d9eaf543' user_id: '7852468e-ac99-4f5e-9ee3-d506b0c4424e' street_name: 'Test1' postcode: '100502' created_at: '2018-12-20 17:10:31' updated_at: '2018-12-20 17:10:31' Roles: created_at: '2018-12-20 17:10:31' id: 'be40ccb3-3a33-4b6e-9467-6907b0c4396b' role: 'Admin' updated_at: '2018-12-20 17:10:31' Setting: created_at: '2018-12-20 17:10:31' id: 'be40ccb3-3a33-4b6e-9467-6907b0c4396b' name: 'AdminSetting' updated_at: '2018-12-20 17:10:31'
UPDATE
let data = await Manager let oneAddresses = dataAddressesoneAddressespostcode = '100508' dataInfolast_name = 'Sokol200'datasecret_key = 'test_33309' await data
SAVE BY DATA
const testNewUser = const newData = email: 'test2010@test.me' public_key: 'test_123' secret_key: 'test_333' personalised: test: 100 Addresses: street_name: 'Test 100' postcode: '100501' const returnData = await testNewUser
DELETE
let data = await Managerawait data /* If all correct function return boolean "true" otherwise array errors*/
DELETE ONE ITEM FROM RELATE
let data = await Managerlet dataDel = dataRolesawait dataDel
License
MIT Licensed, Copyright (c) 2018 Aleksandr Sokol