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

@translated/mymodels

v1.1.1

Published

MyModels is a library for creating and managing Domain Model classes in Node.js with JSON-serialization and MySQL DAO classes support.

Downloads

19

Readme

MyModels

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

Quickstart

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 field visible is set to false, 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.

JSON Serialization

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.

Interacting with the Database

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