@webda/core
Version:
Expose API with Lambda
157 lines • 5.3 kB
JavaScript
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
import { ModelRef, ModelRefCustom, NotEnumerable } from "./coremodel.js";
/**
* Define a link to 1:n relation
*/
export class ModelLink {
constructor(uuid, model, parent) {
this.uuid = uuid;
this.model = model;
this.parent = parent;
}
async get() {
return (await this.model.ref(this.uuid).get())?.setContext(this.parent?.getContext());
}
set(id) {
this.uuid = typeof id === "string" ? id : id.getUuid();
// Set dirty for parent
this.parent?.__dirty.add(Object.keys(this.parent)
.filter(k => this.parent[k] === this)
.pop());
}
toString() {
return this.uuid;
}
toJSON() {
return this.uuid;
}
getUuid() {
return this.uuid;
}
}
__decorate([
NotEnumerable
], ModelLink.prototype, "parent", void 0);
/**
* Link to a collection of objects
*/
export class ModelLinksSimpleArray extends Array {
constructor(model, content = [], parent) {
super();
this.model = model;
content.forEach(c => this.add(c));
this.parent = parent;
}
add(model) {
this.push(typeof model === "string"
? new ModelRef(model, this.model, this.parent)
: new ModelRef(model.getUuid(), this.model, this.parent));
this.parent?.__dirty.add(Object.keys(this.parent)
.filter(k => this.parent[k] === this)
.pop());
}
remove(model) {
let index = this.findIndex(m => m.toString() === (typeof model === "string" ? model : model.getUuid()));
if (index >= 0) {
this.splice(index, 1);
}
this.parent?.__dirty.add(Object.keys(this.parent)
.filter(k => this.parent[k] === this)
.pop());
}
}
__decorate([
NotEnumerable
], ModelLinksSimpleArray.prototype, "parent", void 0);
/**
* Link to a collection of objects including some additional data
*/
export class ModelLinksArray extends Array {
constructor(model, content = [], parent) {
super();
this.model = model;
this.parent = parent;
this.push(...content
.filter(c => c && c.uuid)
.map(c => new ModelRefCustom(c.uuid, model, c, this.parent)));
}
add(model) {
this.push(((model instanceof ModelRefCustom
? model
: new ModelRefCustom(model.uuid || model.getUuid(), this.model, model, this.parent))));
this.parent?.__dirty.add(Object.keys(this.parent)
.filter(k => this.parent[k] === this)
.pop());
}
remove(model) {
const uuid = typeof model === "string" ? model : model.uuid || model.getUuid();
let index = this.findIndex(m => m.getUuid() === uuid);
if (index >= 0) {
this.splice(index, 1);
this.parent?.__dirty.add(Object.keys(this.parent)
.filter(k => this.parent[k] === this)
.pop());
}
}
}
__decorate([
NotEnumerable
], ModelLinksArray.prototype, "parent", void 0);
export function createModelLinksMap(model, data = {}, parent) {
let result = {
add: (model) => {
result[model.uuid || model.getUuid()] = model;
parent?.__dirty.add(Object.keys(parent)
.filter(k => parent[k] === result)
.pop());
},
remove: (model) => {
// @ts-ignore
const uuid = typeof model === "string" ? model : model.uuid || model.getUuid();
delete result[uuid];
parent?.__dirty.add(Object.keys(parent)
.filter(k => parent[k] === result)
.pop());
}
};
Object.keys(data)
.filter(k => k !== "__proto__")
.forEach(key => {
result[key] = new ModelRefCustom(data[key].uuid, model, data[key], parent);
});
Object.defineProperty(result, "add", { enumerable: false });
Object.defineProperty(result, "remove", { enumerable: false });
return result;
}
/**
* Mapper attribute (target of a Mapper service)
*
* This is not exported as when mapped the target is always an array
* TODO Handle 1:1 map
*/
export class ModelMapLoaderImplementation {
constructor(model, data, parent) {
Object.assign(this, data);
this._model = model;
this._parent = parent;
}
/**
*
* @returns the model
*/
async get() {
return this._model.ref(this.uuid).get(this._parent.getContext());
}
}
__decorate([
NotEnumerable
], ModelMapLoaderImplementation.prototype, "_model", void 0);
__decorate([
NotEnumerable
], ModelMapLoaderImplementation.prototype, "_parent", void 0);
//# sourceMappingURL=relations.js.map