Skip to content

Commit dc8d35b

Browse files
committed
Fix seeding Promise issue
1 parent 112ab12 commit dc8d35b

File tree

25 files changed

+534
-435
lines changed

25 files changed

+534
-435
lines changed
Lines changed: 65 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
1-
import { Categories } from '../../modules/categories/index.js';
1+
/* global Vulcan */
2+
import { Promise } from 'meteor/promise';
23
import { Utils, newMutation, getSetting } from 'meteor/vulcan:core';
4+
import { Categories } from '../../modules/categories/index.js';
35

46
if (getSetting('forum.seedOnStart')) {
5-
67
const dummyFlag = {
78
fieldName: 'isDummy',
89
fieldSchema: {
910
type: Boolean,
1011
optional: true,
11-
hidden: true
12-
}
13-
}
12+
hidden: true,
13+
},
14+
};
15+
1416
Categories.addField(dummyFlag);
1517

1618
const dummyCategories = [
@@ -19,71 +21,82 @@ if (getSetting('forum.seedOnStart')) {
1921
description: 'The first test category',
2022
order: 4,
2123
slug: 'testcat1',
22-
isDummy: true
24+
isDummy: true,
2325
},
2426
{
2527
name: 'Test Category 2',
2628
description: 'The second test category',
2729
order: 7,
2830
slug: 'testcat2',
29-
isDummy: true
31+
isDummy: true,
3032
},
3133
{
3234
name: 'Test Category 3',
3335
description: 'The third test category',
3436
order: 10,
3537
slug: 'testcat3',
36-
isDummy: true
37-
}
38+
isDummy: true,
39+
},
3840
];
3941

40-
// Load categories from settings, if there are any
41-
if (Meteor.settings && Meteor.settings.categories) {
42-
Meteor.settings.categories.forEach(category => {
42+
const originalRemoveGettingStartedContent = Vulcan.removeGettingStartedContent;
4343

44-
// get slug (or slugified name)
45-
const slug = category.slug || Utils.slugify(category.name);
44+
Vulcan.removeGettingStartedContent = () => {
45+
if (originalRemoveGettingStartedContent) {
46+
originalRemoveGettingStartedContent();
47+
}
48+
49+
Categories.remove({ isDummy: true });
50+
// eslint-disable-next-line no-console
51+
console.log('// Getting started content removed from seed_categories');
52+
};
4653

47-
// look for existing category with same slug
48-
let existingCategory = Categories.findOne({slug: slug});
54+
Meteor.startup(() => {
55+
// Load categories from settings, if there are any
56+
if (Meteor.settings && Meteor.settings.categories) {
57+
Meteor.settings.categories.forEach(category => {
4958

50-
// look for parent category
51-
if (category.parent) {
52-
const parentCategory = Categories.findOne({slug: category.parent});
53-
if (parentCategory) {
54-
category.parentId = parentCategory._id;
55-
delete category.parent;
59+
// get slug (or slugified name)
60+
const slug = category.slug || Utils.slugify(category.name);
61+
62+
// look for existing category with same slug
63+
const existingCategory = Categories.findOne({slug: slug});
64+
65+
// look for parent category
66+
if (category.parent) {
67+
const parentCategory = Categories.findOne({slug: category.parent});
68+
if (parentCategory) {
69+
category.parentId = parentCategory._id;
70+
delete category.parent;
71+
}
5672
}
57-
}
5873

59-
if (existingCategory) {
60-
// if category exists, update it with settings data except slug
61-
delete category.slug;
62-
Categories.update(existingCategory._id, {$set: category});
63-
} else {
64-
// if not, create it
65-
newMutation({
66-
collection: Categories,
67-
document: category,
68-
validate: false,
69-
});
70-
71-
// Categories.insert(category);
72-
// eslint-disable-next-line no-console
73-
console.log(`// Creating category “${category.name}”`); // eslint-disable-line
74-
}
75-
});
76-
} else if (!Categories.find().count()) {
77-
// eslint-disable-next-line no-console
78-
console.log('// inserting dummy categories');
79-
// else if there are no categories yet, create dummy categories
80-
dummyCategories.forEach(category => {
81-
newMutation({
74+
if (existingCategory) {
75+
// if category exists, update it with settings data except slug
76+
delete category.slug;
77+
Categories.update(existingCategory._id, { $set: category });
78+
} else {
79+
// if not, create it
80+
Promise.await(newMutation({
81+
collection: Categories,
82+
document: category,
83+
validate: false,
84+
}));
85+
86+
// Categories.insert(category);
87+
// eslint-disable-next-line no-console
88+
console.log(`// Creating category “${category.name}”`); // eslint-disable-line
89+
}
90+
});
91+
} else if (!Categories.find().count()) {
92+
// eslint-disable-next-line no-console
93+
console.log('// inserting dummy categories');
94+
// else if there are no categories yet, create dummy categories
95+
Promise.awaitAll(dummyCategories.map(document => newMutation({
8296
collection: Categories,
83-
document: category,
97+
document,
8498
validate: false,
85-
});
86-
});
87-
}
88-
89-
}
99+
})));
100+
}
101+
});
102+
}
Lines changed: 72 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
/* global Vulcan */
2-
import { newMutation, registerSetting, getSetting } from 'meteor/vulcan:core';
32
import moment from 'moment';
3+
import { newMutation, registerSetting, getSetting } from 'meteor/vulcan:core';
4+
import Users from 'meteor/vulcan:users';
5+
import { Promise } from 'meteor/promise';
46
import { Posts } from '../../modules/posts/index.js';
57
import { Comments } from '../../modules/comments/index.js';
6-
import { Categories } from '../../modules/categories/index.js';
7-
import Users from 'meteor/vulcan:users';
88

