Skip to content
Snippets Groups Projects
Commit ff402877 authored by Tamlyn Rhodes's avatar Tamlyn Rhodes
Browse files

fix(base-model): support graph inserts

Allow graph inserts by allowing proxy to set #special properties
Make relation properties settable without being in the schema
Fall back to inserting a model if updating fails which allows setting the ID to a known value.
parent a1a7ae62
No related branches found
No related tags found
No related merge requests found
...@@ -26,7 +26,7 @@ class BaseModel extends Model { ...@@ -26,7 +26,7 @@ class BaseModel extends Model {
const handler = { const handler = {
set: (obj, prop, value) => { set: (obj, prop, value) => {
if (this.constructor.jsonSchema.properties[prop]) { if (this.isSettable(prop)) {
obj[prop] = value obj[prop] = value
return true return true
} }
...@@ -84,6 +84,16 @@ class BaseModel extends Model { ...@@ -84,6 +84,16 @@ class BaseModel extends Model {
return baseSchema return baseSchema
} }
isSettable(prop) {
const special = ['#id', '#ref']
return (
special.includes(prop) ||
this.constructor.jsonSchema.properties[prop] ||
(this.constructor.relationMappings &&
this.constructor.relationMappings[prop])
)
}
$beforeInsert() { $beforeInsert() {
this.id = this.id || uuid.v4() this.id = this.id || uuid.v4()
this.created = new Date().toISOString() this.created = new Date().toISOString()
...@@ -99,7 +109,10 @@ class BaseModel extends Model { ...@@ -99,7 +109,10 @@ class BaseModel extends Model {
saved = await this.constructor saved = await this.constructor
.query() .query()
.patchAndFetchById(this.id, this.toJSON()) .patchAndFetchById(this.id, this.toJSON())
} else { }
if (!saved) {
// either model has no ID or the ID was not found in the database
saved = await this.constructor.query().insert(this.toJSON()) saved = await this.constructor.query().insert(this.toJSON())
} }
logger.info(`Saved ${this.constructor.name} with UUID ${saved.id}`) logger.info(`Saved ${this.constructor.name} with UUID ${saved.id}`)
...@@ -114,7 +127,7 @@ class BaseModel extends Model { ...@@ -114,7 +127,7 @@ class BaseModel extends Model {
updateProperties(properties) { updateProperties(properties) {
Object.keys(properties).forEach(prop => { Object.keys(properties).forEach(prop => {
if (this.constructor.jsonSchema.properties[prop]) { if (this.isSettable(prop)) {
this[prop] = properties[prop] this[prop] = properties[prop]
} else { } else {
throw validationError(prop, this.constructor.name) throw validationError(prop, this.constructor.name)
......
...@@ -65,8 +65,19 @@ describe('Manuscript', () => { ...@@ -65,8 +65,19 @@ describe('Manuscript', () => {
) )
}) })
it('can assign to special properties', () => {
const manuscript = new Manuscript()
manuscript['#id'] = 'idref'
})
it('takes schema specified in config into account', async () => { it('takes schema specified in config into account', async () => {
const manuscript = new Manuscript({ configField: 'hello' }) const manuscript = new Manuscript({ configField: 'hello' })
expect(manuscript.configField).toEqual('hello') expect(manuscript.configField).toEqual('hello')
}) })
it('can save new entity with known ID', async () => {
const id = '1838d074-fb9d-4ed6-9c63-39e6bc7429ce'
const manuscript = await new Manuscript({ id }).save()
expect(manuscript.id).toEqual(id)
})
}) })
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment