mirror of
https://github.com/devcode-it/openstamanager.git
synced 2025-02-24 23:37:51 +01:00
100 lines
2.4 KiB
TypeScript
100 lines
2.4 KiB
TypeScript
import {
|
|
type PluralResponse,
|
|
Model as BaseModel,
|
|
PaginationStrategy
|
|
} from 'coloquent';
|
|
import {snakeCase} from 'lodash-es';
|
|
|
|
import {
|
|
hasGetter,
|
|
hasSetter
|
|
} from '../utils';
|
|
|
|
export interface InstantiableModel<T extends Model = Model> {
|
|
new(): (Model | T) & {[prop: string]: any};
|
|
}
|
|
|
|
export type IModel<T extends Model = Model> = InstanceType<InstantiableModel<T>>;
|
|
|
|
/**
|
|
* The base model for all models.
|
|
*/
|
|
export abstract class Model extends BaseModel {
|
|
public static relationships: string[] = [];
|
|
protected jsonApiType: string = '';
|
|
protected static paginationStrategy = PaginationStrategy.PageBased;
|
|
|
|
constructor() {
|
|
super();
|
|
|
|
// Return a proxy of this object to allow dynamic attributes getters and setters
|
|
// eslint-disable-next-line no-constructor-return
|
|
return new Proxy(this, {
|
|
get(target, property: string, receiver): any {
|
|
if (hasGetter(target, property)) {
|
|
return Reflect.get(target, property, receiver);
|
|
}
|
|
|
|
const snakeCasedProperty = snakeCase(property);
|
|
if (snakeCasedProperty in target.getAttributes()) {
|
|
return target.getAttribute(snakeCasedProperty);
|
|
}
|
|
|
|
return Reflect.get(target, property, receiver);
|
|
},
|
|
set(target, property: string, value) {
|
|
if (hasSetter(target, property)) {
|
|
return Reflect.set(target, property, value);
|
|
}
|
|
|
|
target.setAttribute(snakeCase(property), value);
|
|
return true;
|
|
}
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Just an alias to the get() method.
|
|
*
|
|
* Returns all the instances of the model.
|
|
*/
|
|
static all(): Promise<PluralResponse<IModel>> {
|
|
// @ts-ignore
|
|
return this.with(this.relationships).get();
|
|
}
|
|
|
|
setAttributes(attributes: Record<string, any>): void {
|
|
for (const [attribute, value] of Object.entries(attributes)) {
|
|
this.setAttribute(attribute, value);
|
|
}
|
|
}
|
|
|
|
getAttribute(attributeName: string): any {
|
|
return super.getAttribute(attributeName);
|
|
}
|
|
|
|
setAttribute(attributeName: string, value: any) {
|
|
super.setAttribute(attributeName, value);
|
|
}
|
|
|
|
getAttributes(): {[p: string]: any} {
|
|
return super.getAttributes();
|
|
}
|
|
|
|
getRelation(relationName: string): IModel | any {
|
|
return super.getRelation(relationName);
|
|
}
|
|
|
|
getJsonApiBaseUrl(): string {
|
|
return '/api/v1';
|
|
}
|
|
|
|
getJsonApiType(): string {
|
|
return (super.getJsonApiType() ?? snakeCase(this.constructor.name));
|
|
}
|
|
|
|
getId() {
|
|
return this.getApiId();
|
|
}
|
|
}
|