insomnia/app/models/environment.js

61 lines
1.1 KiB
JavaScript
Raw Normal View History

2016-11-10 05:56:23 +00:00
import * as db from '../common/database';
2016-09-21 20:32:45 +00:00
export const name = 'Environment';
export const type = 'Environment';
export const prefix = 'env';
export const canDuplicate = true;
export function init () {
2016-11-10 01:15:27 +00:00
return {
name: 'New Environment',
data: {},
isPrivate: false
};
}
export function migrate (doc) {
return doc;
}
export function create (patch = {}) {
2016-09-21 20:32:45 +00:00
if (!patch.parentId) {
throw new Error('New Environment missing `parentId`', patch);
}
return db.docCreate(type, patch);
}
2016-09-21 20:32:45 +00:00
export function update (environment, patch) {
2016-09-21 20:32:45 +00:00
return db.docUpdate(environment, patch);
}
2016-09-21 20:32:45 +00:00
export function findByParentId (parentId) {
return db.find(type, {parentId});
}
2016-09-21 20:32:45 +00:00
export async function getOrCreateForWorkspace (workspace) {
let environment = await db.getWhere(type, {
parentId: workspace._id
});
if (!environment) {
environment = await create({
parentId: workspace._id,
name: 'Base Environment'
});
}
return environment;
}
export function getById (id) {
return db.get(type, id);
}
export function remove (environment) {
2016-09-21 20:32:45 +00:00
return db.remove(environment);
}
2016-09-21 20:32:45 +00:00
export function all () {
return db.all(type);
}