Last active
August 29, 2015 14:06
-
-
Save SimeonC/37a3e6395b9b2a740552 to your computer and use it in GitHub Desktop.
Custom wrapper for squel, I require this file instead of requiring squel itself. This specifically targets using squel and tedious.
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
squel = require 'squel' | |
squel.useFlavour 'mssql' | |
squel.cls.DefaultQueryBuilderOptions.autoQuoteAliasNames = true | |
squel.cls.DefaultQueryBuilderOptions.tableAliasQuoteCharacter = '"' | |
_parsers = [ | |
# if a field ends with (Date) then parse the value as a date | |
regex: /\(Date\)$/ | |
parse: (value) -> new Date value | |
, | |
# if a field ends with (Exists) then parse to a boolean | |
regex: /\(Exists\)$/ | |
parse: (value) -> value isnt null | |
] | |
squel.mapfields = (columns, fields=Object.keys columns) -> | |
obj = {} | |
for f in fields | |
for _parser in _parsers when f.match _parser.regex | |
columns[f.replace _parser.regex, ''] = Object.merge columns[f], value: _parser.parse columns[f].value | |
f = f.replace _parser.regex, '' | |
if -1 is f.indexOf '.' then obj[f] = columns[f].value | |
# if there is a '.' in the name we want to place this as a sub-object. Do NOT do this if it is a null object. | |
else if columns[f].value isnt null | |
_path = f.split '.' | |
_target = obj | |
for _index in [0..._path.length-1] | |
if not _target[_path[_index]]? then _target[_path[_index]] = {} | |
_target = _target[_path[_index]] | |
_target[_path.last()] = columns[f].value | |
obj | |
module.exports = exports = squel |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment