-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSentenceDomainRepository.js
109 lines (102 loc) · 2.7 KB
/
SentenceDomainRepository.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/**
* Database repository for sentence domain data.
*/
const sqlite3 = require("sqlite3").verbose();
/**
* Class for interacting with the sentence domain database.
* @class SentenceDomainRepository
*/
class SentenceDomainRepository {
constructor(dbPath) {
this.db = new sqlite3.Database(dbPath);
this.initializeDatabase();
}
/**
* Initializes the database.
* @returns {Promise<void>}
*/
async initializeDatabase() {
try {
await this.db.run(`
CREATE TABLE IF NOT EXISTS sentence_domains (
id INTEGER PRIMARY KEY AUTOINCREMENT,
created_at DATE NOT NULL,
value TEXT,
computed_domain TEXT,
user_domain TEXT,
model TEXT,
total_tokens INTEGER
)
`);
} catch (error) {
console.log("initializeDatabase", error);
}
}
/**
* Inserts data into the database.
* @param {Object} data The data to insert.
* @param {Date} data.created_at The creation date.
* @param {string} data.value The sentence value.
* @param {string} data.computed_domain The computed domain.
* @param {string} data.userDomain The user-provided domain.
* @param {string} data.model The model ID.
* @param {number} data.totalTokens The total number of tokens used.
* @returns {Promise<void>}
*/
async insertData(data) {
try {
const {
created_at,
value,
computed_domain,
userDomain,
model,
totalTokens,
} = data;
await this.db.run(
`
INSERT INTO sentence_domains (created_at, value, computed_domain, user_domain, model, total_tokens)
VALUES (?, ?, ?, ?, ?, ?)
`,
[created_at, value, computed_domain, userDomain, model, totalTokens]
);
console.log("Data inserted successfully!", [
created_at,
value,
computed_domain,
userDomain,
model,
totalTokens,
]);
} catch (error) {
console.error("Error inserting data:", error);
}
}
/**
* Retrieves a sentence from the database.
* @param {string} sentence The sentence value.
* @param {string} model The model ID.
* @returns {Promise<Object>} A promise that resolves with the retrieved sentence data.
*/
async getSentence(sentence, model) {
return new Promise((resolve, reject) => {
this.db.get(
`
SELECT * FROM sentence_domains WHERE value = ? AND model = ?
`,
[sentence, model],
(err, row) => {
if (err) {
reject(err);
} else {
resolve(row);
}
}
);
});
}
close() {
this.db.close();
}
}
module.exports = SentenceDomainRepository;