0.10.3 • Published 8 months ago

@e-mc/db v0.10.3

Weekly downloads
-
License
BSD-3-Clause
Repository
github
Last release
8 months ago

@e-mc/db

  • NodeJS 16
  • ES2020

General Usage

Interface

import type { DbDataSource } from "./squared";

import type { IHost } from "./index";
import type { ClientDbConstructor, IClientDb } from "./core";
import type { BatchQueryResult, DB_TYPE, ErrorQueryCallback, ExecuteBatchQueryOptions, ExecuteQueryOptions, HandleFailOptions, ProcessRowsOptions, QueryResult, SQL_COMMAND } from "./db";
import type { AuthValue } from "./http";
import type { DbCoerceSettings, DbModule, DbSourceOptions, PoolConfig } from "./settings";

import type { SecureContextOptions } from "tls";

interface IDb extends IClientDb<IHost, DbModule, DbDataSource, DbSourceOptions, DbCoerceSettings> {
    setCredential(item: DbDataSource): Promise<void>;
    getCredential(item: DbDataSource): Record<string | number | symbol, unknown>;
    hasSource(source: string, ...type: number[]): boolean;
    applyCommand(...items: DbDataSource[]): void;
    executeQuery(item: V, callback: ErrorQueryCallback): Promise<QueryResult>;
    executeQuery(item: DbDataSource, sessionKey: string): Promise<QueryResult>;
    executeQuery(item: DbDataSource, options?: ExecuteQueryOptions | string): Promise<QueryResult>;
    executeBatchQuery(batch: DbDataSource[], callback: ErrorQueryCallback, outResult?: BatchQueryResult): Promise<BatchQueryResult>;
    executeBatchQuery(batch: DbDataSource[], sessionKey: string, outResult?: BatchQueryResult): Promise<BatchQueryResult>;
    executeBatchQuery(batch: DbDataSource[], options?: ExecuteBatchQueryOptions | string, outResult?: BatchQueryResult): Promise<BatchQueryResult>;
    processRows(batch: DbDataSource[], tasks: Promise<QueryResult | null>[], parallel: boolean): Promise<BatchQueryResult>;
    processRows(batch: DbDataSource[], tasks: Promise<QueryResult | null>[], options?: ProcessRowsOptions, outResult?: BatchQueryResult): Promise<BatchQueryResult>;
    handleFail(err: unknown, item: DbDataSource, options?: HandleFailOptions): boolean;
    readTLSCert(value: unknown, cache?: boolean): string;
    readTLSConfig(options: SecureContextOptions, cache?: boolean): void;
    settingsOf(source: string, name: keyof Omit<DbSourceOptions, "coerce">): unknown;
    settingsOf(source: string, name: "coerce", component: keyof DbCoerceSettings): unknown;
    settingsKey(source: string, name: keyof Omit<DbSourceOptions, "coerce">): unknown;
    settingsKey(source: string, name: "coerce", component: keyof DbCoerceSettings): unknown;
    getPoolConfig(source: string, uuidKey?: string): Required<PoolConfig> | undefined;
    get sourceType(): DB_TYPE;
    get commandType(): SQL_COMMAND;
}

interface DbConstructor extends ClientDbConstructor<IHost> {
    setPoolConfig(value: Record<string, PoolConfig>): void;
    getPoolConfig(source: string): Required<PoolConfig> | undefined;
    readonly prototype: IDb;
    new(module?: DbModule, database?: DbDataSource[], ...args: unknown[]): IDb;
}

interface IDbSourceClient {
    DB_SOURCE_NAME: string;
    DB_SOURCE_CLIENT: boolean;
    DB_SOURCE_TYPE: number;
    setCredential(this: IDb, item: DbDataSource): Promise<void>;
    executeQuery(this: IDb, item: DbDataSource, options?: ExecuteQueryOptions | string): Promise<QueryResult>;
    executeBatchQuery(this: IDb, batch: DbDataSource[], options?: ExecuteBatchQueryOptions | string, outResult?: BatchQueryResult): Promise<BatchQueryResult>;
    checkTimeout?(this: IDbSourceClient, value: number, limit?: number): Promise<number>;
}

interface IDbPool {
    client: unknown;
    lastAccessed: number;
    success: number;
    failed: number;
    poolKey: string;
    uuidKey: AuthValue | null;
    add(item: DbDataSource, uuidKey?: string): this;
    has(item: DbDataSource): boolean;
    getConnection(credential?: unknown): Promise<unknown>;
    remove(): void;
    detach(force?: boolean): Promise<void>;
    close(): Promise<void>;
    isIdle(timeout: number): boolean;
    isEmpty(): boolean;
    set connected(value: boolean);
    get persist(): boolean;
    get closed(): boolean;
    get closeable(): boolean;
    set parent(value: Record<string, IDbPool>);
}

interface DbPoolConstructor {
    CACHE_UNUSED: readonly string[];
    asString(credential: unknown): string;
    sanitize(credential: unknown): unknown;
    removeUUIDKey(credential: unknown): unknown;
    findKey(pools: Record<string, IDbPool>, uuidKey: unknown, poolKey: string | undefined, ...items: DbDataSource[]): Record<string, IDbPool> | null;
    validateKey(pools: Record<string, IDbPool>, username: string, uuidKey: unknown): [string, Record<string, IDbPool> | null];
    checkTimeout(pools: Record<string, IDbPool>, value: number, limit?: number): Promise<number>;
    readonly prototype: IDbPool;
    new(pool: unknown, poolKey: string, uuidKey?: AuthValue | null): IDbPool;
}

