4.3.1 • Published 8 months ago

@kmhgmbh/kmh-db-connector v4.3.1

Weekly downloads
60
License
UNLICENSED
Repository
bitbucket
Last release
8 months ago

KMH Database Connector

Simple abstraction layer for mssql.

Installation

This package supports Node v18+.

npm install --save @kmhgmbh/kmh-db-connector

Usage

import DbConnector from '@kmhgmbh/kmh-db-connector';

const conf = {
    user: 'username',
    password: 'password',
    server: 'host',
    database: 'database name',
    port: 1433,
}

const connector = new DbConnector(config);

const result = await connector.executeProcedureSafe(
    'SCHEMA.GET_USER',
    {
        FIRST_NAME: 'Martina',
        LAST_NAME: 'Mustermann',
    },
    'TEST_DATABASE',
);

Docs

class DbConnector

constructor(configuration: DbConnectorConfiguration, language: string = 'deutsch')

Creates a connector object with an underlying MSSQL connection.

Configuration fields

The DbConnectorConfiguration type picks a subset of fields from mssql.config.

NameTypeDescription
serverstringRequired - database host
portnumberTCP port to use for connection
userstringUsername
passwordstringPassword for specified user
databasestringSpecifies the database to USE when connecting to the server
connectionTimeoutnumberMaximum amount of time to connect to the server (milliseconds)
requestTimeoutnumberMaximum duration of a request to the database (milliseconds)
optionsmssql.IOptionsAdditional options. Will automatically add options.trustServerCertificate: true before connecting
poolmssql.PoolOpts<mssql.Connection>connection pool options

async tryConnect(): Promise<void>

Actively tries to connect to the configured database server. Does nothing if connection is already established.

async tryDisconnect(): Promise<void>

Actively tries to close the connection to the configured database server. Does nothing if no connection exists.

async executeQueryRequest<T>(query: string): Promise<mssql.IResult<T>>

Accepts a raw query string which will be directly executed, and returns the raw MSSQL result.

const result = await connector.executeQueryRequest<FooObj>('SELECT * FROM DATABASE.TEST.FOO');

async executeProcedureRaw<T>(name: string, parameters: string[], database: string): Promise<mssql.IResult<T>[]>

This method is deprecated and will be removed in v5.0.0. Use executeProcedureSafeRaw instead.

Constructs a stored procedure execution query from given parameters and passes it to executeQueryRequest.

const result = await connector.executeProcedureRaw<FooObj>(
    'TEST.GET_FOO',
    [
        '@FOO = bar',
    ],
    'DATABASE',
);

// result: { recordset: [...], recordsets: [...], ... }

async executeProcedure<T>(name: string, parameters: string[], database: string): Promise<T[]>

This method is deprecated and will be removed in v5.0.0. Use executeProcedureSafe instead.

Calls executeProcedureRaw and extracs the recordSet property from the IResult object as an array.

const result = await connector.executeProcedure<FooObj>(
    'TEST.GET_FOO',
    [
        '@FOO = bar',
    ],
    'DATABASE',
);

// result: [...]

async executeProcedureNormalized<T>(name: string, parameters: string[], database: string): Promise<T[]>

This method is deprecated and will be removed in v5.0.0. Use executeProcedureSafeNormalized instead.

Calls executeProcedure and normalizes the resulting array (a normalized object has its keys lowercased).

type FooObj = {
    FOO: string;
}

const result = await connector.executeProcedureNormalized<FooObj>(
    'TEST.GET_FOO',
    [
        '@FOO = bar',
    ],
    'DATABASE',
);

//result = [{ foo: 'bar' }]

async executeProcedureSafeRaw<T>(name: string, parameters: string[], database: string): Promise<mssql.IProcedureResult<T>[]>

Constructs a mssql.PreparedStatement from the given parameters and executes it.

const result = await connector.executeProcedureSafeRaw<FooObj>(
    'TEST.GET_FOO',
    { FOO: 'bar' },
    'DATABASE',
);

// result = { returnValue: ..., recordset: [...], recordsets: [...], ... }

async executeProcedureSafe<T>(name: string, parameters: string[], database: string): Promise<T[]>

Calls executeProcedureSafeRaw and extracts the recordset property from the IProcedureResult object as an array.

const result = await connector.executeProcedureSafe<FooObj>(
    'TEST.GET_FOO',
    { FOO: 'bar' },
    'DATABASE',
);

// result: [...]

async executeProcedureSafeNormalized<T>(name: string, parameters: string[], database: string): Promise<T[]>

Calls executeProcedureSafe and normalizes the resulting array (a normalized object has its keys lowercased).

type FooObj = {
    FOO: string;
}

const result = await connector.executeProcedureSafeNormalized<FooObj>(
    'TEST.GET_FOO',
    { FOO: 'bar' },
    'DATABASE',
);

//result = [{ foo: 'bar' }]
4.2.3

9 months ago

4.0.5

10 months ago

4.3.1

8 months ago

4.2.2

9 months ago

4.1.0

10 months ago

4.3.0

8 months ago

4.2.1

9 months ago

4.1.1

9 months ago

2.0.3

1 year ago

4.0.4

1 year ago

4.0.3

2 years ago

4.0.1

2 years ago

4.0.0

2 years ago

4.0.2

2 years ago

3.0.6

3 years ago

3.0.5

3 years ago

3.0.4

3 years ago

3.0.3

3 years ago

3.0.2

5 years ago

3.0.1

5 years ago

3.0.0

5 years ago

2.0.1

5 years ago

2.0.2

6 years ago