1
0
mirror of https://github.com/devcode-it/openstamanager.git synced 2025-02-24 23:37:51 +01:00
Maicol Battistini bda53b272a refactor: ♻️ Refactor generale
- Fix problemi rilevati da ESLint (per togliere alcune regole commentate)
- Spostati i tipi nella cartella typings
- Aggiunti alcuni alias per la cartella resources
2022-01-26 16:24:20 +01:00

81 lines
2.0 KiB
TypeScript

import {
type PluralResponse,
Model as BaseModel
} from 'coloquent';
import {snakeCase} from 'lodash';
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 = '';
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 {
const snakeCasedProperty = snakeCase(property);
if (snakeCasedProperty in target.getAttributes()) {
return target.getAttribute(snakeCasedProperty);
}
return Reflect.get(target, property, receiver);
},
set(target, property: string, 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();
}
getJsonApiBaseUrl(): string {
return '/api';
}
getJsonApiType(): string {
return (super.getJsonApiType() ?? snakeCase(this.constructor.name));
}
getId() {
return this.getApiId();
}
}