MCPcopy Create free account
hub / github.com/adonisjs/session / write

Method write

src/stores/database.ts:207–231  ·  view source on GitHub ↗

* 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)

Source from the content-addressed store, hash-verified

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

Callers

nothing calls this directly

Calls 6

#serializeSessionDataMethod · 0.95
#collectGarbageMethod · 0.95
updateMethod · 0.80
getMethod · 0.45
hasMethod · 0.45
mergeMethod · 0.45

Tested by

no test coverage detected