Skip to content
This repository has been archived by the owner on Jun 29, 2021. It is now read-only.

Transcation #258

Closed
jayrajk opened this issue Mar 6, 2019 · 5 comments
Closed

Transcation #258

jayrajk opened this issue Mar 6, 2019 · 5 comments

Comments

@jayrajk
Copy link

jayrajk commented Mar 6, 2019

Add on functionality of transaction which mongoose has provide
https://mongoosejs.com/docs/transactions.html

@hasezoey
Copy link
Contributor

i know this is a bit old, but:
why would this be needed to be included in typegoose, because as far as i could see, this has nothing todo with the model creation / schema creation?

@hasezoey hasezoey mentioned this issue Jul 31, 2019
14 tasks
@hasezoey
Copy link
Contributor

hasezoey commented Aug 6, 2019

I'm currently investigating this for release 6.0.0, but it seems like this cannot/should not be implemented in typegoose, here are the reasons:

  • const session = mongoose.startSession() and the following session.startTransaction() have nothing to do with typegoose
  • it cannot be tested without changing the tests to use (package)run-rs
  • it works (with types and co) without Typegoose implementing it

full example (needs Replica Set connection):

const session = await mongoose.startSession();
session.startTransaction();
await somemodel.create({}, { session });
await somemodel.find({}, { session });
await session.commitTransaction():

TL;DR: "wontfix" & removing from 6.0.0 spec (this can be closed)
@Ben305

@hasezoey

This comment has been minimized.

@Ben305 Ben305 closed this as completed Sep 10, 2019
@vanishdark
Copy link

vanishdark commented Mar 7, 2020

Hey, maybe coming a lit late, but just to people arrive here looking for typegoose transactions, i have an example may you can use.

export class ClassName {
           constructor (@InjectModel('modelname') private readonly modelname: ReturnModelType<typeof ModelClass>)
}

public findOneAndReplaceByOne(){
 let session: ClientSession = null;
        this.modelname.db.startSession()
        .then(_session => {
            session = _session;
            session.startTransaction();
            return this.modelname.findOne({});
        })
        .then((model: any) => {
            model.ENUMID = model.ENUMID + 1;
            return refData.save();
        })
        .then((result) => {
                session.commitTransaction();
            })
        .catch(e => {
               session.abortTransaction();
               console.error(e);
});
}

ofc you can turn this into async await., just follow the same logic

@sebastiangug
Copy link

@vanishdark

why are we using the object reference in the constructor when reading/modifying data?

If I have other unrelated queries that use the same object and then I abort my transaction, won't that abort all other operations as well?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants