/
write.js
77 lines (68 loc) · 2.74 KB
/
write.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
'use strict';
const error = require('./error.js');
const r = require('rethinkdb');
const check = error.check;
const fail = error.fail;
module.exports.make_write_reql = function (request) {
var type = request.type;
var options = request.options;
var collection = options.collection;
var data = options.data;
check(data !== undefined, `'options.data' must be specified.`);
check(data.length >= 0, `'options.data' must be an array of at least length 1.`);
check(collection !== undefined, `'options.collection' must be specified.`);
check(collection.constructor.name === 'String',
`'options.collection' must be a string.`)
var reql = r.table(collection);
switch (type) {
case 'store':
var missing = options.missing;
var conflict = options.conflict;
check(missing !== undefined, `'options.missing' must be specified for a 'store' operation.`);
check(conflict !== undefined, `'options.conflict' must be specified for a 'store' operation.`);
check(conflict === 'update' || conflict === 'replace' || conflict === 'error',
`'options.conflict' must be one of 'update', 'replace', or 'error'.`);
if (missing === 'insert') {
reql = reql.insert(data, { conflict: conflict });
} else if (missing === 'error') {
if (conflict === 'update') {
reql = r.expr(data).forEach((row) =>
reql.get(row('id')).replace((old) =>
r.branch(old.ne(null), old.merge(row),
r.error(r.expr("The document with id '")
.add(row('id').coerceTo('string'))
.add("' was missing.")))));
} else if (conflict === 'replace') {
reql = r.expr(data).forEach((row) =>
reql.get(row('id')).replace((old) =>
r.branch(old.ne(null), row,
r.error(r.expr("The document with id '")
.add(row('id').coerceTo('string'))
.add("' was missing.")))));
} else {
fail(`'options.missing' and 'options.conflict' cannot both be 'error'.`);
}
} else {
fail(`'options.missing' must be one of 'insert' or 'error'.`);
}
break;
case 'remove':
var ids = data.map((row) => {
check(row.id !== undefined, `'options.data[i].id' must be specified for 'remove'.`);
return row.id;
});
reql = reql.getAll(r.args(ids), { index: 'id' }).delete();
}
return reql;
};
module.exports.handle_write_response = function (client, request, response, send_cb) {
if (response.errors !== 0) {
send_cb({ error: response.first_error });
} else {
var index = 0;
var ids = request.options.data.map((row) => {
return row.id === undefined ? response.generated_keys[index++] : row.id;
});
send_cb({ data: ids, state: 'complete' });
}
};