npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

mongoose-transaction-decorator

v1.0.1

Published

mongoose transaction decorator

Downloads

211

Readme

mongoose-transaction-decorator

A transactional decorator implementation is provided for Mongoose Transactions.

Quick Start

Install

$ npm install mongoose-transaction-decorator

Usage

import {
  set,
  plugin,
  Schema,
  model,
  connect,
  connection,
  Model,
} from 'mongoose';
import {
  transaction,
  TransactionConnection,
  Transactional,
} from 'mongoose-transaction-decorator';

interface IPerson {
  name: string;
  age: number;
  gender: boolean | null;
}

class Person {
  private readonly model: Model<IPerson>;

  constructor(model: Model<IPerson>) {
    this.model = model;
  }

  // Transactional Decorator that marks the method as a transaction method.
  @Transactional()
  async fun() {
    // do some transaction operations
    const doc = new this.model({ name: 'ZhangSan', age: 18 });
    await doc.save();
    await this.model.findOne();
    return;
  }
}

async function main() {
  set('debug', true);

  // use transaction plugin to support transactional decorator
  plugin(transaction);

  await connect('mongodb://localhost:27017/test?directConnection=true');

  // set connection
  new TransactionConnection().setConnection(connection);

  const personSchema = new Schema<IPerson>({
    name: { type: String, required: true },
    age: { type: Number, required: true },
    gender: { type: Boolean, default: null },
  });

  const personModel = model('persons', personSchema);

  const person = new Person(personModel);
  await person.fun();

  await connection.close();
}

main();

Code execution mongoose debug logs:

Mongoose: persons.insertOne({ name: 'ZhangSan', age: 18, gender: null, _id: new ObjectId("6240777d8d80087294f603da"), __v: 0}, { session: ClientSession("c68b32c60cdc4f4ea0b12255954100e3") })
Mongoose: persons.findOne({}, { session: ClientSession("c68b32c60cdc4f4ea0b12255954100e3"), projection: {}})

All operations within a method marked by a transaction decorator become the same transaction operation.

API

Plugin: transaction

The mongoose plugin to support transactional decorator.

Example:

import { plugin } from 'mongoose';
import { transaction } from 'mongoose-transaction-decorator';

plugin(transaction);

Class: TransactionConnection

This Class configures and saves Mongoose connections for use by the transaction decorator.

It is a Singleton Class.

new TransactionConnection()

Creates a new instance of TransactionConnection.

TransactionConnection.setConnection()

Name you mongoose connections.

Parameters:

  • connection <mongoose.Connection> mongoose connection
  • connectionName? <mongoose.Connection> Give a name for this connection, default name "default"

Example:

const transactionConnection = new TransactionConnection();

// set connection use default name "default"
transactionConnection.setConnection(connection);

// If you have multiple database connections
transactionConnection.setConnection(connection1, 'connection1');
transactionConnection.setConnection(connection2, 'connection2');

TransactionConnection.getConnection()

Get mongoose connection.

Parameters:

  • connectionName <string> connection name

Decorator: Transactional

Decorator that marks the method as a transaction method.

note:

  • Decorator normally use dependent the transaction plugin and TransactionConnection.setConnection().
  • Decorator do not affect custom transactions that exist within the decorator method.

Parameters:

  • connectionName? <string> connection name from you set by TransactionConnection.setConnection(), default name "default".

    The database connection used within the decorated method must be the same as the database connection corresponding to this connection name, otherwise a transaction exception may occur.

  • options? <ClientSessionOptions> Optional settings for the transaction.

Example:

class Person {
  @Transactional('connectionName')
  async fun() {
    // do some database operations
    ...;
    return;
  }
}

License

MIT license.