Skip to content

Instantly share code, notes, and snippets.

@SimeonC SimeonC/

Last active Aug 29, 2015
What would you like to do?
Custom wrapper for squel, I require this file instead of requiring squel itself. This specifically targets using squel and tedious.
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
module.exports = exports = squel
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.