-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.js
More file actions
88 lines (82 loc) · 2.17 KB
/
database.js
File metadata and controls
88 lines (82 loc) · 2.17 KB
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
'use strict';
const db = require('@arangodb').db;
const aql = require('@arangodb').aql;
const edgeList = db._query(aql`
FOR g IN meta_graph
FOR l in g.links
FOR e in meta_collection
FILTER e.name == l.edge AND e.kind == 'edge'
LET from = (
FOR f in l.from_collections
FOR c in meta_collection
FILTER f == c.name
RETURN c
)
LET to = (
FOR t in l.to_collections
FOR c in meta_collection
FILTER t == c.name
RETURN c
)
SORT e.name ASC
RETURN {
'edge': e,
'from_collections': from,
'to_collections': to
}
`).toArray()
const collectionList = db._query(aql`
FOR c in meta_collection
FILTER c.kind == 'document'
LET edges = (
FOR g IN meta_graph
FOR l in g.links
FILTER c.name in l.from_collections or c.name in l.to_collections
FOR e in meta_collection
FILTER e.name == l.edge
RETURN e
)
SORT c.name ASC
RETURN {
'collection': c,
'edges': edges
}
`).toArray()
function getObject(collection, args) {
let filters = Object.keys(args).map((key) => {
return `c.${key} == '${args[key]}'`
}).join(' AND ')
let where = ''
if (filters.length > 0)
where = ` FILTER ${filters}`
let query = `FOR c in ${collection} ${where} RETURN c`
let queryCollection = db._query(query).toArray()
if (queryCollection.length > 1){
throw "Must be more specific. Many results was returned"
}
else {
if (queryCollection.length == 0){
return null
}
else{
return queryCollection[0]
}
}
}
function getList(collection, args) {
let filters = Object.keys(args).map((key) => {
return `c.${key} == '${args[key]}'`
}).join(' AND ')
let where = ''
if (filters.length > 0)
where = ` FILTER ${filters}`
let query = `FOR c in ${collection} ${where} RETURN c`
let queryCollection = db._query(query).toArray()
return queryCollection
}
module.exports = {
edgeList: edgeList,
collectionList: collectionList,
getObject: getObject,
getList: getList
}