|
1 | | -import nano from 'nano' |
| 1 | +import { |
| 2 | + DatabaseSetup, |
| 3 | + JsDesignDocument, |
| 4 | + makeMangoIndex, |
| 5 | + MangoDesignDocument, |
| 6 | + setupDatabase |
| 7 | +} from 'edge-server-tools' |
2 | 8 |
|
3 | 9 | import { config } from './config' |
4 | | -import { datelog } from './util' |
5 | 10 |
|
6 | | -const nanoDb = nano(config.couchDbFullpath) |
7 | | - |
8 | | -const transactionIndexFields: string[][] = [ |
9 | | - ['isoDate'], |
10 | | - ['status'], |
11 | | - ['status', 'depositCurrency', 'isoDate'], |
12 | | - ['status', 'depositCurrency', 'payoutCurrency', 'isoDate'], |
13 | | - ['status', 'isoDate'], |
14 | | - ['status', 'payoutAmount', 'depositAmount'], |
15 | | - ['status', 'payoutCurrency', 'isoDate'], |
16 | | - ['status', 'usdValue'], |
17 | | - ['status', 'usdValue', 'timestamp'], |
18 | | - ['usdValue'], |
19 | | - ['timestamp'] |
20 | | -] |
21 | | - |
22 | | -const transactionIndexFieldsNoPartition: string[][] = [ |
23 | | - ['depositAddress'], |
24 | | - ['payoutAddress'] |
25 | | -] |
26 | | - |
27 | | -interface Index { |
28 | | - index: { fields: string[] } |
29 | | - ddoc: string |
30 | | - name: string |
31 | | - type: 'json' |
32 | | - partitioned: boolean |
| 11 | +interface DesignDocumentMap { |
| 12 | + [designDocName: string]: MangoDesignDocument | JsDesignDocument |
33 | 13 | } |
34 | 14 |
|
35 | | -const transactionIndexes: Index[] = [] |
36 | | - |
37 | | -transactionIndexFields.forEach(index => { |
38 | | - const indexLower = index.map(i => i.toLowerCase()) |
39 | | - const out: Index = { |
40 | | - index: { fields: index }, |
41 | | - ddoc: indexLower.join('-'), |
42 | | - name: indexLower.join('-'), |
43 | | - type: 'json', |
| 15 | +function fieldsToDesign( |
| 16 | + fields: string[], |
| 17 | + withPartitionVariant: boolean = true |
| 18 | +): DesignDocumentMap { |
| 19 | + const indexLower = fields.map(i => i.toLowerCase()) |
| 20 | + const name = indexLower.join('-') |
| 21 | + const out: DesignDocumentMap = {} |
| 22 | + out[`_design/${name}`] = makeMangoIndex(name, fields, { |
44 | 23 | partitioned: false |
| 24 | + }) |
| 25 | + if (withPartitionVariant) { |
| 26 | + out[`_design/${name}-p`] = makeMangoIndex(`${name}-p`, fields, { |
| 27 | + partitioned: true |
| 28 | + }) |
45 | 29 | } |
46 | | - transactionIndexes.push(out) |
47 | | - const out2 = { ...out } |
48 | | - out2.ddoc += '-p' |
49 | | - out2.name += '-p' |
50 | | - out2.partitioned = true |
51 | | - transactionIndexes.push(out2) |
52 | | -}) |
| 30 | + return out |
| 31 | +} |
53 | 32 |
|
54 | | -transactionIndexFieldsNoPartition.forEach(index => { |
55 | | - const indexLower = index.map(i => i.toLowerCase()) |
56 | | - const out: Index = { |
57 | | - index: { fields: index }, |
58 | | - ddoc: indexLower.join('-'), |
59 | | - name: indexLower.join('-'), |
60 | | - type: 'json', |
61 | | - partitioned: false |
62 | | - } |
63 | | - transactionIndexes.push(out) |
64 | | -}) |
| 33 | +const transactionIndexes: DesignDocumentMap = { |
| 34 | + ...fieldsToDesign(['isoDate']), |
| 35 | + ...fieldsToDesign(['status']), |
| 36 | + ...fieldsToDesign(['status', 'depositCurrency', 'isoDate']), |
| 37 | + ...fieldsToDesign(['status', 'depositCurrency', 'payoutCurrency', 'isoDate']), |
| 38 | + ...fieldsToDesign(['status', 'isoDate']), |
| 39 | + ...fieldsToDesign(['status', 'payoutAmount', 'depositAmount']), |
| 40 | + ...fieldsToDesign(['status', 'payoutCurrency', 'isoDate']), |
| 41 | + ...fieldsToDesign(['status', 'usdValue']), |
| 42 | + ...fieldsToDesign(['status', 'usdValue', 'timestamp']), |
| 43 | + ...fieldsToDesign(['usdValue']), |
| 44 | + ...fieldsToDesign(['timestamp']), |
| 45 | + ...fieldsToDesign(['depositAddress'], false), |
| 46 | + ...fieldsToDesign(['payoutAddress'], false) |
| 47 | +} |
65 | 48 |
|
66 | | -const cacheIndexes: Index[] = [ |
67 | | - { |
68 | | - index: { fields: ['timestamp'] }, |
69 | | - ddoc: 'timestamp-p', |
70 | | - name: 'timestamp-p', |
71 | | - type: 'json' as 'json', |
| 49 | +const cacheIndexes: DesignDocumentMap = { |
| 50 | + '_design/timestamp-p': makeMangoIndex('timestamp-p', ['timestamp'], { |
72 | 51 | partitioned: true |
73 | | - } |
74 | | -] |
75 | | - |
76 | | -const options = { partitioned: true } |
| 52 | + }) |
| 53 | +} |
77 | 54 |
|
78 | | -const DB_NAMES = [ |
79 | | - { name: 'reports_apps' }, |
80 | | - { name: 'reports_settings' }, |
81 | | - { |
82 | | - name: 'reports_transactions', |
83 | | - options, |
84 | | - indexes: transactionIndexes |
85 | | - }, |
86 | | - { name: 'reports_progresscache', options }, |
87 | | - { |
88 | | - name: 'reports_hour', |
89 | | - options, |
90 | | - indexes: cacheIndexes |
91 | | - }, |
92 | | - { |
93 | | - name: 'reports_day', |
94 | | - options, |
95 | | - indexes: cacheIndexes |
96 | | - }, |
97 | | - { |
98 | | - name: 'reports_month', |
99 | | - options, |
100 | | - indexes: cacheIndexes |
| 55 | +const appsDatabaseSetup: DatabaseSetup = { |
| 56 | + name: 'reports_apps' |
| 57 | +} |
| 58 | +const settingsDatabaseSetup: DatabaseSetup = { |
| 59 | + name: 'reports_settings' |
| 60 | +} |
| 61 | +const transactionsDatabaseSetup: DatabaseSetup = { |
| 62 | + name: 'reports_transactions', |
| 63 | + options: { partitioned: true }, |
| 64 | + documents: { |
| 65 | + ...transactionIndexes |
101 | 66 | } |
| 67 | +} |
| 68 | +const progressCacheDatabaseSetup: DatabaseSetup = { |
| 69 | + name: 'reports_progresscache', |
| 70 | + options: { partitioned: true } |
| 71 | +} |
| 72 | +const hourDatabaseSetup: DatabaseSetup = { |
| 73 | + name: 'reports_hour', |
| 74 | + options: { partitioned: true }, |
| 75 | + documents: cacheIndexes |
| 76 | +} |
| 77 | +const dayDatabaseSetup: DatabaseSetup = { |
| 78 | + name: 'reports_day', |
| 79 | + options: { partitioned: true }, |
| 80 | + documents: cacheIndexes |
| 81 | +} |
| 82 | +const monthDatabaseSetup: DatabaseSetup = { |
| 83 | + name: 'reports_month', |
| 84 | + options: { partitioned: true }, |
| 85 | + documents: cacheIndexes |
| 86 | +} |
| 87 | + |
| 88 | +const databases = [ |
| 89 | + appsDatabaseSetup, |
| 90 | + settingsDatabaseSetup, |
| 91 | + transactionsDatabaseSetup, |
| 92 | + progressCacheDatabaseSetup, |
| 93 | + hourDatabaseSetup, |
| 94 | + dayDatabaseSetup, |
| 95 | + monthDatabaseSetup |
102 | 96 | ] |
103 | 97 |
|
104 | 98 | export async function initDbs(): Promise<void> { |
105 | | - // get a list of all databases within couchdb |
106 | | - const result = await nanoDb.db.list() |
107 | | - datelog(result) |
108 | | - // if database does not exist, create it |
109 | | - for (const dbName of DB_NAMES) { |
110 | | - if (!result.includes(dbName.name)) { |
111 | | - await nanoDb.db.create(dbName.name, dbName.options) |
112 | | - } |
113 | | - if (dbName.indexes !== undefined) { |
114 | | - const currentDb = nanoDb.db.use(dbName.name) |
115 | | - for (const dbIndex of dbName.indexes) { |
116 | | - try { |
117 | | - await currentDb.get(`_design/${dbIndex.ddoc}`) |
118 | | - datelog(`${dbName.name} already has '${dbIndex.name}' index.`) |
119 | | - } catch { |
120 | | - await currentDb.createIndex(dbIndex) |
121 | | - datelog(`Created '${dbIndex.name}' index for ${dbName.name}.`) |
122 | | - } |
123 | | - } |
124 | | - } |
125 | | - } |
| 99 | + await Promise.all( |
| 100 | + databases.map( |
| 101 | + async setup => await setupDatabase(config.couchDbFullpath, setup) |
| 102 | + ) |
| 103 | + ) |
126 | 104 | } |
0 commit comments