# dtmcli

> client sdk for distributed transaction framework dtm

Latest version **0.0.12** (published 2022-10-09) · MIT license · 0 weekly downloads

## Install

```sh
npm install dtmcli
pnpm add dtmcli
yarn add dtmcli
bun add dtmcli
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.12 |
| Published | 2022-10-09 |
| First published | 2021-07-13 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 3 |
| Unpacked size | 55.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 23 |
| Author | yedf |
| Maintainers | yedf |

## Links

- npm: https://www.npmjs.com/package/dtmcli
- Repository: https://github.com/dtm-labs/dtmcli-node
- Homepage: https://github.com/dtm-labs/dtmcli-node#readme
- Issues: https://github.com/dtm-labs/dtmcli-node/issues
- npm.io page: https://npm.io/package/dtmcli

## Dependencies (3)

- [axios](https://npm.io/package/axios.md) ^1.1.2
- [typedjson](https://npm.io/package/typedjson.md) ^1.8.0
- [reflect-metadata](https://npm.io/package/reflect-metadata.md) ^0.1.13

## Recent versions

- 0.0.12 (latest) — 2022-10-09
- 0.0.11 — 2022-10-09
- 0.0.10 — 2022-10-09
- 0.0.9 — 2021-12-27
- 0.0.8 — 2021-12-27
- 0.0.7 — 2021-12-27
- 0.0.6 — 2021-07-13
- 0.0.5 — 2021-07-13
- 0.0.4 — 2021-07-13
- 0.0.3 — 2021-07-13
- 0.0.2 — 2021-07-13
- 0.0.1 — 2021-07-13

## README

a client for distributed transaction manager [dtm](https://github.com/dtm-labs/dtm)

`dtmcli` 是分布式事务管理器[dtm](https://github.com/dtm-labs/dtm)的客户端sdk

## dtm分布式事务管理服务

DTM是一款跨语言的开源分布式事务管理器，优雅的解决了幂等、空补偿、悬挂等分布式事务难题。提供了简单易用、高性能、易水平扩展的分布式事务解决方案。

## 亮点

* 极易接入
  - 支持HTTP，提供非常简单的接口，极大降低上手分布式事务的难度，新手也能快速接入
* 使用简单
  - 开发者不再担心悬挂、空补偿、幂等各类问题，框架层代为处理
* 跨语言
  - 可适合多语言栈的公司使用。方便go、python、php、nodejs、ruby各类语言使用。
* 易部署、易扩展
  - 仅依赖mysql，部署简单，易集群化，易水平扩展
* 多种分布式事务协议支持
  - TCC、SAGA、XA、事务消息

## 与其他框架对比

目前开源的分布式事务框架，暂未看到非Java语言有成熟的框架。而Java语言的较多，有阿里的SEATA、华为的ServiceComb-Pack，京东的shardingsphere，以及himly，tcc-transaction，ByteTCC等等，其中以seata应用最为广泛。

下面是dtm和seata的主要特性对比：

|  特性| DTM | SEATA |备注|
|:-----:|:----:|:----:|:----:|
| 支持语言 |<span style="color:green">Golang、python、php及其他</span>|<span style="color:orange">Java</span>|dtm可轻松接入一门新语言|
|异常处理| <span style="color:green">[子事务屏障自动处理](https://zhuanlan.zhihu.com/p/388444465)</span>|<span style="color:orange">手动处理</span> |dtm解决了幂等、悬挂、空补偿|
| TCC事务| <span style="color:green">✓</span>|<span style="color:green">✓</span>||
| XA事务|<span style="color:green">✓</span>|<span style="color:green">✓</span>||
|AT事务|<span style="color:red">✗</span>|<span style="color:green">✓</span>|AT与XA类似，性能更好，但有脏回滚|
| SAGA事务 |<span style="color:orange">简单模式</span> |<span style="color:green">状态机复杂模式</span> |dtm的状态机模式在规划中|
|事务消息|<span style="color:green">✓</span>|<span style="color:red">✗</span>|dtm提供类似rocketmq的事务消息|
|通信协议|HTTP|dubbo等协议，无HTTP|dtm后续将支持grpc类协议|
|star数量|<img src="https://img.shields.io/github/stars/dtm-labs/dtm.svg?style=social" alt="github stars"/>|<img src="https://img.shields.io/github/stars/seata/seata.svg?style=social" alt="github stars"/>|dtm从20210604发布0.1，发展快|

从上面对比的特性来看，如果您的语言栈包含了Java之外的语言，那么dtm是您的首选。如果您的语言栈是Java，您也可以选择接入dtm，使用子事务屏障技术，简化您的业务编写。

## typescript使用

```ts
import * as dtmcli from "dtmcli"

