-
Notifications
You must be signed in to change notification settings - Fork 114
/
postgres_initializer.ts
61 lines (45 loc) · 1.63 KB
/
postgres_initializer.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import { PostgresSaver } from "@langchain/langgraph-checkpoint-postgres";
import {
PostgreSqlContainer,
type StartedPostgreSqlContainer,
} from "@testcontainers/postgresql";
import pg from "pg";
import type { CheckpointerTestInitializer } from "../types.js";
const dbName = "test_db";
const container = new PostgreSqlContainer("postgres:16.2")
.withDatabase("postgres")
.withUsername("postgres")
.withPassword("postgres");
let startedContainer: StartedPostgreSqlContainer;
let client: pg.Pool | undefined;
export const initializer: CheckpointerTestInitializer<PostgresSaver> = {
checkpointerName: "@langchain/langgraph-checkpoint-postgres",
async beforeAll() {
startedContainer = await container.start();
},
beforeAllTimeout: 300_000, // five minutes, to pull docker container
async afterAll() {
await startedContainer.stop();
},
async createCheckpointer() {
client = new pg.Pool({
connectionString: startedContainer.getConnectionUri(),
});
await client?.query(`CREATE DATABASE ${dbName}`);
const url = new URL("", "postgres://");
url.hostname = startedContainer.getHost();
url.port = startedContainer.getPort().toString();
url.pathname = dbName;
url.username = startedContainer.getUsername();
url.password = startedContainer.getPassword();
const checkpointer = PostgresSaver.fromConnString(url.toString());
await checkpointer.setup();
return checkpointer;
},
async destroyCheckpointer(checkpointer: PostgresSaver) {
await checkpointer.end();
await client?.query(`DROP DATABASE ${dbName}`);
await client?.end();
},
};
export default initializer;