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

@cemiltokatli/node-data

v1.0.3

Published

Database helper library that provides classes and methods for you to query MySQL database.

Downloads

5

Readme

Node Data

node-data is a database helper library that provides classes and methods for you to query MySQL database.

Structure

Database class is the fundamental block of the library. It creates a connection pool to a database. An object of this class should be initialized per database used in the application and the same object should be used everywhere to query the same database.

getConnection() method of Database class returns a new Connection object which represents an isolated database connection grabbed from the pool. It is cheap to create a Connection object as it is just a wrapper for mysql.PoolConnection object. When you call getConnection() method, it retrieves a connection from the pool by using getConnection() method of mysql.Pool object and initializes Connection object with that actual database connection object.

Reference

Annotations

@Model

Marks a class as a database table. It is used for mapping tables to objects while fetching data from database.

@ModelField(column: string)

Marks a class property as a database field. It is used for mapping fields to class properties while fetching data from database. It takes a string parameter which determines the name of the field in a database table.

Config interface

It is the sceme of a database configuration object.

interface Config {
  host: string
  port: number
  user: string
  password: string
  database: string
}

Connection class

It represents an isolated database connection. You usually create a new connection per request in a web application.

class Connection {
	constructor(dbConnection: mysql.PoolConnection)
  public async beginTransaction(): Promise<void>
  public async call(procedureCallSql: string, selectSql: string, ...values: any[]): Promise<QueryResult>
  public async commit(): Promise<void>
  public async execute(sql: string, ...values: any[]): Promise<ExecuteResult>
  public async executeBatch(sql: string, ...values: any[]): Promise<ExecuteResult>
  public async query(sql: string, ...values: any[]): Promise<QueryResult>
  public release()
  public async rollback(): Promise<void>
}

DataConversion object

It is a single object. It just provides methods to convert given data to certain types.

const DataConversion = {
	boolean: (data: any): Nullable<boolean>,
  date: (data: any): Nullable<Date>,
  enum: <T>(model: T, data: any): Nullable<T[keyof T]>,
  number: (data: any): Nullable<number>,
  string: (data: any): Nullable<string>
}

Database class

It creates a new connection pool to a single database. It is used for getting a connection to the database.

class Database {
  constructor(config: Config)
  public async getConnection(): Promise<Connection>
}

DatabaseError class

It represents a database error.

class DatabaseError {
	constructor(code: string, errno: number, sql: string, sqlState: string, sqlMessage: string)
}

ExecuteResult class

It stores information about the result of an INSERT, UPDATE or DELETE execution.

class ExecuteResult {
	constructor(affectedRows: number, insertId: number, error: DatabaseError | null)
  get affectedRows(): number
  get error(): DatabaseError | null
  get insertId(): number
}

Field class

An object of this class represents a single field in a row, esentially represents data in a cell. It provides methods to convert data to certain types.

class Field {
	constructor(data: any)
  public toBoolean(): Nullable<boolean>
  public toDate(): Nullable<Date>
  public toEnum<T>(model: T): Nullable<T[keyof T]>
  public toNumber(): Nullable<number>
  public toString(): Nullable<string>
}

QueryResult class

It stores information about the result of a SELECT query.

class QueryResult {
	constructor(data: any[], error: DatabaseError | null)
  get error(): DatabaseError | null
  public isDataAvailable(): boolean
  public toList(model?: undefined): Row[]
  public toList<T>(model: T): T[]
  public toRecord(model?: undefined): Row | null
  public toRecord<T>(model: T): T | null
}

Row interface

It is the scheme of an object that stores data of a row created as result of a SELECT query.

Each key of this object corresponds to a column in a table.

interface Row {
  [key: string]: Field
}

Installation

You can install node-data with the following npm command:

npm i @cemiltokatli/node-data

Usage

Creating a connection pool

import Database from '@cemiltokatli/node-data/Database'

const database = new Database({
    host: 'localhost',
    port: 3306,
    user: 'root',
    password: 'root',
    database: 'mydatabase',
})

Then, you can retrieve a new database connection from the pool:

const connection = await database.getConnection()

// database operations...

connection.release()

Please check the sample project for usage examples.