// @flow import * as db from '../common/database'; import type { BaseModel } from './index'; export const name = 'Proto File'; export const type = 'ProtoFile'; export const prefix = 'pf'; export const canDuplicate = true; export const canSync = true; type BaseProtoFile = { name: string, protoText: string, }; export type ProtoFile = BaseModel & BaseProtoFile; export function init(): BaseProtoFile { return { name: 'New Proto File', protoText: '', }; } export function migrate(doc: ProtoFile): ProtoFile { return doc; } export function create(patch: $Shape = {}): Promise { if (!patch.parentId) { throw new Error('New ProtoFile missing `parentId`'); } return db.docCreate(type, patch); } export function remove(protoFile: ProtoFile): Promise { return db.remove(protoFile); } export async function batchRemoveIds(ids: Array): Promise { const files = await db.find(type, { _id: { $in: ids } }); await db.batchModifyDocs({ upsert: [], remove: files }); } export function update(protoFile: ProtoFile, patch: $Shape = {}): Promise { return db.docUpdate(protoFile, patch); } export function getById(_id: string): Promise { return db.getWhere(type, { _id }); } export function getByParentId(parentId: string): Promise { return db.getWhere(type, { parentId }); } export function findByParentId(parentId: string): Promise> { return db.find(type, { parentId }); } export function all(): Promise> { return db.all(type); }