@cocreate/crud-server 1.22.0 → 1.22.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/backup.js CHANGED
@@ -1,150 +1,150 @@
1
- const json2csv = require("json-2-csv")
2
- const csvtojson = require("csvtojson");
3
-
4
- class CoCreateBackup {
5
- constructor(wsManager, dbClient) {
6
- this.wsManager = wsManager
7
- this.dbClient = dbClient
8
- this.importCollection = '';
9
- this.importType = '';
10
- this.importDB = '';
11
- this.init();
12
- }
13
-
14
- init() {
15
- if (this.wsManager) {
16
- // this.wsManager.on('exportDB', (socket, data) => this.exportData(socket, data));
17
- // this.wsManager.on('importDB', (socket, data) => this.importData(socket, data));
18
- // this.wsManager.on('downloadData', (socket, data) => this.downloadData(socket, data))
19
- }
20
- }
21
-
22
- /**
23
- * data: {
24
- collection: '',
25
- type: 'csv/json'
26
- data: JSON data
27
- }
28
- **/
29
- // async downloadData(socket, data) {
30
- // const export_type = data.type || "json";
31
-
32
- // try {
33
- // let binaryData = null;
34
- // const result = data.data;
35
- // const organization_id = socket.config.organization_id ;
36
- // if (export_type === 'csv') {
37
- // binaryData = await json2csv.json2csvAsync(JSON.parse(JSON.stringify(result)), {
38
- // emptyFieldValue: ''
39
- // });
40
- // } else {
41
- // binaryData = Buffer.from(JSON.stringify(result));
42
- // }
43
-
44
- // this.wsManager.send(socket, 'downloadFileInfo', {file_name: `backup_${data['collection']}.${export_type}`, binaryData, export_type}, organization_id);
45
-
46
- // this.wsManager.sendBinary(socket, binaryData, organization_id);
47
-
48
- // } catch (error) {
49
- // console.log('export error', error);
50
- // }
51
- // }
52
-
53
- /**
54
- CoCreateSocket.exportData({
55
- collection: '',
56
- db: '',
57
- export_type: json/csv,
58
-
59
- })
60
- **/
61
- // async exportData(socket, data) {
62
- // const self = this;
63
-
64
- // const export_type = data.export_type || "json";
65
-
66
- // try {
67
-
68
- // var collection = this.dbClient.db(data['namespace']).collection(data["collection"]);
69
- // const organization_id = socket.config.organization_id
70
-
71
- // var query = {};
72
-
73
- // collection.find(query).toArray(async function(error, result) {
74
- // if (!error) {
75
- // let binaryData = null;
76
- // self.wsManager.send(socket, 'downloadFileInfo', {file_name: `backup_${data['collection']}.${export_type}`}, organization_id);
77
- // if (export_type === 'csv') {
78
- // binaryData = await json2csv.json2csvAsync(JSON.parse(JSON.stringify(result)), {
79
- // emptyFieldValue: ''
80
- // });
81
- // } else {
82
- // binaryData = Buffer.from(JSON.stringify(result));
83
- // }
84
-
85
- // self.wsManager.sendBinary(socket, binaryData, organization_id);
86
- // }
87
- // });
88
- // } catch (error) {
89
- // console.log('export error', error);
90
- // }
91
- // }
92
-
93
- // async setImportData(socket, data) {
94
- // this.importCollection = data['collection']
95
- // this.importType = data['import_type'];
96
- // this.importDB = data['namespace'];
97
- // }
98
-
99
- // async importData(socket, data) {
100
- // const importCollection = data['collection']
101
- // const importType = data['import_type'];
102
- // const importFile = data['file'];
103
- // // const importDB = data['namespace'];
104
-
105
- // console.log('import:', importCollection, importType, importFile)
106
- // const self = this;
107
- // // const organization_id = socket.config.organization_id
108
- // if (!importCollection || !importType) {
109
- // return;
110
- // }
111
- // try {
112
- // console.log('data: ', data)
113
- // // return;
114
- // let jsonData = null;
115
- // if (this.importType === 'csv') {
116
- // jsonData = await csvtojson({ignoreEmpty: true}).fromString(data.toString())
117
- // }
118
- // else {
119
- // jsonData = JSON.parse(importFile.toString());
120
- // }
121
- // // TODO: validate json / if json is object error happens
122
- // jsonData.map((item) => delete item._id);
123
- // console.log('json: ', jsonData)
124
- // var collection = this.dbClient.db(organization_id).collection(importCollection);
125
- // // console.log(this.importCollection)
126
- // collection.insertMany(jsonData, function(err, result) {
127
- // if (!err) {
128
- // self.wsManager.send(socket, 'importedFile2DB', {
129
- // 'database': organization_id,
130
- // 'collection': importCollection,
131
- // 'import_type': importType,
132
- // 'data': result
133
- // }, organization_id)
134
- // }
135
- // })
136
-
137
- // // this.importCollection = '';
138
- // // this.importType = '';
139
-
140
- // } catch (error) {
141
- // console.log('import db error', error)
142
- // }
143
-
144
- // }
145
-
146
- }
147
- module.exports = CoCreateBackup;
148
-
149
-
150
-
1
+ const json2csv = require("json-2-csv")
2
+ const csvtojson = require("csvtojson");
3
+
4
+ class CoCreateBackup {
5
+ constructor(wsManager, dbClient) {
6
+ this.wsManager = wsManager
7
+ this.dbClient = dbClient
8
+ this.importCollection = '';
9
+ this.importType = '';
10
+ this.importDB = '';
11
+ this.init();
12
+ }
13
+
14
+ init() {
15
+ if (this.wsManager) {
16
+ // this.wsManager.on('exportDB', (socket, data) => this.exportData(socket, data));
17
+ // this.wsManager.on('importDB', (socket, data) => this.importData(socket, data));
18
+ // this.wsManager.on('downloadData', (socket, data) => this.downloadData(socket, data))
19
+ }
20
+ }
21
+
22
+ /**
23
+ * data: {
24
+ collection: '',
25
+ type: 'csv/json'
26
+ data: JSON data
27
+ }
28
+ **/
29
+ // async downloadData(socket, data) {
30
+ // const export_type = data.type || "json";
31
+
32
+ // try {
33
+ // let binaryData = null;
34
+ // const result = data.data;
35
+ // const organization_id = socket.config.organization_id ;
36
+ // if (export_type === 'csv') {
37
+ // binaryData = await json2csv.json2csvAsync(JSON.parse(JSON.stringify(result)), {
38
+ // emptyFieldValue: ''
39
+ // });
40
+ // } else {
41
+ // binaryData = Buffer.from(JSON.stringify(result));
42
+ // }
43
+
44
+ // this.wsManager.send(socket, 'downloadFileInfo', {file_name: `backup_${data['collection']}.${export_type}`, binaryData, export_type}, organization_id);
45
+
46
+ // this.wsManager.sendBinary(socket, binaryData, organization_id);
47
+
48
+ // } catch (error) {
49
+ // console.log('export error', error);
50
+ // }
51
+ // }
52
+
53
+ /**
54
+ CoCreateSocket.exportData({
55
+ collection: '',
56
+ db: '',
57
+ export_type: json/csv,
58
+
59
+ })
60
+ **/
61
+ // async exportData(socket, data) {
62
+ // const self = this;
63
+
64
+ // const export_type = data.export_type || "json";
65
+
66
+ // try {
67
+
68
+ // var collection = this.dbClient.db(data['namespace']).collection(data["collection"]);
69
+ // const organization_id = socket.config.organization_id
70
+
71
+ // var query = {};
72
+
73
+ // collection.find(query).toArray(async function(error, result) {
74
+ // if (!error) {
75
+ // let binaryData = null;
76
+ // self.wsManager.send(socket, 'downloadFileInfo', {file_name: `backup_${data['collection']}.${export_type}`}, organization_id);
77
+ // if (export_type === 'csv') {
78
+ // binaryData = await json2csv.json2csvAsync(JSON.parse(JSON.stringify(result)), {
79
+ // emptyFieldValue: ''
80
+ // });
81
+ // } else {
82
+ // binaryData = Buffer.from(JSON.stringify(result));
83
+ // }
84
+
85
+ // self.wsManager.sendBinary(socket, binaryData, organization_id);
86
+ // }
87
+ // });
88
+ // } catch (error) {
89
+ // console.log('export error', error);
90
+ // }
91
+ // }
92
+
93
+ // async setImportData(socket, data) {
94
+ // this.importCollection = data['collection']
95
+ // this.importType = data['import_type'];
96
+ // this.importDB = data['namespace'];
97
+ // }
98
+
99
+ // async importData(socket, data) {
100
+ // const importCollection = data['collection']
101
+ // const importType = data['import_type'];
102
+ // const importFile = data['file'];
103
+ // // const importDB = data['namespace'];
104
+
105
+ // console.log('import:', importCollection, importType, importFile)
106
+ // const self = this;
107
+ // // const organization_id = socket.config.organization_id
108
+ // if (!importCollection || !importType) {
109
+ // return;
110
+ // }
111
+ // try {
112
+ // console.log('data: ', data)
113
+ // // return;
114
+ // let jsonData = null;
115
+ // if (this.importType === 'csv') {
116
+ // jsonData = await csvtojson({ignoreEmpty: true}).fromString(data.toString())
117
+ // }
118
+ // else {
119
+ // jsonData = JSON.parse(importFile.toString());
120
+ // }
121
+ // // TODO: validate json / if json is object error happens
122
+ // jsonData.map((item) => delete item._id);
123
+ // console.log('json: ', jsonData)
124
+ // var collection = this.dbClient.db(organization_id).collection(importCollection);
125
+ // // console.log(this.importCollection)
126
+ // collection.insertMany(jsonData, function(err, result) {
127
+ // if (!err) {
128
+ // self.wsManager.send(socket, 'importedFile2DB', {
129
+ // 'database': organization_id,
130
+ // 'collection': importCollection,
131
+ // 'import_type': importType,
132
+ // 'data': result
133
+ // }, organization_id)
134
+ // }
135
+ // })
136
+
137
+ // // this.importCollection = '';
138
+ // // this.importType = '';
139
+
140
+ // } catch (error) {
141
+ // console.log('import db error', error)
142
+ // }
143
+
144
+ // }
145
+
146
+ }
147
+ module.exports = CoCreateBackup;
148
+
149
+
150
+