@@ -6,24 +6,18 @@ const path = require('path');
66let config ;
77
88let jsConfig = path . resolve ( process . cwd ( ) , 'CoCreate.config.js' ) ;
9- // let jsonConfig = path.resolve(process.cwd(), 'CoCreate.config.json')
109if ( fs . existsSync ( jsConfig ) )
1110 config = require ( jsConfig ) ;
12- // else if (fs.existsSync(jsonConfig)) {
13- // config = require(jsonConfig)
14- // }
1511else {
16- process . exit ( )
1712 console . log ( 'config not found.' )
13+ process . exit ( )
1814}
1915
2016const { crud, sources, config : socketConfig } = config ;
2117
22- console . log ( config )
23-
18+ // ToDo: throwing error
2419process . env [ 'NODE_TLS_REJECT_UNAUTHORIZED' ] = 0
2520
26- /** init cocreatecrud and socket **/
2721CoCreateCrud . socket . create ( {
2822 organization_id : socketConfig . organization_id ,
2923 apiKey : socketConfig . apiKey ,
@@ -33,14 +27,14 @@ CoCreateCrud.socket.create({
3327const commonParam = {
3428 apiKey : socketConfig . apiKey ,
3529 organization_id : socketConfig . organization_id ,
30+ host : socketConfig . host ,
3631 broadcast : false
3732}
3833
39- async function runStore ( info , type ) {
34+ async function runStore ( info , type ) {
4035 try {
4136 let response = false ;
42- // const event = "docEvent" + Date.now()
43- if ( ! info . document_id ) {
37+ if ( ! info . document . _id ) {
4438 response = await CoCreateCrud . createDocument ( {
4539 ...commonParam ,
4640 ...info ,
@@ -49,12 +43,10 @@ async function runStore (info, type) {
4943 response = await CoCreateCrud . updateDocument ( {
5044 ...commonParam ,
5145 ...info ,
52- upsert : true ,
46+ upsert : true
5347 } )
5448 }
5549 if ( response ) {
56- console . log ( 'type ------------------------- ' , type )
57- console . log ( response )
5850 return response ;
5951 }
6052 } catch ( err ) {
@@ -81,9 +73,13 @@ if (sources) {
8173
8274 async function runSources ( ) {
8375 for ( let i = 0 ; i < sources . length ; i ++ ) {
84- const { entry, collection, document_id, key, data } = sources [ i ] ;
76+ const { entry, collection, document_id, key, document } = sources [ i ] ;
8577
8678 let new_source = { ...sources [ i ] } ;
79+ new_source . document = { _id : '' , ...new_source . document }
80+ new_source . document . _id = ''
81+ delete new_source . _id
82+
8783 let response = { } ;
8884 if ( entry ) {
8985
@@ -99,30 +95,37 @@ if (sources) {
9995 let content = new Buffer . from ( binary ) . toString ( read_type ) ;
10096
10197 if ( content && key && collection ) {
102- if ( ! data ) data = { } ;
103- let storeData = {
104- [ key ] : content ,
105- ...data ,
106- } ;
107-
108- response = await runStore ( { collection, document_id, data : storeData } , 'sources' ) ;
98+ if ( ! document ) document = { } ;
99+ // let storeData = {
100+ // [key]: content,
101+ // ...data,
102+ // };
103+ document [ key ] = content
104+
105+ // ToDo: can be removed once all configs are updated
106+ if ( ! document . _id && document_id )
107+ document . _id = document_id
108+
109+ response = await runStore ( { collection, document} , 'sources' ) ;
109110 }
110111 } catch ( err ) {
111112 console . log ( err )
112113 }
113- if ( response . document_id ) {
114- new_source . document_id = response . document_id
114+ if ( response . document [ 0 ] . _id ) {
115+ delete new_source . document_id
116+ delete new_source . document . src
117+ new_source . document . _id = response . document [ 0 ] . _id
115118 }
116119 }
117120 new_sources_list . push ( new_source )
118121 }
122+
123+
119124 return new_sources_list
120125
121126 }
122127
123128 runSources ( ) . then ( ( data ) => {
124-
125- console . log ( data )
126129 let new_config = {
127130 config : socketConfig ,
128131 sources : new_sources_list ,
@@ -133,13 +136,8 @@ if (sources) {
133136 write_str = "module.exports = " + write_str ;
134137
135138 fs . writeFileSync ( jsConfig , write_str ) ;
136- // fs.writeFileSync(jsonConfig, write_str);
137-
139+ setTimeout ( function ( ) {
140+ process . exit ( )
141+ } , 2000 )
138142 } )
139143}
140-
141- console . log ( 'end....' )
142-
143- setTimeout ( function ( ) {
144- process . exit ( )
145- } , 1000 * 60 )
0 commit comments