knex_cleandata v1.2.3
Purpose
To be able to quickly render a knex post, and match whatever value that you are trying to post to your database
Directions
See example here - example
- Install the package
npm install knex_cleandata
- Instantiate it in a javascript file
const {knexCleanData} = require("knex_cleandata")
- Pass in object configuration
1. knex database
2. table name
3. callback functions (specific ones that you want)
See example here - [example](https://github.com/lezzles11/knex_cleandata/tree/master/callbackExample.js)
4. object to post
let data = await cleanKnexPost.init({ knex: knex, table: "datetime", callbacks: callbacks, object: getObject })
- Enjoy!
CleanKnexPost
Kind: global class
Date: 2022-02-16
- CleanKnexPost
- new CleanKnexPost()
- .init(options) ⇒ any
- .trimArr(arr) ⇒ any
- .trimObj() ⇒ object
- .handleCallbacks(callbacks, columnName, value) ⇒ any
- .finalClean(knex, table, obj) ⇒ any
- .cleanPostObject(knex, table, checkObj, callbacks) ⇒ any
- .getTableInfo(knex, table, type) ⇒ any
- .dataExistsAndMatches(knex, table, checkObj) ⇒ any
- .formatObject(colName, object) ⇒ any
- .formatNumber(colName, number) ⇒ any
- .formatString(colName, string, callbacks) ⇒ any
- .formatBoolean(colName, bool) ⇒ any
new CleanKnexPost()
let cleanKnexPost = new CleanKnexPost()
cleanKnexPost.init(options) ⇒ any
init(options)
Kind: instance method of CleanKnexPost
Date: 2022-02-16
Param | Type |
---|---|
options | any |
cleanKnexPost.trimArr(arr) ⇒ any
Trims value of object if it's a string
Kind: instance method of CleanKnexPost
Date: 2022-02-15
Param | Type |
---|---|
arr | any |
cleanKnexPost.trimObj() ⇒ object
this.trimObj(obj) - cleans the object (values)
Kind: instance method of CleanKnexPost
Date: 2022-02-16
cleanKnexPost.handleCallbacks(callbacks, columnName, value) ⇒ any
Kind: instance method of CleanKnexPost
Date: 2022-02-16
Param | Type |
---|---|
callbacks | any |
columnName | any |
value | any |
Example
this.handleCallbacks({
"importance": function(value) {
if column is importance, then
return 3
}
}, "importance", 3)
// returns "high"
cleanKnexPost.finalClean(knex, table, obj) ⇒ any
final step before returning object (removes NaN if supposed to be a number)
Kind: instance method of CleanKnexPost
Date: 2022-02-16
Param | Type |
---|---|
knex | any |
table | any |
obj | any |
cleanKnexPost.cleanPostObject(knex, table, checkObj, callbacks) ⇒ any
- Trims object
- If it has id, will delete it
- Gets table (only matches with values that has table)
- Gets keys from that table
- Loops through array
- If there is a callback passed in and the key matches the column, then it will assign that to the new object
- If the column type doesn't match the column value
Kind: instance method of CleanKnexPost
Date: 2022-02-16
Param | Type | Default |
---|---|---|
knex | any | |
table | any | |
checkObj | any | |
callbacks | any | {} |
cleanKnexPost.getTableInfo(knex, table, type) ⇒ any
- Has a dictionary -
- Gets knex(table).columnInfo()
- if property isn't created
- Push it into array called tableColumns
- If third parameter is object, will return key and value pairs of column: type. Else, if third parameter is array, will return an array of the key values
Kind: instance method of CleanKnexPost
Date: 2022-02-16
Param | Type | Default |
---|---|---|
knex | any | |
table | any | |
type | any | " object" |
Example
this.getTableInfo(knex, "hourglass", "object")
// returns {emails: string... }
cleanKnexPost.dataExistsAndMatches(knex, table, checkObj) ⇒ any
- Deletes id from the object, if we are posting
- Grabs table columns
- Loops through the table column array
- If the object has that property and the value is not ""
- Append it to newObj
- Return newObj
Kind: instance method of CleanKnexPost
Date: 2022-02-16
Param | Type |
---|---|
knex | any |
table | any |
checkObj | any |
Example
this.dataExistsAndMatches(knex, "hourglass", {
email: "hello"
})
// output:
this.dataExistsAndMatches(knex, "hourglass", {
{
start: '11/11/2011',
edit: '2011/11/11',
end: '2021-12-03'
}
})
// output { start: '11/11/2011', edit: '2011/11/11', end: '2021-12-03' }
cleanKnexPost.formatObject(colName, object) ⇒ any
cleanData.formatObject(colName, object) Will format date for us
Kind: instance method of CleanKnexPost
Date: 2022-02-16
Param | Type |
---|---|
colName | any |
object | any |
cleanKnexPost.formatNumber(colName, number) ⇒ any
formatNumber
Kind: instance method of CleanKnexPost
Date: 2022-02-16
Param | Type |
---|---|
colName | any |
number | any |
cleanKnexPost.formatString(colName, string, callbacks) ⇒ any
formatString
Kind: instance method of CleanKnexPost
Date: 2022-02-16
Param | Type | Default |
---|---|---|
colName | any | |
string | any | |
callbacks | any | {} |
cleanKnexPost.formatBoolean(colName, bool) ⇒ any
formatBoolean
Kind: instance method of CleanKnexPost
Date: 2022-02-16
Param | Type |
---|---|
colName | any |
bool | any |