53 lines
1.7 KiB
TypeScript
53 lines
1.7 KiB
TypeScript
import * as mongodb from 'mongodb';
|
|
import type * as tsclass from '@tsclass/tsclass';
|
|
|
|
let mongoClient: mongodb.MongoClient | undefined;
|
|
|
|
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,
|
|
},
|
|
},
|
|
});
|
|
}
|
|
|
|
await houseCollection.insertMany(houseDocs);
|
|
await truckCollection.insertMany(truckDocs);
|
|
};
|