This repository has been archived by the owner on Jan 15, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
schema.js
113 lines (103 loc) · 4.1 KB
/
schema.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
/* jshint unused:false */
function addDefaultUniqueAttributeTypes(db) {
return db.table('UniqueAttributeTypes').insert([
{ name: 'email' },
{ name: 'account' },
{ name: 'url' },
{ name: 'tel' },
{ name: 'keyID' },
{ name: 'bitcoin' },
{ name: 'identifiNode' },
{ name: 'gpg_fingerprint' },
{ name: 'gpg_keyid' },
]);
}
function catcher(e) {
// Ignore sqlite and pg "already exists" errors
if (!(e.code.match(/^42/) || e.errno === 1 || e.errno === 19)) {
console.error(e);
}
}
function init(db) {
const queries = [];
queries.push(db.schema.createTableIfNotExists('UniqueAttributeTypes', (t) => {
t.string('name').primary();
}).then(() => addDefaultUniqueAttributeTypes(db).catch(catcher)).catch(catcher));
queries.push(db.schema.createTableIfNotExists('Messages', (t) => {
t.string('hash').unique().primary();
t.string('ipfs_hash', 50).unique();
t.string('jws', 10000).notNullable();
t.datetime('saved_at').notNullable();
t.datetime('timestamp');
t.string('type');
t.integer('rating');
t.integer('max_rating');
t.integer('min_rating');
t.boolean('public').default(true);
t.integer('priority').unsigned();
t.boolean('is_latest');
t.string('signer_keyid');
t.index(['timestamp']);
t.index(['ipfs_hash']);
t.index(['type']);
}).catch(catcher)
.then(() => {
db.schema.createTableIfNotExists('MessageAttributes', (t) => {
t.string('message_hash').references('Messages.hash');
t.string('name').notNullable();
t.string('value').notNullable();
t.boolean('is_recipient');
t.index(['message_hash', 'name', 'value']);
t.index(['message_hash', 'is_recipient']);
t.index(['message_hash']);
t.index(['name', 'value']);
t.index(['value']);
// t.index(['lower("value")'], 'lowercase_value');
t.primary(['message_hash', 'is_recipient', 'name', 'value']);
}).catch(catcher);
}));
queries.push(db.schema.createTableIfNotExists('TrustDistances', (t) => {
t.string('start_attr_name').notNullable();
t.string('start_attr_value').notNullable();
t.string('end_attr_name').notNullable();
t.string('end_attr_value').notNullable();
t.integer('distance').notNullable();
t.primary(['start_attr_name', 'start_attr_value', 'end_attr_name', 'end_attr_value']);
}).catch(catcher));
queries.push(db.schema.createTableIfNotExists('IdentityAttributes', (t) => {
t.integer('identity_id').unsigned();
t.string('name').notNullable();
t.string('value').notNullable();
t.string('viewpoint_name').notNullable();
t.string('viewpoint_value').notNullable();
t.integer('confirmations').unsigned();
t.integer('refutations').unsigned();
t.index(['identity_id']);
t.index(['identity_id', 'name']);
t.index(['viewpoint_name', 'viewpoint_value']);
t.index(['name', 'viewpoint_name', 'viewpoint_value']);
t.index(['name', 'value', 'viewpoint_name', 'viewpoint_value']);
t.primary(['identity_id', 'name', 'value', 'viewpoint_name', 'viewpoint_value']);
}).catch(catcher));
queries.push(db.schema.createTableIfNotExists('IdentityStats', (t) => {
t.integer('identity_id').unsigned().primary();
t.string('viewpoint_name').notNullable();
t.string('viewpoint_value').notNullable();
t.integer('distance').notNullable().default(-1);
t.integer('positive_score').unsigned().notNullable().default(0);
t.integer('negative_score').unsigned().notNullable().default(0);
t.integer('sent_positive').unsigned().notNullable().default(0);
t.integer('sent_negative').unsigned().notNullable().default(0);
t.string('cached_identity_profile').notNullable().default('');
t.index(['viewpoint_name', 'viewpoint_value', 'distance']);
}).catch(catcher));
queries.push(db.schema.createTableIfNotExists('IndexedViewpoints', (t) => {
t.string('name').notNullable();
t.string('value').notNullable();
t.string('ipfs_index_root');
t.integer('depth').unsigned().notNullable();
t.primary(['name', 'value']);
}).catch(catcher));
return Promise.all(queries);
}
module.exports = { init };