Comparing version 0.1.17-alpha.2 to 0.1.18-alpha.0
@@ -172,3 +172,3 @@ const { And } = require('./And') | ||
.first() | ||
return result.__djorm_cnt | ||
return result?.__djorm_cnt || 0 | ||
} | ||
@@ -175,0 +175,0 @@ |
@@ -9,4 +9,12 @@ function formatObject (obj) { | ||
class DjormError extends Error {} | ||
class NotImplemented extends Error {} | ||
class DjormError extends Error { | ||
serialize () { | ||
return { | ||
code: this.code, | ||
message: this.message | ||
} | ||
} | ||
} | ||
class NotImplemented extends DjormError {} | ||
class ConfigError extends DjormError {} | ||
@@ -21,9 +29,2 @@ class ModelError extends DjormError {} | ||
code = 'validation-error' | ||
serialize () { | ||
return { | ||
code: this.code, | ||
message: this.message | ||
} | ||
} | ||
} | ||
@@ -30,0 +31,0 @@ |
@@ -5,2 +5,3 @@ const camelCase = require('camelcase') | ||
const { getModel, getModelName, SELF } = require('../models/ModelRegistry') | ||
const { isNullish } = require('../values') | ||
const { PositiveIntegerField } = require('./PositiveIntegerField') | ||
@@ -102,7 +103,18 @@ const { Relation } = require('./Relation') | ||
return await model.objects.get({ | ||
[model.pkName]: inst.get(this.keyField) | ||
[model.pkName]: this.getForeignKeyValue(inst) | ||
}) | ||
} | ||
getForeignKeyValue (inst) { | ||
return inst.get(this.keyField) | ||
} | ||
isNull (value, inst, fieldName) { | ||
return ( | ||
super.isNull(value, inst, fieldName) && | ||
isNullish(this.getForeignKeyValue(inst), inst, this.keyFieldName) | ||
) | ||
} | ||
} | ||
module.exports = { ForeignKey } |
@@ -5,2 +5,3 @@ const { Field } = require('../models/AttrModel') | ||
...require('./ArrayField'), | ||
...require('./AutoField'), | ||
...require('./BooleanField'), | ||
@@ -16,2 +17,3 @@ ...require('./CharField'), | ||
...require('./ObjectField'), | ||
...require('./ObjectArrayField'), | ||
...require('./PasswordField'), | ||
@@ -18,0 +20,0 @@ ...require('./PositiveIntegerField'), |
@@ -0,1 +1,2 @@ | ||
const { Field } = require('../models/AttrModel') | ||
const { TrivialField } = require('./TrivialField') | ||
@@ -6,2 +7,4 @@ const { ValueError } = require('../errors') | ||
class IntegerField extends TrivialField { | ||
static autoIncrement = new Field({ default: false }) | ||
parse (value) { | ||
@@ -8,0 +11,0 @@ if (value) { |
@@ -6,2 +6,4 @@ const { FieldValidationError } = require('../errors') | ||
static choices = new Field() | ||
static unique = new Field({ default: false }) | ||
static primary = new Field({ default: false }) | ||
db = true | ||
@@ -8,0 +10,0 @@ indexable = true |
@@ -24,29 +24,9 @@ const { DatabaseModel } = require('../DatabaseModel') | ||
it('instance toString returns model name with new tag given there are no props to show', () => { | ||
it('instance toString returns model name with new tag', () => { | ||
class User extends DatabaseModel { | ||
static id = new PositiveIntegerField() | ||
} | ||
expect(String(User.from({}))).toEqual('User#{}') | ||
expect(String(User.from({}))).toEqual('User#(new)') | ||
}) | ||
it('instance toString returns model name with new tag given all props are empty', () => { | ||
class User extends DatabaseModel { | ||
static id = new PositiveIntegerField() | ||
static firstName = new CharField() | ||
static lastName = new CharField() | ||
} | ||
expect(String(User.from({}))).toEqual('User#{}') | ||
}) | ||
it('instance toString returns model name with new tag given all props are empty', () => { | ||
class User extends DatabaseModel { | ||
static id = new PositiveIntegerField() | ||
static firstName = new CharField() | ||
static lastName = new CharField() | ||
} | ||
expect(String(User.from({ firstName: 'John', lastName: 'Green' }))).toEqual( | ||
'User#{"firstName":"John","lastName":"Green"}' | ||
) | ||
}) | ||
it('instance toString returns model name with pk value', () => { | ||
@@ -87,3 +67,3 @@ class User extends DatabaseModel { | ||
it('instance toString returns meta model name with props given there is no primary key', () => { | ||
it('instance toString returns meta model name with new tag', () => { | ||
class User extends DatabaseModel { | ||
@@ -106,19 +86,4 @@ static id = new PositiveIntegerField() | ||
) | ||
).toEqual( | ||
'User#{"name":"John","password":"aes256:646a6f726d2d7365637265742d6b6579:7cf8aef56dc2df2dd3f9b80bdbff87ff"}' | ||
) | ||
).toEqual('User#(new)') | ||
}) | ||
it('instance toString returns meta model name with new tag given there is no primary key and props are empty', () => { | ||
class User extends DatabaseModel { | ||
static id = new PositiveIntegerField() | ||
static name = new CharField() | ||
static password = new TextField({ encrypted: true }) | ||
static meta = class { | ||
static modelName = 'User' | ||
} | ||
} | ||
expect(String(User.from({}))).toEqual('User#{}') | ||
}) | ||
}) |
const { concatValidators, filterUnique } = require('../filters') | ||
const { FieldError, FieldValidationError, UnknownField } = require('../errors') | ||
const { getModelName, registerModel } = require('./ModelRegistry') | ||
const { isNullish } = require('../values') | ||
@@ -124,2 +125,14 @@ let FieldModel = null | ||
getValues () { | ||
return this.constructor.fieldObjects | ||
.filter(([key, field]) => !field.private) | ||
.reduce((aggr, [key, field]) => { | ||
const value = this.get(key) | ||
if (typeof value !== 'undefined') { | ||
aggr[key] = value | ||
} | ||
return aggr | ||
}, {}) | ||
} | ||
setValues (params = {}) { | ||
@@ -223,8 +236,8 @@ const entries = Object.entries(params) | ||
/** Given this field has a validator, try to run it as a callback. Callback | ||
* will receive field value, the model instance, and field name as | ||
* arguments. | ||
*/ | ||
async validateValue (value, inst, fieldName) { | ||
if (!this.null && value === null) { | ||
isNull (value, inst, fieldName) { | ||
return isNullish(value) | ||
} | ||
validateNullValue (value, inst, fieldName) { | ||
if (this.isNull(value, inst, fieldName)) { | ||
throw new FieldValidationError( | ||
@@ -238,2 +251,12 @@ inst, | ||
} | ||
} | ||
/** Given this field has a validator, try to run it as a callback. Callback | ||
* will receive field value, the model instance, and field name as | ||
* arguments. | ||
*/ | ||
async validateValue (value, inst, fieldName) { | ||
if (!this.null && !this.autoIncrement) { | ||
this.validateNullValue(value, inst, fieldName) | ||
} | ||
return this.validator ? await this.validator(value, inst, fieldName) : null | ||
@@ -240,0 +263,0 @@ } |
@@ -272,3 +272,3 @@ const { DatabaseModelBase } = require('./DatabaseModelBase') | ||
toString () { | ||
return `${this.constructor}#${this.pk || JSON.stringify(this.toJson())}` | ||
return `${this.constructor}#${this.pk || '(new)'}` | ||
} | ||
@@ -275,0 +275,0 @@ } |
@@ -21,2 +21,4 @@ const { ModelError } = require('../errors') | ||
const getAllModels = () => Object.values(models) | ||
const getAllRelationships = () => Object.values(refs) | ||
const getModels = () => models | ||
@@ -96,2 +98,4 @@ const getRelationships = () => refs | ||
clearModels, | ||
getAllModels, | ||
getAllRelationships, | ||
getModel, | ||
@@ -98,0 +102,0 @@ getModelName, |
{ | ||
"name": "djorm", | ||
"version": "0.1.17-alpha.2", | ||
"version": "0.1.18-alpha.0", | ||
"description": "Django like ORM framework", | ||
@@ -9,2 +9,5 @@ "author": "Pavel Žák <pavel@zak.global>", | ||
"main": "index.js", | ||
"engines": { | ||
"node": ">=16" | ||
}, | ||
"standard": { | ||
@@ -40,3 +43,3 @@ "env": [ | ||
}, | ||
"gitHead": "16fe9590ba4e5683bcec9bec8f9453f982dac9b8" | ||
"gitHead": "2abd6e06437504b23c9cbb6177746ffbcbdb2d46" | ||
} |
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
87067
77
3026