-
Notifications
You must be signed in to change notification settings - Fork 2
/
salesforce.js
146 lines (123 loc) · 3.95 KB
/
salesforce.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
var debug = require('debug')('seneca-salesforce-store');
var _ = require('lodash');
var jsforce = require('jsforce');
module.exports = function salesforce (seneca, opts) {
var conn = new jsforce.Connection(opts);
function _connect (cb) {
conn.identity(function (err, id) {
if (err) return conn.login(opts.username, opts.password, cb);
else return cb();
});
};
// salesforce sql doesn't support 'select *' so instead
// we build up the select statement from metadata
function _readTable (table, where, cb) {
conn.sobject(table).describe(function (err, meta) {
if (err) return cb(err);
var wanted = _.filter(meta.fields, function (f) {
if (f.createable === true && f.updateable === true) return f.name;
});
var names = _.pluck(wanted, 'name');
var q = 'SELECT Id, ' + names.join(', ') + ' from ' + table + ' ' + where;
debug('_readTable select statement: ', q);
conn.query(q, cb);
});
};
// load$ returns a full SF object, containing fields which
// are not updateable (SF throws an error if you attempt to persist them).
// This function checks the object fields against the table metadata
// and remove any fields which are not updateable.
function _removeUnUpdateableFields (obj, table, cb) {
conn.sobject(table).describe(function (err, meta) {
if (err) return cb(err);
// Note: 'Id' field is not in the table metadata
var cleaned = {
'Id': obj['Id']
};
_.each(_.keys(obj), function (k) {
var mf = _.findWhere(meta.fields, {name: k, updateable: true});
if (mf) cleaned[k] = obj[k];
});
return cb(null, cleaned);
});
};
// TODO - is there a way of doing this in seneca already?
function _unwrapEnt (ent) {
var obj = {};
_.each(ent.fields$(), function (f) {
obj[f] = ent[f];
});
return obj;
};
function _where (args) {
var s = _.map(_.keys(args), function (k) {
return k + ' = \'' + args[k] + '\'';
});
return s.length > 0 ? 'WHERE ' + s.join(' and ') : '';
};
function list (args, cb) {
debug('list', args);
_connect(function (err) {
if (err) return cb(err);
return _readTable(args.name, _where(args.q), cb);
});
};
function save (args, cb) {
debug('save', args);
var ent = args.ent;
var obj = _unwrapEnt(ent);
if (ent.id$ && !obj.Id) obj.Id = ent.id$;
_connect(function (err) {
if (err) return cb(err);
var name = ent.name ? ent.name : args.name;
if (ent.id$) {
_removeUnUpdateableFields(obj, name, function (err, obj) {
if (err) return cb(err);
debug('updating obj', obj);
conn.sobject(name).update(obj, function (err, res) {
if (err || !res.success) return cb(err || res);
return cb(null, ent);
});
});
} else {
debug('creating obj', obj);
conn.sobject(name).create(obj, function (err, res) {
if (err || !res.success) return cb(err || res);
ent.id$ = res.id;
return cb(null, ent);
});
}
});
};
function load (args, cb) {
debug('load', args);
if (!args.q || !args.q.id) return cb('"id" field required');
_connect(function (err) {
if (err) return cb(err);
conn.sobject(args.name).retrieve(args.q.id, function (err, obj) {
if (err) return cb(err);
debug('loaded entity: ', obj.Id);
var ent = seneca.make$(args.name, obj);
ent.id$ = obj.Id;
return cb(null, ent);
});
});
};
function remove (args, cb) {
debug('remove', args);
if (!args.q || !args.q.id) return cb('"id" field required');
_connect(function (err) {
if (err) return cb(err);
conn.sobject(args.name).destroy(args.q.id, function (err, res) {
if (err) return cb(err);
cb(null, res);
});
});
};
return {
list: list,
save: save,
load: load,
remove: remove
};
};