Updated multer-gridfs-storage to v4 (#40122)

* Updated to v4

* Fixed tslint path for old package
This commit is contained in:
Rafael 2019-11-04 11:32:26 -05:00 committed by Nathan Shively-Sanders
parent cb5ee9a060
commit e671fbc00a
7 changed files with 258 additions and 5 deletions

View File

@ -1,4 +1,4 @@
// Type definitions for multer-gridfs-storage 3.1
// Type definitions for multer-gridfs-storage 4.0
// Project: https://github.com/devconcept/multer-gridfs-storage
// Definitions by: devconcept <https://github.com/devconcept>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
@ -47,10 +47,19 @@ declare class MulterGridfsStorage extends EventEmitter implements Multer.Storage
_removeFile(req: Express.Request, file: Express.Multer.File, callback: (error: Error) => void): void;
ready(): Promise<MulterGridfsStorage.ConnectionResult>;
static cache: Cache;
static generateBytes(): Promise<{filename: string}>;
}
declare namespace MulterGridfsStorage {
interface ConnectionResult {
db: Db;
client?: MongoClient;
}
interface UrlStorageOptions extends MulterGfsOptions {
url: string;
options?: any;
@ -58,7 +67,8 @@ declare namespace MulterGridfsStorage {
}
interface DbStorageOptions extends MulterGfsOptions {
db: Mongoose | Connection | Db | MongoClient | Promise<Mongoose | Connection | Db | MongoClient>;
db: Mongoose | Connection | Db | Promise<Mongoose | Connection | Db>;
client?: MongoClient | Promise<MongoClient>;
}
interface FileConfig {

View File

@ -8,8 +8,6 @@ const conf: MulterGridfsStorage.FileConfig = {
bucketName: 'plants'
};
// Connection promise
const dbPromise = MongoClient.connect('mongodb://yourhost:27017/database');
// Mongoose promise
const mongoosePromise = mongoose.connect('mongodb://yourhost:27017/database');
const mgConnectionPromise = mongoose
@ -71,10 +69,24 @@ const opt5: MulterGridfsStorage.UrlStorageOptions = {
cache: 'cache'
};
// Generators
const opt6: MulterGridfsStorage.UrlStorageOptions = {
url: 'mongodb://yourhost:27017/database',
*file(req, file) {
yield {
metadata: file.mimetype
};
}
};
const cachedStorage = new MulterGridfsStorage(opt5);
// Connection promise
const clientPromise = MongoClient.connect('mongodb://yourhost:27017/database');
const dbPromise = clientPromise.then(client => client.db('database'));
// Other properties are optional
const promiseStorage = new MulterGridfsStorage({db: dbPromise});
const clientPromiseStorage = new MulterGridfsStorage({db: dbPromise, client: clientPromise});
const dbStorage = new MulterGridfsStorage({db});
@ -82,6 +94,17 @@ const urlStorage = new MulterGridfsStorage({
url: 'mongodb://yourhost:27017/database'
});
// Ready method
clientPromiseStorage.ready().then(result => {
const db = result.db;
const client = result.client;
});
MulterGridfsStorage.generateBytes().then(result => {
const filename = result.filename;
console.log(result);
});
// Extends event emitter
promiseStorage.on('connection', () => {
});

View File

@ -20,4 +20,4 @@
"index.d.ts",
"multer-gridfs-storage-tests.ts"
]
}
}

View File

@ -0,0 +1,97 @@
// 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
// TypeScript Version: 3.0
import { EventEmitter } from 'events';
import { Express } from 'express';
import * as Multer from 'multer';
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 {
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;
ready: Promise<Db>;
static cache: Cache;
}
declare namespace MulterGridfsStorage {
interface UrlStorageOptions extends MulterGfsOptions {
url: string;
options?: any;
cache?: boolean | string;
}
interface DbStorageOptions extends MulterGfsOptions {
db: Mongoose | Connection | Db | Promise<Mongoose | Connection | Db>;
}
interface FileConfig {
filename?: string;
id?: any;
metadata?: object;
chunkSize?: number;
bucketName?: string;
contentType?: string;
aliases?: string[];
disableMD5?: boolean;
}
}
// 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,94 @@
import MulterGridfsStorage = require('multer-gridfs-storage');
import { Db, MongoClient, Server } from 'mongodb';
import * as mongoose from 'mongoose';
// Exported interfaces
const conf: MulterGridfsStorage.FileConfig = {
filename: 'name',
bucketName: 'plants'
};
// 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) => {
return new Promise((resolve) => {
resolve({
filename: file.originalname
});
});
}
};
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',
options: {},
file: (req, file) => {
return {
metadata: file.mimetype
};
}
};
const urlFileStorage = new MulterGridfsStorage(opt4);
// Cache
const opt5: MulterGridfsStorage.UrlStorageOptions = {
url: 'mongodb://yourhost:27017/database',
cache: 'cache'
};
const cachedStorage = new MulterGridfsStorage(opt5);
// Connection promise
const dbPromise = MongoClient.connect('mongodb://yourhost:27017/database')
.then(client => client.db('database'));
// 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');
MulterGridfsStorage.cache.connections();

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/v3"
]
},
"types": [],
"noEmit": true,
"forceConsistentCasingInFileNames": true
},
"files": [
"index.d.ts",
"multer-gridfs-storage-tests.ts"
]
}

View File

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