mirror of
https://github.com/zhigang1992/DefinitelyTyped.git
synced 2026-04-23 04:49:15 +08:00
adds ClientSession to SaveOptions mongoose (#28880)
* adds session to SaveOptions * removes redundant create method
This commit is contained in:
committed by
Ryan Cavanaugh
parent
2d6f7180c4
commit
8062de22e4
4
types/mongoose/index.d.ts
vendored
4
types/mongoose/index.d.ts
vendored
@@ -2705,6 +2705,7 @@ declare module "mongoose" {
|
||||
* Triggers the save() hook.
|
||||
*/
|
||||
create(docs: any[], callback?: (err: any, res: T[]) => void): Promise<T[]>;
|
||||
create(docs: any[], options?: SaveOptions, callback?: (err: any, res: T[]) => void): Promise<T[]>;
|
||||
create(...docs: any[]): Promise<T>;
|
||||
create(...docsWithCallback: any[]): Promise<T>;
|
||||
|
||||
@@ -2992,6 +2993,7 @@ declare module "mongoose" {
|
||||
interface SaveOptions {
|
||||
safe?: boolean | WriteConcern;
|
||||
validateBeforeSave?: boolean;
|
||||
session?: ClientSession;
|
||||
}
|
||||
|
||||
interface WriteConcern {
|
||||
@@ -3034,7 +3036,7 @@ declare module "mongoose" {
|
||||
interface ModelOptions {
|
||||
session?: ClientSession | null;
|
||||
}
|
||||
|
||||
|
||||
interface ModelFindByIdAndUpdateOptions extends ModelOptions {
|
||||
/** true to return the modified document rather than the original. defaults to false */
|
||||
new?: boolean;
|
||||
|
||||
@@ -1960,4 +1960,30 @@ db.createCollection('users').
|
||||
]).session(session).exec()).
|
||||
then((res: any) => {
|
||||
session.commitTransaction();
|
||||
});
|
||||
});
|
||||
|
||||
/** https://mongoosejs.com/docs/transactions.html */
|
||||
|
||||
const Customer = db.model('Customer', new mongoose.Schema({ name: String }));
|
||||
|
||||
db.createCollection('customers').
|
||||
then(() => db.startSession()).
|
||||
then(_session => {
|
||||
session = _session;
|
||||
// Start a transaction
|
||||
session.startTransaction();
|
||||
// This `create()` is part of the transaction because of the `session`
|
||||
// option.
|
||||
return Customer.create([{ name: 'Test' }], { session: session });
|
||||
}).
|
||||
// Transactions execute in isolation, so unless you pass a `session`
|
||||
// to `findOne()` you won't see the document until the transaction
|
||||
// is committed.
|
||||
then((customer: mongoose.Document[]) => Customer.findOne({ name: 'Test' }).exec()).
|
||||
// This `findOne()` will return the doc, because passing the `session`
|
||||
// means this `findOne()` will run as part of the transaction.
|
||||
then(() => Customer.findOne({ name: 'Test' }).session(session).exec()).
|
||||
// Once the transaction is committed, the write operation becomes
|
||||
// visible outside of the transaction.
|
||||
then(() => session.commitTransaction()).
|
||||
then(() => Customer.findOne({ name: 'Test' }).exec())
|
||||
|
||||
Reference in New Issue
Block a user