2021-10-11 19:57:42 +02:00
|
|
|
import {
|
2021-12-31 00:25:08 +01:00
|
|
|
type PluralResponse,
|
|
|
|
Model as BaseModel
|
2021-10-11 19:57:42 +02:00
|
|
|
} from 'coloquent';
|
2022-01-27 10:10:45 +01:00
|
|
|
import {snakeCase} from 'lodash-es';
|
2021-09-29 18:07:35 +02:00
|
|
|
|
2022-01-29 11:20:55 +01:00
|
|
|
import {
|
|
|
|
hasGetter,
|
|
|
|
hasSetter
|
|
|
|
} from '../utils';
|
|
|
|
|
2022-01-26 11:33:08 +01:00
|
|
|
export interface InstantiableModel<T extends Model = Model> {
|
2022-01-29 11:20:55 +01:00
|
|
|
new(): (Model | T) & {[prop: string]: any};
|
2022-01-07 14:22:14 +01:00
|
|
|
}
|
|
|
|
|
2022-01-07 16:04:50 +01:00
|
|
|
export type IModel<T extends Model = Model> = InstanceType<InstantiableModel<T>>;
|
2022-01-07 14:22:14 +01:00
|
|
|
|
2021-10-12 13:04:36 +02:00
|
|
|
/**
|
2021-12-21 11:06:53 +01:00
|
|
|
* The base model for all models.
|
2021-10-12 13:04:36 +02:00
|
|
|
*/
|
2022-01-07 14:22:14 +01:00
|
|
|
export abstract class Model extends BaseModel {
|
2022-01-10 17:13:27 +01:00
|
|
|
public static relationships: string[] = [];
|
|
|
|
protected jsonApiType: string = '';
|
2021-12-20 17:57:55 +01:00
|
|
|
|
2021-12-18 14:54:34 +01:00
|
|
|
constructor() {
|
|
|
|
super();
|
|
|
|
|
|
|
|
// Return a proxy of this object to allow dynamic attributes getters and setters
|
2022-01-26 16:24:20 +01:00
|
|
|
// eslint-disable-next-line no-constructor-return
|
2021-12-18 14:54:34 +01:00
|
|
|
return new Proxy(this, {
|
2022-01-06 15:45:35 +01:00
|
|
|
get(target, property: string, receiver): any {
|
2022-01-29 11:20:55 +01:00
|
|
|
if (hasGetter(target, property)) {
|
|
|
|
return Reflect.get(target, property, receiver);
|
|
|
|
}
|
|
|
|
|
2021-12-21 11:05:55 +01:00
|
|
|
const snakeCasedProperty = snakeCase(property);
|
2021-12-18 14:54:34 +01:00
|
|
|
if (snakeCasedProperty in target.getAttributes()) {
|
|
|
|
return target.getAttribute(snakeCasedProperty);
|
|
|
|
}
|
|
|
|
|
|
|
|
return Reflect.get(target, property, receiver);
|
|
|
|
},
|
2022-01-06 15:45:35 +01:00
|
|
|
set(target, property: string, value) {
|
2022-01-29 11:20:55 +01:00
|
|
|
if (hasSetter(target, property)) {
|
|
|
|
return Reflect.set(target, property, value);
|
|
|
|
}
|
|
|
|
|
2022-01-03 15:11:32 +01:00
|
|
|
target.setAttribute(snakeCase(property), value);
|
|
|
|
return true;
|
2021-12-18 14:54:34 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-10-08 10:53:56 +02:00
|
|
|
/**
|
2021-12-18 13:23:33 +01:00
|
|
|
* Just an alias to the get() method.
|
|
|
|
*
|
|
|
|
* Returns all the instances of the model.
|
2022-01-19 19:23:42 +01:00
|
|
|
*/
|
|
|
|
static all(): Promise<PluralResponse<IModel>> {
|
2022-01-06 15:45:35 +01:00
|
|
|
// @ts-ignore
|
2022-01-19 19:23:42 +01:00
|
|
|
return this.with(this.relationships).get();
|
2021-09-29 18:07:35 +02:00
|
|
|
}
|
|
|
|
|
2022-01-06 15:45:35 +01:00
|
|
|
setAttributes(attributes: Record<string, any>): void {
|
2021-10-08 16:49:17 +02:00
|
|
|
for (const [attribute, value] of Object.entries(attributes)) {
|
2022-01-06 15:45:35 +01:00
|
|
|
this.setAttribute(attribute, value);
|
2021-10-08 16:49:17 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-18 13:23:33 +01:00
|
|
|
getAttribute(attributeName: string): any {
|
|
|
|
return super.getAttribute(attributeName);
|
|
|
|
}
|
|
|
|
|
|
|
|
setAttribute(attributeName: string, value: any) {
|
|
|
|
super.setAttribute(attributeName, value);
|
|
|
|
}
|
|
|
|
|
2022-01-06 15:45:35 +01:00
|
|
|
getAttributes(): {[p: string]: any} {
|
2021-12-18 13:23:33 +01:00
|
|
|
return super.getAttributes();
|
|
|
|
}
|
|
|
|
|
2022-01-27 16:41:58 +01:00
|
|
|
getRelation(relationName: string): IModel | any {
|
|
|
|
return super.getRelation(relationName);
|
|
|
|
}
|
|
|
|
|
2021-10-08 10:53:56 +02:00
|
|
|
getJsonApiBaseUrl(): string {
|
2022-02-02 00:33:51 +01:00
|
|
|
return '/api/v1';
|
2021-09-29 20:29:08 +02:00
|
|
|
}
|
|
|
|
|
2021-10-08 10:53:56 +02:00
|
|
|
getJsonApiType(): string {
|
|
|
|
return (super.getJsonApiType() ?? snakeCase(this.constructor.name));
|
2021-09-29 18:07:35 +02:00
|
|
|
}
|
2022-01-06 15:45:35 +01:00
|
|
|
|
|
|
|
getId() {
|
|
|
|
return this.getApiId();
|
|
|
|
}
|
2021-09-29 18:07:35 +02:00
|
|
|
}
|