Skip to content

Commit 963014a

Browse files
committed
Working test for saving outbox entries.
1 parent e93bdad commit 963014a

File tree

4 files changed

+58
-57
lines changed

4 files changed

+58
-57
lines changed
Lines changed: 10 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,18 @@
1-
# outbox-core
1+
# outbox-prisma-adapter
22

3-
Main package that contains the core functionality of the Outbox pattern to provide "at least once" delivery semantics for messages.
3+
This package provides a Prisma adapter for the Outbox pattern.
44

5-
## Installation
5+
### Development
66

7-
```bash
8-
npm i -S @message-queue-toolkit/outbox-core
9-
```
10-
11-
## Usage
12-
13-
To process outbox entries and emit them to the message queue, you need to create an instance of the `OutboxPeriodicJob` class:
7+
#### Tests
148

15-
```typescript
16-
import { OutboxPeriodicJob } from '@message-queue-toolkit/outbox-core';
9+
To run the tests, you need to have a PostgreSQL database running. You can use the following command to start a PostgreSQL database using Docker:
1710

18-
const job = new OutboxPeriodicJob(
19-
//Implementation of OutboxStorage interface, TODO: Point to other packages in message-queue-toolkit
20-
outboxStorage,
21-
//Default available accumulator for gathering outbox entries as the process job is progressing.
22-
new InMemoryOutboxAccumulator(),
23-
//DomainEventEmitter, it will be used to publish events, see @message-queue-toolkit/core
24-
eventEmitter,
25-
//See PeriodicJobDependencies from @lokalise/background-jobs-common
26-
dependencies,
27-
//Retry count, how many times outbox entries should be retried to be processed
28-
3,
29-
//emitBatchSize - how many outbox entries should be emitted at once
30-
10,
31-
//internalInMs - how often the job should be executed, e.g. below it runs every 1sec
32-
1000
33-
)
11+
```sh
12+
docker-compose up -d
3413
```
3514

36-
Job will take care of processing outbox entries emitted by:
37-
```typescript
38-
const emitter = new OutboxEventEmitter(
39-
//Same instance of outbox storage that is used by OutboxPeriodicJob
40-
outboxStorage
41-
)
15+
Then update Prisma client:
16+
```sh
17+
npx prisma generate --schema=./test/schema.prisma
4218
```

packages/outbox-prisma-adapter/lib/outbox-prisma-adapter.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,15 @@ export class OutboxPrismaAdapter<SupportedEvents extends CommonEventDefinition[]
1616
): Promise<OutboxEntry<SupportedEvents[number]>> {
1717
const prismaModel: PrismaClient[typeof this.modelName] = this.prisma[this.modelName]
1818

19+
const messageType = getMessageType(outboxEntry.event)
1920
return prismaModel.create({
20-
data: getMessageType(outboxEntry.event),
21+
data: {
22+
id: outboxEntry.id,
23+
type: messageType,
24+
created: outboxEntry.created,
25+
data: outboxEntry.data,
26+
status: outboxEntry.status,
27+
},
2128
})
2229
}
2330

@@ -26,6 +33,12 @@ export class OutboxPrismaAdapter<SupportedEvents extends CommonEventDefinition[]
2633
}
2734

2835
getEntries(maxRetryCount: number): Promise<OutboxEntry<SupportedEvents[number]>[]> {
29-
return Promise.resolve([])
36+
return this.prisma[this.modelName].findMany({
37+
where: {
38+
retryCount: {
39+
lte: maxRetryCount,
40+
},
41+
},
42+
})
3043
}
3144
}

packages/outbox-prisma-adapter/test/outbox-prisma-adapter.spec.ts

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,11 @@ describe('outbox-prisma-adapter', () => {
3535
await prisma.$queryRaw`
3636
CREATE TABLE prisma.outbox_entry (
3737
id UUID PRIMARY KEY,
38-
created TIMESTAMP NOT NULL
38+
type TEXT NOT NULL,
39+
created TIMESTAMP NOT NULL,
40+
retry_count INT NOT NULL DEFAULT 0,
41+
data JSONB NOT NULL,
42+
status TEXT NOT NULL
3943
)
4044
`
4145
})
@@ -46,24 +50,6 @@ describe('outbox-prisma-adapter', () => {
4650
await prisma.$disconnect()
4751
})
4852

49-
it('test db connection', async () => {
50-
const creationDate = new Date()
51-
await prisma.outboxEntry.create({
52-
data: {
53-
id: 'ce08b43b-6162-4913-86ea-fa9367875e3b',
54-
created: creationDate,
55-
},
56-
})
57-
58-
const result = await prisma.outboxEntry.findMany()
59-
expect(result).toEqual([
60-
{
61-
id: 'ce08b43b-6162-4913-86ea-fa9367875e3b',
62-
created: creationDate,
63-
},
64-
])
65-
})
66-
6753
it('creates entry in DB via outbox storage implementation', async () => {
6854
await outboxPrismaAdapter.createEntry({
6955
id: uuidv7(),
@@ -77,6 +63,28 @@ describe('outbox-prisma-adapter', () => {
7763
metadata: {},
7864
timestamp: new Date().toISOString(),
7965
},
66+
retryCount: 0,
67+
created: new Date(),
8068
} satisfies OutboxEntry<SupportedEvents[number]>)
69+
70+
const entries = await outboxPrismaAdapter.getEntries(10)
71+
72+
expect(entries).toEqual([
73+
{
74+
id: expect.any(String),
75+
type: 'entity.created',
76+
created: expect.any(Date),
77+
retryCount: 0,
78+
data: {
79+
id: expect.any(String),
80+
payload: {
81+
message: 'TEST EVENT',
82+
},
83+
metadata: {},
84+
timestamp: expect.any(String),
85+
},
86+
status: 'CREATED',
87+
},
88+
])
8189
})
8290
})

packages/outbox-prisma-adapter/test/schema.prisma

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,12 @@ datasource db {
44
}
55

66
model OutboxEntry {
7-
id String @id @default(uuid()) @db.Uuid
8-
created DateTime @default(now())
7+
id String @id @default(uuid()) @db.Uuid
8+
created DateTime @default(now())
9+
type String
10+
retryCount Int @default(0) @map("retry_count")
11+
data Json
12+
status String
913
1014
@@map("outbox_entry")
1115
}

0 commit comments

Comments
 (0)