-
Notifications
You must be signed in to change notification settings - Fork 0
/
database.js
58 lines (51 loc) · 1.8 KB
/
database.js
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
const sqlite3 = require('sqlite3').verbose();
const DBSOURCE = 'db.sqlite';
const config = require('./config');
const ACTIVE_PORTS_TABLE_NAME = 'recording_active_ports';
const AVAILABLE_PORTS = Array.from(
{ length: config.mediasoup.recording.rtpRecordingMaxPort - config.mediasoup.recording.rtpRecordingMinPort },
(_, i) => config.mediasoup.recording.rtpRecordingMinPort + i
);
const db = new sqlite3.Database(DBSOURCE, (err) => {
if (err) {
console.error(err.message);
throw err;
} else {
console.log('Connected to the SQlite database.');
// yeah we can use "CREATE TABLE if not exists", but also need to seed the table once
db.all("SELECT name FROM sqlite_master WHERE type='table' AND name='recording_active_ports'", [], (err, rows) => {
if (err) {
console.error(err.message);
return;
}
if (rows.length === 0) {
db.run(
`CREATE TABLE ${ACTIVE_PORTS_TABLE_NAME} (
id INTEGER PRIMARY KEY AUTOINCREMENT,
value INTEGER UNIQUE,
kind TEXT,
session_id TEXT,
locked_at DATETIME,
CONSTRAINT value_unique UNIQUE (value),
CONSTRAINT kind_of_session_unique UNIQUE (session_id, kind)
)`, function (err, _result) {
if (err) {
console.error(err.message);
throw(err);
}
AVAILABLE_PORTS.forEach((port) => {
const query = `INSERT INTO ${ACTIVE_PORTS_TABLE_NAME} (value) VALUES (?)`;
db.run(query, [port], function (err, _result) {
if (err) {
console.error(err.message);
throw(err);
}
});
});
}
);
}
});
}
});
module.exports = { db, ACTIVE_PORTS_TABLE_NAME };