0.0.2 • Published 1 year ago

@c10k/tx-manager v0.0.2

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

tx-manager

Provides CRUD development experience similar to Spring-Boot + MyBatis using mysql2 and mybatis-mapper.

npm install @c10k/tx-manager

Declaration and Usage of Mapper.xml

Refer to the following files:

  1. tests/resources/mapper/TestMapper.xml
  2. tests/src/services/TestService.js
    • Contains the concepts of Dao, Mapper, and Service.
  3. tests/src/config/database.js
    • Similar to Bean objects in Spring-Boot

Using MySQL Transactions

The following code demonstrates how to start a transaction, and use the same transaction in nested callbacks, satisfying most development scenarios.

const uuid1 = uuidv4();
const uuid2 = uuidv4();
await txManager.runTransaction(async (_, connectionId1) => {
    const id1 = await testService.save(uuid1);

    await txManager.runTransaction(async (_, connectionId2) => {
        const id2 = await testService.save(uuid2);

        expect(connectionId1).toBe(connectionId2);

        const {id: connectionId3} = txManager.getCurrentContext();

        expect(connectionId1).toBe(connectionId3);

        throw new Error(`rollback by error`);
    });
});