@hemia/db-connector 0.0.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,40 @@
1
+ import { AggregateOptions, ClientSession, Connection, Document, Model, PipelineStage } from "mongoose";
2
+ import { CredentialsConnection } from "../types/CredentialsConnection";
3
+ import { NoSQLOptions } from "../types/NoSQLOptions";
4
+ export declare abstract class NoSQLConnector {
5
+ protected config: CredentialsConnection;
6
+ protected provider: string;
7
+ protected connection: any;
8
+ constructor();
9
+ abstract connect(): Promise<void>;
10
+ abstract disconnect(): Promise<void>;
11
+ abstract getConnection(): Connection;
12
+ abstract isConnected(): boolean;
13
+ abstract state(): number;
14
+ abstract getCredentials(): CredentialsConnection;
15
+ abstract getProvider(): String;
16
+ abstract startSession(): Promise<ClientSession>;
17
+ abstract commitTransaction(): Promise<void>;
18
+ abstract abortTransaction(): Promise<void>;
19
+ abstract withTransaction<T>(transactionFn: (session: ClientSession) => Promise<T>): Promise<T>;
20
+ abstract create<T>(model: any, data: Partial<T>): Promise<T>;
21
+ abstract create<T extends Document>(model: Model<T>, data: Partial<T>, session?: ClientSession): Promise<T>;
22
+ abstract find<T>(model: any, query: object, options?: NoSQLOptions): Promise<T[]>;
23
+ abstract find<T extends Document>(model: Model<T>, query: object, options?: NoSQLOptions): Promise<T[]>;
24
+ abstract findOne<T>(model: any, query: object): Promise<T | null>;
25
+ abstract findOne<T extends Document>(model: Model<T>, query: object): Promise<T | null>;
26
+ abstract findById<T>(model: any, id: string): Promise<T | null>;
27
+ abstract findById<T extends Document>(model: Model<T>, id: string): Promise<T | null>;
28
+ abstract update<T>(model: any, query: object, updateData: object): Promise<T | null>;
29
+ abstract update<T extends Document>(model: Model<T>, query: object, updateData: object, session?: ClientSession): Promise<T | null>;
30
+ abstract updateById<T>(model: any, id: string, updateData: object): Promise<T | null>;
31
+ abstract updateById<T extends Document>(model: Model<T>, id: string, updateData: object, session?: ClientSession): Promise<T | null>;
32
+ abstract delete<T>(model: any, query: object): Promise<boolean>;
33
+ abstract delete<T extends Document>(model: Model<T>, query: object, session?: ClientSession): Promise<boolean>;
34
+ abstract deleteById<T>(model: any, id: string): Promise<boolean>;
35
+ abstract deleteById<T extends Document>(model: Model<T>, id: string, session?: ClientSession): Promise<boolean>;
36
+ abstract findAllFromMultipleModels<U>(models: any[], filter: object): Promise<U[]>;
37
+ abstract findAllFromMultipleModels<U extends Document>(models: Array<Model<U>>, filter: object): Promise<U[]>;
38
+ abstract aggregate<T>(model: any, pipeline: PipelineStage[]): Promise<any[]>;
39
+ abstract aggregate<T extends Document>(model: Model<T>, pipeline: PipelineStage[], options?: AggregateOptions): Promise<any[]>;
40
+ }
@@ -0,0 +1,22 @@
1
+ import { CreateOptions, CreationAttributes, DestroyOptions, FindOptions, InferAttributes, Model, QueryOptions, Sequelize, Transaction, UpdateOptions, WhereOptions } from "sequelize";
2
+ export type Filter = Record<string, any>;
3
+ export declare abstract class SqlConnector {
4
+ abstract connect(): Promise<void>;
5
+ abstract disconnect(): Promise<void>;
6
+ abstract getSequelize(): Sequelize;
7
+ abstract isConnected(): Promise<boolean>;
8
+ abstract select<T extends Model<InferAttributes<T>>, O extends FindOptions<InferAttributes<T>> = FindOptions<InferAttributes<T>>>(entity: string, options?: O & {
9
+ where?: WhereOptions<InferAttributes<T>>;
10
+ }): Promise<T[]>;
11
+ abstract insert<T extends Model<InferAttributes<T>>, O extends CreateOptions<InferAttributes<T>> = CreateOptions<InferAttributes<T>>>(entity: string, values: CreationAttributes<T>, options?: O): Promise<O extends {
12
+ returning: false;
13
+ } | {
14
+ ignoreDuplicates: true;
15
+ } ? void : T>;
16
+ abstract update<T extends Model<InferAttributes<T>>, O extends UpdateOptions<InferAttributes<T>> = UpdateOptions<InferAttributes<T>>>(entity: string, filter: WhereOptions<InferAttributes<T>>, data: Partial<InferAttributes<T>>, options?: O & {
17
+ where?: WhereOptions<InferAttributes<T>>;
18
+ }): Promise<number>;
19
+ abstract delete<T extends Model<InferAttributes<T>>, O extends DestroyOptions<InferAttributes<T>> = DestroyOptions<InferAttributes<T>>>(entity: string, filter: WhereOptions<InferAttributes<T>>, options?: O): Promise<number>;
20
+ abstract query<T>(sql: string, options?: QueryOptions): Promise<T[]>;
21
+ abstract withTransaction<T>(fn: (tx: Transaction) => Promise<T>): Promise<T>;
22
+ }
@@ -0,0 +1,8 @@
1
+ export declare enum CoreSqlTypes {
2
+ Number = "Number",
3
+ string = "string",
4
+ DateTime = "DateTime",
5
+ Decimal = "Decimal",
6
+ Boolean = "Boolean",
7
+ Float = "Float"
8
+ }
@@ -0,0 +1,4 @@
1
+ export declare enum QueryType {
2
+ STORE = "STORE",
3
+ QUERY = "QUERY"
4
+ }
@@ -0,0 +1,11 @@
1
+ export declare class DBError extends Error {
2
+ readonly code: string;
3
+ readonly sqlState?: string;
4
+ readonly errno?: number;
5
+ readonly status?: number;
6
+ constructor(code: string, message: string, meta?: {
7
+ sqlState?: string;
8
+ errno?: number;
9
+ status?: number;
10
+ });
11
+ }
@@ -0,0 +1,4 @@
1
+ export declare class MySQLConnectionError {
2
+ constructor();
3
+ static handleMysqlConnectionError(error: unknown): never;
4
+ }
@@ -0,0 +1,13 @@
1
+ export { CredentialsConnection } from './types/CredentialsConnection';
2
+ export { CoreSqlTypes } from './enums/CoreSqlTypes';
3
+ export { Recorset } from './types/RecordSet';
4
+ export { DBConnector } from "./DBConnector";
5
+ export { DBSQLType } from "./types/DBSQLTypes";
6
+ export { DBNoSQLType } from "./types/DBNoSQLTypes";
7
+ export { NoSQLConnector } from "./abstract/NoSQLConnector";
8
+ export { SqlConnector, Filter } from "./abstract/SQLConnector";
9
+ export { NoSQLOptions } from "./types/NoSQLOptions";
10
+ export { Document, Model, Schema, UpdateQuery, FilterQuery, ClientSession, PipelineStage, SortOrder, AggregateOptions } from 'mongoose';
11
+ export { Model as ModelSequelize, Sequelize, DataTypes, CreateOptions, UpdateOptions, DestroyOptions, FindOptions, Attributes, WhereOptions, QueryOptions, InferAttributes, CreationAttributes, InferCreationAttributes, QueryTypes, QueryOptionsWithType, Transaction, Op, fn } from "sequelize";
12
+ export { DBError } from "./errors/DBError";
13
+ export { MySQLConnectionError } from "./errors/MySQLConnectionError";
@@ -0,0 +1,6 @@
1
+ import { NoSQLConnector } from "../abstract/NoSQLConnector";
2
+ import { CredentialsConnection } from "../types/CredentialsConnection";
3
+ import { DBNoSQLType } from "../types/DBNoSQLTypes";
4
+ export declare class NoSQLConnectionManager {
5
+ static getInstance(type: DBNoSQLType, credentials: CredentialsConnection): NoSQLConnector;
6
+ }
@@ -0,0 +1,6 @@
1
+ import { SqlConnector } from "../abstract/SQLConnector";
2
+ import { CredentialsConnection } from "../types/CredentialsConnection";
3
+ import { DBSQLType } from "../types/DBSQLTypes";
4
+ export declare class SQLConnectionManager {
5
+ static getInstance(type: DBSQLType, credentials: CredentialsConnection): SqlConnector;
6
+ }
@@ -0,0 +1,12 @@
1
+ export interface CredentialsConnection {
2
+ host: string;
3
+ user?: string;
4
+ password?: string;
5
+ database: string;
6
+ port?: number;
7
+ timeOut?: number;
8
+ options?: object;
9
+ instance?: string;
10
+ dialect?: 'mysql' | 'postgres' | 'sqlite' | 'mariadb' | 'mssql';
11
+ logging?: boolean;
12
+ }
@@ -0,0 +1,4 @@
1
+ export declare enum DBNoSQLType {
2
+ MongoDB = "MongoDB",
3
+ Cassandra = "Cassandra"
4
+ }
@@ -0,0 +1,4 @@
1
+ export declare enum DBSQLType {
2
+ SQLServer = "SQLServer",
3
+ MYSQL = "MYSQL"
4
+ }
@@ -0,0 +1,7 @@
1
+ import { SortOrder } from "mongoose";
2
+ export interface NoSQLOptions {
3
+ limit?: number;
4
+ skip?: number;
5
+ sort?: Record<string, SortOrder>;
6
+ select?: string | string[] | Record<string, number | boolean | string | object>;
7
+ }
@@ -0,0 +1,6 @@
1
+ import { CoreSqlTypes } from '../enums/CoreSqlTypes';
2
+ export type Recorset = {
3
+ id: string;
4
+ value: unknown;
5
+ type?: CoreSqlTypes;
6
+ };
package/package.json ADDED
@@ -0,0 +1,77 @@
1
+ {
2
+ "name": "@hemia/db-connector",
3
+ "version": "0.0.1",
4
+ "description": "Hemia Database Conector",
5
+ "main": "dist/hemia-db-connector.js",
6
+ "module": "dist/hemia-db-connector.esm.js",
7
+ "types": "dist/types/index.d.ts",
8
+ "scripts": {
9
+ "clean": "rimraf dist",
10
+ "tscBuild": "rollup -c",
11
+ "build": "npm run clean && npm run tscBuild",
12
+ "coverage": "nyc npm run test",
13
+ "prepublishOnly": "npm run build",
14
+ "test": "jest --detectOpenHandles",
15
+ "test:coverage": "jest --coverage",
16
+ "test:watch": "jest --watch"
17
+ },
18
+ "nyc": {
19
+ "reporter": [
20
+ "lcov",
21
+ "text"
22
+ ],
23
+ "include": [
24
+ "src/**/*.ts"
25
+ ],
26
+ "exclude": [
27
+ "test/**/*.ts"
28
+ ]
29
+ },
30
+ "keywords": [
31
+ "hemia",
32
+ "database",
33
+ "connector",
34
+ "mongoose",
35
+ "mssql",
36
+ "mysql",
37
+ "sequelize",
38
+ "typescript"
39
+ ],
40
+ "author": "Hemia Technologies",
41
+ "license": "MIT",
42
+ "dependencies": {
43
+ "eslint": "^9.21.0",
44
+ "mongoose": "^8.8.2",
45
+ "rollup-plugin-visualizer": "^5.12.0"
46
+ },
47
+ "devDependencies": {
48
+ "@rollup/plugin-commonjs": "^26.0.1",
49
+ "@rollup/plugin-json": "^6.1.0",
50
+ "@rollup/plugin-node-resolve": "^15.2.3",
51
+ "@types/chai": "^4.3.17",
52
+ "@types/mocha": "^10.0.7",
53
+ "@types/mssql": "^9.1.5",
54
+ "@types/node": "^22.3.0",
55
+ "@typescript-eslint/eslint-plugin": "^8.5.0",
56
+ "@types/jest": "^29.5.14",
57
+ "chai": "^4.2.0",
58
+ "events": "^3.3.0",
59
+ "jest": "^29.7.0",
60
+ "ts-jest": "^29.2.5",
61
+ "mocha": "^10.7.3",
62
+ "mssql": "^11.0.1",
63
+ "mysql2": "^3.11.0",
64
+ "rimraf": "^6.0.1",
65
+ "rollup": "^4.20.0",
66
+ "rollup-plugin-typescript2": "^0.36.0",
67
+ "sequelize": "^6.37.3",
68
+ "ts-node": "^8.9.0",
69
+ "typescript": "^5.5.4"
70
+ },
71
+ "overrides": {
72
+ "glob": "10.4.2"
73
+ },
74
+ "files": [
75
+ "dist"
76
+ ]
77
+ }