npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@ijx/orm

v2.7.3

Published

Object-Relational mapping

Downloads

76

Readme

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