Comparing version 0.1.5-alpha.4 to 0.1.5-alpha.5
@@ -28,2 +28,3 @@ let currentConfig = { | ||
shutdown, | ||
getSettings: () => currentConfig, | ||
get settings () { | ||
@@ -30,0 +31,0 @@ return currentConfig |
@@ -5,2 +5,11 @@ const { getModelName } = require('../models/ModelRegistry') | ||
class DatabaseMapper extends Transform { | ||
static parseFieldName (fieldName, prefix) { | ||
if (fieldName.startsWith(prefix)) { | ||
return fieldName.substr(prefix.length) | ||
} else if (!fieldName.includes('__')) { | ||
return fieldName | ||
} | ||
return null | ||
} | ||
static createMapper = Model => { | ||
@@ -11,14 +20,19 @@ if (!Model) { | ||
const prefix = `${getModelName(Model)}__` | ||
const prefixLength = prefix.length | ||
return item => | ||
new Model( | ||
Object.entries(item).reduce((aggr, [fieldName, fieldValue]) => { | ||
if (fieldName.startsWith(prefix)) { | ||
aggr[fieldName.substr(prefixLength)] = fieldValue | ||
} else if (!fieldName.includes('__')) { | ||
aggr[fieldName] = fieldValue | ||
return item => { | ||
const inst = new Model() | ||
const entries = Object.entries(item) | ||
for (const [fieldName, fieldValue] of entries) { | ||
const fieldNameStripped = this.parseFieldName(fieldName, prefix) | ||
if (fieldNameStripped) { | ||
try { | ||
inst.set(fieldNameStripped, fieldValue) | ||
} catch (e) { | ||
// Avoid killing mapper by parsing errors | ||
inst.set(fieldNameStripped, null) | ||
// @TODO: Warn about this error! | ||
} | ||
return aggr | ||
}, {}) | ||
) | ||
} | ||
} | ||
return inst | ||
} | ||
} | ||
@@ -25,0 +39,0 @@ |
@@ -0,1 +1,3 @@ | ||
const moment = require('moment') | ||
const { ComparisonOperator } = require('./ComparisonOperator') | ||
@@ -38,3 +40,10 @@ const { QueryError } = require('./errors') | ||
formatDate (value) { | ||
return `'${moment(value).format('YYYY-MM-DDTHH:mm:ss.SSS')}'` | ||
} | ||
formatValue (value) { | ||
if (value instanceof Date) { | ||
return this.formatDate(value) | ||
} | ||
if (typeof value === 'number') { | ||
@@ -41,0 +50,0 @@ return String(value) |
@@ -10,2 +10,3 @@ const { Field } = require('../models/AttrModel') | ||
...require('./IntegerField'), | ||
...require('./JsonField'), | ||
...require('./ObjectField'), | ||
@@ -12,0 +13,0 @@ ...require('./PositiveIntegerField'), |
@@ -101,4 +101,15 @@ const { FieldError } = require('../errors') | ||
set (fieldName, value) { | ||
return this.setValue(fieldName, value) | ||
} | ||
setValue (fieldName, value) { | ||
const field = this.constructor.fields[fieldName] | ||
if (!(field instanceof FieldModel)) { | ||
throw new Error( | ||
`Unknown key "${fieldName}" for model "${getModelName( | ||
this.constructor | ||
)}"` | ||
) | ||
} | ||
try { | ||
@@ -114,16 +125,9 @@ this[fieldName] = field.parse(value, this) | ||
} | ||
return this | ||
} | ||
setValues (params = {}) { | ||
const fields = this.constructor.fields | ||
const entries = Object.entries(params) | ||
for (const [key, value] of entries) { | ||
const field = fields[key] | ||
if (field instanceof FieldModel) { | ||
this.setValue(key, value) | ||
} else { | ||
throw new Error( | ||
`Unknown key "${key}" for model "${getModelName(this.constructor)}"` | ||
) | ||
} | ||
this.setValue(key, value) | ||
} | ||
@@ -130,0 +134,0 @@ } |
@@ -11,3 +11,3 @@ const { DatabaseModelBase } = require('./DatabaseModelBase') | ||
const { Update } = require('../db/Update') | ||
const { getModelName, getRelationship } = require('./ModelRegistry') | ||
const { isAbstract, getModelName, getRelationship } = require('./ModelRegistry') | ||
@@ -132,21 +132,28 @@ class DatabaseModel extends DatabaseModelBase { | ||
serializeDbValues () { | ||
let values = [] | ||
const fields = [] | ||
let obj = this.constructor | ||
do { | ||
values = values.concat({ | ||
model: obj, | ||
values: parseFieldObjects(obj) | ||
.filter(([key, field]) => field.db) | ||
.reduce( | ||
(aggr, [key, field]) => ({ | ||
...aggr, | ||
[key]: this.get(key) | ||
}), | ||
{} | ||
) | ||
}) | ||
const values = parseFieldObjects(obj) | ||
.filter(([key, field]) => field.db) | ||
.reduce( | ||
(aggr, [key, field]) => ({ | ||
...aggr, | ||
[key]: field.serialize | ||
? field.serialize(this.get(key)) | ||
: this.get(key) | ||
}), | ||
{} | ||
) | ||
if (isAbstract(obj)) { | ||
fields[0].values = { ...fields[0].values, ...values } | ||
} else { | ||
fields.unshift({ | ||
model: obj, | ||
values | ||
}) | ||
} | ||
obj = Object.getPrototypeOf(obj) | ||
} while (obj && obj !== DatabaseModel && (!obj.meta || !obj.meta.abstract)) | ||
return values.reverse() | ||
} while (obj && obj !== DatabaseModel) | ||
return fields | ||
} | ||
@@ -153,0 +160,0 @@ } |
@@ -67,2 +67,7 @@ const { ModelError } = require('../errors') | ||
const isAbstract = model => { | ||
const meta = Object.getOwnPropertyDescriptor(model, 'meta') | ||
return Boolean(meta && meta.value && meta.value.abstract) | ||
} | ||
module.exports = { | ||
@@ -75,4 +80,5 @@ clearModels, | ||
getRelationships, | ||
isAbstract, | ||
registerModel, | ||
unregisterModel | ||
} |
{ | ||
"name": "djorm", | ||
"version": "0.1.5-alpha.4", | ||
"version": "0.1.5-alpha.5", | ||
"description": "> TODO: description", | ||
@@ -35,3 +35,3 @@ "author": "Pavel Žák <pavel@zak.global>", | ||
}, | ||
"gitHead": "6367b21e84590d40d2ea9b14bf25f19ec7d4ce85" | ||
"gitHead": "aeb148038c85942671f80ba6090cf2b803b6ec1e" | ||
} |
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
49283
54
1750