dk-db
TypeScript icon, indicating that this package has built-in type declarations

0.1.3 • Public • Published

DK-db

DK database - library for working with database.

Examples

| Used code: TypeScript

db_new

Connect in server and make / update record

Ex_1

import {err_is, Err_s, err_throw} from "dk-err";
import {num_1u, num_new} from "dk-num";
import {str_make, str_new} from "dk-str";
import {Db_s, db_table, Db_table, db_table_rec, db_tables} from "./main";
import {db_new} from "./new";
 
export const main = () => {
    // make pointer DB connection (create the database manually)
 
    const db: Db_s = db_new({
        name: str_make("test"),
    }); // DB connected true - 80 ms (mysql://root:***@localhost/test)
 
    // make pointer Table
 
    const table: Db_table = db_table(db, {
        name    : str_make("table_example"),
        uniqueLs: [
            str_new(num_1u(1, 2), "unique", "unique_2"),
        ],
        fields  : [
            {
                name: str_make("unique"),
                type: str_make("integer"),
                size: num_1u(1, 4),
            }, {
                name: str_make("unique_2"),
                type: str_make("integer"),
                size: num_1u(1, 4),
            }, {
                name: str_make("data"),
                type: str_make("text"),
                size: num_new(2, 1, 20000), // varchar(20000)
            },
        ],
    });
 
    // create all tables
 
    const err: Err_s = db_tables(db);
 
    if (err !== undefined) {
        err_throw(err);
    }
 
    // record create
 
    const rec_1 = db_table_rec(table, [
        {
            name: str_make("data"),
            val : str_make("Record data"),
        }, {
            name: str_make("unique"),
            val : str_make("1"),
        }, {
            name: str_make("unique_2"),
            val : str_make("1"),
        },
    ]);
 
    // check error
 
    if (err_is(rec_1)) {
        err_throw(rec_1); // throw
    }
 
    // record update
 
    const rec_2 = db_table_rec(table, [
        {
            name: str_make("id"),
            val : str_make("1"),
        }, {
            name: str_make("data"),
            val : str_make("Record update2"),
        }, {
            name: str_make("unique"),
            val : str_make("2"),
        },
    ]);
 
    // check error
 
    if (err_is(rec_2)) {
        err_throw(rec_2);
    }
 
    // Print id records
 
    console.log(
        "rec_1.id = %s, rec_2.id = %s", rec_1.id, rec_2.id,
    ); // rec_1.id = 1, rec_2.id = 1
};
 
main();
 

Api

Types

Db

Db_o
Field Type Default Desc
type Str_s DB_CONNECT_TYPE Type server
host Str_s DB_CONNECT_HOST Server host
port Num_s DB_CONNECT_PORT Server port
name Str_s DB_CONNECT_NAME Database name
user Str_s DB_CONNECT_USER Database user
password Str_s DB_CONNECT_PASSWORD Database password
timeout Num_s DB_CONNECT_TIMEOUT Max connection timeout
findRecords Num_s DB_FIND_LIMIT Limit list
findTimeout Time_s DB_FIND_TIMEOUT Max get find timeout
recTimeout Time_s DB_REC_TIMEOUT Max get record timeout
tableTimeout Time_s DB_TABLE_TIMEOUT Max timeout creating table
Db_s
Field Type Value
type Str_s Db_o.type
host Str_s Db_o.host
port Num_s Db_o.port
name Str_s Db_o.name
user Str_s Db_o.user
password Str_s Db_o.password
timeout Time_s Db_o.timeout
findRecords Num_s Db_o.findRecords
findTimeout Time_s Db_o.findTimeout
recTimeout Time_s Db_o.recTimeout
tableTimeout Time_s Db_o.tableTimeout
orm any Db_o.orm

Table

Db_table_opt
Field Type Desc
name Str_s Table name
fields Db_fields Fields list
uniqueLs Db_table_ls_unique Unique list (name field)
Db_table
Field Type Desc
db Db_s Pointer on DB
orm any Orm table object
name Str_s Db_table_opt.name
fields Db_fields Db_table_opt.fields
uniqueLs Str_s Db_table_opt.uniqueLs
Db_table_ls_unique => Array<Str_s>

List unique group (Str_s)

Db_tables => Array<Db_table>

List Db_table

Field

Db_field
Field Type Desc
name Str_s Name
type Str_s Type: integer, text
size Num_s Size 1-65555
val Str_s Value
Db_fields => Array<Db_field>

Constants

Default library

Name Type Value Desc
DB_NAME Str_std DK-db Name library
DB_KEY Str_std db Key library
DB_ID Any_id 0 Id library
DB_VER Str_std 0.1.3 Version library

Connection default

Name Type Value Desc
DB_CONNECT_TYPE Str_std mysql Server type
DB_CONNECT_HOST Str_std localhost Server host
DB_CONNECT_PORT Num_std 3306 Server port
DB_CONNECT_USER Str_std root User name
DB_CONNECT_NAME Str_std db Database name
DB_CONNECT_PASSWORD Str_std "" User password
DB_CONNECT_TIMEOUT Num_std 10 Timeout connection

Record default

Name Type Value Desc
DB_REC_TIMEOUT Num_std 3 Timeout get record

Find default

Name Type Value Desc
DB_FIND_TIMEOUT Num_std 5 Timeout find
DB_FIND_LIMIT Num_std 100 Limit list

Fields

Name Type Value Desc
DB_FIELD_ID Str_std id Primary (AUTO_INCREMENT)
DB_FIELD_CREATED Str_std _created Created record
DB_FIELD_UPDATED Str_std _updated Updated record

Table

Name Type Value Desc
DB_TABLE_FIELD_CREATED boolean true Make field "_created"
DB_TABLE_FIELD_UPDATED boolean true Make field "_updated"

Functions

db_new(opt: Db_o): Db_s

Create new structure Db_s and connection with server

db_table(db: Db_s, opt: Db_table_opt): Db_table

Create new table model by fields

db_tables(db: Db_s):void

Create tables if they do not exist

db_table_rec(table: Db_table, data: Db_fields)

Get / make record in table

db_table_find(table: Db_table, filter: any, offset: Num_s):Array

Search records in table

| filter - orm filter (where)

About

Notes

License

MIT

Readme

Keywords

Package Sidebar

Install

npm i dk-db

Weekly Downloads

1

Version

0.1.3

License

MIT

Unpacked Size

26.6 kB

Total Files

17

Last publish

Collaborators

  • denkar