mrepo-sql 1.0.0 → 1.0.2

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.
package/dist/db/repo.d.ts CHANGED
@@ -2,43 +2,60 @@ import mariadb, { type PoolConfig } from "mariadb";
2
2
  import { ExecSPOptions } from "../interfaces/db.interface.js";
3
3
  export declare class BaseRepository {
4
4
  private pool;
5
+ /**
6
+ * Creates a new BaseRepository instance.
7
+ * @param {PoolConfig} config - Connection configuration for the MariaDB pool.
8
+ */
5
9
  constructor(config: PoolConfig);
6
10
  /**
7
11
  * Executes a stored procedure with the given parameters and handling options.
8
- * @OPTIONS {params: any[] | Record<string, any>, out: OutVariable[], plain: boolean, extract: boolean, order: string[], deleteExtra: boolean, fillWithNulls: boolean}
9
- * @param spName The name of the stored procedure to call.
10
- * @param options The options for the stored procedure execution.
11
- * @returns The result of the stored procedure execution, typed as T.
12
+ *
13
+ * @param {string} spName - The name of the stored procedure to call.
14
+ * @param {ExecSPOptions} options - Execution and result handling options.
15
+ * @param {any[] | Record<string, any>} [options.params=[]] - Parameters for the SP.
16
+ * @param {OutVariable[]} [options.out=[]] - Output variables to handle.
17
+ * @param {boolean} [options.plain=true] - Whether to return a single object (true) or an array of results.
18
+ * @param {boolean} [options.extract=false] - Whether to extract parameters from an object based on `order`.
19
+ * @param {string[]} [options.order=[]] - The order of keys for parameter extraction.
20
+ * @param {boolean} [options.deleteExtra=false] - Whether to remove keys not present in `order`.
21
+ * @param {boolean} [options.fillWithNulls=false] - Whether to fill missing ordered parameters with null.
22
+ * @returns {Promise<T>} The result of the stored procedure execution.
12
23
  */
13
24
  execSP<T = any>(spName: string, { params, out, plain, extract, order, deleteExtra, fillWithNulls, }: ExecSPOptions): Promise<T>;
14
25
  /**
15
- * Retrieves all records from a view.
26
+ * Retrieves all records from a database view.
16
27
  *
17
- * @param viewName - The name of the view to query.
18
- * @returns The result of the query.
28
+ * @param {string} viewName - The name of the view to query.
29
+ * @returns {Promise<T[]>} An array containing the records from the view.
19
30
  */
20
31
  view<T = any>(viewName: string): Promise<T[]>;
21
32
  /**
22
- * Executes a query with the given parameters.
33
+ * Executes a raw SQL query with optional parameters.
23
34
  *
24
- * @param query - The SQL query to execute.
25
- * @param params - The parameters to pass to the query.
26
- * @returns The result of the query.
35
+ * @param {string} query - The SQL query to execute.
36
+ * @param {any[]} [params] - The parameters to safely bind to the query.
37
+ * @returns {Promise<T[]>} The result set from the query.
27
38
  */
28
39
  query<T = any>(query: string, params?: any[]): Promise<T[]>;
29
40
  /**
30
- * Connects to the database.
41
+ * Tests the database connection and invokes callbacks based on the result.
31
42
  *
32
- * @param onSuccess - Callback function to execute on success.
33
- * @param onError - Callback function to execute on error.
43
+ * @param {function} [onSuccess] - Callback function to execute if the connection is successful.
44
+ * @param {function} [onError] - Callback function to execute if the connection fails.
45
+ * @throws {Error} If the connection fails and no onError callback is provided.
34
46
  */
35
47
  connectDB(onSuccess?: () => void, onError?: (error: Error) => void): Promise<void>;
36
48
  /**
37
- * Closes the connection pool.
49
+ * Closes the connection pool and all active connections.
38
50
  *
39
- * @param onSuccess - Callback function to execute on success.
40
- * @param onError - Callback function to execute on error.
51
+ * @param {function} [onSuccess] - Callback function to execute after the pool is closed.
52
+ * @param {function} [onError] - Callback function to execute if closing the pool fails.
53
+ * @returns {Promise<void>}
41
54
  */
42
55
  closeDB(onSuccess?: () => void, onError?: (error: Error) => void): Promise<void>;
56
+ /**
57
+ * Returns the underlying MariaDB connection pool.
58
+ * @returns {Promise<mariadb.Pool>} The MariaDB pool instance.
59
+ */
43
60
  getOriginalPool(): Promise<mariadb.Pool>;
44
61
  }
