基本schema测试

This commit is contained in:
2025-09-22 16:00:32 +08:00
commit b70b69c886
2754 changed files with 408678 additions and 0 deletions

2
node_modules/@fastify/mongodb/.gitattributes generated vendored Normal file
View File

@@ -0,0 +1,2 @@
# Set default behavior to automatically convert line endings
* text=auto eol=lf

13
node_modules/@fastify/mongodb/.github/dependabot.yml generated vendored Normal file
View File

@@ -0,0 +1,13 @@
version: 2
updates:
- package-ecosystem: "github-actions"
directory: "/"
schedule:
interval: "monthly"
open-pull-requests-limit: 10
- package-ecosystem: "npm"
directory: "/"
schedule:
interval: "weekly"
open-pull-requests-limit: 10

21
node_modules/@fastify/mongodb/.github/stale.yml generated vendored Normal file
View File

@@ -0,0 +1,21 @@
# Number of days of inactivity before an issue becomes stale
daysUntilStale: 15
# Number of days of inactivity before a stale issue is closed
daysUntilClose: 7
# Issues with these labels will never be considered stale
exemptLabels:
- "discussion"
- "feature request"
- "bug"
- "help wanted"
- "plugin suggestion"
- "good first issue"
# Label to use when marking an issue as stale
staleLabel: stale
# Comment to post when marking an issue as stale. Set to `false` to disable
markComment: >
This issue has been automatically marked as stale because it has not had
recent activity. It will be closed if no further activity occurs. Thank you
for your contributions.
# Comment to post when closing a stale issue. Set to `false` to disable
closeComment: false

23
node_modules/@fastify/mongodb/.github/workflows/ci.yml generated vendored Normal file
View File

@@ -0,0 +1,23 @@
name: CI
on:
push:
branches:
- main
- master
- next
- 'v*'
paths-ignore:
- 'docs/**'
- '*.md'
pull_request:
paths-ignore:
- 'docs/**'
- '*.md'
jobs:
test:
uses: fastify/workflows/.github/workflows/plugins-ci-mongo.yml@v5
with:
lint: true
license-check: true

21
node_modules/@fastify/mongodb/LICENSE generated vendored Normal file
View File

@@ -0,0 +1,21 @@
MIT License
Copyright (c) 2017 Fastify
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

134
node_modules/@fastify/mongodb/README.md generated vendored Normal file
View File

