@herlinus/coloquent
Version:
Library for retrieving model objects from a JSON-API, with a fluent syntax inspired by Laravel Eloquent.
35 lines • 1.13 kB
JavaScript
export const Attribut = (name = null, readOnly = false) => (target, key, descriptor) => {
if (readOnly) {
if (!target.readOnlyAttributes) {
target.readOnlyAttributes = [];
}
target.readOnlyAttributes.push(name || key);
}
const getter = function () {
return this.getAttribute(name || key);
};
const setter = function (value) {
this.setAttribute(name || key, value);
};
descriptor = descriptor || {};
descriptor.get = getter;
descriptor.set = setter;
return descriptor;
};
export const toManyRelation = (type_cb) => (target, key, descriptor) => {
const getter = function () {
return this.hasMany(type_cb(), key);
};
descriptor = descriptor || {};
descriptor.get = getter;
return descriptor;
};
export const toOneRelation = (type_cb) => (target, key, descriptor) => {
const getter = function () {
return this.hasOne(type_cb(), key);
};
descriptor = descriptor || {};
descriptor.get = getter;
return descriptor;
};
//# sourceMappingURL=Helpers.js.map