2017-09-25 21:32:58 +00:00
|
|
|
// @flow
|
2016-11-10 05:56:23 +00:00
|
|
|
import * as db from '../common/database';
|
2017-09-25 21:32:58 +00:00
|
|
|
import type {BaseModel} from './index';
|
|
|
|
import type {Workspace} from './workspace';
|
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;
|
2017-09-25 21:32:58 +00:00
|
|
|
|
|
|
|
type BaseEnvironment = {
|
|
|
|
name: string,
|
|
|
|
data: Object,
|
|
|
|
color: string | null,
|
2017-10-31 18:05:35 +00:00
|
|
|
|
|
|
|
// For sync control
|
2017-09-25 21:32:58 +00:00
|
|
|
isPrivate: boolean
|
|
|
|
};
|
|
|
|
|
|
|
|
export type Environment = BaseModel & BaseEnvironment;
|
|
|
|
|
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-06-27 21:44:23 +00:00
|
|
|
color: null,
|
2017-03-03 20:09:08 +00:00
|
|
|
isPrivate: false
|
|
|
|
};
|
2016-10-02 20:57:00 +00:00
|
|
|
}
|
|
|
|
|
2017-09-25 21:32:58 +00:00
|
|
|
export function migrate (doc: Environment): Environment {
|
2016-11-22 19:42:10 +00:00
|
|
|
return doc;
|
|
|
|
}
|
|
|
|
|
2017-09-25 21:32:58 +00:00
|
|
|
export function create (patch: Object = {}): Promise<Environment> {
|
2016-09-21 20:32:45 +00:00
|
|
|
if (!patch.parentId) {
|
2017-09-25 21:32:58 +00:00
|
|
|
throw new Error(`New Environment missing \`parentId\`: ${JSON.stringify(patch)}`);
|
2016-09-21 20:32:45 +00:00
|
|
|
}
|
|
|
|
|
2016-10-02 20:57:00 +00:00
|
|
|
return db.docCreate(type, patch);
|
|
|
|
}
|
2016-09-21 20:32:45 +00:00
|
|
|
|
2017-09-25 21:32:58 +00:00
|
|
|
export function update (environment: Environment, patch: Object): Promise<Environment> {
|
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
|
|
|
|
2017-09-25 21:32:58 +00:00
|
|
|
export function findByParentId (parentId: string): Promise<Array<Environment>> {
|
2016-10-02 20:57:00 +00:00
|
|
|
return db.find(type, {parentId});
|
|
|
|
}
|
2016-09-21 20:32:45 +00:00
|
|
|
|
2017-09-25 21:32:58 +00:00
|
|
|
export async function getOrCreateForWorkspaceId (workspaceId: string): Promise<Environment> {
|
2017-11-13 19:05:46 +00:00
|
|
|
const environments = await db.find(type, {parentId: workspaceId});
|
2016-10-02 20:57:00 +00:00
|
|
|
|
2017-11-13 19:05:46 +00:00
|
|
|
if (!environments.length) {
|
2017-11-20 16:07:36 +00:00
|
|
|
return create({
|
2017-09-13 06:11:49 +00:00
|
|
|
parentId: workspaceId,
|
2016-10-02 20:57:00 +00:00
|
|
|
name: 'Base Environment'
|
2017-03-03 20:09:08 +00:00
|
|
|
});
|
2016-10-02 20:57:00 +00:00
|
|
|
}
|
2017-03-30 18:52:07 +00:00
|
|
|
|
2017-11-13 19:05:46 +00:00
|
|
|
return environments[environments.length - 1];
|
2016-10-02 20:57:00 +00:00
|
|
|
}
|
|
|
|
|
2017-09-25 21:32:58 +00:00
|
|
|
export async function getOrCreateForWorkspace (workspace: Workspace): Promise<Environment> {
|
2017-09-13 06:11:49 +00:00
|
|
|
return getOrCreateForWorkspaceId(workspace._id);
|
|
|
|
}
|
|
|
|
|
2017-09-25 21:32:58 +00:00
|
|
|
export function getById (id: string): Promise<Environment | null> {
|
2016-10-02 20:57:00 +00:00
|
|
|
return db.get(type, id);
|
|
|
|
}
|
|
|
|
|
2017-09-25 21:32:58 +00:00
|
|
|
export function remove (environment: Environment): Promise<void> {
|
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
|
|
|
|
2017-09-25 21:32:58 +00:00
|
|
|
export function all (): Promise<Array<Environment>> {
|
2016-10-02 20:57:00 +00:00
|
|
|
return db.all(type);
|
|
|
|
}
|