You're Invited:Meet the Socket Team at BlackHat and DEF CON in Las Vegas, Aug 4-6.RSVP
Socket
Book a DemoInstallSign in
Socket

tsbean-driver-mysql

Package Overview
Dependencies
Maintainers
1
Versions
19
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

tsbean-driver-mysql

MySQL driver for TSBean-ORM. Works with any database with support for the MySQL protocol.

3.0.1
latest
Source
npmnpm
Version published
Weekly downloads
32
-57.33%
Maintainers
1
Weekly downloads
 
Created
Source

TSBean-ORM MySQL Driver

npm version

This a MySQL driver for tsbean-orm.

Based on mysql2 package.

Installation

npm install --save tsbean-driver-mysql

Usage

import { DataSourceDriver, DataSource } from "tsbean-orm";
import { MySQLDriver } from "tsbean-driver-mysql"

const mySource = MySQLDriver.createDataSource({
    host: "localhost",
    port: 3306,
    user: "root",
    password: "",
    database: "my_database"
});

DataSource.set(DataSource.DEFAULT, mySource);

Correspondence of identifiers

This driver maps from camel case to snake case. By default, we expect all the database table and column identifiers in snake case. Those identifiers will be converted to camel case before passing to tsbean, so you can work with camel case identifiers in your code.

Here is an example:

CREATE TABLE `person` (
    `id` BIGINT NOT NULL PRIMARY KEY,
    `name` VARCHAR(255) NOT NULL COLLATE utf8_bin,
    `surname` VARCHAR(255) NOT NULL COLLATE utf8_bin,
    `age` INT,
    `has_driver_license` TINYINT(1),
    `preferences` TEXT,
    `birth_date` DATE
);
const SOURCE = DataSource.DEFAULT;
const TABLE = "person";
const PRIMARY_KEY = "id";

export class Person extends DataModel {

    public static finder = new DataFinder<Person>(
        SOURCE, // The data source
        TABLE, // The table or collection name
        PRIMARY_KEY, // The primary key. Leave blank if no primary key
        (data: GenericRow) => {
            return new Person(data);
        },
    );

    public id: number;
    public name: string;
    public surname: string;
    public age: number;
    public hasDriverLicense: boolean;
    public preferences: string[];
    public birthDate: Date;

    constructor(data: GenericRow) {
        // First, we call DataModel constructor 
        super(
            SOURCE, // The data source
            TABLE, // The table or collection name
            PRIMARY_KEY // The primary key. Leave blank if no primary key
        );

        // Second, we set the class properties
        // The recommended way is to set one by one to prevent prototype pollution
        // You can also enforce the types if you do not trust the data source
        // In that case you can use the enforceType utility function

        this.id = enforceType(data.id, "int");
        this.name = enforceType(data.name, "string");
        this.surname = enforceType(data.surname, "string");
        this.age = enforceType(data.age, "int");
        this.hasDriverLicense = enforceType(data.hasDriverLicense, "boolean");
        this.preferences = enforceType(data.preferences, "array");
        this.birthDate = enforceType(data.birthDate, "date");

        // Finally, we must call init()
        this.init();
    }
}

If you want to disable this behaviour, and instead use identifiers as-it, you can use the disableIdentifierConversion option of the data source constructor:

const mySource = MySQLDriver.createDataSource({
    host: "localhost",
    port: 3306,
    user: "root",
    password: "",
    database: "my_database",
    disableIdentifierConversion: true
});

You can also set customIdentifierConversion to implement your own identifier conversion.

FAQs

Package last updated on 12 Jul 2025

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

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.