refactor: dynamic list to nestjs

This commit is contained in:
Ahmed Bouhuolia
2025-01-12 18:22:48 +02:00
parent ddaea20d16
commit 270b421a6c
117 changed files with 4232 additions and 1493 deletions

View File

@@ -0,0 +1,26 @@
import { BaseModel } from '@/models/Model';
export const CustomViewBaseModel = (Model: typeof BaseModel) =>
class extends Model {
/**
* Retrieve the default custom views, roles and columns.
*/
static get defaultViews() {
return [];
}
/**
* Retrieve the default view by the given slug.
*/
static getDefaultViewBySlug(viewSlug) {
return this.defaultViews.find((view) => view.slug === viewSlug) || null;
}
/**
* Retrieve the default views.
* @returns {IView[]}
*/
static getDefaultViews() {
return this.defaultViews;
}
};

View File

@@ -0,0 +1,93 @@
import { get } from 'lodash';
import {
IModelMeta,
IModelMetaField,
IModelMetaDefaultSort,
} from '@/interfaces/Model';
import { BaseModel } from '@/models/Model';
const defaultModelMeta = {
fields: {},
fields2: {},
};
export interface IMetadataModel extends BaseModel {
meta: IModelMeta;
parsedMeta: IModelMeta;
fields: { [key: string]: IModelMetaField };
defaultSort: IModelMetaDefaultSort;
defaultFilterField: string;
getField(key: string, attribute?: string): IModelMetaField;
getMeta(key?: string): IModelMeta;
}
type GConstructor<T = {}> = new (...args: any[]) => T;
export const MetadataModelMixin = <T extends GConstructor<BaseModel>>(
Model: T,
) =>
class ModelSettings extends Model {
/**
* Retrieve the model meta.
* @returns {IModelMeta}
*/
static get meta(): IModelMeta {
throw new Error('');
}
/**
* Parsed meta merged with default emta.
* @returns {IModelMeta}
*/
static get parsedMeta(): IModelMeta {
return {
...defaultModelMeta,
...this.meta,
};
}
/**
* Retrieve specific model field meta of the given field key.
* @param {string} key
* @returns {IModelMetaField}
*/
public static getField(key: string, attribute?: string): IModelMetaField {
const field = get(this.meta.fields, key);
return attribute ? get(field, attribute) : field;
}
/**
* Retrieves the specific model meta.
* @param {string} key
* @returns
*/
public static getMeta(key?: string) {
return key ? get(this.parsedMeta, key) : this.parsedMeta;
}
/**
* Retrieve the model meta fields.
* @return {{ [key: string]: IModelMetaField }}
*/
public static get fields(): { [key: string]: IModelMetaField } {
return this.getMeta('fields');
}
/**
* Retrieve the model default sort settings.
* @return {IModelMetaDefaultSort}
*/
public static get defaultSort(): IModelMetaDefaultSort {
return this.getMeta('defaultSort');
}
/**
* Retrieve the default filter field key.
* @return {string}
*/
public static get defaultFilterField(): string {
return this.getMeta('defaultFilterField');
}
};

View File

@@ -0,0 +1,24 @@
import { BaseModel } from '@/models/Model';
import { IModelMeta } from '@/interfaces/Model';
import { ISearchRole } from '../DynamicFilter.types';
type GConstructor<T = {}> = new (...args: any[]) => T;
export const SearchableBaseModelMixin = <T extends GConstructor<BaseModel>>(
Model: T,
) =>
class SearchableBaseModel extends Model {
/**
* Searchable model.
*/
static get searchable(): IModelMeta {
throw true;
}
/**
* Search roles.
*/
static get searchRoles(): ISearchRole[] {
return [];
}
};