Skip to content

Commit

Permalink
HANDLES-72 feat: Specify postgres table containing handles (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
shrink authored Nov 25, 2024
1 parent 44cdd21 commit a2c7e83
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 14 deletions.
5 changes: 4 additions & 1 deletion providers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ export const providers: Record<
> = {
map: (config: string) => HandleMap.fromEnvironmentVariable(config),
pg: (config: string) =>
PostgresHandles.fromEnvironmentVariable(process.env[config] || config),
PostgresHandles.fromEnvironmentVariable(
process.env[config] || config,
process.env.HANDLES_PG_TABLE || "handles",
),
};

export function instantiateProviderFromStringConfiguration(
Expand Down
21 changes: 12 additions & 9 deletions providers/pg.test.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
import { PostgresHandles } from "./pg";

const examplePostgresHandler = new PostgresHandles({
query: (q: string, v: string[]) =>
Promise.resolve({
rows:
v.join() == "alice.at.example.com"
? [{ handle: "alice.at.example.com", did: "did:plc:example1" }]
: [],
}),
});
const examplePostgresHandler = new PostgresHandles(
{
query: (q: string, v: string[]) =>
Promise.resolve({
rows:
v.join() == "alice.at.example.com"
? [{ handle: "alice.at.example.com", did: "did:plc:example1" }]
: [],
}),
},
"handles",
);

describe("PostgresHandles", () => {
test("Finds matching handle", async () => {
Expand Down
14 changes: 10 additions & 4 deletions providers/pg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,14 @@ export type QueriesDatabaseForHandle = {
};

export class PostgresHandles implements FindsDecentralizedIDOfHandle {
constructor(private readonly database: QueriesDatabaseForHandle) {}
constructor(
private readonly database: QueriesDatabaseForHandle,
private readonly table: string,
) {}

async findDecentralizedIDofHandle(handle: string) {
const { rows } = await this.database.query(
"SELECT handle, did FROM handles WHERE handle = $1",
`SELECT handle, did FROM ${this.table} WHERE LOWER(handle) = $1`,
[handle.toLowerCase()],
);

Expand All @@ -23,11 +26,14 @@ export class PostgresHandles implements FindsDecentralizedIDOfHandle {
return rows.length === 1 ? rows[0].did : null;
}

static fromEnvironmentVariable(config: string): PostgresHandles {
static fromEnvironmentVariable(
config: string,
table: string,
): PostgresHandles {
const client = new Pool({ connectionString: config });

client.connect();

return new PostgresHandles(client);
return new PostgresHandles(client, table);
}
}

0 comments on commit a2c7e83

Please sign in to comment.