99
registerSetting('forum.seedOnStart', true, 'Seed the app with dummy content on startup');
1010

@@ -15,9 +15,10 @@ if (getSetting('forum.seedOnStart')) {
1515
fieldSchema: {
1616
type: Boolean,
1717
optional: true,
18-
hidden: true
19-
}
20-
}
18+
hidden: true,
19+
},
20+
};
21+
2122
Users.addField(dummyFlag);
2223
Posts.addField(dummyFlag);
2324
Comments.addField(dummyFlag);
@@ -27,135 +28,130 @@ if (getSetting('forum.seedOnStart')) {
2728
fieldSchema: {
2829
type: String,
2930
optional: true,
30-
hidden: true // never show this
31-
}
31+
hidden: true, // never show this
32+
},
3233
});
3334

34-
var toTitleCase = function (str) {
35+
const toTitleCase = (str) => {
3536
return str.replace(/\w\S*/g, function(txt){return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();});
3637
};
3738

38-
var createPost = function (slug, postedAt, username, thumbnail) {
39-
40-
const user = Users.findOne({username: username});
41-
42-
var post = {
39+
const createPost = async (slug, postedAt, username, thumbnail) => {
40+
const currentUser = await Users.rawCollection().findOne({ username: username });
41+
const document = {
4342
postedAt: postedAt,
4443
body: Assets.getText("lib/assets/content/" + slug + ".md"),
4544
title: toTitleCase(slug.replace(/_/g, ' ')),
4645
dummySlug: slug,
4746
isDummy: true,
48-
userId: user._id
47+
userId: currentUser._id,
4948
};
5049

51-
if (typeof thumbnail !== "undefined")
52-
post.thumbnailUrl = "/packages/example-forum/lib/assets/images/" + thumbnail;
50+
if (typeof thumbnail !== "undefined") {
51+
document.thumbnailUrl = "/packages/example-forum/lib/assets/images/" + thumbnail;
52+
}
5353

54-
newMutation({
54+
return newMutation({
5555
collection: Posts,
56-
document: post,
57-
currentUser: user,
58-
validate: false
56+
document,
57+
currentUser,
58+
validate: false,
5959
});
60-
6160
};
6261

63-
var createComment = function (slug, username, body, parentBody) {
64-
65-
const user = Users.findOne({username: username});
66-
67-
var comment = {
68-
postId: Posts.findOne({dummySlug: slug})._id,
62+
const createComment = async (slug, username, body, parentBody) => {
63+
const user = await Users.rawCollection().findOne({ username: username });
64+
const comment = {
65+
postId: await Posts.rawCollection().findOne({ dummySlug: slug })._id,
6966
userId: user._id,
7067
body: body,
7168
isDummy: true,
72-
disableNotifications: true
69+
disableNotifications: true,
7370
};
74-
var parentComment = Comments.findOne({body: parentBody});
75-
if (parentComment)
71+
const parentComment = await Comments.rawCollection().findOne({ body: parentBody });
72+
73+
if (parentComment) {
7674
comment.parentCommentId = parentComment._id;
75+
}
7776

78-
newMutation({
77+
return newMutation({
7978
collection: Comments,
8079
document: comment,
8180
currentUser: user,
82-
validate: false
81+
validate: false,
8382
});
8483
};
8584

86-
const createUser = function (username, email) {
87-
const user = {
85+
const createUser = async (username, email) => {
86+
const document = {
8887
username,
8988
email,
90-
isDummy: true
89+
isDummy: true,
9190
};
92-
newMutation({
91+
92+
return newMutation({
9393
collection: Users,
94-
document: user,
95-
validate: false
94+
document,
95+
validate: false,
9696
});
97-
}
97+
};
9898

99-
var createDummyUsers = function () {
99+
const createDummyUsers = async () => {
100100
// eslint-disable-next-line no-console
101101
console.log('// inserting dummy users…');
102-
createUser('Bruce', 'dummyuser1@telescopeapp.org');
103-
createUser('Arnold', 'dummyuser2@telescopeapp.org');
104-
createUser('Julia', 'dummyuser3@telescopeapp.org');
102+
return Promise.all([
103+
createUser('Bruce', 'dummyuser1@telescopeapp.org'),
104+
createUser('Arnold', 'dummyuser2@telescopeapp.org'),
105+
createUser('Julia', 'dummyuser3@telescopeapp.org'),
106+
]);
105107
};
106108

107-
var createDummyPosts = function () {
109+
const createDummyPosts = async () => {
108110
// eslint-disable-next-line no-console
109111
console.log('// inserting dummy posts');
110112

111-
createPost("read_this_first", moment().toDate(), "Bruce", "telescope.png");
112-
113-
createPost("deploying", moment().subtract(10, 'minutes').toDate(), "Arnold");
114-
115-
createPost("customizing", moment().subtract(3, 'hours').toDate(), "Julia");
116-
117-
createPost("getting_help", moment().subtract(1, 'days').toDate(), "Bruce", "stackoverflow.png");
118-
119-
createPost("removing_getting_started_posts", moment().subtract(2, 'days').toDate(), "Julia");
120-
113+
return Promise.all([
114+
createPost("read_this_first", moment().toDate(), "Bruce", "telescope.png"),
115+
createPost("deploying", moment().subtract(10, 'minutes').toDate(), "Arnold"),
116+
createPost("customizing", moment().subtract(3, 'hours').toDate(), "Julia"),
117+
createPost("getting_help", moment().subtract(1, 'days').toDate(), "Bruce", "stackoverflow.png"),
118+
createPost("removing_getting_started_posts", moment().subtract(2, 'days').toDate(), "Julia"),
119+
]);
121120
};
122121

123-
var createDummyComments = function () {
122+
const createDummyComments = async () => {
124123
// eslint-disable-next-line no-console
125124
console.log('// inserting dummy comments…');
126125

127-
createComment("read_this_first", "Bruce", "What an awesome app!");
128-
129-
createComment("deploying", "Arnold", "Deploy to da choppah!");
130-
createComment("deploying", "Julia", "Do you really need to say this all the time?", "Deploy to da choppah!");
131-
132-
createComment("customizing", "Julia", "This is really cool!");
133-
134-
createComment("removing_getting_started_posts", "Bruce", "Yippee ki-yay!");
135-
createComment("removing_getting_started_posts", "Arnold", "I'll be back.", "Yippee ki-yay!");
136-
126+
return Promise.all([
127+
createComment("read_this_first", "Bruce", "What an awesome app!"),
128+
createComment("deploying", "Arnold", "Deploy to da choppah!"),
129+
createComment("deploying", "Julia", "Do you really need to say this all the time?", "Deploy to da choppah!"),
130+
createComment("customizing", "Julia", "This is really cool!"),
131+
createComment("removing_getting_started_posts", "Bruce", "Yippee ki-yay!"),
132+
createComment("removing_getting_started_posts", "Arnold", "I'll be back.", "Yippee ki-yay!"),
133+
]);
137134
};
138135

139136
Vulcan.removeGettingStartedContent = () => {
140-
Users.remove({'profile.isDummy': true});
141-
Posts.remove({isDummy: true});
142-
Comments.remove({isDummy: true});
143-
Categories.remove({isDummy: true});
137+
Users.remove({ 'profile.isDummy': true });
138+
Posts.remove({ isDummy: true });
139+
Comments.remove({ isDummy: true });
144140
// eslint-disable-next-line no-console
145-
console.log('// Getting started content removed');
141+
console.log('// Getting started content removed from seed_posts');
146142
};
147143

148-
Meteor.startup(function () {
144+
// Uses Promise.await to await async functions in a Fiber to make them "Meteor synchronous"
145+
Meteor.startup(() => {
149146
// insert dummy content only if createDummyContent hasn't happened and there aren't any posts or users in the db
150147
if (!Users.find().count()) {
151-
createDummyUsers();
148+
Promise.await(createDummyUsers());
152149
}
153150
if (!Posts.find().count()) {
154-
createDummyPosts();
151+
Promise.await(createDummyPosts());
155152
}
156153
if (!Comments.find().count()) {
157-
createDummyComments();
154+
Promise.await(createDummyComments());
158155
}
159156
});
160-
161157
}

packages/example-forum/package.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ Package.onUse(function (api) {
1111

1212
api.use([
1313

14+
'promise',
1415
'fourseven:scss@4.5.0',
1516

1617
// vulcan core

0 commit comments

Comments
 (0)