tangled
alpha
login
or
join now
danabra.mov
/
statusphere-react
forked from
samuel.fm/statusphere-react
0
fork
atom
the statusphere demo reworked into a vite/react app in a monorepo
0
fork
atom
overview
issues
pulls
pipelines
Merge /src/db/* into /src/db.ts
Paul Frazee
2 years ago
10b67361
74dc0f96
+92
-82
4 changed files
expand all
collapse all
unified
split
src
db
index.ts
migrations.ts
schema.ts
db.ts
+92
src/db.ts
···
1
1
+
import SqliteDb from 'better-sqlite3'
2
2
+
import {
3
3
+
Kysely,
4
4
+
Migrator,
5
5
+
SqliteDialect,
6
6
+
Migration,
7
7
+
MigrationProvider,
8
8
+
} from 'kysely'
9
9
+
10
10
+
// Types
11
11
+
12
12
+
export type DatabaseSchema = {
13
13
+
status: Status
14
14
+
auth_session: AuthSession
15
15
+
auth_state: AuthState
16
16
+
}
17
17
+
18
18
+
export type Status = {
19
19
+
authorDid: string
20
20
+
status: string
21
21
+
updatedAt: string
22
22
+
indexedAt: string
23
23
+
}
24
24
+
25
25
+
export type AuthSession = {
26
26
+
key: string
27
27
+
session: AuthSessionJson
28
28
+
}
29
29
+
30
30
+
export type AuthState = {
31
31
+
key: string
32
32
+
state: AuthStateJson
33
33
+
}
34
34
+
35
35
+
type AuthStateJson = string
36
36
+
37
37
+
type AuthSessionJson = string
38
38
+
39
39
+
// Migrations
40
40
+
41
41
+
const migrations: Record<string, Migration> = {}
42
42
+
43
43
+
const migrationProvider: MigrationProvider = {
44
44
+
async getMigrations() {
45
45
+
return migrations
46
46
+
},
47
47
+
}
48
48
+
49
49
+
migrations['001'] = {
50
50
+
async up(db: Kysely<unknown>) {
51
51
+
await db.schema
52
52
+
.createTable('status')
53
53
+
.addColumn('authorDid', 'varchar', (col) => col.primaryKey())
54
54
+
.addColumn('status', 'varchar', (col) => col.notNull())
55
55
+
.addColumn('updatedAt', 'varchar', (col) => col.notNull())
56
56
+
.addColumn('indexedAt', 'varchar', (col) => col.notNull())
57
57
+
.execute()
58
58
+
await db.schema
59
59
+
.createTable('auth_session')
60
60
+
.addColumn('key', 'varchar', (col) => col.primaryKey())
61
61
+
.addColumn('session', 'varchar', (col) => col.notNull())
62
62
+
.execute()
63
63
+
await db.schema
64
64
+
.createTable('auth_state')
65
65
+
.addColumn('key', 'varchar', (col) => col.primaryKey())
66
66
+
.addColumn('state', 'varchar', (col) => col.notNull())
67
67
+
.execute()
68
68
+
},
69
69
+
async down(db: Kysely<unknown>) {
70
70
+
await db.schema.dropTable('auth_state').execute()
71
71
+
await db.schema.dropTable('auth_session').execute()
72
72
+
await db.schema.dropTable('status').execute()
73
73
+
},
74
74
+
}
75
75
+
76
76
+
// APIs
77
77
+
78
78
+
export const createDb = (location: string): Database => {
79
79
+
return new Kysely<DatabaseSchema>({
80
80
+
dialect: new SqliteDialect({
81
81
+
database: new SqliteDb(location),
82
82
+
}),
83
83
+
})
84
84
+
}
85
85
+
86
86
+
export const migrateToLatest = async (db: Database) => {
87
87
+
const migrator = new Migrator({ db, provider: migrationProvider })
88
88
+
const { error } = await migrator.migrateToLatest()
89
89
+
if (error) throw error
90
90
+
}
91
91
+
92
92
+
export type Database = Kysely<DatabaseSchema>
-20
src/db/index.ts
···
1
1
-
import SqliteDb from 'better-sqlite3'
2
2
-
import { Kysely, Migrator, SqliteDialect } from 'kysely'
3
3
-
import { migrationProvider } from './migrations'
4
4
-
import type { DatabaseSchema } from './schema'
5
5
-
6
6
-
export const createDb = (location: string): Database => {
7
7
-
return new Kysely<DatabaseSchema>({
8
8
-
dialect: new SqliteDialect({
9
9
-
database: new SqliteDb(location),
10
10
-
}),
11
11
-
})
12
12
-
}
13
13
-
14
14
-
export const migrateToLatest = async (db: Database) => {
15
15
-
const migrator = new Migrator({ db, provider: migrationProvider })
16
16
-
const { error } = await migrator.migrateToLatest()
17
17
-
if (error) throw error
18
18
-
}
19
19
-
20
20
-
export type Database = Kysely<DatabaseSchema>
-36
src/db/migrations.ts
···
1
1
-
import type { Kysely, Migration, MigrationProvider } from 'kysely'
2
2
-
3
3
-
const migrations: Record<string, Migration> = {}
4
4
-
5
5
-
export const migrationProvider: MigrationProvider = {
6
6
-
async getMigrations() {
7
7
-
return migrations
8
8
-
},
9
9
-
}
10
10
-
11
11
-
migrations['001'] = {
12
12
-
async up(db: Kysely<unknown>) {
13
13
-
await db.schema
14
14
-
.createTable('status')
15
15
-
.addColumn('authorDid', 'varchar', (col) => col.primaryKey())
16
16
-
.addColumn('status', 'varchar', (col) => col.notNull())
17
17
-
.addColumn('updatedAt', 'varchar', (col) => col.notNull())
18
18
-
.addColumn('indexedAt', 'varchar', (col) => col.notNull())
19
19
-
.execute()
20
20
-
await db.schema
21
21
-
.createTable('auth_session')
22
22
-
.addColumn('key', 'varchar', (col) => col.primaryKey())
23
23
-
.addColumn('session', 'varchar', (col) => col.notNull())
24
24
-
.execute()
25
25
-
await db.schema
26
26
-
.createTable('auth_state')
27
27
-
.addColumn('key', 'varchar', (col) => col.primaryKey())
28
28
-
.addColumn('state', 'varchar', (col) => col.notNull())
29
29
-
.execute()
30
30
-
},
31
31
-
async down(db: Kysely<unknown>) {
32
32
-
await db.schema.dropTable('auth_state').execute()
33
33
-
await db.schema.dropTable('auth_session').execute()
34
34
-
await db.schema.dropTable('status').execute()
35
35
-
},
36
36
-
}
-26
src/db/schema.ts
···
1
1
-
export type DatabaseSchema = {
2
2
-
status: Status
3
3
-
auth_session: AuthSession
4
4
-
auth_state: AuthState
5
5
-
}
6
6
-
7
7
-
export type Status = {
8
8
-
authorDid: string
9
9
-
status: string
10
10
-
updatedAt: string
11
11
-
indexedAt: string
12
12
-
}
13
13
-
14
14
-
export type AuthSession = {
15
15
-
key: string
16
16
-
session: AuthSessionJson
17
17
-
}
18
18
-
19
19
-
export type AuthState = {
20
20
-
key: string
21
21
-
state: AuthStateJson
22
22
-
}
23
23
-
24
24
-
type AuthStateJson = string
25
25
-
26
26
-
type AuthSessionJson = string