2020-10-21 21:02:43 +00:00
|
|
|
// @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<ProtoFile> = {}): Promise<ProtoFile> {
|
|
|
|
if (!patch.parentId) {
|
2020-10-28 00:05:54 +00:00
|
|
|
throw new Error('New ProtoFile missing `parentId`');
|
2020-10-21 21:02:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return db.docCreate(type, patch);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function remove(protoFile: ProtoFile): Promise<void> {
|
|
|
|
return db.remove(protoFile);
|
|
|
|
}
|
|
|
|
|
2021-01-27 22:00:43 +00:00
|
|
|
export async function batchRemoveIds(ids: Array<string>): Promise<void> {
|
|
|
|
const files = await db.find(type, { _id: { $in: ids } });
|
|
|
|
await db.batchModifyDocs({ upsert: [], remove: files });
|
|
|
|
}
|
|
|
|
|
2020-10-21 21:02:43 +00:00
|
|
|
export function update(protoFile: ProtoFile, patch: $Shape<ProtoFile> = {}): Promise<ProtoFile> {
|
|
|
|
return db.docUpdate(protoFile, patch);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getById(_id: string): Promise<ProtoFile | null> {
|
|
|
|
return db.getWhere(type, { _id });
|
|
|
|
}
|
|
|
|
|
2021-01-25 02:50:53 +00:00
|
|
|
export function getByParentId(parentId: string): Promise<ProtoFile | null> {
|
|
|
|
return db.getWhere(type, { parentId });
|
|
|
|
}
|
|
|
|
|
2020-10-22 19:14:46 +00:00
|
|
|
export function findByParentId(parentId: string): Promise<Array<ProtoFile>> {
|
|
|
|
return db.find(type, { parentId });
|
2020-10-21 21:02:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export function all(): Promise<Array<ProtoFile>> {
|
|
|
|
return db.all(type);
|
|
|
|
}
|