2021-08-20 15:12:36 +00:00
|
|
|
import { getAppName } from '../common/constants';
|
|
|
|
import { database as db } from '../common/database';
|
|
|
|
import { generateId } from '../common/misc';
|
|
|
|
import type { BaseModel } from './index';
|
|
|
|
|
|
|
|
export const name = 'Project';
|
|
|
|
export const type = 'Project';
|
|
|
|
export const prefix = 'proj';
|
|
|
|
export const canDuplicate = false;
|
|
|
|
export const canSync = false;
|
|
|
|
|
2021-08-25 00:51:40 +00:00
|
|
|
export const DEFAULT_PROJECT_ID = `${prefix}_default-project`;
|
2021-08-20 15:12:36 +00:00
|
|
|
|
2021-08-25 00:51:40 +00:00
|
|
|
export const isDefaultProject = (project: Project) => project._id === DEFAULT_PROJECT_ID;
|
|
|
|
export const isNotDefaultProject = (project: Project) => !isDefaultProject(project);
|
2021-08-20 15:12:36 +00:00
|
|
|
export const isLocalProject = (project: Project): project is LocalProject => project.remoteId === null;
|
|
|
|
export const isRemoteProject = (project: Project): project is RemoteProject => !isLocalProject(project);
|
2021-08-25 00:51:40 +00:00
|
|
|
export const projectHasSettings = (project: Project) => !isDefaultProject(project);
|
2021-08-20 15:12:36 +00:00
|
|
|
|
|
|
|
interface CommonProject {
|
|
|
|
name: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface RemoteProject extends BaseModel, CommonProject {
|
|
|
|
remoteId: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface LocalProject extends BaseModel, CommonProject {
|
|
|
|
remoteId: null;
|
|
|
|
}
|
|
|
|
|
|
|
|
export type Project = LocalProject | RemoteProject;
|
|
|
|
|
|
|
|
export const isProject = (model: Pick<BaseModel, 'type'>): model is Project => (
|
|
|
|
model.type === type
|
|
|
|
);
|
|
|
|
|
|
|
|
export const isProjectId = (id: string | null) => (
|
|
|
|
id?.startsWith(`${prefix}_`)
|
|
|
|
);
|
|
|
|
|
|
|
|
export function init(): Partial<Project> {
|
|
|
|
return {
|
|
|
|
name: 'My Project',
|
|
|
|
remoteId: null, // `null` is necessary for the model init logic to work properly
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function migrate(project: Project) {
|
|
|
|
return project;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function createId() {
|
|
|
|
return generateId(prefix);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function create(patch: Partial<Project> = {}) {
|
|
|
|
return db.docCreate<Project>(type, patch);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getById(_id: string) {
|
|
|
|
return db.getWhere<Project>(type, { _id });
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getByRemoteId(remoteId: string) {
|
|
|
|
return db.getWhere<Project>(type, { remoteId });
|
|
|
|
}
|
|
|
|
|
|
|
|
export function remove(project: Project) {
|
|
|
|
return db.remove(project);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function update(project: Project, patch: Partial<Project>) {
|
|
|
|
return db.docUpdate(project, patch);
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function all() {
|
|
|
|
const projects = await db.all<Project>(type);
|
|
|
|
|
2021-08-25 00:51:40 +00:00
|
|
|
if (!projects.find(c => c._id === DEFAULT_PROJECT_ID)) {
|
|
|
|
await create({ _id: DEFAULT_PROJECT_ID, name: getAppName(), remoteId: null });
|
2021-08-20 15:12:36 +00:00
|
|
|
return db.all<Project>(type);
|
|
|
|
}
|
|
|
|
|
|
|
|
return projects;
|
|
|
|
}
|