Skip to content

Commit 76de3f4

Browse files
committed
use matrix-appservice-bridge support for postgres
1 parent eb66353 commit 76de3f4

File tree

5 files changed

+79
-53
lines changed

5 files changed

+79
-53
lines changed

mx-tester.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ up:
55
- docker run --rm --network $MX_TEST_NETWORK_NAME --name mjolnir-test-postgres --domainname mjolnir-test-postgres -e POSTGRES_PASSWORD=mjolnir-test -e POSTGRES_USER=mjolnir-tester -e POSTGRES_DB=mjolnir-test-db -d -p 127.0.0.1:8083:5432 postgres
66
# Wait until postgresql is ready
77
- until psql postgres://mjolnir-tester:mjolnir-test@localhost:8083/mjolnir-test-db -c ""; do echo "Waiting for psql..."; sleep 1s; done
8-
# Make table in postgres
9-
- psql postgres://mjolnir-tester:mjolnir-test@localhost:8083/mjolnir-test-db -c "CREATE TABLE mjolnir (local_part VARCHAR(255), owner VARCHAR(255), management_room TEXT)"
108
# Launch the reverse proxy, listening for connections *only* on the local host.
119
- docker run --rm --network host --name mjolnir-test-reverse-proxy -p 127.0.0.1:8081:80 -v $MX_TEST_CWD/test/nginx.conf:/etc/nginx/nginx.conf:ro -d nginx
1210
- yarn install

src/appservice/AppService.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ limitations under the License.
1616

1717
import { AppServiceRegistration, Bridge, Request, WeakEvent, BridgeContext, MatrixUser } from "matrix-appservice-bridge";
1818
import { MjolnirManager } from ".//MjolnirManager";
19-
import { DataStore, PgDataStore } from ".//datastore";
19+
import { DataStore } from ".//datastore";
20+
import { PgDataStore } from "./postgres/PgDataStore";
2021
import { Api } from "./Api";
2122
import { IConfig } from "./config/config";
2223
import { AccessControl } from "./AccessControl";

src/appservice/datastore.ts

Lines changed: 0 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1313
See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
16-
import { Client } from "pg";
1716

1817
export interface MjolnirRecord {
1918
local_part: string,
@@ -56,53 +55,4 @@ export interface DataStore {
5655
lookupByLocalPart(localPart: string): Promise<MjolnirRecord[]>;
5756
}
5857

59-
export class PgDataStore implements DataStore {
60-
private pgClient: Client;
6158

62-
constructor(connectionString: string) {
63-
this.pgClient = new Client({ connectionString: connectionString });
64-
}
65-
66-
public async init(): Promise<void> {
67-
await this.pgClient.connect();
68-
}
69-
70-
public async close(): Promise<void> {
71-
await this.pgClient.end()
72-
}
73-
74-
public async list(): Promise<MjolnirRecord[]> {
75-
const result = await this.pgClient.query<MjolnirRecord>("SELECT local_part, owner, management_room FROM mjolnir");
76-
77-
if (!result.rowCount) {
78-
return [];
79-
}
80-
81-
return result.rows;
82-
}
83-
84-
public async store(mjolnirRecord: MjolnirRecord): Promise<void> {
85-
await this.pgClient.query(
86-
"INSERT INTO mjolnir (local_part, owner, management_room) VALUES ($1, $2, $3)",
87-
[mjolnirRecord.local_part, mjolnirRecord.owner, mjolnirRecord.management_room],
88-
);
89-
}
90-
91-
public async lookupByOwner(owner: string): Promise<MjolnirRecord[]> {
92-
const result = await this.pgClient.query<MjolnirRecord>(
93-
"SELECT local_part, owner, management_room FROM mjolnir WHERE owner = $1",
94-
[owner],
95-
);
96-
97-
return result.rows;
98-
}
99-
100-
public async lookupByLocalPart(localPart: string): Promise<MjolnirRecord[]> {
101-
const result = await this.pgClient.query<MjolnirRecord>(
102-
"SELECT local_part, owner, management_room FROM mjolnir WHERE local_part = $1",
103-
[localPart],
104-
);
105-
106-
return result.rows;
107-
}
108-
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
Copyright 2022 The Matrix.org Foundation C.I.C.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
import { PostgresStore } from "matrix-appservice-bridge";
18+
import { DataStore, MjolnirRecord } from "../datastore";
19+
import postgres from 'postgres';
20+
21+
function getSchema(): ((sql: postgres.Sql) => Promise<void>)[] {
22+
const nSchema = 1;
23+
const schema = [];
24+
for (let schemaID = 1; schemaID < nSchema + 1; schemaID++) {
25+
schema.push(require(`./schema/v${schemaID}`).runSchema);
26+
}
27+
return schema;
28+
}
29+
30+
export class PgDataStore extends PostgresStore implements DataStore {
31+
32+
constructor(connectionString: string) {
33+
super(getSchema(), { url: connectionString })
34+
}
35+
36+
public async init(): Promise<void> {
37+
await this.ensureSchema();
38+
}
39+
40+
public async close(): Promise<void> {
41+
await this.close();
42+
}
43+
44+
public async list(): Promise<MjolnirRecord[]> {
45+
const result = await this.sql`SELECT local_part, owner, management_room FROM mjolnir`;
46+
if (!result.count) {
47+
return [];
48+
}
49+
50+
return result.flat() as MjolnirRecord[];
51+
}
52+
53+
public async store(mjolnirRecord: MjolnirRecord): Promise<void> {
54+
await this.sql`INSERT INTO mjolnir (local_part, owner, management_room)
55+
VALUES (${mjolnirRecord.local_part}, ${mjolnirRecord.owner}, ${mjolnirRecord.management_room})`;
56+
}
57+
58+
public async lookupByOwner(owner: string): Promise<MjolnirRecord[]> {
59+
const result = await this.sql`SELECT local_part, owner, management_room FROM mjolnir
60+
WHERE owner = ${owner}`;
61+
return result.flat() as MjolnirRecord[];
62+
}
63+
64+
public async lookupByLocalPart(localPart: string): Promise<MjolnirRecord[]> {
65+
const result = await this.sql`SELECT local_part, owner, management_room FROM mjolnir
66+
WHERE local_part = ${localPart}`;
67+
return result.flat() as MjolnirRecord[];
68+
}
69+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
2+
import postgres from 'postgres';
3+
4+
export async function v0Schema(sql: postgres.Sql) {
5+
await sql.begin(s => [
6+
s`CREATE TABLE mjolnir (local_part VARCHAR(255), owner VARCHAR(255), management_room TEXT);`
7+
]);
8+
}

0 commit comments

Comments
 (0)