-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathsetup.js
68 lines (50 loc) · 1.48 KB
/
setup.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import arg from 'arg';
import { MongoClient } from 'mongodb';
import mongoose from 'mongoose';
import Papr from '../esm/index.js';
const help = `
USAGE
pnpm benchmark --option arg
OPTIONS
--help | -h Shows this help message
--db | -d Name of the database used for benchmarking
--url | -u Mongo instance used for benchmarking
`;
const args = arg({
'--db': String,
'--help': Boolean,
'--url': String,
'-d': '--db',
'-h': '--help',
'-u': '--url',
});
if (args['--help']) {
console.log(help);
process.exit(0);
}
const DATABASE = args['--db'] && args['--db'] ? args['--db'] : `benchmark-${Date.now()}`;
const URL = args['--url'] && args['--url'].length > 0 ? args['--url'] : 'mongodb://localhost:27017';
export const COLLECTIONS = ['mongodbtests', 'mongoosetests', 'paprtests'];
export const papr = new Papr();
export let db;
export default async function setup() {
const connection = await MongoClient.connect(URL, {
directConnection: true,
});
db = connection.db(DATABASE);
for (const collectionName of COLLECTIONS) {
const collection = await db.collection(collectionName);
await collection.deleteMany({});
await collection.createIndex({ firstName: 1, lastName: 1 });
await collection.createIndex({ source: 1 });
}
papr.initialize(db);
papr.updateSchemas();
await mongoose.connect(`${URL}/${DATABASE}`, {
directConnection: true,
});
}
export async function teardown() {
await db.dropDatabase();
process.exit(0);
}