-
Notifications
You must be signed in to change notification settings - Fork 80
Expand file tree
/
Copy pathindex.ts
More file actions
60 lines (54 loc) · 1.66 KB
/
index.ts
File metadata and controls
60 lines (54 loc) · 1.66 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
54
55
56
57
58
59
60
import { StarbaseApp } from '../../src/handler'
import { StarbasePlugin } from '../../src/plugin'
import { DataSource } from '../../src/types'
const SQL = {
CREATE_REPLICATION_JOBS: `
CREATE TABLE IF NOT EXISTS tmp_replication_jobs (
id INTEGER PRIMARY KEY AUTOINCREMENT,
source_name TEXT NOT NULL,
table_name TEXT NOT NULL,
cursor_column TEXT DEFAULT 'updated_at',
last_cursor_value TEXT,
status TEXT NOT NULL DEFAULT 'idle',
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP
)
`,
CREATE_REPLICATION_RUNS: `
CREATE TABLE IF NOT EXISTS tmp_replication_runs (
id INTEGER PRIMARY KEY AUTOINCREMENT,
job_id INTEGER NOT NULL,
rows_synced INTEGER NOT NULL DEFAULT 0,
status TEXT NOT NULL,
error_message TEXT,
started_at DATETIME DEFAULT CURRENT_TIMESTAMP,
finished_at DATETIME,
FOREIGN KEY(job_id) REFERENCES tmp_replication_jobs(id)
)
`,
}
export class PullReplicatorPlugin extends StarbasePlugin {
private dataSource?: DataSource
constructor() {
super('starbasedb:pull-replicator')
}
override async register(app: StarbaseApp) {
app.use(async (c, next) => {
this.dataSource = c?.get('dataSource')
await this.ensureTables(this.dataSource)
await next()
})
app.get('/api/plugins/pull-replicator/health', (c) => {
return c.json({
ok: true,
plugin: this.name,
hasDataSource: !!this.dataSource,
})
})
}
private async ensureTables(dataSource?: DataSource) {
if (!dataSource) return
await dataSource.rpc.executeQuery({ sql: SQL.CREATE_REPLICATION_JOBS, params: [] })
await dataSource.rpc.executeQuery({ sql: SQL.CREATE_REPLICATION_RUNS, params: [] })
}
}