-
-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathapps-queries.ts
More file actions
44 lines (40 loc) · 1.45 KB
/
Copy pathapps-queries.ts
File metadata and controls
44 lines (40 loc) · 1.45 KB
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
import { ClientBase, QueryResult } from 'pg';
import { ApiApp } from '../shapes/app.js';
import { camelize, KeysToSnakeCase, TypesForKeys } from '../utils.js';
/**
* Get all registered apps.
*/
export async function getAllApps(client: ClientBase): Promise<ApiApp[]> {
const results = await client.query<KeysToSnakeCase<ApiApp>>({
name: 'get_all_apps',
text: 'SELECT * FROM apps',
});
return results.rows.map((row) => camelize(row));
}
// TODO: Add a last modified column to apps and use that for more efficient syncing. This will require us to be able to disable apps rather than deleting them. And we need an index on that column.
/**
* Get an app by its ID.
*/
export async function getAppById(client: ClientBase, id: string): Promise<ApiApp | null> {
const results = await client.query<KeysToSnakeCase<ApiApp>>({
name: 'get_apps',
text: 'SELECT * FROM apps where id = $1',
values: [id],
});
if (results.rows.length > 0) {
return camelize(results.rows[0]);
} else {
return null;
}
}
/**
* Insert a new app into the list of registered apps.
*/
export async function insertApp(client: ClientBase, app: ApiApp): Promise<QueryResult> {
return client.query<any, TypesForKeys<ApiApp, ['id', 'bungieApiKey', 'dimApiKey', 'origin']>>({
name: 'insert_app',
text: `insert into apps (id, bungie_api_key, dim_api_key, origin)
values ($1, $2, $3, $4)`,
values: [app.id, app.bungieApiKey, app.dimApiKey, app.origin],
});
}