2020-04-26 20:33:39 +00:00
|
|
|
import type { BaseModel } from './index';
|
2021-05-12 06:35:00 +00:00
|
|
|
import { database as db } from '../common/database';
|
2021-03-01 21:32:32 +00:00
|
|
|
import { strings } from '../common/strings';
|
2020-04-26 20:33:39 +00:00
|
|
|
|
|
|
|
export const name = 'ApiSpec';
|
2021-05-12 06:35:00 +00:00
|
|
|
|
2020-04-26 20:33:39 +00:00
|
|
|
export const type = 'ApiSpec';
|
2021-05-12 06:35:00 +00:00
|
|
|
|
2020-04-26 20:33:39 +00:00
|
|
|
export const prefix = 'spc';
|
2021-05-12 06:35:00 +00:00
|
|
|
|
2020-04-26 20:33:39 +00:00
|
|
|
export const canDuplicate = true;
|
2021-05-12 06:35:00 +00:00
|
|
|
|
2020-04-27 22:23:56 +00:00
|
|
|
export const canSync = false;
|
2020-04-26 20:33:39 +00:00
|
|
|
|
2021-05-12 06:35:00 +00:00
|
|
|
interface BaseApiSpec {
|
|
|
|
fileName: string;
|
|
|
|
contentType: 'json' | 'yaml';
|
|
|
|
contents: string;
|
|
|
|
}
|
2020-04-26 20:33:39 +00:00
|
|
|
|
|
|
|
export type ApiSpec = BaseModel & BaseApiSpec;
|
|
|
|
|
|
|
|
export function init(): BaseApiSpec {
|
|
|
|
return {
|
2021-03-01 21:32:32 +00:00
|
|
|
fileName: `New ${strings.document}`,
|
2020-04-26 20:33:39 +00:00
|
|
|
contents: '',
|
|
|
|
contentType: 'yaml',
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-05-12 06:35:00 +00:00
|
|
|
export async function migrate(doc: ApiSpec) {
|
2020-04-29 02:48:25 +00:00
|
|
|
return doc;
|
2020-04-26 20:33:39 +00:00
|
|
|
}
|
|
|
|
|
2021-05-12 06:35:00 +00:00
|
|
|
export function getByParentId(workspaceId: string) {
|
|
|
|
return db.getWhere<ApiSpec>(type, { parentId: workspaceId });
|
2020-04-26 20:33:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export async function getOrCreateForParentId(
|
|
|
|
workspaceId: string,
|
2021-05-12 06:35:00 +00:00
|
|
|
patch: Partial<ApiSpec> = {},
|
|
|
|
) {
|
|
|
|
const spec = await db.getWhere<ApiSpec>(type, {
|
|
|
|
parentId: workspaceId,
|
|
|
|
});
|
2020-04-26 20:33:39 +00:00
|
|
|
|
|
|
|
if (!spec) {
|
2021-05-12 06:35:00 +00:00
|
|
|
return db.docCreate<ApiSpec>(type, { ...patch, parentId: workspaceId });
|
2020-04-26 20:33:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return spec;
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function updateOrCreateForParentId(
|
|
|
|
workspaceId: string,
|
2021-05-12 06:35:00 +00:00
|
|
|
patch: Partial<ApiSpec> = {},
|
|
|
|
) {
|
2020-04-26 20:33:39 +00:00
|
|
|
const spec = await getOrCreateForParentId(workspaceId);
|
|
|
|
return db.docUpdate(spec, patch);
|
|
|
|
}
|
|
|
|
|
2021-05-12 06:35:00 +00:00
|
|
|
export async function all() {
|
|
|
|
return db.all<ApiSpec>(type);
|
2020-04-26 20:33:39 +00:00
|
|
|
}
|
|
|
|
|
2021-05-12 06:35:00 +00:00
|
|
|
export function update(apiSpec: ApiSpec, patch: Partial<ApiSpec> = {}) {
|
2020-04-26 20:33:39 +00:00
|
|
|
return db.docUpdate(apiSpec, patch);
|
|
|
|
}
|
2020-04-28 18:29:51 +00:00
|
|
|
|
2021-05-12 06:35:00 +00:00
|
|
|
export function removeWhere(parentId: string) {
|
2020-04-28 18:29:51 +00:00
|
|
|
return db.removeWhere(type, { parentId });
|
|
|
|
}
|