classql
A light-weight Typescript ORM for MySQL basic CRUD that is database-model-indepedent.
What it does not do:
Run TABLE or VIEW creation queries for you.
What it does do:
Allows you to decorate your Typescript class with a MySQL TABLE or VIEW name so you can run basic CRUD operations easily.
Installation
npm install --save classql
Example Use
Say you have a MySQL TABLE or VIEW:
CREATE TABLE USER_ACCOUNTS (
id INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
email VARCHAR(45) NOT NULL,
hashedPassword VARCHAR(100) NOT NULL,
firstName VARCHAR(20) NOT NULL,
lastName VARCHAR(20) NOT NULL
)
import * as classql from 'classql';
@classql.model('USER_ACCOUNTS')
class UserAccount {
id: number;
email: string;
hashedPassword: string;
firstName: string;
lastName: string;
}
let db = new classql.Database({
host: 'localhost',
user: 'root',
password: 'root',
database: 'my_database_name'
});
let db = new classql.Database('mysql://root:root@localhost/my_database_name?debug=true&timeout=1000000');
await db.on(UserAccount).get({ id: 1 });
await db.on(UserAccount).get({ id: 1 }, ['email']);
await db.on(UserAccount)>getAll();
await db.on(UserAccount).getAll({ firstName: 'John' });
await db.on(UserAccount).getAll({ limit: 10, offset: 20 });
await db.on(UserAccount).getAll({ firstName: 'John' }, { limit: 10 });
await db.on(UserAccount).delete({ id: 5 });
let account = new UserAccount({
email: 'jd@works.io',
hashedPassword: 'hasedPassword',
firstName: 'John',
lastName: 'Doe'
});
let result = await db.on(UserAccount).save(account);
let id = result.insertId;
db.query('SELECT * FROM USER_ACCOUNTS WHERE id > 5').then(doSth).catch(doSthElse);