-
Notifications
You must be signed in to change notification settings - Fork 3
/
move_formulas.js
92 lines (81 loc) · 2.11 KB
/
move_formulas.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
/**
* ### Модуль переноса svg из вложений в реквизит характеристики
*
* @module move_svgs
*
* Created 24.02.2018
*/
/**
* ### Переменные окружения
* DEBUG "wb:*,-not_this"
* ZONE 21
* DBPWD admin
* DBUSER admin
* COUCHPATH http://cou221:5984/wb_
*/
'use strict';
const debug = require('debug')('wb:move_formulas');
const PouchDB = require('./pouchdb')
.plugin(require('pouchdb-find'));
debug('required');
// инициализируем параметры сеанса и метаданные
const {DBUSER, DBPWD, COUCHPATH, ZONE} = process.env;
const prefix = 'wb_';
const src = new PouchDB(`${COUCHPATH}${ZONE}_doc`, {
auth: {
username: DBUSER,
password: DBPWD
},
skip_setup: true,
ajax: {timeout: 100000}
});
const dst = new PouchDB(`${COUCHPATH}${ZONE}_ram`, {
auth: {
username: DBUSER,
password: DBPWD
},
skip_setup: true,
ajax: {timeout: 100000}
});
src.info()
.then((info) => {
debug(`connected to ${info.host}, doc count: ${info.doc_count}`);
})
.then(() => move_docs('cat.formulas'))
.then(() => move_docs('cch.predefined_elmnts'))
.then(() => debug('all done'))
.catch(err => {
debug(err)
});
function move_docs(class_name) {
return src.allDocs({
limit: 10,
include_docs: true,
attachments: true,
startkey: `${class_name}|`,
endkey: `${class_name}|\ufff0`,
})
.then(({total_rows, rows}) => {
if(!rows.length) {
return true;
}
// записываем документы в новое место
rows = rows.map(v => v.doc);
return dst.bulkDocs(rows, {new_edits: false})
.then((res) => {
// удаляем документы в старом месте
for(const doc of rows) {
for(const _fld in doc) {
if(_fld[0] !== '_') {
delete doc[_fld];
}
}
doc._deleted = true;
}
return src.bulkDocs(rows);
})
.then((res) => {
return rows.length === 10 ? move_docs(class_name) : true;
})
});
}