package/dist/db/repo.js CHANGED
@@ -4,6 +4,10 @@ const defaultConfig = {
4
4
  multipleStatements: true,
5
5
  };
6
6
  export class BaseRepository {
7
+ /**
8
+ * Creates a new BaseRepository instance.
9
+ * @param {PoolConfig} config - Connection configuration for the MariaDB pool.
10
+ */
7
11
  constructor(config) {
8
12
  this.pool = mariadb.createPool({
9
13
  ...defaultConfig,
@@ -12,10 +16,17 @@ export class BaseRepository {
12
16
  }
13
17
  /**
14
18
  * Executes a stored procedure with the given parameters and handling options.
15
- * @OPTIONS {params: any[] | Record<string, any>, out: OutVariable[], plain: boolean, extract: boolean, order: string[], deleteExtra: boolean, fillWithNulls: boolean}
16
- * @param spName The name of the stored procedure to call.
17
- * @param options The options for the stored procedure execution.
18
- * @returns The result of the stored procedure execution, typed as T.
19
+ *
20
+ * @param {string} spName - The name of the stored procedure to call.
21
+ * @param {ExecSPOptions} options - Execution and result handling options.
22
+ * @param {any[] | Record<string, any>} [options.params=[]] - Parameters for the SP.
23
+ * @param {OutVariable[]} [options.out=[]] - Output variables to handle.
24
+ * @param {boolean} [options.plain=true] - Whether to return a single object (true) or an array of results.
25
+ * @param {boolean} [options.extract=false] - Whether to extract parameters from an object based on `order`.
26
+ * @param {string[]} [options.order=[]] - The order of keys for parameter extraction.
27
+ * @param {boolean} [options.deleteExtra=false] - Whether to remove keys not present in `order`.
28
+ * @param {boolean} [options.fillWithNulls=false] - Whether to fill missing ordered parameters with null.
29
+ * @returns {Promise<T>} The result of the stored procedure execution.
19
30
  */
20
31
  async execSP(spName, { params = [], out = [], plain = true, extract = false, order = [], deleteExtra = false, fillWithNulls = false, }) {
21
32
  const endParams = extract && !Array.isArray(params) && params !== null
@@ -30,7 +41,7 @@ export class BaseRepository {
30
41
  try {
31
42
  conn = await this.pool.getConnection();
32
43
  const declareOutVars = out.length
33
- ? out.map(({ name, type }) => `SET @${name} = CAST(NULL AS ${type ?? 'VARCHAR(255)'});`).join('\n')
44
+ ? out.map(({ name, type }) => `SET ${name} = CAST(NULL AS ${type ?? 'VARCHAR(255)'});`).join('\n')
34
45
  : '';
35
46
  const selectOuts = out.length
36
47
  ? 'SELECT ' + out
@@ -54,10 +65,10 @@ export class BaseRepository {
54
65
  }
55
66
  }
56
67
  /**
57
- * Retrieves all records from a view.
68
+ * Retrieves all records from a database view.
58
69
  *
59
- * @param viewName - The name of the view to query.
60
- * @returns The result of the query.
70
+ * @param {string} viewName - The name of the view to query.
71
+ * @returns {Promise<T[]>} An array containing the records from the view.
61
72
  */
62
73
  async view(viewName) {
63
74
  let conn;
@@ -75,11 +86,11 @@ export class BaseRepository {
75
86
  }
76
87
  }
77
88
  /**
78
- * Executes a query with the given parameters.
89
+ * Executes a raw SQL query with optional parameters.
79
90
  *
80
- * @param query - The SQL query to execute.
81
- * @param params - The parameters to pass to the query.
82
- * @returns The result of the query.
91
+ * @param {string} query - The SQL query to execute.
92
+ * @param {any[]} [params] - The parameters to safely bind to the query.
93
+ * @returns {Promise<T[]>} The result set from the query.
83
94
  */
84
95
  async query(query, params) {
85
96
  let conn;
@@ -97,10 +108,11 @@ export class BaseRepository {
97
108
  }
98
109
  }
99
110
  /**
100
- * Connects to the database.
111
+ * Tests the database connection and invokes callbacks based on the result.
101
112
  *
102
- * @param onSuccess - Callback function to execute on success.
103
- * @param onError - Callback function to execute on error.
113
+ * @param {function} [onSuccess] - Callback function to execute if the connection is successful.
114
+ * @param {function} [onError] - Callback function to execute if the connection fails.
115
+ * @throws {Error} If the connection fails and no onError callback is provided.
104
116
  */
105
117
  async connectDB(onSuccess, onError) {
106
118
  try {
@@ -114,10 +126,11 @@ export class BaseRepository {
114
126
  }
115
127
  }
116
128
  /**
117
- * Closes the connection pool.
129
+ * Closes the connection pool and all active connections.
118
130
  *
119
- * @param onSuccess - Callback function to execute on success.
120
- * @param onError - Callback function to execute on error.
131
+ * @param {function} [onSuccess] - Callback function to execute after the pool is closed.
132
+ * @param {function} [onError] - Callback function to execute if closing the pool fails.
133
+ * @returns {Promise<void>}
121
134
  */
122
135
  async closeDB(onSuccess, onError) {
123
136
  return this.pool.end().then(() => {
@@ -127,6 +140,10 @@ export class BaseRepository {
127
140
  throw error;
128
141
  });
129
142
  }
143
+ /**
144
+ * Returns the underlying MariaDB connection pool.
145
+ * @returns {Promise<mariadb.Pool>} The MariaDB pool instance.
146
+ */
130
147
  async getOriginalPool() {
131
148
  return this.pool;
132
149
  }
@@ -1,20 +1,48 @@
1
+ /**
2
+ * Represents an output variable for a stored procedure.
3
+ */
1
4
  export interface OutVariable {
5
+ /** The name of the variable (without the @ symbol). */
2
6
  name: string;
7
+ /** The SQL type of the variable (e.g., 'VARCHAR(255)', 'INT'). Defaults to 'VARCHAR(255)'. */
3
8
  type?: string;
4
9
  }
10
+ /**
11
+ * Options for executing a stored procedure.
12
+ */
5
13
  export interface ExecSPOptions {
6
- /** Parameters to pass to the SP. Can be an array or an object if `extract` is true. */
14
+ /**
15
+ * Parameters to pass to the stored procedure.
16
+ * Can be an array of values or an object (if `extract` is set to true).
17
+ */
7
18
  params?: any[] | Record<string, any>;
8
- /** Definition of output variables. */
19
+ /**
20
+ * Definitions of output variables to be captured after execution.
21
+ */
9
22
  out?: OutVariable[];
10
- /** If true, flattens the result into a single object. */
23
+ /**
24
+ * If true, flattens the result into a single object when the SP returns a single row.
25
+ * @default true
26
+ */
11
27
  plain?: boolean;
12
- /** If true, extracts parameters from an object based on the `order` array. */
28
+ /**
29
+ * If true, extracts parameters from an object based on the `order` array.
30
+ * Useful when you want to pass an object but the SP expects positional parameters.
31
+ * @default false
32
+ */
13
33
  extract?: boolean;
14
- /** The order of keys to extract from `params`. */
34
+ /**
35
+ * The order of keys to extract from `params` when `extract` is true.
36
+ */
15
37
  order?: string[];
16
- /** If true, deletes extra parameters not specified in `order`. */
38
+ /**
39
+ * If true, deletes keys from the `params` object that are not specified in the `order` array.
40
+ * @default false
41
+ */
17
42
  deleteExtra?: boolean;
18
- /** If true, fills missing ordered parameters with null. */
43
+ /**
44
+ * If true, fills missing parameters defined in `order` with `null` if they are not in `params`.
45
+ * @default false
46
+ */
19
47
  fillWithNulls?: boolean;
20
48
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mrepo-sql",
3
- "version": "1.0.0",
3
+ "version": "1.0.2",
4
4
  "description": "With mrepo-sql, the ability to create repositories with features that improve code readability and project scalability is added.",
5
5
  "license": "MIT",
6
6
  "author": "daniel_hz",
package/src/db/repo.ts CHANGED
@@ -9,6 +9,11 @@ const defaultConfig = {
9
9
 
10
10
  export class BaseRepository {
11
11
  private pool: mariadb.Pool;
12
+
13
+ /**
14
+ * Creates a new BaseRepository instance.
15
+ * @param {PoolConfig} config - Connection configuration for the MariaDB pool.
16
+ */
12
17
  constructor(config: PoolConfig) {
13
18
  this.pool = mariadb.createPool(
14
19
  {
@@ -20,10 +25,17 @@ export class BaseRepository {
20
25
 
21
26
  /**
22
27
  * Executes a stored procedure with the given parameters and handling options.
23
- * @OPTIONS {params: any[] | Record<string, any>, out: OutVariable[], plain: boolean, extract: boolean, order: string[], deleteExtra: boolean, fillWithNulls: boolean}
24
- * @param spName The name of the stored procedure to call.
25
- * @param options The options for the stored procedure execution.
26
- * @returns The result of the stored procedure execution, typed as T.
28
+ *
29
+ * @param {string} spName - The name of the stored procedure to call.
30
+ * @param {ExecSPOptions} options - Execution and result handling options.
31
+ * @param {any[] | Record<string, any>} [options.params=[]] - Parameters for the SP.
32
+ * @param {OutVariable[]} [options.out=[]] - Output variables to handle.
33
+ * @param {boolean} [options.plain=true] - Whether to return a single object (true) or an array of results.
34
+ * @param {boolean} [options.extract=false] - Whether to extract parameters from an object based on `order`.
35
+ * @param {string[]} [options.order=[]] - The order of keys for parameter extraction.
36
+ * @param {boolean} [options.deleteExtra=false] - Whether to remove keys not present in `order`.
37
+ * @param {boolean} [options.fillWithNulls=false] - Whether to fill missing ordered parameters with null.
38
+ * @returns {Promise<T>} The result of the stored procedure execution.
27
39
  */
28
40
  async execSP<T = any>(spName: string, {
29
41
  params = [],
@@ -50,7 +62,7 @@ export class BaseRepository {
50
62
  conn = await this.pool.getConnection();
51
63
 
52
64
  const declareOutVars = out.length
53
- ? out.map(({ name, type }) => `SET @${name} = CAST(NULL AS ${type ?? 'VARCHAR(255)'});`).join('\n')
65
+ ? out.map(({ name, type }) => `SET ${name} = CAST(NULL AS ${type ?? 'VARCHAR(255)'});`).join('\n')
54
66
  : '';
55
67
 
56
68
  const selectOuts = out.length
@@ -76,10 +88,10 @@ export class BaseRepository {
76
88
  }
77
89
 
78
90
  /**
79
- * Retrieves all records from a view.
91
+ * Retrieves all records from a database view.
80
92
  *
81
- * @param viewName - The name of the view to query.
82
- * @returns The result of the query.
93
+ * @param {string} viewName - The name of the view to query.
94
+ * @returns {Promise<T[]>} An array containing the records from the view.
83
95
  */
84
96
  async view<T = any>(viewName: string): Promise<T[]> {
85
97
  let conn;
@@ -95,11 +107,11 @@ export class BaseRepository {
95
107
  }
96
108
 
97
109
  /**
98
- * Executes a query with the given parameters.
110
+ * Executes a raw SQL query with optional parameters.
99
111
  *
100
- * @param query - The SQL query to execute.
101
- * @param params - The parameters to pass to the query.
102
- * @returns The result of the query.
112
+ * @param {string} query - The SQL query to execute.
113
+ * @param {any[]} [params] - The parameters to safely bind to the query.
114
+ * @returns {Promise<T[]>} The result set from the query.
103
115
  */
104
116
  async query<T = any>(query: string, params?: any[]): Promise<T[]> {
105
117
  let conn;
@@ -115,10 +127,11 @@ export class BaseRepository {
115
127
  }
116
128
 
117
129
  /**
118
- * Connects to the database.
130
+ * Tests the database connection and invokes callbacks based on the result.
119
131
  *
120
- * @param onSuccess - Callback function to execute on success.
121
- * @param onError - Callback function to execute on error.
132
+ * @param {function} [onSuccess] - Callback function to execute if the connection is successful.
133
+ * @param {function} [onError] - Callback function to execute if the connection fails.
134
+ * @throws {Error} If the connection fails and no onError callback is provided.
122
135
  */
123
136
  async connectDB(onSuccess?: () => void, onError?: (error: Error) => void) {
124
137
  try {
@@ -132,10 +145,11 @@ export class BaseRepository {
132
145
  }
133
146
 
134
147
  /**
135
- * Closes the connection pool.
148
+ * Closes the connection pool and all active connections.
136
149
  *
137
- * @param onSuccess - Callback function to execute on success.
138
- * @param onError - Callback function to execute on error.
150
+ * @param {function} [onSuccess] - Callback function to execute after the pool is closed.
151
+ * @param {function} [onError] - Callback function to execute if closing the pool fails.
152
+ * @returns {Promise<void>}
139
153
  */
140
154
  async closeDB(onSuccess?: () => void, onError?: (error: Error) => void) {
141
155
  return this.pool.end().then(() => {
@@ -146,6 +160,10 @@ export class BaseRepository {
146
160
  });
147
161
  }
148
162
 
163
+ /**
164
+ * Returns the underlying MariaDB connection pool.
165
+ * @returns {Promise<mariadb.Pool>} The MariaDB pool instance.
166
+ */
149
167
  async getOriginalPool() {
150
168
  return this.pool;
151
169
  }
@@ -1,21 +1,49 @@
1
+ /**
2
+ * Represents an output variable for a stored procedure.
3
+ */
1
4
  export interface OutVariable {
5
+ /** The name of the variable (without the @ symbol). */
2
6
  name: string;
7
+ /** The SQL type of the variable (e.g., 'VARCHAR(255)', 'INT'). Defaults to 'VARCHAR(255)'. */
3
8
  type?: string;
4
9
  }
5
10
 
11
+ /**
12
+ * Options for executing a stored procedure.
13
+ */
6
14
  export interface ExecSPOptions {
7
- /** Parameters to pass to the SP. Can be an array or an object if `extract` is true. */
15
+ /**
16
+ * Parameters to pass to the stored procedure.
17
+ * Can be an array of values or an object (if `extract` is set to true).
18
+ */
8
19
  params?: any[] | Record<string, any>;
9
- /** Definition of output variables. */
20
+ /**
21
+ * Definitions of output variables to be captured after execution.
22
+ */
10
23
  out?: OutVariable[];
11
- /** If true, flattens the result into a single object. */
24
+ /**
25
+ * If true, flattens the result into a single object when the SP returns a single row.
26
+ * @default true
27
+ */
12
28
  plain?: boolean;
13
- /** If true, extracts parameters from an object based on the `order` array. */
29
+ /**
30
+ * If true, extracts parameters from an object based on the `order` array.
31
+ * Useful when you want to pass an object but the SP expects positional parameters.
32
+ * @default false
33
+ */
14
34
  extract?: boolean;
15
- /** The order of keys to extract from `params`. */
35
+ /**
36
+ * The order of keys to extract from `params` when `extract` is true.
37
+ */
16
38
  order?: string[];
17
- /** If true, deletes extra parameters not specified in `order`. */
39
+ /**
40
+ * If true, deletes keys from the `params` object that are not specified in the `order` array.
41
+ * @default false
42
+ */
18
43
  deleteExtra?: boolean;
19
- /** If true, fills missing ordered parameters with null. */
44
+ /**
45
+ * If true, fills missing parameters defined in `order` with `null` if they are not in `params`.
46
+ * @default false
47
+ */
20
48
  fillWithNulls?: boolean;
21
49
  }
@@ -0,0 +1,16 @@
1
+ import { BaseRepository } from "./dist/index.js";
2
+
3
+ /**
4
+ * This is a sample JS file to verify autocomplete.
5
+ * If you hover over BaseRepository or its methods, you should see full documentation.
6
+ */
7
+
8
+ const repo = new BaseRepository({
9
+ host: 'localhost',
10
+ user: 'root',
11
+ password: 'password',
12
+ database: 'test'
13
+ });
14
+
15
+ // Try typing 'repo.execSP(' and observe the parameter hints!
16
+ // repo.execSP('sp_test', { params: ... });