// 使用Msg模式需要先初始化DB model
async function startup() {
  const sequelize = await getDB();
  dtmcli.init({
    sequelize,
  });
}

async function FireTcc() {
  let dtm = "http://localhost:8080/api/dtmsvr"
  let svc = "http://localhost:4005/api/msg/testDtm"
  await dtmcli.tccGlobalTransaction(dtm, async (t: dtmcli.Tcc) => {
    let req = { amount: 30 }
    console.log("calling trans out")
    await t.callBranch(req, svc + "/TransOutTry", svc + "/TransOutConfirm", svc + "/TransOutCancel")
    console.log("calling trans in")
    await t.callBranch(req, svc + "/TransInTry", svc + "/TransInConfirm", svc + "/TransInCancel")
  })
}

async function FireSaga() {
  let dtm = "http://localhost:36789/api/dtmsvr" // dtm服务地址
  let svc = "http://localhost:4005/api" // 本地服务前缀
  let req = { amount: 30 } // 子事务需要的负荷
  const saga = new dtmcli.Saga(dtm, await dtmcli.mustGenGid(dtm))
  saga.add(svc+'/TransOut', svc+'/TransOutCompensate', req)
  saga.add(svc+'/TransIn', svc+'/TransInCompensate', req)

  await saga.submit()
}

async function FireSagaConcurrent() {
  let dtm = "http://localhost:36789/api/dtmsvr" // dtm服务地址
  let svc = "http://localhost:4005/api" // 本地服务前缀
  let req = { amount: 30 } // 子事务需要的负荷
  const saga = new dtmcli.Saga(dtm, await dtmcli.mustGenGid(dtm))

  saga.add(svc+'/TransOut', svc+'/TransOutCompensate', req)
  saga.add(svc+'/TransOut', svc+'/TransOutCompensate', req)
  saga.add(svc+'/TransIn', svc+'/TransInCompensate', req)
  saga.add(svc+'/TransIn', svc+'/TransInCompensate', req)
  saga.addBranchOrder(2, [0, 1]).addBranchOrder(3, [0, 1])
  saga.enableConcurrent()

  await saga.submit()
}

async function FireMsg() {
  let req = { amount: 30 } // 子事务需要的负荷
  const gid = await dtmcli.mustGenGid(dtm)
  const msg = new dtmcli.Msg(dtm, gid).add(`${svc }/TransOut`, req).add(`${svc }/TransIn`, req)
  await msg.prepare(`${svc }/query`)
  await msg.submit()
}


async function FireMsgWithLocalTransaction() {
  const req = { amount: 30 }
  const gid = await dtmcli.mustGenGid(dtm)
  const seuqelize = await getDB()
  const msg = new dtmcli.Msg(dtm, gid).add(`${svc }/TransIn`, req)
  await msg.doAndSubmitDB(`${svc }/query`, seuqelize, async (tx) => {
    await transUserBalance(tx, transOutUid, -req.amount)
  })
}
```

## javascript使用

```js
const dtmcli = require("dtmcli")

async function FireTcc() {
  let dtm = "http://localhost:8080/api/dtmsvr"
  let svc = "http://localhost:4005/api"
  await dtmcli.tccGlobalTransaction(dtm, async (t) => {
    let req = { amount: 30 }
    console.log("calling trans out")
    await t.callBranch(req, svc + "/TransOutTry", svc + "/TransOutConfirm", svc + "/TransOutCancel")
    console.log("calling trans in")
    await t.callBranch(req, svc + "/TransInTry", svc + "/TransInConfirm", svc + "/TransInCancel")
  })
}
```

## 可运行的使用示例

见[https://github.com/dtm-labs/dtmcli-node-sample](https://github.com/dtm-labs/dtmcli-node-sample)

---
_Source: https://npm.io/package/dtmcli · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
