mrepo-sql 1.0.4 → 1.0.7
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 +1 -1
- package/dist/db/repo.js +10 -3
- package/dist/interfaces/db.interface.d.ts +3 -2
- package/package.json +1 -1
- package/src/db/repo.ts +10 -2
- package/src/index.ts +1 -1
- package/src/interfaces/db.interface.ts +4 -2
package/dist/db/repo.d.ts
CHANGED
|
@@ -21,7 +21,7 @@ export declare class BaseRepository {
|
|
|
21
21
|
* @param {boolean} [options.fillWithNulls=false] - Whether to fill missing ordered parameters with null.
|
|
22
22
|
* @returns {Promise<T>} The result of the stored procedure execution.
|
|
23
23
|
*/
|
|
24
|
-
execSP<T = any>(spName: string, { params, out, plain, extract, order, deleteExtra, fillWithNulls, }: ExecSPOptions): Promise<T>;
|
|
24
|
+
execSP<T = any>(spName: string, { params, out, plain, extract, order, deleteExtra, fillWithNulls, onNotData }: ExecSPOptions): Promise<T>;
|
|
25
25
|
/**
|
|
26
26
|
* Retrieves all records from a database view.
|
|
27
27
|
*
|
package/dist/db/repo.js
CHANGED
|
@@ -28,7 +28,7 @@ export class BaseRepository {
|
|
|
28
28
|
* @param {boolean} [options.fillWithNulls=false] - Whether to fill missing ordered parameters with null.
|
|
29
29
|
* @returns {Promise<T>} The result of the stored procedure execution.
|
|
30
30
|
*/
|
|
31
|
-
async execSP(spName, { params = [], out = [], plain = true, extract = false, order = [], deleteExtra = false, fillWithNulls = false, }) {
|
|
31
|
+
async execSP(spName, { params = [], out = [], plain = true, extract = false, order = [], deleteExtra = false, fillWithNulls = false, onNotData }) {
|
|
32
32
|
const endParams = extract && !Array.isArray(params) && params !== null
|
|
33
33
|
? extractParams({ params: params, order, deleteExtra, fillWithNulls })
|
|
34
34
|
: (Array.isArray(params) ? params : []);
|
|
@@ -41,7 +41,7 @@ export class BaseRepository {
|
|
|
41
41
|
try {
|
|
42
42
|
conn = await this.pool.getConnection();
|
|
43
43
|
const declareOutVars = out.length
|
|
44
|
-
? out.map(({ name
|
|
44
|
+
? out.map(({ name }) => `SET @${name} = NULL;`).join('\n')
|
|
45
45
|
: '';
|
|
46
46
|
const selectOuts = out.length
|
|
47
47
|
? 'SELECT ' + out
|
|
@@ -54,7 +54,14 @@ export class BaseRepository {
|
|
|
54
54
|
${selectOuts}
|
|
55
55
|
`;
|
|
56
56
|
const res = await conn.query(query, endParams);
|
|
57
|
-
|
|
57
|
+
const returnData = (plain ? flattenToObject(res) : (res[0] ?? []));
|
|
58
|
+
if (!onNotData) {
|
|
59
|
+
return returnData;
|
|
60
|
+
}
|
|
61
|
+
if (!returnData || (Array.isArray(returnData) && !returnData.length) || (typeof returnData === 'object' && !Object.keys(returnData).length)) {
|
|
62
|
+
onNotData();
|
|
63
|
+
}
|
|
64
|
+
return returnData;
|
|
58
65
|
}
|
|
59
66
|
catch (error) {
|
|
60
67
|
throw error;
|
|
@@ -4,8 +4,8 @@
|
|
|
4
4
|
export interface OutVariable {
|
|
5
5
|
/** The name of the variable (without the @ symbol). */
|
|
6
6
|
name: string;
|
|
7
|
-
/**
|
|
8
|
-
|
|
7
|
+
/** If true, the value will be converted to a number. */
|
|
8
|
+
toNumber?: boolean;
|
|
9
9
|
}
|
|
10
10
|
/**
|
|
11
11
|
* Options for executing a stored procedure.
|
|
@@ -45,4 +45,5 @@ export interface ExecSPOptions {
|
|
|
45
45
|
* @default false
|
|
46
46
|
*/
|
|
47
47
|
fillWithNulls?: boolean;
|
|
48
|
+
onNotData?: () => void;
|
|
48
49
|
}
|
package/package.json
CHANGED
package/src/db/repo.ts
CHANGED
|
@@ -45,6 +45,7 @@ export class BaseRepository {
|
|
|
45
45
|
order = [],
|
|
46
46
|
deleteExtra = false,
|
|
47
47
|
fillWithNulls = false,
|
|
48
|
+
onNotData
|
|
48
49
|
}: ExecSPOptions): Promise<T> {
|
|
49
50
|
const endParams = extract && !Array.isArray(params) && params !== null
|
|
50
51
|
? extractParams({ params: params as Record<string, any>, order, deleteExtra, fillWithNulls })
|
|
@@ -62,7 +63,7 @@ export class BaseRepository {
|
|
|
62
63
|
conn = await this.pool.getConnection();
|
|
63
64
|
|
|
64
65
|
const declareOutVars = out.length
|
|
65
|
-
? out.map(({ name
|
|
66
|
+
? out.map(({ name }) => `SET @${name} = NULL;`).join('\n')
|
|
66
67
|
: '';
|
|
67
68
|
|
|
68
69
|
const selectOuts = out.length
|
|
@@ -79,7 +80,14 @@ export class BaseRepository {
|
|
|
79
80
|
|
|
80
81
|
const res = await conn.query(query, endParams);
|
|
81
82
|
|
|
82
|
-
|
|
83
|
+
const returnData = (plain ? flattenToObject(res) : (res[0] ?? [])) as T;
|
|
84
|
+
if(!onNotData) {
|
|
85
|
+
return returnData
|
|
86
|
+
}
|
|
87
|
+
if(!returnData || (Array.isArray(returnData) && !returnData.length) || (typeof returnData === 'object' && !Object.keys(returnData).length)) {
|
|
88
|
+
onNotData();
|
|
89
|
+
}
|
|
90
|
+
return returnData;
|
|
83
91
|
} catch (error) {
|
|
84
92
|
throw error;
|
|
85
93
|
} finally {
|
package/src/index.ts
CHANGED
|
@@ -4,8 +4,8 @@
|
|
|
4
4
|
export interface OutVariable {
|
|
5
5
|
/** The name of the variable (without the @ symbol). */
|
|
6
6
|
name: string;
|
|
7
|
-
/**
|
|
8
|
-
|
|
7
|
+
/** If true, the value will be converted to a number. */
|
|
8
|
+
toNumber?: boolean;
|
|
9
9
|
}
|
|
10
10
|
|
|
11
11
|
/**
|
|
@@ -46,4 +46,6 @@ export interface ExecSPOptions {
|
|
|
46
46
|
* @default false
|
|
47
47
|
*/
|
|
48
48
|
fillWithNulls?: boolean;
|
|
49
|
+
|
|
50
|
+
onNotData?: () => void;
|
|
49
51
|
}
|