forked from SheetJS/sheetjs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MongoDBCRUD.js
62 lines (52 loc) · 1.72 KB
/
MongoDBCRUD.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/* xlsx.js (C) 2013-present SheetJS -- http://sheetjs.com */
/* eslint-env node */
/* global Promise */
const XLSX = require('xlsx');
const SheetJSMongo = require("./SheetJSMongo");
const MongoClient = require('mongodb').MongoClient;
const url = 'mongodb://localhost:27017/sheetjs';
const db_name = 'sheetjs';
let P = Promise.resolve("sheetjs");
/* Connect to mongodb server */
P = P.then(async () => {
const client = await MongoClient.connect(url);
return [client];
});
/* Sample data table */
P = P.then(async ([client]) => {
const db = client.db(db_name);
try { await db.collection('pres').drop(); } catch(e) {}
const pres = db.collection('pres');
await pres.insertMany([
{ name: "Barack Obama", idx: 44 },
{ name: "Donald Trump", idx: 45 }
], {ordered: true});
try { await db.collection('fmts').drop(); } catch(e) {}
const fmts = db.collection('fmts');
await fmts.insertMany([
{ ext: 'XLSB', ctr: 'ZIP', multi: 1 },
{ ext: 'XLS', ctr: 'CFB', multi: 1 },
{ ext: 'XLML', multi: 1 },
{ ext: 'CSV', ctr: 'ZIP', multi: 0 },
], {ordered: true});
return [client, pres, fmts];
});
/* Export database to XLSX */
P = P.then(async ([client, pres, fmts]) => {
const wb = XLSX.utils.book_new();
await SheetJSMongo.book_append_mongo(wb, pres, "pres");
await SheetJSMongo.book_append_mongo(wb, fmts, "fmts");
XLSX.writeFile(wb, "mongocrud.xlsx");
return [client, pres, fmts];
});
/* Read the new file and dump all of the data */
P = P.then(() => {
const wb = XLSX.readFile('mongocrud.xlsx');
wb.SheetNames.forEach((n,i) => {
console.log(`Sheet #${i+1}: ${n}`);
const ws = wb.Sheets[n];
console.log(XLSX.utils.sheet_to_csv(ws));
});
});
/* Close connection */
P.then(async ([client]) => { client.close(); });