@@ -0,0 +1,134 @@
# @fastify/mongodb
[![CI](https://github.com/fastify/fastify-mongodb/actions/workflows/ci.yml/badge.svg?branch=master)](https://github.com/fastify/fastify-mongodb/actions/workflows/ci.yml)
[![NPM version](https://img.shields.io/npm/v/@fastify/mongodb.svg?style=flat)](https://www.npmjs.com/package/@fastify/mongodb)
[![neostandard javascript style](https://img.shields.io/badge/code_style-neostandard-brightgreen?style=flat)](https://github.com/neostandard/neostandard)
Fastify MongoDB connection plugin; with this you can share the same MongoDB connection pool in every part of your server.
Under the hood the official [MongoDB](https://github.com/mongodb/node-mongodb-native) driver is used,
the options that you pass to `register` will be passed to the Mongo client.
The `mongodb` driver is v6.x.x.
If you do not provide the client by yourself (see below), the URL option is *required*.
## Install
```
npm i @fastify/mongodb
```
## Usage
Add it to your project with `register` and you are done!
```js
const fastify = require('fastify')()
fastify.register(require('@fastify/mongodb'), {
// force to close the mongodb connection when app stopped
// the default value is false
forceClose: true,
url: 'mongodb://mongo/mydb'
})
fastify.get('/user/:id', async function (req, reply) {
// Or this.mongo.client.db('mydb').collection('users')
const users = this.mongo.db.collection('users')
// if the id is an ObjectId format, you need to create a new ObjectId
const id = new this.mongo.ObjectId(req.params.id)
try {
const user = await users.findOne({ id })
return user
} catch (err) {
return err
}
})
fastify.listen({ port: 3000 }, err => {
if (err) throw err
})
```
You may also supply a pre-configured instance of `mongodb.MongoClient`:
```js
const mongodb = require('mongodb')
mongodb.MongoClient.connect('mongodb://mongo/db')
.then((client) => {
const fastify = require('fastify')()
fastify.register(require('@fastify/mongodb'), { client: client })
.register(function (fastify, opts, next) {
const db = fastify.mongo.client.db('mydb')
// ...
// ...
// ...
next()
})
})
.catch((err) => {
throw err
})
```
Notes:
* the passed `client` connection will **not** be closed when the Fastify server
shuts down.
* to terminate the MongoDB connection you have to manually call the [fastify.close](https://fastify.dev/docs/latest/Reference/Server/#close) method (for example for testing purposes, otherwise the test will hang).
* `mongodb` connection timeout is reduced from 30s (default) to 7.5s to throw an error before `fastify` plugin timeout.
## Reference
This plugin decorates the `fastify` instance with a `mongo` object. That object has the
following properties:
- `client` is the [`MongoClient` instance](http://mongodb.github.io/node-mongodb-native/3.3/api/MongoClient.html)
- `ObjectId` is the [`ObjectId` class](http://mongodb.github.io/node-mongodb-native/3.3/api/ObjectID.html)
- `db` is the [`DB` instance](http://mongodb.github.io/node-mongodb-native/3.3/api/Db.html)
The `ObjectId` class can also be directly imported from the plugin as it gets re-exported from `mongodb`:
```js
const { ObjectId } = require('@fastify/mongodb')
const id = new ObjectId('some-id-here')
```
The `db` property is added **only if**:
- a `database` string option is given during the plugin registration.
- the connection string contains the database name. See the [Connection String URI Format](https://docs.mongodb.com/manual/reference/connection-string/#connection-string-uri-format)
A `name` option can be used to connect to multiple MongoDB clusters.
```js
const fastify = require('fastify')()
fastify
.register(require('@fastify/mongodb'), { url: 'mongodb://mongo1/mydb', name: 'MONGO1' })
.register(require('@fastify/mongodb'), { url: 'mongodb://mongo2/otherdb', name: 'MONGO2' })
fastify.get('/', function (req, res) {
// This collection comes from "mongodb://mongo1/mydb"
const coll1 = this.mongo.MONGO1.db.collection('my_collection')
// This collection comes from "mongodb://mongo2/otherdb"
const coll2 = this.mongo.MONGO2.db.collection('my_collection')
// ...
// ...
// do your stuff here
// ...
// ...
res.send(yourResult)
})
```
## Acknowledgments
This project is kindly sponsored by:
- [nearForm](https://nearform.com)
- [LetzDoIt](https://www.letzdoitapp.com/)
## License
Licensed under [MIT](./LICENSE).

6
node_modules/@fastify/mongodb/eslint.config.js generated vendored Normal file
View File

@@ -0,0 +1,6 @@
'use strict'
module.exports = require('neostandard')({
ignores: require('neostandard').resolveIgnoresFromGitignore(),
ts: true
})

87
node_modules/@fastify/mongodb/index.js generated vendored Normal file
View File

@@ -0,0 +1,87 @@
'use strict'
const fp = require('fastify-plugin')
const { MongoClient, ObjectId } = require('mongodb')
function decorateFastifyInstance (fastify, client, options) {
const { forceClose, database, name, newClient } = options
if (newClient) {
// done() is not needed because .close() returns a Promise
fastify.addHook('onClose', function closeMongoDb () {
return client.close(forceClose)
})
}
const mongo = {
client,
ObjectId
}
if (name) {
if (!fastify.mongo) {
fastify.decorate('mongo', mongo)
}
if (fastify.mongo[name]) {
throw Error('Connection name already registered: ' + name)
}
fastify.mongo[name] = mongo
} else {
if (fastify.mongo) {
throw Error('fastify-mongodb has already registered')
}
}
if (database) {
mongo.db = client.db(database)
}
if (!fastify.mongo) {
fastify.decorate('mongo', mongo)
}
}
async function fastifyMongodb (fastify, options) {
options = Object.assign({
serverSelectionTimeoutMS: 7500
}, options)
const { forceClose, name, database, url, client, ...opts } = options
if (client) {
decorateFastifyInstance(fastify, client, {
newClient: false,
forceClose,
database,
name
})
} else {
if (!url) {
throw Error('`url` parameter is mandatory if no client is provided')
}
const urlTokens = /\w\/([^?]*)/g.exec(url)
const parsedDbName = urlTokens && urlTokens[1]
const databaseName = database || parsedDbName
const client = new MongoClient(url, opts)
await client.connect()
decorateFastifyInstance(fastify, client, {
newClient: true,
forceClose,
database: databaseName,
name
})
}
}
module.exports = fp(fastifyMongodb, {
fastify: '5.x',
name: '@fastify/mongodb'
})
module.exports.default = fastifyMongodb
module.exports.fastifyMongodb = fastifyMongodb
module.exports.mongodb = require('mongodb')
module.exports.ObjectId = ObjectId

80
node_modules/@fastify/mongodb/package.json generated vendored Normal file
View File

@@ -0,0 +1,80 @@
{
"name": "@fastify/mongodb",
"version": "9.0.2",
"description": "Fastify MongoDB connection plugin",
"main": "index.js",
"type": "commonjs",
"types": "types/index.d.ts",
"scripts": {
"coverage": "npm run unit -- --cov --coverage-report=html",
"lint": "eslint",
"lint:fix": "eslint --fix",
"mongo": "docker run --rm -d -p 27017:27017 mongo:5.0.0",
"test": "npm run test:unit && npm run test:typescript",
"test:typescript": "tsd",
"test:unit": "c8 --100 node --test"
},
"repository": {
"type": "git",
"url": "git+https://github.com/fastify/fastify-mongodb.git"
},
"keywords": [
"fastify",
"mongo",
"mongodb",
"database",
"connection"
],
"author": "Tomas Della Vedova - @delvedor (https://delvedor.dev)",
"contributors": [
{
"name": "Matteo Collina",
"email": "hello@matteocollina.com"
},
{
"name": "Manuel Spigolon",
"email": "behemoth89@gmail.com"
},
{
"name": "KaKa Ng",
"email": "kaka@kakang.dev",
"url": "https://github.com/climba03003"
},
{
"name": "Frazer Smith",
"email": "frazer.dev@icloud.com",
"url": "https://github.com/fdawgs"
}
],
"license": "MIT",
"bugs": {
"url": "https://github.com/fastify/fastify-mongodb/issues"
},
"homepage": "https://github.com/fastify/fastify-mongodb#readme",
"funding": [
{
"type": "github",
"url": "https://github.com/sponsors/fastify"
},
{
"type": "opencollective",
"url": "https://opencollective.com/fastify"
}
],
"devDependencies": {
"@fastify/pre-commit": "^2.1.0",
"@types/node": "^22.0.0",
"c8": "^10.1.2",
"eslint": "^9.17.0",
"fastify": "^5.0.0",
"neostandard": "^0.12.0",
"tsd": "^0.31.0"
},
"dependencies": {
"fastify-plugin": "^5.0.0",
"mongodb": "^6.5.0"
},
"publishConfig": {
"access": "public"
}
}

448
node_modules/@fastify/mongodb/test/index.test.js generated vendored Normal file
View File

@@ -0,0 +1,448 @@
'use strict'
const { test } = require('node:test')
const Fastify = require('fastify')
const fastifyMongo = require('..')
const { ObjectId } = require('..')
const mongodb = require('mongodb')
const NO_DATABASE_MONGODB_URL = 'mongodb://127.0.0.1'
const DATABASE_NAME = 'test'
const MONGODB_URL = 'mongodb://127.0.0.1/' + DATABASE_NAME
const CLIENT_NAME = 'client_name'
const ANOTHER_DATABASE_NAME = 'my_awesome_database'
const COLLECTION_NAME = 'mycoll'
function objectIdTest (t, ObjectId, message) {
t.plan(4)
message = message || 'expect ObjectId value'
const obj1 = new ObjectId()
t.assert.ok(obj1)
const obj2 = new ObjectId(obj1)
t.assert.ok(obj2)
t.assert.ok(obj1.equals(obj2))
const obj3 = new ObjectId()
t.assert.ok(!obj1.equals(obj3))
}
async function clientTest (t, client, message) {
t.plan(1)
message = message || 'expect client'
const db = client.db(DATABASE_NAME)
const col = db.collection(COLLECTION_NAME)
const r = await col.insertMany([{ a: 1 }])
t.assert.strictEqual(1, r.insertedCount)
}
async function databaseTest (t, db, message) {
t.plan(1)
message = message || 'expect database'
const col = db.collection(COLLECTION_NAME)
const r = await col.insertMany([{ a: 1 }])
t.assert.strictEqual(1, r.insertedCount)
}
test('re-export ObjectId', async (t) => {
t.plan(1)
await t.test(async t => objectIdTest(t, fastifyMongo.ObjectId))
})
test('re-export ObjectId destructured', async (t) => {
t.plan(1)
await t.test(async t => objectIdTest(t, ObjectId))
})
test('export of mongodb', async (t) => {
t.plan(2)
t.assert.strictEqual(typeof fastifyMongo.mongodb.ObjectId, 'function')
t.assert.strictEqual(fastifyMongo.mongodb.BSONType.array, 4)
})
test('{ url: NO_DATABASE_MONGODB_URL }', async (t) => {
t.plan(6)
const fastify = await register(t, { url: NO_DATABASE_MONGODB_URL })
t.assert.ok(fastify.mongo)
t.assert.ok(fastify.mongo.client)
t.assert.ok(fastify.mongo.ObjectId)
t.assert.ifError(fastify.mongo.db)
await t.test(async t => objectIdTest(t, fastify.mongo.ObjectId))
await t.test(async t => clientTest(t, fastify.mongo.client))
})
test('{ url: MONGODB_URL }', async (t) => {
t.plan(8)
const fastify = await register(t, { url: MONGODB_URL })
t.assert.ok(fastify.mongo)
t.assert.ok(fastify.mongo.client)
t.assert.ok(fastify.mongo.ObjectId)
t.assert.ok(fastify.mongo.db)
t.assert.strictEqual(fastify.mongo.db.databaseName, DATABASE_NAME)
await t.test(async t => objectIdTest(t, fastify.mongo.ObjectId))
await t.test(async t => clientTest(t, fastify.mongo.client))
await t.test(async t => databaseTest(t, fastify.mongo.db))
})
test('{ url: NO_DATABASE_MONGODB_URL, name: CLIENT_NAME }', async (t) => {
t.plan(11)
const fastify = await register(t, { url: NO_DATABASE_MONGODB_URL, name: CLIENT_NAME })
t.assert.ok(fastify.mongo)
t.assert.ok(fastify.mongo.client)
t.assert.ok(fastify.mongo.ObjectId)
t.assert.ifError(fastify.mongo.db)
t.assert.ok(fastify.mongo[CLIENT_NAME].client)
t.assert.ok(fastify.mongo[CLIENT_NAME].ObjectId)
t.assert.ifError(fastify.mongo[CLIENT_NAME].db)
await t.test(async t => objectIdTest(t, fastify.mongo.ObjectId))
await t.test(async t => clientTest(t, fastify.mongo.client))
await t.test(async t => objectIdTest(t, fastify.mongo[CLIENT_NAME].ObjectId))
await t.test(async t => clientTest(t, fastify.mongo[CLIENT_NAME].client))
})
test('{ url: MONGODB_URL, name: CLIENT_NAME }', async (t) => {
t.plan(15)
const fastify = await register(t, { url: MONGODB_URL, name: CLIENT_NAME })
t.assert.ok(fastify.mongo)
t.assert.ok(fastify.mongo.client)
t.assert.ok(fastify.mongo.ObjectId)
t.assert.ok(fastify.mongo.db)
t.assert.strictEqual(fastify.mongo.db.databaseName, DATABASE_NAME)
t.assert.ok(fastify.mongo[CLIENT_NAME].client)
t.assert.ok(fastify.mongo[CLIENT_NAME].ObjectId)
t.assert.ok(fastify.mongo[CLIENT_NAME].db)
t.assert.strictEqual(fastify.mongo[CLIENT_NAME].db.databaseName, DATABASE_NAME)
await t.test(async t => objectIdTest(t, fastify.mongo.ObjectId))
await t.test(async t => clientTest(t, fastify.mongo.client))
await t.test(async t => databaseTest(t, fastify.mongo.db))
await t.test(async t => objectIdTest(t, fastify.mongo[CLIENT_NAME].ObjectId))
await t.test(async t => clientTest(t, fastify.mongo[CLIENT_NAME].client))
await t.test(async t => databaseTest(t, fastify.mongo[CLIENT_NAME].db))
})
test('{ url: NO_DATABASE_MONGODB_URL, name: CLIENT_NAME, database: ANOTHER_DATABASE_NAME }', async (t) => {
t.plan(15)
const fastify = await register(t, { url: NO_DATABASE_MONGODB_URL, name: CLIENT_NAME, database: ANOTHER_DATABASE_NAME })
t.assert.ok(fastify.mongo)
t.assert.ok(fastify.mongo.client)
t.assert.ok(fastify.mongo.ObjectId)
t.assert.ok(fastify.mongo.db)
t.assert.strictEqual(fastify.mongo.db.databaseName, ANOTHER_DATABASE_NAME)
t.assert.ok(fastify.mongo[CLIENT_NAME].client)
t.assert.ok(fastify.mongo[CLIENT_NAME].ObjectId)
t.assert.ok(fastify.mongo[CLIENT_NAME].db)
t.assert.strictEqual(fastify.mongo[CLIENT_NAME].db.databaseName, ANOTHER_DATABASE_NAME)
await t.test(async t => objectIdTest(t, fastify.mongo.ObjectId))
await t.test(async t => clientTest(t, fastify.mongo.client))
await t.test(async t => databaseTest(t, fastify.mongo.db))
await t.test(async t => objectIdTest(t, fastify.mongo[CLIENT_NAME].ObjectId))
await t.test(async t => clientTest(t, fastify.mongo[CLIENT_NAME].client))
await t.test(async t => databaseTest(t, fastify.mongo[CLIENT_NAME].db))
})
test('{ url: MONGODB_URL, name: CLIENT_NAME, database: ANOTHER_DATABASE_NAME }', async (t) => {
t.plan(15)
const fastify = await register(t, { url: MONGODB_URL, name: CLIENT_NAME, database: ANOTHER_DATABASE_NAME })
t.assert.ok(fastify.mongo)
t.assert.ok(fastify.mongo.client)
t.assert.ok(fastify.mongo.ObjectId)
t.assert.ok(fastify.mongo.db)
t.assert.strictEqual(fastify.mongo.db.databaseName, ANOTHER_DATABASE_NAME)
t.assert.ok(fastify.mongo[CLIENT_NAME].client)
t.assert.ok(fastify.mongo[CLIENT_NAME].ObjectId)
t.assert.ok(fastify.mongo[CLIENT_NAME].db)
t.assert.strictEqual(fastify.mongo[CLIENT_NAME].db.databaseName, ANOTHER_DATABASE_NAME)
await t.test(async t => objectIdTest(t, fastify.mongo.ObjectId))
await t.test(async t => clientTest(t, fastify.mongo.client))
await t.test(async t => databaseTest(t, fastify.mongo.db))
await t.test(async t => objectIdTest(t, fastify.mongo[CLIENT_NAME].ObjectId))
await t.test(async t => clientTest(t, fastify.mongo[CLIENT_NAME].client))
await t.test(async t => databaseTest(t, fastify.mongo[CLIENT_NAME].db))
})
test('{ url: NO_DATABASE_MONGODB_URL, database: ANOTHER_DATABASE_NAME }', async (t) => {
t.plan(8)
const fastify = await register(t, { url: NO_DATABASE_MONGODB_URL, database: ANOTHER_DATABASE_NAME })
t.assert.ok(fastify.mongo)
t.assert.ok(fastify.mongo.client)
t.assert.ok(fastify.mongo.ObjectId)
t.assert.ok(fastify.mongo.db)
t.assert.strictEqual(fastify.mongo.db.databaseName, ANOTHER_DATABASE_NAME)
await t.test(async t => objectIdTest(t, fastify.mongo.ObjectId))
await t.test(async t => clientTest(t, fastify.mongo.client))
await t.test(async t => databaseTest(t, fastify.mongo.db))
})
test('{ url: MONGODB_URL, database: ANOTHER_DATABASE_NAME }', async (t) => {
t.plan(8)
const fastify = await register(t, { url: MONGODB_URL, name: CLIENT_NAME, database: ANOTHER_DATABASE_NAME })
t.assert.ok(fastify.mongo)
t.assert.ok(fastify.mongo.client)
t.assert.ok(fastify.mongo.ObjectId)
t.assert.ok(fastify.mongo.db)
t.assert.strictEqual(fastify.mongo.db.databaseName, ANOTHER_DATABASE_NAME)
await t.test(async t => objectIdTest(t, fastify.mongo.ObjectId))
await t.test(async t => clientTest(t, fastify.mongo.client))
await t.test(async t => databaseTest(t, fastify.mongo.db))
})
test('{ client: client }', async (t) => {
t.plan(6)
const client = await mongodb.MongoClient.connect(NO_DATABASE_MONGODB_URL)
t.after(() => client.close())
const fastify = await register(t, { client })
t.assert.ok(fastify.mongo)
t.assert.ok(fastify.mongo.client)
t.assert.ok(fastify.mongo.ObjectId)
t.assert.ifError(fastify.mongo.db)
await t.test(async t => objectIdTest(t, fastify.mongo.ObjectId))
await t.test(async t => clientTest(t, fastify.mongo.client))
})
test('{ client: client, database: DATABASE_NAME }', async (t) => {
t.plan(8)
const client = await mongodb.MongoClient.connect(NO_DATABASE_MONGODB_URL)
t.after(() => client.close())
const fastify = await register(t, { client, database: ANOTHER_DATABASE_NAME })
t.assert.ok(fastify.mongo)
t.assert.ok(fastify.mongo.client)
t.assert.ok(fastify.mongo.ObjectId)
t.assert.ok(fastify.mongo.db)
t.assert.strictEqual(fastify.mongo.db.databaseName, ANOTHER_DATABASE_NAME)
await t.test(async t => objectIdTest(t, fastify.mongo.ObjectId))
await t.test(async t => clientTest(t, fastify.mongo.client))
await t.test(async t => databaseTest(t, fastify.mongo.db))
})
test('{ client: client, name: CLIENT_NAME }', async (t) => {
t.plan(11)
const client = await mongodb.MongoClient.connect(NO_DATABASE_MONGODB_URL)
t.after(() => client.close())
const fastify = await register(t, { client, name: CLIENT_NAME })
t.assert.ok(fastify.mongo)
t.assert.ok(fastify.mongo.client)
t.assert.ok(fastify.mongo.ObjectId)
t.assert.ifError(fastify.mongo.db)
t.assert.ok(fastify.mongo[CLIENT_NAME].client)
t.assert.ok(fastify.mongo[CLIENT_NAME].ObjectId)
t.assert.ifError(fastify.mongo[CLIENT_NAME].db)
await t.test(async t => objectIdTest(t, fastify.mongo.ObjectId))
await t.test(async t => clientTest(t, fastify.mongo.client))
await t.test(async t => objectIdTest(t, fastify.mongo[CLIENT_NAME].ObjectId))
await t.test(async t => clientTest(t, fastify.mongo[CLIENT_NAME].client))
})
test('{ client: client, name: CLIENT_NAME, database: ANOTHER_DATABASE_NAME }', async (t) => {
t.plan(15)
const client = await mongodb.MongoClient.connect(NO_DATABASE_MONGODB_URL)
t.after(() => client.close())
const fastify = await register(t, { client, name: CLIENT_NAME, database: ANOTHER_DATABASE_NAME })
t.assert.ok(fastify.mongo)
t.assert.ok(fastify.mongo.client)
t.assert.ok(fastify.mongo.ObjectId)
t.assert.ok(fastify.mongo.db)
t.assert.strictEqual(fastify.mongo.db.databaseName, ANOTHER_DATABASE_NAME)
t.assert.ok(fastify.mongo[CLIENT_NAME].client)
t.assert.ok(fastify.mongo[CLIENT_NAME].ObjectId)
t.assert.ok(fastify.mongo[CLIENT_NAME].db)
t.assert.strictEqual(fastify.mongo[CLIENT_NAME].db.databaseName, ANOTHER_DATABASE_NAME)
await t.test(async t => objectIdTest(t, fastify.mongo.ObjectId))
await t.test(async t => clientTest(t, fastify.mongo.client))
await t.test(async t => databaseTest(t, fastify.mongo.db))
await t.test(async t => objectIdTest(t, fastify.mongo[CLIENT_NAME].ObjectId))
await t.test(async t => clientTest(t, fastify.mongo[CLIENT_NAME].client))
await t.test(async t => databaseTest(t, fastify.mongo[CLIENT_NAME].db))
})
test('{ client: client } does not set onClose', async (t) => {
const client = await mongodb.MongoClient.connect(NO_DATABASE_MONGODB_URL)
t.after(() => client.close())
const fastify = Fastify()
fastify.register(fastifyMongo, { client, database: DATABASE_NAME })
await fastify.ready()
await fastify.close()
await t.test(async t => databaseTest(t, fastify.mongo.db))
})
test('{ }', async (t) => {
t.plan(2)
try {
await register(t, {})
} catch (err) {
t.assert.ok(err)
t.assert.strictEqual(err.message, '`url` parameter is mandatory if no client is provided')
}
})
test('{ url: "unknown://protocol" }', async (t) => {
t.plan(2)
try {
await register(t, { url: 'unknown://protocol' })
} catch (err) {
t.assert.ok(err)
t.assert.match(err.message, /expected connection string/)
}
})
test('double register without name', async (t) => {
t.plan(2)
const fastify = Fastify()
t.after(() => fastify.close())
try {
await fastify
.register(fastifyMongo, { url: MONGODB_URL })
.register(fastifyMongo, { url: MONGODB_URL })
.ready()
} catch (err) {
t.assert.ok(err)
t.assert.strictEqual(err.message, 'fastify-mongodb has already registered')
}
})
test('double register with different name', async (t) => {
t.plan(22)
const fastify = Fastify()
t.after(() => fastify.close())
await fastify
.register(fastifyMongo, { url: MONGODB_URL, name: 'client1' })
.register(fastifyMongo, { url: NO_DATABASE_MONGODB_URL, name: 'client2', database: ANOTHER_DATABASE_NAME })
.ready()
t.assert.ok(fastify.mongo)
t.assert.ok(fastify.mongo.client)
t.assert.ok(fastify.mongo.ObjectId)
t.assert.ok(fastify.mongo.db)
t.assert.strictEqual(fastify.mongo.db.databaseName, DATABASE_NAME)
t.assert.ok(fastify.mongo.client1.client)
t.assert.ok(fastify.mongo.client1.ObjectId)
t.assert.ok(fastify.mongo.client1.db)
t.assert.strictEqual(fastify.mongo.client1.db.databaseName, DATABASE_NAME)
t.assert.ok(fastify.mongo.client2.client)
t.assert.ok(fastify.mongo.client2.ObjectId)
t.assert.ok(fastify.mongo.client2.db)
t.assert.strictEqual(fastify.mongo.client2.db.databaseName, ANOTHER_DATABASE_NAME)
await t.test(async t => objectIdTest(t, fastify.mongo.ObjectId))
await t.test(async t => clientTest(t, fastify.mongo.client))
await t.test(async t => databaseTest(t, fastify.mongo.db))
await t.test(async t => objectIdTest(t, fastify.mongo.client1.ObjectId))
await t.test(async t => clientTest(t, fastify.mongo.client1.client))
await t.test(async t => databaseTest(t, fastify.mongo.client1.db))
await t.test(async t => objectIdTest(t, fastify.mongo.client2.ObjectId))
await t.test(async t => clientTest(t, fastify.mongo.client2.client))
await t.test(async t => databaseTest(t, fastify.mongo.client2.db))
})
test('double register with the same name', async (t) => {
t.plan(2)
const fastify = Fastify()
t.after(() => fastify.close())
try {
await fastify
.register(fastifyMongo, { url: MONGODB_URL, name: CLIENT_NAME })
.register(fastifyMongo, { url: MONGODB_URL, name: CLIENT_NAME })
.ready()
} catch (err) {
t.assert.ok(err)
t.assert.strictEqual(err.message, 'Connection name already registered: ' + CLIENT_NAME)
}
})
test('Immutable options', async (t) => {
t.plan(1)
const given = { url: MONGODB_URL, name: CLIENT_NAME, database: DATABASE_NAME }
await register(t, given)
t.assert.deepStrictEqual(given, {
url: MONGODB_URL,
name: CLIENT_NAME,
database: DATABASE_NAME
})
})
test('timeout', async (t) => {
t.plan(2)
const fastify = Fastify()
t.after(() => fastify.close())
try {
await fastify
.register(fastifyMongo, { url: 'mongodb://127.0.0.1:9999' })
.ready()
} catch (err) {
t.assert.ok(err)
t.assert.strictEqual(err.message, 'connect ECONNREFUSED 127.0.0.1:9999')
}
})
async function register (t, options) {
const fastify = Fastify()
t.after(() => fastify.close())
fastify.register(fastifyMongo, options)
await fastify.ready()
return fastify
}

62
node_modules/@fastify/mongodb/types/index.d.ts generated vendored Normal file
View File

@@ -0,0 +1,62 @@
import type { FastifyPluginAsync } from 'fastify'
import type { Db, MongoClient, MongoClientOptions } from 'mongodb'
import mongodb, { ObjectId } from 'mongodb'
declare module 'fastify' {
interface FastifyInstance {
mongo: fastifyMongodb.FastifyMongoObject & fastifyMongodb.FastifyMongoNestedObject;
}
}
type FastifyMongodb = FastifyPluginAsync<fastifyMongodb.FastifyMongodbOptions>
declare namespace fastifyMongodb {
export interface FastifyMongoObject {
/**
* Mongo client instance
*/
client: MongoClient;
/**
* DB instance
*/
db?: Db;
/**
* Mongo ObjectId class
*/
ObjectId: typeof mongodb.ObjectId;
}
export interface FastifyMongoNestedObject {
[name: string]: FastifyMongoObject;
}
export interface FastifyMongodbOptions extends MongoClientOptions {
/**
* Force to close the mongodb connection when app stopped
* @default false
*/
forceClose?: boolean;
/**
* Database name to connect
*/
database?: string;
name?: string;
/**
* Pre-configured instance of MongoClient
*/
client?: MongoClient;
/**
* Connection url
*/
url?: string;
}
export { ObjectId, mongodb }
export const fastifyMongodb: FastifyMongodb
export { fastifyMongodb as default }
}
declare function fastifyMongodb (...params: Parameters<FastifyMongodb>): ReturnType<FastifyMongodb>
export = fastifyMongodb

23
node_modules/@fastify/mongodb/types/index.test-d.ts generated vendored Normal file
View File

@@ -0,0 +1,23 @@
import { ObjectId } from 'bson'
import fastify from 'fastify'
import { expectNotType, expectType } from 'tsd'
import fastifyMongodb, { mongodb, ObjectId as ReExportedObjectId } from '..'
const app = fastify()
app
.register(fastifyMongodb, {
database: 'testdb',
name: 'db',
url: 'mongodb://localhost:27017/testdb',
})
.after((_err) => {
app.mongo.client.db('test')
expectNotType<undefined>(app.mongo.db)
const ObjectId = app.mongo.ObjectId
expectType<ObjectId>(new ReExportedObjectId('aaaa'))
expectType<ObjectId>(new ObjectId('aaa'))
})
expectType<typeof ReExportedObjectId>(mongodb.ObjectId)
expectType<4>(mongodb.BSONType.array)