A
A
Adel Khalitov2020-02-24 02:44:51
typescript
Adel Khalitov, 2020-02-24 02:44:51

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};
    }
}


helpersDataCB - cb that overrides helpersData and retrieves other entities from the database
handlerCB - cb that adds some data to the main object before saving it to the database.

By the same principle, methods for searching, updating, etc. are written.

Next, a new class is created inheriting this one, the collection is wonderfully created, the data is saved and searched, but the data interface remains the base class (Promise < DefaultInterfase > ) and I have no way to override it by declaring a new view class.

create(): NewResult {
     super.create()
}

I think a bad option, if I add or change something in the parent class, I will need to go through all the inherited classes and add interfaces to all.
How to be?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question