Add types for mongodb-queue (#43021)

This commit is contained in:
Kyle
2020-03-14 19:14:03 -07:00
committed by GitHub
parent c6b8f6a519
commit 171968299c
4 changed files with 161 additions and 0 deletions
+49
View File
@@ -0,0 +1,49 @@
// Type definitions for mongodb-queue 4.0
// Project: https://github.com/chilts/mongodb-queue
// Definitions by: FiveOFive <https://github.com/FiveOFive>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 3.0
import { Db, MongoError } from 'mongodb';
declare function mongodbQueue(db: Db, name: string, opts?: mongodbQueue.QueueOptions): mongodbQueue.Queue;
declare namespace mongodbQueue {
class Queue {
constructor(db: Db, name: string, opts?: QueueOptions);
createIndexes(callback: QueueCallback<string>): void;
add(payload: string, callback: QueueCallback<string>): void;
add(payload: string, opts: QueueOptions, callback: QueueCallback<string>): void;
get(callback: QueueCallback<QueueMessage | undefined>): void;
get(opts: QueueOptions, callback: QueueCallback<QueueMessage | undefined>): void;
ping(ack: string, callback: QueueCallback<string>): void;
ping(ack: string, opts: QueueOptions, callback: QueueCallback<string>): void;
ack(ack: string, callback: QueueCallback<string>): void;
clean(callback: QueueCallback<any>): void;
total(callback: QueueCallback<number>): void;
size(callback: QueueCallback<number>): void;
inflight(callback: QueueCallback<number>): void;
done(callback: QueueCallback<number>): void;
}
interface QueueOptions {
deadQueue?: string;
delay?: number;
maxRetries?: number;
visibility?: number;
}
interface QueueMessage {
ack: string;
id: string;
payload: string;
tries: number;
}
interface QueueCallback<T> {
(err: MongoError | Error, result: T): void;
}
}
export = mongodbQueue;
@@ -0,0 +1,88 @@
import * as queue from 'mongodb-queue';
import { MongoClient } from 'mongodb';
const mongoClient = new MongoClient('mongodb://localhost:27017');
mongoClient.connect().then(() => {
const opts: queue.QueueOptions = {
delay: 0,
maxRetries: 5,
visibility: 30,
};
const mongoQueue = queue(mongoClient.db('test'), 'test-queue', opts);
mongoQueue.add('foo', (err, res) => {
if (err) {
console.log('error: ' + err);
} else {
const msgId: string = res;
console.log('msgId: ' + msgId);
}
});
mongoQueue.get((err, res) => {
if (err) {
console.log('error: ' + err);
} else {
if (res) {
const msg: queue.QueueMessage = res;
console.log('msg: ' + JSON.stringify(msg));
} else {
console.log('no messages available');
}
}
});
mongoQueue.ping('testAck', (err, res) => {
if (err) {
console.log('error: ' + err);
} else {
const msgId: string = res;
console.log('msgId: ' + msgId);
}
});
mongoQueue.ack('testAck', (err, res) => {
if (err) {
console.log('error: ' + err);
} else {
const msgId: string = res;
console.log('msgId: ' + msgId);
}
});
mongoQueue.total((err, res) => {
if (err) {
console.log('error: ' + err);
} else {
const total: number = res;
console.log('total: ' + total);
}
});
mongoQueue.size((err, res) => {
if (err) {
console.log('error: ' + err);
} else {
const size: number = res;
console.log('size: ' + size);
}
});
mongoQueue.inflight((err, res) => {
if (err) {
console.log('error: ' + err);
} else {
const inflight: number = res;
console.log('inflight: ' + inflight);
}
});
mongoQueue.done((err, res) => {
if (err) {
console.log('error: ' + err);
} else {
const done: number = res;
console.log('done: ' + done);
}
});
});
+23
View File
@@ -0,0 +1,23 @@
{
"compilerOptions": {
"module": "commonjs",
"lib": [
"es6"
],
"noImplicitAny": true,
"noImplicitThis": true,
"strictFunctionTypes": true,
"strictNullChecks": true,
"baseUrl": "../",
"typeRoots": [
"../"
],
"types": [],
"noEmit": true,
"forceConsistentCasingInFileNames": true
},
"files": [
"index.d.ts",
"mongodb-queue-tests.ts"
]
}
+1
View File
@@ -0,0 +1 @@
{ "extends": "dtslint/dt.json" }