MyModels is a library for creating and managing Domain Model classes in Node.js with JSON-serialization and MySQL DAO classes support. It's simple to pick up and use, and it's designed to be flexible and extensible, while still being very lightweight and avoid boilerplate code.
npm i @translated/mymodels
Let's create our first model with its DAO; a simple User class:
import {JsonSerializable, field, MyDAO} from "@translated/mymodels"
import {Md5} from "ts-md5";
class User extends JsonSerializable {
@field() id: number;
@field() name: string;
@field(false) password: string;
@field() get passwordHash(): string {
return Md5.hashStr(this.password);
}
}
class UserDao extends MyDAO<User> {
constructor(db: MyDatabase) {
super(db, User, "users");
}
}
We have defined 4 fields in the User class:
-
id
: the unique identifier. -
name
: the name of the user. -
password
: the password of the user - note this fieldvisible
is set tofalse
, so it won't be JSON-serialized. -
passwordHash
: this is a virtual field, it's not stored in the database, but it's computed at runtime and included in the JSON.
Let's see how the JSON serialization works:
let user = new User();
user.id = 1;
user.name = "John Doe";
user.password = "s3cret";
console.log(user.json());
// {
// id: 1,
// name: 'John Doe',
// passwordHash: '33e1b232a4e6fa0028a6670753749a17'
// }
console.log(user.json({case: "snake"}));
// {
// id: 1,
// name: 'John Doe',
// password_hash: '33e1b232a4e6fa0028a6670753749a17'
// }
As you can see, the password
field is not included in the JSON, but the passwordHash
field is. Moreover, you can
further customize the JSON output by passing an options object to the json()
method; in the example above, we're using
the case: "snake"
option to convert the keys to snake_case.
Note: storing passwords in plain text is a very bad practice, and you should always salt-hash them before storing them in the database. This code is intended for demonstration purposes only.
By using the default inherited methods in the defined UserDao
class, you can already perform basic CRUD operations:
const mydb = new MyDatabase({
host: "localhost",
port: 3306,
user: "root",
password: "your_password_here",
database: "test"
});
const dao = new UserDao(mydb);
const user = new User();
user.name = "John Doe";
user.password = "s3cret";
await dao.insert(user);
console.log(user.id); // If the id is auto-generated by the database, the library will automatically update the object.
Now that we have our database populated with a user, we can retrieve it:
const dao = new UserDao(mydb);
const user = await dao.selectOne({id: 1});
console.log(user.name); // John Doe
Finally, we conclude this quickstart by showing how, exploiting the basic methods provided by the MyDAO
class, you can
create queries with more complex logic, for example:
class UserDao extends MyDAO<User> {
constructor(db: MyDatabase) {
super(db, User, "users");
}
// return all users who have been active in the last 30 days
public async getActiveUsers(days: number = 30): Promise<User[]> {
return await this.selectWhere("last_login > NOW() - INTERVAL ? DAY", [days]);
}
}