Comparing version 0.1.6-alpha.0 to 0.1.7-alpha.1
let currentConfig = { | ||
name: 'djorm', | ||
apps: [], | ||
databases: {}, | ||
name: 'djorm', | ||
logger: { | ||
@@ -15,14 +15,12 @@ level: 'info', | ||
} | ||
const getSettings = () => currentConfig | ||
const init = async config => { | ||
if (config) { | ||
configure(config) | ||
} | ||
const settings = currentConfig | ||
const init = async () => { | ||
const settings = getSettings() | ||
if (settings.apps) { | ||
require('./init/apps').init(settings) | ||
await require('./init/apps').init(settings) | ||
} | ||
require('./init/logger').init(settings) | ||
await require('./init/logger').init(settings) | ||
if (settings.databases) { | ||
require('./init/databases').init(settings.databases) | ||
await require('./init/databases').init(settings.databases) | ||
} | ||
@@ -32,4 +30,6 @@ } | ||
const shutdown = async () => { | ||
require('./init/apps').shutdown(currentConfig) | ||
const settings = getSettings() | ||
require('./init/apps').shutdown(settings) | ||
require('./init/logger').shutdown() | ||
await require('./init/databases').shutdown() | ||
} | ||
@@ -39,5 +39,5 @@ | ||
configure, | ||
getSettings, | ||
init, | ||
shutdown, | ||
getSettings: () => currentConfig | ||
shutdown | ||
} |
@@ -26,3 +26,3 @@ const { getModelName } = require('../models/ModelRegistry') | ||
try { | ||
inst.set(fieldNameStripped, fieldValue) | ||
inst.setFromDb(fieldNameStripped, fieldValue) | ||
} catch (e) { | ||
@@ -29,0 +29,0 @@ // Avoid killing mapper by parsing errors |
@@ -15,2 +15,3 @@ function formatObject (obj) { | ||
class FieldError extends ModelError {} | ||
class UnknownField extends FieldError {} | ||
class ValueError extends FieldError {} | ||
@@ -99,4 +100,5 @@ | ||
serializeError, | ||
UnknownField, | ||
ValidationError, | ||
ValueError | ||
} |
@@ -12,2 +12,3 @@ const { Field } = require('../models/AttrModel') | ||
...require('./ObjectField'), | ||
...require('./PasswordField'), | ||
...require('./PositiveIntegerField'), | ||
@@ -14,0 +15,0 @@ ...require('./TextField'), |
@@ -13,4 +13,8 @@ const { TextField } = require('./TextField') | ||
} | ||
fromDb (value) { | ||
return this.parse(super.fromDb(value)) | ||
} | ||
} | ||
module.exports = { JsonField } |
const { TrivialField } = require('./TrivialField') | ||
const { Field } = require('../models/AttrModel') | ||
/** Field used for char values */ | ||
class TextField extends TrivialField {} | ||
class TextField extends TrivialField { | ||
static encrypted = new Field({ default: false }) | ||
algorithm = 'aes256' | ||
getSecretKey () { | ||
return require('../config').getSettings().secretKey | ||
} | ||
getSecretKeyDigest () { | ||
return require('crypto') | ||
.createHash('sha256') | ||
.update(this.getSecretKey()) | ||
.digest() | ||
} | ||
encryptValue (value) { | ||
const iv = Buffer.from(this.getSecretKey().substring(0, 16)) | ||
const cipher = require('crypto').createCipheriv( | ||
this.algorithm, | ||
this.getSecretKeyDigest(), | ||
iv | ||
) | ||
const msg = Buffer.concat([cipher.update(value), cipher.final()]) | ||
return `${this.algorithm}:${iv.toString('hex')}:${msg.toString('hex')}` | ||
} | ||
decryptValue (value) { | ||
const [algorithm, ivKey, cipher] = value.split(':') | ||
const decipher = require('crypto').createDecipheriv( | ||
algorithm, | ||
this.getSecretKeyDigest(), | ||
Buffer.from(ivKey, 'hex') | ||
) | ||
return [decipher.update(cipher, 'hex'), decipher.final()].join('') | ||
} | ||
serialize (value) { | ||
if (!this.encrypted || !value) { | ||
return value | ||
} | ||
return this.encryptValue(value) | ||
} | ||
fromDb (value) { | ||
if (!this.encrypted || !value) { | ||
return value | ||
} | ||
return this.decryptValue(value) | ||
} | ||
} | ||
module.exports = { TextField } |
const getAppConfigModule = app => require(app) | ||
const initApp = async app => { | ||
const mod = getAppConfigModule() | ||
if (mod.init) { | ||
await mod.init() | ||
} | ||
} | ||
const initApp = async app => await getAppConfigModule(app).init() | ||
const shutdownApp = async app => await getAppConfigModule(app).shutdown() | ||
const shutdownApp = async app => { | ||
const mod = getAppConfigModule(app) | ||
if (mod.shutdown) { | ||
await mod.shutdown() | ||
} | ||
} | ||
const init = async settings => { | ||
@@ -18,0 +7,0 @@ for (const app of settings.apps) { |
@@ -9,2 +9,7 @@ const init = async databases => { | ||
module.exports = { init } | ||
const shutdown = async () => { | ||
const { disconnect } = require('../db/DatabasePool') | ||
await disconnect() | ||
} | ||
module.exports = { init, shutdown } |
@@ -1,2 +0,2 @@ | ||
const { FieldError } = require('../errors') | ||
const { FieldError, UnknownField } = require('../errors') | ||
const { concatValidators, filterUnique } = require('../filters') | ||
@@ -81,7 +81,8 @@ const { getModelName, registerModel } = require('./ModelRegistry') | ||
) | ||
if (!fieldAttrs) { | ||
return null | ||
if (fieldAttrs && fieldAttrs[1] instanceof FieldModel) { | ||
return fieldAttrs[1] | ||
} | ||
const [, field] = fieldAttrs | ||
return field | ||
throw new UnknownField( | ||
`Unknown field "${fieldName}" for model "${getModelName(this)}"` | ||
) | ||
} | ||
@@ -97,3 +98,7 @@ | ||
const field = this.constructor.getField(fieldName) | ||
return field.getDefault(this) | ||
const defaultValue = field.getDefault(this) | ||
if (defaultValue !== undefined) { | ||
this.set(fieldName, defaultValue) | ||
} | ||
return defaultValue | ||
} | ||
@@ -104,14 +109,3 @@ return value | ||
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 | ||
)}"` | ||
) | ||
} | ||
const field = this.constructor.getField(fieldName) | ||
try { | ||
@@ -133,3 +127,3 @@ this[fieldName] = field.parse(value, this) | ||
for (const [key, value] of entries) { | ||
this.setValue(key, value) | ||
this.set(key, value) | ||
} | ||
@@ -182,2 +176,10 @@ return this | ||
fromDb (value) { | ||
return this.parse(value) | ||
} | ||
serialize (value) { | ||
return value | ||
} | ||
hasDefault () { | ||
@@ -184,0 +186,0 @@ return this.default !== undefined |
@@ -7,3 +7,3 @@ const { DatabaseModelBase } = require('./DatabaseModelBase') | ||
const { ObjectManager } = require('./ObjectManager') | ||
const { ObjectNotFound } = require('../errors') | ||
const { FieldError, ObjectNotFound, UnknownField } = require('../errors') | ||
const { parseFieldObjects } = require('./AttrModel') | ||
@@ -57,12 +57,32 @@ const { Relation } = require('../fields/Relation') | ||
rel (relatedName) { | ||
const field = this.constructor.getField(relatedName) | ||
if (field) { | ||
return field.queryTargetModel(this) | ||
try { | ||
return this.constructor.getField(relatedName).queryTargetModel(this) | ||
} catch (e) { | ||
if (e instanceof UnknownField) { | ||
return getRelationship(this.constructor, relatedName).queryParentModel( | ||
this | ||
) | ||
} | ||
throw e | ||
} | ||
return getRelationship(this.constructor, relatedName).queryParentModel(this) | ||
} | ||
setFromDb (fieldName, value) { | ||
const field = this.constructor.getField(fieldName) | ||
try { | ||
this[fieldName] = field.fromDb(value, this) | ||
} catch (e) { | ||
if (e instanceof FieldError) { | ||
e.message = `${e.message} when processing value for ${getModelName( | ||
this.constructor | ||
)}.${fieldName}` | ||
} | ||
throw e | ||
} | ||
return this | ||
} | ||
async fetchRelationship (fieldName) { | ||
if (!this.get(fieldName)) { | ||
this.setValue( | ||
this.set( | ||
fieldName, | ||
@@ -82,3 +102,3 @@ await this.constructor.getField(fieldName).fetch(this) | ||
values.forEach(([field, value]) => { | ||
this.setValue(field.keyField, value.get(value.constructor.pkName)) | ||
this.set(field.keyField, value.get(value.constructor.pkName)) | ||
}) | ||
@@ -97,3 +117,3 @@ } | ||
if (result.insertId) { | ||
this.setValue(row.model.pkName, result.insertId) | ||
this.set(row.model.pkName, result.insertId) | ||
// Set this back to the cascade ^^ | ||
@@ -161,5 +181,3 @@ inject = { ...inject, [row.model.pkName]: result.insertId } | ||
...aggr, | ||
[key]: field.serialize | ||
? field.serialize(this.get(key)) | ||
: this.get(key) | ||
[key]: field.serialize(this.get(key)) | ||
}), | ||
@@ -166,0 +184,0 @@ {} |
{ | ||
"name": "djorm", | ||
"version": "0.1.6-alpha.0", | ||
"version": "0.1.7-alpha.1", | ||
"description": "Django like ORM framework", | ||
@@ -37,3 +37,3 @@ "author": "Pavel Žák <pavel@zak.global>", | ||
}, | ||
"gitHead": "208cedc10ee05f04e30ac1083a92bfe19a85e6f7" | ||
"gitHead": "83a3f180119b11e3f1966e5e4519c5aba977c82d" | ||
} |
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
55720
62
1979