Skip to content

Commit

Permalink
feat(apis, script): set restore & purge db ✨
Browse files Browse the repository at this point in the history
  • Loading branch information
PierreBrisorgueil committed Jun 23, 2020
1 parent 8a4cd99 commit d530a4f
Show file tree
Hide file tree
Showing 12 changed files with 43 additions and 10 deletions.
30 changes: 26 additions & 4 deletions modules/apis/repositories/apis.repository.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,27 @@ exports.update = (api) => new Api(api).save().then((a) => a.populate(defaultPopu
*/
exports.delete = (api) => Api.deleteOne({ _id: api.id }).exec();

/**
* @desc Function to import list of apis in db
* @param {[Object]} apis
* @param {[String]} filters
* @return {Object} apis
*/
exports.import = (apis, filters) => Api.bulkWrite(apis.map((api) => {
const filter = {};
filters.forEach((value) => {
filter[value] = api[value];
});
return {
updateOne: {
filter,
update: api,
upsert: true,
},
};
}));


/**
* @desc Function to update scrap history in db
* @param {Object} scrap
Expand All @@ -72,11 +93,12 @@ exports.historize = (api, history) => Api.updateOne(
);

/**
* @desc Function to import list of locations in db
* @param {Object} locations
* @return {Object} locations
* @desc Function to insert list of data in db
* @param {Object} collection
* @param {Object} items
* @return {Object} updateOne result
*/
exports.import = (collection, items) => {
exports.insert = (collection, items) => {
const _schema = new mongoose.Schema({}, { collection, strict: false });
let model;
try {
Expand Down
15 changes: 15 additions & 0 deletions modules/apis/services/apis.data.service.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/**
* Module dependencies
*/
const ApisRepository = require('../repositories/apis.repository');

/**
* @desc Function to ask repository to import a list of apis
* @param {[Object]} apis
* @param {[String]} filters
* @return {Promise} apis
*/
exports.import = (apis, filters) => {
const result = ApisRepository.import(apis, filters);
return result;
};
4 changes: 2 additions & 2 deletions modules/apis/services/apis.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ exports.load = async (api, user) => {
result.prepare = result.temp;
result.temp = montaineSave.save(result.temp, start);
result.mongo = result.temp;
if (api.savedb) result.result = await ApisRepository.import(api.slug, _.cloneDeep(result.temp));
if (api.savedb) result.result = await ApisRepository.insert(api.slug, _.cloneDeep(result.temp));
delete result.temp;
}

Expand Down Expand Up @@ -203,7 +203,7 @@ exports.workerAuto = async (api, body) => {
if (result.temp) {
result.temp = montaineSave.prepare(result.temp, start);
result.temp = montaineSave.save(result.temp, start);
if (api.savedb) result.result = await ApisRepository.import(api.slug, _.cloneDeep(result.temp));
if (api.savedb) result.result = await ApisRepository.insert(api.slug, _.cloneDeep(result.temp));
}

// historize
Expand Down
Binary file modified scripts/db/dump/MontaineNodeDev/apis.bson
Binary file not shown.
Binary file removed scripts/db/dump/MontaineNodeDev/histories.bson
Binary file not shown.
1 change: 0 additions & 1 deletion scripts/db/dump/MontaineNodeDev/histories.metadata.json

This file was deleted.

Binary file removed scripts/db/dump/MontaineNodeDev/mareeInfo.bson
Binary file not shown.
1 change: 0 additions & 1 deletion scripts/db/dump/MontaineNodeDev/mareeInfo.metadata.json

This file was deleted.

Binary file removed scripts/db/dump/MontaineNodeDev/meteoFrance.bson
Binary file not shown.
1 change: 0 additions & 1 deletion scripts/db/dump/MontaineNodeDev/meteoFrance.metadata.json

This file was deleted.

Empty file.

This file was deleted.

0 comments on commit d530a4f

Please sign in to comment.