Skip to content

Commit

Permalink
Removed bluebird from fixture-manager.js (#15629)
Browse files Browse the repository at this point in the history
refs: #14882

- Removing bluebird specific methods in favour of the Ghost sequence method so we can remove the bluebird dependency

Co-authored-by: Hannah Wolfe <github.erisds@gmail.com>
  • Loading branch information
hthorhalls and ErisDS authored Oct 21, 2022
1 parent a79c48e commit 39e246a
Showing 1 changed file with 16 additions and 14 deletions.
30 changes: 16 additions & 14 deletions ghost/core/core/server/data/schema/fixtures/fixture-manager.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
const _ = require('lodash');
const Promise = require('bluebird');
const logging = require('@tryghost/logging');
const {sequence} = require('@tryghost/promise');

Expand Down Expand Up @@ -83,16 +82,16 @@ class FixtureManager {
const userRolesRelation = this.fixtures.relations.find(r => r.from.relation === 'roles');
await this.addFixturesForRelation(userRolesRelation, localOptions);

await Promise.mapSeries(this.fixtures.models.filter(m => !['User', 'Role'].includes(m.name)), (model) => {
await sequence(this.fixtures.models.filter(m => !['User', 'Role'].includes(m.name)).map(model => () => {
logging.info('Model: ' + model.name);

return this.addFixturesForModel(model, localOptions);
});
}));

await Promise.mapSeries(this.fixtures.relations.filter(r => r.from.relation !== 'roles'), (relation) => {
await sequence(this.fixtures.relations.filter(r => r.from.relation !== 'roles').map(relation => () => {
logging.info('Relation: ' + relation.from.model + ' to ' + relation.to.model);
return this.addFixturesForRelation(relation, localOptions);
});
}));
}

/*
Expand Down Expand Up @@ -191,12 +190,15 @@ class FixtureManager {
fetchRelationData(relation, options) {
const fromOptions = _.extend({}, options, {withRelated: [relation.from.relation]});

const props = {
from: models[relation.from.model].findAll(fromOptions),
to: models[relation.to.model].findAll(options)
};
const fromRelations = models[relation.from.model].findAll(fromOptions);
const toRelations = models[relation.to.model].findAll(options);

return Promise.props(props);
return Promise.all([fromRelations, toRelations]).then(([from, to]) => {
return {
from: from,
to: to
};
});
}

/**
Expand All @@ -223,7 +225,7 @@ class FixtureManager {
});
}

const results = await Promise.mapSeries(modelFixture.entries, async (entry) => {
const results = await sequence(modelFixture.entries.map(entry => async () => {
let data = {};

// CASE: if id is specified, only query by id
Expand All @@ -243,7 +245,7 @@ class FixtureManager {
if (!found) {
return models[modelFixture.name].add(entry, options);
}
});
}));

return {expected: modelFixture.entries.length, done: _.compact(results).length};
}
Expand Down Expand Up @@ -308,12 +310,12 @@ class FixtureManager {
}

async removeFixturesForModel(modelFixture, options) {
const results = await Promise.mapSeries(modelFixture.entries, async (entry) => {
const results = await sequence(modelFixture.entries.map(entry => async () => {
const found = models[modelFixture.name].findOne(entry.id ? {id: entry.id} : entry, options);
if (found) {
return models[modelFixture.name].destroy(_.extend(options, {id: found.id}));
}
});
}));

return {expected: modelFixture.entries.length, done: results.length};
}
Expand Down

0 comments on commit 39e246a

Please sign in to comment.