insomnia/app/backend/database/models/environment.js

55 lines
1.0 KiB
JavaScript
Raw Normal View History

2016-09-21 20:32:45 +00:00
'use strict';
2016-09-22 01:26:40 +00:00
const db = require('../index');
2016-09-21 20:32:45 +00:00
export const type = 'Environment';
export const prefix = 'env';
export function init () {
return db.initModel({
name: 'New Environment',
data: {},
})
}
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) {
2016-09-21 20:32:45 +00:00
const parentId = workspace._id;
const environments = await db.find(type, {parentId});
if (environments.length === 0) {
return await create({
parentId,
name: 'Base Environment'
})
} else {
return environments[0];
}
}
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);
}