From f3f5e01ea85cc32dbd104e38e5b22d3172d004cb Mon Sep 17 00:00:00 2001 From: Evan Broder Date: Wed, 8 Feb 2023 15:48:14 -0800 Subject: [PATCH] Thread upstream mongodb types through npm-mongo As it exists currently, the types for the mongo package don't work out of the box because they can't find the types for the mongodb NPM package. TypeScript app developers could work around this by adding mongodb directly as a dependency, but they shouldn't have to. However, `zodern:types` can handle pulling in types for NPM dependencies, but only if the package _with_ those NPM dependencies is itself exporting type declarations. Since npm-mongo is actually depending on the mongodb package, add a type declaration reflecting both the types and objects that it exports. Then switch the mongo package to import from npm-mongo, giving it access to the types for the mongodb package. --- packages/mongo/mongo.d.ts | 41 +++++++++++++---------------------- packages/npm-mongo/index.d.ts | 3 +++ packages/npm-mongo/package.js | 1 + 3 files changed, 19 insertions(+), 26 deletions(-) create mode 100644 packages/npm-mongo/index.d.ts diff --git a/packages/mongo/mongo.d.ts b/packages/mongo/mongo.d.ts index e72bccaabe..5db84aa59f 100644 --- a/packages/mongo/mongo.d.ts +++ b/packages/mongo/mongo.d.ts @@ -1,12 +1,4 @@ -import * as MongoNpmModule from 'mongodb'; -import { - Collection as MongoCollection, - CreateIndexesOptions, - Db as MongoDb, - Hint, - IndexSpecification, - MongoClient, -} from 'mongodb'; +import { NpmModuleMongodb } from 'meteor/npm-mongo'; import { Meteor } from 'meteor/meteor'; // Based on https://github.com/microsoft/TypeScript/issues/28791#issuecomment-443520161 @@ -16,13 +8,13 @@ export type UnionOmit = T extends T export namespace Mongo { - export type Selector = MongoNpmModule.Filter; + export type Selector = NpmModuleMongodb.Filter; - type Modifier = MongoNpmModule.UpdateFilter; + type Modifier = NpmModuleMongodb.UpdateFilter; export type OptionalId = UnionOmit & { _id?: any }; - type SortSpecifier = MongoNpmModule.Sort; + type SortSpecifier = NpmModuleMongodb.Sort; export interface FieldSpecifier { [id: string]: Number; @@ -40,7 +32,7 @@ export namespace Mongo { /** Dictionary of fields to return or exclude. */ fields?: FieldSpecifier | undefined; /** (Server only) Overrides MongoDB's default index selection and query optimization process. Specify an index to force its use, either by its name or index specification. */ - hint?: Hint | undefined; + hint?: NpmModuleMongodb.Hint | undefined; /** (Client only) Default `true`; pass `false` to disable reactivity */ reactive?: boolean | undefined; /** Overrides `transform` on the [`Collection`](#collections) for this cursor. Pass `null` to disable transformation. */ @@ -61,7 +53,7 @@ export namespace Mongo { * Constructor for a Collection * @param name The name of the collection. If null, creates an unmanaged (unsynchronized) local collection. */ - new ( + new ( name: string | null, options?: { /** @@ -86,7 +78,7 @@ export namespace Mongo { } ): Collection; } - interface Collection { + interface Collection { allow = undefined>(options: { insert?: | ((userId: string, doc: DispatchTransform) => boolean) @@ -110,12 +102,10 @@ export namespace Mongo { maxDocuments?: number ): Promise; createIndex( - indexSpec: IndexSpecification, - options?: CreateIndexesOptions + options?: NpmModuleMongodb.CreateIndexesOptions ): void; createIndexAsync( - indexSpec: IndexSpecification, - options?: CreateIndexesOptions + options?: NpmModuleMongodb.CreateIndexesOptions ): Promise; deny = undefined>(options: { insert?: @@ -194,12 +184,12 @@ export namespace Mongo { * Returns the [`Collection`](http://mongodb.github.io/node-mongodb-native/3.0/api/Collection.html) object corresponding to this collection from the * [npm `mongodb` driver module](https://www.npmjs.com/package/mongodb) which is wrapped by `Mongo.Collection`. */ - rawCollection(): MongoCollection; + rawCollection(): NpmModuleMongodb.Collection; /** * Returns the [`Db`](http://mongodb.github.io/node-mongodb-native/3.0/api/Db.html) object corresponding to this collection's database connection from the * [npm `mongodb` driver module](https://www.npmjs.com/package/mongodb) which is wrapped by `Mongo.Collection`. */ - rawDatabase(): MongoDb; + rawDatabase(): NpmModuleMongodb.Db; /** * Remove documents from the collection * @param selector Specifies which documents to remove @@ -303,8 +293,7 @@ export namespace Mongo { _createCappedCollection(byteSize?: number, maxDocuments?: number): void; /** @deprecated */ _ensureIndex( - indexSpec: IndexSpecification, - options?: CreateIndexesOptions + options?: NpmModuleMongodb.CreateIndexesOptions ): void; _dropCollection(): Promise; _dropIndex(indexName: string): void; @@ -448,8 +437,8 @@ export namespace Mongo { export declare module MongoInternals { interface MongoConnection { - db: MongoDb; - client: MongoClient; + db: NpmModuleMongodb.Db; + client: NpmModuleMongodb.MongoClient; } function defaultRemoteCollectionDriver(): { @@ -459,7 +448,7 @@ export declare module MongoInternals { var NpmModules: { mongodb: { version: string; - module: typeof MongoNpmModule; + module: typeof NpmModuleMongodb; }; }; } diff --git a/packages/npm-mongo/index.d.ts b/packages/npm-mongo/index.d.ts new file mode 100644 index 0000000000..d7ab3d7558 --- /dev/null +++ b/packages/npm-mongo/index.d.ts @@ -0,0 +1,3 @@ +import * as NpmModuleMongodb from 'mongodb'; +declare const NpmModuleMongodbVersion: string; +export { NpmModuleMongodb, NpmModuleMongodbVersion }; diff --git a/packages/npm-mongo/package.js b/packages/npm-mongo/package.js index de803fad90..818f243b5f 100644 --- a/packages/npm-mongo/package.js +++ b/packages/npm-mongo/package.js @@ -17,4 +17,5 @@ Package.onUse(function (api) { "NpmModuleMongodb", "NpmModuleMongodbVersion", ], "server"); + api.addAssets('index.d.ts', 'server'); });