-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi-access.repository.ts
More file actions
53 lines (46 loc) · 1.34 KB
/
api-access.repository.ts
File metadata and controls
53 lines (46 loc) · 1.34 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
45
46
47
48
49
50
51
52
53
import { db } from '../db/database'
import type { ApiKeyCreationParams, SelectableApiKey } from './api-access'
import { v4 as uuid } from 'uuid'
export function createApiAccess(
projectId: number,
name: string | null = null
): Promise<SelectableApiKey> {
const insertKey: ApiKeyCreationParams = {
apikey: uuid(),
name,
project_id: projectId
}
return db
.insertInto('apiaccess')
.values(insertKey)
.returningAll()
.executeTakeFirstOrThrow(() => new Error('Error creating Api Access Key'))
}
export function getApiAccessForProject(projectId: number): Promise<SelectableApiKey[]> {
return db.selectFrom('apiaccess').selectAll().where('project_id', '==', projectId).execute()
}
export async function setApiAccessName(keyId: number, name: string): Promise<void> {
await db
.updateTable('apiaccess')
.set({
name
})
.where('id', '==', keyId)
.execute()
}
export async function projectHasKey(projectId: number, apikey: string): Promise<boolean> {
const result = await db
.selectFrom('apiaccess')
.selectAll()
.where('project_id', '==', projectId)
.where('apikey', '==', apikey)
.execute()
return !!result.length
}
export async function deleteApiAccess(projectId: number, apikey: string): Promise<void> {
await db
.deleteFrom('apiaccess')
.where('project_id', '==', projectId)
.where('apikey', '==', apikey)
.execute()
}