2.7.3 • Published 2 months ago

@ijx/orm v2.7.3

Weekly downloads
-
License
ISC
Repository
github
Last release
2 months ago

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 element by ID
 * @param id PK value
 * @return schema with data
 */
get(id: string|number): Promise<Schema>
/**
 * Get element by other ID
 * @param key: column name
 * @param id value
 * @return schema with data
 */
getBy(key: string, id: string|number): Promise<Schema>
/**
 * Get array of elements (ORDER BY coming soon)
 * @param where where conditions to filter
 * @param limit number of elements
 * @param offset initial value to get elements
 * @return array of elements
 */
getAll(where?: Where, limit?: number|null, offset?: number): Promise<Schema[]>
/**
 * Add new element
 * @param dataDB Object with data
 * @param checkExists Check if the value already exists, no insert if exists
 * @return checkExists == true ? Schema : null
 */
add(dataDB: object, checkExists?: boolean): Promise<Schema|null>
/**
 * Delete element by ID
 * @param id PK value
 * @return true if element is deleted
 */
delete(id: string|number): Promise<boolean>
/**
 * Delete multiple elements
 * @param where where conditions to filter
 * @param limit number of elements
 * @param offset initial value to delete elements
 * @return number of elements deleted
 */
deleteAll(where?: Where, limit?: number|null, offset?: number): Promise<number>
/**
 * Get number of elements
 * @param where where conditions to filter
 * @return number of elements
 */
count(where?: Where): Promise<number>

List schema functions

/**
 * Set value to attribute
 * @param value data
 * @return schema
 */
setExampleAttribute...(value: any): Schema
/**
 * Save schema
 * @return number of changed attributes
 */
save(): Promise<number>
/**
 * Delete schema
 */
delete(): Promise<void>
/**
 * Devuelve un objeto para transformar en json
 */
toObject(): Object
/**
 * Transform schema to json string
 */
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 key
  • TypePK.NONE Required to specify primary key

Column types

  • Type.INT Integer
  • Type.UINT Positive integer
  • Type.FLOAT Decimal number
  • Type.STRING String
  • Type.TEXT Long string
  • Type.DATE Date: 03/01/1994
  • Type.DATETIME Datetime: 03/01/1997 13:45:58
  • Type.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, // Auto generated in db: created_at
		modifiedAt: false, // Auto generated in db: modified_at
	};
}

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)
	)
);
2.7.3

2 months ago

2.7.0

4 months ago

2.7.2

4 months ago

2.7.1

4 months ago

2.6.4

7 months ago

2.6.3

7 months ago

2.6.2

7 months ago

2.6.1

7 months ago

2.6.0

7 months ago

2.5.19

8 months ago

2.5.18

8 months ago

2.5.17

8 months ago

2.5.16

8 months ago

2.5.15

9 months ago

2.5.14

9 months ago

2.5.13

9 months ago

2.5.12

9 months ago

2.5.11

9 months ago

2.5.10

9 months ago

2.5.9

9 months ago

2.5.8

9 months ago

2.5.7

9 months ago

2.5.6

9 months ago

2.5.5

9 months ago

2.5.4

9 months ago

2.5.3

9 months ago

2.5.2

9 months ago

2.5.1

9 months ago

2.5.0

9 months ago

0.5.0

9 months ago

0.4.4

9 months ago

0.4.3

9 months ago

0.4.2

9 months ago

0.4.1

9 months ago

0.3.10

9 months ago

0.3.9

9 months ago

0.3.8

9 months ago

0.3.7

10 months ago

0.3.6

10 months ago

0.3.5

10 months ago

0.3.4

10 months ago

0.3.3

10 months ago

0.3.2

10 months ago

0.3.1

10 months ago

0.3.0

10 months ago

0.2.0

10 months ago

2.0.2

10 months ago

2.0.1

11 months ago

2.0.0

11 months ago