-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsqlite.js
137 lines (124 loc) · 3.05 KB
/
sqlite.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import { existsSync } from "fs";
import sqlite3 from "sqlite3";
import { open } from "sqlite";
const dbFile = "./.data/users.db";
const exists = existsSync(dbFile);
const ddlQueries = {
users: `CREATE TABLE Users
(
id INTEGER PRIMARY KEY AUTOINCREMENT,
email TEXT UNIQUE
)`,
userCredentials: `CREATE TABLE UserCredentials
(
id INTEGER PRIMARY KEY AUTOINCREMENT,
userId INTEGER NOT NULL,
credentialId TEXT NOT NULL,
credentialKey TEXT NOT NULL,
FOREIGN KEY (userId) REFERENCES Users (id)
)`,
notes: `CREATE TABLE Notes
(
id INTEGER PRIMARY KEY AUTOINCREMENT,
userId INTEGER NOT NULL,
text TEXT,
FOREIGN KEY (userId) REFERENCES Users (id)
)`,
};
let db;
open({
filename: dbFile,
driver: sqlite3.Database,
}).then(async (dBase) => {
db = dBase;
try {
if (!exists) {
await db.run(ddlQueries.users);
await db.run(ddlQueries.userCredentials);
await db.run(ddlQueries.notes);
}
} catch (dbError) {
console.error(dbError);
}
});
export const getUser = async (email) => {
try {
return await db.get("SELECT * FROM Users WHERE email = ? ", email);
} catch (dbError) {
console.error(dbError);
}
};
export const getUserCredentials = async (userId) => {
try {
return await db.all(
"SELECT * FROM UserCredentials WHERE userId = ? ",
userId
);
} catch (dbError) {
console.error(dbError);
}
};
export const getUserCredential = async (userId, credentialID) => {
try {
return await db.get(
"SELECT * FROM UserCredentials WHERE userId = ? and credentialId = ?",
userId,
credentialID
);
} catch (dbError) {
console.error(dbError);
}
};
export const addUser = async (email) => {
let success = false;
try {
success = await db.run("INSERT INTO Users (email) VALUES (?)", [email]);
} catch (dbError) {
console.error(dbError);
}
return success.changes > 0 ? true : false;
};
export const addUserCredential = async (
userId,
credentialId,
credentialKey
) => {
let success = false;
try {
success = await db.run(
"INSERT INTO UserCredentials (userId, credentialId, credentialKey) VALUES (?,?,?)",
[userId, credentialId, credentialKey]
);
} catch (dbError) {
console.error(dbError);
}
return success.changes > 0 ? true : false;
};
export const getNotes = async (userId) => {
try {
return await db.all("SELECT * FROM Notes WHERE userId = ? ", userId);
} catch (dbError) {
console.error(dbError);
}
};
export const addNote = async (userId, text) => {
let success = false;
try {
success = await db.run("INSERT INTO Notes (userId, text) VALUES (?,?)", [
userId,
text,
]);
} catch (dbError) {
console.error(dbError);
}
return success.changes > 0 ? true : false;
};
export const deleteNote = async (noteId) => {
let success = false;
try {
success = await db.run("Delete FROM Notes WHERE id = ?", noteId);
} catch (dbError) {
console.error(dbError);
}
return success.changes > 0 ? true : false;
};