-
Notifications
You must be signed in to change notification settings - Fork 3
/
clear_builder_props.js
77 lines (65 loc) · 1.71 KB
/
clear_builder_props.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
/**
* ### Ошибочные свойства построителя
*
* @module clear_builder_props
*
*/
/**
* ### Переменные окружения
* DEBUG "wb:*,-not_this"
* DBPWD admin
* DBUSER admin
* COUCHPATH http://oknosoft.ecookna.ru:5984/pl_events
*/
'use strict';
const debug = require('debug')('wb:clear_builder_props');
const PouchDB = require('pouchdb').plugin(require('pouchdb-find'));
debug('required');
// инициализируем параметры сеанса и метаданные
const {DBUSER: username, DBPWD: password, COUCHPATH, BOOKMARK} = process.env;
const src = new PouchDB(COUCHPATH, {
auth: {username, password},
skip_setup: true,
ajax: {timeout: 100000}
});
const limit = 60;
src.info()
.then((info) => {
debug(`connected to ${info.host}, doc count: ${info.doc_count}`);
})
.then(() => clear_bp(BOOKMARK))
.then(() => debug(`ok`));
function sleep(time, res) {
return new Promise((resolve) => {
setTimeout(() => resolve(res), time);
});
}
function clear_bp(bookmark) {
// получаем в ОЗУ все номенклатуры и единицы измерения
if(!bookmark) {
bookmark = null;
}
debug(`find ${bookmark}`);
return src.find({
selector: {
class_name: 'cat.characteristics',
builder_props: {$regex: '{"0":'}
},
limit,
bookmark,
})
.then(({docs, bookmark}) => {
if(docs.length) {
debug(`received ${docs.length} rows`);
return src.bulkDocs(docs.map((row) => {
delete row.builder_props;
return row;
}))
.then(() => sleep(5000, 0))
.then(() => clear_bp(bookmark));
}
})
.catch(err => {
debug(err)
});
}