insomnia/packages/insomnia-app/app/models/plugin-data.js
Opender Singh b072745b6d
Introduce flow $Shape<T> to db models (#2002)
* Introduce flow <T> to db models

* Resolve errors discovered while fixing typing

* Revert change for response
2020-03-18 11:58:57 -07:00

59 lines
1.5 KiB
JavaScript

// @flow
import * as db from '../common/database';
import type { BaseModel } from './index';
export const name = 'PluginData';
export const type = 'PluginData';
export const prefix = 'plg';
export const canDuplicate = false;
export const canSync = false;
type BasePluginData = {
plugin: string,
key: string,
value: string,
};
export type PluginData = BaseModel & BasePluginData;
export function init(): BasePluginData {
return {
plugin: '',
key: '',
value: '',
};
}
export function migrate(doc: PluginData): PluginData {
return doc;
}
export function create(patch: $Shape<PluginData> = {}): Promise<PluginData> {
return db.docCreate(type, patch);
}
export async function update(doc: PluginData, patch: $Shape<PluginData>): Promise<PluginData> {
return db.docUpdate(doc, patch);
}
export async function upsertByKey(plugin: string, key: string, value: string): Promise<PluginData> {
const doc = await getByKey(plugin, key);
return doc ? update(doc, { value }) : create({ plugin, key, value });
}
export async function removeByKey(plugin: string, key: string): Promise<void> {
return db.removeWhere(type, { plugin, key });
}
export async function all(plugin: string): Promise<Array<PluginData>> {
return db.find(type, { plugin });
}
export async function removeAll(plugin: string): Promise<void> {
return db.removeWhere(type, { plugin });
}
export async function getByKey(plugin: string, key: string): Promise<PluginData | null> {
return db.getWhere(type, { plugin, key });
}