@@ -80,79 +80,62 @@ db.once('open', async () => {
8080
8181 for ( const name in populateTheseCollections ) {
8282 if ( names . includes ( name ) ) {
83- logger . info ( `Dropping ${ name } collection indexes!!!` )
84- await db . collections [ name ] . dropIndexes ( )
85- logger . info ( `Dropping ${ name } collection !!!` )
86- await db . dropCollection ( name )
83+ if ( db . collections [ name ] ) {
84+ logger . info ( `Dropping ${ name } collection indexes!!!` )
85+ await db . collections [ name ] . dropIndexes ( )
86+ logger . info ( `Dropping ${ name } collection !!!` )
87+ await db . dropCollection ( name )
88+ }
8789 }
8890 }
8991
90- if ( ! names . includes ( 'Cve-Id-Range' ) && ! names . includes ( 'Cve-Id' ) && ! names . includes ( 'Cve' ) && ! names . includes ( 'Org' ) && ! names . includes ( 'User' ) && ! names . includes ( 'BaseOrg' ) && ! names . includes ( 'BaseUser' ) ) {
91- // Org
92- await dataUtils . populateCollection (
93- './datadump/pre-population/orgs.json' ,
94- Org , dataUtils . newOrgTransform
95- )
96-
97- // await dataUtils.populateCollection(
98- // './datadump/pre-population/registry-orgs.json',
99- // RegistryOrg
100- // )
101-
102- // User, depends on Org
103- const hash = await dataUtils . preprocessUserSecrets ( )
104- await dataUtils . populateCollection (
105- './datadump/pre-population/users.json' ,
106- User , dataUtils . newUserTransform , hash
107- )
108-
109- // const registryUserHash = await dataUtils.preprocessUserSecrets()
110- // await dataUtils.populateCollection(
111- // './datadump/pre-population/registry-users.json',
112- // RegistryUser, dataUtils.newRegistryUserTransform, registryUserHash
113- // )
114-
115- const populatePromises = [ ]
116-
117- // CVE ID Range
92+ // Org
93+ await dataUtils . populateCollection (
94+ './datadump/pre-population/orgs.json' ,
95+ Org , dataUtils . newOrgTransform
96+ )
97+
98+ // User, depends on Org
99+ const hash = await dataUtils . preprocessUserSecrets ( )
100+ await dataUtils . populateCollection (
101+ './datadump/pre-population/users.json' ,
102+ User , dataUtils . newUserTransform , hash
103+ )
104+
105+ const populatePromises = [ ]
106+
107+ // CVE ID Range
108+ populatePromises . push ( dataUtils . populateCollection (
109+ './datadump/pre-population/cve-ids-range.json' ,
110+ CveIdRange
111+ ) )
112+
113+ // CVE
114+ if ( process . env . NODE_ENV === 'development' || process . env . NODE_ENV === 'test' ) {
118115 populatePromises . push ( dataUtils . populateCollection (
119- './datadump/pre-population/cve-ids-range .json' ,
120- CveIdRange
116+ './datadump/pre-population/cves .json' ,
117+ Cve , dataUtils . newCveTransform
121118 ) )
119+ }
122120
123- // CVE
124- if ( process . env . NODE_ENV === 'development' || process . env . NODE_ENV === 'test' ) {
125- populatePromises . push ( dataUtils . populateCollection (
126- './datadump/pre-population/cves.json' ,
127- Cve , dataUtils . newCveTransform
128- ) )
129- }
121+ // CVE ID, depends on User and Org
122+ populatePromises . push ( dataUtils . populateCollection (
123+ './datadump/pre-population/cve-ids.json' ,
124+ CveId , dataUtils . newCveIdTransform
125+ ) )
130126
131- // CVE ID, depends on User and Org
132- populatePromises . push ( dataUtils . populateCollection (
133- './datadump/pre-population/cve-ids.json' ,
134- CveId , dataUtils . newCveIdTransform
135- ) )
127+ // don't close database connection until all remaining populate
128+ // promises are resolved
129+ Promise . all ( populatePromises ) . then ( function ( ) {
130+ logger . info ( 'Successfully populated the database!' )
136131
137- // don't close database connection until all remaining populate
138- // promises are resolved
139- Promise . all ( populatePromises ) . then ( function ( ) {
140- logger . info ( 'Successfully populated the database!' )
141-
142- Object . keys ( indexesToCreate ) . forEach ( col => {
143- indexesToCreate [ col ] . forEach ( index => {
144- db . collections [ col ] . createIndex ( index )
145- } )
132+ Object . keys ( indexesToCreate ) . forEach ( col => {
133+ indexesToCreate [ col ] . forEach ( index => {
134+ db . collections [ col ] . createIndex ( index )
146135 } )
147- mongoose . connection . close ( )
148136 } )
149- } else {
150- logger . error (
151- 'The database was not populated because ' +
152- 'some of the collections were not deleted.'
153- )
154137 mongoose . connection . close ( )
155- }
138+ } )
156139 } else {
157140 mongoose . connection . close ( )
158141 }
0 commit comments