add simple-schema

This commit is contained in:
tamaina 2021-09-06 12:59:51 +09:00
parent f89a326d7f
commit abbeb9a071
5 changed files with 28 additions and 21 deletions

View file

@ -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<T extends Obj> = {
@ -71,7 +63,7 @@ type UndefinedPropertyNames<T extends Obj> = {
type OnlyRequired<T extends Obj> = Pick<T, NonUndefinedPropertyNames<T>>;
type OnlyOptional<T extends Obj> = Pick<T, UndefinedPropertyNames<T>>;
export type Obj = { [key: string]: Schema };
export interface Obj extends SimpleObj { [key: string]: Schema };
export type ObjType<s extends Obj> =
{ [P in keyof OnlyOptional<s>]?: SchemaType<s[P]> } &

15
src/misc/simple-schema.ts Normal file
View file

@ -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 };

View file

@ -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;
/**
*

View file

@ -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<T extends Record<string, any>> {
diff: DeepPartial<T>;
group: string | null;
}[] = [];
public schema: Schema;
public schema: SimpleSchema;
protected repository: Repository<Log>;
protected abstract genNewLog(latest: T): DeepPartial<T>;
@ -69,7 +69,7 @@ export default abstract class Chart<T extends Record<string, any>> {
protected abstract fetchActual(group: string | null): Promise<DeepPartial<T>>;
@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<T extends Record<string, any>> {
}
@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<T extends Record<string, any>> {
});
}
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<T extends Record<string, any>> {
}
}
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 = {

View file

@ -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,