• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

suculent / thinx-device-api / #252646121

07 May 2025 02:21PM UTC coverage: 5.408% (-66.1%) from 71.538%
#252646121

push

suculent
moved some tests aside

28 of 1292 branches covered (2.17%)

Branch coverage included in aggregate %.

171 of 2388 relevant lines covered (7.16%)

0.08 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

11.85
/lib/thinx/database.js
1
// Database Manager
2

3
const Globals = require("./globals.js");
1✔
4
const app_config = Globals.app_config(); // for (deprecated/development) database_uri
1✔
5
const fs = require("fs-extra");
1✔
6

7
const Filez = require("./files.js");
1✔
8
let ROOT = Filez.appRoot();
1✔
9
module.exports = class Database {
1✔
10

11
        constructor() {
12

13
                let db_uri;
14
                let user = process.env.COUCHDB_USER;
2✔
15
                let pass = process.env.COUCHDB_PASS;
2✔
16

17
                if ((typeof (user) !== "undefined") && (typeof (pass) !== "undefined")) {
2!
18
                        db_uri = `http://${user}:${pass}@couchdb:5984`;
2✔
19
                } else {
20
                        db_uri = app_config.database_uri; // fallback to old config.json; deprecated
×
21
                        console.log("⛔️ [deprecated] Using database credentials from configuration:", db_uri);
×
22
                }
23

24
                this.db_uri = db_uri;
2✔
25
                this.nano = require("nano")(db_uri);
2✔
26

27
        }
28

29
        nano() {
30
                return this.nano;
×
31
        }
32

33
        uri() {
34

35
                /* duplicate code, happens in constructor and that's enough
36
                let db_uri;
37
                let user = process.env.COUCHDB_USER;
38
                let pass = process.env.COUCHDB_PASS;
39

40
                if ((typeof (user) !== "undefined") && (typeof (pass) !== "undefined")) {
41
                        db_uri = `http://${user}:${pass}@couchdb:5984`;
42
                } else {
43
                        db_uri = app_config.database_uri; // fallback to old config.json; deprecated
44
                        console.log("⛔️ [deprecated] Using database credentials from configuration...");
45
                }
46

47
                this.db_uri = db_uri;
48
                */
49

50
                return this.db_uri;
2✔
51
        }
52
        
53

54
        null_cb(err, body, header) {
55
                // only unexpected errors should be logged
56
                if (process.env.ENVIRONMENT === "test") {
×
57
                        // The database may already exist.
58
                        if (err.ststusCode !== 412) {
×
59
                                console.log(err, body, header);
×
60
                        }
61
                }
62
        }
63

64
        // Designated initalizer
65
        init(callback) {
66

67
                console.log("ℹ️ [info] Initializing databases...");
×
68

69
                let db_names = [
×
70
                        "devices", "builds", "users", "logs"
71
                ];
72

73
                this.nano.db.list((_err, existing_dbs) => {
×
74

75
                        if ((typeof(existing_dbs) === "undefined") || (existing_dbs === null)) existing_dbs = [];
×
76

77
                        db_names.forEach((name) => {
×
78

79
                                if (existing_dbs.includes(name)) {
×
80
                                        console.log(`ℹ️ [info] DB ${name} already exists.`);
×
81
                                        return;
×
82
                                }
83

84
                                const dbprefix = Globals.prefix();
×
85

86
                                this.nano.db.create(dbprefix + "managed_" + name).then((/* cerr, data */) => {
×
87
                                        let couch_db = this.nano.db.use(dbprefix + "managed_" + name);
×
88
                                        this.injectDesign(couch_db, name, ROOT + "/design/design_" + name + ".json");
×
89
                                        this.injectReplFilter(couch_db, ROOT + "/design/filters_" + name + ".json");
×
90
                                        console.log(`ℹ️ [info] Database managed_${name} initialized.`);
×
91
                                }).catch((err2) => {
92
                                        // returns error normally if DB already exists
93
                                        this.handleDatabaseErrors(err2, "managed_" + name, dbprefix);
×
94
                                });
95
                        });
96

97
                        this.nano.db.list((err2, new_dbs) => {
×
98
                                if (typeof (callback) !== "undefined") {
×
99
                                        callback(err2, new_dbs);
×
100
                                } else {
101
                                        return new_dbs;
×
102
                                }
103
                        });
104

105
                        setTimeout(() => {
×
106
                                setInterval(this.compactDatabases, 3600 * 1000); // Compact databases once an hour        
×
107
                        }, 30000);
108
                        
109
                });
110
        }
111

112
        compactDatabases(opt_callback) {
113
                const prefix = Globals.prefix();
×
114
                let db_uri = new Database().uri();
×
115
                this.nano = require("nano")(db_uri);
×
116
                this.nano.db.compact(prefix + "managed_logs")
×
117
                .then(() => {
118
                        this.nano.db.compact(prefix + "managed_builds");
×
119
                }).then(() => {
120
                        this.nano.db.compact(prefix + "managed_devices");
×
121
                }).then(() => {
122
                        this.nano.db.compact(prefix + "managed_users");
×
123
                }).then(() => {
124
                        if (typeof (opt_callback) !== "undefined") opt_callback(true);
×
125
                }).catch(e => {
126
                        console.log("InitDB compactDatabases error", e);
×
127
                        if (typeof (opt_callback) !== "undefined") opt_callback(e);
×
128
                });
129
        }
130

131
        // Database preparation on first run
132
        getDocument(file) {
133
                if (!fs.existsSync(file)) {
×
134
                        console.log("☣️ [error] Initializing replication filter failed, file does not exist", file);
×
135
                        return false;
×
136
                }
137
                const data = fs.readFileSync(file);
×
138
                if (typeof (data) === "undefined") {
×
139
                        console.log("☣️ [error] [getDocument] no data read.");
×
140
                        return false;
×
141
                }
142
                // Parser may fail
143
                try {
×
144
                        return JSON.parse(data);
×
145
                } catch (e) {
146
                        console.log("☣️ [error] Document File may not exist: " + e);
×
147
                        return false;
×
148
                }
149
        }
150

151
        logCouchError(err, body, header, tag) {
152
                if (err !== null) {
×
153
                        if (err.toString().indexOf("conflict") === -1) {
×
154
                                console.log("☣️ [error] Couch Init error: ", err, body, header, tag);
×
155
                        }
156
                        if (err.toString().indexOf("ENOTFOUND") !== -1) {
×
157
                                console.log("Critical DB integration error, exiting.");
×
158
                                process.exit(1);
×
159
                        }
160
                } else {
161
                        return;
×
162
                }
163
                if (typeof (body) !== "undefined") {
×
164
                        console.log("☣️ [error] Log Couch Insert body: " + body + " " + tag);
×
165
                }
166
                if (typeof (header) !== "undefined") {
×
167
                        console.log("☣️ [error] Log Couchd Insert header: " + header + " " + tag);
×
168
                }
169
        }
170

171
        injectDesign(db, design, file) {
172
                if (typeof (design) === "undefined") return;
×
173
                let design_doc = this.getDocument(file);
×
174
                if (design_doc != null) {
×
175
                        db.insert(design_doc, "_design/" + design, (err, body, header) => {
×
176
                                this.logCouchError(err, body, header, "init:design:" + design); // returns if no err
×
177
                        });
178
                } else {
179
                        console.log("☣️ [error] Design doc injection issue at " + file);
×
180
                }
181
        }
182

183
        injectReplFilter(db, file) {
184
                let filter_doc = this.getDocument(file);
×
185
                if (filter_doc !== false) {
×
186
                        db.insert(filter_doc, "_design/repl_filters", (err, body, header) => {
×
187
                                this.logCouchError(err, body, header, "init:repl:" + JSON.stringify(filter_doc)); // returns if no err
×
188
                        });
189
                } else {
190
                        console.log("☣️ [error] Filter doc injection issue (no doc) at " + file);
×
191
                }
192
        }
193

194
        handleDatabaseErrors(err, name, info) {
195
                if (err.toString().indexOf("the file already exists") !== -1) {
×
196
                        // silently fail, this is ok
197
                } else if (err.toString().indexOf("error happened") !== -1) {
×
198
                        console.log("🚫 [critical] Database connectivity issue. " + err.toString());
×
199
                        // give some time for DB to wake up until next try, also prevents too fast restarts...
200
                        setTimeout(() => {
×
201
                                process.exit(1);
×
202
                        }, 1000);
203
                } else {
204
                        console.log("🚫 [critical] Database " + name + " creation failed. " + err, " info:", info);
×
205
                        setTimeout(() => {
×
206
                                process.exit(2);
×
207
                        }, 1000);
208
                }
209
        }
210
};
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc