Socket
Socket
Sign inDemoInstall

jeefo-mysql

Package Overview
Dependencies
Maintainers
2
Versions
57
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

jeefo-mysql - npm Package Compare versions

Comparing version 0.0.48 to 0.0.49

161

index.js
/* -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.
* File Name : index.js
* Created at : 2021-10-09
* Updated at : 2022-03-12
* Updated at : 2022-03-24
* Author : jeefo

@@ -28,2 +28,4 @@ * Purpose :

retry_interval : 2000,
idle_timeout : 10000,
is_persistent : false,
is_auto_reconnect_enabled : true,

@@ -36,8 +38,6 @@ };

const async_connect = config => {
return new Promise((resolve, reject) => {
const connection = mysql.createConnection(config);
connection.connect(err => err ? reject(err) : resolve(connection));
});
};
const async_connect = config => new Promise((resolve, reject) => {
const connection = mysql.createConnection(config);
connection.connect(err => err ? reject(err) : resolve(connection));
});

@@ -61,3 +61,3 @@ const mock_query = (instance, connection, config) => {

} else {
return new Promise(function(resolve, reject) {
return new Promise((resolve, reject) => {
request.callback = (err, results, fields) => {

@@ -77,5 +77,14 @@ if (err) {

class JeefoMysqlQuery {
constructor(query, values) {
this.query = query;
this.values = values;
}
toString() { return this.query; }
}
class JeefoMySQLConnection {
constructor(table_name, config) {
if (! is.object(config)) throw new TypeError("Invalid argument");
if (!is.object(config)) throw new TypeError("Invalid argument");
this.state = "idle";

@@ -141,4 +150,11 @@ this.queue = new Set();

clearTimeout(this.timeout_id);
this.timeout_id = setTimeout(() => this.destroy(), 10000);
const config = config_map.get(this);
if (!config.is_persistent) {
if (config.idle_timeout) {
clearTimeout(this.timeout_id);
this.timeout_id = setTimeout(() => this.destroy(), config.idle_timeout);
} else {
this.destroy();
}
}

@@ -153,29 +169,18 @@ return result;

where = this.prepare_where(where);
const {values} = where;
where = where.query;
let order = '';
if (is.string(options.order)) {
order = ` ORDER BY ${options.order}`;
}
const order = is.string(options.order) ? ` ORDER BY ${options.order}` : '';
const limit = is.number(options.limit) ? ` LIMIT ${options.limit}` : '';
let limit = '';
if (is.number(options.limit)) {
limit = ` LIMIT ${options.limit}`;
}
const tbl = this.table_name;
const query = `SELECT ${fields} FROM ${tbl}${where}${order}${limit};`;
const {results} = await this.exec(query, where.values);
const tbl = this.table_name;
const query = `SELECT ${fields} FROM ${tbl}${where}${order}${limit};`;
const res = await this.exec(query, values);
if (options.limit === 1) return res.results[0];
return res.results;
return options.limit === 1 ? results[0] : results;
}
async insert(data, return_back) {
const {fields, values} = this.prepare_set(data);
const query = `INSERT INTO ${this.table_name} SET ${fields};`;
const set = this.prepare_set(data);
const query = `INSERT INTO ${this.table_name} SET ${set};`;
const res = await this.exec(query, values);
const res = await this.exec(query, set.values);

@@ -189,42 +194,47 @@ return (

/*
async update(data, where, options) {
const query = `UPDATE ${tbl} SET ${fields};`;
}
*/
async update(data, where, options, return_back) {
const set = this.prepare_set(data);
where = this.prepare_where(where);
if (is.boolean(options)) {
return_back = options;
options = {};
} else {
options = options || {};
}
async delete(where, options = {}) {
where = this.prepare_where(where);
const {values} = where;
where = where.query;
const order = is.string(options.order) ? ` ORDER BY ${options.order}` : '';
const limit = is.number(options.limit) ? ` LIMIT ${options.limit}` : '';
let order = '';
if (is.string(options.order)) {
order = ` ORDER BY ${options.order}`;
}
const tbl = this.table_name;
const query = `UPDATE ${tbl} SET ${set}${where}${order}${limit};`;
await this.exec(query, [...set.values, ...where.values]);
let limit = '';
if (is.number(options.limit)) {
limit = ` LIMIT ${options.limit}`;
if (return_back) {
let {fields} = options;
fields = fields ? this.prepare_fields(fields) : '*';
const query = `SELECT ${fields} FROM ${tbl}${where}${order}${limit};`;
const {results} = await this.exec(query, where.values);
return options.limit === 1 ? results[0] : results;
}
}
const tbl = this.table_name;
await this.exec(`DELETE FROM ${tbl}${where}${order}${limit};`, values);
update_first(data, where, options, return_back) {
return this.update(data, where, {...options, limit: 1}, return_back);
}
async delete_first(where, options = {}) {
async delete(where, options = {}) {
where = this.prepare_where(where);
const {values} = where;
where = where.query;
let order = '';
if (is.string(options.order)) {
order = ` ORDER BY ${options.order}`;
}
const order = is.string(options.order) ? ` ORDER BY ${options.order}` : '';
const limit = is.number(options.limit) ? ` LIMIT ${options.limit}` : '';
const tbl = this.table_name;
const limit = ` LIMIT 1`;
await this.exec(`DELETE FROM ${tbl}${where}${order}${limit};`, values);
const query = `DELETE FROM ${tbl}${where}${order}${limit};`;
await this.exec(query, where.values);
}
delete_first(where, options) {
return this.delete(where, {...options, limit: 1});
}
prepare_fields(fields) {

@@ -237,4 +247,4 @@ if (is.string(fields)) fields = [fields];

const values = [];
const fields = Object.keys(data).map(key => {
let value = data[key];
const fields = [];
for (let [key, value] of Object.entries(data)) {
if (is.object(value) && !(value instanceof Date)) {

@@ -244,14 +254,15 @@ value = JSON.stringify(value);

values.push(value);
return `${mysql.escapeId(key)} = ?`;
}).join(", ");
fields.push(`${mysql.escapeId(key)} = ?`);
}
return {fields, values};
return new JeefoMysqlQuery(fields.join(", "), values);
}
prepare_where(where) {
const values = [];
if (!where) return {query: '', values};
if (!where) return new JeefoMysqlQuery('', []);
const conditions = Object.keys(where).map(key => {
let value = where[key];
const values = [];
const conditions = [];
for (let [key, value] of Object.entries(where)) {
if (is.object(value) && !(value instanceof Date)) {

@@ -261,12 +272,10 @@ value = JSON.stringify(value);

values.push(value);
return `${mysql.escapeId(key)} = ?`;
}).join(" AND ");
conditions.push(`${mysql.escapeId(key)} = ?`);
}
const query = ` WHERE ${conditions}`;
return {query, values};
return new JeefoMysqlQuery(` WHERE ${conditions.join(" AND ")}`, values);
}
async first(where, options) {
options = Object.assign({}, options, {limit: 1});
return await this.select(where, options);
first(where, options) {
return this.select(where, {...options, limit: 1});
}

@@ -277,5 +286,3 @@

const {results: [result]} = await this.exec(`SHOW CREATE TABLE ${tbl}`);
const keys = Object.keys(result);
for (const key of keys) {
const s = result[key];
for (const [, s] of Object.entries(result)) {
if (typeof s === "string" && s.startsWith("CREATE TABLE")) {

@@ -282,0 +289,0 @@ const query = s.replace(AUTO_INC_REGEXP, "AUTO_INCREMENT=0");

{
"name": "jeefo-mysql",
"version": "0.0.48",
"version": "0.0.49",
"description": "Very simple MySQL connection instances for each table. I had annoyed enough for MySQL Deadlocks.",

@@ -5,0 +5,0 @@ "main": "index.js",

/* -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.
* File Name : test.js
* Created at : 2021-10-09
* Updated at : 2021-10-10
* Updated at : 2022-03-24
* Author : jeefo

@@ -20,31 +20,42 @@ * Purpose :

function assert(condition, message) {
if (condition) {
console.log(`ASSERT '${message}' passed.`);
} else {
throw new Error(`ASSERT '${message}' failed.`);
}
}
const assert = (condition, message) => {
if (condition) return console.log(`ASSERT '${message}' passed.`);
throw new Error(`ASSERT '${message}' failed.`);
};
(async () => {
await mysql.config_load(`${process.env.HOME}/configs/database.json`);
const process_db = mysql("processes");
const config = require(`${process.env.HOME}/configs/database.json`);
config.idle_timeout = 0;
await mysql.config(config);
const process_db = mysql("processes");
await process_db.connect();
await process_db.insert({
pid : 321,
command : "ffmpeg something something".split(' '),
});
await process_db.reset();
await process_db.reset();
await process_db.insert({
pid : 123,
command : "ffmpeg something something".split(' '),
});
await process_db.insert({
pid : 123,
command : "ffmpeg something something".split(' '),
});
const results = await process_db.get_all();
let total = await process_db.total();
assert(results.length === total, "results.length === total");
const results = await process_db.get_all();
let total = await process_db.total();
assert(results.length === total, "results.length === total");
const record = await process_db.first({pid: 123});
assert(record !== null, "record !== null");
await process_db.delete_all();
total = await process_db.total();
assert(total === 0, "total length = 0");
const r = await process_db.update_first({
command: 'node index.js'
}, {pid: 123 }, {fields: ["id", "command"]}, true);
})().catch(console.error.bind(console));
assert(r !== null, "r !== null");
assert(record.id === r.id, "record.id !== r.id");
assert(r.command === "node index.js", 'r.command === "node index.js"');
await process_db.delete_all();
total = await process_db.total();
assert(total === 0, "total length = 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