Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@krishnapawar/kp-mysql-models

Package Overview
Dependencies
Maintainers
1
Versions
13
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@krishnapawar/kp-mysql-models - npm Package Compare versions

Comparing version 1.2.5 to 1.2.6

lib/BaseModels.js

1284

lib/index.js

@@ -1,1272 +0,21 @@

var pool;
var database_name;
var port;
var host;
function setDBConnection(db) {
if (!db) {
console.error("kp-mysql-models is not connect database.Please Check You Database Connection!");
return false;
}
if (!db.config) {
console.error("kp-mysql-models is not connect database.Please Check You Database Connection!");
return false;
}
if (!db.config.connectionConfig) {
console.error("kp-mysql-models is not connect database.Please Check You Database Connection!");
return false;
}
if (!db.config.connectionConfig.database) {
console.error("kp-mysql-models is not connect database.Please Check You Database Connection!");
return false;
}
if (!db.config.connectionConfig.host) {
console.error("kp-mysql-models is not connect database.Please Check You Database Connection!");
return false;
}
if (!db.config.connectionConfig.port) {
console.error("kp-mysql-models is not connect database.Please Check You Database Connection!");
return false;
}
pool = db;
database_name = pool.config.connectionConfig.database;
host = pool.config.connectionConfig.host;
port = pool.config.connectionConfig.port;
if (!pool || !database_name || !host || !port) {
console.error("kp-mysql-models is not connect database.Please Check You Database Connection!");
return false;
}
return true;
}
const dbQuery = (sql,dbcon=null) => {
let db = dbcon ?? pool;
return new Promise((resolve, reject) => {
if (sqlConnect(db)) {
return reject(sqlConnect(db));
}
db.query(sql, function (error, results, fields) {
if (error) return reject(error);
return resolve(results);
});
});
};
const get = (data) => {
return new Promise((resolve, reject) => {
if (sqlConnect(pool)) {
return reject(sqlConnect(pool));
}
if (isTable(data.table)) return reject(isTable(data.table));
let response = [];
pool.query(
`SELECT ${selectOption(data)} FROM ${data.table} ${whereClause(
data
)} ${isLatest(data)} ${isLimit(data)} ${pagination(data)}`,
async (err, res) => {
if (err) {
return reject(err);
}
if (data!= undefined && (!isCheck(data.pagination) || data.pagination >= 0)) {
pool.query(
`SELECT count(*) as totalData FROM ${data.table} ${whereClause(
data
)} ${isLatest(data)}`,
async (err, resp) => {
if (err) {
return reject(err);
}
let paginate = paginationData(resp[0].totalData, data);
for (const iterator of res) {
const item = await withdataForGet(reject, iterator, data);
response.push(item);
}
return resolve({ response, paginate });
}
);
} else {
for (const iterator of res) {
const item = await withdataForGet(reject, iterator, data);
response.push(item);
}
return resolve(response);
}
}
);
});
};
//get singal data
const first = (data) => {
return new Promise((resolve, reject) => {
if (sqlConnect(pool)) {
return reject(sqlConnect(pool));
}
if (isTable(data.table)) return reject(isTable(data.table));
pool.query(
`SELECT ${selectOption(data)} FROM ${data.table} ${whereClause(
data
)} ${isLatest(data)} ${isLimit(data, 1)}`,
(err, res) => {
if (err) {
return reject(err);
}
if (res.length > 0) {
return withdata(reject, resolve, res[0], data);
}
return resolve({});
}
);
});
};
const { isCheck } = require('./isCheck');
const dbJoin = (data) => {
return new Promise((resolve, reject) => {
if (sqlConnect(pool)) {
return reject(sqlConnect(pool));
}
pool.query(
`SELECT ${selectOption(data)} FROM ${data.table} ${joinTable(
data.join
)} ${whereClause(data)} ${isLatest(data)} ${isLimit(
data
)} ${pagination(data)}`,
(err, res) => {
if (err) {
return reject(err);
}
if (data!= undefined && (!isCheck(data.pagination) || data.pagination >= 0)) {
pool.query(
`SELECT count(*) as totalData FROM ${data.table} ${joinTable(
data.join
)} ${whereClause(data)} ${isLatest(data)}`,
(err, resp) => {
if (err) {
return reject(err);
}
let paginate = paginationData(resp[0].totalData, data);
const {
setDBConnection,
get,
first,
dbQuery,
trunCate,
deleleAll,
destroy,
create,
update,
save,
dbJoin,
dbWith,
} = require('./helper');
return resolve({ res, paginate });
}
);
} else {
return resolve(res);
}
}
);
});
};
const dbWith = (data) => {
return new Promise((resolve, reject) => {
if (sqlConnect(pool)) {
return reject(sqlConnect(pool));
}
pool.query(
`SELECT ${selectOption(data)} FROM ${data.table} ${withTable(
data.with
)} ${whereClause(data)} ${isLatest(data)} ${isLimit(
data
)} ${pagination(data)}`,
(err, res) => {
if (err) {
return reject(err);
}
if (data!= undefined && (!isCheck(data.pagination) || data.pagination >= 0)) {
pool.query(
`SELECT count(*) as totalData FROM ${data.table} ${withTable(
data.with
)} ${whereClause(data)} ${isLatest(data)}`,
(err, resp) => {
if (err) {
return reject(err);
}
let paginate = paginationData(resp[0].totalData, data);
const BaseModels = require('./BaseModels');
return resolve({ res, paginate });
}
);
} else {
return resolve(res);
}
}
);
});
};
const create = (data) => {
return new Promise((resolve, reject) => {
if (sqlConnect(pool)) {
return reject(sqlConnect(pool));
}
if (isTable(data.table)) return reject(isTable(data.table));
if (data.elements != undefined && data.elements != null) {
let keys = Object.keys(data.elements).toString();
let values = Object.values(data.elements).toString();
pool.query(
`INSERT INTO ${data.table} (${keys}) VALUES (${values})`,
(err, res) => {
if (err) {
return reject(err);
}
return resolve(res);
}
);
} else {
return resolve(true);
}
});
};
const save = (data) => {
return new Promise((resolve, reject) => {
if (sqlConnect(pool)) {
return reject(sqlConnect(pool));
}
if (isTable(data.table)) return reject(isTable(data.table));
if (data.elements != undefined && data.elements != null) {
if (
whereClause(data) != undefined &&
whereClause(data) != null &&
whereClause(data) != ""
) {
var sqlQuery = `UPDATE ${data.table} ${getKeyValue(
data.elements,
"SET"
)} ${whereClause(data)}`;
} else {
var sqlQuery = `INSERT INTO ${data.table} ${getKeyValue(
data.elements,
"SET"
)}`;
}
pool.query(sqlQuery, (err, res) => {
if (err) {
return reject(err);
}
return resolve(res);
});
} else {
return resolve(true);
}
});
};
const update = (data) => {
if (isTable(data.table)) return reject(isTable(data.table));
return new Promise(async (resolve, reject) => {
if (sqlConnect(pool)) {
return reject(sqlConnect(pool));
}
if (data.elements != undefined && data.elements != null) {
var sqlQuery = `UPDATE ${data.table} ${getKeyValue(
data.elements,
"SET"
)} ${whereClause(data)}`;
pool.query(sqlQuery, (err, res) => {
if (err) {
return reject(err);
}
return resolve(res);
});
} else {
return resolve(true);
}
});
};
const destroy = (data) => {
if (isTable(data.table)) return reject(isTable(data.table));
return new Promise((resolve, reject) => {
if (sqlConnect(pool)) {
return reject(sqlConnect(pool));
}
if (data.where != undefined) {
pool.query(
`DELETE FROM ${data.table} ${whereClause(data)}`,
(err, res) => {
if (err) {
return reject(err);
}
return resolve(res);
}
);
}
});
};
const deleleAll = (data) => {
if (isTable(data.table)) return reject(isTable(data.table));
return new Promise((resolve, reject) => {
if (sqlConnect(pool)) {
return reject(sqlConnect(pool));
}
pool.query(`DELETE FROM ${data.table} ${whereClause(data)}`, (err, res) => {
if (err) {
return reject(err);
}
return resolve(res);
});
});
};
const trunCate = (data) => {
if (isTable(data.table)) return reject(isTable(data.table));
return new Promise((resolve, reject) => {
if (sqlConnect(pool)) {
return reject(sqlConnect(pool));
}
pool.query(`TRUNCATE TABLE ${data.table}`, (err, res) => {
if (err) {
return reject(err);
}
return resolve(res);
});
});
};
//end
const getKeyValue = (data, setType = "WHERE") => {
if (isCheck(data)) return "";
let setItem = [];
for (const [key, value] of Object.entries(data)) {
setItem.push(`${key}='${value}'`);
}
return " " + setType + " " + setItem.toString();
};
const selectOption = (data) => {
if (data != undefined && data.select != undefined && data.select != null && data.select != "" && data.select.length > 0)
return data.select.toString();
return "*";
};
const isTable = (data) => {
if (data != undefined && data != null && data != "") return false;
return "Table name is missing!";
};
const isLimit = (data, count = "") => {
if (isCheck(data)) return "";
if (isCheck(data.limit)) return "";
if (
data.limit != undefined &&
data.limit != null &&
data.limit != "" &&
typeof data.limit == "number" &&
data.limit > 0
) {
if (count != "" && data.limit < 2) return `LIMIT ${count}`;
return `LIMIT ${data.limit}`;
}
if (count != "") return `LIMIT ${count}`;
return "";
};
const isLatest = (data) => {
if (isCheck(data)) return "";
if (isCheck(data.latest)) return "";
if (data.latest != undefined && data.latest != null && data.latest != "")
return `order by ${data.latest} DESC`;
return "";
};
const isCheck = (data) => {
if (data != undefined && data != null && data != "") return false;
return true;
};
const isKey = (data) => {
if (data != undefined && (data == null || data == "")) return true;
return false;
};
//where clause condition
const whereAnd = (data) => {
if (isCheck(data)) return "";
let setItem = [];
for (const [key, value] of Object.entries(data)) {
if (setItem.length > 0) {
setItem.push(` AND ${key}='${value}'`);
} else {
setItem.push(`${key}='${value}'`);
}
}
return `${setItem.toString()}`;
};
const whereOr = (data) => {
if (isCheck(data)) return "";
let setItem = [];
for (const [key, value] of Object.entries(data)) {
if (setItem.length > 0) {
setItem.push(` OR ${key}='${value}'`);
} else {
setItem.push(`${key}='${value}'`);
}
}
return `${setItem.toString()}`;
};
const whereIs = (data) => {
if (isCheck(data)) return "";
let setItem = [];
for (const [key, value] of Object.entries(data)) {
if (setItem.length > 0) {
setItem.push(` AND ${key} IS ${value}`);
} else {
setItem.push(`${key} IS ${value}`);
}
}
return `${setItem.toString()}`;
};
const whereIn = (data) => {
if (isCheck(data)) return "";
let setItem = [];
for (const [key, value] of Object.entries(data)) {
if (setItem.length > 0) {
setItem.push(` AND ${key} IN (${value.toString()})`);
} else {
setItem.push(`${key} IN (${value.toString()})`);
}
}
return ` ${setItem.toString()}`;
};
const whereNotIn = (data) => {
if (isCheck(data)) return "";
let setItem = [];
for (const [key, value] of Object.entries(data)) {
if (setItem.length > 0) {
setItem.push(` AND ${key} NOT IN (${value.toString()})`);
} else {
setItem.push(`${key} NOT IN (${value.toString()})`);
}
}
return `${setItem.toString()}`;
};
const whereRaw = (data) => {
if (isCheck(data)) return "";
return `${data}`;
};
const onlyTrashed= (data) => {
if (isCheck(data)) return "";
if (data === true ) return " deleted_at IS NOT NULL ";
return "";
};
const whereClause = (data, resData) => {
if (isCheck(data)) return "";
let addCondition = "";
const where_not_in = whereNotIn(data.whereNotIn).replace(/,/g, "");
if (where_not_in != "") addCondition += where_not_in;
const where_in = whereIn(data.whereIn);
if (where_in != "" && where_not_in != "") {
addCondition += ` AND ${where_in}`;
} else if (where_in != "") {
addCondition += where_in;
}
const where_and = whereAnd(data.where).replace(/,/g, "");
if (where_and != "" && (where_in != "" || where_not_in != "")) {
addCondition += ` AND ${where_and}`;
} else if (where_and != "") {
addCondition += where_and;
}
const where_or = whereOr(data.whereOr).replace(/,/g, "");
if (
where_or != "" &&
(where_and != "" || where_in != "" || where_not_in != "")
) {
addCondition += ` OR ${where_or}`;
} else if (where_or != "") {
addCondition += where_or;
}
const where_is = whereIs(data.whereIs).replace(/,/g, "");
if (
where_is != "" &&
(where_or != "" || where_and != "" || where_in != "" || where_not_in != "")
) {
addCondition += ` AND ${where_is}`;
} else if (where_is != "") {
addCondition += where_is;
}
const connect_tb = connect(data.connect, resData);
if (
connect_tb != "" &&
(where_is != "" ||
where_or != "" ||
where_and != "" ||
where_in != "" ||
where_not_in != "")
) {
addCondition += ` AND ${connect_tb}`;
} else if (connect_tb != "") {
addCondition += connect_tb;
}
const where_row = whereRaw(data.whereRaw);
if (
where_row != "" &&
(connect_tb != "" ||
where_is != "" ||
where_or != "" ||
where_and != "" ||
where_in != "" ||
where_not_in != "")
) {
addCondition += ` AND ${where_row}`;
} else if (where_row != "") {
addCondition += 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()}` : "";
};
// on clause condition
const onAnd = (data) => {
if (isCheck(data)) return "";
let setItem = [];
for (const [key, value] of Object.entries(data)) {
if (setItem.length > 0) {
setItem.push(` AND ${key}=${value}`);
} else {
setItem.push(`${key}=${value}`);
}
}
return `${setItem.toString()}`;
};
const onOr = (data) => {
if (isCheck(data)) return "";
let setItem = [];
for (const [key, value] of Object.entries(data)) {
if (setItem.length > 0) {
setItem.push(` OR ${key}='${value}'`);
} else {
setItem.push(`${key}=${value}`);
}
}
return `${setItem.toString()}`;
};
const onIs = (data) => {
if (isCheck(data)) return "";
let setItem = [];
for (const [key, value] of Object.entries(data)) {
if (setItem.length > 0) {
setItem.push(` AND ${key} IS ${value}`);
} else {
setItem.push(`${key} IS ${value}`);
}
}
return `${setItem.toString()}`;
};
const onIn = (data) => {
if (isCheck(data)) return "";
let setItem = [];
for (const [key, value] of Object.entries(data)) {
if (setItem.length > 0) {
setItem.push(` AND ${key} IN (${value.toString()})`);
} else {
setItem.push(`${key} IN (${value.toString()})`);
}
}
return ` ${setItem.toString()}`;
};
const onNotIn = (data) => {
if (isCheck(data)) return "";
let setItem = [];
for (const [key, value] of Object.entries(data)) {
if (setItem.length > 0) {
setItem.push(` AND ${key} NOT IN (${value.toString()})`);
} else {
setItem.push(`${key} NOT IN (${value.toString()})`);
}
}
return `${setItem.toString()}`;
};
const onRaw = (data) => {
if (isCheck(data)) return "";
return `${data}`;
};
const onClause = (data) => {
let addCondition = "";
const on_not_in = onNotIn(data.onNotIn).replace(/,/g, "");
if (on_not_in != "") addCondition += on_not_in;
const on_in = onIn(data.onIn);
if (on_in != "" && on_not_in != "") {
addCondition += ` AND ${on_in}`;
} else if (on_in != "") {
addCondition += on_in;
}
const on_and = onAnd(data.on).replace(/,/g, "");
if (on_and != "" && (on_in != "" || on_not_in != "")) {
addCondition += ` AND ${on_and}`;
} else if (on_and != "") {
addCondition += on_and;
}
const on_or = onOr(data.onOr).replace(/,/g, "");
if (on_or != "" && (on_and != "" || on_in != "" || on_not_in != "")) {
addCondition += ` OR ${on_or}`;
} else if (on_or != "") {
addCondition += on_or;
}
const on_is = onIs(data.onIs).replace(/,/g, "");
if (
on_is != "" &&
(on_or != "" || on_and != "" || on_in != "" || on_not_in != "")
) {
addCondition += ` AND ${on_is}`;
} else if (on_is != "") {
addCondition += on_is;
}
const on_row = onRaw(data.onRaw);
if (
on_row != "" &&
(on_is != "" ||
on_or != "" ||
on_and != "" ||
on_in != "" ||
on_not_in != "")
) {
addCondition += ` AND ${on_row}`;
} else if (on_row != "") {
addCondition += on_row;
}
return addCondition != "" ? `ON ${addCondition.trim()}` : "";
};
//end on
const pagination = (data) => {
if (isCheck(data)) return "";
if (isCheck(data.pagination) || data.pagination == NaN) return "";
if (
isLimit(data) == "" ||
(isCheck(data.pagination) && isLimit(data) == 0)
)
return "";
return `OFFSET ${parseInt(data.pagination>=1 ? data.pagination-1:data.pagination) * parseInt(data.limit)}`;
};
const paginationData = (totalData, data, page = 0) => {
if (isCheck(totalData) && data.pagination < 0) return "";
let pageCountExce =
totalData > 0 ? totalData / data.limit : 0;
let totalPage =
isInt(pageCountExce) == 0
? pageCountExce
: pageCountExce - isInt(pageCountExce) + 1;
let currentPage = data.pagination>0?data.pagination:1;
let pagelenth = [];
for (let index = 1; index <= totalPage; index++) {
pagelenth.push(index);
}
let pageDataLimit=data.limit;
return {pageDataLimit, totalPage, totalData, currentPage, pagelenth };
};
const joinTable = (data) => {
if (data !== undefined && isCheck(data) && data.length <= 0) return "";
let str = " ";
for (const join of data) {
if (isTable(join.table)) return "";
if (
join.type != undefined &&
join.type != null &&
joinType(join.type) != ""
) {
str += ` ${joinType(join.type)} ${join.table} ${onClause(join)}`;
} else {
str += ` JOIN ${join.table} ${onClause(join)}`;
}
}
return str;
};
const withTable = (data) => {
if (data !== undefined && isCheck(data) && data.length <= 0) return "";
let str = " ";
for (const key in data) {
if (Object.hasOwnProperty.call(data, key)) {
const element = data[key];
for (const join of data[key]) {
if (isTable(join.table)) return "";
if (key != undefined && key != null && joinType(key) != "") {
str += ` ${joinType(key)} ${join.table} ${onClause(join)}`;
} else {
str += ` JOIN ${join.table} ${onClause(join)}`;
}
}
}
}
return str;
};
const withdata = (errThrow, result, resData, datas,dbcon=null) => {
let db = dbcon ?? pool;
if(isCheck(datas)) return result(resData);
if (datas.with !== undefined && isCheck(datas.with) && datas.with.length <= 0)
return result(resData);
let totalKeyCount = 0;
for (const _ in datas.with) {
totalKeyCount++;
}
let keyIndex = 0;
for (const key in datas.with) {
console.log(key);
let data = datas.with[key];
db.query(
`SELECT ${selectOption(data)} FROM ${data.table} ${whereClause(
data,
resData
)} ${isLatest(data)} ${isLimit(data)} ${pagination(data)}`,
(err, res) => {
if (err) {
return errThrow(err);
}
if (data!= undefined && (!isCheck(data.pagination) || data.pagination >= 0)) {
db.query(
`SELECT count(*) as totalData FROM ${data.table} ${whereClause(
data,
resData
)} ${isLatest(data)}`,
(err, resp) => {
if (err) {
return errThrow(err);
}
let paginate = paginationData(resp[0].totalData, data);
resData[key] = { res, paginate };
}
);
} else {
resData[key] = res;
}
keyIndex++;
if (keyIndex === totalKeyCount) {
return result(resData);
}
}
);
}
if (keyIndex === totalKeyCount) {
return result(resData);
}
};
const withdataForGet = (errThrow, resData, datas,dbcon=null) => {
let db = dbcon ?? pool;
return new Promise((resolve, reject) => {
if(isCheck(datas)) return resolve(resData);
if (datas.with !== undefined && isCheck(datas.with) && datas.with.length <= 0)
return resolve(resData);
let totalKeyCount = 0;
for (const _ in datas.with) {
totalKeyCount++;
}
let keyIndex = 0;
for (const key in datas.with) {
// console.log(key);
let data = datas.with[key];
db.query(
`SELECT ${selectOption(data)} FROM ${data.table} ${whereClause(
data,
resData
)} ${isLatest(data)} ${isLimit(data)} ${pagination(data)}`,
(err, res) => {
if (err) {
return errThrow(err);
}
if (data!= undefined && (!isCheck(data.pagination) || data.pagination >= 0)) {
db.query(
`SELECT count(*) as totalData FROM ${data.table} ${whereClause(
data,
resData
)} ${isLatest(data)}`,
(err, resp) => {
if (err) {
return errThrow(err);
}
let paginate = paginationData(resp[0].totalData, data);
resData[key] = { res, paginate };
}
);
} else {
resData[key] = res;
}
keyIndex++;
if (keyIndex === totalKeyCount) {
return resolve(resData);
}
}
);
}
if (keyIndex === totalKeyCount) {
return resolve(resData);
}
});
};
const connect = (tbcon, resdata) => {
if (isCheck(tbcon)) return "";
let str = "";
for (const key in tbcon) {
let baseColunm = isCheck(resdata[key]) ? resdata[tbcon[key]] : resdata[key];
let colunm = isCheck(resdata[key]) ? key : tbcon[key];
if (str === "") {
str += `${colunm} = ${baseColunm} `;
} else {
str += `AND ${colunm} = ${baseColunm}`;
}
}
console.log(str);
return str;
};
const joinType = (data) => {
let send = "";
if (data == "belongsTo" || data == "INNER JOIN" || data == "inner join") {
send = "INNER JOIN";
}
if (data == "hasOne" || data == "JOIN" || data == "join") {
send = "JOIN";
}
if (data == "belongsToMany" || data == "LEFT JOIN" || data == "left join") {
send = "LEFT JOIN";
}
if (data == "hasMany" || data == "RIGHT JOIN" || data == "right join") {
send = "RIGHT JOIN";
}
return send;
};
const isInt = (data) => {
return data % 1;
};
const sqlConnect = (pool) => {
if (pool !== undefined && pool !== null && pool !== "") {
pool.getConnection((err, connection) => {
if (err) {
console.log(err.message);
return "kp-mysql-models is not connect database.Please Check You Database Connection!";
}
});
} else {
return "kp-mysql-models is not connect database.Please Check You Database Connection!";
}
};
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 {
constructor(db=""){
this._table= this.constructor.name.toUpperCase()+'s';
this._softDelete=false;
this._connection=db;
}
//start
get(data){
return new Promise((resolve, reject) => {
if (sqlConnect(this._connection)) {
return reject(sqlConnect(this._connection));
}
if (isTable(this._table)) return reject(isTable(this._table));
let response = [];
this._connection.query(
`SELECT ${selectOption(data)} FROM ${this._table} ${whereClause(
data
)} ${isLatest(data)} ${isLimit(data)} ${pagination(data)}`,
async (err, res) => {
if (err) {
return reject(err);
}
if (data!= undefined && (!isCheck(data.pagination) || data.pagination >= 0)) {
this._connection.query(
`SELECT count(*) as totalData FROM ${this._table} ${whereClause(
data
)} ${isLatest(data)}`,
async (err, resp) => {
if (err) {
return reject(err);
}
let paginate = paginationData(resp[0].totalData, data);
for (const iterator of res) {
const item = await withdataForGet(reject, iterator, data,this._connection);
response.push(item);
}
return resolve({ response, paginate });
}
);
} else {
for (const iterator of res) {
const item = await withdataForGet(reject, iterator, data,this._connection);
response.push(item);
}
return resolve(response);
}
}
);
});
};
//start
first(data){
return new Promise((resolve, reject) => {
if (sqlConnect(this._connection)) {
return reject(sqlConnect(this._connection));
}
if (isTable(this._table)) return reject(isTable(this._table));
this._connection.query(
`SELECT ${selectOption(data)} FROM ${this._table} ${whereClause(
data
)} ${isLatest(data)} ${isLimit(data, 1)}`,
(err, res) => {
if (err) {
return reject(err);
}
if (res.length > 0) {
return withdata(reject, resolve, res[0], data,this._connection);
}
return resolve({});
}
);
});
};
//start
dbQuery(sql){
return new Promise((resolve, reject) => {
if (sqlConnect(this._connection)) {
return reject(sqlConnect(this._connection));
}
this._connection.query(sql, function (error, results, fields) {
if (error) return reject(error);
return resolve(results);
});
});
};
dbJoin(data){
return new Promise((resolve, reject) => {
if (sqlConnect(this._connection)) {
return reject(sqlConnect(this._connection));
}
this._connection.query(
`SELECT ${selectOption(data)} FROM ${this._table} ${joinTable(
data.join
)} ${whereClause(data)} ${isLatest(data)} ${isLimit(
data
)} ${pagination(data)}`,
(err, res) => {
if (err) {
return reject(err);
}
if (data!= undefined && (!isCheck(data.pagination) || data.pagination >= 0)) {
this._connection.query(
`SELECT count(*) as totalData FROM ${this._table} ${joinTable(
data.join
)} ${whereClause(data)} ${isLatest(data)}`,
(err, resp) => {
if (err) {
return reject(err);
}
let paginate = paginationData(resp[0].totalData, data);
return resolve({ res, paginate });
}
);
} else {
return resolve(res);
}
}
);
});
};
dbWith(data){
return new Promise((resolve, reject) => {
if (sqlConnect(this._connection)) {
return reject(sqlConnect(this._connection));
}
this._connection.query(
`SELECT ${selectOption(data)} FROM ${this._table} ${withTable(
data.with
)} ${whereClause(data)} ${isLatest(data)} ${isLimit(
data
)} ${pagination(data)}`,
(err, res) => {
if (err) {
return reject(err);
}
if (data!= undefined && (!isCheck(data.pagination) || data.pagination >= 0)) {
this._connection.query(
`SELECT count(*) as totalData FROM ${this._table} ${withTable(
data.with
)} ${whereClause(data)} ${isLatest(data)}`,
(err, resp) => {
if (err) {
return reject(err);
}
let paginate = paginationData(resp[0].totalData, data);
return resolve({ res, paginate });
}
);
} else {
return resolve(res);
}
}
);
});
};
create(data){
return new Promise((resolve, reject) => {
if (sqlConnect(this._connection)) {
return reject(sqlConnect(this._connection));
}
if (isTable(this._table)) return reject(isTable(this._table));
if (data.elements != undefined && data.elements != null) {
let keys = Object.keys(data.elements).toString();
let values = Object.values(data.elements).toString();
this._connection.query(
`INSERT INTO ${this._table} (${keys}) VALUES (${values})`,
(err, res) => {
if (err) {
return reject(err);
}
return resolve(res);
}
);
} else {
return resolve(true);
}
});
};
save(data){
return new Promise((resolve, reject) => {
if (sqlConnect(this._connection)) {
return reject(sqlConnect(this._connection));
}
if (isTable(this._table)) return reject(isTable(this._table));
if (data.elements != undefined && data.elements != null) {
if (
whereClause(data) != undefined &&
whereClause(data) != null &&
whereClause(data) != ""
) {
var sqlQuery = `UPDATE ${this._table} ${getKeyValue(
data.elements,
"SET"
)} ${whereClause(data)}`;
} else {
var sqlQuery = `INSERT INTO ${this._table} ${getKeyValue(
data.elements,
"SET"
)}`;
}
this._connection.query(sqlQuery, (err, res) => {
if (err) {
return reject(err);
}
return resolve(res);
});
} else {
return resolve(true);
}
});
};
update(data){
if (isTable(this._table)) return reject(isTable(this._table));
return new Promise(async (resolve, reject) => {
if (sqlConnect(this._connection)) {
return reject(sqlConnect(this._connection));
}
if (data.elements != undefined && data.elements != null) {
var sqlQuery = `UPDATE ${this._table} ${getKeyValue(
data.elements,
"SET"
)} ${whereClause(data)}`;
this._connection.query(sqlQuery, (err, res) => {
if (err) {
return reject(err);
}
return resolve(res);
});
} else {
return resolve(true);
}
});
};
destroy(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);
}
);
}
});
};
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){
if (isTable(this._table)) return reject(isTable(this._table));
return new Promise((resolve, reject) => {
if (sqlConnect(this._connection)) {
return reject(sqlConnect(this._connection));
}
this._connection.query(`DELETE FROM ${this._table} ${whereClause(data)}`, (err, res) => {
if (err) {
return reject(err);
}
return resolve(res);
});
});
};
trunCate(data){
if (isTable(this._table)) return reject(isTable(this._table));
return new Promise((resolve, reject) => {
if (sqlConnect(this._connection)) {
return reject(sqlConnect(this._connection));
}
this._connection.query(`TRUNCATE TABLE ${this._table}`, (err, res) => {
if (err) {
return reject(err);
}
return resolve(res);
});
});
};
trashed(data){
if (isTable(this._table)) return reject(isTable(this._table));
return new Promise( async(resolve,reject)=>{
if(isCheck(this._softDelete)) return reject("this._softDelete is not true!");
if(isCheck(whereClause(data))) return reject("Where condition is required!");
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);
}
});
}
trashedAll(data){
if (isTable(this._table)) return reject(isTable(this._table));
return new Promise( async(resolve,reject)=>{
if(isCheck(this._softDelete)) return reject("this._softDelete is not true!");
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)=>{
if(isCheck(this._softDelete)) return reject("this._softDelete is not true!");
if(isCheck(whereClause(data))) return reject("Where condition is required!");
this._connection.query(`UPDATE ${this._table} SET deleted_at = NULL ${whereClause(data)}`, (err, res) => {
if (err) {
return reject(err);
}
return resolve(res);
});
});
}
restoreAll(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)=>{
if(isCheck(this._softDelete)) return reject("this._softDelete is not true!");
this._connection.query(`UPDATE ${this._table} SET deleted_at = NULL ${whereClause(data)}`, (err, res) => {
if (err) {
return reject(err);
}
return resolve(res);
});
});
}
}
module.exports = {

@@ -1286,2 +35,3 @@ BaseModels,

dbWith,
isCheck,
};
{
"name": "@krishnapawar/kp-mysql-models",
"version": "1.2.5",
"version": "1.2.6",
"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.",

@@ -14,3 +14,3 @@ "main": "lib/index.js",

"keywords": [
"kp-mysql-models","@krishnapawar/kp-mysql-models","models","mysql","query","builder","model","mysql models","mysql query builder","query builder","mysql builder","kp","krishna pawar","krishnapawar"
"kp-mysql-models","@krishnapawar/kp-mysql-models","mysql-models-builder","models","mysql","query","builder","model","mysql models","mysql query builder","query builder","mysql builder","kp","krishna pawar","krishnapawar"
],

@@ -17,0 +17,0 @@ "author": "krishna pawar <krishnapawar90906@gmail.com>",

@@ -102,2 +102,3 @@ # kp-mysql-models

```JavaScript

@@ -159,2 +160,41 @@ const data = await first({

```
>More Examples (using with key) with hasOne, belognsTo, hasMany, connect
```JavaScript
{
select:['id','first_name','role_id','created_at'],
whereIsNotNull:['last_name'],
with:{
hasOne_appointment:{
select:['id','user_id'],
table:"appointments",
hasOne:{
user_id:'id'
}
},
belongsTo_appointment:{
select:['id','user_id'],
table:"appointments",
belongsTo:{
user_id:'id'
}
},
connect_appointment:{
select:['id','user_id'],
table:"appointments",
connect:{
user_id:'id'
}
},
hasMany_appointment:{
select:['id','user_id'],
table:"appointments",
hasMany:{
user_id:'id'
}
}
}
}
```
>belongsTo and hasOne give single response with single object data and hasMany and connect give array object response with multiple object data
***dbJoin for using mysql all types join***

@@ -296,2 +336,4 @@ ```JavaScript

>exmple 1
```JavaScript

@@ -301,4 +343,2 @@ const { BaseModels } = require("kp-mysql-models");

>exm 1
class User extends BaseModels{

@@ -311,4 +351,7 @@ constructor(){

>OR exm. 2
module.exports= User;
```
>OR exmple 2
```JavaScript
class User extends BaseModels{

@@ -454,7 +497,8 @@ constructor(){

* connect,
* hasOne,
* belongsTo,
* hasMany,
* join,
* dbWith,
* hasOne,
* belongsTo,

@@ -466,2 +510,4 @@ * where,

* whereIs,
* whereIsNull,
* whereIsNotNull,
* whereRaw,

@@ -468,0 +514,0 @@

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc