ORM (beta)
Object-Relational mapping
Installation
Use the package manager npm to install ORM.
npm install @ijx/orm
Documentation
All models should be inherit Schema
You can easy switch mysql versions (mysql/mysql2) in file ./src/connectors/mysql-conn.js line 2
import MysqlPool from "../drivers/MySQL_pool.js";
import MysqlPool from "../drivers/MySQL_pool2.js";
List schema static functions
get(id: string|number): Promise<Schema>
getBy(key: string, id: string|number): Promise<Schema>
getAll(where?: Where, limit?: number|null, offset?: number): Promise<Schema[]>
add(dataDB: object, checkExists?: boolean): Promise<Schema|null>
delete(id: string|number): Promise<boolean>
deleteAll(where?: Where, limit?: number|null, offset?: number): Promise<number>
count(where?: Where): Promise<number>
List schema functions
setExampleAttribute...(value: any): Schema
save(): Promise<number>
delete(): Promise<void>
toObject(): Object
toJSON(replacer?: (this: any, key: string, value: any) => any, space?: string | number): string
Debug
import ORM, { DBConnector, Schema } from "@ijx/orm"
ORM.onDebug(msg => console.log(msg));
DBConnector.onDebug(msg => console.log(msg));
Schema.onDebug(msg => console.log(msg));
Primary key types:
TypePK.AUTO
Autogenerate primary keyTypePK.NONE
Required to specify primary key
Column types
Type.INT
IntegerType.UINT
Positive integerType.FLOAT
Decimal numberType.STRING
StringType.TEXT
Long stringType.DATE
Date: 03/01/1994Type.DATETIME
Datetime: 03/01/1997 13:45:58Type.BOOLEAN
True or false
Examples
Example Load
import ORM, { Connector } from "@ijx/orm"
await ORM.addEntities([ User ]).init({
db: {
conn: Connector.MYSQL,
host: "localhost",
port: 3306,
user: "user",
pass: "password",
name: "dbname",
pref: "cig_"
}
});
Example model
import { Schema, Type, TypePK } from "@ijx/orm"
export default class User extends Schema {
static config = {
columns: {
id: { type: Type.UINT, size: 11, pk: TypePK.AUTO },
username: { type: Type.STRING, size: 32, required: true },
displayname: { type: Type.STRING, size: 100 },
password: { type: Type.STRING, size: 128, required: true },
email: { type: Type.STRING, size: 60, required: true },
phone: { type: Type.STRING, size: 12 },
block: { type: Type.BOOLEAN, required: true, default: false },
verify_level: { type: Type.UINT, values: [0, 1, 2] },
},
unique: [ "username" ],
createdAt: true,
modifiedAt: false,
};
}
Example use
import { Where, Cmp } from "@ijx/orm";
import User from "./models/user.model.js"
const user = await User.get(2);
user
.setBlock(true)
.setDisplayname("displayed2")
.setVerifyLevel(1);
await user.save();
console.log(user.toJSON());
const users = await User.getAll(
Where.AND(
Cmp.EQ("username", "usuario1"),
Cmp.GE("verify_level", 1)
)
);