Skip to content

Commit

Permalink
Merge pull request #2 from odedelharar/master
Browse files Browse the repository at this point in the history
adding support for TEXT fields, remove non-standard ascii characters
  • Loading branch information
cognitom committed Nov 7, 2016
2 parents c8859f3 + 577dfed commit 1211199
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ Create a new `momyfile.json` file like this:
"createdAt": "DATETIME",
"field1": "number",
"field2": "string",
"field3": "boolean"
"field3": "boolean",
"field4": "TEXT"
}
}
}
Expand Down Expand Up @@ -103,6 +104,7 @@ Currently these native types are supported:
- `DATE`
- `DATETIME`
- `TIME`
- `TEXT`

There're also some aliases:

Expand Down
21 changes: 19 additions & 2 deletions lib/defs.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ const
type: 'DATE',
convert: val => {
if (!val) return 'NULL'
if (typeof val == 'number') val = moment(val).format('YYYY-MM-DD')
if (typeof val === 'string' || typeof val === 'number') {
var m = moment(val).isValid()? moment(val) : moment(val, 'x')
val = m.format('YYYY-MM-DD')
}
if (!/\d{4}-\d{2}-\d{2}/.test(val)) return 'NULL'
return `"${ val }"`
}
Expand All @@ -25,7 +28,10 @@ const
type: 'DATETIME',
convert: val => {
if (!val) return 'NULL'
if (typeof val == 'number') val = moment(val).format('YYYY-MM-DD HH:mm:ss')
if (typeof val === 'string' || typeof val === 'number') {
var m = moment(val).isValid()? moment(val) : moment(val, 'x')
val = m.format('YYYY-MM-DD HH:mm:ss')
}
if (!/\d{4}-\d{2}-\d{2} \d{2}:\d{2}(:\d{2})?/.test(val)) return 'NULL'
return `"${ val }"`
}
Expand Down Expand Up @@ -56,6 +62,17 @@ const
.replace(/"/g, '\\"') // escape double quotations
return `"${ val }"`
}
},
'TEXT': {
type: 'TEXT',
convert: val => {
val = (val || '')
.toString()
.replace(/\\/g, '\\\\') // escape backslashs
.replace(/"/g, '\\"') // escape double quotations
.replace(/[^\x00-\x7F]/g, "") // remove non ascii characters
return `"${ val }"`
}
}
}

Expand Down

0 comments on commit 1211199

Please sign in to comment.