-
Notifications
You must be signed in to change notification settings - Fork 1
/
slpdb.js
133 lines (117 loc) · 2.3 KB
/
slpdb.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
(() => {
const configResult = require('dotenv').config()
if (configResult.error) {
throw configResult.error
}
})()
const btoa = require('btoa')
const axios = require('axios')
const _ = require('lodash')
const slpdb = {
regex: {
TOKENIDHEX: '[0-9a-f]{64}',
TXID: '[0-9a-f]{64}',
UTXO: '[0-9a-f]{64}:[0-9]+'
},
query: (query) => new Promise((resolve, reject) => {
if (!query) {
return reject(new Error('no query provided'))
}
const b64 = btoa(JSON.stringify(query))
const url = process.env.SLPSERVE_URL + b64
// log to help debugging
console.info(url)
resolve(axios.get(url)
.then(data => data.data,
e => reject(e))
)
}),
match: (prop, db, match, additional_requirements = {}) => {
let find = {
[prop]: match
};
_.merge(find, additional_requirements);
return {
'v': 3,
'q': {
'db': db,
'find': find,
'limit': 1
}
};
},
// helper query to check the property exists
exists: (prop, db, additional_requirements = {}) => {
let find = {
[prop]: {
'$exists': false
}
}
_.merge(find, additional_requirements)
return {
'v': 3,
'q': {
'db': [db],
'find': find,
'limit': 1
}
}
},
inverse_regex: (regex) => `^((?!${regex}).)*$`,
inverse_match_regex: (prop, db, regex) => ({
'v': 3,
'q': {
'db': [db],
'find': {
[prop]: {
'$regex': slpdb.inverse_regex(regex)
}
},
'limit': 1
}
}),
inverse_match_array: (prop, db, arr) => ({
'v': 3,
'q': {
'db': [db],
'find': {
[prop]: {
'$nin': arr
}
},
'limit': 1
}
}),
inverse_match_list: (prop, db, list, count = prop) => {
let aggConditions = list.map(v => ({
'$match': {
[prop]: v
}
})).concat({
'$count': count
})
return {
'v': 3,
'q': {
'db': [db],
'aggregate': aggConditions,
'limit': 1
}
}
},
inverse_match_not_type: (prop, db, ntype) => ({
'v': 3,
'q': {
'db': db,
'find': {
[prop]: {
'$not': {
'$type': ntype
}
}
},
'limit': 1
}
})
}
module.exports = slpdb