Settings

import type { DbSourceOptions, PurgeComponent } from "./settings";

interface DbModule {
    // handler: "@e-mc/db";
    mariadb?: DbStoredCredentials;
    mongodb?: DbStoredCredentials;
    mssql?: DbStoredCredentials;
    mysql?: DbStoredCredentials;
    oracle?: DbStoredCredentials;
    postgres?: DbStoredCredentials;
    redis?: DbStoredCredentials;
    settings?: {
        broadcast_id?: string | string[];
        users?: Record<string, Record<string, unknown>>;
        cache_dir?: string;
        session_expires?: number;
        user_key?: Record<string, DbSourceOptions>;
        imports?: StringMap;
        purge?: PurgeComponent;
        mariadb?: DbSourceOptions;
        mongodb?: DbSourceOptions;
        mssql?: DbSourceOptions;
        mysql?: DbSourceOptions;
        oracle?: DbSourceOptions;
        postgres?: DbSourceOptions;
        redis?: DbSourceOptions;
    };
}

type DbStoredCredentials = Record<string, Record<string, unknown>>;

Example usage

const Db = require("@e-mc/db"); // Using @pi-r/mongodb

const instance = new Db({
    mongodb: {
        main: {
            server: "localhost:27017",
            auth: {
                username: "**********",
                password: "**********"
            },
            authMechanism: "SCRAM-SHA-1"
        }
    },
    settings: {
        mongodb: {
            pool: {
                max: 10,
                idle: 60 * 1000,
                queue_max: 4,
                queue_idle: 30 * 1000,
                timeout: 10 * 1000
            },
            cache: {
                timeout: "1d",
                when_empty: false
            },
            coerce: {
                credential: false,
                options: true
            }
        }
    }
});
// instance.host = new Host();
instance.init();

const item = {
    source: "mongodb",
    credential: "main",
    table: "demo",
    name: "nodejs",
    query: {
        id: {
            "$eq": "1"
        }
    },
    willAbort: true
};
await instance.setCredential(item);

const rows = await instance.executeQuery(item, (err, item) => {
    if (err.code === "E11000") {
        return true; // throw err;
    }
    return false; // return [];
});

const [rows1, rows2] = await instance.executeBatchQuery([
        { ...item, usePool: true },
        { ...item, query: { id: { "$eq": "2" } } }
    ],
    { parallel: true, connectOnce: true }
);

References

LICENSE

BSD 3-Clause

0.5.10

11 months ago

0.5.11

11 months ago

0.5.16

8 months ago

0.5.14

9 months ago

0.5.15

9 months ago

0.5.12

10 months ago

0.5.13

9 months ago

0.9.8

10 months ago

0.7.11

11 months ago

0.9.7

11 months ago

0.7.10

11 months ago

0.7.13

10 months ago

0.9.9

10 months ago

0.7.12

10 months ago

0.9.4

12 months ago

0.5.8

12 months ago

0.9.3

1 year ago

0.5.7

1 year ago

0.9.6

11 months ago

0.5.9

11 months ago

0.9.5

12 months ago

0.7.15

9 months ago

0.7.14

9 months ago

0.7.17

8 months ago

0.7.16

9 months ago

0.10.1

10 months ago

0.10.2

9 months ago

0.10.3

8 months ago

0.10.0

11 months ago

0.9.12

9 months ago

0.9.13

8 months ago

0.6.7

11 months ago

0.6.6

11 months ago

0.9.10

9 months ago

0.6.9

10 months ago

0.9.11

9 months ago

0.6.8

11 months ago

0.6.10

10 months ago

0.6.12

9 months ago

0.6.11

9 months ago

0.6.14

8 months ago

0.6.13

9 months ago

0.9.2

1 year ago

0.7.9

11 months ago

0.8.21

8 months ago

0.7.8

12 months ago

0.8.20

9 months ago

0.7.7

1 year ago

0.8.12

12 months ago

0.8.11

1 year ago

0.8.14

11 months ago

0.8.13

11 months ago

0.8.19

9 months ago

0.8.16

10 months ago

0.8.15

11 months ago

0.8.18

9 months ago

0.8.17

10 months ago

0.6.5

12 months ago

0.6.4

1 year ago

0.8.9

1 year ago

0.8.8

1 year ago

0.7.6

1 year ago

0.7.5

1 year ago

0.8.10

1 year ago

0.6.3

1 year ago

0.7.4

1 year ago

0.5.6

1 year ago

0.9.1

1 year ago

0.9.0

1 year ago

0.8.7

1 year ago

0.6.2

1 year ago

0.7.3

1 year ago

0.5.5

1 year ago

0.8.6

1 year ago

0.7.2

1 year ago

0.5.4

1 year ago

0.6.1

1 year ago

0.8.5

1 year ago

0.8.4

1 year ago

0.8.3

1 year ago

0.8.2

1 year ago

0.8.1

1 year ago

0.8.0

1 year ago

0.7.1

2 years ago

0.7.0

2 years ago

0.6.0

2 years ago

0.5.3

2 years ago

0.5.2

2 years ago

0.5.1

2 years ago

0.5.0

2 years ago

0.4.2

2 years ago

0.4.1

2 years ago

0.4.0

2 years ago

0.3.3

2 years ago

0.3.2

2 years ago

0.3.1

2 years ago

0.3.0

2 years ago

0.2.0

2 years ago

0.1.0

2 years ago

0.0.4

2 years ago

0.0.3

2 years ago

0.0.2

2 years ago

0.0.1

2 years ago