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

Fixing bug: Upsetting instead of inserting on set. #3

Merged
merged 4 commits into from
Nov 28, 2023
Merged
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
2 changes: 1 addition & 1 deletion demo/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "demo",
"version": "0.4.1",
"version": "0.4.2-alpha.1",
"private": true,
"type": "module",
"scripts": {
Expand Down
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@iwsio/mongodb-express-session-root",
"version": "0.4.1",
"version": "0.4.2-alpha.2",
"description": "",
"scripts": {
"prebuild": "tsc --build --clean && npm run prebuild -ws --if-present",
Expand Down
2 changes: 1 addition & 1 deletion package/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@iwsio/mongodb-express-session",
"version": "0.4.1",
"version": "0.4.2-alpha.2",
"description": "An ESM express-session store for MongoDB using the latest MongoDb Node driver v6. Supports TTL and touch. Supports MongoDB v3.6+ (without TTL index), 5.1+ (with TTL Index).",
"main": "dist/index.js",
"module": "dist/index.js",
Expand Down
3 changes: 2 additions & 1 deletion package/src/index.mts
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,8 @@ export class MongoSessionStore extends Store {
expires,
session
}
const result = await this.collection().insertOne(doc)
const { _id, ...body } = doc
const result = await this.collection().updateOne({ _id: doc._id }, { $set: body }, { upsert: true })
result.acknowledged ? cb() : cb(new Error(`set not acknowledged for ${sid}`))
} catch (err) {
cb(err)
Expand Down
37 changes: 36 additions & 1 deletion package/src/index.test.mts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import * as mongoSessionStore from './index.mjs'
const connectSpy = vi.fn()
const constSpy = vi.fn()
const createIndexSpy = vi.fn()
const updateOneSpy = vi.fn(() => Promise.resolve({ acknowledged: true }))

vi.mock('mongodb', async () => {
const mongodb = (await vi.importActual('mongodb')) as any
Expand Down Expand Up @@ -30,7 +31,7 @@ vi.mock('mongodb', async () => {
findOne: vi.fn(),
findMany: () => ({ toArray: vi.fn() }),
deleteOne: vi.fn(() => Promise.resolve({ acknowledged: true })),
updateOne: vi.fn(() => Promise.resolve({ acknowledged: true })),
updateOne: updateOneSpy,
countDocuments: vi.fn(),
createIndex: createIndexSpy
}
Expand Down Expand Up @@ -96,4 +97,38 @@ describe('MongoSessionStore', () => {
expect(spyInfo).toHaveBeenCalledWith('Closing MongoDB connection to session store.')
})
})

it('should upsert on set', async () => {
const store = new mongoSessionStore.MongoSessionStore()
const errorSpy = vi.fn()
store.on('error', errorSpy)

await vi.waitFor(() => {
expect(connectSpy).toHaveBeenCalled()
})

await store.set('1', {
cookie: {
maxAge: 2000,
originalMaxAge: null
}
} as any)

expect(errorSpy).not.toHaveBeenCalled()
expect(updateOneSpy).toHaveBeenCalledWith(
{ _id: '1' },
{
$set: expect.objectContaining({
// expires, ignoring for test
session: {
cookie: {
maxAge: 2000,
originalMaxAge: null
}
}
})
},
{ upsert: true } // mainly we're asserting this and that it's an update
)
})
})