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

58 lines
1.4 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-10-17 16:42:33 +00:00
export async function update(doc: PluginData, patch: Object): Promise<PluginData> {
return db.docUpdate(doc, patch);
}
2018-10-17 16:42:33 +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 });
}
2018-11-06 13:52:55 +00:00
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 });
}
2018-10-17 16:42:33 +00:00
export async function getByKey(plugin: string, key: string): Promise<PluginData | null> {
2018-06-25 17:42:50 +00:00
return db.getWhere(type, { plugin, key });
}