@unvired/cordova-plugin-unvired-electron-db 0.0.36

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,215 @@
1
+ const { app } = require('electron');
2
+ const path = require('path');
3
+ const fs = require('fs');
4
+ const sqlite3 = require('sqlite3').verbose();
5
+ const userDataPath = app.getPath('userData');
6
+ var appDbFilePath = null;
7
+ var fwDbFilePath = null;
8
+ var appDb = null;
9
+ var fwDb = null;
10
+
11
+ const DBType = {
12
+ AppDb: "AppDb",
13
+ FrameworkDb: "FrameworkDb"
14
+ }
15
+
16
+ const create = (args) => {
17
+ if (args[0].userId == undefined || args[0].userId == null || args[0].userId == "") {
18
+ throw "userId is required"
19
+ }
20
+ const userPath = getUserDirectoryPath(args);
21
+
22
+ fwDbFilePath = path.join(userPath, `framework.db`);
23
+ fwDb = new sqlite3.Database(fwDbFilePath, (err) => {
24
+ if (err) {
25
+ throw err.message
26
+ } else {
27
+ // console.log('Connected to database:', fwDbFilePath);
28
+ // Set PRAGMA for framework database
29
+ fwDb.run("PRAGMA journal_mode = WAL;");
30
+ fwDb.run("PRAGMA read_uncommitted = 1;");
31
+ return("Connected to database at " + fwDbFilePath)
32
+ }
33
+ });
34
+
35
+ appDbFilePath = path.join(userPath, `app.db`);
36
+ appDb = new sqlite3.Database(appDbFilePath, (err) => {
37
+ if (err) {
38
+ throw err.message
39
+ } else {
40
+ // console.log('Connected to database:', appDbFilePath);
41
+ // Set PRAGMA for app database
42
+ appDb.run("PRAGMA journal_mode = WAL;");
43
+ appDb.run("PRAGMA read_uncommitted = 1;");
44
+ appDb.run("PRAGMA foreign_keys = ON;");
45
+ return("Connected to database at " + appDbFilePath)
46
+ }
47
+ });
48
+ }
49
+
50
+ const execute = (args) => {
51
+ return new Promise((resolve,reject) => {
52
+ const userId = args[0].userId
53
+ const dbType = args[0].dbType
54
+ const query = args[0].query;
55
+
56
+ if (userId == undefined || userId == null || userId == "") {
57
+ reject("userId is required")
58
+ return;
59
+ }
60
+
61
+ if (dbType != DBType.AppDb && dbType != DBType.FrameworkDb) {
62
+ reject("Invalid dbType")
63
+ return;
64
+ }
65
+
66
+ if (query == undefined || query==null || query=="") {
67
+ reject("query is required")
68
+ return;
69
+ }
70
+
71
+ const db = dbType == DBType.AppDb ? appDb : fwDb;
72
+
73
+ if(db == null || db == undefined) {
74
+ try {
75
+ create(args);
76
+ } catch (error) {
77
+ reject(error);
78
+ return;
79
+ }
80
+ }
81
+
82
+ if(query.toUpperCase().includes("SELECT")) {
83
+ db.all(query, [], (err, rows) => {
84
+ if (err) {
85
+ reject(err)
86
+ } else {
87
+ resolve(rows)
88
+ }
89
+ });
90
+ }
91
+ else {
92
+ db.run(query, (err) => {
93
+ if (err) {
94
+ reject(err)
95
+ } else {
96
+ resolve("Success")
97
+ }
98
+ });
99
+ }
100
+ });
101
+ }
102
+
103
+ const close = ()=>{
104
+ if (appDb) {
105
+ appDb.close();
106
+ }
107
+ if (fwDb) {
108
+ fwDb.close();
109
+ }
110
+ appDb = null;
111
+ fwDb = null;
112
+ return("DB connection closed")
113
+ }
114
+
115
+ const getDBFilePath = (args) => {
116
+ const dbType = args[0].dbType;
117
+ switch (dbType) {
118
+ case DBType.AppDb:
119
+ return appDbFilePath;
120
+ case DBType.FrameworkDb:
121
+ return fwDbFilePath;
122
+ default:
123
+ return null;
124
+ }
125
+ }
126
+
127
+ const getUserDirectoryPath = (args) => {
128
+ const userId = args[0].userId;
129
+ if (userId == undefined || userId == null || userId == "") {
130
+ return null;
131
+ }
132
+ const userPath = path.join(userDataPath, `${userId}`);
133
+ if (!fs.existsSync(userPath)) {
134
+ fs.mkdirSync(userPath, { recursive: true });
135
+ }
136
+ return userPath;
137
+ }
138
+
139
+ const saveWebDB = (args) => {
140
+ return "Web DB save is only for browser."
141
+ }
142
+
143
+ /**
144
+ * Deletes the user directory and all its contents for the given userId.
145
+ * @param {Array} args - Array with an object containing userId.
146
+ * @returns {string} - Success or error message.
147
+ */
148
+ const deleteUserData = (args) => {
149
+ const userId = args[0]?.userId;
150
+ if (!userId) {
151
+ throw new Error("userId is required");
152
+ }
153
+ const userPath = path.join(userDataPath, `${userId}`);
154
+ if (!fs.existsSync(userPath)) {
155
+ return `User directory does not exist: ${userPath}`;
156
+ }
157
+ // Close DBs if they are open and point to this user's directory
158
+ if (appDb) {
159
+ try {
160
+ appDb.close();
161
+ } catch (e) {
162
+ // Optionally log error
163
+ }
164
+ appDb = null;
165
+ appDbFilePath = null;
166
+ }
167
+ if (fwDb) {
168
+ try {
169
+ fwDb.close();
170
+ } catch (e) {
171
+ // Optionally log error
172
+ }
173
+ fwDb = null;
174
+ fwDbFilePath = null;
175
+ }
176
+ try {
177
+ fs.rmSync(userPath, { recursive: true, force: true });
178
+ return `User directory deleted: ${userPath}`;
179
+ } catch (err) {
180
+ throw new Error(`Failed to delete user directory: ${err.message}`);
181
+ }
182
+ }
183
+
184
+ app.on('before-quit', () => {
185
+ if (appDb) {
186
+ appDb.close((err) => {
187
+ if (err) {
188
+ throw err.message;
189
+ } else {
190
+ console.log('Database connection closed.');
191
+ }
192
+ appDb = null;
193
+ });
194
+ }
195
+ if (fwDb) {
196
+ fwDb.close((err) => {
197
+ if (err) {
198
+ throw err.message;
199
+ } else {
200
+ console.log('Database connection closed.');
201
+ }
202
+ fwDb = null;
203
+ });
204
+ }
205
+ });
206
+
207
+ module.exports = {
208
+ close,
209
+ create,
210
+ execute,
211
+ getDBFilePath,
212
+ getUserDirectoryPath,
213
+ saveWebDB,
214
+ deleteUserData
215
+ };
@@ -0,0 +1,34 @@
1
+ //
2
+ // UnviredDB.h
3
+ // RemindTrac
4
+ //
5
+ // Created by VENKADESH on 13/06/17.
6
+ //
7
+ //
8
+
9
+ #import <Cordova/CDV.h>
10
+ #import "sqlite3.h"
11
+ #import <Foundation/Foundation.h>
12
+
13
+
14
+ @interface UnviredDB : CDVPlugin {
15
+ }
16
+
17
+ //@property (nonatomic, strong)NSString *commandDelegate;
18
+
19
+ - (void)create:(CDVInvokedUrlCommand*)command;
20
+
21
+ - (void)execute:(CDVInvokedUrlCommand*)command;
22
+
23
+ - (void)close:(CDVInvokedUrlCommand*)command;
24
+
25
+ - (void)getDBFilePath:(CDVInvokedUrlCommand*)command;
26
+
27
+ - (void)getUserDirectoryPath:(CDVInvokedUrlCommand*)command;
28
+
29
+ - (void)saveWebDB:(CDVInvokedUrlCommand*)command;
30
+
31
+ - (void)deleteUserData:(CDVInvokedUrlCommand*)command;
32
+
33
+
34
+ @end