Skip to content

Commit b83bb25

Browse files
authored
feat: first implementation (#1)
* feat: basic structure * wip * add: test * add: docs * fix: ci
1 parent 32b62e8 commit b83bb25

File tree

9 files changed

+395
-1
lines changed

9 files changed

+395
-1
lines changed

.eslintrc

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"extends": "standard"}

.github/dependabot.yml

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
version: 2
2+
updates:
3+
- package-ecosystem: github-actions
4+
open-pull-requests-limit: 10
5+
directory: /
6+
commit-message:
7+
prefix: chore
8+
prefix-development: chore
9+
include: scope
10+
schedule:
11+
interval: daily
12+
13+
- package-ecosystem: npm
14+
open-pull-requests-limit: 10
15+
directory: /
16+
commit-message:
17+
prefix: build
18+
prefix-development: chore
19+
include: scope
20+
schedule:
21+
interval: daily

.github/workflows/ci.yml

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
name: ci
2+
3+
on:
4+
push:
5+
paths-ignore:
6+
- 'docs/**'
7+
- '*.md'
8+
pull_request:
9+
paths-ignore:
10+
- 'docs/**'
11+
- '*.md'
12+
13+
jobs:
14+
linter:
15+
name: Lint Code
16+
runs-on: ubuntu-latest
17+
permissions:
18+
contents: read
19+
steps:
20+
- name: Check out repo
21+
uses: actions/checkout@v3
22+
with:
23+
persist-credentials: false
24+
25+
- name: Setup Node
26+
uses: actions/setup-node@v3
27+
with:
28+
node-version: lts/*
29+
30+
- name: Install dependencies
31+
run: npm i --ignore-scripts
32+
33+
- name: Lint code
34+
run: npm run lint
35+
36+
test:
37+
name: Test
38+
needs: linter
39+
runs-on: ${{ matrix.os }}
40+
permissions:
41+
contents: read
42+
strategy:
43+
matrix:
44+
node-version: [14, 16, 18]
45+
os: [macos-latest, ubuntu-latest, windows-latest]
46+
steps:
47+
- name: Check out repo
48+
uses: actions/checkout@v3
49+
with:
50+
persist-credentials: false
51+
52+
- name: Setup Node ${{ matrix.node-version }}
53+
uses: actions/setup-node@v3
54+
with:
55+
node-version: ${{ matrix.node-version }}
56+
57+
- name: Install dependencies
58+
run: npm i
59+
60+
- name: Run tests
61+
run: npm test
62+
63+
automerge:
64+
name: Automerge Dependabot PRs
65+
if: >
66+
github.event_name == 'pull_request' &&
67+
github.event.pull_request.user.login == 'dependabot[bot]'
68+
needs: test
69+
permissions:
70+
pull-requests: write
71+
contents: write
72+
runs-on: ubuntu-latest
73+
steps:
74+
- uses: fastify/github-action-merge-dependabot@v3
75+
with:
76+
exclude: ${{ inputs.auto-merge-exclude }}
77+
github-token: ${{ secrets.GITHUB_TOKEN }}
78+
target: major

.github/workflows/release.yml

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
name: Release
2+
on:
3+
workflow_dispatch:
4+
inputs:
5+
semver:
6+
description: 'The semver to use'
7+
required: true
8+
default: 'patch'
9+
pull_request:
10+
types: [closed]
11+
12+
jobs:
13+
release:
14+
runs-on: ubuntu-latest
15+
steps:
16+
- uses: nearform/optic-release-automation-action@v3
17+
with:
18+
github-token: ${{ secrets.github_token }}
19+
npm-token: ${{ secrets.NPM_TOKEN }}
20+
optic-token: ${{ secrets.OPTIC_TOKEN }}
21+
semver: ${{ github.event.inputs.semver }}

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -102,3 +102,5 @@ dist
102102

103103
# TernJS port file
104104
.tern-port
105+
.vscode/
106+
package-lock.json

README.md

+65-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,66 @@
11
# fastify-sqlite
2-
Fastify plugin to connect to a SQLite database
2+
3+
[![JavaScript Style Guide](https://img.shields.io/badge/code_style-standard-brightgreen.svg)](https://standardjs.com)
4+
[![ci](https://github.com/Eomm/fastify-sqlite/actions/workflows/ci.yml/badge.svg)](https://github.com/Eomm/fastify-sqlite/actions/workflows/ci.yml)
5+
6+
Fastify plugin to connect to a SQLite3 database.
7+
Under the hood, this plugin uses [`sqlite3`](https://www.npmjs.com/package/sqlite3).
8+
9+
## Install
10+
11+
```
12+
npm install fastify-sqlite
13+
```
14+
15+
### Compatibility
16+
17+
| Plugin version | Fastify version |
18+
| ------------- |:---------------:|
19+
| `^1.0.0` | `^4.0.0` |
20+
21+
22+
## Usage
23+
24+
```js
25+
const fastifySqlite = require('fastify-sqlite')
26+
27+
async function main () {
28+
const app = fastify()
29+
app.register(fastifySqlite, {
30+
dbFile: 'foo.db'
31+
})
32+
await app.ready()
33+
34+
app.sqlite.all('SELECT * FROM myTable', (err, rows) => {
35+
// do something
36+
})
37+
}
38+
main()
39+
```
40+
41+
Checkout the [sqlite3 documentation](https://github.com/TryGhost/node-sqlite3/wiki/API) to see all the available methods.
42+
43+
_Note that Promise is not supported by the `sqlite3` module._
44+
45+
## Options
46+
47+
You can pass the following options to the plugin:
48+
49+
```js
50+
app.register(require('fastify-sqlite'), {
51+
name: 'mydb', // optional decorator name. Default null
52+
verbose: true, // log sqlite3 queries as trace. Default false
53+
dbFile: ':memory:', // select the database file. Default ':memory:'
54+
mode: fastifySqlite.sqlite3.OPEN_READONLY
55+
// how to connecto to the DB, Default: OPEN_READWRITE | OPEN_CREATE | OPEN_FULLMUTEX
56+
})
57+
58+
// usage WITH name option
59+
app.sqlite.myDb.all('SELECT * FROM myTable', (err, rows) => {
60+
// do something
61+
})
62+
```
63+
64+
## License
65+
66+
Copyright [Manuel Spigolon](https://github.com/Eomm), Licensed under [MIT](./LICENSE).

index.js

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
'use strict'
2+
3+
const fp = require('fastify-plugin')
4+
const sqlite3 = require('sqlite3')
5+
6+
function fastifySqlite (fastify, opts, next) {
7+
const Sqlite = (opts.verbose === true)
8+
? sqlite3.verbose()
9+
: sqlite3
10+
11+
const filename = opts.dbFile || ':memory:'
12+
const mode = opts.mode || (sqlite3.OPEN_READWRITE | sqlite3.OPEN_CREATE | sqlite3.OPEN_FULLMUTEX)
13+
14+
const db = new Sqlite.Database(filename, mode, (err) => {
15+
if (err) {
16+
return next(err)
17+
}
18+
19+
if (opts.verbose === true) {
20+
db.on('trace', function (trace) {
21+
fastify.log.trace({ sql: trace }, 'sqlite verbose trace')
22+
})
23+
}
24+
25+
decorateFastifyInstance(fastify, db, opts, next)
26+
})
27+
}
28+
29+
function decorateFastifyInstance (fastify, db, opts, next) {
30+
fastify.addHook('onClose', close.bind(db))
31+
32+
const name = opts.name
33+
34+
if (!name) {
35+
if (fastify.sqlite) {
36+
return next(new Error('fastify-sqlite has been already registered'))
37+
}
38+
fastify.decorate('sqlite', db)
39+
} else {
40+
if (!fastify.sqlite) {
41+
fastify.decorate('sqlite', Object.create(null))
42+
}
43+
44+
if (fastify.sqlite[name]) {
45+
return next(new Error(`Connection name [${name}] already registered`))
46+
}
47+
48+
fastify.sqlite[name] = db
49+
}
50+
51+
next()
52+
}
53+
54+
function close (instance, done) {
55+
this.close(done)
56+
}
57+
58+
module.exports = fp(fastifySqlite, {
59+
name: 'fastify-sqlite',
60+
fastify: '^4.x'
61+
})
62+
63+
// let the user access the sqlite3 mode constants eg: sqlite3.OPEN_READONLY
64+
module.exports.sqlite3 = sqlite3

package.json

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
{
2+
"name": "fastify-sqlite",
3+
"version": "0.0.1",
4+
"description": "Fastify plugin to connect to a SQLite database",
5+
"main": "index.js",
6+
"scripts": {
7+
"lint": "standard",
8+
"lint:fix": "standard --fix",
9+
"test": "tap test/**/*.test.js"
10+
},
11+
"repository": {
12+
"type": "git",
13+
"url": "git+https://github.com/Eomm/fastify-sqlite.git"
14+
},
15+
"keywords": [
16+
"fastify",
17+
"fastify-plugin",
18+
"database",
19+
"in-memory",
20+
"sql",
21+
"sqlite",
22+
"sqlite3"
23+
],
24+
"author": "Manuel Spigolon <[email protected]> (https://github.com/Eomm)",
25+
"funding": "https://github.com/Eomm/fastify-sqlite?sponsor=1",
26+
"license": "MIT",
27+
"bugs": {
28+
"url": "https://github.com/Eomm/fastify-sqlite/issues"
29+
},
30+
"homepage": "https://github.com/Eomm/fastify-sqlite#readme",
31+
"devDependencies": {
32+
"fastify": "^4.5.3",
33+
"standard": "^17.0.0",
34+
"tap": "^16.3.0"
35+
},
36+
"dependencies": {
37+
"fastify-plugin": "^4.2.1",
38+
"sqlite3": "^5.0.11"
39+
}
40+
}

0 commit comments

Comments
 (0)