# [**@ocap/statedb-qldb**](https://github.com/arcblock/blockchain)

[![styled with prettier](https://img.shields.io/badge/styled_with-prettier-ff69b4.svg)](https://github.com/prettier/prettier)

> OCAP statedb adapter that uses Amazon QLDB as backend statedb.

## Requirements

Please follow https://docs.aws.amazon.com/qldb/latest/developerguide/getting-started.nodejs.html to setup your AWS SDK credentials and Amazon QLDB ledgers.
**This adapter will create tables in the ledger, but not the ledger itself.**

## Install

```sh
npm install @ocap/statedb-qldb
// or
pnpm install @ocap/statedb-qldb
```

## Usage

```js
const QLDBStateDB = require('@ocap/statedb-qldb');

const db = new QLDBStateDB({
  ledgerName: 'qldb-statedb-dev',
  clientOptions: {
    region: 'ap-northeast-1',
    accessKeyId: process.env.AWS_ACCESS_KEY_ID,
    secretAccessKey: process.env.AWS_ACCESS_KEY_SECRET,
  },
});

db.onReady(async () => {
  const account = {
    address: 'z1VUFgtw9JSENAswYm5vqdpg461oi6JPEdP',
    balance: '100000000000000000000000000',
    gasBalance: '0',
    nonce: 2,
    numTxs: 1,
    numAssets: 0,
    migratedTo: [],
    migratedFrom: [],
    stake: null,
    context: {
      genesisTime: '2021-02-02T02:17:48.141Z',
      genesisTx: '',
      renaissanceTime: '2021-02-02T02:17:48.141Z',
      renaissanceTx: '',
    },
    moniker: 'system',
  };

  await db.driver.executeLambda(async (txn) => {
    const exist = await db.account.get(account.address, { txn });
    console.log('account', { exist });

    if (!exist) {
      const created = await db.account.create(account.address, account, { txn });
      console.log('account', { created });
    }

    const count = await db.account.count({ txn });
    console.log('account', { count });
  });
});
```
