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

suculent / thinx-device-api / #252646180

18 Feb 2022 10:26PM UTC coverage: 1.813% (-0.3%) from 2.154%
#252646180

push

suculent
submodule update

2 of 587 branches covered (0.34%)

Branch coverage included in aggregate %.

33 of 1344 relevant lines covered (2.46%)

0.06 hits per line

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

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

3
var Globals = require("./globals.js");
1✔
4

×
5
module.exports = class Database {
×
6

×
7
        constructor() {
8
                console.log("[info] Will init DBs...");
9
                this.nano = require("nano")(Globals.app_config().database_uri);
×
10
                console.log("[info] Will access DBs...");
×
11
        }
×
12

13
        nano() {
14
                return this.nano;
15
        }
×
16

17
        null_cb(err, body, header) {
18
                // only unexpected errors should be logged
19
                if (process.env.ENVIRONMENT === "test") {
20
                        console.log(err, body, header);
×
21
                }
22
        }
×
23

×
24
        // Designated initalizer
25
        init(callback) {
26

27
                const dbprefix = Globals.prefix();
28

29
                console.log("[info] Initializing databases...");
30

31
                // only to fix bug in CouchDB 2.3.1 first-run
×
32
                this.nano.db.create("_users", this.null_cb);
33
                this.nano.db.create("_stats", this.null_cb);
×
34
                this.nano.db.create("_replicator", this.null_cb);
35
                this.nano.db.create("_global_changes", this.null_cb);
36

37
                const db_names = [
×
38
                        "devices", "builds", "users", "logs"
39
                ];
×
40

41
                db_names.forEach((name) => {
×
42
                        console.log("[info] Creating database", name);
43
                        // TODO: Get DB before creating to prevent unneccessary logic errors.
×
44
                        this.nano.db.create(dbprefix + "managed_" + name).then((body) => {
×
45
                                console.log(body);
×
46
                                var couch_db = this.nano.db.use(dbprefix + "managed_" + name);
47
                                this.injectDesign(couch_db, name, "./design/design_" + name + ".json");
48
                                this.injectReplFilter(couch_db, "./design/filters_" + name + ".json");
×
49
                                console.log("[info] managed_" + name + " db is ready now.");
50
                        }).catch((err) => {
×
51
                                this.handleDatabaseErrors(err, "managed_" + name);
52
                        });
×
53
                }).then(() => {
×
54
                        if (typeof(callback) !== "undefined") callback();
×
55
                });
×
56

×
57
        }
58

×
59
        // Database preparation on first run
60
        getDocument(file) {
61
                if (!fs.existsSync(file)) {
62
                        return false;
×
63
                }
×
64
                const data = fs.readFileSync(file);
×
65
                if (typeof (data) === "undefined") {
66
                        console.log("» [getDocument] no data read.");
×
67
                        return false;
68
                }
69
                // Parser may fail
70
                try {
×
71
                        return JSON.parse(data);
×
72
                } catch (e) {
73
                        console.log("» Document File may not exist: " + e);
74
                        return false;
75
                }
76
        }
77

78
        logCouchError(err, body, header, tag) {
×
79
                if (err !== null) {
×
80
                        if (err.toString().indexOf("conflict") === -1) {
×
81
                                console.log("[error] Couch Init error: ", err, body, header, tag);
×
82
                        }
×
83
                        if (err.toString().indexOf("ENOTFOUND") !== -1) {
×
84
                                console.log("Critical DB integration error, exiting.");
×
85
                                process.exit(1);
×
86
                        }
87
                } else {
×
88
                        return;
×
89
                }
90
                if (typeof (body) !== "undefined") {
91
                        console.log("[error] Log Couch Insert body: " + body + " " + tag);
92
                }
93
                if (typeof (header) !== "undefined") {
94
                        console.log("[error] Log Couchd Insert header: " + header + " " + tag);
95
                }
×
96
        }
×
97

×
98
        injectDesign(db, design, file) {
99
                if (typeof (design) === "undefined") return;
×
100
                let design_doc = this.getDocument(file);
×
101
                if (design_doc != null) {
×
102
                        db.insert(design_doc, "_design/" + design, (err, body, header) => {
×
103
                                logCouchError(err, body, header, "init:design:" + JSON.stringify(design)); // returns if no err
104
                        });
105
                } else {
×
106
                        console.log("[error] Design doc injection issue at " + file);
×
107
                }
108
        }
×
109

×
110
        injectReplFilter(db, file) {
111
                let filter_doc = this.getDocument(file);
112
                if (filter_doc !== false) {
113
                        db.insert(filter_doc, "_design/repl_filters", (err, body, header) => {
114
                                logCouchError(err, body, header, "init:repl:" + JSON.stringify(filter_doc)); // returns if no err
×
115
                        });
×
116
                } else {
×
117
                        console.log("[error] Filter doc injection issue (no doc) at " + file);
118
                }
×
119
        }
×
120

×
121
        handleDatabaseErrors(err, name) {
122
                if (err.toString().indexOf("the file already exists") !== -1) {
123
                        // silently fail, this is ok
×
124
                } else if (err.toString().indexOf("error happened") !== -1) {
125
                        console.log("[CRITICAL] 🚫 Database connectivity issue. " + err.toString() + " URI: " + app_config.database_uri);
×
126
                        // give some time for DB to wake up until next try, also prevents too fast restarts...
×
127
                        setTimeout(() => {
128
                                process.exit(1);
×
129
                        }, 1000);
×
130
                } else {
131
                        console.log("[CRITICAL] 🚫 Database " + name + " creation failed. " + err + " URI: " + app_config.database_uri);
132
                        setTimeout(() => {
133
                                process.exit(2);
134
                        }, 1000);
×
135
                }
×
136
        }
×
137
};
×
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