mielk-api 1.3.1 → 1.3.3

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.
@@ -4,6 +4,7 @@ let connectionString = '';
4
4
  export const initDb = (dbConfig) => {
5
5
  const { host, database, user, password, driver } = dbConfig;
6
6
  connectionString = `Server=${host};Database=${database};UID=${user};PWD=${password};Driver=${driver}`;
7
+ console.log(connectionString);
7
8
  };
8
9
  export const query = (queryText) => {
9
10
  return new Promise((resolve, reject) => {
@@ -10,7 +10,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
10
10
  import { Msg } from '../../../internal/messaging/messageTags.js';
11
11
  import { Pool } from 'pg';
12
12
  import { openSshTunnel } from './tunnel.js';
13
- import { isProd } from '../../../express.js';
13
+ import { isProd } from '../../../routing/express.js';
14
14
  let pool = null;
15
15
  let config = null;
16
16
  export const initDb = (dbConfig) => {
@@ -1,6 +1,6 @@
1
1
  import { isObject, isPrimitive } from 'mielk-fn/variables';
2
2
  import { HttpResponseStatus } from '../httpResponseStatus/HttpResponseStatus.js';
3
- import { isProd } from '../../express.js';
3
+ import { isProd } from '../../routing/express.js';
4
4
  export function success(res, status, data) {
5
5
  const apiResponse = {
6
6
  success: true,
package/dist/index.d.ts CHANGED
@@ -3,4 +3,4 @@ export * as mssql from './db/mssql/index.js';
3
3
  export * as http from './http/index.js';
4
4
  export { dbRouter } from './routing/routes/db.route.js';
5
5
  export { healthRouter } from './routing/routes/health.route.js';
6
- export { createExpressApp } from './express.js';
6
+ export { createExpressApp } from './routing/express.js';
package/dist/index.js CHANGED
@@ -3,4 +3,4 @@ export * as mssql from './db/mssql/index.js';
3
3
  export * as http from './http/index.js';
4
4
  export { dbRouter } from './routing/routes/db.route.js';
5
5
  export { healthRouter } from './routing/routes/health.route.js';
6
- export { createExpressApp } from './express.js';
6
+ export { createExpressApp } from './routing/express.js';
@@ -1,4 +1,4 @@
1
- import { isProd } from "../../express.js";
1
+ import { isProd } from "../../routing/express.js";
2
2
  const corsConfig = {
3
3
  allowedOrigins: '',
4
4
  apiKey: ''
@@ -1,6 +1,6 @@
1
1
  import { HttpResponseStatus } from '../../http/httpResponseStatus/HttpResponseStatus.js';
2
2
  import { Msg } from '../../internal/messaging/messageTags.js';
3
- import { isProd } from '../../express.js';
3
+ import { isProd } from '../../routing/express.js';
4
4
  import { failure } from '../../http/index.js';
5
5
  import { apiKey } from '../cors/cors.js';
6
6
  export function apiKeyAuthorization(req, res, next) {
@@ -0,0 +1 @@
1
+ export type DbProvider = 'postgre' | 'mssql' | undefined;
@@ -0,0 +1 @@
1
+ export {};
@@ -1,5 +1,5 @@
1
1
  import { HttpResponseStatus, success } from '../../http/index.js';
2
- import { isProd } from '../../express.js';
2
+ import { isProd } from '../express.js';
3
3
  export const getHealth = (_req, res) => {
4
4
  return success(res, HttpResponseStatus.OK, new Date().toISOString());
5
5
  };
@@ -1,6 +1,8 @@
1
- import { PostgreDbConfig } from './db/pg/index.js';
2
- import { MsSqlDbConfig } from './db/mssql/index.js';
3
- import { CorsConfig, RateLimitConfig } from './middlewares/index.js';
1
+ import { PostgreDbConfig } from '../db/pg/index.js';
2
+ import { MsSqlDbConfig } from '../db/mssql/index.js';
3
+ import { CorsConfig, RateLimitConfig } from '../middlewares/index.js';
4
+ import { DbProvider } from './DbProvider.js';
4
5
  export type DbConfig = PostgreDbConfig | MsSqlDbConfig;
5
6
  export declare const createExpressApp: (isProd: boolean, dbConfig: DbConfig, corsConfig: CorsConfig, rateLimitConfig?: RateLimitConfig) => import("express-serve-static-core").Express;
6
7
  export declare const isProd: () => boolean;
8
+ export declare const getDbProvider: () => DbProvider;
@@ -1,13 +1,15 @@
1
1
  import express from 'express';
2
- import { initDb as postgreInitDb } from './db/pg/index.js';
3
- import { initDb as msSqlInitDb } from './db/mssql/index.js';
4
- import { initCors, setRateLimit } from './middlewares/index.js';
2
+ import { initDb as postgreInitDb } from '../db/pg/index.js';
3
+ import { initDb as msSqlInitDb } from '../db/mssql/index.js';
4
+ import { initCors, setRateLimit } from '../middlewares/index.js';
5
5
  const env = {
6
- isProd: true
6
+ isProd: true,
7
+ provider: undefined
7
8
  };
8
9
  export const createExpressApp = (isProd, dbConfig, corsConfig, rateLimitConfig) => {
9
10
  const app = express();
10
11
  env.isProd = isProd;
12
+ env.provider = dbConfig.provider;
11
13
  switch (dbConfig.provider) {
12
14
  case 'mssql':
13
15
  msSqlInitDb(dbConfig);
@@ -22,3 +24,4 @@ export const createExpressApp = (isProd, dbConfig, corsConfig, rateLimitConfig)
22
24
  return app;
23
25
  };
24
26
  export const isProd = () => env.isProd;
27
+ export const getDbProvider = () => env.provider;
@@ -0,0 +1,2 @@
1
+ export declare function selectDatabaseTime(): Promise<Date>;
2
+ export declare function selectDatabaseVersion(): Promise<string>;
@@ -0,0 +1,29 @@
1
+ /* eslint-disable @typescript-eslint/no-explicit-any */
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ import { query } from '../../db/mssql/connection/pool.js';
12
+ export function selectDatabaseTime() {
13
+ return __awaiter(this, void 0, void 0, function* () {
14
+ const field = 'timestamp';
15
+ const rows = yield query(`SELECT SYSDATETIME() AS [${field}]`);
16
+ const row = rows[0];
17
+ const value = row[field];
18
+ return value;
19
+ });
20
+ }
21
+ export function selectDatabaseVersion() {
22
+ return __awaiter(this, void 0, void 0, function* () {
23
+ const field = 'version';
24
+ const rows = yield query(`SELECT @@VERSION AS [${field}]`);
25
+ const row = rows[0];
26
+ const value = row[field];
27
+ return value;
28
+ });
29
+ }
@@ -7,14 +7,27 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
7
7
  step((generator = generator.apply(thisArg, _arguments || [])).next());
8
8
  });
9
9
  };
10
- import * as dbRepository from '../repositories/db.repository.js';
10
+ import * as postgreRepository from '../repositories/postgre.repository.js';
11
+ import * as msSqlRepository from '../repositories/mssql.repository.js';
12
+ import { getDbProvider } from '../express.js';
13
+ import { Msg } from '../../internal/messaging/messageTags.js';
11
14
  export function getDatabaseTime() {
12
15
  return __awaiter(this, void 0, void 0, function* () {
13
- return dbRepository.selectDatabaseTime();
16
+ const provider = getDbProvider();
17
+ const repository = provider === 'postgre' ? postgreRepository : provider === 'mssql' ? msSqlRepository : undefined;
18
+ if (repository) {
19
+ return repository.selectDatabaseTime();
20
+ }
21
+ throw new Error(Msg.connection.notInitialized);
14
22
  });
15
23
  }
16
24
  export function getDatabaseVersion() {
17
25
  return __awaiter(this, void 0, void 0, function* () {
18
- return dbRepository.selectDatabaseVersion();
26
+ const provider = getDbProvider();
27
+ const repository = provider === 'postgre' ? postgreRepository : provider === 'mssql' ? msSqlRepository : undefined;
28
+ if (repository) {
29
+ return repository.selectDatabaseVersion();
30
+ }
31
+ throw new Error(Msg.connection.notInitialized);
19
32
  });
20
33
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mielk-api",
3
- "version": "1.3.1",
3
+ "version": "1.3.3",
4
4
  "keywords": [],
5
5
  "author": "mielk",
6
6
  "description": "Wrapper for API operations",