2018-06-09 03:22:39 +00:00
|
|
|
// @flow
|
|
|
|
import * as db from '../common/database';
|
2018-06-25 17:42:50 +00:00
|
|
|
import type { BaseModel } from './index';
|
2018-06-09 03:22:39 +00:00
|
|
|
|
|
|
|
export const name = 'PluginData';
|
|
|
|
export const type = 'PluginData';
|
|
|
|
export const prefix = 'plg';
|
|
|
|
export const canDuplicate = false;
|
2019-04-18 00:50:03 +00:00
|
|
|
export const canSync = false;
|
2018-06-09 03:22:39 +00:00
|
|
|
|
|
|
|
type BasePluginData = {
|
|
|
|
plugin: string,
|
|
|
|
key: string,
|
2018-12-12 17:36:11 +00:00
|
|
|
value: string,
|
2018-06-09 03:22:39 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export type PluginData = BaseModel & BasePluginData;
|
|
|
|
|
2018-06-25 17:42:50 +00:00
|
|
|
export function init(): BasePluginData {
|
2018-06-09 03:22:39 +00:00
|
|
|
return {
|
|
|
|
plugin: '',
|
|
|
|
key: '',
|
2018-12-12 17:36:11 +00:00
|
|
|
value: '',
|
2018-06-09 03:22:39 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-06-25 17:42:50 +00:00
|
|
|
export function migrate(doc: PluginData): PluginData {
|
2018-06-09 03:22:39 +00:00
|
|
|
return doc;
|
|
|
|
}
|
|
|
|
|
2018-06-25 17:42:50 +00:00
|
|
|
export function create(patch: Object = {}): Promise<PluginData> {
|
2018-06-09 03:22:39 +00:00
|
|
|
return db.docCreate(type, patch);
|
|
|
|
}
|
|
|
|
|
2018-10-17 16:42:33 +00:00
|
|
|
export async function update(doc: PluginData, patch: Object): Promise<PluginData> {
|
2018-06-09 03:22:39 +00:00
|
|
|
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> {
|
2018-06-09 03:22:39 +00:00
|
|
|
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-09 03:22:39 +00:00
|
|
|
}
|
|
|
|
|
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-06-09 03:22:39 +00:00
|
|
|
}
|
|
|
|
|
2018-11-06 13:52:55 +00:00
|
|
|
export async function all(plugin: string): Promise<Array<PluginData>> {
|
|
|
|
return db.find(type, { plugin });
|
|
|
|
}
|
|
|
|
|
2018-06-27 04:58:34 +00:00
|
|
|
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 });
|
2018-06-09 03:22:39 +00:00
|
|
|
}
|