Update Multer gridfs storage api to version 3 (#29498)

* Update multer-grdfs-storage definitions to version 3

* Update tests in older version to reflect changes in mongodb3 api
This commit is contained in:
Rafael
2018-10-08 12:32:34 -04:00
committed by Andy
parent 2934e2c7b1
commit 6f05b6429b
6 changed files with 236 additions and 20 deletions

View File

@@ -1,4 +1,4 @@
// Type definitions for multer-gridfs-storage 2.0
// Type definitions for multer-gridfs-storage 3.1
// Project: https://github.com/devconcept/multer-gridfs-storage
// Definitions by: devconcept <https://github.com/devconcept>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
@@ -7,37 +7,69 @@
import { EventEmitter } from 'events';
import { Express } from 'express';
import * as Multer from 'multer';
import { Db } from 'mongodb';
import { Db, MongoClient } from 'mongodb';
import { Connection, Mongoose } from 'mongoose';
declare class Cache {
initialize(opts: object): object;
findUri(cacheName: string, url: string): string;
has(cacheIndex: object): boolean;
get(cacheIndex: object): object;
set(cacheIndex: object, value: object): void;
isPending(cacheIndex: object): boolean;
isOpening(cacheIndex: object): boolean;
resolve(cacheIndex: object, db: Db, client: MongoClient): void;
reject(cacheIndex: object, err: Error): void;
waitFor(cacheIndex: object): Promise<object>;
connections(): number;
remove(cacheIndex: object): void;
clear(): void;
}
interface MulterGfsOptions {
file?(req: Express.Request, file: Express.Multer.File): any;
}
declare class MulterGridfsStorage extends EventEmitter implements Multer.StorageEngine {
constructor(settings: MulterGridfsStorage.UrlStorageOptions | MulterGridfsStorage.DbStorageOptions);
db: Db;
client: MongoClient;
connected: boolean;
connecting: boolean;
configuration: MulterGridfsStorage.UrlStorageOptions | MulterGridfsStorage.DbStorageOptions;
error: Error;
caching: boolean;
cacheName: string;
cacheIndex: object;
constructor(configuration: MulterGridfsStorage.UrlStorageOptions | MulterGridfsStorage.DbStorageOptions);
_handleFile(req: Express.Request, file: Express.Multer.File, callback: (error?: any, info?: Express.Multer.File) => void): void;
_removeFile(req: Express.Request, file: Express.Multer.File, callback: (error: Error) => void): void;
static cache: Cache;
}
declare namespace MulterGridfsStorage {
interface UrlStorageOptions extends MulterGfsOptions {
url: string;
connectionOpts?: any;
options?: any;
cache?: boolean | string;
}
interface DbStorageOptions extends MulterGfsOptions {
db: Promise<Db> | Db;
db: Mongoose | Connection | Db | Promise<Mongoose | Connection | Db>;
}
interface FileConfig {
filename?: string;
id?: any;
metadata?: any;
metadata?: object;
chunkSize?: number;
bucketName?: string;
contentType?: string;
aliases?: string[];
disableMD5?: boolean;
}
}

View File

@@ -1,5 +1,6 @@
import MulterGridfsStorage = require('multer-gridfs-storage');
import { Db, MongoClient, Server } from "mongodb";
import { Db, MongoClient, Server } from 'mongodb';
import * as mongoose from 'mongoose';
// Exported interfaces
const conf: MulterGridfsStorage.FileConfig = {
@@ -9,10 +10,16 @@ const conf: MulterGridfsStorage.FileConfig = {
// Connection promise
const dbPromise = MongoClient.connect('mongodb://yourhost:27017/database');
// Mongoose promise
const mongoosePromise = mongoose.connect('mongodb://yourhost:27017/database');
const mgConnectionPromise = mongoose
.connect('mongodb://yourhost:27017/database')
.then(instance => instance.connection);
const server = new Server('localhost', 27017);
const db = new Db('database', server);
// Database instance
const opt1: MulterGridfsStorage.DbStorageOptions = {
db,
file: (req, file) => {
@@ -24,9 +31,31 @@ const opt1: MulterGridfsStorage.DbStorageOptions = {
}
};
const opt2: MulterGridfsStorage.UrlStorageOptions = {
const dbFileStorage = new MulterGridfsStorage(opt1);
const opt2: MulterGridfsStorage.DbStorageOptions = {
db: mongoosePromise,
file: (req, file) => {
return {
disableMd5: true
};
}
};
const opt3: MulterGridfsStorage.DbStorageOptions = {
db: mgConnectionPromise,
file: (req, file) => {
return 5;
}
};
const mongooseStorage = new MulterGridfsStorage(opt2);
const mgConnectionStorage = new MulterGridfsStorage(opt3);
// Url based instance
const opt4: MulterGridfsStorage.UrlStorageOptions = {
url: 'mongodb://yourhost:27017/database',
connectionOpts: {},
options: {},
file: (req, file) => {
return {
metadata: file.mimetype
@@ -34,25 +63,30 @@ const opt2: MulterGridfsStorage.UrlStorageOptions = {
}
};
// All options
const dbFileStorage = new MulterGridfsStorage(opt1);
const urlFileStorage = new MulterGridfsStorage(opt4);
const urlFileStorage = new MulterGridfsStorage(opt2);
// Cache
const opt5: MulterGridfsStorage.UrlStorageOptions = {
url: 'mongodb://yourhost:27017/database',
cache: 'cache'
};
const cachedStorage = new MulterGridfsStorage(opt5);
// Other properties are optional
const promiseStorage = new MulterGridfsStorage({
db: dbPromise
});
const promiseStorage = new MulterGridfsStorage({db: dbPromise});
const dbStorage = new MulterGridfsStorage({
db
});
const dbStorage = new MulterGridfsStorage({db});
const urlStorage = new MulterGridfsStorage({
url: 'mongodb://yourhost:27017/database'
});
// Extends event emitter
promiseStorage.on('connection', () => {});
urlStorage.addListener('conection', () => {});
promiseStorage.on('connection', () => {
});
urlStorage.addListener('conection', () => {
});
dbStorage.removeAllListeners('conection');
MulterGridfsStorage.cache.connections();

View File

@@ -0,0 +1,63 @@
// Type definitions for multer-gridfs-storage 2.0
// Project: https://github.com/devconcept/multer-gridfs-storage
// Definitions by: devconcept <https://github.com/devconcept>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 2.3
import { EventEmitter } from 'events';
import { Express } from 'express';
import * as Multer from 'multer';
import { Db } from 'mongodb';
interface MulterGfsOptions {
file?(req: Express.Request, file: Express.Multer.File): any;
}
declare class MulterGridfsStorage extends EventEmitter implements Multer.StorageEngine {
constructor(settings: MulterGridfsStorage.UrlStorageOptions | MulterGridfsStorage.DbStorageOptions);
_handleFile(req: Express.Request, file: Express.Multer.File, callback: (error?: any, info?: Express.Multer.File) => void): void;
_removeFile(req: Express.Request, file: Express.Multer.File, callback: (error: Error) => void): void;
}
declare namespace MulterGridfsStorage {
interface UrlStorageOptions extends MulterGfsOptions {
url: string;
connectionOpts?: any;
}
interface DbStorageOptions extends MulterGfsOptions {
db: Promise<Db> | Db;
}
interface FileConfig {
filename?: string;
id?: any;
metadata?: any;
chunkSize?: number;
bucketName?: string;
contentType?: string;
}
}
// Merge multer's file declaration with ours
declare global {
namespace Express {
namespace Multer {
interface File {
id: any;
filename: string;
metadata: any;
contentType: string;
chunkSize: number;
bucketName: string;
uploadDate: Date;
md5: string;
size: number;
}
}
}
}
export = MulterGridfsStorage;

View File

@@ -0,0 +1,58 @@
import MulterGridfsStorage = require('multer-gridfs-storage');
import { Db, MongoClient, Server } from "mongodb";
// Exported interfaces
const conf: MulterGridfsStorage.FileConfig = {
filename: 'name',
bucketName: 'plants'
};
// Connection promise
const dbPromise = MongoClient.connect('mongodb://yourhost:27017').then((client) => client.db('database'));
const server = new Server('localhost', 27017);
const db = new Db('database', server);
const opt1: MulterGridfsStorage.DbStorageOptions = {
db,
file: (req, file) => {
return new Promise((resolve) => {
resolve({
filename: file.originalname
});
});
}
};
const opt2: MulterGridfsStorage.UrlStorageOptions = {
url: 'mongodb://yourhost:27017/database',
connectionOpts: {},
file: (req, file) => {
return {
metadata: file.mimetype
};
}
};
// All options
const dbFileStorage = new MulterGridfsStorage(opt1);
const urlFileStorage = new MulterGridfsStorage(opt2);
// Other properties are optional
const promiseStorage = new MulterGridfsStorage({
db: dbPromise
});
const dbStorage = new MulterGridfsStorage({
db
});
const urlStorage = new MulterGridfsStorage({
url: 'mongodb://yourhost:27017/database'
});
// Extends event emitter
promiseStorage.on('connection', () => {});
urlStorage.addListener('conection', () => {});
dbStorage.removeAllListeners('conection');

View File

@@ -0,0 +1,28 @@
{
"compilerOptions": {
"module": "commonjs",
"lib": [
"es6"
],
"noImplicitAny": true,
"noImplicitThis": true,
"strictNullChecks": true,
"strictFunctionTypes": true,
"baseUrl": "../../",
"typeRoots": [
"../../"
],
"paths": {
"multer-gridfs-storage": [
"multer-gridfs-storage/v2"
]
},
"types": [],
"noEmit": true,
"forceConsistentCasingInFileNames": true
},
"files": [
"index.d.ts",
"multer-gridfs-storage-tests.ts"
]
}

View File

@@ -0,0 +1 @@
{ "extends": "dtslint/dt.json" }