-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlegacy.js
200 lines (161 loc) · 10.6 KB
/
legacy.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/**
* UnSQL_Legacy base class
* @class UnSQL_Legacy
* @description All the model classes must extend using this base class
*
* @author Siddharth Tiwari <dev.unsql@gmail.com>
*/
class UnSQL_Legacy {
/**
* Generates 'select' statement
* @method find
* @description This method is used to dynamically generate valid SQL 'select' query that is used to read / retrieve records from the database
* @param {object} findParam
* @param {string} [findParam.select] (optional) comma separated string value of columns, functions that needs to be selected from the database
* @param {string} [findParam.alias] (optional) local reference name for the database table
* @param {Array<{type:string, table:string, on:string}>} [findParam.join] (optional) array of join object(s), each object representing the association of child table to this table
* @param {Array<Array<string|number|boolean>>} [findParam.where] (optional) array of array containing conditions to filter the records from the database, each condition is joined using 'and' clause
* @param {Array<Array<string|number|boolean>>} [findParam.whereOr] (optional) same as 'where' property, only difference is the conditions are connected using 'or' clause
* @param {'and'|'or'} [findParam.junction] (optional) connects 'where' and 'whereOr' together, can be either 'and' or 'or', default is 'and'
* @param {string} [findParam.groupBy] (optional) takes in comma separated string value of column(s) that will be used to group the records together
* @param {string} [findParam.having] (optional) takes in comma separated string value of column(s) / aggregate method(s) with comparators to filter records
* @param {string} [findParam.orderBy] (optional) takes in comma separated string value of column(s) along with key words 'ASC' or 'DESC', that will be used to reorder the records together
* @param {'asc'|'desc'} [findParam.orderDirection] (optional) used to define the order 'ascending' or 'descending' via. keywords 'asc' and 'desc' respectively, used when only one column name is entered in 'orderBy' property
* @param {number} [findParam.rowCount] (optional) limits the number of records that will be fetched from the database table
* @param {number} [findParam.offset] (optional) defines the starting index for the records to be fetched from the database table
*
* @returns {{ success: boolean, result?: Array, error?: * }} execution success acknowledgement along with either 'result' or 'error' object
*
* @static
* @memberof UnSQL_Legacy
*/
static async find({ select = '*', alias = null, join = null, where = null, whereOr = null, junction = 'AND', groupBy = null, having = null, orderBy = null, orderDirection = 'DESC', rowCount = null, offset = null }) {
if (!this.POOL) return { success: false, error: { message: 'Connection pool not defined! Please assign a mysql connection pool variable as a \'static POOL="your_mysql_connection_pool"\' inside model class.' } }
if (!this.TABLE_NAME) return { success: false, error: { message: 'Database table name not mapped! Please assign a mysql table name variable as a \'static TABLE_NAME="db_table_name"\' inside model class.' } }
let sql = `SELECT ${select} FROM ${this.TABLE_NAME} `
if (alias) sql += `${alias} `
if (join != null) {
for (const element of join) {
if (element?.type && element?.table && element?.on) sql += ` ${element?.type} JOIN ${element?.table} ON ${element?.on} `
}
}
if (where != null || whereOr != null) {
sql += ' WHERE '
if (where) sql += ` (${where && where.map(condition => condition.join(' ')).join(' AND ')}) `
if (where && whereOr) sql += ` ${junction || 'AND'} `
if (whereOr) sql += ` (${whereOr && whereOr.map(condition => condition.join(' ')).join(' OR ')}) `
}
if (groupBy) sql += ` GROUP BY ${groupBy} `
if (having) sql += ` HAVING ${having} `
if (orderBy) sql += ` ORDER BY ${orderBy} `
if (orderBy != null && ('ASC' === orderDirection || 'DESC' === orderDirection)) sql += ` ${orderDirection} `
if (rowCount) sql += ` LIMIT ${rowCount}`
if (offset) sql += ` OFFSET ${offset}`
const connection = await this.POOL.getConnection()
try {
await connection.beginTransaction()
const [rows] = await connection.query(sql)
await connection.commit()
return { success: true, result: rows }
} catch (error) {
if (connection) await connection.rollback()
return { success: false, error }
} finally {
if (connection) await connection.release()
}
}
/**
* Generates 'insert' and 'update' query
* @method save
* @description This method is used to 'insert' or 'update' data into the database table by dynamically generating valid SQL based on the parameters passed
* @param {object} saveParam
* @param {string} [saveParam.alias] (optional) local reference name for the database table
* @param {object} saveParam.data (required) actual data that needs to be 'inserted' or 'updated' into the database table
* @param {object} [saveParam.updateObj] (optional) data that needs to be 'upsert' into the database table in case of 'duplicate key' is detected
* @param {Array<Array<string|number|boolean>>} [saveParam.where] (optional) array of array containing conditions to filter the record in the database that needs to be 'updated', each condition is joined using 'and' clause
* @param {Array<Array<string|number|boolean>>} [saveParam.whereOr] (optional) same as 'where' property, only difference is the conditions are connected using 'or' clause
* @param {'and'|'or'} [saveParam.junction] (optional) connects 'where' and 'whereOr' together, can be either 'and' or 'or', default is 'and'
*
* @returns {{success: boolean, insertID?: number, error?: object }} execution success acknowledgement along with either 'insertID' (inserted index) or 'error' object
*
* @static
* @memberof UnSQL_Legacy
*/
static async save({ alias = null, data, updateObj = null, where = null, whereOr = null, junction = 'AND' }) {
if (!this.POOL) return { success: false, error: { message: 'Connection pool not defined! Please assign a mysql connection pool variable as a \'static POOL="your_mysql_connection_pool"\' inside model class.' } }
if (!this.TABLE_NAME) return { success: false, error: { message: 'Database table name not mapped! Please assign a mysql table name variable as a \'static TABLE_NAME="db_table_name"\' inside model class.' } }
let sql = ''
const params = []
if (where != null || whereOr != null) {
sql = `UPDATE ${this.TABLE_NAME + (alias ? ' ' + alias : '')} SET `
sql += Object.entries(data).map(([k, v]) => {
params.push(k, v)
return '?? = ?'
}).join(', ')
sql += ` WHERE`
if (where) sql += ` (${where && where.map(condition => condition.join(' ')).join(' AND ')}) `
if (where && whereOr) sql += ` ${junction || 'AND'} `
if (whereOr) sql += ` (${whereOr && whereOr.map(condition => condition.join(' ')).join(' OR ')}) `
} else {
sql = `INSERT INTO ${this.TABLE_NAME + (alias ? ' ' + alias : '')} SET ` + Object.entries(data).map(([k, v]) => {
params.push(k, v)
return '?? = ?'
}).join(', ')
if (updateObj) sql += ' ON DUPLICATE KEY UPDATE ' + Object.entries(data).map(([k, v]) => {
params.push(k, v)
return '?? = ?'
}).join(', ')
}
try {
const connection = await this.POOL.getConnection()
await connection.beginTransaction()
try {
const [result] = await connection.query(sql, params)
await connection.commit()
return { success: true, insertID: result.insertId }
} catch (error) {
if (connection) await connection.rollback()
return { success: false, error }
} finally {
if (connection) await connection.release()
}
} catch (error) {
return { success: false, error }
}
}
/**
* Generates 'delete' query
* @method del
* @param {object} delParam
* @param {string} [delParam.alias] (optional) local reference name for the database table
* @param {Array<Array<string|number|boolean>>} [delParam.where] (optional) array of array containing conditions to filter the record in the database that needs to be 'deleted', each condition is joined using 'and' clause
* @param {Array<Array<string|number|boolean>>} [delParam.whereOr] (optional) same as 'where' property, only difference is the conditions are connected using 'or' clause
* @param {'and'|'or'} [delParam.junction] (optional) connects 'where' and 'whereOr' together, can be either 'and' or 'or', default is 'and'
*
* @returns {{success: boolean, result?: *, error?: object }}
*/
static async del({ alias = null, where = null, whereOr = null, junction = 'AND' }) {
if (!this.POOL) return { success: false, error: { message: 'Connection pool not defined! Please assign a mysql connection pool variable as a \'static POOL="your_mysql_connection_pool"\' inside model class.' } }
if (!this.TABLE_NAME) return { success: false, error: { message: 'Database table name not mapped! Please assign a mysql table name variable as a \'static TABLE_NAME="db_table_name"\' inside model class.' } }
let sql = `DELETE FROM ${this.TABLE_NAME + (alias != null ? ' ' + alias : '')} `
if (where != null || whereOr != null) {
sql += ' WHERE '
if (where) sql += ` (${where && where.map(condition => condition.join(' ')).join(' AND ')}) `
if (where && whereOr) sql += ` ${junction || 'AND'} `
if (whereOr) sql += ` (${whereOr && whereOr.map(condition => condition.join(' ')).join(' OR ')}) `
}
const connection = await this.POOL.getConnection()
try {
await connection.beginTransaction()
const result = await connection.query(sql)
await connection.commit()
return { success: true, result }
} catch (error) {
if (connection) await connection.rollback()
return { success: false, error }
} finally {
if (connection) await connection.release()
}
}
}
module.exports = UnSQL_Legacy