* Writes session values to the database with expiry. A session read by * this store is only updated (no insert), so a logout or a destroy that * happened during the request is not undone. Other sessions are upserted. * * @param sessionId - Session identifier * @param values - Session
(sessionId: string, values: Object)
| 205 | * await store.write('sess_abc123', { userId: 123 }) |
| 206 | */ |
| 207 | async write(sessionId: string, values: Object): Promise<void> { |
| 208 | debug('database store: writing session data %s, %O', sessionId, values) |
| 209 | |
| 210 | const message = this.#serializeSessionData(values, sessionId) |
| 211 | const expiresAt = new Date(Date.now() + this.#ttlSeconds * 1000) |
| 212 | const userId = this.#tags.get(sessionId) |
| 213 | const columns = { |
| 214 | data: message, |
| 215 | expires_at: expiresAt, |
| 216 | ...(userId && { user_id: userId }), |
| 217 | } |
| 218 | |
| 219 | if (this.#found.has(sessionId)) { |
| 220 | await this.#client.from(this.#tableName).where('id', sessionId).update(columns) |
| 221 | } else { |
| 222 | await this.#client |
| 223 | .insertQuery() |
| 224 | .table(this.#tableName) |
| 225 | .insert({ id: sessionId, ...columns }) |
| 226 | .knexQuery.onConflict('id') |
| 227 | .merge(Object.keys(columns)) |
| 228 | } |
| 229 | |
| 230 | await this.#collectGarbage() |
| 231 | } |
| 232 | |
| 233 | /** |
| 234 | * Removes session data from the database |
nothing calls this directly
no test coverage detected