mirror of
https://github.com/gosticks/DefinitelyTyped.git
synced 2026-12-04 04:01:52 +00:00
Reformat test file to saitisfy tslint
This commit is contained in:
@@ -1,11 +1,11 @@
|
||||
// Test source : https://github.com/mongodb/node-mongodb-native
|
||||
import mongodb = require('mongodb');
|
||||
var MongoClient = mongodb.MongoClient;
|
||||
|
||||
import { format } from 'util';
|
||||
import * as mongodb from 'mongodb';
|
||||
|
||||
const connectionString = 'mongodb://127.0.0.1:27017/test';
|
||||
|
||||
var format = require('util').format;
|
||||
let options: mongodb.MongoClientOptions = {
|
||||
const options: mongodb.MongoClientOptions = {
|
||||
authSource: ' ',
|
||||
loggerLevel: 'debug',
|
||||
w: 1,
|
||||
@@ -24,7 +24,7 @@ let options: mongodb.MongoClientOptions = {
|
||||
reconnectInterval: 123456,
|
||||
ssl: true,
|
||||
sslValidate: false,
|
||||
checkServerIdentity: function () { },
|
||||
checkServerIdentity: () => {},
|
||||
sslCA: ['str'],
|
||||
sslCRL: ['str'],
|
||||
sslCert: new Buffer(999),
|
||||
@@ -34,58 +34,58 @@ let options: mongodb.MongoClientOptions = {
|
||||
useNewUrlParser: false,
|
||||
authMechanism: 'SCRAM-SHA-1',
|
||||
forceServerObjectId: false
|
||||
}
|
||||
MongoClient.connect(connectionString, options, function (err: mongodb.MongoError, client: mongodb.MongoClient) {
|
||||
};
|
||||
|
||||
mongodb.MongoClient.connect(connectionString, options, (err: mongodb.MongoError, client: mongodb.MongoClient) => {
|
||||
if (err) throw err;
|
||||
const db = client.db('test');
|
||||
|
||||
var collection = db.collection('test_insert');
|
||||
collection.insertOne({ a: 2 }, function (err: mongodb.MongoError, docs: any) {
|
||||
|
||||
const collection = db.collection('test_insert');
|
||||
collection.insertOne({ a: 2 }, (err: mongodb.MongoError, docs: any) => {
|
||||
// Intentionally omitted type annotation from 'count'.
|
||||
// This way it requires a more accurate typedef which allows inferring that it's a number.
|
||||
collection.countDocuments(function (err: mongodb.MongoError, count) {
|
||||
collection.countDocuments((err: mongodb.MongoError, count) => {
|
||||
console.log(format("count = %s", count));
|
||||
});
|
||||
|
||||
collection.countDocuments().then(function (count: number) {
|
||||
collection.countDocuments().then((count: number) => {
|
||||
console.log(format("count = %s", count));
|
||||
});
|
||||
|
||||
collection.countDocuments({ foo: 1 }, function (err: mongodb.MongoError, count: number) {
|
||||
collection.countDocuments({ foo: 1 }, (err: mongodb.MongoError, count: number) => {
|
||||
console.log(format("count = %s", count));
|
||||
});
|
||||
|
||||
collection.countDocuments({ foo: 1 }).then(function (count: number) {
|
||||
collection.countDocuments({ foo: 1 }).then((count: number) => {
|
||||
console.log(format("count = %s", count));
|
||||
});
|
||||
|
||||
collection.countDocuments({ foo: 1 }, { limit: 10 }, function (err: mongodb.MongoError, count: number) {
|
||||
collection.countDocuments({ foo: 1 }, { limit: 10 }, (err: mongodb.MongoError, count: number) => {
|
||||
console.log(format("count = %s", count));
|
||||
});
|
||||
|
||||
collection.countDocuments({ foo: 1 }, { limit: 10 }).then(function (count: number) {
|
||||
collection.countDocuments({ foo: 1 }, { limit: 10 }).then((count: number) => {
|
||||
console.log(format("count = %s", count));
|
||||
});
|
||||
|
||||
// Locate all the entries using find
|
||||
collection.find({}).toArray(function (err: mongodb.MongoError, results: any) {
|
||||
collection.find({}).toArray((err: mongodb.MongoError, results: any) => {
|
||||
console.dir(results);
|
||||
// Let's close the db
|
||||
client.close();
|
||||
});
|
||||
|
||||
// Get some statistics
|
||||
collection.stats(function (err: mongodb.MongoError, stats: any) {
|
||||
collection.stats((err: mongodb.MongoError, stats: any) => {
|
||||
console.log(stats.count + " documents");
|
||||
});
|
||||
|
||||
//
|
||||
collection.stats().then(function (stats) {
|
||||
collection.stats().then((stats) => {
|
||||
console.log(stats.wiredTiger.cache['bytes currently in the cache']);
|
||||
})
|
||||
});
|
||||
|
||||
collection.createIndex({}, { partialFilterExpression: { rating: { $exists: 1 } } })
|
||||
collection.createIndex({}, { partialFilterExpression: { rating: { $exists: 1 } } });
|
||||
});
|
||||
|
||||
{
|
||||
@@ -98,7 +98,7 @@ MongoClient.connect(connectionString, options, function (err: mongodb.MongoError
|
||||
cursor = cursor.filter(1);
|
||||
cursor = cursor.hint({});
|
||||
cursor = cursor.limit(1);
|
||||
cursor = cursor.map(function (result) { });
|
||||
cursor = cursor.map((result) => {});
|
||||
cursor = cursor.max(1);
|
||||
cursor = cursor.min(1);
|
||||
cursor = cursor.maxAwaitTimeMS(1);
|
||||
@@ -116,54 +116,62 @@ MongoClient.connect(connectionString, options, function (err: mongodb.MongoError
|
||||
}
|
||||
// Collection .findM<T>() & .agggregate<T>() generic tests
|
||||
{
|
||||
let bag: { cost: number, color: string };
|
||||
type bag = typeof bag;
|
||||
let cursor: mongodb.Cursor<bag> = collection.find<bag>({ color: 'black' })
|
||||
cursor.toArray(function (err, r) { r[0].cost });
|
||||
cursor.forEach(function (bag) { bag.color }, () => { });
|
||||
collection.findOne({ color: 'white' }).then(b => { let _b: bag = b; })
|
||||
collection.findOne<bag>({ color: 'white' }).then(b => { b.cost; })
|
||||
interface Bag {
|
||||
cost: number;
|
||||
color: string;
|
||||
}
|
||||
const cursor: mongodb.Cursor<Bag> = collection.find<Bag>({ color: 'black' });
|
||||
cursor.toArray((err, r) => { r[0].cost; });
|
||||
cursor.forEach((bag) => { bag.color; }, () => {});
|
||||
collection.findOne({ color: 'white' }).then(b => { const _b: Bag = b; });
|
||||
collection.findOne<Bag>({ color: 'white' }).then(b => { b.cost; });
|
||||
}
|
||||
{
|
||||
let payment: { total: number };
|
||||
type payment = typeof payment;
|
||||
let cursor: mongodb.AggregationCursor<payment> = collection.aggregate<payment>([{}])
|
||||
interface Payment {
|
||||
total: number;
|
||||
}
|
||||
const cursor: mongodb.AggregationCursor<Payment> = collection.aggregate<Payment>([{}]);
|
||||
|
||||
collection.aggregate([{ $match: { bar: 1 } }, { $limit: 10 }])
|
||||
collection.aggregate([{ $match: { bar: 1 } }]).limit(10)
|
||||
collection.aggregate([]).match({ bar: 1 }).limit(10)
|
||||
collection.aggregate().match({ bar: 1 }).limit(10)
|
||||
collection.aggregate([{ $match: { bar: 1 } }, { $limit: 10 }]);
|
||||
collection.aggregate([{ $match: { bar: 1 } }]).limit(10);
|
||||
collection.aggregate([]).match({ bar: 1 }).limit(10);
|
||||
collection.aggregate().match({ bar: 1 }).limit(10);
|
||||
|
||||
collection.aggregate<payment>(
|
||||
collection.aggregate<Payment>(
|
||||
[{ $match: { bar: 1 } }],
|
||||
function (err: mongodb.MongoError, cursor: mongodb.AggregationCursor<payment>) {
|
||||
cursor.limit(10)
|
||||
(err: mongodb.MongoError, cursor: mongodb.AggregationCursor<Payment>) => {
|
||||
cursor.limit(10);
|
||||
}
|
||||
)
|
||||
);
|
||||
|
||||
collection.aggregate<payment>(
|
||||
collection.aggregate<Payment>(
|
||||
[],
|
||||
function (err: mongodb.MongoError, cursor: mongodb.AggregationCursor<payment>) {
|
||||
cursor.match({ bar: 1 }).limit(10)
|
||||
(err: mongodb.MongoError, cursor: mongodb.AggregationCursor<Payment>) => {
|
||||
cursor.match({ bar: 1 }).limit(10);
|
||||
}
|
||||
)
|
||||
);
|
||||
|
||||
collection.aggregate<payment>(
|
||||
function (err: mongodb.MongoError, cursor: mongodb.AggregationCursor<payment>) {
|
||||
cursor.match({ bar: 1 }).limit(10)
|
||||
collection.aggregate<Payment>(
|
||||
(err: mongodb.MongoError, cursor: mongodb.AggregationCursor<Payment>) => {
|
||||
cursor.match({ bar: 1 }).limit(10);
|
||||
}
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
// test for new typings
|
||||
{
|
||||
type TestCollection = {
|
||||
interface TestCollection {
|
||||
stringField: string;
|
||||
numberField?: number;
|
||||
};
|
||||
let testCollection = db.collection<TestCollection>('testCollection');
|
||||
testCollection.insertOne({stringField:'hola'})
|
||||
testCollection.insertMany([{stringField:'hola'},{stringField:'hola', numberField: 1}])
|
||||
}
|
||||
const testCollection = db.collection<TestCollection>('testCollection');
|
||||
testCollection.insertOne(
|
||||
{ stringField: 'hola' }
|
||||
);
|
||||
testCollection.insertMany([
|
||||
{ stringField: 'hola' },
|
||||
{ stringField: 'hola', numberField: 1 },
|
||||
]);
|
||||
testCollection.find({
|
||||
numberField: {
|
||||
$and: [{ $gt: 0, $lt: 100 }]
|
||||
@@ -172,15 +180,15 @@ MongoClient.connect(connectionString, options, function (err: mongodb.MongoError
|
||||
|
||||
const res: mongodb.Cursor<TestCollection> = testCollection.find({ _id: 123 });
|
||||
}
|
||||
})
|
||||
});
|
||||
|
||||
let testFunc = async () => {
|
||||
let testClient: mongodb.MongoClient;
|
||||
testClient = await mongodb.connect(connectionString);
|
||||
};
|
||||
async function testFunc(): Promise<mongodb.MongoClient> {
|
||||
const testClient: mongodb.MongoClient = await mongodb.connect(connectionString);
|
||||
return testClient;
|
||||
}
|
||||
|
||||
mongodb.connect(connectionString, (err: mongodb.MongoError, client: mongodb.MongoClient) => { });
|
||||
mongodb.connect(connectionString, options, (err: mongodb.MongoError, client: mongodb.MongoClient) => { });
|
||||
mongodb.connect(connectionString, (err: mongodb.MongoError, client: mongodb.MongoClient) => {});
|
||||
mongodb.connect(connectionString, options, (err: mongodb.MongoError, client: mongodb.MongoClient) => {});
|
||||
|
||||
// https://docs.mongodb.com/manual/core/transactions/
|
||||
|
||||
@@ -193,10 +201,10 @@ async function commitWithRetry(session: mongodb.ClientSession) {
|
||||
error.errorLabels &&
|
||||
error.errorLabels.indexOf('UnknownTransactionCommitResult') < 0
|
||||
) {
|
||||
console.log('UnknownTransactionCommitResult, retrying commit operation ...');
|
||||
console.log('UnknownTransactionCommitResult, retrying commit operation...');
|
||||
await commitWithRetry(session);
|
||||
} else {
|
||||
console.log('Error during commit ...');
|
||||
console.log('Error during commit...');
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
@@ -205,7 +213,8 @@ async function commitWithRetry(session: mongodb.ClientSession) {
|
||||
async function runTransactionWithRetry(
|
||||
txnFunc: (client: mongodb.MongoClient, session: mongodb.ClientSession) => Promise<void>,
|
||||
client: mongodb.MongoClient,
|
||||
session: mongodb.ClientSession) {
|
||||
session: mongodb.ClientSession
|
||||
) {
|
||||
try {
|
||||
await txnFunc(client, session);
|
||||
} catch (error) {
|
||||
@@ -256,31 +265,31 @@ async function transfer(client: mongodb.MongoClient, from: any, to: any, amount:
|
||||
const session = client.startSession();
|
||||
session.startTransaction();
|
||||
try {
|
||||
const opts = { session, returnOriginal: false };
|
||||
const A = await db.collection('Account').
|
||||
const opts = { session, returnOriginal: false };
|
||||
const A = await db.collection('Account').
|
||||
findOneAndUpdate({ name: from }, { $inc: { balance: -amount } }, opts).
|
||||
then(res => res.value);
|
||||
if (A.balance < 0) {
|
||||
// If A would have negative balance, fail and abort the transaction
|
||||
// `session.abortTransaction()` will undo the above `findOneAndUpdate()`
|
||||
throw new Error('Insufficient funds: ' + (A.balance + amount));
|
||||
}
|
||||
if (A.balance < 0) {
|
||||
// If A would have negative balance, fail and abort the transaction
|
||||
// `session.abortTransaction()` will undo the above `findOneAndUpdate()`
|
||||
throw new Error('Insufficient funds: ' + (A.balance + amount));
|
||||
}
|
||||
|
||||
const B = await db.collection('Account').
|
||||
findOneAndUpdate({ name: to }, { $inc: { balance: amount } }, opts).
|
||||
then(res => res.value);
|
||||
const B = await db.collection('Account')
|
||||
.findOneAndUpdate({ name: to }, { $inc: { balance: amount } }, opts)
|
||||
.then(res => res.value);
|
||||
|
||||
await session.commitTransaction();
|
||||
session.endSession();
|
||||
return { from: A, to: B };
|
||||
await session.commitTransaction();
|
||||
session.endSession();
|
||||
return { from: A, to: B };
|
||||
} catch (error) {
|
||||
// If an error occurred, abort the whole transaction and
|
||||
// undo any changes that might have happened
|
||||
await session.abortTransaction();
|
||||
session.endSession();
|
||||
throw error; // Rethrow so calling function sees error
|
||||
// If an error occurred, abort the whole transaction and
|
||||
// undo any changes that might have happened
|
||||
await session.abortTransaction();
|
||||
session.endSession();
|
||||
throw error; // Rethrow so calling function sees error
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
mongodb.connect(connectionString).then((client) => {
|
||||
client.startSession();
|
||||
@@ -288,5 +297,3 @@ mongodb.connect(connectionString).then((client) => {
|
||||
runTransactionWithRetry(updateEmployeeInfo, client, session)
|
||||
);
|
||||
});
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user