Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
ulixee
GitHub Repository: ulixee/secret-agent
Path: blob/main/core/models/SessionTable.ts
1028 views
1
import { Database as SqliteDatabase } from 'better-sqlite3';
2
import IViewport from '@secret-agent/interfaces/IViewport';
3
import SqliteTable from '@secret-agent/commons/SqliteTable';
4
5
export default class SessionTable extends SqliteTable<ISessionRecord> {
6
constructor(readonly db: SqliteDatabase) {
7
super(
8
db,
9
'Session',
10
[
11
['id', 'TEXT'],
12
['name', 'TEXT'],
13
['browserEmulatorId', 'TEXT'],
14
['browserVersion', 'TEXT'],
15
['humanEmulatorId', 'TEXT'],
16
['screenWidth', 'INTEGER'],
17
['screenHeight', 'INTEGER'],
18
['deviceScaleFactor', 'INTEGER'],
19
['startDate', 'INTEGER'],
20
['closeDate', 'INTEGER'],
21
['scriptInstanceId', 'TEXT'],
22
['scriptEntrypoint', 'TEXT'],
23
['scriptStartDate', 'INTEGER'],
24
['timezoneId', 'TEXT'],
25
['locale', 'TEXT'],
26
['createSessionOptions', 'TEXT'],
27
],
28
true,
29
);
30
}
31
32
public insert(
33
id: string,
34
name: string,
35
browserEmulatorId: string,
36
browserVersion: string,
37
humanEmulatorId: string,
38
startDate: Date,
39
scriptInstanceId: string,
40
scriptEntrypoint: string,
41
scriptStartDate: number,
42
timezoneId: string,
43
viewport: IViewport,
44
locale: string,
45
createSessionOptions: any,
46
) {
47
const record = [
48
id,
49
name,
50
browserEmulatorId,
51
browserVersion,
52
humanEmulatorId,
53
viewport.screenWidth,
54
viewport.screenHeight,
55
viewport.deviceScaleFactor,
56
startDate.getTime(),
57
null,
58
scriptInstanceId,
59
scriptEntrypoint,
60
scriptStartDate,
61
timezoneId,
62
locale,
63
JSON.stringify(createSessionOptions),
64
];
65
this.insertNow(record);
66
}
67
68
public close(id: string, closeDate: Date) {
69
const values = [closeDate.getTime(), id];
70
const fields = ['closeDate'];
71
const sql = `UPDATE ${this.tableName} SET ${fields.map(n => `${n}=?`).join(', ')} WHERE id=?`;
72
this.db.prepare(sql).run(...values);
73
if (this.insertCallbackFn) this.insertCallbackFn([]);
74
}
75
76
public get() {
77
return this.db.prepare(`select * from ${this.tableName}`).get() as ISessionRecord;
78
}
79
}
80
81
export interface ISessionRecord {
82
id: string;
83
name: string;
84
browserEmulatorId: string;
85
browserVersion: string;
86
humanEmulatorId: string;
87
screenWidth: number;
88
screenHeight: number;
89
deviceScaleFactor: number;
90
startDate: number;
91
closeDate: number;
92
scriptInstanceId: string;
93
scriptEntrypoint: string;
94
scriptStartDate: number;
95
timezoneId: string;
96
locale: string;
97
createSessionOptions: string;
98
}
99
100