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,
|
2020-04-26 20:33:39 +00:00
|
|
|
activeActivity: string | null,
|
2017-07-27 22:59:07 +00:00
|
|
|
sidebarFilter: string,
|
|
|
|
sidebarHidden: boolean,
|
2020-04-26 20:33:39 +00:00
|
|
|
previewHidden: boolean,
|
2017-07-27 22:59:07 +00:00
|
|
|
sidebarWidth: number,
|
|
|
|
paneWidth: number,
|
|
|
|
paneHeight: number,
|
2018-12-12 17:36:11 +00:00
|
|
|
hasSeen: boolean,
|
2020-04-26 20:33:39 +00:00
|
|
|
gitRepositoryId: string | null,
|
|
|
|
cachedGitRepositoryBranch: string | null,
|
|
|
|
cachedGitLastAuthor: string | null,
|
|
|
|
cachedGitLastCommitTime: number | null,
|
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,
|
2020-04-26 20:33:39 +00:00
|
|
|
activeActivity: null,
|
2016-12-01 01:50:59 +00:00
|
|
|
sidebarFilter: '',
|
|
|
|
sidebarHidden: false,
|
2020-04-26 20:33:39 +00:00
|
|
|
previewHidden: false,
|
2016-12-01 01:50:59 +00:00
|
|
|
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,
|
2020-04-26 20:33:39 +00:00
|
|
|
gitRepositoryId: null,
|
|
|
|
cachedGitRepositoryBranch: null,
|
|
|
|
cachedGitLastAuthor: null,
|
|
|
|
cachedGitLastCommitTime: null,
|
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;
|
|
|
|
}
|
|
|
|
|
2020-03-18 18:58:57 +00:00
|
|
|
export function create(patch: $Shape<WorkspaceMeta> = {}): 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);
|
|
|
|
}
|
|
|
|
|
2020-03-18 18:58:57 +00:00
|
|
|
export function update(
|
|
|
|
workspaceMeta: WorkspaceMeta,
|
|
|
|
patch: $Shape<WorkspaceMeta> = {},
|
|
|
|
): Promise<WorkspaceMeta> {
|
2016-12-01 01:50:59 +00:00
|
|
|
return db.docUpdate(workspaceMeta, patch);
|
|
|
|
}
|
|
|
|
|
2020-05-14 22:54:07 +00:00
|
|
|
export async function updateByParentId(
|
|
|
|
workspaceId: string,
|
|
|
|
patch: Object = {},
|
|
|
|
): Promise<WorkspaceMeta> {
|
2020-04-26 20:33:39 +00:00
|
|
|
const meta = await getByParentId(workspaceId);
|
|
|
|
return db.docUpdate(meta, 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
|
|
|
}
|
|
|
|
|
2020-04-26 20:33:39 +00:00
|
|
|
export async function getByGitRepositoryId(gitRepositoryId: string): Promise<WorkspaceMeta | null> {
|
|
|
|
return db.getWhere(type, { gitRepositoryId });
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
}
|