From d4dc2af9f575650b767f21ba541433f845102cf6 Mon Sep 17 00:00:00 2001 From: Oded Elharar Date: Mon, 7 Nov 2016 12:35:10 +0200 Subject: [PATCH 1/3] adding support for TEXT fields, remove non-standard ascii characters --- lib/defs.js | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/lib/defs.js b/lib/defs.js index 11f3662..3084d53 100644 --- a/lib/defs.js +++ b/lib/defs.js @@ -16,6 +16,7 @@ const type: 'DATE', convert: val => { if (!val) return 'NULL' + if (typeof val === 'string') val = moment(parseInt(val)).format('YYYY-MM-DD') if (typeof val == 'number') val = moment(val).format('YYYY-MM-DD') if (!/\d{4}-\d{2}-\d{2}/.test(val)) return 'NULL' return `"${ val }"` @@ -25,6 +26,7 @@ const type: 'DATETIME', convert: val => { if (!val) return 'NULL' + if (typeof val === 'string') val = moment(parseInt(val)).format('YYYY-MM-DD HH:mm:ss') if (typeof val == 'number') val = moment(val).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 }"` @@ -56,6 +58,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 }"` + } } } From ec73eae3323be2215eeba67c534df6cd1882bdc7 Mon Sep 17 00:00:00 2001 From: Oded Elharar Date: Mon, 7 Nov 2016 17:22:19 +0200 Subject: [PATCH 2/3] Allow date format strings as well as unix time string/number fields --- lib/defs.js | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/lib/defs.js b/lib/defs.js index 3084d53..ba7f639 100644 --- a/lib/defs.js +++ b/lib/defs.js @@ -16,8 +16,10 @@ const type: 'DATE', convert: val => { if (!val) return 'NULL' - if (typeof val === 'string') val = moment(parseInt(val)).format('YYYY-MM-DD') - 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 }"` } @@ -26,8 +28,10 @@ const type: 'DATETIME', convert: val => { if (!val) return 'NULL' - if (typeof val === 'string') val = moment(parseInt(val)).format('YYYY-MM-DD HH:mm:ss') - 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 }"` } From 577dfedeec55f168423b9a3a7ed38521f600e08a Mon Sep 17 00:00:00 2001 From: Oded Elharar Date: Mon, 7 Nov 2016 17:28:19 +0200 Subject: [PATCH 3/3] Add documentation for the new text field to Read.md --- README.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 2209f95..43b37ff 100644 --- a/README.md +++ b/README.md @@ -69,7 +69,8 @@ Create a new `momyfile.json` file like this: "createdAt": "DATETIME", "field1": "number", "field2": "string", - "field3": "boolean" + "field3": "boolean", + "field4": "TEXT" } } } @@ -103,6 +104,7 @@ Currently these native types are supported: - `DATE` - `DATETIME` - `TIME` +- `TEXT` There're also some aliases: