From abbeb9a0718759c8d92bd2ec4db51010ee64767f Mon Sep 17 00:00:00 2001 From: tamaina Date: Mon, 6 Sep 2021 12:59:51 +0900 Subject: [PATCH] add simple-schema --- src/misc/schema.ts | 14 +++----------- src/misc/simple-schema.ts | 15 +++++++++++++++ src/server/api/endpoints.ts | 4 ++-- src/services/chart/core.ts | 14 +++++++------- test/utils.ts | 2 +- 5 files changed, 28 insertions(+), 21 deletions(-) create mode 100644 src/misc/simple-schema.ts diff --git a/src/misc/schema.ts b/src/misc/schema.ts index c4ca80249d..f6fb128751 100644 --- a/src/misc/schema.ts +++ b/src/misc/schema.ts @@ -1,3 +1,4 @@ +import { SimpleObj, SimpleSchema } from './simple-schema'; import { packedUserSchema } from '@/models/repositories/user'; import { packedNoteSchema } from '@/models/repositories/note'; import { packedUserListSchema } from '@/models/repositories/user-list'; @@ -46,18 +47,9 @@ export const refs = { GalleryPost: packedGalleryPostSchema, }; -export type Schema = { - type: 'boolean' | 'number' | 'string' | 'array' | 'object' | 'any'; - nullable: boolean; - optional: boolean; - items?: Schema; +export interface Schema extends SimpleSchema { properties?: Obj; - description?: string; - example?: any; - format?: string; ref?: keyof typeof refs; - enum?: string[]; - default?: boolean | null; }; type NonUndefinedPropertyNames = { @@ -71,7 +63,7 @@ type UndefinedPropertyNames = { type OnlyRequired = Pick>; type OnlyOptional = Pick>; -export type Obj = { [key: string]: Schema }; +export interface Obj extends SimpleObj { [key: string]: Schema }; export type ObjType = { [P in keyof OnlyOptional]?: SchemaType } & diff --git a/src/misc/simple-schema.ts b/src/misc/simple-schema.ts new file mode 100644 index 0000000000..83590edbf2 --- /dev/null +++ b/src/misc/simple-schema.ts @@ -0,0 +1,15 @@ +export interface SimpleSchema { + type: 'boolean' | 'number' | 'string' | 'array' | 'object' | 'any'; + nullable: boolean; + optional: boolean; + items?: SimpleSchema; + properties?: SimpleObj; + description?: string; + example?: any; + format?: string; + ref?: string; + enum?: string[]; + default?: boolean | null; +}; + +export type SimpleObj = { [key: string]: SimpleSchema }; diff --git a/src/server/api/endpoints.ts b/src/server/api/endpoints.ts index 640b14ed6a..6d9d2b0782 100644 --- a/src/server/api/endpoints.ts +++ b/src/server/api/endpoints.ts @@ -3,7 +3,7 @@ import { dirname } from 'path'; import { Context } from 'cafy'; import * as path from 'path'; import * as glob from 'glob'; -import { Schema } from '@/misc/schema'; +import { SimpleSchema } from '@/misc/simple-schema'; //const _filename = fileURLToPath(import.meta.url); const _filename = __filename; @@ -34,7 +34,7 @@ export interface IEndpointMeta { }; }; - res?: Schema; + res?: SimpleSchema; /** * このエンドポイントにリクエストするのにユーザー情報が必須か否か diff --git a/src/services/chart/core.ts b/src/services/chart/core.ts index eee7d20efb..c0d3280c2b 100644 --- a/src/services/chart/core.ts +++ b/src/services/chart/core.ts @@ -7,7 +7,7 @@ import * as nestedProperty from 'nested-property'; import autobind from 'autobind-decorator'; import Logger from '../logger'; -import { Schema } from '@/misc/schema'; +import { SimpleSchema } from '@/misc/simple-schema'; import { EntitySchema, getRepository, Repository, LessThan, Between } from 'typeorm'; import { dateUTC, isTimeSame, isTimeBefore, subtractTime, addTime } from '@/prelude/time'; import { getChartInsertLock } from '@/misc/app-lock'; @@ -56,7 +56,7 @@ export default abstract class Chart> { diff: DeepPartial; group: string | null; }[] = []; - public schema: Schema; + public schema: SimpleSchema; protected repository: Repository; protected abstract genNewLog(latest: T): DeepPartial; @@ -69,7 +69,7 @@ export default abstract class Chart> { protected abstract fetchActual(group: string | null): Promise>; @autobind - private static convertSchemaToFlatColumnDefinitions(schema: Schema) { + private static convertSchemaToFlatColumnDefinitions(schema: SimpleSchema) { const columns = {} as any; const flatColumns = (x: Obj, path?: string) => { for (const [k, v] of Object.entries(x)) { @@ -181,7 +181,7 @@ export default abstract class Chart> { } @autobind - public static schemaToEntity(name: string, schema: Schema): EntitySchema { + public static schemaToEntity(name: string, schema: SimpleSchema): EntitySchema { return new EntitySchema({ name: `__chart__${camelToSnake(name)}`, columns: { @@ -211,7 +211,7 @@ export default abstract class Chart> { }); } - constructor(name: string, schema: Schema, grouped = false) { + constructor(name: string, schema: SimpleSchema, grouped = false) { this.name = name; this.schema = schema; const entity = Chart.schemaToEntity(name, schema); @@ -546,8 +546,8 @@ export default abstract class Chart> { } } -export function convertLog(logSchema: Schema): Schema { - const v: Schema = JSON.parse(JSON.stringify(logSchema)); // copy +export function convertLog(logSchema: SimpleSchema): SimpleSchema { + const v: SimpleSchema = JSON.parse(JSON.stringify(logSchema)); // copy if (v.type === 'number') { v.type = 'array'; v.items = { diff --git a/test/utils.ts b/test/utils.ts index 1a0c54463d..253c410bf0 100644 --- a/test/utils.ts +++ b/test/utils.ts @@ -158,7 +158,7 @@ export async function initTestDb(justBorrow = false, initEntities?: any[]) { await conn.close(); } catch (e) {} - return await createConnection({ + return createConnection({ type: 'postgres', host: config.db.host, port: config.db.port,