Skip to content

Commit af9cdb6

Browse files
nstuyvesantAwk34
authored andcommitted
fix (app): prevent excess sync() calls during db seed (#2482)
* fix app: prevent excess sync()s when seeding db Change app.js to seed database in promise chain before startServer, move seeding conditional to seed.js and wrap in function. * fix (app): prevent excess sync() calls during db seed Previously, .sync() was being called once in server/config/seed.js and once during sqldb.sequelize.sync() in server/app.js. As a result, duplicate queries were being sent to the database if the tables needed to be defined causing an error when it tries to define constraints a second time (like for an identity column). Seed.js is wrapped in a function and contains the conditional to determine whether to seed the database. This makes it easy to include in the promise chain under sqldb.sequelize.sync(). * fix (app): prevent excess sync() calls during seed Previously, .sync() was being called once in server/config/seed.js and once during sqldb.sequelize.sync() in server/app.js. As a result, duplicate queries were being sent to the database if the tables needed to be defined causing an error when it tries to define constraints a second time (like for an identity column). Seed.js is wrapped in a function and contains the conditional to determine whether to seed the database. This makes it easy to include in the promise chain under sqldb.sequelize.sync().
1 parent d3133df commit af9cdb6

File tree

2 files changed

+69
-68
lines changed

2 files changed

+69
-68
lines changed

Diff for: templates/app/server/app.js

+5-7
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,14 @@ mongoose.Promise = require('bluebird');<% } %><% if (filters.sequelize) { %>
1010
import sqldb from './sqldb';<% } %>
1111
import config from './config/environment';
1212
import http from 'http';
13+
<% if(filters.models) { %>import seedDatabaseIfNeeded from './config/seed';<% } %>
1314
<% if (filters.mongoose) { %>
1415
// Connect to MongoDB
1516
mongoose.connect(config.mongo.uri, config.mongo.options);
1617
mongoose.connection.on('error', function(err) {
1718
console.error('MongoDB connection error: ' + err);
1819
process.exit(-1); // eslint-disable-line no-process-exit
1920
});
20-
<% } %><% if(filters.models) { %>
21-
// Populate databases with sample data
22-
if(config.seedDB) {
23-
require('./config/seed');
24-
}
2521
<% } %>
2622
// Setup server
2723
var app = express();
@@ -41,12 +37,14 @@ function startServer() {
4137
});
4238
}
4339
<% if(filters.sequelize) { %>
44-
sqldb.sequelize.sync()
40+
sqldb.sequelize.sync()<% if(filters.models) { %>
41+
.then(seedDatabaseIfNeeded)<% } %>
4542
.then(startServer)
4643
.catch(function(err) {
4744
console.log('Server failed to start due to error: %s', err);
4845
});
49-
<% } else { %>
46+
<% } else { %><% if(filters.models) { %>
47+
seedDatabaseIfNeeded();<% } %>
5048
setImmediate(startServer);
5149
<% } %>
5250
// Expose app

Diff for: templates/app/server/config/seed(models).js

+64-61
Original file line numberDiff line numberDiff line change
@@ -6,67 +6,70 @@
66
'use strict';<% if (filters.mongooseModels) { %>
77
import Thing from '../api/thing/thing.model';<% if (filters.auth) { %>
88
import User from '../api/user/user.model';<% } %><% } %><% if (filters.sequelizeModels) { %>
9-
import sqldb from '../sqldb';
10-
var Thing = sqldb.Thing;<% if (filters.auth) { %>
11-
var User = sqldb.User;<% } %><% } %>
9+
import sqldb from '../sqldb';<% } %>
10+
import config from './environment/';
1211

