-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
90a5bd3
commit c8f89d2
Showing
7 changed files
with
253 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
}); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} | ||
}; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters