-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcsv.js
More file actions
80 lines (64 loc) · 2.73 KB
/
csv.js
File metadata and controls
80 lines (64 loc) · 2.73 KB
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
// There is a bit of magic in the require call for a plugin.
// You can choose from a whitelist of internal system objects.
// If the required file is not in that list, it will be required from disk.
const lrsPlugin = require('./utils/plugins/lrsPlugin.js');
//This is probably not quite what you were expecting. This relates to how we sandbox plugins
const stringify = require(process.cwd() + '/plugins/csvExport/node_modules/csv-stringify')
// When requiring a node_module, if the compiled LRS uses that module, it will be returned from the compiled bundle.
// Otherwise, you'll need to make sure to npm install it alongside the plugin.
const express = require('express');
module.exports = class CSVExporter extends lrsPlugin {
constructor(lrs, dal, settings) {
super(lrs, dal, settings);
// put an export link on the LRS sidebar
this.on('lrsSidebar', (event, lrs) => ({
text: 'CSV Export',
href: this.getLink('/export', 'lrs'), // get a link to this plugin's lrs scoped router
id: this.uuid,
}));
// Set up an Express Router to handle the request
const router = express.Router();
// Get the export link
router.get('/export', async (req, res, next) => {
const connectionPool = require('./utils/connectionPool.js');
const DAL = await connectionPool.dal(this.lrs.uuid, this.lrs.strict, this.lrs.preferRead);
const Mongo = DAL.db;
const statements = Mongo.collection('statements');
const cursor = statements.find({});
const stringifier = stringify({
delimiter: ','
})
stringifier.pipe(res);
res.setHeader('Content-Disposition', 'attachment; filename="export.csv"');
cursor.each((err, item) => {
if (item == null) {
return res.end();
}
stringifier.write([item.statement.actor.id,item.statement.object.id])
});
});
// Associate the router with the plugin at the LRS level
this.setRouter('lrs', router);
}
// Metadata for display
static get display() {
return {
title: 'CSV Exporter',
description: 'A write CSV files to a HTTP stream.',
};
}
// Additional metadata for display
static get metadata() {
return {
author: 'Veracity Technology Consultants',
version: '1.0.0',
moreInfo: 'https://www.veracity.it',
};
}
// No form is necessary, since there are no per instance settings.
static get settingsForm() {
return [
];
}
};
module.exports.pluginName = "csvExport";