2021-05-12 06:35:00 +00:00
|
|
|
import { database as db } from '../common/database';
|
2020-10-28 00:05:54 +00:00
|
|
|
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';
|
2021-05-12 06:35:00 +00:00
|
|
|
|
2020-10-28 00:05:54 +00:00
|
|
|
export const type = 'GrpcRequestMeta';
|
2021-05-12 06:35:00 +00:00
|
|
|
|
2020-10-28 00:05:54 +00:00
|
|
|
export const prefix = 'greqm';
|
2021-05-12 06:35:00 +00:00
|
|
|
|
2020-10-28 00:05:54 +00:00
|
|
|
export const canDuplicate = false;
|
2021-05-12 06:35:00 +00:00
|
|
|
|
2020-10-28 00:05:54 +00:00
|
|
|
export const canSync = false;
|
|
|
|
|
2021-05-12 06:35:00 +00:00
|
|
|
interface BaseGrpcRequestMeta {
|
|
|
|
pinned: boolean;
|
|
|
|
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
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-05-12 06:35:00 +00:00
|
|
|
export function migrate(doc: GrpcRequestMeta) {
|
2020-10-28 00:05:54 +00:00
|
|
|
return doc;
|
|
|
|
}
|
|
|
|
|
2021-05-12 06:35:00 +00:00
|
|
|
export function create(patch: Partial<GrpcRequestMeta> = {}) {
|
2020-10-28 00:05:54 +00:00
|
|
|
if (!patch.parentId) {
|
|
|
|
throw new Error('New GrpcRequestMeta missing `parentId`');
|
|
|
|
}
|
|
|
|
|
2020-11-02 00:55:22 +00:00
|
|
|
expectParentToBeGrpcRequest(patch.parentId);
|
2021-05-12 06:35:00 +00:00
|
|
|
return db.docCreate<GrpcRequestMeta>(type, patch);
|
2020-10-28 00:05:54 +00:00
|
|
|
}
|
|
|
|
|
2021-05-12 06:35:00 +00:00
|
|
|
export function update(requestMeta: GrpcRequestMeta, patch: Partial<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);
|
|
|
|
}
|
|
|
|
|
2021-05-12 06:35:00 +00:00
|
|
|
export function getByParentId(parentId: string) {
|
2020-11-02 00:55:22 +00:00
|
|
|
expectParentToBeGrpcRequest(parentId);
|
2021-05-12 06:35:00 +00:00
|
|
|
return db.getWhere<GrpcRequestMeta>(type, { parentId });
|
2020-10-28 00:05:54 +00:00
|
|
|
}
|
|
|
|
|
2021-05-12 06:35:00 +00:00
|
|
|
export async function getOrCreateByParentId(parentId: string) {
|
2020-10-28 00:05:54 +00:00
|
|
|
const requestMeta = await getByParentId(parentId);
|
|
|
|
|
|
|
|
if (requestMeta) {
|
|
|
|
return requestMeta;
|
|
|
|
}
|
|
|
|
|
|
|
|
return create({ parentId });
|
|
|
|
}
|
|
|
|
|
2021-05-12 06:35:00 +00:00
|
|
|
export async function updateOrCreateByParentId(parentId: string, patch: Partial<GrpcRequestMeta>) {
|
2020-10-28 22:23:07 +00:00
|
|
|
const requestMeta = await getByParentId(parentId);
|
|
|
|
|
|
|
|
if (requestMeta) {
|
|
|
|
return update(requestMeta, patch);
|
|
|
|
} else {
|
2021-05-12 06:35:00 +00:00
|
|
|
const newPatch = Object.assign(
|
|
|
|
{
|
|
|
|
parentId,
|
|
|
|
},
|
|
|
|
patch,
|
|
|
|
);
|
2020-10-28 22:23:07 +00:00
|
|
|
return create(newPatch);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-12 06:35:00 +00:00
|
|
|
export function all() {
|
|
|
|
return db.all<GrpcRequestMeta>(type);
|
2020-10-28 00:05:54 +00:00
|
|
|
}
|
2020-11-02 00:55:22 +00:00
|
|
|
|
2021-05-12 06:35:00 +00:00
|
|
|
function expectParentToBeGrpcRequest(parentId: string | null) {
|
2020-11-02 00:55:22 +00:00
|
|
|
if (!isGrpcRequestId(parentId)) {
|
|
|
|
throw new Error('Expected the parent of GrpcRequestMeta to be a GrpcRequest');
|
|
|
|
}
|
|
|
|
}
|