mielk-api 1.2.2 → 1.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,5 @@
1
+ import { RawData } from 'msnodesqlv8/types';
2
+ import { MsSqlDbConfig } from '../types/DbConfig.js';
3
+ export declare const initDb: (dbConfig: MsSqlDbConfig) => void;
4
+ export declare const query: (queryText: string) => Promise<any[]>;
5
+ export declare const spExecute: (sqlText: string, params: MsNodeSqlV8.sqlQueryParamType[]) => Promise<RawData | undefined>;
@@ -0,0 +1,33 @@
1
+ /* eslint-disable @typescript-eslint/no-explicit-any */
2
+ import sql from 'msnodesqlv8';
3
+ let connectionString = '';
4
+ export const initDb = (dbConfig) => {
5
+ const { host, database, user, password, driver } = dbConfig;
6
+ connectionString = `Server=${host};Database=${database};UID=${user};PWD=${password};Driver=${driver}`;
7
+ };
8
+ export const query = (queryText) => {
9
+ return new Promise((resolve, reject) => {
10
+ sql.query(connectionString, queryText, (err, rows) => {
11
+ if (err)
12
+ reject(err);
13
+ if (rows === undefined)
14
+ reject();
15
+ else
16
+ resolve(rows);
17
+ });
18
+ });
19
+ };
20
+ export const spExecute = (sqlText, params) => {
21
+ return new Promise((resolve, reject) => {
22
+ sql.open(connectionString, (err, conn) => {
23
+ if (err)
24
+ return reject(err);
25
+ conn.queryRaw(sqlText, params, (err, results) => {
26
+ if (err)
27
+ reject(err);
28
+ else
29
+ resolve(results);
30
+ });
31
+ });
32
+ });
33
+ };
@@ -1 +1,4 @@
1
- export {};
1
+ import { initDb, query, spExecute } from './connection/pool.js';
2
+ import { MsSqlDbConfig } from './types/DbConfig.js';
3
+ export { initDb, query, spExecute };
4
+ export { MsSqlDbConfig };
@@ -1 +1,2 @@
1
- export {};
1
+ import { initDb, query, spExecute } from './connection/pool.js';
2
+ export { initDb, query, spExecute };
@@ -0,0 +1,8 @@
1
+ export interface MsSqlDbConfig {
2
+ provider: 'mssql';
3
+ host: string;
4
+ database: string;
5
+ user: string;
6
+ password: string;
7
+ driver: string;
8
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -1,4 +1,4 @@
1
1
  import { Pool } from 'pg';
2
- import { DbConfig } from '../types/DbConfig.js';
3
- export declare const initDb: (dbConfig: DbConfig) => void;
2
+ import { PostgreDbConfig } from '../types/DbConfig.js';
3
+ export declare const initDb: (dbConfig: PostgreDbConfig) => void;
4
4
  export declare function getPool(): Promise<Pool>;
@@ -1,6 +1,6 @@
1
1
  import { POSTGRE_ERRORS } from './static/PostgreErrorCodes.js';
2
2
  import { initDb, getPool } from './connection/pool.js';
3
- import { DbConfig } from './types/DbConfig.js';
3
+ import { PostgreDbConfig } from './types/DbConfig.js';
4
4
  export { POSTGRE_ERRORS };
5
5
  export { initDb, getPool };
6
- export { DbConfig };
6
+ export { PostgreDbConfig };
@@ -1,5 +1,6 @@
1
1
  import { SshOptions } from "tunnel-ssh";
2
- export interface DbConfig {
2
+ export interface PostgreDbConfig {
3
+ provider: 'postgre';
3
4
  host: string;
4
5
  port: number;
5
6
  user: string;
package/dist/express.d.ts CHANGED
@@ -1,4 +1,6 @@
1
- import { DbConfig } from './db/pg/index.js';
1
+ import { PostgreDbConfig } from './db/pg/index.js';
2
+ import { MsSqlDbConfig } from './db/mssql/index.js';
2
3
  import { CorsConfig, RateLimitConfig } from './middlewares/index.js';
4
+ export type DbConfig = PostgreDbConfig | MsSqlDbConfig;
3
5
  export declare const createExpressApp: (isProd: boolean, dbConfig: DbConfig, corsConfig: CorsConfig, rateLimitConfig?: RateLimitConfig) => import("express-serve-static-core").Express;
4
6
  export declare const isProd: () => boolean;
package/dist/express.js CHANGED
@@ -1,5 +1,6 @@
1
1
  import express from 'express';
2
- import { initDb } from './db/pg/index.js';
2
+ import { initDb as postgreInitDb } from './db/pg/index.js';
3
+ import { initDb as msSqlInitDb } from './db/mssql/index.js';
3
4
  import { initCors, setRateLimit } from './middlewares/index.js';
4
5
  const env = {
5
6
  isProd: true
@@ -7,8 +8,14 @@ const env = {
7
8
  export const createExpressApp = (isProd, dbConfig, corsConfig, rateLimitConfig) => {
8
9
  const app = express();
9
10
  env.isProd = isProd;
10
- console.log();
11
- initDb(dbConfig);
11
+ switch (dbConfig.provider) {
12
+ case 'mssql':
13
+ msSqlInitDb(dbConfig);
14
+ break;
15
+ case 'postgre':
16
+ postgreInitDb(dbConfig);
17
+ break;
18
+ }
12
19
  initCors(corsConfig);
13
20
  setRateLimit(app, rateLimitConfig);
14
21
  app.use(express.json());
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mielk-api",
3
- "version": "1.2.2",
3
+ "version": "1.3.1",
4
4
  "keywords": [],
5
5
  "author": "mielk",
6
6
  "description": "Wrapper for API operations",
@@ -77,6 +77,7 @@
77
77
  "dependencies": {
78
78
  "cors": "^2.8.6",
79
79
  "mielk-fn": "^1.1.1",
80
+ "msnodesqlv8": "^5.1.3",
80
81
  "pg": "^8.20.0",
81
82
  "tunnel-ssh": "^5.2.0"
82
83
  }