Files
mongodump/test/sampledata.ts
T

53 lines
1.7 KiB
TypeScript
Raw Normal View History

import * as mongodb from 'mongodb';
import type * as tsclass from '@tsclass/tsclass';
2022-06-05 21:04:16 +02:00
let mongoClient: mongodb.MongoClient | undefined;
2022-06-05 21:04:16 +02:00
export const stop = async () => {
await mongoClient?.close();
mongoClient = undefined;
};
export const generateTestData = async (mongoDescriptorArg: tsclass.database.IMongoDescriptor) => {
const finalConnectionUrl = mongoDescriptorArg.mongoDbUrl
.replace('<USERNAME>', mongoDescriptorArg.mongoDbUser ?? '')
.replace('<username>', mongoDescriptorArg.mongoDbUser ?? '')
.replace('<USER>', mongoDescriptorArg.mongoDbUser ?? '')
.replace('<user>', mongoDescriptorArg.mongoDbUser ?? '')
.replace('<PASSWORD>', mongoDescriptorArg.mongoDbPass ?? '')
.replace('<password>', mongoDescriptorArg.mongoDbPass ?? '')
.replace('<DBNAME>', mongoDescriptorArg.mongoDbName ?? '')
.replace('<dbname>', mongoDescriptorArg.mongoDbName ?? '');
mongoClient = await mongodb.MongoClient.connect(finalConnectionUrl);
const db = mongoClient.db(mongoDescriptorArg.mongoDbName);
const houseCollection = db.collection('House');
const truckCollection = db.collection('Truck');
const houseDocs: mongodb.OptionalUnlessRequiredId<mongodb.Document>[] = [];
const truckDocs: mongodb.OptionalUnlessRequiredId<mongodb.Document>[] = [];
for (let counter = 0; counter < 100; counter++) {
houseDocs.push({
id: `hello-${counter}`,
data: {
some: {
complex: 'structure',
more: 4,
},
},
});
truckDocs.push({
id: `hello-${counter}`,
data: {
some: {
complex: 'structure',
more: 2,
},
},
});
2022-06-05 21:04:16 +02:00
}
await houseCollection.insertMany(houseDocs);
await truckCollection.insertMany(truckDocs);
};