File tree Expand file tree Collapse file tree 4 files changed +626
-481
lines changed
Expand file tree Collapse file tree 4 files changed +626
-481
lines changed Original file line number Diff line number Diff line change 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+ } ;
Original file line number Diff line number Diff line change 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 ;
You can’t perform that action at this time.
0 commit comments