insomnia/app/models/environment.js

81 lines
1.8 KiB
JavaScript
Raw Normal View History

// @flow
2016-11-10 05:56:23 +00:00
import * as db from '../common/database';
import type {BaseModel} from './index';
import type {Workspace} from './workspace';
2016-09-21 20:32:45 +00:00
export const name = 'Environment';
export const type = 'Environment';
export const prefix = 'env';
export const canDuplicate = true;
type BaseEnvironment = {
name: string,
data: Object,
color: string | null,
// For sync control
isPrivate: boolean
};
export type Environment = BaseModel & BaseEnvironment;
export function init () {
2016-11-10 01:15:27 +00:00
return {
name: 'New Environment',
data: {},
color: null,
isPrivate: false
};
}
export function migrate (doc: Environment): Environment {
return doc;
}
export function create (patch: Object = {}): Promise<Environment> {
2016-09-21 20:32:45 +00:00
if (!patch.parentId) {
throw new Error(`New Environment missing \`parentId\`: ${JSON.stringify(patch)}`);
2016-09-21 20:32:45 +00:00
}
return db.docCreate(type, patch);
}
2016-09-21 20:32:45 +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-09-21 20:32:45 +00:00
export function findByParentId (parentId: string): Promise<Array<Environment>> {
return db.find(type, {parentId});
}
2016-09-21 20:32:45 +00:00
export async function getOrCreateForWorkspaceId (workspaceId: string): Promise<Environment> {
let environment = await db.getWhere(type, {
2017-09-13 06:11:49 +00:00
parentId: workspaceId
});
if (!environment) {
environment = await create({
2017-09-13 06:11:49 +00:00
parentId: workspaceId,
name: 'Base Environment'
});
}
return environment;
}
export async function getOrCreateForWorkspace (workspace: Workspace): Promise<Environment> {
2017-09-13 06:11:49 +00:00
return getOrCreateForWorkspaceId(workspace._id);
}
export function getById (id: string): Promise<Environment | null> {
return db.get(type, id);
}
export function remove (environment: Environment): Promise<void> {
2016-09-21 20:32:45 +00:00
return db.remove(environment);
}
2016-09-21 20:32:45 +00:00
export function all (): Promise<Array<Environment>> {
return db.all(type);
}