Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Database Intergration #1

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions database/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class DatabaseSessionManager {
constructor() {
this.sessions = [];
}

}
52 changes: 52 additions & 0 deletions database/mock.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
'use strict';

class InMemorySessionManager{
constructor(){
this.sessions = [];
}

createNew(session){
this.sessions.push(session);

return session;
}

get(msisdn) {
var session = null;
this.sessions.forEach(element => {
if (element !== null && element.msisdn === msisdn){
session = element;
}
});

return session;
}

close(session){
const testing = (e) => e.msisdn === session.msisdn;

const index = this.sessions.findIndex(testing);

if(index > -1){
this.sessions.splice(index, 1);
}
}

update(session){
var ses = []

for(var i = 0; i < this.sessions.length; i++){
if(this.sessions[i] !== null && this.sessions[i].msisdn !== session.msisdn) {
ses.push(session);
}
}

var n_session = session;

ses.push(n_session);

this.sessions = ses;
}
}

module.exports = InMemorySessionManager;
37 changes: 37 additions & 0 deletions database/sqlite.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
'use strict';

const Database = require('better-sqlite3');

class SQLiteSessionManager {

constructor(file) {
this.db = new Database(file);

// This is optional to improve performance
this.db.pragma('journal_mode = WAL');

this.db.prepare('CREATE TABLE IF NOT EXISTS sessions (msisdn TEXT, prompt TEXT, menu TEXT, session TEXT)').run();
}

createNew(session) {
this.db.prepare('INSERT INTO sessions VALUES (?, ?, ?, ?)').run(session.msisdn, session.prompt, session.menu, JSON.stringify(session));
return session;
}

get(msisdn) {
const row = this.db.prepare('SELECT * FROM sessions WHERE msisdn = ?').get(msisdn);
return row ? JSON.parse(row.session) : null;
}

close(session) {
this.db.prepare('DELETE FROM sessions WHERE msisdn = ?').run(session.msisdn);
}

update(session) {
this.db.prepare('UPDATE sessions SET session = ? WHERE msisdn = ?').run(JSON.stringify(session), session.msisdn);
}


}

module.exports = SQLiteSessionManager;