-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #78 from obeliss-nlesc/feature/use_sqlitedb
Feature/use sqlitedb
- Loading branch information
Showing
16 changed files
with
1,312 additions
and
92 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,4 +8,5 @@ jobs: | |
- uses: actions/setup-node@v4 | ||
with: | ||
node-version: "20" | ||
- run: npm install | ||
- run: npm run test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
const User = require("./user.js") | ||
|
||
class UserMap extends Map { | ||
constructor(file) { | ||
super() | ||
} | ||
|
||
load() { | ||
throw new Error("Method not implemented.") | ||
} | ||
|
||
find(userId) { | ||
return Array.from(this.values()).filter((u) => { | ||
return u.userId == userId | ||
}) | ||
} | ||
|
||
getUsersInSession(session) { | ||
return Array.from(this.values()).filter((u) => { | ||
if (!u.server) { | ||
return false | ||
} | ||
const userSession = u.server.split("#")[1] | ||
return userSession == session | ||
}) | ||
} | ||
|
||
getUsedUrls() { | ||
return Array.from(this.values()) | ||
.filter((u) => { | ||
return u.experimentUrl | ||
}) | ||
.map((u) => { | ||
return u.experimentUrl | ||
}) | ||
} | ||
|
||
dump() { | ||
const data = [] | ||
this.forEach((v, k) => { | ||
data.push(v.serialize()) | ||
}) | ||
|
||
return JSON.stringify(data) | ||
} | ||
|
||
upsert(user) { | ||
throw new Error("Method not implemented.") | ||
} | ||
|
||
save(user) { | ||
throw new Error("Method not implemented.") | ||
} | ||
|
||
saveAll() { | ||
throw new Error("Method not implemented.") | ||
} | ||
|
||
findAll() { | ||
throw new Error("Method not implemented.") | ||
} | ||
|
||
forceSave() { | ||
throw new Error("Method not implemented.") | ||
} | ||
} | ||
|
||
module.exports = UserMap |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
const sqlite3 = require("sqlite3").verbose() | ||
const User = require("./user.js") | ||
const UserMap = require("./UserMap.js") | ||
|
||
class UserSqliteDb extends UserMap { | ||
constructor(file) { | ||
super() | ||
this.writeCounter = 0 | ||
this.db = new sqlite3.Database(file) | ||
this.db.serialize(() => { | ||
this.db.run(` | ||
CREATE TABLE IF NOT EXISTS users ( | ||
userId TEXT PRIMARY KEY, | ||
jsonObj TEXT | ||
) | ||
`) | ||
}) | ||
} | ||
|
||
_getTableNames() { | ||
return new Promise((resolve, reject) => { | ||
this.db.all( | ||
"SELECT name FROM sqlite_master WHERE type='table'", | ||
[], | ||
(err, rows) => { | ||
if (err) { | ||
reject(err) | ||
} else { | ||
const tableNames = rows.map((row) => row.name) | ||
resolve(tableNames) | ||
} | ||
}, | ||
) | ||
}) // Promise | ||
} | ||
|
||
load() { | ||
return new Promise((resolve, reject) => { | ||
const query = `SELECT * FROM users` | ||
this.db.all(query, (err, rows) => { | ||
if (err) { | ||
console.error(err) | ||
reject(err) | ||
} else { | ||
rows | ||
.map((r) => { | ||
return JSON.parse(r.jsonObj) | ||
}) | ||
.forEach((u) => { | ||
const user = new User(u.userId, u.experimentId) | ||
user.redirectedUrl = u.redirectedUrl | ||
user.experimentUrl = u.experimentUrl | ||
user.groupId = u.groupId | ||
user.server = u.server | ||
user.oTreeId = u.oTreeId | ||
user.tokenParams = u.tokenParams | ||
user.state = u.redirectedUrl ? u.state : user.state | ||
const compoundKey = `${user.userId}:${user.experimentId}` | ||
this.set(compoundKey, user) | ||
}) | ||
resolve(this) | ||
} | ||
}) | ||
}) //Promise | ||
} | ||
|
||
findAll() { | ||
return new Promise((resolve, reject) => { | ||
const query = `SELECT * FROM users` | ||
this.db.all(query, (err, rows) => { | ||
if (err) { | ||
reject(err) | ||
} else { | ||
resolve(rows) | ||
} | ||
}) | ||
}) // Promise | ||
} | ||
|
||
upsert(user) { | ||
return new Promise((resolve, reject) => { | ||
const query = ` | ||
INSERT INTO users (userId, jsonObj) | ||
VALUES (?, ?) | ||
ON CONFLICT(userId) DO UPDATE SET | ||
jsonObj = excluded.jsonObj | ||
` | ||
const compoundKey = `${user.userId}:${user.experimentId}` | ||
this.db.run( | ||
query, | ||
[compoundKey, JSON.stringify(user.serialize())], | ||
function (err) { | ||
if (err) { | ||
reject(err) | ||
} else { | ||
// console.log(`${compoundKey} upsert`) | ||
resolve(compoundKey) | ||
} | ||
}, | ||
) | ||
}) | ||
} | ||
|
||
save(user) { | ||
return new Promise((resolve, reject) => { | ||
const query = `INSERT INTO users (userId, jsonObj) VALUES (?, ?)` | ||
const compoundKey = `${user.userId}:${user.experimentId}` | ||
this.db.run( | ||
query, | ||
[compoundKey, JSON.stringify(user.serialize())], | ||
function (err) { | ||
if (err) { | ||
reject(err) | ||
} else { | ||
resolve(compoundKey) | ||
} | ||
}, | ||
) | ||
}) // Promise | ||
} | ||
|
||
saveAll() { | ||
this.writeCounter += 1 | ||
const currentCounter = this.writeCounter | ||
setTimeout(() => { | ||
if (currentCounter != this.writeCounter || this.writeCounter === 0) { | ||
return | ||
} | ||
this.forEach((u, k) => { | ||
this.upsert(u) | ||
}) | ||
}, 500) | ||
} | ||
|
||
forceSave() { | ||
const allUpserts = [...this.values()].map((u) => { | ||
return this.upsert(u) | ||
}) | ||
return Promise.all(allUpserts) | ||
} | ||
} | ||
|
||
module.exports = UserSqliteDb |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.