This repository was archived by the owner on May 4, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdatabases.ts
More file actions
149 lines (144 loc) · 4.25 KB
/
databases.ts
File metadata and controls
149 lines (144 loc) · 4.25 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
137
138
139
140
141
142
143
144
145
146
147
148
149
/*
* Copyright 2021, 2022 Macquarie University
*
* Licensed under the Apache License Version 2.0 (the, "License");
* you may not use, this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing software
* distributed under the License is distributed on an "AS IS" BASIS
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND either express or implied.
* See, the License, for the specific language governing permissions and
* limitations under the License.
*
* Filename: databases.ts
* Description:
* Functions for initialising databases
*/
type DesignDocument = {
_id: string;
_rev?: string;
views?: {
[key: string]: {
map: string;
reduce?: string;
};
};
language?: string;
validate_doc_update?: string;
};
const isEqualObjects = (a: any, b: any) => {
// we have the same keys
if (
!Object.keys(a).every(key => key in b) ||
!Object.keys(b).every(key => key in a)
) {
return false;
}
// and all the key values match
for (const key in a) {
const a_value = a[key];
const b_value = b[key];
if (a_value instanceof Object && b_value instanceof Object) {
if (!isEqualObjects(a_value, b_value)) {
return false;
}
} else {
if (a_value !== b_value) {
console.log('objects differ on', key);
return false;
}
}
}
return true;
};
export const addDesignDocsForNotebook = async (
dataDB: PouchDB.Database<any>
) => {
const documents: DesignDocument[] = [];
documents.push({
_id: '_design/attachment_filter',
views: {
attachment_filter: {
map: `function (doc) {
if (doc.attach_format_version === undefined) {
emit(doc._id);
}
}`,
},
},
});
documents.push({
_id: '_design/permissions',
validate_doc_update: `function (newDoc, oldDoc, userCtx, _secObj) {
if (userCtx === null || userCtx === undefined) {
throw {unauthorized: 'You must be logged in. No token given.'};
}
if (userCtx.name === null || userCtx.name === undefined) {
throw {unauthorized: 'You must be logged in. No username given.'};
}
return;
}`,
});
// create indexes for each kind of document in the database
documents.push({
_id: '_design/index',
views: {
record: {
map: 'function (doc) {\n if (doc.record_format_version === 1)\n emit(doc._id, 1);\n}',
},
recordRevisions: {
map: `function (doc) {
if (doc.record_format_version === 1)
if (doc.heads.length > 0) {
const conflict = doc.heads.length > 1;
const created = doc.created;
const type = doc.type;
emit(doc._id, {_id: doc.heads[0], conflict, created, type});
}
}`,
},
avpAttachment: {
map: `function (doc) {
if (doc.avp_format_version === 1 && doc.faims_attachments)
doc.faims_attachments.forEach((att) => {
emit(doc._id, {_id: att.attachment_id});
});
}`,
},
revision: {
map: 'function (doc) {\n if (doc.revision_format_version === 1) \n emit(doc._id, 1);\n}',
},
avp: {
map: 'function (doc) {\n if (doc.avp_format_version === 1)\n emit(doc._id, 1);\n}',
},
recordCount: {
map: 'function (doc) {\n if (doc.record_format_version === 1)\n emit(doc._id, 1);\n}',
reduce: '_count',
},
},
language: 'javascript',
});
// add or update each of the documents
for (let i = 0; i < documents.length; i++) {
try {
const existing = await dataDB.get(documents[i]._id);
if (existing) {
documents[i]['_rev'] = existing['_rev'];
// are they the same?
if (isEqualObjects(existing, documents[i])) {
continue;
}
}
await dataDB.put(documents[i]);
} catch (error) {
try {
await dataDB.put(documents[i]);
} catch (error) {
console.log('Error adding design documents to database:', error);
}
}
}
};