This repository contains essentials for using pgledger on the Node platform. Pgledger is a fantastic and lightweight project for implementing a ledger system on Postgres. By handling the heavy lifting, Pgledger enables platform-specific libraries to build on top of it and provide convenient wrapper code for easier usage.
Node PgLedger provides strongly-typed interfaces and convenient methods for interacting with pgLedger, making it easier to integrate ledger functionality into Node applications.
node-pgdledger is published as a NPM package:
npm install node-pgledgeror using yarn
yarn add node-pgledgerNode-Pgledger has its own database connection that is created using sequelize. You are free to confgiure the Sequelize instantce.
For initilize PgLedger you can follow this template code:
import { PGLedger } from "node-pgledger";
(async () => {
const pl = await PGLedger.init({
host: "localhost",
port: 5432,
database: "test-ledger",
username: "postgres",
password: "postgres",
});
// Create a test account
const accountId = await pl.createAccount("test-account", "USD");
console.log(accountId);
// Closes the postgres connections
await pl.destroy();
})();pgledger supports accounts with both positive and negative balances that is configurable. Every account has a name that and a currency. There is no validation on currency code.
const onlyPositiveBalanceAccountId = await pl.createAccount(
"usd-account",
"USD",
// Allow negative balance
false,
// Allow positive balance
true,
);createAccount returns a PgId. Every account and transfer on pgledger is identified by a prefixed ULID. PgId is an alias to string.
There is no batch account insertion at this point.
Using an array of ids you can fetch accounts. An array of PglAccount instances will return.
const accountIds = ["pgla_01KAGDF3F9FFFS3D8ER0WPW9V0"]
const accounts = await pl.lookup(accountIds)Any transfer has a from account and a to account and an amount. All amounts are string.
const fromAccountId = "pgla_01KAGDF3F9FFFS3D8ER0WPW9V0"
const toAccountId = "pgla_01KAM2M4APEQFSF0Z08BW1G7YA"
const transferId = await pl.createTransfer(fromAccountId, toAccountId, "12.2")