From 32d3ea4d65ebebe52c40e23cfbdbaf6a558d54c0 Mon Sep 17 00:00:00 2001 From: Philipp Kunz Date: Mon, 6 Jun 2022 13:04:30 +0200 Subject: [PATCH] fix(core): update --- package-lock.json | 6 +++--- package.json | 2 ++ test/sampledata.ts | 15 +++++++++++++++ test/test.ts | 8 ++++++++ ts/00_commitinfo_data.ts | 2 +- ts/index.ts | 5 +++-- ...s => mongodump.classes.mongocompresseddump.ts} | 4 ++-- ...ongodump.ts => mongodump.classes.mongodump.ts} | 2 +- ...et.ts => mongodump.classes.mongodumptarget.ts} | 12 ++++++++++-- ts/mongodump.plugins.ts | 11 +++++++++++ 10 files changed, 56 insertions(+), 11 deletions(-) rename ts/{mongocompresseddump.ts => mongodump.classes.mongocompresseddump.ts} (97%) rename ts/{mongodb.classes.mongodump.ts => mongodump.classes.mongodump.ts} (89%) rename ts/{mongodb.classes.mongodumptarget.ts => mongodump.classes.mongodumptarget.ts} (80%) diff --git a/package-lock.json b/package-lock.json index 94d5043..f2fe623 100644 --- a/package-lock.json +++ b/package-lock.json @@ -11,6 +11,8 @@ "dependencies": { "@pushrocks/lik": "^6.0.0", "@pushrocks/smartfile": "^9.0.6", + "@pushrocks/smartjson": "^4.0.6", + "@pushrocks/smartpath": "^5.0.5", "@pushrocks/smartpromise": "^3.1.7", "@tsclass/tsclass": "^4.0.3", "mongodb": "^4.6.0" @@ -686,7 +688,6 @@ "version": "5.0.5", "resolved": "https://verdaccio.lossless.one/@pushrocks%2fsmartpath/-/smartpath-5.0.5.tgz", "integrity": "sha512-t2lXXGMpKnPlwubIcYGD6cGi2CUJxJ3t2yftVt8tHfjX68jELA5sJhFMtyD3AeFZVxePFZOCiHwWEbWkSDNnmw==", - "dev": true, "license": "MIT" }, "node_modules/@pushrocks/smartpdf": { @@ -6944,8 +6945,7 @@ "@pushrocks/smartpath": { "version": "5.0.5", "resolved": "https://verdaccio.lossless.one/@pushrocks%2fsmartpath/-/smartpath-5.0.5.tgz", - "integrity": "sha512-t2lXXGMpKnPlwubIcYGD6cGi2CUJxJ3t2yftVt8tHfjX68jELA5sJhFMtyD3AeFZVxePFZOCiHwWEbWkSDNnmw==", - "dev": true + "integrity": "sha512-t2lXXGMpKnPlwubIcYGD6cGi2CUJxJ3t2yftVt8tHfjX68jELA5sJhFMtyD3AeFZVxePFZOCiHwWEbWkSDNnmw==" }, "@pushrocks/smartpdf": { "version": "3.0.2", diff --git a/package.json b/package.json index 70df943..777c467 100644 --- a/package.json +++ b/package.json @@ -39,6 +39,8 @@ "dependencies": { "@pushrocks/lik": "^6.0.0", "@pushrocks/smartfile": "^9.0.6", + "@pushrocks/smartjson": "^4.0.6", + "@pushrocks/smartpath": "^5.0.5", "@pushrocks/smartpromise": "^3.1.7", "@tsclass/tsclass": "^4.0.3", "mongodb": "^4.6.0" diff --git a/test/sampledata.ts b/test/sampledata.ts index 50c28ee..a592209 100644 --- a/test/sampledata.ts +++ b/test/sampledata.ts @@ -24,9 +24,24 @@ export const generateTestData = async (mongoDescriptorArg: smartdata.IMongoDescr } } + @smartdata.Collection(db) + class Truck extends smartdata.SmartDataDbDoc { + @smartdata.unI() + id = `hello-${counter}`; + + @smartdata.svDb() + data = { + 'some' : { + 'complex': 'structure' + } + } + } + while (counter < 100) { const house = new House(); await house.save(); + const truck = new Truck(); + await truck.save(); counter++; } } diff --git a/test/test.ts b/test/test.ts index 6a99dc7..786dbf6 100644 --- a/test/test.ts +++ b/test/test.ts @@ -23,6 +23,14 @@ tap.test('should deploy sample data', async () => { tap.test('should add a mongotarget to mongodump instance', async () => { const target = await testMongodump.addMongoTargetByMongoDescriptor(await testSmartMongo.getMongoDescriptor()); await target.getCollections(); +}); + +tap.test('should dump a collection to a directory', async () => { + const target = await testMongodump.addMongoTargetByMongoDescriptor(await testSmartMongo.getMongoDescriptor()); + const collections = await target.getCollections(); + for (const collection of collections) { + await target.dumpCollectionToDir(collection, '.nogit', doc => doc.id); + } }) tap.test('should stop the smartmongo instance', async () => { diff --git a/ts/00_commitinfo_data.ts b/ts/00_commitinfo_data.ts index 92d5eec..7bb6e9c 100644 --- a/ts/00_commitinfo_data.ts +++ b/ts/00_commitinfo_data.ts @@ -3,6 +3,6 @@ */ export const commitinfo = { name: '@pushrocks/mongodump', - version: '1.0.2', + version: '1.0.3', description: 'a tool to handle dumps of mongodb databases' } diff --git a/ts/index.ts b/ts/index.ts index d228119..60fe2e1 100644 --- a/ts/index.ts +++ b/ts/index.ts @@ -1,2 +1,3 @@ -export * from './mongodb.classes.mongodump.js'; -export * from './mongodb.classes.mongodumptarget.js'; +export * from './mongodump.classes.mongodump.js'; +export * from './mongodump.classes.mongodumptarget.js'; + diff --git a/ts/mongocompresseddump.ts b/ts/mongodump.classes.mongocompresseddump.ts similarity index 97% rename from ts/mongocompresseddump.ts rename to ts/mongodump.classes.mongocompresseddump.ts index 1501e8d..4a4cb5e 100644 --- a/ts/mongocompresseddump.ts +++ b/ts/mongodump.classes.mongocompresseddump.ts @@ -6,5 +6,5 @@ import * as plugins from './mongodump.plugins.js'; * */ export class MongoCompressedDump { - -} \ No newline at end of file + +} diff --git a/ts/mongodb.classes.mongodump.ts b/ts/mongodump.classes.mongodump.ts similarity index 89% rename from ts/mongodb.classes.mongodump.ts rename to ts/mongodump.classes.mongodump.ts index 0466bf2..a5e7c6a 100644 --- a/ts/mongodb.classes.mongodump.ts +++ b/ts/mongodump.classes.mongodump.ts @@ -1,5 +1,5 @@ import * as plugins from './mongodump.plugins.js'; -import { MongoDumpTarget } from './mongodb.classes.mongodumptarget.js'; +import { MongoDumpTarget } from './mongodump.classes.mongodumptarget.js'; export class MongoDump { public mongoTargetObjectMap = new plugins.lik.ObjectMap(); diff --git a/ts/mongodb.classes.mongodumptarget.ts b/ts/mongodump.classes.mongodumptarget.ts similarity index 80% rename from ts/mongodb.classes.mongodumptarget.ts rename to ts/mongodump.classes.mongodumptarget.ts index 64f8333..9029eda 100644 --- a/ts/mongodb.classes.mongodumptarget.ts +++ b/ts/mongodump.classes.mongodumptarget.ts @@ -61,8 +61,16 @@ export class MongoDumpTarget { /** * dumps a collection to a directory */ - public async dumpCollectionToDir(collectionArg: plugins.mongodb.Collection, dirArg: string) { - + public async dumpCollectionToDir(collectionArg: plugins.mongodb.Collection, dirArg: string, nameTransformFunction = (doc: any) => doc._id) { + const dirPath = plugins.smartpath.transform.makeAbsolute(dirArg); + const collectionDir = plugins.path.join(dirPath, collectionArg.collectionName); + await plugins.smartfile.fs.ensureDir(collectionDir); + const cursor = collectionArg.find(); + let value = await cursor.next(); + while(value) { + await plugins.smartfile.memory.toFs(JSON.stringify(value, null, 2), plugins.path.join(collectionDir, `${nameTransformFunction(value)}.json`)); + value = await cursor.next(); + } } public async dumpCollectionToTarArchive(collectionArg: plugins.mongodb.Collection) {} diff --git a/ts/mongodump.plugins.ts b/ts/mongodump.plugins.ts index 306f86a..a1144b9 100644 --- a/ts/mongodump.plugins.ts +++ b/ts/mongodump.plugins.ts @@ -1,11 +1,22 @@ +// node native +import * as path from 'path'; + +export { + path +} + // pushrocks scope import * as lik from '@pushrocks/lik'; import * as smartfile from '@pushrocks/smartfile'; +import * as smartjson from '@pushrocks/smartjson'; +import * as smartpath from '@pushrocks/smartpath'; import * as smartpromise from '@pushrocks/smartpromise'; export { lik, smartfile, + smartjson, + smartpath, smartpromise }