-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
use matrix-appservice-bridge support for postgres
- Loading branch information
Showing
5 changed files
with
78 additions
and
53 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
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,68 @@ | ||
/* | ||
Copyright 2022 The Matrix.org Foundation C.I.C. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
import { PostgresStore, SchemaUpdateFunction } from "matrix-appservice-bridge"; | ||
import { DataStore, MjolnirRecord } from "../datastore"; | ||
|
||
function getSchema(): SchemaUpdateFunction[] { | ||
const nSchema = 1; | ||
const schema = []; | ||
for (let schemaID = 1; schemaID < nSchema + 1; schemaID++) { | ||
schema.push(require(`./schema/v${schemaID}`).runSchema); | ||
} | ||
return schema; | ||
} | ||
|
||
export class PgDataStore extends PostgresStore implements DataStore { | ||
|
||
constructor(connectionString: string) { | ||
super(getSchema(), { url: connectionString }) | ||
} | ||
|
||
public async init(): Promise<void> { | ||
await this.ensureSchema(); | ||
} | ||
|
||
public async close(): Promise<void> { | ||
await this.destroy(); | ||
} | ||
|
||
public async list(): Promise<MjolnirRecord[]> { | ||
const result = await this.sql`SELECT local_part, owner, management_room FROM mjolnir`; | ||
if (!result.count) { | ||
return []; | ||
} | ||
|
||
return result.flat() as MjolnirRecord[]; | ||
} | ||
|
||
public async store(mjolnirRecord: MjolnirRecord): Promise<void> { | ||
await this.sql`INSERT INTO mjolnir (local_part, owner, management_room) | ||
VALUES (${mjolnirRecord.local_part}, ${mjolnirRecord.owner}, ${mjolnirRecord.management_room})`; | ||
} | ||
|
||
public async lookupByOwner(owner: string): Promise<MjolnirRecord[]> { | ||
const result = await this.sql`SELECT local_part, owner, management_room FROM mjolnir | ||
WHERE owner = ${owner}`; | ||
return result.flat() as MjolnirRecord[]; | ||
} | ||
|
||
public async lookupByLocalPart(localPart: string): Promise<MjolnirRecord[]> { | ||
const result = await this.sql`SELECT local_part, owner, management_room FROM mjolnir | ||
WHERE local_part = ${localPart}`; | ||
return result.flat() as MjolnirRecord[]; | ||
} | ||
} |
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,8 @@ | ||
|
||
import postgres from 'postgres'; | ||
|
||
export async function runSchema(sql: postgres.Sql) { | ||
await sql.begin(s => [ | ||
s`CREATE TABLE mjolnir (local_part VARCHAR(255), owner VARCHAR(255), management_room TEXT);` | ||
]); | ||
} |