2020-10-28 00:05:54 +00:00
|
|
|
// @flow
|
|
|
|
import * as db from '../common/database';
|
|
|
|
import type { BaseModel } from './index';
|
2020-11-02 00:55:22 +00:00
|
|
|
import { isGrpcRequestId } from './helpers/is-model';
|
2020-10-28 00:05:54 +00:00
|
|
|
|
|
|
|
export const name = 'gRPC Request Meta';
|
|
|
|
export const type = 'GrpcRequestMeta';
|
|
|
|
export const prefix = 'greqm';
|
|
|
|
export const canDuplicate = false;
|
|
|
|
export const canSync = false;
|
|
|
|
|
|
|
|
type BaseGrpcRequestMeta = {
|
|
|
|
pinned: boolean,
|
2020-10-28 22:23:07 +00:00
|
|
|
lastActive: number,
|
2020-10-28 00:05:54 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export type GrpcRequestMeta = BaseModel & BaseGrpcRequestMeta;
|
|
|
|
|
|
|
|
export function init() {
|
|
|
|
return {
|
|
|
|
pinned: false,
|
2020-10-28 22:23:07 +00:00
|
|
|
lastActive: 0,
|
2020-10-28 00:05:54 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function migrate(doc: GrpcRequestMeta): GrpcRequestMeta {
|
|
|
|
return doc;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function create(patch: $Shape<GrpcRequestMeta> = {}): Promise<GrpcRequestMeta> {
|
|
|
|
if (!patch.parentId) {
|
|
|
|
throw new Error('New GrpcRequestMeta missing `parentId`');
|
|
|
|
}
|
|
|
|
|
2020-11-02 00:55:22 +00:00
|
|
|
expectParentToBeGrpcRequest(patch.parentId);
|
2020-10-28 22:23:07 +00:00
|
|
|
|
2020-10-28 00:05:54 +00:00
|
|
|
return db.docCreate(type, patch);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function update(
|
|
|
|
requestMeta: GrpcRequestMeta,
|
|
|
|
patch: $Shape<GrpcRequestMeta>,
|
|
|
|
): Promise<GrpcRequestMeta> {
|
2020-11-02 00:55:22 +00:00
|
|
|
expectParentToBeGrpcRequest(patch.parentId || requestMeta.parentId);
|
2020-10-28 00:05:54 +00:00
|
|
|
return db.docUpdate(requestMeta, patch);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getByParentId(parentId: string): Promise<GrpcRequestMeta> {
|
2020-11-02 00:55:22 +00:00
|
|
|
expectParentToBeGrpcRequest(parentId);
|
2020-10-28 00:05:54 +00:00
|
|
|
return db.getWhere(type, { parentId });
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function getOrCreateByParentId(parentId: string): Promise<GrpcRequestMeta> {
|
|
|
|
const requestMeta = await getByParentId(parentId);
|
|
|
|
|
|
|
|
if (requestMeta) {
|
|
|
|
return requestMeta;
|
|
|
|
}
|
|
|
|
|
|
|
|
return create({ parentId });
|
|
|
|
}
|
|
|
|
|
2020-10-28 22:23:07 +00:00
|
|
|
export async function updateOrCreateByParentId(parentId: string, patch: $Shape<GrpcRequestMeta>) {
|
|
|
|
const requestMeta = await getByParentId(parentId);
|
|
|
|
|
|
|
|
if (requestMeta) {
|
|
|
|
return update(requestMeta, patch);
|
|
|
|
} else {
|
|
|
|
const newPatch = Object.assign({ parentId }, patch);
|
|
|
|
return create(newPatch);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-28 00:05:54 +00:00
|
|
|
export function all(): Promise<Array<GrpcRequestMeta>> {
|
|
|
|
return db.all(type);
|
|
|
|
}
|
2020-11-02 00:55:22 +00:00
|
|
|
|
|
|
|
function expectParentToBeGrpcRequest(parentId: string) {
|
|
|
|
if (!isGrpcRequestId(parentId)) {
|
|
|
|
throw new Error('Expected the parent of GrpcRequestMeta to be a GrpcRequest');
|
|
|
|
}
|
|
|
|
}
|