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.
This commit is contained in:
Evan Broder
2023-02-08 15:48:14 -08:00
parent 366e18117c
commit f3f5e01ea8
3 changed files with 19 additions and 26 deletions

View File

@@ -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, K extends keyof any> = T extends T
export namespace Mongo {
export type Selector<T> = MongoNpmModule.Filter<T>;
export type Selector<T> = NpmModuleMongodb.Filter<T>;
type Modifier<T> = MongoNpmModule.UpdateFilter<T>;
type Modifier<T> = NpmModuleMongodb.UpdateFilter<T>;
export type OptionalId<TSchema> = UnionOmit<TSchema, '_id'> & { _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 <T extends MongoNpmModule.Document, U = T>(
new <T extends NpmModuleMongodb.Document, U = T>(
name: string | null,
options?: {
/**
@@ -86,7 +78,7 @@ export namespace Mongo {
}
): Collection<T, U>;
}
interface Collection<T extends MongoNpmModule.Document, U = T> {
interface Collection<T extends NpmModuleMongodb.Document, U = T> {
allow<Fn extends Transform<T> = undefined>(options: {
insert?:
| ((userId: string, doc: DispatchTransform<Fn, T, U>) => boolean)
@@ -110,12 +102,10 @@ export namespace Mongo {
maxDocuments?: number
): Promise<void>;
createIndex(
indexSpec: IndexSpecification,
options?: CreateIndexesOptions
options?: NpmModuleMongodb.CreateIndexesOptions
): void;
createIndexAsync(
indexSpec: IndexSpecification,
options?: CreateIndexesOptions
options?: NpmModuleMongodb.CreateIndexesOptions
): Promise<void>;
deny<Fn extends Transform<T> = 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<T>;
rawCollection(): NpmModuleMongodb.Collection<T>;
/**
* 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<void>;
_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;
};
};
}

3
packages/npm-mongo/index.d.ts vendored Normal file
View File

@@ -0,0 +1,3 @@
import * as NpmModuleMongodb from 'mongodb';
declare const NpmModuleMongodbVersion: string;
export { NpmModuleMongodb, NpmModuleMongodbVersion };

View File

@@ -17,4 +17,5 @@ Package.onUse(function (api) {
"NpmModuleMongodb",
"NpmModuleMongodbVersion",
], "server");
api.addAssets('index.d.ts', 'server');
});