Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
ulixee
GitHub Repository: ulixee/secret-agent
Path: blob/main/core/models/SessionsTable.ts
1028 views
1
import { Database as SqliteDatabase } from 'better-sqlite3';
2
import SqliteTable from '@secret-agent/commons/SqliteTable';
3
4
export default class SessionsTable extends SqliteTable<ISessionRecord> {
5
constructor(readonly db: SqliteDatabase) {
6
super(db, 'Sessions', [
7
['id', 'TEXT'],
8
['name', 'TEXT'],
9
['startDate', 'TEXT'],
10
['scriptInstanceId', 'TEXT'],
11
['scriptEntrypoint', 'TEXT'],
12
['scriptStartDate', 'TEXT'],
13
]);
14
}
15
16
public insert(
17
id: string,
18
name: string,
19
startDate: number,
20
scriptInstanceId: string,
21
scriptEntrypoint: string,
22
scriptStartDate: number,
23
) {
24
const record = [
25
id,
26
name,
27
new Date(startDate).toISOString(),
28
scriptInstanceId,
29
scriptEntrypoint,
30
new Date(scriptStartDate).toISOString(),
31
];
32
this.insertNow(record);
33
}
34
35
public findByName(name: string, scriptInstanceId: string): ISessionRecord {
36
const sql = `SELECT * FROM ${this.tableName} WHERE name=? AND scriptInstanceId=? ORDER BY scriptStartDate DESC, startDate DESC LIMIT 1`;
37
return this.db.prepare(sql).get([name, scriptInstanceId]) as ISessionRecord;
38
}
39
40
public findByScriptEntrypoint(scriptEntrypoint, limit = 50): ISessionRecord[] {
41
const sql = `SELECT * FROM ${
42
this.tableName
43
} WHERE scriptEntrypoint=? ORDER BY scriptStartDate DESC, startDate DESC limit ${limit ?? 50}`;
44
return this.db.prepare(sql).all([scriptEntrypoint]) as ISessionRecord[];
45
}
46
}
47
48
export interface ISessionRecord {
49
id: string;
50
name: string;
51
startDate: string;
52
scriptInstanceId: string;
53
scriptEntrypoint: string;
54
scriptStartDate: string;
55
}
56
57