UNPKG

@herlinus/coloquent

Version:

Library for retrieving model objects from a JSON-API, with a fluent syntax inspired by Laravel Eloquent.

38 lines 1.16 kB
import { Model } from "./Model"; export class PolymorphicEntry { constructor(klass, value) { this.klass = klass; this.value = value; } isTheGood(type) { return type === this.value; } } export class PolymorphicMapping { constructor(polymorphicEntries) { this.polymorphicEntries = polymorphicEntries; } getClass(type) { for (const entry of this.polymorphicEntries) { if (entry.isTheGood(type)) { return entry.klass; } } return null; } } export class PolymorphicModel extends Model { static appendPolymorph(value) { let that = this; return function appendPolymorph_(klass) { klass.JsonApiBaseType_ = that.getJsonApiBaseType(); that.polymorphicOn.polymorphicEntries.push(new PolymorphicEntry(klass, value)); return klass; }; } static getClass(doc) { return this.polymorphicOn.getClass(doc.type) || this; } } PolymorphicModel.polymorphicOn = new PolymorphicMapping([]); //# sourceMappingURL=PolymorphicModel.js.map