-
Notifications
You must be signed in to change notification settings - Fork 477
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[WIP]add export Invoices and Contacts to CSV #143
Closed
Closed
Changes from 8 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
6e62eb8
Merge branch 'dev' into dev
hql287 0059955
Merge branch 'dev' into dev
hql287 41e88ae
refactoring
AlphaStyle 64f8044
Merge branch 'dev' into dev
hql287 ec93298
minor updates
AlphaStyle d119dee
simplefy getData()
AlphaStyle 3e3b6ec
Prettify and lint
AlphaStyle 7b05f59
Merge branch 'dev' into dev
hql287 a6f5c01
remove unnecessary helper functions and use getAllDocs instead
AlphaStyle 130ba73
Merge branch 'dev' of github.com:AlphaStyle/Manta into dev
AlphaStyle 6432cdd
forgot to remove old helper imports
AlphaStyle 4952dcf
dirty json to csv converter script (before refactoring)
AlphaStyle 43d3616
Merge branch 'dev' into dev
hql287 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
// Node Libs | ||
const fs = require('fs'); | ||
const path = require('path'); | ||
|
||
// Electron Libs | ||
const { dialog } = require('electron').remote; | ||
|
||
const openDialog = require('../renderers/dialog'); // Dialog on errors, warnings, info, etc... | ||
const { pouchDBInvoices, pouchDBContacts } = require('./pouchDB'); // PouchDB helpers | ||
const csvjson = require('csvjson'); // CSV to JSON and JSON to CSV | ||
|
||
// Export PouchDB | ||
const exportDB = () => { | ||
// Save PouchDB as CSV | ||
const saveFile = () => { | ||
dialog.showSaveDialog( | ||
{ filters: [{ name: 'CSV', extensions: ['csv'] }] }, | ||
savePath => { | ||
if (path) { | ||
// Get Directory from path | ||
const dir = path.parse(savePath).dir; | ||
// Check whether you have write permission for directory | ||
fs.access(dir, fs.constants.W_OK, err => { | ||
if (err) { | ||
openDialog({ | ||
type: 'warning', | ||
title: 'No Write Permission', | ||
message: `No write permission to ${dir}, please choose a different directory!`, | ||
}); | ||
} else { | ||
writeFile(savePath); | ||
} | ||
}); | ||
} | ||
} | ||
); | ||
}; | ||
|
||
async function writeFile(savePath) { | ||
const file = fs.createWriteStream(savePath); | ||
const data = await getData(); // PouchDB JSON Data | ||
// console.log(data) // TO see how the PouchDB JSON looks like | ||
|
||
if (data) { | ||
// csvjson options | ||
const options = { | ||
delimiter: ',', | ||
wrap: false, | ||
headers: 'full', | ||
objectDenote: '.', | ||
arrayDenote: '[]', | ||
}; | ||
|
||
file.write(csvjson.toCSV(data, options)); | ||
file.close(); // Close when done | ||
} | ||
} | ||
|
||
// Get PouchDB Invoices | ||
async function getData() { | ||
let Invoices; | ||
let Contacts; | ||
|
||
try { | ||
await pouchDBInvoices().then(invoices => (Invoices = invoices)); | ||
await pouchDBContacts().then(contacts => (Contacts = contacts)); | ||
} catch (err) { | ||
openDialog({ | ||
type: 'error', | ||
title: 'PouchDB Error', | ||
message: 'Exporting PouchDB to CSV failed!', | ||
}); | ||
} | ||
|
||
if ((Invoices, Contacts)) { | ||
return { Invoices, Contacts }; | ||
} | ||
} | ||
|
||
saveFile(); | ||
}; | ||
|
||
// Import PouchDB | ||
const importDB = () => { | ||
const openFile = () => { | ||
dialog.showOpenDialog( | ||
{ | ||
filters: [{ name: 'CSV', extensions: ['csv'] }], | ||
properties: ['openFile'], | ||
}, | ||
openPath => { | ||
if (openPath) { | ||
fs.access(openPath[0], fs.constants.R_OK, err => { | ||
if (err) { | ||
openDialog({ | ||
type: 'warning', | ||
title: 'No Read Permission', | ||
message: `No read permission on ${ | ||
openPath[0] | ||
}, please make sure you have read permission!`, | ||
}); | ||
} else { | ||
// DBLoad() | ||
} | ||
}); | ||
} | ||
} | ||
); | ||
}; | ||
|
||
// const DBLoad = () => { | ||
// // Read the CSV file | ||
// const data = fs.readFileSync(path, 'utf8'); | ||
|
||
// if (data) { | ||
// const options = { | ||
// delimiter: ',', | ||
// quote: '"', | ||
// }; | ||
|
||
// // TODO: This does not output the correct JSON as of now! | ||
// const j = csvjson.toSchemaObject(data); | ||
// console.log(j); | ||
// } | ||
// } | ||
// | ||
// openFile(); | ||
}; | ||
|
||
module.exports = { | ||
exportDB, | ||
importDB, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,16 @@ const PouchDB = require('pouchdb-browser'); | |
const contactsDB = new PouchDB('contacts'); | ||
const invoicesDB = new PouchDB('invoices'); | ||
|
||
// Used for Export and Import PouchDB | ||
const MemoryStream = require('memorystream'); | ||
const replicationStream = require('pouchdb-replication-stream'); | ||
|
||
// CSV to JSON and JSON to CSV | ||
const csvjson = require('csvjson'); | ||
|
||
PouchDB.plugin(replicationStream.plugin); | ||
PouchDB.adapter('writableStream', replicationStream.adapters.writableStream); | ||
|
||
// Utility | ||
import { omit } from 'lodash'; | ||
|
||
|
@@ -79,6 +89,67 @@ runMigration( | |
} | ||
); | ||
|
||
// Get PouchDB Invoices (Used for Exporting) | ||
const pouchDBInvoices = () => { | ||
const invoices = new Promise((resolve, reject) => { | ||
const invoicesStream = new MemoryStream(); | ||
let invoicesStreamObject; | ||
|
||
invoicesStream.on('data', chunk => { | ||
const json = JSON.parse(chunk.toString()); | ||
if (json.docs) { | ||
invoicesStreamObject = json; | ||
} | ||
}); | ||
|
||
invoicesStream.on('end', () => { | ||
resolve(invoicesStreamObject); | ||
}); | ||
|
||
// Dump invoices to memoryStream | ||
invoicesDB.dump(invoicesStream).then(res => { | ||
if (res.ok !== true) { | ||
reject(new Error('Exporting PouchDB to CSV failed!')); | ||
} | ||
}); | ||
}); | ||
|
||
return invoices; | ||
}; | ||
|
||
// Get PouchDB Contacts (Used for Exporting) | ||
const pouchDBContacts = () => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as above, why don't we use |
||
const contacts = new Promise((resolve, reject) => { | ||
const contactsStream = new MemoryStream(); | ||
let contactsStreamObject; | ||
|
||
contactsStream.on('data', chunk => { | ||
const json = JSON.parse(chunk.toString()); | ||
if (json.docs) { | ||
contactsStreamObject = json; | ||
} | ||
}); | ||
|
||
contactsStream.on('end', () => { | ||
resolve(contactsStreamObject); | ||
}); | ||
|
||
// Dump contacts to memoryStream | ||
contactsDB.dump(contactsStream).then(res => { | ||
if (res.ok !== true) { | ||
reject(new Error('Importing PouchDB from CSV failed!')); | ||
} | ||
}); | ||
}); | ||
|
||
return contacts; | ||
}; | ||
|
||
// Import PouchDB from CSV file | ||
const DBLoad = load => { | ||
// TODO: load invoices and contacts to PouchDB | ||
}; | ||
|
||
// Set DB via dbName | ||
const setDB = dbName => | ||
new Promise((resolve, reject) => { | ||
|
@@ -157,4 +228,11 @@ const updateDoc = (dbName, docId, updatedDoc) => | |
.catch(err => reject(err)); | ||
}); | ||
|
||
export { getAllDocs, deleteDoc, saveDoc, updateDoc }; | ||
export { | ||
getAllDocs, | ||
deleteDoc, | ||
saveDoc, | ||
updateDoc, | ||
pouchDBInvoices, | ||
pouchDBContacts, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why don't we use
getAllDocs
instead? 🧐There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like you need this to return different kind of doc than
getAllDocs
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Once again I didn't do enough testing 😞 I tried to use it in the beginning and saw the end result being different, so I just assumed it would be wrong to use. But now that I can compare
getAllDocs('invoices')
andpouchDBInvoices
I see the only difference is that I have to wrapgetAllDocs
with an array and that it is missing the_revisions
object wich is not important.I will use
getAllDocs
instead 🙂 And remove the extrapouchDBInvoices
andpouchDBContacts
helpers which will reduce dependencies!Thank you!