Skip to content

Commit

Permalink
Restructure + nodenext compatibility (#197)
Browse files Browse the repository at this point in the history
* restructure

* nodenext compatibility

* disable package-lock generation

* fix
  • Loading branch information
Uzlopak authored Nov 24, 2022
1 parent ece4358 commit a3315d3
Show file tree
Hide file tree
Showing 9 changed files with 79 additions and 63 deletions.
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ jobs:
test:
uses: fastify/workflows/.github/workflows/plugins-ci-mongo.yml@v3
with:
lint: true
license-check: true
1 change: 1 addition & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package-lock=false
2 changes: 2 additions & 0 deletions .taprc
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
files:
- 'test/**/*.test.js'
55 changes: 0 additions & 55 deletions index.d.ts

This file was deleted.

2 changes: 2 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,5 +80,7 @@ module.exports = fp(fastifyMongodb, {
fastify: '4.x',
name: '@fastify/mongodb'
})
module.exports.default = fastifyMongodb
module.exports.fastifyMongodb = fastifyMongodb

module.exports.ObjectId = ObjectId
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@
"version": "6.1.0",
"description": "Fastify MongoDB connection plugin",
"main": "index.js",
"types": "types/index.d.ts",
"scripts": {
"coverage": "npm run unit -- --cov --coverage-report=html",
"lint": "standard",
"lint:fix": "standard --fix",
"mongo": "docker run --rm -d -p 27017:27017 mongo:5.0.0",
"test": "npm run lint && npm run unit && npm run typescript",
"typescript": "tsd",
"unit": "tap test.js"
"test": "npm run test:unit && npm run test:typescript",
"test:typescript": "tsd",
"test:unit": "tap"
},
"repository": {
"type": "git",
Expand Down
4 changes: 2 additions & 2 deletions test.js → test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
const t = require('tap')
const { test } = t
const Fastify = require('fastify')
const fastifyMongo = require('./index')
const { ObjectId } = require('./index')
const fastifyMongo = require('..')
const { ObjectId } = require('..')

const mongodb = require('mongodb')

Expand Down
62 changes: 62 additions & 0 deletions types/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import type { FastifyPluginAsync } from 'fastify';
import type { Db, MongoClient, MongoClientOptions } from 'mongodb';
import { 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 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 }

export const fastifyMongodb: FastifyMongodb
export { fastifyMongodb as default }
}

declare function fastifyMongodb(...params: Parameters<FastifyMongodb>): ReturnType<FastifyMongodb>
export = fastifyMongodb
8 changes: 5 additions & 3 deletions index.test-d.ts → types/index.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import fastify from 'fastify';
import fastifyMongodb, { ObjectId as reExportedObjectId } from '../fastify-mongodb';
import fastifyMongodb, { ObjectId as reExportedObjectId } from '..';
import { expectType } from "tsd";
import { ObjectID } from 'bson';

const app = fastify();

Expand All @@ -13,6 +15,6 @@ app
app.mongo.client.db('test');
app.mongo.db!;
const ObjectId = app.mongo.ObjectId;
new ObjectId('aaaa');
new reExportedObjectId('aaaa');
expectType<ObjectID>(new reExportedObjectId('aaaa'))
expectType<ObjectID>(new ObjectId('aaa'))
});

0 comments on commit a3315d3

Please sign in to comment.