Comparing version 0.1.7-alpha.5 to 0.1.7-alpha.6
const moment = require('moment-timezone') | ||
const { TrivialField } = require('./TrivialField') | ||
const { ValueError } = require('../errors') | ||
@@ -8,9 +9,17 @@ /** Field used for datetime values */ | ||
parse (value) { | ||
if (typeof value === 'string') { | ||
return super.parse(moment(value).toDate()) | ||
if (value instanceof Date) { | ||
return value | ||
} | ||
return super.parse(value) | ||
const parsed = moment(value) | ||
if (!parsed.isValid()) { | ||
throw new ValueError(`Value "${value}" is not a valid Date/Time`) | ||
} | ||
return parsed.toDate() | ||
} | ||
serialize (value) { | ||
return moment(value).toISOString() | ||
} | ||
} | ||
module.exports = { DateTimeField } |
@@ -6,2 +6,3 @@ const { Field } = require('../models/AttrModel') | ||
...require('./CharField'), | ||
...require('./DateField'), | ||
...require('./DateTimeField'), | ||
@@ -8,0 +9,0 @@ ...require('./FileField'), |
const { TextField } = require('./TextField') | ||
const { ValueError } = require('../errors') | ||
@@ -6,3 +7,3 @@ class JsonField extends TextField { | ||
serialize (value) { | ||
toDb (value) { | ||
return typeof value === 'string' ? value : JSON.stringify(value) | ||
@@ -12,3 +13,9 @@ } | ||
parse (value) { | ||
return typeof value === 'string' ? JSON.parse(value) : value | ||
try { | ||
return typeof value === 'string' ? JSON.parse(value) : value | ||
} catch (e) { | ||
const err = new ValueError(e.message) | ||
err.parentError = e | ||
throw err | ||
} | ||
} | ||
@@ -15,0 +22,0 @@ |
const { NestedValidationError } = require('./errors') | ||
const serialize = obj => { | ||
const serialize = (obj, ...args) => { | ||
if (obj instanceof Array) { | ||
return obj.map(serialize) | ||
return obj.map(item => serialize(item, ...args)) | ||
} | ||
return obj && obj.serializeValues ? obj.serializeValues() : obj | ||
return obj && obj.toJson ? obj.toJson(...args) : obj | ||
} | ||
@@ -9,0 +9,0 @@ |
const { AttrModel } = require('../AttrModel') | ||
const { CharField, DateTimeField } = require('../../fields') | ||
const { CharField, DateField } = require('../../fields') | ||
describe('AttrModel', () => { | ||
it('serializeValues filters out private values', () => { | ||
it('toJson filters out private values', () => { | ||
class User extends AttrModel { | ||
static name = new CharField() | ||
static password = new CharField({ private: true }) | ||
static dateOfBirth = new DateTimeField() | ||
static dateOfBirth = new DateField() | ||
} | ||
@@ -14,9 +14,27 @@ const user = new User({ | ||
password: '0fb08', | ||
dateOfBirth: new Date(1994, 6, 22) | ||
dateOfBirth: new Date(Date.UTC(1994, 6, 22)) | ||
}) | ||
expect(user.serializeValues()).toEqual({ | ||
expect(user.toJson()).toEqual({ | ||
name: 'Jon', | ||
dateOfBirth: new Date(1994, 6, 22) | ||
dateOfBirth: '1994-07-22' | ||
}) | ||
}) | ||
it('toJson includes serialized values given includePrivate is true', () => { | ||
class User extends AttrModel { | ||
static name = new CharField() | ||
static password = new CharField({ private: true }) | ||
static dateOfBirth = new DateField() | ||
} | ||
const user = new User({ | ||
name: 'Jon', | ||
password: '0fb08', | ||
dateOfBirth: '1994-07-22' | ||
}) | ||
expect(user.toJson(true)).toEqual({ | ||
name: 'Jon', | ||
password: '0fb08', | ||
dateOfBirth: '1994-07-22' | ||
}) | ||
}) | ||
}) |
@@ -129,12 +129,13 @@ const { FieldError, UnknownField } = require('../errors') | ||
serializeValues () { | ||
return this.constructor.fieldObjects | ||
.filter(([key, field]) => !field.private) | ||
.reduce( | ||
(aggr, [key, field]) => ({ | ||
...aggr, | ||
[key]: this.get(key) | ||
}), | ||
{} | ||
) | ||
toJson (includePrivate = false) { | ||
return this.constructor.fieldObjects.reduce((aggr, [key, field]) => { | ||
if (field.private && !includePrivate) { | ||
return aggr | ||
} | ||
const value = field.serialize(this.get(key)) | ||
if (typeof value !== 'undefined') { | ||
aggr[key] = value | ||
} | ||
return aggr | ||
}, {}) | ||
} | ||
@@ -177,2 +178,6 @@ | ||
toDb (value) { | ||
return this.serialize(value) | ||
} | ||
serialize (value) { | ||
@@ -179,0 +184,0 @@ return value |
@@ -177,3 +177,3 @@ const { DatabaseModelBase } = require('./DatabaseModelBase') | ||
...aggr, | ||
[key]: field.serialize(this.get(key)) | ||
[key]: field.toDb(this.get(key)) | ||
}), | ||
@@ -180,0 +180,0 @@ {} |
{ | ||
"name": "djorm", | ||
"version": "0.1.7-alpha.5", | ||
"version": "0.1.7-alpha.6", | ||
"description": "Django like ORM framework", | ||
@@ -38,3 +38,3 @@ "author": "Pavel Žák <pavel@zak.global>", | ||
}, | ||
"gitHead": "480e131ebc3b8dc0a96fd8d73fe559797a77b56a" | ||
"gitHead": "841cb63c8b0e08fcff0856d967770488dfcfb280" | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
59919
65
2138