import type { GrpcRequest } from 'insomnia-data'; import { database as db, models } from 'insomnia-data'; const { type, name } = models.grpcRequest; export function create(patch: Partial = {}) { if (!patch.parentId) { throw new Error('New GrpcRequest missing `parentId`'); } return db.docCreate(type, patch); } export function remove(obj: GrpcRequest) { return db.remove(obj); } export function update(obj: GrpcRequest, patch: Partial = {}) { return db.docUpdate(obj, patch); } export function getById(_id: string) { return db.findOne(type, { _id }); } export function findByProtoFileId(protoFileId: string) { return db.find(type, { protoFileId }); } export function findByParentId(parentId: string) { return db.find(type, { parentId }); } // This is duplicated (lol) from models/request.js export async function duplicate(request: GrpcRequest, patch: Partial = {}) { // Only set name and "(Copy)" if the patch does // not define it and the request itself has a name. // Otherwise leave it blank so the request URL can // fill it in automatically. if (!patch.name && request.name) { patch.name = `${request.name} (Copy)`; } // Get sort key of next request const q = { metaSortKey: { $gt: request.metaSortKey, }, }; const [nextRequest] = await db.find(type, q, { metaSortKey: 1, }); const nextSortKey = nextRequest ? nextRequest.metaSortKey : request.metaSortKey + 100; // Calculate new sort key const sortKeyIncrement = (nextSortKey - request.metaSortKey) / 2; const metaSortKey = request.metaSortKey + sortKeyIncrement; return db.duplicate(request, { name, metaSortKey, ...patch, }); } export function all() { return db.find(type); }