Skip to content

Commit

Permalink
prettier
Browse files Browse the repository at this point in the history
  • Loading branch information
dillonstreator committed Dec 21, 2023
1 parent e10cbf0 commit 32d4b4c
Show file tree
Hide file tree
Showing 13 changed files with 434 additions and 408 deletions.
35 changes: 17 additions & 18 deletions .github/workflows/coverage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,32 @@ on: [push, pull_request]

jobs:
build:

runs-on: ubuntu-latest

strategy:
matrix:
node-version: [18.x, 20.x]

steps:
- name: Checkout repository
uses: actions/checkout@v2
- name: Checkout repository
uses: actions/checkout@v2

- name: Set up Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}
- name: Set up Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}

- name: Install dependencies
run: yarn install
- name: Install dependencies
run: yarn install

- name: Remove examples dir
run: |
rm -rf examples
- name: Remove examples dir
run: |
rm -rf examples
- name: Run the tests
run: yarn test:ci
- name: Run the tests
run: yarn test:ci

- name: Upload coverage to Codecov
uses: codecov/codecov-action@v2
with:
token: ${{ secrets.CODECOV_TOKEN }}
- name: Upload coverage to Codecov
uses: codecov/codecov-action@v2
with:
token: ${{ secrets.CODECOV_TOKEN }}
3 changes: 3 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
dist
coverage
node_modules
1 change: 1 addition & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
</a>
</p>


## Installation

