From c8f89d2442873cdaadd21f1146be25f3ace5f8cc Mon Sep 17 00:00:00 2001 From: reashetyrr <6582364+reashetyrr@users.noreply.github.com> Date: Tue, 25 Feb 2020 15:06:03 +0100 Subject: [PATCH] create fetch push set and query_db --- dbutils.js | 13 +++++ index.js | 131 +++++++++++++++++++++++++++++++++++++++++++--- lib/fetch.js | 20 +++++++ lib/push.js | 52 ++++++++++++++++++ lib/set.js | 38 ++++++++++++++ package-lock.json | 7 ++- package.json | 1 + 7 files changed, 253 insertions(+), 9 deletions(-) create mode 100644 dbutils.js create mode 100644 lib/fetch.js create mode 100644 lib/push.js create mode 100644 lib/set.js diff --git a/dbutils.js b/dbutils.js new file mode 100644 index 0000000..a1086d7 --- /dev/null +++ b/dbutils.js @@ -0,0 +1,13 @@ +module.exports = { + query_database: query_db +}; + +async function query_db(connection, query, params) { + return new Promise((resolve, reject) => { + connection.query(query, params, (err, results) => { + if (err) + return reject(err); + return resolve(results); + }); + }); +} \ No newline at end of file diff --git a/index.js b/index.js index 8216cd3..cb9dc2e 100644 --- a/index.js +++ b/index.js @@ -4,13 +4,13 @@ let connection; const methods = { fetch: require('lib/fetch.js'), set: require('lib/set.js'), - add: require('lib/add.js'), - subtract: require('lib/subtract.js'), + // add: require('lib/add.js'), + // subtract: require('lib/subtract.js'), push: require('lib/push.js'), - delete: require('lib/delete.js'), - has: require('lib/has.js'), - all: require('lib/all.js'), - type: require('lib/type') + // delete: require('lib/delete.js'), + // has: require('lib/has.js'), + // all: require('lib/all.js'), + // type: require('lib/type') }; function init_connection(config) { @@ -62,7 +62,122 @@ module.exports = { version: '1.0.0', init: init_connection, fetch: (key, ops) => { - if (!key) throw new TypeError('No key specified. Need Help? Check Out: discord.gg/plexidev'); + if (!key) throw new TypeError('No key specified'); return arbitrate('fetch', {id: key, ops: ops || {}}); - } + }, + get: function(key, ops) { + if (!key) throw new TypeError('No key specified'); + return arbitrate('fetch', {id: key, ops: ops || {}}); + }, + + /** + * This function sets new data based on a key in the database. + * @param {key} input any string as a key. Also allows for dot notation following the key. + * @param {options} [input={ target: null }] Any options to be added to the request. + * @returns {data} the updated data. + */ + + set: function(key, value, ops) { + if (!key) throw new TypeError('No key specified'); + if (value === undefined) throw new TypeError('No value specified'); + return arbitrate('set', {stringify: true, id: key, data: value, ops: ops || {}}); + }, + + /** + * This function adds a number to a key in the database. (If no existing number, it will add to 0) + * @param {key} input any string as a key. Also allows for dot notation following the key. + * @param {options} [input={ target: null }] Any options to be added to the request. + * @returns {data} the updated data. + */ + + add: function(key, value, ops) { + if (!key) throw new TypeError('No key specified'); + if (isNaN(value)) throw new TypeError('Must specify value to add'); + return arbitrate('add', {id: key, data: value, ops: ops || {}}); + }, + + /** + * This function subtracts a number to a key in the database. (If no existing number, it will subtract from 0) + * @param {key} input any string as a key. Also allows for dot notation following the key. + * @param {options} [input={ target: null }] Any options to be added to the request. + * @returns {data} the updated data. + */ + + subtract: function(key, value, ops) { + if (!key) throw new TypeError('No key specified'); + if (isNaN(value)) throw new TypeError('Must specify value to add'); + return arbitrate('subtract', {id: key, data: value, ops: ops || {}}); + }, + + /** + * This function will push into an array in the database based on the key. (If no existing array, it will create one) + * @param {key} input any string as a key. Also allows for dot notation following the key. + * @param {options} [input={ target: null }] Any options to be added to the request. + * @returns {data} the updated data. + */ + + push: function(key, value, ops) { + if (!key) throw new TypeError('No key specified'); + if (!value && value != 0) throw new TypeError('Must specify value to push'); + return arbitrate('push', {stringify: true, id: key, data: value, ops: ops || {}}); + }, + + + /** + + */ + + /** + * This function will delete an object (or property) in the database. + * @param {key} input any string as a key. Also allows for dot notation following the key, this will delete the prop in the object. + * @param {options} [input={ target: null }] Any options to be added to the request. + * @returns {boolean} if it was a success or not. + */ + + delete: function(key, ops) { + if (!key) throw new TypeError('No key specified'); + return arbitrate('delete', {id: key, ops: ops || {}}); + }, + + /** + * This function returns a boolean indicating whether an element with the specified key exists or not. + * @param {key} input any string as a key. Also allows for dot notation following the key, this will return if the prop exists or not. + * @param {options} [input={ target: null }] Any options to be added to the request. + * @returns {boolean} if it exists. + */ + + has: function(key, ops) { + if (!key) throw new TypeError('No key specified'); + return arbitrate('has', {id: key, ops: ops || {}}); + }, + + includes: function(key, ops) { + if (!key) throw new TypeError('No key specified'); + return arbitrate('has', {id: key, ops: ops || {}}); + }, + + /** + * This function fetches the entire active table + * @param {options} [input={ target: null }] Any options to be added to the request. + * @returns {boolean} if it exists. + */ + + all: function(ops) { + return arbitrate('all', {ops: ops || {}}); + }, + + fetchAll: function(ops) { + return arbitrate('all', {ops: ops || {}}); + }, + + + /* + * Used to get the type of the value. + */ + + + type: function(key, ops) { + if (!key) throw new TypeError('No key specified'); + return arbitrate('type', {id: key, ops: ops || {}}); + }, }; diff --git a/lib/fetch.js b/lib/fetch.js new file mode 100644 index 0000000..7a8110d --- /dev/null +++ b/lib/fetch.js @@ -0,0 +1,20 @@ +// Require Packages +const get = require('lodash/get'); +const query_db = require('dbutils').query_database; + +module.exports = async function(connection, params, options) { + // Fetch Entry + let fetched = await query_db(connection, `SELECT * FROM ${options.table} WHERE ID = (?)`, [params.id]); + if (!fetched) return null; // If empty, return null + fetched = JSON.parse(fetched.json); + try { + fetched = JSON.parse(fetched) + } catch (e) {} + + // Check if target was supplied + if (params.ops.target) + fetched = get(fetched, params.ops.target); // Get prop using dot notation + + // Return data + return fetched; +}; \ No newline at end of file diff --git a/lib/push.js b/lib/push.js new file mode 100644 index 0000000..f918511 --- /dev/null +++ b/lib/push.js @@ -0,0 +1,52 @@ +// Require Packages +const get = require('lodash/get'); +const set = require('lodash/set'); +const query_db = require('dbutils').query_database; + +module.exports = async function(connection, params, options) { + + // Fetch entry + let fetched = await query_db(connection,`SELECT * FROM ${options.table} WHERE ID = (?)`, [params.id]); + + // If not found, create empty row + if (!fetched) { + await query_db(connection, `INSERT INTO ${options.table} (ID,json) VALUES (?,?)`, [params.id, '{}']); + fetched = await query_db(connection,`SELECT * FROM ${options.table} WHERE ID = (?)`,[params.id]); + } + + // Check if a target was supplied + if (params.ops.target) { + fetched = JSON.parse(fetched.json); + try { fetched = JSON.parse(fetched) } catch (e) {} + params.data = JSON.parse(params.data); + if (typeof fetched !== 'object') throw new TypeError('Cannot push into a non-object.'); + let oldArray = get(fetched, params.ops.target); + if (oldArray === undefined) oldArray = []; + else if (!Array.isArray(oldArray)) throw new TypeError('Target is not an array.'); + oldArray.push(params.data); + params.data = set(fetched, params.ops.target, oldArray); + } else { + if (fetched.json === '{}') fetched.json = []; + else fetched.json = JSON.parse(fetched.json); + try { fetched.json = JSON.parse(fetched.json) } catch (e) {} + params.data = JSON.parse(params.data); + if (!Array.isArray(fetched.json)) throw new TypeError('Target is not an array.'); + fetched.json.push(params.data); + params.data = fetched.json; + } + + // Stringify data + params.data = JSON.stringify(params.data); + + // Update entry with new data + await query_db(connection, `UPDATE ${options.table} SET json = (?) WHERE ID = (?)`, [params.data, params.id]); + + // Fetch & return new data + let newData = await query_db(connection, `SELECT * FROM ${options.table} WHERE ID = (?)`, [params.id]).json; + if (newData === '{}') return null; + else { + newData = JSON.parse(newData); + try { newData = JSON.parse(newData) } catch (e) {} + return newData + } +}; \ No newline at end of file diff --git a/lib/set.js b/lib/set.js new file mode 100644 index 0000000..e3887af --- /dev/null +++ b/lib/set.js @@ -0,0 +1,38 @@ +const set = require('lodash/set'); +const query_db = require('dbutils').query_database; + +module.exports = async function(connection, params, options) { + // Fetch entry + let fetched = await query_db(connection, `SELECT * FROM ${options.table} WHERE ID = (?)`, [params.id]); + + // If not found, create empty row + if (!fetched) { + await query_db(connection, `INSERT INTO ${options.table} (ID,json) VALUES (?,?)`, [params.id, '{}']); + fetched = await query_db(connection, `SELECT * FROM ${options.table} WHERE ID = (?)`, [params.id]); + } + + // Parse fetched + fetched = JSON.parse(fetched.json); + try { fetched = JSON.parse(fetched) } catch (e) {} + + // Check if a target was supplied + if (typeof fetched === 'object' && params.ops.target) { + params.data = JSON.parse(params.data); + params.data = set(fetched, params.ops.target, params.data); + } else if (params.ops.target) throw new TypeError('Cannot target a non-object.'); + + // Stringify data + params.data = JSON.stringify(params.data); + + // Update entry with new data + await query_db(connection, `UPDATE ${options.table} SET json = (?) WHERE ID = (?)`, [params.data, params.id]); + + // Fetch & return new data + let newData = await query_db(connection, `SELECT * FROM ${options.table} WHERE ID = (?)`, [params.id]).json; + if (newData === '{}') return null; + else { + newData = JSON.parse(newData); + try { newData = JSON.parse(newData) } catch (e) {} + return newData + } +}; \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index ec84b2d..79ffb09 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,5 +1,5 @@ { - "name": "quick.mysql", + "name": "@reashetyr/quick.mysql", "version": "1.0.0", "lockfileVersion": 1, "requires": true, @@ -24,6 +24,11 @@ "resolved": "https://registry.npmjs.org/isarray/-/isarray-1.0.0.tgz", "integrity": "sha1-u5NdSFgsuhaMBoNJV6VKPgcSTxE=" }, + "lodash": { + "version": "4.17.15", + "resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.15.tgz", + "integrity": "sha512-8xOcRHvCjnocdS5cpwXQXVzmmh5e5+saE2QGoeQmbKmRS6J3VQppPOIt0MnmE+4xlZoumy0GPG0D0MVIQbNA1A==" + }, "mysql": { "version": "2.18.1", "resolved": "https://registry.npmjs.org/mysql/-/mysql-2.18.1.tgz", diff --git a/package.json b/package.json index 4a8ed82..48af9fe 100644 --- a/package.json +++ b/package.json @@ -18,6 +18,7 @@ "author": "Reashetyr", "license": "MIT", "dependencies": { + "lodash": "^4.17.15", "mysql": "^2.18.1" }, "repository": {