insomnia/packages/insomnia-app/app/models/workspace-meta.js

79 lines
1.8 KiB
JavaScript
Raw Normal View History

2017-07-27 22:59:07 +00:00
// @flow
2018-06-25 17:42:50 +00:00
import type { BaseModel } from './index';
import * as db from '../common/database';
2018-06-25 17:42:50 +00:00
import {
DEFAULT_SIDEBAR_WIDTH,
DEFAULT_PANE_WIDTH,
DEFAULT_PANE_HEIGHT
} from '../common/constants';
export const name = 'Workspace Meta';
export const type = 'WorkspaceMeta';
export const prefix = 'wrkm';
export const canDuplicate = false;
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,
hasSeen: boolean
};
export type WorkspaceMeta = BaseWorkspaceMeta & BaseModel;
2018-06-25 17:42:50 +00:00
export function init(): BaseWorkspaceMeta {
return {
parentId: null,
activeRequestId: null,
activeEnvironmentId: null,
sidebarFilter: '',
sidebarHidden: false,
sidebarWidth: DEFAULT_SIDEBAR_WIDTH,
paneWidth: DEFAULT_PANE_WIDTH,
2017-07-27 22:59:07 +00:00
paneHeight: DEFAULT_PANE_HEIGHT,
hasSeen: true
};
}
2018-06-25 17:42:50 +00:00
export function migrate(doc: WorkspaceMeta): WorkspaceMeta {
return doc;
}
2018-06-25 17:42:50 +00:00
export function create(patch: Object = {}): Promise<WorkspaceMeta> {
if (!patch.parentId) {
2018-06-25 17:42:50 +00:00
throw new Error(
`New WorkspaceMeta missing parentId ${JSON.stringify(patch)}`
);
}
return db.docCreate(type, patch);
}
2018-06-25 17:42:50 +00:00
export function update(
workspaceMeta: WorkspaceMeta,
patch: Object = {}
): Promise<WorkspaceMeta> {
return db.docUpdate(workspaceMeta, patch);
}
2018-06-25 17:42:50 +00:00
export async function getByParentId(
parentId: string
): Promise<WorkspaceMeta | null> {
return db.getWhere(type, { parentId });
}
2018-06-25 17:42:50 +00:00
export async function getOrCreateByParentId(
parentId: string
): Promise<WorkspaceMeta> {
const doc = await getByParentId(parentId);
2018-06-25 17:42:50 +00:00
return doc || this.create({ parentId });
}
2018-06-25 17:42:50 +00:00
export function all(): Promise<Array<WorkspaceMeta>> {
return db.all(type);
}