```sh
Expand Down
2 changes: 1 addition & 1 deletion examples/pg/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
version: '3.8'
version: "3.8"

services:
postgres:
Expand Down
176 changes: 88 additions & 88 deletions examples/pg/index.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
import http from 'node:http';
import { randomUUID } from 'node:crypto';
import { Client } from 'pg';
import { processEvents } from '../../src/processor';
import { createProcessorClient } from '../../src/pg/client';
import dotenv from 'dotenv';
import http from "node:http";
import { randomUUID } from "node:crypto";
import { Client } from "pg";
import { processEvents } from "../../src/processor";
import { createProcessorClient } from "../../src/pg/client";
import dotenv from "dotenv";
dotenv.config();

const eventTypes = {
ResourceSaved: 'ResourceSaved',
ResourceSaved: "ResourceSaved",
} as const;

type EventType = keyof typeof eventTypes;

const main = async () => {
const client = new Client({
user: process.env.POSTGRES_USER,
password: process.env.POSTGRES_PASSWORD,
database: process.env.POSTGRES_DB,
});
await client.connect();
await client.query(`CREATE TABLE IF NOT EXISTS events (
const client = new Client({
user: process.env.POSTGRES_USER,
password: process.env.POSTGRES_PASSWORD,
database: process.env.POSTGRES_DB,
});
await client.connect();
await client.query(`CREATE TABLE IF NOT EXISTS events (
id UUID,
timestamp TIMESTAMPTZ DEFAULT CURRENT_TIMESTAMP,
type VARCHAR(255) NOT NULL,
Expand All @@ -30,7 +30,7 @@ const main = async () => {
backoff_until TIMESTAMPTZ,
processed_at TIMESTAMPTZ
)`);
await client.query(`CREATE TABLE IF NOT EXISTS activity (
await client.query(`CREATE TABLE IF NOT EXISTS activity (
id UUID,
timestamp TIMESTAMPTZ DEFAULT CURRENT_TIMESTAMP,
ip TEXT,
Expand All @@ -40,91 +40,91 @@ const main = async () => {
correlation_id UUID
)`);

const ab = new AbortController();
const ab = new AbortController();

const processorClient = createProcessorClient<EventType>(client);
const processorClient = createProcessorClient<EventType>(client);

const processorTick = () => {
if (ab.signal.aborted) return;
const processorTick = () => {
if (ab.signal.aborted) return;

processEvents(processorClient, {
ResourceSaved: {
thing1: async (event) => {
console.log(`${event.id} thing1 ${event.correlation_id}`);
if (Math.random() > 0.9) throw new Error('some issue');
processEvents(processorClient, {
ResourceSaved: {
thing1: async (event) => {
console.log(`${event.id} thing1 ${event.correlation_id}`);
if (Math.random() > 0.9) throw new Error("some issue");

return;
},
thing2: async (event) => {
console.log(`${event.id} thing2 ${event.correlation_id}`);
if (Math.random() > 0.9) throw new Error('some issue');
return;
},
thing2: async (event) => {
console.log(`${event.id} thing2 ${event.correlation_id}`);
if (Math.random() > 0.9) throw new Error("some issue");

return;
},
thing3: async (event) => {
console.log(`${event.id} thing3 ${event.correlation_id}`);
if (Math.random() > 0.75) throw new Error('some issue');
return;
},
thing3: async (event) => {
console.log(`${event.id} thing3 ${event.correlation_id}`);
if (Math.random() > 0.75) throw new Error("some issue");

return;
},
},
}).finally(() => {
setTimeout(processorTick, 5000);
});
};
processorTick();
return;
},
},
}).finally(() => {
setTimeout(processorTick, 5000);
});
};
processorTick();

const server = http.createServer(async (req, res) => {
const correlationId = randomUUID();
try {
const activityId = randomUUID();
await client.query('BEGIN');
await client.query(
`INSERT INTO activity (id, timestamp, ip, ua, method, path, correlation_id) VALUES (
const server = http.createServer(async (req, res) => {
const correlationId = randomUUID();
try {
const activityId = randomUUID();
await client.query("BEGIN");
await client.query(
`INSERT INTO activity (id, timestamp, ip, ua, method, path, correlation_id) VALUES (
$1, $2, $3, $4, $5, $6, $7
)`,
[
activityId,
new Date(),
req.socket.remoteAddress,
req.headers['user-agent'],
req.method,
req.url,
correlationId,
]
);
await client.query(
`INSERT INTO events (id, type, data, correlation_id, handler_results, errors) VALUES (
[
activityId,
new Date(),
req.socket.remoteAddress,
req.headers["user-agent"],
req.method,
req.url,
correlationId,
],
);
await client.query(
`INSERT INTO events (id, type, data, correlation_id, handler_results, errors) VALUES (
$1, $2, $3, $4, $5, $6
)`,
[
randomUUID(),
eventTypes.ResourceSaved,
{
type: 'activity',
id: activityId,
},
correlationId,
{},
0,
]
);
await client.query('COMMIT');
res.statusCode = 201;
} catch (e) {
await client.query('ROLLBACK');
console.log(e);
res.statusCode = 500;
}
res.end();
});
const port = process.env.PORT || 3000;
server.listen(port, () => console.log(`listening on ${port}`));
[
randomUUID(),
eventTypes.ResourceSaved,
{
type: "activity",
id: activityId,
},
correlationId,
{},
0,
],
);
await client.query("COMMIT");
res.statusCode = 201;
} catch (e) {
await client.query("ROLLBACK");
console.log(e);
res.statusCode = 500;
}
res.end();
});
const port = process.env.PORT || 3000;
server.listen(port, () => console.log(`listening on ${port}`));
};

if (require.main === module) {
main().catch((err) => {
console.error(err);
process.exit(1);
});
main().catch((err) => {
console.error(err);
process.exit(1);
});
}
24 changes: 12 additions & 12 deletions examples/pg/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
{
"compilerOptions": {
"target": "ESNext",
"module": "NodeNext",
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"strict": true,
"skipLibCheck": true,
"outDir": "dist",
"declaration": true
},
"include": ["index.ts"],
"exclude": ["dist", "node_modules"]
"compilerOptions": {
"target": "ESNext",
"module": "NodeNext",
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"strict": true,
"skipLibCheck": true,
"outDir": "dist",
"declaration": true
},
"include": ["index.ts"],
"exclude": ["dist", "node_modules"]
}
16 changes: 8 additions & 8 deletions src/pg/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ describe("getUnprocessedEvents", () => {
query: vi.fn<any, any>(() =>
Promise.resolve({
rows,
})
}),
),
};
const opts = {
Expand All @@ -30,7 +30,7 @@ describe("getUnprocessedEvents", () => {
expect(pgClient.query).toHaveBeenCalledOnce();
expect(pgClient.query).toHaveBeenCalledWith(
"SELECT id, errors FROM events WHERE processed_at IS NULL AND (backoff_until IS NULL OR backoff_until < NOW()) AND errors < $1",
[opts.maxErrors]
[opts.maxErrors],
);
expect(result).toBe(rows);
});
Expand Down Expand Up @@ -70,7 +70,7 @@ describe("transaction", () => {
Promise.resolve({
rows,
rowCount: rows.length,
})
}),
),
};
const eventId = "123";
Expand All @@ -83,7 +83,7 @@ describe("transaction", () => {
expect(pgClient.query).toHaveBeenCalledTimes(3);
expect(pgClient.query).toHaveBeenCalledWith(
"SELECT id, timestamp, type, data, correlation_id, handler_results, errors, backoff_until, processed_at FROM events WHERE id = $1 FOR UPDATE SKIP LOCKED",
[eventId]
[eventId],
);
expect(result).toBe(1);
});
Expand All @@ -95,7 +95,7 @@ describe("transaction", () => {
Promise.resolve({
rows,
rowCount: rows.length,
})
}),
),
};
const eventId = "123";
Expand All @@ -108,7 +108,7 @@ describe("transaction", () => {
expect(pgClient.query).toHaveBeenCalledTimes(3);
expect(pgClient.query).toHaveBeenCalledWith(
"SELECT id, timestamp, type, data, correlation_id, handler_results, errors, backoff_until, processed_at FROM events WHERE id = $1 FOR UPDATE SKIP LOCKED",
[eventId]
[eventId],
);
expect(result).toBeNull();
});
Expand All @@ -121,7 +121,7 @@ describe("transaction", () => {
query: vi.fn<any, any>(() =>
Promise.resolve({
rows,
})
}),
),
};
const event = {
Expand Down Expand Up @@ -151,7 +151,7 @@ describe("transaction", () => {
event.processed_at,
event.backoff_until,
event.id,
]
],
);
});
});
Expand Down
Loading

0 comments on commit 32d4b4c

Please sign in to comment.