-
Notifications
You must be signed in to change notification settings - Fork 6
/
WriteMongoRecord.ts
74 lines (70 loc) · 1.91 KB
/
WriteMongoRecord.ts
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
import * as noflo from 'noflo'
import{ MongoClient } from 'mongodb'
import { ProcessInput, ProcessOutput } from '../libs/noflo-types'
const getComponent = () => {
const c = new noflo.Component()
c.description = ''
c.icon = 'handshake-o'
c.inPorts.add('mongo_uri', {
datatype: 'string',
description: 'The mongodb uri to connect to',
required: true
})
c.inPorts.add('database_name', {
datatype: 'string',
description: 'The name of the database',
required: true
})
c.inPorts.add('collection_name', {
datatype: 'string',
description: 'The name of the collection',
required: true
})
c.inPorts.add('record', {
datatype: 'object',
description: 'The object to insert into the collection',
required: true
})
c.outPorts.add('error', {
datatype: 'all'
})
let mongoUri: string, databaseName: string, collectionName: string
c.process((input: ProcessInput, output: ProcessOutput) => {
console.log('calling WriteMongoRecord')
if (input.hasData('mongo_uri', 'database_name', 'collection_name')) {
databaseName = input.getData('database_name')
mongoUri = input.getData('mongo_uri')
collectionName = input.getData('collection_name')
}
// Check preconditions on input data
if (!input.hasData('record') || !(databaseName && mongoUri && collectionName)) {
return
}
// Read packets we need to process
const record = input.getData('record')
MongoClient.connect(mongoUri, function (err, db) {
if (err) {
output.send({
error: err
})
// Deactivate
output.done()
return
}
db.db(databaseName).collection(collectionName).insertOne(record, function (err, res) {
if (err) {
output.send({
error: err
})
}
db.close()
// Deactivate
output.done()
})
})
})
return c
}
export {
getComponent
}