2.0.1 • Published 6 months ago

darco2903-db v2.0.1

Weekly downloads
-
License
ISC
Repository
github
Last release
6 months ago

Darco2903 Database Module

Information

This module uses TypeORM to ensure the connection to the database.

The Table class is a wrapper of the TypeORM EntitySchema class.

Installation

npm install darco2903-db

Configuration

Create a config.json file that contains the following information:

{
    "type": "DB_TYPE",
    "host": "HOST_NAME",
    "port": PORT_NUMBER, // optional (default: 3306)
    "user": "DB_USERNAME",
    "password": "DB_PASSWORD", // optional
    "database": "DB_NAME",
    "synchronize": true|false // optional (default: false) - synchronize tables with database on connection
}

Usage

const { DataBase, Table } = require("darco2903-db");
const { type, host, port, user, password, database, synchronize } = require("./config.json");

const tables = [
    new Table({
        name: "sample_table",
        columns: {
            id: {
                type: "int",
                primary: true,
                generated: true,
            },
            name: {
                type: "varchar",
                length: 32,
                nullable: true,
                ...
            },
            ...
        },
    }),
];

const db = new DataBase({ type, host, port, user, password, database, tables, synchronize });

(async()=>{
    await db.connect();

    const rows = await db.find("sample_table", {
        select: ["id", "name"],
        where: {
            name: "sample",
        },
        ...
    });

    console.log(rows); // [{ id: 1, name: "sample" }, ...]

    await db.disconnect();
})();

Methods

MethodDescription
static getByKeyGet DataBase instance by key
onListen to event
onceListen to event once
offStop listening to event
connectInit connection
disconnectClose connection
getRepositoryGet repository of a table
findFind all rows in a table
findOneFind one row in a table
insertInsert a row in a table
updateUpdate a row in a table
deleteDelete a row in a table
countCount rows in a table

Attributes

AttributeDescription
static allList of all DataBase instances
isConnectedConnection status
dataSourceData source
keyDatabase key
nameDatabase name
hostHost name
portPort number
userUser name

Events

EventDescription
connectConnected to database
disconnectDisconnected from database
2.0.1

6 months ago

1.9.2

8 months ago

1.9.1

2 years ago

1.9.0

2 years ago

1.8.0

2 years ago

1.7.0

2 years ago

1.6.0

2 years ago

1.5.0

2 years ago

1.4.1

2 years ago

1.4.0

2 years ago

1.3.0

2 years ago

1.2.0

2 years ago

1.1.0

2 years ago

1.0.5

2 years ago

1.0.4

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago