-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathinitDbs.ts
More file actions
136 lines (128 loc) · 3.6 KB
/
Copy pathinitDbs.ts
File metadata and controls
136 lines (128 loc) · 3.6 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
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
import {
DatabaseSetup,
JsDesignDocument,
makeJsDesign,
makeMangoIndex,
MangoDesignDocument,
setupDatabase
} from 'edge-server-tools'
import { config } from './config'
import { fixJs } from './util/fixJs'
interface DesignDocumentMap {
[designDocName: string]: MangoDesignDocument | JsDesignDocument
}
function fieldsToDesignDocs(
fields: string[],
opts?: {
noPartitionVariant?: boolean
}
): DesignDocumentMap {
const { noPartitionVariant = false } = opts ?? {}
const indexLower = fields.map(i => i.toLowerCase())
const name = indexLower.join('-')
const out: DesignDocumentMap = {}
out[`_design/${name}`] = makeMangoIndex(name, fields, {
partitioned: false
})
if (!noPartitionVariant) {
out[`_design/${name}-p`] = makeMangoIndex(`${name}-p`, fields, {
partitioned: true
})
}
return out
}
const transactionIndexes: DesignDocumentMap = {
...fieldsToDesignDocs(['isoDate']),
...fieldsToDesignDocs(['status']),
...fieldsToDesignDocs(['status', 'depositCurrency', 'isoDate']),
...fieldsToDesignDocs([
'status',
'depositCurrency',
'payoutCurrency',
'isoDate'
]),
...fieldsToDesignDocs(['status', 'isoDate']),
...fieldsToDesignDocs(['status', 'payoutAmount', 'depositAmount']),
...fieldsToDesignDocs(['status', 'payoutCurrency', 'isoDate']),
...fieldsToDesignDocs(['status', 'usdValue']),
...fieldsToDesignDocs(['status', 'usdValue', 'timestamp']),
...fieldsToDesignDocs(['usdValue']),
...fieldsToDesignDocs(['timestamp']),
...fieldsToDesignDocs(['depositAddress'], { noPartitionVariant: true }),
...fieldsToDesignDocs(['payoutAddress'], { noPartitionVariant: true }),
...fieldsToDesignDocs(['payoutAddress', 'isoDate'], {
noPartitionVariant: true
})
}
const cacheIndexes: DesignDocumentMap = {
'_design/timestamp-p': makeMangoIndex('timestamp-p', ['timestamp'], {
partitioned: true
})
}
const appsDatabaseSetup: DatabaseSetup = {
name: 'reports_apps'
}
const settingsDatabaseSetup: DatabaseSetup = {
name: 'reports_settings'
}
const transactionsDatabaseSetup: DatabaseSetup = {
name: 'reports_transactions',
options: { partitioned: true },
documents: {
...transactionIndexes,
'_design/getTxInfo': makeJsDesign(
'payoutHashfixByDate',
({ emit }) => ({
map: function(doc) {
const space = 1099511627776 // 5 bytes of space; 2^40
const prime = 769 // large prime number
let hashfix = 0 // the final hashfix
for (let i = 0; i < doc.payoutAddress.length; i++) {
const byte = doc.payoutAddress.charCodeAt(i)
hashfix = (hashfix * prime + byte) % space
}
emit([hashfix, doc.isoDate], doc._id)
}
}),
{
fixJs,
partitioned: false
}
)
}
}
const progressCacheDatabaseSetup: DatabaseSetup = {
name: 'reports_progresscache',
options: { partitioned: true }
}
const hourDatabaseSetup: DatabaseSetup = {
name: 'reports_hour',
options: { partitioned: true },
documents: cacheIndexes
}
const dayDatabaseSetup: DatabaseSetup = {
name: 'reports_day',
options: { partitioned: true },
documents: cacheIndexes
}
const monthDatabaseSetup: DatabaseSetup = {
name: 'reports_month',
options: { partitioned: true },
documents: cacheIndexes
}
const databases = [
appsDatabaseSetup,
settingsDatabaseSetup,
transactionsDatabaseSetup,
progressCacheDatabaseSetup,
hourDatabaseSetup,
dayDatabaseSetup,
monthDatabaseSetup
]
export async function initDbs(): Promise<void> {
await Promise.all(
databases.map(
async setup => await setupDatabase(config.couchDbFullpath, setup)
)
)
}