Skip to content

Commit 089b011

Browse files
committed
fix: persist chat sessions in db
Signed-off-by: Alexander Alemayhu <alexander@alemayhu.com>
1 parent a4cbd05 commit 089b011

File tree

4 files changed

+626
-481
lines changed

4 files changed

+626
-481
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
exports.up = async function (knex) {
2+
return knex.schema.createTable('sessions', (table) => {
3+
table.increments('id').primary();
4+
table.string('userId', 255).notNullable();
5+
table.jsonb('data').notNullable();
6+
table.timestamp('created_at').defaultTo(knex.fn.now());
7+
table.timestamp('updated_at').defaultTo(knex.fn.now());
8+
});
9+
};
10+
11+
exports.down = async function (knex) {
12+
return knex.schema.dropTable('sessions');
13+
};
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import { Knex } from 'knex';
2+
3+
class SessionRepository {
4+
constructor(private readonly database: Knex) {}
5+
6+
createSession(userId: string | number, data: any) {
7+
console.log('[SESSION] Creating session for user:', userId);
8+
return this.database('sessions').insert({ userId, data });
9+
}
10+
11+
getSession(userId: string | number) {
12+
console.log('[SESSION] Getting session for user:', userId);
13+
return this.database('sessions').where({ userId }).first();
14+
}
15+
16+
async updateSession(userId: string | number, data: any) {
17+
console.log('[SESSION] Updating session for user:', userId);
18+
const old = await this.getSession(userId);
19+
return this.database('sessions')
20+
.where({ userId })
21+
.update({
22+
data: {
23+
...old?.data,
24+
...data,
25+
},
26+
});
27+
}
28+
29+
deleteSession(userId: string | number) {
30+
console.log('[SESSION] Deleting session for user:', userId);
31+
return this.database('sessions').where({ userId }).del();
32+
}
33+
}
34+
35+
export default SessionRepository;

0 commit comments

Comments
 (0)