lemon-core
Version:
Lemon Serverless Micro-Service Platform
95 lines (94 loc) • 3.64 kB
TypeScript
import { CoreModel } from 'lemon-model';
import { StorageMakeable, GeneralModelFilter, TypedStorageService, UniqueFieldManager } from './proxy-storage-service';
/**
* class: `AbstractManager`
* - abstract model manager to cover all models which extend CoreModel.
* - feature to handle 'name' like unique value in same type.
* - typed-storage based.
* @abstract
*/
export declare abstract class AbstractManager<T extends CoreModel<ModelType>, S extends StorageMakeable<T, ModelType>, ModelType extends string> extends GeneralModelFilter<T, ModelType> {
readonly NS: string;
readonly parent: S;
readonly storage: TypedStorageService<T, ModelType>;
readonly $unique: UniqueFieldManager<T, ModelType>;
/**
* default constructor
* @param type model type string
* @param parent service instance which implements StorageMakeable interface
* @param fields list of model field names
* @param uniqueField (optional) unique field name
* @param ns (optional) namespace to be printed. pass NULL to suppress all logs.
* global NS value will be used if not specified or undefined.
*/
protected constructor(type: ModelType, parent: S, fields: string[], uniqueField?: string, ns?: string);
/**
* hello of this service-identity
*/
hello: () => string;
/**
* type getter
*/
get type(): ModelType;
/**
* prepare default model when creation
* - sub classes should implement this method
* @param $def default model
* @abstract
*/
protected abstract prepareDefault($def: T): T;
/**
* callback invoked just before the model is saved
* - override this to customize default behavior to perform model validation, cleansing or normalization
* @param model model object
* @param origin original model currently in the storage
*/
onBeforeSave(model: T, origin?: T): T;
/**
* default implementation of 'preparing' model
* - prepare model w/ safe creation
* @param id model-id to prepare
* @param $def default model value
* @param isCreate (optional) flag to allow creating a new model (default: true)
*/
prepare(id: string, $def?: T, isCreate?: boolean): Promise<T>;
/**
* default implementation of 'inserting' model
* @param model model object
*/
insert(model: T): Promise<T>;
/**
* default implementation of 'retrieving' model
* @param id model id
*/
retrieve(id: string): Promise<T>;
/**
* default implementation of updating model
* @param id model id
* @param model model object
* @param $inc (optional) incremental set
*/
update(id: string, model: T, $inc?: T): Promise<T>;
/**
* default implementation of 'upserting' model
* @param id model id
* @param model model object
* @param $inc (optional) incremental set
*/
updateOrCreate(id: string, model: T, $inc?: T): Promise<T>;
/**
* default implementation of deleting model
* @param id model id
* @param destroy flag for hard delete or soft delete (by setting 'deletedAt' field)
*/
delete(id: string, destroy?: boolean): Promise<T>;
/**
* internal implementation for 'update' and 'updateOrCreate'
* @param id model id
* @param model model object
* @param $inc (optional) incremental set
* @param isCreate (optional) flag to allow creating a new model (default: true)
* @private
*/
private _updateModel;
}