Skip to content

Commit

Permalink
chore: update namings
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelPretorius committed Apr 22, 2024
1 parent 3737a86 commit ca74398
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 22 deletions.
Binary file modified .DS_Store
Binary file not shown.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,5 @@ blockEvent.json
blockRes.json
blockTx.json
bondsInfo.json

*.DS_Store
6 changes: 3 additions & 3 deletions src/postgres/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ DATABASE_URL=<your_database_url>
```

The `src/postgres/client.ts` file provides helper functions for connecting to the PostgreSQL database and managing transactions.
The `withCoreTransaction` function simplifies executing queries within a single transaction and also a `ROLLBACK` in case an error occurrs. Below is an example:
The `withTransaction` function simplifies executing queries within a single transaction and also a `ROLLBACK` in case an error occurrs. Below is an example:

```ts
const insertBlockSql = `
Expand All @@ -26,7 +26,7 @@ FROM jsonb_to_recordset($1) AS tr(hash text, code int, fee jsonb, "gasUsed" text
export const insertBlock = async (block: BlockCore): Promise<void> => {
try {
// do all the insertions in a single transaction
await withCoreTransaction(async (client) => {
await withTransaction(async (client) => {
await client.query(insertBlockSql, [
block.height,
block.hash,
Expand Down Expand Up @@ -54,7 +54,7 @@ SELECT * FROM "ChainCore" WHERE "chainId" = $1
`;
export const getChain = async (chainId: string): Promise<Chain | undefined> => {
try {
const res = await corePool.query(getChainSql, [chainId]);
const res = await pool.query(getChainSql, [chainId]);
return res.rows[0];
} catch (error) {
throw error;
Expand Down
4 changes: 2 additions & 2 deletions src/postgres/block.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { withCoreTransaction } from "./client";
import { withTransaction } from "./client";

export type BlockCore = {
height: number;
Expand Down Expand Up @@ -54,7 +54,7 @@ FROM jsonb_to_recordset($1) AS ev("type" text, attributes jsonb[], "beginBlockEv
export const insertBlock = async (block: BlockCore): Promise<void> => {
try {
// do all the insertions in a single transaction
await withCoreTransaction(async (client) => {
await withTransaction(async (client) => {
await client.query(insertBlockSql, [
block.height,
block.hash,
Expand Down
20 changes: 8 additions & 12 deletions src/postgres/chain.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
import { corePool } from "./client";
import { pool } from "./client";

export type Chain = {
chainId: string;
blockHeight: number;
};

const getChainSql = `
SELECT * FROM "ChainCore" WHERE "chainId" = $1
SELECT * FROM "ChainCore" WHERE "chainId" = $1;
`;
export const getChain = async (chainId: string): Promise<Chain | undefined> => {
try {
const res = await corePool.query(getChainSql, [chainId]);
const res = await pool.query(getChainSql, [chainId]);
return res.rows[0];
} catch (error) {
throw error;
}
};

const createChainSql = `
INSERT INTO "ChainCore" ("chainId", "blockHeight") VALUES ($1, $2) RETURNING *
INSERT INTO "ChainCore" ("chainId", "blockHeight") VALUES ($1, $2) RETURNING *;
`;
export const createChain = async (chainDoc: Chain): Promise<Chain> => {
try {
const res = await corePool.query(createChainSql, [
const res = await pool.query(createChainSql, [
chainDoc.chainId,
chainDoc.blockHeight,
]);
Expand All @@ -33,15 +33,11 @@ export const createChain = async (chainDoc: Chain): Promise<Chain> => {
};

const updateChainSql = `
UPDATE "ChainCore" SET "blockHeight" = $2 WHERE "chainId" = $1
UPDATE "ChainCore" SET "blockHeight" = $2 WHERE "chainId" = $1;
`;
export const updateChain = async (chainDoc: Chain): Promise<Chain> => {
export const updateChain = async (chainDoc: Chain): Promise<void> => {
try {
const res = await corePool.query(updateChainSql, [
chainDoc.chainId,
chainDoc.blockHeight,
]);
return res.rows[0];
await pool.query(updateChainSql, [chainDoc.chainId, chainDoc.blockHeight]);
} catch (error) {
throw error;
}
Expand Down
10 changes: 5 additions & 5 deletions src/postgres/client.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Pool, PoolClient } from "pg";

export const corePool = new Pool({
export const pool = new Pool({
application_name: "Blocksync-core",
connectionString: process.env.DATABASE_URL,
// maximum number of clients the pool should contain
Expand All @@ -17,11 +17,11 @@ export const corePool = new Pool({

// helper function that manages connection transaction start and commit and rollback
// on fail, user can just pass a function that takes a client as argument
export const withCoreTransaction = async (
export const withTransaction = async (
fn: (client: PoolClient) => Promise<any>
) => {
// const start = Date.now();
const client = await corePool.connect();
const client = await pool.connect();
try {
await client.query("BEGIN");
const res = await fn(client);
Expand All @@ -38,9 +38,9 @@ export const withCoreTransaction = async (

// helper function that manages connect to pool and release,
// user can just pass a function that takes a client as argument
export const withCoreQuery = async (fn: (client: any) => Promise<any>) => {
export const withQuery = async (fn: (client: any) => Promise<any>) => {
// const start = Date.now();
const client = await corePool.connect();
const client = await pool.connect();
try {
return await fn(client);
} catch (error) {
Expand Down

0 comments on commit ca74398

Please sign in to comment.