2021-05-12 06:35:00 +00:00
|
|
|
import { database as db } from '../common/database';
|
|
|
|
import { generateId } from '../common/misc';
|
2021-07-22 23:04:56 +00:00
|
|
|
import type { BaseModel } from './index';
|
2021-05-12 06:35:00 +00:00
|
|
|
|
|
|
|
export const name = 'Proto Directory';
|
|
|
|
|
|
|
|
export const type = 'ProtoDirectory';
|
|
|
|
|
|
|
|
export const prefix = 'pd';
|
|
|
|
|
|
|
|
export const canDuplicate = true;
|
|
|
|
|
|
|
|
export const canSync = true;
|
|
|
|
|
|
|
|
interface BaseProtoDirectory {
|
|
|
|
name: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
export type ProtoDirectory = BaseModel & BaseProtoDirectory;
|
|
|
|
|
2021-06-16 19:19:00 +00:00
|
|
|
export const isProtoDirectory = (model: Pick<BaseModel, 'type'>): model is ProtoDirectory => (
|
|
|
|
model.type === type
|
|
|
|
);
|
|
|
|
|
2021-05-12 06:35:00 +00:00
|
|
|
export function init(): BaseProtoDirectory {
|
|
|
|
return {
|
|
|
|
name: 'New Proto Directory',
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function migrate(doc: ProtoDirectory) {
|
|
|
|
return doc;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function createId() {
|
|
|
|
return generateId(prefix);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function create(patch: Partial<ProtoDirectory> = {}) {
|
|
|
|
if (!patch.parentId) {
|
|
|
|
throw new Error('New ProtoDirectory missing `parentId`');
|
|
|
|
}
|
|
|
|
|
|
|
|
return db.docCreate<ProtoDirectory>(type, patch);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getById(_id: string) {
|
|
|
|
return db.getWhere<ProtoDirectory>(type, { _id });
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getByParentId(parentId: string) {
|
|
|
|
return db.getWhere<ProtoDirectory>(type, { parentId });
|
|
|
|
}
|
|
|
|
|
|
|
|
export function remove(obj: ProtoDirectory) {
|
|
|
|
return db.remove(obj);
|
|
|
|
}
|
|
|
|
|
2021-05-18 20:32:18 +00:00
|
|
|
export async function batchRemoveIds(ids: string[]) {
|
2021-05-12 06:35:00 +00:00
|
|
|
const dirs = await db.find(type, {
|
|
|
|
_id: {
|
|
|
|
$in: ids,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
await db.batchModifyDocs({
|
|
|
|
upsert: [],
|
|
|
|
remove: dirs,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
export function all() {
|
|
|
|
return db.all<ProtoDirectory>(type);
|
|
|
|
}
|