New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@translated/mymodels

Package Overview
Dependencies
Maintainers
3
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@translated/mymodels

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

  • 1.0.0
  • unpublished
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
0
Maintainers
3
Weekly downloads
 
Created
Source

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 install mymodels

Quickstart

Let's create our first model with its DAO; a simple User class:

import {JsonSerializable, field, MyDAO} from "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]);
    }
}

Keywords

FAQs

Package last updated on 09 Apr 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc