OOR
Documention | 简体中文 |
NodeJs ORM tool library , for Postgresql
, ElasticSearch
, MySql
.
Features
- High performance 🚀。
- Code is Type, Type is Code, Code is Schema!
- Easy API.
- Builtin Magic Suffix📍. Save your time and code lines.
- Support Elastic Search, Same API with SQL!
- Common business Support : Pagition, Ignore Column, Logical Delete, Date Marker.
- Promise only , NO callback!
Install
npm install --save oor pg
Setup
import { setup } from 'oor';
setup({ provider: { host:'1.2.3.4', port:5432, user:'postgres' ... } });
Define
import { Table, UType, Static } from 'oor';
export const UserSchema = UType.Table({
id: UType.Integer(),
name: UType.String({ maxLength: 32 }),
age: UType.Integer({ minimum: 0, maximum: 128 }),
sex: UType.Boolean(),
profile: UType.String({ ignore: true }),
address: UType.String({ maxLength: 128 }),
salary: UType.Number(),
registerDate: UType.Date({ column: 'register_date', isCreate: true }),
lastModify: UType.Date({ column: 'last_modify', isModify: true })
});
export type User = Static<typeof UserSchema>;
export const User = new Table('public.user', UserSchema);
Usage
const result = await User.all();
console.log(result);
const addResult = await User.add({
name: 'test',
age: 23,
sex: false,
address: 'address',
salary: 1221.2,
});
console.log('Add Result', addResult)
let userId = addResult.id;
const afterAdd= await User.getById(userId);
console.log('After Add', afterAdd)
await new Promise(r => setTimeout(r, 1234));
let isUpdate = await User.update({ id: userId, age: 60, });
console.log('Update is Success ? : ', isUpdate == 1);
const afterUpdate = await User.getById(userId);
console.log('After Update', afterUpdate);
let isDelete = await User.deleteById(userId);
console.log('Delete is Success ? : ', isDelete == 1);
const afterDelete = await User.getById(userId);
console.log('After Delete', afterDelete)
const result = await User.exec(
`SELECT XXX FROM YYY WHERE ZZZ = $1 ORDER BY $2 $3`,
['value','id','DESC']
);
console.log(result);
Elastic Search & MySql
Elastic Search has the same api with postgresql / mysql. Here is how :
import { Table, setup } from 'oor/es';
import { Client } from '@elastic/elasticsearch';
setup({
provider: {
node: 'https://localhost:9200',
auth: { username: 'elastic', password: 'changeme' },
tls: { ca: readFileSync('/home/ssh/pki/es_ca.crt'), rejectUnauthorized: false, }
},
showSQL: console.log
})