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
Pankaj Patel
committed
Oct 1, 2019
1 parent
8043d10
commit 8c6cf07
Showing
11 changed files
with
245 additions
and
78 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,4 @@ | ||
DB_HOST=localhost | ||
DB_USER=root | ||
DB_PASS= | ||
DB_DATABASE=twitter_clone |
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 |
---|---|---|
|
@@ -61,3 +61,4 @@ typings/ | |
.next | ||
|
||
*.spf | ||
.env |
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
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 @@ | ||
const query = require('../helpers/query'); | ||
const valuesForQuery = require('../helpers/values-for-insert'); | ||
|
||
/** | ||
* @param {} conn MySQL Connection reference | ||
* @param {String} table Table to insert the values | ||
* @param {[String]} columns Array of column names | ||
* @param {[String]} values Array of values for those column names, can be multidientional | ||
*/ | ||
module.exports = async (conn, table, columns, values) => { | ||
const VALUES = valuesForQuery(values) | ||
try { | ||
const user = await query(conn, `INSERT INTO ${table}(${columns.join(', ')}) VALUES ${VALUES};`); | ||
if (user.insertId) { | ||
console.log(user.insertId); | ||
return await query(conn, `SELECT * FROM ${table} WHERE ID=?`, [user.insertId]); | ||
} | ||
return user; | ||
} catch(e) { console.log(e)} | ||
} |
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
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 @@ | ||
/** | ||
* @param {[String]} values Array of values for those column names | ||
*/ | ||
const valueForQuery = (_values) => { | ||
const values = _values.map(item => { | ||
let val; | ||
switch (typeof item) { | ||
case 'number': | ||
return item; | ||
case 'string': | ||
val = item; | ||
break; | ||
default: | ||
return item.toString(); | ||
} | ||
return `'${val}'`; | ||
}) | ||
return `(${values.join(', ')})`; | ||
}; | ||
|
||
/** | ||
* @param {[String]} values Array of values for those column names, can be multidientional | ||
*/ | ||
const valuesForQuery = (values) => { | ||
const value = values[0]; | ||
let VALUES = ''; | ||
if (value instanceof Array) { | ||
VALUES = values.map(valueForQuery).join(', ') | ||
} else { | ||
VALUES = valueForQuery(values); | ||
} | ||
return VALUES; | ||
} | ||
|
||
module.exports = { | ||
valueForQuery, | ||
valuesForQuery, | ||
} |
Oops, something went wrong.