insomnia/packages/insomnia-app/app/models/plugin-data.js

64 lines
1.3 KiB
JavaScript
Raw Normal View History

// @flow
import * as db from '../common/database';
2018-06-25 17:42:50 +00:00
import type { BaseModel } from './index';
export const name = 'PluginData';
export const type = 'PluginData';
export const prefix = 'plg';
export const canDuplicate = false;
type BasePluginData = {
plugin: string,
key: string,
2018-06-25 17:42:50 +00:00
value: string
};
export type PluginData = BaseModel & BasePluginData;
2018-06-25 17:42:50 +00:00
export function init(): BasePluginData {
return {
plugin: '',
key: '',
value: ''
};
}
2018-06-25 17:42:50 +00:00
export function migrate(doc: PluginData): PluginData {
return doc;
}
2018-06-25 17:42:50 +00:00
export function create(patch: Object = {}): Promise<PluginData> {
return db.docCreate(type, patch);
}
2018-06-25 17:42:50 +00:00
export async function update(
doc: PluginData,
patch: Object
): Promise<PluginData> {
return db.docUpdate(doc, patch);
}
2018-06-25 17:42:50 +00:00
export async function upsertByKey(
plugin: string,
key: string,
value: string
): Promise<PluginData> {
const doc = await getByKey(plugin, key);
2018-06-25 17:42:50 +00:00
return doc ? update(doc, { value }) : create({ plugin, key, value });
}
2018-06-25 17:42:50 +00:00
export async function removeByKey(plugin: string, key: string): Promise<void> {
return db.removeWhere(type, { plugin, key });
}
export async function removeAll(plugin: string): Promise<void> {
return db.removeWhere(type, { plugin });
}
2018-06-25 17:42:50 +00:00
export async function getByKey(
plugin: string,
key: string
): Promise<PluginData | null> {
return db.getWhere(type, { plugin, key });
}