fix(mongodb): modernize MongoDB dump handling and filesystem integration
This commit is contained in:
+44
-41
@@ -1,49 +1,52 @@
|
||||
import * as smartdata from '@push.rocks/smartdata';
|
||||
import * as mongodb from 'mongodb';
|
||||
import type * as tsclass from '@tsclass/tsclass';
|
||||
|
||||
let db: smartdata.SmartdataDb;
|
||||
let mongoClient: mongodb.MongoClient | undefined;
|
||||
|
||||
export const stop = async () => {
|
||||
await db.close();
|
||||
}
|
||||
await mongoClient?.close();
|
||||
mongoClient = undefined;
|
||||
};
|
||||
|
||||
export const generateTestData = async (mongoDescriptorArg: smartdata.IMongoDescriptor) => {
|
||||
db = new smartdata.SmartdataDb(mongoDescriptorArg);
|
||||
await db.init();
|
||||
let counter = 0;
|
||||
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');
|
||||
|
||||
@smartdata.Collection(db)
|
||||
class House extends smartdata.SmartDataDbDoc<House, House> {
|
||||
@smartdata.unI()
|
||||
id = `hello-${counter}`;
|
||||
const houseDocs: mongodb.OptionalUnlessRequiredId<mongodb.Document>[] = [];
|
||||
const truckDocs: mongodb.OptionalUnlessRequiredId<mongodb.Document>[] = [];
|
||||
|
||||
@smartdata.svDb()
|
||||
data = {
|
||||
'some' : {
|
||||
'complex': 'structure',
|
||||
more: 4
|
||||
}
|
||||
}
|
||||
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,
|
||||
},
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
@smartdata.Collection(db)
|
||||
class Truck extends smartdata.SmartDataDbDoc<Truck, Truck> {
|
||||
@smartdata.unI()
|
||||
id = `hello-${counter}`;
|
||||
|
||||
@smartdata.svDb()
|
||||
data = {
|
||||
'some' : {
|
||||
'complex': 'structure',
|
||||
more: 2
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
while (counter < 100) {
|
||||
const house = new House();
|
||||
await house.save();
|
||||
const truck = new Truck();
|
||||
await truck.save();
|
||||
counter++;
|
||||
}
|
||||
}
|
||||
await houseCollection.insertMany(houseDocs);
|
||||
await truckCollection.insertMany(truckDocs);
|
||||
};
|
||||
|
||||
+4
-4
@@ -17,8 +17,8 @@ tap.test('should create a mongodump instance', async () => {
|
||||
});
|
||||
|
||||
tap.test('should deploy sample data', async () => {
|
||||
await sampledata.generateTestData(await testSmartMongo.getMongoDescriptor())
|
||||
})
|
||||
await sampledata.generateTestData(await testSmartMongo.getMongoDescriptor());
|
||||
});
|
||||
|
||||
tap.test('should add a mongotarget to mongodump instance', async () => {
|
||||
const target = await testMongodump.addMongoTargetByMongoDescriptor(await testSmartMongo.getMongoDescriptor());
|
||||
@@ -27,8 +27,8 @@ tap.test('should add a mongotarget to mongodump instance', async () => {
|
||||
|
||||
tap.test('should dump a collection to a directory', async () => {
|
||||
const target = await testMongodump.addMongoTargetByMongoDescriptor(await testSmartMongo.getMongoDescriptor());
|
||||
await target.dumpAllCollectionsToDir('.nogit', docArg => docArg.id, true);
|
||||
})
|
||||
await target.dumpAllCollectionsToDir('.nogit', (docArg) => String(docArg.id), true);
|
||||
});
|
||||
|
||||
tap.test('should stop the smartmongo instance', async () => {
|
||||
await sampledata.stop();
|
||||
|
||||
Reference in New Issue
Block a user