13-
<% if (filters.mongooseModels) { %>Thing.find({}).remove()<% }
14-
if (filters.sequelizeModels) { %>Thing.sync()
15-
.then(() => {
16-
return Thing.destroy({ where: {} });
17-
})<% } %>
18-
.then(() => {
19-
<% if (filters.mongooseModels) { %>Thing.create({<% }
20-
if (filters.sequelizeModels) { %>Thing.bulkCreate([{<% } %>
21-
name: 'Development Tools',
22-
info: 'Integration with popular tools such as Webpack, Gulp, Babel, TypeScript, Karma, '
23-
+ 'Mocha, ESLint, Node Inspector, Livereload, Protractor, Pug, '
24-
+ 'Stylus, Sass, and Less.'
25-
}, {
26-
name: 'Server and Client integration',
27-
info: 'Built with a powerful and fun stack: MongoDB, Express, '
28-
+ 'AngularJS, and Node.'
29-
}, {
30-
name: 'Smart Build System',
31-
info: 'Build system ignores `spec` files, allowing you to keep '
32-
+ 'tests alongside code. Automatic injection of scripts and '
33-
+ 'styles into your index.html'
34-
}, {
35-
name: 'Modular Structure',
36-
info: 'Best practice client and server structures allow for more '
37-
+ 'code reusability and maximum scalability'
38-
}, {
39-
name: 'Optimized Build',
40-
info: 'Build process packs up your templates as a single JavaScript '
41-
+ 'payload, minifies your scripts/css/images, and rewrites asset '
42-
+ 'names for caching.'
43-
}, {
44-
name: 'Deployment Ready',
45-
info: 'Easily deploy your app to Heroku or Openshift with the heroku '
46-
+ 'and openshift subgenerators'
47-
<% if (filters.mongooseModels) { %>});<% }
48-
if (filters.sequelizeModels) { %>}]);<% } %>
49-
});
12+
export default function seedDatabaseIfNeeded() {
13+
if(config.seedDB) {
14+
<% if (filters.sequelizeModels) { %>let Thing = sqldb.Thing;<% if (filters.auth) { %>
15+
let User = sqldb.User;<% } %><% } %>
16+
17+
<% if (filters.mongooseModels) { %>Thing.find({}).remove()<% }
18+
if (filters.sequelizeModels) { %>return Thing.destroy({ where: {} })<% } %>
19+
.then(() => {
20+
<% if (filters.mongooseModels) { %>Thing.create({<% }
21+
if (filters.sequelizeModels) { %>return Thing.bulkCreate([{<% } %>
22+
name: 'Development Tools',
23+
info: 'Integration with popular tools such as Webpack, Gulp, Babel, TypeScript, Karma, '
24+
+ 'Mocha, ESLint, Node Inspector, Livereload, Protractor, Pug, '
25+
+ 'Stylus, Sass, and Less.'
26+
}, {
27+
name: 'Server and Client integration',
28+
info: 'Built with a powerful and fun stack: MongoDB, Express, '
29+
+ 'AngularJS, and Node.'
30+
}, {
31+
name: 'Smart Build System',
32+
info: 'Build system ignores `spec` files, allowing you to keep '
33+
+ 'tests alongside code. Automatic injection of scripts and '
34+
+ 'styles into your index.html'
35+
}, {
36+
name: 'Modular Structure',
37+
info: 'Best practice client and server structures allow for more '
38+
+ 'code reusability and maximum scalability'
39+
}, {
40+
name: 'Optimized Build',
41+
info: 'Build process packs up your templates as a single JavaScript '
42+
+ 'payload, minifies your scripts/css/images, and rewrites asset '
43+
+ 'names for caching.'
44+
}, {
45+
name: 'Deployment Ready',
46+
info: 'Easily deploy your app to Heroku or Openshift with the heroku '
47+
+ 'and openshift subgenerators'
48+
<% if (filters.mongooseModels) { %>});<% }
49+
if (filters.sequelizeModels) { %>}]);<% } %>
50+
})
51+
.then(() => console.log('finished populating things'))
52+
.catch(err => console.log('error populating things', err));
5053
<% if (filters.auth) { %>
51-
<% if (filters.mongooseModels) { %>User.find({}).remove()<% }
52-
if (filters.sequelizeModels) { %>User.sync()
53-
.then(() => User.destroy({ where: {} }))<% } %>
54-
.then(() => {
55-
<% if (filters.mongooseModels) { %>User.create({<% }
56-
if (filters.sequelizeModels) { %>User.bulkCreate([{<% } %>
57-
provider: 'local',
58-
name: 'Test User',
59-
60-
password: 'test'
61-
}, {
62-
provider: 'local',
63-
role: 'admin',
64-
name: 'Admin',
65-
66-
password: 'admin'
67-
<% if (filters.mongooseModels) { %>})<% }
68-
if (filters.sequelizeModels) { %>}])<% } %>
69-
.then(() => {
70-
console.log('finished populating users');
54+
<% if (filters.mongooseModels) { %>User.find({}).remove()<% }
55+
if (filters.sequelizeModels) { %>User.destroy({ where: {} })<% } %>
56+
.then(() => {
57+
<% if (filters.mongooseModels) { %>User.create({<% }
58+
if (filters.sequelizeModels) { %>return User.bulkCreate([{<% } %>
59+
provider: 'local',
60+
name: 'Test User',
61+
62+
password: 'test'
63+
}, {
64+
provider: 'local',
65+
role: 'admin',
66+
name: 'Admin',
67+
68+
password: 'admin'
69+
<% if (filters.mongooseModels) { %>})<% }
70+
if (filters.sequelizeModels) { %>}])<% } %>
71+
.then(() => console.log('finished populating users'))
72+
.catch(err => console.log('error populating users', err));<% } %>
7173
});
72-
});<% } %>
74+
}
75+
}

0 commit comments

Comments
 (0)