Answer the question
In order to leave comments, you need to log in
How to change the interfaces of an inherited typescript class?
Hello. I solve a very simple problem, I got lost in 2 pines.
I am writing a default class for working with entities that I will save in the database.
I am very abstract in all my projects and came to the conclusion that any entity is created, updated, deleted, etc. according to the same algorithm.
1) Data is
received 2) Validated
3) Added
4) Saved/Updated/Deleted
Therefore, I decided to write a common database interaction class for all entities.
It will also be very useful if I suddenly want to change the database from mongi to another. That is, I need to override only in 1 place, and add some validators and try to initially maintain a common structure for any database.
Conditional example:
let _mongooseSchema!: Model<any>;
export interface DefaultInterfase {
_id?: number;
}
export default class DefaultObject {
set mongooseSchema(params: SchemaInitParams) {
_mongooseSchema = mongoose.model(params.schemaName, new Schema(params.schema));
}
public async create(params: Create, helpersDataCB, hendlerCB ): Promise<DefaultInterfase> {
const bulk = _mongooseSchema.collection.initializeOrderedBulkOp();
const errors: Array<{ object: any; note: string }> = [];
let helpersData;
await helpersDataCB(helpersData);
params.list.map(el => {
try {
hendlerCB(el, helpersData)
bulk.insert(el);
} catch (e) {
errors.push({ object: el, note: e.message });
}
});
bulk.execute();
return {some result};
}
}
create(): NewResult {
super.create()
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question