|
|
@ -9,10 +9,11 @@ class StructuredNode {
|
|
|
|
this.definition = StructuredNode.classes[this.constructor.name]
|
|
|
|
this.definition = StructuredNode.classes[this.constructor.name]
|
|
|
|
this.hooks = []
|
|
|
|
this.hooks = []
|
|
|
|
this.deleted = false
|
|
|
|
this.deleted = false
|
|
|
|
|
|
|
|
this.__name__ = this.constructor.name.toLowerCase()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
check(name, type, new_value) {
|
|
|
|
check(name, type, new_value) {
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
_link_property(name, elm) {
|
|
|
|
_link_property(name, elm) {
|
|
|
@ -21,7 +22,7 @@ class StructuredNode {
|
|
|
|
if (!relation) {
|
|
|
|
if (!relation) {
|
|
|
|
throw new ApiError(`Cannot find realation ${name} of object ${this.constructor.name}!`, 404, {obj: this, elm: elm})
|
|
|
|
throw new ApiError(`Cannot find realation ${name} of object ${this.constructor.name}!`, 404, {obj: this, elm: elm})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const type = StructuredNode.classes[relation.target]
|
|
|
|
const type = StructuredNode.classes[relation.target].klass
|
|
|
|
|
|
|
|
|
|
|
|
if (typeof elm == "string") {
|
|
|
|
if (typeof elm == "string") {
|
|
|
|
elm = type.by_id(elm)
|
|
|
|
elm = type.by_id(elm)
|
|
|
@ -40,7 +41,7 @@ class StructuredNode {
|
|
|
|
this.json[name] = elm
|
|
|
|
this.json[name] = elm
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return Api.post(`/${this.constructor.name}/${this.json.uid}/${name}/${elm.json.uid}`).then(json => {
|
|
|
|
return Api.post(`/${this.__name__}/${this.json.uid}/${name}/${elm.json.uid}`).then(json => {
|
|
|
|
this.json = json
|
|
|
|
this.json = json
|
|
|
|
this.update('link')
|
|
|
|
this.update('link')
|
|
|
|
return this
|
|
|
|
return this
|
|
|
@ -49,12 +50,15 @@ class StructuredNode {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
_unlink_property(name, type, elm) {
|
|
|
|
_unlink_property(name, type, elm) {
|
|
|
|
this.json[name] = this.json[name].filter(elm => elm.json.uid === (elm.uid || elm))
|
|
|
|
return Api.delete(`/${this.__name__}/${this.json.uid}/${name}/${elm.json.uid}`).then(json => {
|
|
|
|
|
|
|
|
this.json = json
|
|
|
|
|
|
|
|
this.update('link')
|
|
|
|
|
|
|
|
return this
|
|
|
|
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static by_id(uid) {
|
|
|
|
static by_id(uid) {
|
|
|
|
return Api.get(this.constructor.name + '/' + uid).then(x => x.map(elm => new this.constructor(elm)))
|
|
|
|
return Api.get('/' + this.name.toLowerCase() + '/' + uid).then(x => new this(x))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static find(attributes, settings) {
|
|
|
|
static find(attributes, settings) {
|
|
|
@ -67,14 +71,14 @@ class StructuredNode {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (this.json.uid) {
|
|
|
|
if (this.json.uid) {
|
|
|
|
return Api.put(`/${this.constructor.name}/${this.json.uid}`, this.changes).then(json => {
|
|
|
|
return Api.put(`/${this.__name__}/${this.json.uid}`, this.changes).then(json => {
|
|
|
|
this.json = json
|
|
|
|
this.json = json
|
|
|
|
this.changes = {}
|
|
|
|
this.changes = {}
|
|
|
|
this.update('save')
|
|
|
|
this.update('save')
|
|
|
|
return this
|
|
|
|
return this
|
|
|
|
})
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
return Api.post('/' + this.constructor.name, this.json).then(json => {
|
|
|
|
return Api.post('/' + this.__name__, this.json).then(json => {
|
|
|
|
this.json = json
|
|
|
|
this.json = json
|
|
|
|
this.changes = {}
|
|
|
|
this.changes = {}
|
|
|
|
this.update('created')
|
|
|
|
this.update('created')
|
|
|
@ -88,14 +92,14 @@ class StructuredNode {
|
|
|
|
throw new ApiError("Cannot delete object that does not exist yet!", {obj: this})
|
|
|
|
throw new ApiError("Cannot delete object that does not exist yet!", {obj: this})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
this.deleted = true
|
|
|
|
this.deleted = true
|
|
|
|
Api.delete(`/${this.constructor.name}/${this.json.uid}`).then(r => {
|
|
|
|
Api.delete(`/${this.__name__}/${this.json.uid}`).then(r => {
|
|
|
|
this.update('deleted')
|
|
|
|
this.update('deleted')
|
|
|
|
return this
|
|
|
|
return this
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
exists() {
|
|
|
|
exists() {
|
|
|
|
return !this.deleted && !!this.json.id
|
|
|
|
return !this.deleted && !!this.json.uid
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
onupdate(callback) {
|
|
|
|
onupdate(callback) {
|
|
|
@ -114,7 +118,8 @@ class StructuredNode {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static registerClassDefinition(klass, props, rels) {
|
|
|
|
static registerClassDefinition(klass, props, rels) {
|
|
|
|
StructuredNode.classes[klass.constructor.name] = {klass, props, rels}
|
|
|
|
console.log(klass, props, rels)
|
|
|
|
|
|
|
|
StructuredNode.classes[klass.name] = {klass, props, rels}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -126,9 +131,9 @@ class Api {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static get(url) {
|
|
|
|
static get(url) {
|
|
|
|
fetch('/api' + url, {headers: Api.headers()}).then(r => {
|
|
|
|
return fetch('/api' + url, {headers: Api.headers()}).then(r => {
|
|
|
|
if (r.ok) {
|
|
|
|
if (r.ok) {
|
|
|
|
return r.json
|
|
|
|
return r.json()
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
// evaluate response and raise error
|
|
|
|
// evaluate response and raise error
|
|
|
|
return ApiError.fromResponse(r)
|
|
|
|
return ApiError.fromResponse(r)
|
|
|
@ -136,9 +141,9 @@ class Api {
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
static post(url, body="") {
|
|
|
|
static post(url, body="") {
|
|
|
|
fetch('/api' + url, {headers: Api.headers(), body: JSON.stringify(body), method: 'POST'}).then(r => {
|
|
|
|
return fetch('/api' + url, {headers: Api.headers(), body: JSON.stringify(body), method: 'POST'}).then(r => {
|
|
|
|
if (r.ok) {
|
|
|
|
if (r.ok) {
|
|
|
|
return r.json
|
|
|
|
return r.json()
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
// evaluate response and raise error
|
|
|
|
// evaluate response and raise error
|
|
|
|
return ApiError.fromResponse(r)
|
|
|
|
return ApiError.fromResponse(r)
|
|
|
@ -146,9 +151,9 @@ class Api {
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
static put(url, body) {
|
|
|
|
static put(url, body) {
|
|
|
|
fetch('/api' + url, {headers: Api.headers(), body: JSON.stringify(body), method: 'PUT'}).then(r => {
|
|
|
|
return fetch('/api' + url, {headers: Api.headers(), body: JSON.stringify(body), method: 'PUT'}).then(r => {
|
|
|
|
if (r.ok) {
|
|
|
|
if (r.ok) {
|
|
|
|
return r.json
|
|
|
|
return r.json()
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
// evaluate response and raise error
|
|
|
|
// evaluate response and raise error
|
|
|
|
return ApiError.fromResponse(r)
|
|
|
|
return ApiError.fromResponse(r)
|
|
|
@ -156,7 +161,7 @@ class Api {
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
static delete(url) {
|
|
|
|
static delete(url) {
|
|
|
|
fetch('/api' + url, {headers: Api.headers(), method: 'DELETE'}).then(r => {
|
|
|
|
return fetch('/api' + url, {headers: Api.headers(), method: 'DELETE'}).then(r => {
|
|
|
|
if (r.ok) {
|
|
|
|
if (r.ok) {
|
|
|
|
return r
|
|
|
|
return r
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
@ -168,7 +173,8 @@ class Api {
|
|
|
|
|
|
|
|
|
|
|
|
static headers() {
|
|
|
|
static headers() {
|
|
|
|
return {
|
|
|
|
return {
|
|
|
|
'Content-Type': 'application/json'
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
|
|
|
|
// 'Authorization': 'Bearer ' + Api.token
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|