import { Knex } from 'knex'; import { LifecycleProvider } from './lifecycles'; import { MigrationProvider } from './migrations'; import { SchemaProvider } from './schema'; type LogicalOperators = { $and?: WhereParams[]; $or?: WhereParams[]; $not?: WhereParams; }; type AttributeOperators = { $eq?: T[K] | Array; $ne?: T[K] | Array; $in?: T[K][]; $notIn?: T[K][]; $lt?: T[K]; $lte?: T[K]; $gt?: T[K]; $gte?: T[K]; $between?: [T[K], T[K]]; $contains?: T[K]; $notContains?: T[K]; $containsi?: T[K]; $notContainsi?: T[K]; $startsWith?: T[K]; $endsWith?: T[K]; $null?: boolean; $notNull?: boolean; $not?: WhereParams | AttributeOperators; }; export type WhereParams = { [K in keyof T]?: T[K] | T[K][] | AttributeOperators; } & LogicalOperators; type Sortables = { // check sortable [P in keyof T]: P; }[keyof T]; type Direction = 'asc' | 'ASC' | 'DESC' | 'desc'; interface FindParams { select?: (keyof T)[]; // TODO: add nested operators & relations where?: WhereParams; limit?: number; offset?: number; orderBy?: // TODO: add relations | Sortables | Sortables[] | { [K in Sortables]?: Direction } | { [K in Sortables]?: Direction }[]; // TODO: define nested obj populate?: (keyof T)[]; } interface CreateParams { select?: (keyof T)[]; populate?: (keyof T)[]; data: T[keyof T]; } interface CreateManyParams { select?: (keyof T)[]; populate?: (keyof T)[]; data: T[keyof T][]; } interface Pagination { page: number; pageSize: number; pageCount: number; total: number; } interface PopulateParams {} interface EntityManager { findOne(uid: K, params: FindParams): Promise; findMany(uid: K, params: FindParams): Promise; create(uid: K, params: CreateParams): Promise; createMany( uid: K, params: CreateManyParams ): Promise<{ count: number }>; update(uid: K, params: any): Promise; updateMany(uid: K, params: any): Promise<{ count: number }>; delete(uid: K, params: any): Promise; deleteMany(uid: K, params: any): Promise<{ count: number }>; count(uid: K, params: any): Promise; attachRelations(uid: K, id: ID, data: any): Promise; updateRelations(uid: K, id: ID, data: any): Promise; deleteRelations(uid: K, id: ID): Promise; populate( uid: K, entity: T, populate: PopulateParams ): Promise; load( uid: K, entity: T, field: SK, populate: PopulateParams ): Promise; } interface QueryFromContentType { findOne(params: FindParams): Promise; findMany(params: FindParams): Promise; findWithCount(params: FindParams): Promise<[any[], number]>; findPage(params: FindParams): Promise<{ results: any[]; pagination: Pagination }>; create(params: CreateParams): Promise; createMany(params: CreateManyParams): Promise<{ count: number }>; update(params: any): Promise; updateMany(params: any): Promise<{ count: number }>; delete(params: any): Promise; deleteMany(params: any): Promise<{ count: number }>; count(params: any): Promise; attachRelations(id: ID, data: any): Promise; updateRelations(id: ID, data: any): Promise; deleteRelations(id: ID): Promise; populate(entity: S, populate: PopulateParams): Promise; load( entity: S, field: K, populate: PopulateParams ): Promise; } interface ModelConfig { tableName: string; [k: string]: any; } interface ConnectionConfig {} interface DatabaseConfig { connection: ConnectionConfig; models: ModelConfig[]; } export interface Database { schema: SchemaProvider; lifecycles: LifecycleProvider; migrations: MigrationProvider; entityManager: EntityManager; queryBuilder: any; metadata: any; connection: Knex; query(uid: T): QueryFromContentType; transaction( cb?: (params: { trx: Knex.Transaction; rollback: () => Promise; commit: () => Promise; }) => Promise ): | Promise | { get: () => Knex.Transaction; rollback: () => Promise; commit: () => Promise }; } export class Database implements Database { static transformContentTypes(contentTypes: any[]): ModelConfig[]; static init(config: DatabaseConfig): Promise; }