2016-11-10 05:56:23 +00:00
|
|
|
import * as db from '../common/database';
|
2016-09-21 20:32:45 +00:00
|
|
|
|
2016-11-22 19:42:10 +00:00
|
|
|
export const name = 'Environment';
|
2016-10-02 20:57:00 +00:00
|
|
|
export const type = 'Environment';
|
|
|
|
export const prefix = 'env';
|
2017-03-23 22:10:42 +00:00
|
|
|
export const canDuplicate = true;
|
2016-10-02 20:57:00 +00:00
|
|
|
export function init () {
|
2016-11-10 01:15:27 +00:00
|
|
|
return {
|
2016-10-02 20:57:00 +00:00
|
|
|
name: 'New Environment',
|
|
|
|
data: {},
|
2017-03-03 20:09:08 +00:00
|
|
|
isPrivate: false
|
|
|
|
};
|
2016-10-02 20:57:00 +00:00
|
|
|
}
|
|
|
|
|
2016-11-22 19:42:10 +00:00
|
|
|
export function migrate (doc) {
|
|
|
|
return doc;
|
|
|
|
}
|
|
|
|
|
2016-10-02 20:57:00 +00:00
|
|
|
export function create (patch = {}) {
|
2016-09-21 20:32:45 +00:00
|
|
|
if (!patch.parentId) {
|
|
|
|
throw new Error('New Environment missing `parentId`', patch);
|
|
|
|
}
|
|
|
|
|
2016-10-02 20:57:00 +00:00
|
|
|
return db.docCreate(type, patch);
|
|
|
|
}
|
2016-09-21 20:32:45 +00:00
|
|
|
|
2016-10-02 20:57:00 +00:00
|
|
|
export function update (environment, patch) {
|
2016-09-21 20:32:45 +00:00
|
|
|
return db.docUpdate(environment, patch);
|
2016-10-02 20:57:00 +00:00
|
|
|
}
|
2016-09-21 20:32:45 +00:00
|
|
|
|
2016-10-02 20:57:00 +00:00
|
|
|
export function findByParentId (parentId) {
|
|
|
|
return db.find(type, {parentId});
|
|
|
|
}
|
2016-09-21 20:32:45 +00:00
|
|
|
|
2016-10-02 20:57:00 +00:00
|
|
|
export async function getOrCreateForWorkspace (workspace) {
|
2016-09-21 20:32:45 +00:00
|
|
|
const parentId = workspace._id;
|
2016-10-02 20:57:00 +00:00
|
|
|
const environments = await db.find(type, {parentId});
|
|
|
|
|
|
|
|
if (environments.length === 0) {
|
|
|
|
return await create({
|
|
|
|
parentId,
|
|
|
|
name: 'Base Environment'
|
2017-03-03 20:09:08 +00:00
|
|
|
});
|
2016-10-02 20:57:00 +00:00
|
|
|
} 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-10-02 20:57:00 +00:00
|
|
|
}
|
2016-09-21 20:32:45 +00:00
|
|
|
|
2016-10-02 20:57:00 +00:00
|
|
|
export function all () {
|
|
|
|
return db.all(type);
|
|
|
|
}
|