2017-07-27 22:59:07 +00:00
|
|
|
// @flow
|
2018-06-25 17:42:50 +00:00
|
|
|
import type { BaseModel } from './index';
|
2016-12-01 01:50:59 +00:00
|
|
|
import * as db from '../common/database';
|
2018-06-25 17:42:50 +00:00
|
|
|
import {
|
|
|
|
DEFAULT_SIDEBAR_WIDTH,
|
|
|
|
DEFAULT_PANE_WIDTH,
|
2018-12-12 17:36:11 +00:00
|
|
|
DEFAULT_PANE_HEIGHT,
|
2018-06-25 17:42:50 +00:00
|
|
|
} from '../common/constants';
|
2016-12-01 01:50:59 +00:00
|
|
|
|
|
|
|
export const name = 'Workspace Meta';
|
|
|
|
export const type = 'WorkspaceMeta';
|
|
|
|
export const prefix = 'wrkm';
|
2017-06-15 17:08:24 +00:00
|
|
|
export const canDuplicate = false;
|
2019-04-18 00:50:03 +00:00
|
|
|
export const canSync = false;
|
2016-12-01 01:50:59 +00:00
|
|
|
|
2017-07-27 22:59:07 +00:00
|
|
|
type BaseWorkspaceMeta = {
|
|
|
|
activeRequestId: string | null,
|
|
|
|
activeEnvironmentId: string | null,
|
|
|
|
sidebarFilter: string,
|
|
|
|
sidebarHidden: boolean,
|
|
|
|
sidebarWidth: number,
|
|
|
|
paneWidth: number,
|
|
|
|
paneHeight: number,
|
2018-12-12 17:36:11 +00:00
|
|
|
hasSeen: boolean,
|
2017-07-27 22:59:07 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export type WorkspaceMeta = BaseWorkspaceMeta & BaseModel;
|
|
|
|
|
2018-06-25 17:42:50 +00:00
|
|
|
export function init(): BaseWorkspaceMeta {
|
2016-12-01 01:50:59 +00:00
|
|
|
return {
|
|
|
|
parentId: null,
|
|
|
|
activeRequestId: null,
|
|
|
|
activeEnvironmentId: null,
|
|
|
|
sidebarFilter: '',
|
|
|
|
sidebarHidden: false,
|
|
|
|
sidebarWidth: DEFAULT_SIDEBAR_WIDTH,
|
2017-03-28 22:45:23 +00:00
|
|
|
paneWidth: DEFAULT_PANE_WIDTH,
|
2017-07-27 22:59:07 +00:00
|
|
|
paneHeight: DEFAULT_PANE_HEIGHT,
|
2018-12-12 17:36:11 +00:00
|
|
|
hasSeen: true,
|
2017-03-03 20:09:08 +00:00
|
|
|
};
|
2016-12-01 01:50:59 +00:00
|
|
|
}
|
|
|
|
|
2018-06-25 17:42:50 +00:00
|
|
|
export function migrate(doc: WorkspaceMeta): WorkspaceMeta {
|
2016-12-01 01:50:59 +00:00
|
|
|
return doc;
|
|
|
|
}
|
|
|
|
|
2018-06-25 17:42:50 +00:00
|
|
|
export function create(patch: Object = {}): Promise<WorkspaceMeta> {
|
2016-12-01 01:50:59 +00:00
|
|
|
if (!patch.parentId) {
|
2018-10-17 16:42:33 +00:00
|
|
|
throw new Error(`New WorkspaceMeta missing parentId ${JSON.stringify(patch)}`);
|
2016-12-01 01:50:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return db.docCreate(type, patch);
|
|
|
|
}
|
|
|
|
|
2018-10-17 16:42:33 +00:00
|
|
|
export function update(workspaceMeta: WorkspaceMeta, patch: Object = {}): Promise<WorkspaceMeta> {
|
2016-12-01 01:50:59 +00:00
|
|
|
return db.docUpdate(workspaceMeta, patch);
|
|
|
|
}
|
|
|
|
|
2018-10-17 16:42:33 +00:00
|
|
|
export async function getByParentId(parentId: string): Promise<WorkspaceMeta | null> {
|
2018-06-25 17:42:50 +00:00
|
|
|
return db.getWhere(type, { parentId });
|
2017-11-01 11:23:22 +00:00
|
|
|
}
|
|
|
|
|
2018-10-17 16:42:33 +00:00
|
|
|
export async function getOrCreateByParentId(parentId: string): Promise<WorkspaceMeta> {
|
2017-11-01 11:23:22 +00:00
|
|
|
const doc = await getByParentId(parentId);
|
2018-06-25 17:42:50 +00:00
|
|
|
return doc || this.create({ parentId });
|
2016-12-01 01:50:59 +00:00
|
|
|
}
|
|
|
|
|
2018-06-25 17:42:50 +00:00
|
|
|
export function all(): Promise<Array<WorkspaceMeta>> {
|
2016-12-01 01:50:59 +00:00
|
|
|
return db.all(type);
|
|
|
|
}
|