fastify-session-better-sqlite3-store 1.0.7 → 1.0.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.github/workflows/ci.yml +1 -1
- package/README.md +7 -6
- package/index.js +7 -1
- package/package.json +1 -1
package/.github/workflows/ci.yml
CHANGED
package/README.md
CHANGED
|
@@ -2,9 +2,10 @@
|
|
|
2
2
|
|
|
3
3
|

|
|
4
4
|

|
|
5
|
-
|
|
5
|
+

|
|
6
|
+
[](https://opensource.org/licenses/MIT)
|
|
6
7
|
|
|
7
|
-
|
|
8
|
+
[better-sqlite3](https://github.com/WiseLibs/better-sqlite3) session store for [@fastify/session](https://github.com/fastify/session). By default [@fastify/session](https://github.com/fastify/session) stores sessions in-memory. With this small package you can store sessions on an **SQLite3** database instead.
|
|
8
9
|
|
|
9
10
|
## Installation
|
|
10
11
|
|
|
@@ -17,20 +18,20 @@ npm install fastify-session-better-sqlite3-store
|
|
|
17
18
|
Use with `fastify-session`'s `store` property.
|
|
18
19
|
|
|
19
20
|
```js
|
|
20
|
-
const fastify = require('fastify')({logger:true})
|
|
21
|
+
const fastify = require('fastify')({ logger: true })
|
|
21
22
|
const fastifyCookie = require('@fastify/cookie')
|
|
22
23
|
const fastifySession = require('@fastify/session')
|
|
23
|
-
const db = require('better-sqlite3')(
|
|
24
|
+
const db = require('better-sqlite3')('./sqlite.db')
|
|
24
25
|
|
|
25
26
|
// require module
|
|
26
27
|
const SqliteStore = require('fastify-session-better-sqlite3-store')
|
|
27
28
|
|
|
28
29
|
fastify.register(fastifyCookie)
|
|
29
|
-
fastify.register(fastifySession,{
|
|
30
|
+
fastify.register(fastifySession, {
|
|
31
|
+
store: new SqliteStore(db),
|
|
30
32
|
// ...
|
|
31
33
|
// other session options
|
|
32
34
|
// ...
|
|
33
|
-
store: new SqliteStore(db)
|
|
34
35
|
})
|
|
35
36
|
```
|
|
36
37
|
|
package/index.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
'use strict'
|
|
2
2
|
|
|
3
|
+
const NODE_VERSION_REQUIREMENT = 14
|
|
3
4
|
const EventEmitter = require('events')
|
|
4
5
|
|
|
5
6
|
class SqliteStore extends EventEmitter {
|
|
@@ -8,6 +9,11 @@ class SqliteStore extends EventEmitter {
|
|
|
8
9
|
* @param {string} table table name where session data will be stored, defaults to `session`.
|
|
9
10
|
*/
|
|
10
11
|
constructor (sqlite3db, table = 'session') {
|
|
12
|
+
const nodeVersion = Number(process.version.match(/^v(\d+)/)[1])
|
|
13
|
+
if (nodeVersion < NODE_VERSION_REQUIREMENT) {
|
|
14
|
+
throw new Error(`node '${process.version}' not supported, needs 'v14.x' or greater`)
|
|
15
|
+
}
|
|
16
|
+
|
|
11
17
|
try {
|
|
12
18
|
sqlite3db.exec(`
|
|
13
19
|
create table ${table} (
|
|
@@ -18,7 +24,7 @@ class SqliteStore extends EventEmitter {
|
|
|
18
24
|
)
|
|
19
25
|
} catch (err) {
|
|
20
26
|
if (err.toString() !== 'SqliteError: table session already exists') {
|
|
21
|
-
throw
|
|
27
|
+
throw err
|
|
22
28
|
}
|
|
23
29
|
}
|
|
24
30
|
super()
|