/
resolver.coffee
84 lines (67 loc) · 2.48 KB
/
resolver.coffee
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
_ = require 'underscore'
async = require 'async'
module.exports = class Resolver
constructor: () ->
@matrix = {}
@embedMe = {}
normalizeName: (name) =>
name = name.toLowerCase()
add:(kind,idOrIds, target, embed = false) =>
kind = @normalizeName(kind)
return unless idOrIds
idOrIds = [idOrIds] unless _.isArray(idOrIds)
collection = @matrix[kind]
collection = @matrix[kind] = {} unless collection
for id in idOrIds
if collection[id]
collection[id].push target
else
collection[id]=[target]
@embedMe[id.toString()] = true if embed
resolve: (resolverMap, rootObject,options = {},client, cb = ->) =>
functions = []
for kind,collection of @matrix
resolver = resolverMap[kind]
objectIds = _.keys(collection)
if resolver && _.isArray(objectIds) && objectIds.length > 0
functions.push (cb) => resolver.resolve kind,objectIds,options,cb
###
console.log "Collection: #{kind}"
for id in _.keys(collection)
console.log "ID: #{id}"
###
if functions.length > 0
async.parallel functions, (err,results) =>
###
console.log "==========++++========="
console.log "GOT RESULTS"
###
for r in (results || [])
r.items = r.items || []
keys = _.keys(r.items)
keys = _.filter keys, (x) => @embedMe[x]
###
console.log JSON.stringify(r)
console.log "-------"
###
if keys.length > 0 #We only add this if we have keys to embed.
rootObject._embedded = {} unless rootObject._embedded
rootObject._embedded[r.collectionName] = {} unless rootObject._embedded[r.collectionName]
# Here we transform the items and add them to the embedded collection.
c = rootObject._embedded[r.collectionName]
c[k] = client.mapObjectSync(r.kind,r.items[k],options,null) for k in keys
###
We need to merge this back. Each r looks like
r =
kind: ...
collectionName: 'users'
items: {} where the index into the items is the string/lowercased object id
###
for id,t of (@matrix[r.kind] || {})
for o in (t || []) when !!o
if r.items[id]
options.scopes = ['inline']
_.extend o, client.mapObjectSync(r.kind,r.items[id],options,null)
cb err, rootObject
else
cb null, rootObject