Comparing version 0.1.14-alpha.0 to 0.1.14-alpha.1
@@ -6,23 +6,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 updateInstanceValues (inst, values, prefix) { | ||
static updateInstanceValues (inst, values) { | ||
const entries = Object.entries(values) | ||
for (const [fieldName, fieldValue] of entries) { | ||
const fieldNameStripped = this.parseFieldName(fieldName, prefix) | ||
if (fieldNameStripped) { | ||
try { | ||
inst.setFromDb(fieldNameStripped, fieldValue) | ||
} catch (e) { | ||
// Avoid killing mapper by parsing errors | ||
inst.set(fieldNameStripped, null) | ||
warn(e) | ||
} | ||
for (const [fieldPath, fieldValue] of entries) { | ||
try { | ||
inst.consumeDbValue(fieldPath, fieldValue) | ||
} catch (e) { | ||
// Avoid killing mapper by parsing errors | ||
inst.consumeDbValue(fieldPath, null) | ||
warn(e) | ||
} | ||
@@ -29,0 +17,0 @@ } |
@@ -68,3 +68,3 @@ const { And } = require('./And') | ||
getModelFields (model) { | ||
getModelFields (model, prefix) { | ||
const selection = [] | ||
@@ -79,3 +79,3 @@ const joins = [] | ||
for (const f of fieldNames) { | ||
last.columns.push(f) | ||
last.columns.push(prefix ? `${prefix}__${f}` : f) | ||
} | ||
@@ -99,4 +99,5 @@ } else { | ||
new QueryColumnGroup({ | ||
source: obj.table, | ||
columns: fieldNames | ||
columns: fieldNames, | ||
prefix, | ||
source: obj.table | ||
}) | ||
@@ -103,0 +104,0 @@ ) |
const { And } = require('./And') | ||
const { Count } = require('./Count') | ||
const { getModelName } = require('../models/ModelRegistry') | ||
const { getModel, getModelName } = require('../models/ModelRegistry') | ||
const { ObjectNotFound } = require('../errors') | ||
@@ -37,2 +37,24 @@ const { Q } = require('./QueryCondition') | ||
selectRelated (...values) { | ||
return values.reduce((aggr, relationName) => { | ||
const field = this.model.getField(relationName) | ||
const TargetModel = getModel(field.model) | ||
const [selection, joins] = this.getModelFields(TargetModel, relationName) | ||
return aggr | ||
.join({ | ||
alias: relationName, | ||
conditions: { | ||
[field.keyField]: new QueryColumn({ | ||
source: relationName, | ||
name: TargetModel.pkName | ||
}) | ||
}, | ||
name: TargetModel.table, | ||
side: field.null ? QueryJoin.left : QueryJoin.inner | ||
}) | ||
.join(...joins) | ||
.appendProp('selection', ...selection) | ||
}, this) | ||
} | ||
from (value) { | ||
@@ -51,6 +73,12 @@ return this.target(value) | ||
join (joinSpec) { | ||
return this.initProp('joins', defaultJoins).appendProp( | ||
'joins', | ||
joinSpec instanceof QueryJoin ? joinSpec : new QueryJoin(joinSpec) | ||
join (...joinSpecs) { | ||
return joinSpecs.reduce( | ||
(aggr, joinSpec) => | ||
aggr | ||
.initProp('joins', defaultJoins) | ||
.appendProp( | ||
'joins', | ||
joinSpec instanceof QueryJoin ? joinSpec : new QueryJoin(joinSpec) | ||
), | ||
this | ||
) | ||
@@ -57,0 +85,0 @@ } |
@@ -47,2 +47,13 @@ const camelCase = require('camelcase') | ||
resolveValueInstance (parentInstance, fieldName) { | ||
const value = parentInstance.get(fieldName) | ||
if (value) { | ||
return value | ||
} | ||
const Model = getModel(this.model) | ||
const newInstance = new Model() | ||
parentInstance[fieldName] = newInstance | ||
return newInstance | ||
} | ||
expand () { | ||
@@ -49,0 +60,0 @@ return this.expandedField |
@@ -20,2 +20,4 @@ const { DatabaseModelBase } = require('./DatabaseModelBase') | ||
const FIELD_SEPARATOR = '__' | ||
class DatabaseModel extends DatabaseModelBase { | ||
@@ -90,8 +92,40 @@ static NotFound = ObjectNotFound | ||
setFromDb (fieldName, value) { | ||
const field = this.constructor.getField(fieldName) | ||
/** | ||
* Used by DatabaseMapper to compose data into nested instances. Returns | ||
* field target as an array of [ | ||
* target DatabaseModel instance, | ||
* target Field instance, | ||
* target field name | ||
* ]. The value should be written into the returned instance. | ||
* | ||
* @param {string} fieldPath Field path separated by double underscore | ||
* @returns {[DatabaseModel, Field, string]} | ||
*/ | ||
getFieldTarget (fieldPath) { | ||
const separatorIndex = fieldPath.indexOf(FIELD_SEPARATOR) | ||
if (separatorIndex === -1) { | ||
return [this, this.constructor.getField(fieldPath), fieldPath] | ||
} | ||
const localFieldName = fieldPath.substr(0, separatorIndex) | ||
const field = this.constructor.getField(localFieldName) | ||
if (field.resolveValueInstance instanceof Function) { | ||
return field | ||
.resolveValueInstance(this, localFieldName) | ||
.getFieldTarget( | ||
fieldPath.substr(separatorIndex + FIELD_SEPARATOR.length) | ||
) | ||
} | ||
throw new FieldError( | ||
`Field ${getModelName( | ||
this | ||
)}.${fieldPath} does not accept nested properties` | ||
) | ||
} | ||
consumeDbValue (fieldPath, value) { | ||
try { | ||
this[fieldName] = field.fromDb( | ||
const [inst, field, fieldName] = this.getFieldTarget(fieldPath) | ||
inst[fieldName] = field.fromDb( | ||
this.constructor.db.parseValue(field, value), | ||
this | ||
inst | ||
) | ||
@@ -102,7 +136,6 @@ } catch (e) { | ||
this.constructor | ||
)}.${fieldName}` | ||
)}.${fieldPath}` | ||
throw e | ||
} | ||
throw e | ||
} | ||
return this | ||
} | ||
@@ -109,0 +142,0 @@ |
{ | ||
"name": "djorm", | ||
"version": "0.1.14-alpha.0", | ||
"version": "0.1.14-alpha.1", | ||
"description": "Django like ORM framework", | ||
@@ -39,3 +39,3 @@ "author": "Pavel Žák <pavel@zak.global>", | ||
}, | ||
"gitHead": "882b72cd289b8b4078e09b9e282775f5f62c0e3d" | ||
"gitHead": "84efba14ec3856951576d980b404ec1fa78d2eb3" | ||
} |
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
84375
2927