@krishnapawar/kp-mysql-models
Advanced tools
Comparing version 1.2.3 to 1.2.4
126
lib/index.js
@@ -41,8 +41,9 @@ var pool; | ||
} | ||
const dbQuery = (sql) => { | ||
const dbQuery = (sql,dbcon=null) => { | ||
let db = dbcon ?? pool; | ||
return new Promise((resolve, reject) => { | ||
if (sqlConnect(pool)) { | ||
return reject(sqlConnect(pool)); | ||
if (sqlConnect(db)) { | ||
return reject(sqlConnect(db)); | ||
} | ||
pool.query(sql, function (error, results, fields) { | ||
db.query(sql, function (error, results, fields) { | ||
if (error) return reject(error); | ||
@@ -435,2 +436,7 @@ return resolve(results); | ||
}; | ||
const onlyTrashed= (data) => { | ||
if (isCheck(data)) return ""; | ||
if (data === true ) return " deleted_at IS NOT NULL "; | ||
return ""; | ||
}; | ||
@@ -474,4 +480,3 @@ const whereClause = (data, resData) => { | ||
} | ||
const where_row = whereRaw(data.whereRaw); | ||
const connect_tb = connect(data.connect, resData); | ||
@@ -491,2 +496,3 @@ if ( | ||
const where_row = whereRaw(data.whereRaw); | ||
if ( | ||
@@ -505,2 +511,18 @@ where_row != "" && | ||
} | ||
const only_trashed = onlyTrashed(data.onlyTrashed); | ||
if ( | ||
only_trashed != "" && | ||
(where_row != "" || connect_tb != "" || | ||
where_is != "" || | ||
where_or != "" || | ||
where_and != "" || | ||
where_in != "" || | ||
where_not_in != "") | ||
) { | ||
addCondition += ` AND ${only_trashed}`; | ||
} else if (only_trashed != "") { | ||
addCondition += only_trashed; | ||
} | ||
return addCondition != "" ? `WHERE ${addCondition.trim()}` : ""; | ||
@@ -836,2 +858,40 @@ }; | ||
}; | ||
const addDeletedAt = (table, connection) => { | ||
if (isTable(table)) return reject(isTable(table)); | ||
return new Promise((resolve, reject) => { | ||
if (sqlConnect(connection)) { | ||
return reject(sqlConnect(connection)); | ||
} | ||
connection.query( | ||
`SHOW COLUMNS FROM ${table} LIKE "deleted_at"`, | ||
async (err, res) => { | ||
if (err) { | ||
return reject(err); | ||
} | ||
if (res.length > 0) return resolve(true); | ||
try { | ||
const colunm = await dbQuery( | ||
`SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = '${table}' ORDER BY ORDINAL_POSITION DESC LIMIT 1`, | ||
connection | ||
); | ||
if (colunm.length) { | ||
connection.query( | ||
`ALTER TABLE ${table} ADD deleted_at TIMESTAMP NULL DEFAULT NULL AFTER ${colunm[0].COLUMN_NAME}`, | ||
(err, res) => { | ||
if (err) { | ||
return reject(err); | ||
} | ||
return resolve(true); | ||
} | ||
); | ||
} else { | ||
return resolve(false); | ||
} | ||
} catch (error) { | ||
return reject(error); | ||
} | ||
} | ||
); | ||
}); | ||
}; | ||
class BaseModels { | ||
@@ -1092,2 +1152,21 @@ constructor(){ | ||
}; | ||
delete(data){ | ||
if (isTable(this._table)) return reject(isTable(this._table)); | ||
return new Promise((resolve, reject) => { | ||
if (sqlConnect(this._connection)) { | ||
return reject(sqlConnect(this._connection)); | ||
} | ||
if (data.where != undefined) { | ||
this._connection.query( | ||
`DELETE FROM ${this._table} ${whereClause(data)}`, | ||
(err, res) => { | ||
if (err) { | ||
return reject(err); | ||
} | ||
return resolve(res); | ||
} | ||
); | ||
} | ||
}); | ||
}; | ||
deleleAll(data){ | ||
@@ -1121,2 +1200,37 @@ if (isTable(this._table)) return reject(isTable(this._table)); | ||
}; | ||
trashed(data){ | ||
if (isTable(this._table)) return reject(isTable(this._table)); | ||
return new Promise( async(resolve,reject)=>{ | ||
try { | ||
if (sqlConnect(this._connection)) { | ||
return reject(sqlConnect(this._connection)); | ||
} | ||
let colunm = await addDeletedAt(this._table,this._connection); | ||
if (colunm) { | ||
this._connection.query(`UPDATE ${this._table} SET deleted_at = NOW() ${whereClause(data)}`, (err, res) => { | ||
if (err) { | ||
return reject(err); | ||
} | ||
return resolve(res); | ||
}); | ||
} | ||
} catch (error) { | ||
return reject(error); | ||
} | ||
}); | ||
} | ||
restore(data){ | ||
if (isTable(this._table)) return reject(isTable(this._table)); | ||
if (sqlConnect(this._connection)) { | ||
return reject(sqlConnect(this._connection)); | ||
} | ||
return new Promise( async(resolve,reject)=>{ | ||
this._connection.query(`UPDATE ${this._table} SET deleted_at = NULL ${whereClause(data)}`, (err, res) => { | ||
if (err) { | ||
return reject(err); | ||
} | ||
return resolve(res); | ||
}); | ||
}); | ||
} | ||
} | ||
@@ -1123,0 +1237,0 @@ module.exports = { |
{ | ||
"name": "@krishnapawar/kp-mysql-models", | ||
"version": "1.2.3", | ||
"version": "1.2.4", | ||
"description": "The `kp-mysql-models` library simplifies MySQL database interaction, streamlining tasks such as creating, inserting, updating, and deleting records, as well as handling complex queries like joins, pagination, and conditional operations. By offering an intuitive and efficient approach, it significantly reduces development time and effort.", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
@@ -5,5 +5,5 @@ # kp-mysql-models | ||
>npm i kp-mysql-models | ||
> npm i kp-mysql-models | ||
>npm i @krishnapawar/kp-mysql-models | ||
> npm i @krishnapawar/kp-mysql-models | ||
@@ -15,3 +15,3 @@ | ||
const { | ||
setBDConnection, | ||
setDBConnection, | ||
get, | ||
@@ -38,3 +38,3 @@ first, | ||
setBDConnection(pool); | ||
setDBConnection(pool); | ||
``` | ||
@@ -302,2 +302,34 @@ >after that use (call) all methods like helper function in you code | ||
``` | ||
>We can use soft delete as well by using BaseModels class for Example | ||
```JavaScript | ||
let user = new User; | ||
//for soft deleteing data | ||
let data = await user.trashed({ | ||
where: { | ||
id: 585, | ||
} | ||
}); | ||
//for soft deleteing restoring data | ||
let data = await user.restore({ | ||
where: { | ||
id: 585, | ||
} | ||
}); | ||
//for fetch soft deleted data useing onlyTrashed:true; | ||
let data = await user.first({ | ||
onlyTrashed:true, | ||
where: { | ||
id: 585, | ||
} | ||
}); | ||
let data = await user.get({ onlyTrashed:true }); | ||
``` | ||
>or you can use same like abow example. | ||
@@ -332,3 +364,3 @@ | ||
``` | ||
>You can use all method same like that | ||
>Some Important methods that You can also use, | ||
* get, | ||
@@ -340,2 +372,3 @@ * first, | ||
* destroy, | ||
* delete, | ||
* create, | ||
@@ -346,3 +379,39 @@ * update, | ||
* dbWith, | ||
* tracested, | ||
* restore, | ||
>Some Important Key Words that can help in abow methods, | ||
* table, | ||
* select, | ||
* elements, | ||
* latest, | ||
* limit, | ||
* pagination, | ||
* with, | ||
* connect, | ||
* join, | ||
* dbWith, | ||
* hasOne, | ||
* belongsTo, | ||
* where, | ||
* whereOr, | ||
* whereIn, | ||
* whereNotIn, | ||
* whereIs, | ||
* whereRaw, | ||
* on, | ||
* onOr, | ||
* onIn, | ||
* onNotIn, | ||
* onIs, | ||
* onRaw, | ||
* onlyTrashed, | ||
***some usefull method that can help with that method*** | ||
@@ -349,0 +418,0 @@ *** |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
47178
1216
493