-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.js
228 lines (186 loc) · 4.8 KB
/
index.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/* Copyright (c) 2014 Matteo Collina, ISC License
*
* Based on seneca-jsonfile-store
* Copyright (c) 2013 Richard Rodger, MIT License */
'use strict'
var Sharder = require('sharder')
var async = require('async')
var Seneca = require('seneca')
var _ = require('underscore')
var name = 'shard-store'
module.exports = function (opts) {
var seneca = this
/**
* configure the store - create a new store specific connection object
*
* params:
* spec - store specific configuration
* cb - callback
*/
var shards
function configure (specification, cb) {
for (var shardId in specification.shards) {
var shard = specification.shards[shardId]
shard.seneca = Seneca()
shard.seneca.use(shard.store.plugin, shard.store.options)
}
shards = new Sharder(specification)
cb(null, shards)
}
function shardWrap (args, cb) {
var seneca = this
var id
var shard
if (args.ent) {
id = args.ent.id$
}
else if (args.q) {
id = args.q.id
}
if (args.cmd !== 'save' && !id) {
// shardWrapAll.call here is just to be clean and execute wrapAll in the right seneca context
return shardWrapAll.call(seneca, args, function (err, list) {
cb(err, list && list[0])
})
}
if (args.cmd === 'save' && !id) {
id = args.ent.id || shards.generate()
args.ent.id$ = id
}
shard = shards.resolve(id)
if (args.zone) {
for (var sh in shards._appendShards) {
if (shards._appendShards[sh].zone === args.zone) {
shard = shards._appendShards[sh]
}
}
}
shard.seneca.act(args, cb)
}
function shardWrapAll (args, cb) {
var skip
var limit
async.concat(Object.keys(shards.shards), function (shardId, cb) {
var shard = shards.shards[shardId]
if (args.q && void 0 !== args.q.limit$) {
if (!limit) {
limit = args.q.limit$
}
}
if (args.q && void 0 !== args.q.skip$) {
skip = args.q.skip$
args.q.limit$ = args.q.limit$ + skip
delete args.q.skip$
}
shard.seneca.act(args, function (err, result) {
if (err) {
this.log.error(err)
}
cb(undefined, result)
})
}, function (err, result) {
if (!err && result) {
merge(args, result)
}
if (args.cmd === 'list' && args.q) {
var startindex = 0
var endindex = result.length
var limitOrSkip = false
if (skip) {
limitOrSkip = true
startindex = skip
}
if (limit) {
limitOrSkip = true
endindex = result.length > limit + startindex ? limit + startindex : result.length
}
if (limitOrSkip) {
result = result.slice(startindex, endindex)
}
}
if ((args.cmd === 'save' || args.cmd === 'load') && result.length === 0) {
result = null
}
cb(err, result)
})
}
var store = {
name: name,
save: shardWrap,
load: shardWrap,
list: shardWrapAll,
remove: shardWrapAll,
close: shardWrapAll,
native: function (done) {
done(null, opts)
}
}
/**
* initialization
*/
var meta = seneca.store.init(seneca, opts, store)
var desc = meta.desc
seneca.add({ init: store.name, tag: meta.tag }, function (args, done) {
configure(opts, function (err) {
if (err) {
return seneca.fail({ code: 'entity/configure', store: store.name, error: err, desc: desc }, done)
}
else done()
})
})
return { name: store.name, tag: meta.tag }
}
function merge (args, list) {
distinct(args, list)
sort(args, list)
}
function distinct (args, list) {
if (args && args.cmd === 'list' && args.q && args.q.distinct$) {
if (_.isArray(list)) {
var index = 0
var visited = {}
while (index < list.length) {
var item = _.clone(list[index])
delete item.id
if (!visited.hasOwnProperty(JSON.stringify(item))) {
visited[JSON.stringify(item)] = true
index++
}
else {
list.splice(index, 1)
}
}
}
}
}
function sort (args, list) {
if (args && args.cmd === 'list' && args.q && args.q.sort$) {
if (_.isArray(list)) {
list.sort(function (a, b) {
if (!a && !b) {
return 0
}
else if (a && !b) {
return 1
}
else if (!a && b) {
return -1
}
else {
for (var sortAttr in args.q.sort$) {
if (a[sortAttr] === b[sortAttr]) {
continue
}
else if (a[sortAttr] < b[sortAttr]) {
return +args.q.sort$[sortAttr]
}
else {
return -args.q.sort$[sortAttr]
}
}
return 0
}
})
}
}
}