-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: DrizzleOutboxMessageRepository
- Loading branch information
Showing
7 changed files
with
269 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
CREATE TABLE IF NOT EXISTS "outbox_messages" ( | ||
"id" uuid PRIMARY KEY NOT NULL, | ||
"payload" jsonb NOT NULL, | ||
"type" varchar(255) NOT NULL, | ||
"created_at" timestamp DEFAULT now() NOT NULL, | ||
"updated_at" timestamp DEFAULT now() NOT NULL | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,206 @@ | ||
{ | ||
"id": "84449fea-aace-45f9-94cd-ddde8ecb0f41", | ||
"prevId": "20fb321c-a6d4-4b5a-a63a-013ae90bb63f", | ||
"version": "7", | ||
"dialect": "postgresql", | ||
"tables": { | ||
"public.accounts": { | ||
"name": "accounts", | ||
"schema": "", | ||
"columns": { | ||
"id": { | ||
"name": "id", | ||
"type": "uuid", | ||
"primaryKey": true, | ||
"notNull": true | ||
}, | ||
"email": { | ||
"name": "email", | ||
"type": "varchar(255)", | ||
"primaryKey": false, | ||
"notNull": true | ||
}, | ||
"password": { | ||
"name": "password", | ||
"type": "varchar(255)", | ||
"primaryKey": false, | ||
"notNull": true | ||
}, | ||
"created_at": { | ||
"name": "created_at", | ||
"type": "timestamp", | ||
"primaryKey": false, | ||
"notNull": true, | ||
"default": "now()" | ||
}, | ||
"updated_at": { | ||
"name": "updated_at", | ||
"type": "timestamp", | ||
"primaryKey": false, | ||
"notNull": true, | ||
"default": "now()" | ||
} | ||
}, | ||
"indexes": { | ||
"emailUniqueIndex": { | ||
"name": "emailUniqueIndex", | ||
"columns": [ | ||
{ | ||
"expression": "lower(\"email\")", | ||
"asc": true, | ||
"isExpression": true, | ||
"nulls": "last" | ||
} | ||
], | ||
"isUnique": true, | ||
"concurrently": false, | ||
"method": "btree", | ||
"with": {} | ||
} | ||
}, | ||
"foreignKeys": {}, | ||
"compositePrimaryKeys": {}, | ||
"uniqueConstraints": {}, | ||
"policies": {}, | ||
"checkConstraints": {}, | ||
"isRLSEnabled": false | ||
}, | ||
"public.password_reset_requests": { | ||
"name": "password_reset_requests", | ||
"schema": "", | ||
"columns": { | ||
"id": { | ||
"name": "id", | ||
"type": "uuid", | ||
"primaryKey": true, | ||
"notNull": true | ||
}, | ||
"account_id": { | ||
"name": "account_id", | ||
"type": "uuid", | ||
"primaryKey": false, | ||
"notNull": false | ||
}, | ||
"token": { | ||
"name": "token", | ||
"type": "varchar(255)", | ||
"primaryKey": false, | ||
"notNull": true | ||
}, | ||
"expires_at": { | ||
"name": "expires_at", | ||
"type": "timestamp", | ||
"primaryKey": false, | ||
"notNull": true | ||
}, | ||
"created_at": { | ||
"name": "created_at", | ||
"type": "timestamp", | ||
"primaryKey": false, | ||
"notNull": true, | ||
"default": "now()" | ||
}, | ||
"updated_at": { | ||
"name": "updated_at", | ||
"type": "timestamp", | ||
"primaryKey": false, | ||
"notNull": true, | ||
"default": "now()" | ||
} | ||
}, | ||
"indexes": { | ||
"uniqueAccountResetRequest": { | ||
"name": "uniqueAccountResetRequest", | ||
"columns": [ | ||
{ | ||
"expression": "account_id", | ||
"isExpression": false, | ||
"asc": true, | ||
"nulls": "last" | ||
} | ||
], | ||
"isUnique": true, | ||
"concurrently": false, | ||
"method": "btree", | ||
"with": {} | ||
} | ||
}, | ||
"foreignKeys": { | ||
"password_reset_requests_account_id_accounts_id_fk": { | ||
"name": "password_reset_requests_account_id_accounts_id_fk", | ||
"tableFrom": "password_reset_requests", | ||
"tableTo": "accounts", | ||
"columnsFrom": [ | ||
"account_id" | ||
], | ||
"columnsTo": [ | ||
"id" | ||
], | ||
"onDelete": "no action", | ||
"onUpdate": "no action" | ||
} | ||
}, | ||
"compositePrimaryKeys": {}, | ||
"uniqueConstraints": {}, | ||
"policies": {}, | ||
"checkConstraints": {}, | ||
"isRLSEnabled": false | ||
}, | ||
"public.outbox_messages": { | ||
"name": "outbox_messages", | ||
"schema": "", | ||
"columns": { | ||
"id": { | ||
"name": "id", | ||
"type": "uuid", | ||
"primaryKey": true, | ||
"notNull": true | ||
}, | ||
"payload": { | ||
"name": "payload", | ||
"type": "jsonb", | ||
"primaryKey": false, | ||
"notNull": true | ||
}, | ||
"type": { | ||
"name": "type", | ||
"type": "varchar(255)", | ||
"primaryKey": false, | ||
"notNull": true | ||
}, | ||
"created_at": { | ||
"name": "created_at", | ||
"type": "timestamp", | ||
"primaryKey": false, | ||
"notNull": true, | ||
"default": "now()" | ||
}, | ||
"updated_at": { | ||
"name": "updated_at", | ||
"type": "timestamp", | ||
"primaryKey": false, | ||
"notNull": true, | ||
"default": "now()" | ||
} | ||
}, | ||
"indexes": {}, | ||
"foreignKeys": {}, | ||
"compositePrimaryKeys": {}, | ||
"uniqueConstraints": {}, | ||
"policies": {}, | ||
"checkConstraints": {}, | ||
"isRLSEnabled": false | ||
} | ||
}, | ||
"enums": {}, | ||
"schemas": {}, | ||
"sequences": {}, | ||
"roles": {}, | ||
"policies": {}, | ||
"views": {}, | ||
"_meta": { | ||
"columns": {}, | ||
"schemas": {}, | ||
"tables": {} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
src/shared-kernel/infrastructure/database/drizzle/schema.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { NodePgDatabase } from "drizzle-orm/node-postgres"; | ||
import { jsonb, pgTable, timestamp, uuid, varchar } from "drizzle-orm/pg-core"; | ||
import { randomUUID } from "node:crypto"; | ||
|
||
export const outboxMessageSchema = pgTable("outbox_messages", { | ||
id: uuid("id") | ||
.$defaultFn(() => randomUUID()) | ||
.primaryKey(), | ||
payload: jsonb("payload").notNull().$type<Record<string, unknown>>(), | ||
type: varchar("type", { length: 255 }).notNull(), | ||
createdAt: timestamp("created_at").defaultNow().notNull(), | ||
updatedAt: timestamp("updated_at") | ||
.defaultNow() | ||
.notNull() | ||
.$defaultFn(() => new Date()), | ||
}); | ||
|
||
export type SharedKernelDatabase = NodePgDatabase<{ | ||
outboxMessages: typeof outboxMessageSchema; | ||
}>; |
25 changes: 25 additions & 0 deletions
25
src/shared-kernel/infrastructure/repositories/drizzle-outbox-message.repository.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { Inject, Injectable } from "@nestjs/common"; | ||
import type { OutboxMessage } from "@shared-kernel/domain/outbox-message/aggregate-root.js"; | ||
import type { OutboxMessageRepository } from "@shared-kernel/domain/outbox-message/repository.js"; | ||
import { DrizzlePostgresPoolToken } from "../database/drizzle/constants.js"; | ||
import { | ||
outboxMessageSchema, | ||
SharedKernelDatabase, | ||
} from "../database/drizzle/schema.js"; | ||
|
||
@Injectable() | ||
export class DrizzleOutboxMessageRepository implements OutboxMessageRepository { | ||
constructor( | ||
@Inject(DrizzlePostgresPoolToken) | ||
private readonly database: SharedKernelDatabase | ||
) {} | ||
|
||
async save(messages: OutboxMessage[]) { | ||
const snapshots = messages.map((message) => message.properties); | ||
|
||
await this.database | ||
.insert(outboxMessageSchema) | ||
.values(snapshots) | ||
.onConflictDoNothing(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters