insomnia/app/backend/database/environment.js

53 lines
1.2 KiB
JavaScript
Raw Normal View History

2016-09-21 20:32:45 +00:00
'use strict';
const db = require('./');
module.exports.type = 'Environment';
module.exports.prefix = 'env';
module.exports.init = () => ({
name: 'New Environment',
data: {},
});
module.exports.create = (patch = {}) => {
if (!patch.parentId) {
throw new Error('New Environment missing `parentId`', patch);
}
return db.docCreate(module.exports.type, patch);
};
module.exports.update = (environment, patch) => {
return db.docUpdate(environment, patch);
};
module.exports.findByParentId = parentId => {
return db.find(module.exports.type, {parentId});
};
module.exports.getOrCreateForWorkspace = workspace => {
const parentId = workspace._id;
return db.find(module.exports.type, {parentId}).then(environments => {
if (environments.length === 0) {
return module.exports.create({
parentId,
name: 'Base Environment'
})
} else {
return new Promise(resolve => resolve(environments[0]));
}
});
};
module.exports.getById = id => {
return db.get(module.exports.type, id);
};
module.exports.remove = environment => {
return db.remove(environment);
};
module.exports.all = () => {
return db.all(module.exports.type);
};