From 89eb4b8d0ba80784f47fa3d3a7d2f92305120bbe Mon Sep 17 00:00:00 2001 From: Dan Caddigan Date: Sat, 26 Jan 2019 12:35:03 -0500 Subject: [PATCH] fix(bugs): fix bugs introduced by previous commits --- examples/1-simple-model/src/index.ts | 3 - examples/2-complex-example/src/index.ts | 4 - .../3-one-to-many-relationship/src/index.ts | 3 - .../4-many-to-many-relationship/src/index.ts | 3 - examples/5-migrations/generated/classes.ts | 138 ++++++++++++++++++ examples/5-migrations/generated/index.ts | 1 + examples/5-migrations/generated/ormconfig.ts | 25 ++++ src/core/server.ts | 3 + src/torm/createConnection.ts | 54 +++---- 9 files changed, 196 insertions(+), 38 deletions(-) create mode 100644 examples/5-migrations/generated/classes.ts create mode 100644 examples/5-migrations/generated/index.ts create mode 100644 examples/5-migrations/generated/ormconfig.ts diff --git a/examples/1-simple-model/src/index.ts b/examples/1-simple-model/src/index.ts index 3daa4b0d..793cf391 100644 --- a/examples/1-simple-model/src/index.ts +++ b/examples/1-simple-model/src/index.ts @@ -1,9 +1,6 @@ -import * as dotenv from 'dotenv'; import 'reflect-metadata'; import { Container } from 'typedi'; -dotenv.config(); - import { Server } from '../../../src/'; async function bootstrap() { diff --git a/examples/2-complex-example/src/index.ts b/examples/2-complex-example/src/index.ts index 9dc07850..9b01024a 100644 --- a/examples/2-complex-example/src/index.ts +++ b/examples/2-complex-example/src/index.ts @@ -1,11 +1,7 @@ import 'reflect-metadata'; -import * as dotenv from 'dotenv'; - import { getServer } from './server'; -dotenv.config(); - async function bootstrap() { const server = getServer(); await server.start(); diff --git a/examples/3-one-to-many-relationship/src/index.ts b/examples/3-one-to-many-relationship/src/index.ts index c4bf5a7e..899e5045 100644 --- a/examples/3-one-to-many-relationship/src/index.ts +++ b/examples/3-one-to-many-relationship/src/index.ts @@ -1,9 +1,6 @@ -import * as dotenv from 'dotenv'; import 'reflect-metadata'; import { Container } from 'typedi'; -dotenv.config(); - import { Server } from '../../../src/'; async function bootstrap() { diff --git a/examples/4-many-to-many-relationship/src/index.ts b/examples/4-many-to-many-relationship/src/index.ts index c4bf5a7e..899e5045 100644 --- a/examples/4-many-to-many-relationship/src/index.ts +++ b/examples/4-many-to-many-relationship/src/index.ts @@ -1,9 +1,6 @@ -import * as dotenv from 'dotenv'; import 'reflect-metadata'; import { Container } from 'typedi'; -dotenv.config(); - import { Server } from '../../../src/'; async function bootstrap() { diff --git a/examples/5-migrations/generated/classes.ts b/examples/5-migrations/generated/classes.ts new file mode 100644 index 00000000..7945a41e --- /dev/null +++ b/examples/5-migrations/generated/classes.ts @@ -0,0 +1,138 @@ +// This file has been auto-generated by Warthog. Do not update directly as it +// will be re-written. If you need to change this file, update models or add +// new TypeGraphQL objects +import { + ArgsType, + Field as TypeGraphQLField, + ID, + InputType as TypeGraphQLInputType +} from 'type-graphql'; +import { registerEnumType } from 'type-graphql'; +import { BaseWhereInput, PaginationArgs } from '../../../src'; +import { User } from '../src/user.model'; + +export enum UserOrderByEnum { + createdAt_ASC = 'createdAt_ASC', + createdAt_DESC = 'createdAt_DESC', + + updatedAt_ASC = 'updatedAt_ASC', + updatedAt_DESC = 'updatedAt_DESC', + + deletedAt_ASC = 'deletedAt_ASC', + deletedAt_DESC = 'deletedAt_DESC', + + firstName_ASC = 'firstName_ASC', + firstName_DESC = 'firstName_DESC', + + lastName_ASC = 'lastName_ASC', + lastName_DESC = 'lastName_DESC', + + email_ASC = 'email_ASC', + email_DESC = 'email_DESC' +} + +registerEnumType(UserOrderByEnum, { + name: 'UserOrderByInput' +}); + +@TypeGraphQLInputType() +export class UserWhereInput extends BaseWhereInput { + @TypeGraphQLField({ nullable: true }) + firstName_eq?: string; + + @TypeGraphQLField({ nullable: true }) + firstName_contains?: string; + + @TypeGraphQLField({ nullable: true }) + firstName_startsWith?: string; + + @TypeGraphQLField({ nullable: true }) + firstName_endsWith?: string; + + @TypeGraphQLField(type => [String], { nullable: true }) + firstName_in?: string[]; + + @TypeGraphQLField({ nullable: true }) + lastName_eq?: string; + + @TypeGraphQLField({ nullable: true }) + lastName_contains?: string; + + @TypeGraphQLField({ nullable: true }) + lastName_startsWith?: string; + + @TypeGraphQLField({ nullable: true }) + lastName_endsWith?: string; + + @TypeGraphQLField(type => [String], { nullable: true }) + lastName_in?: string[]; + + @TypeGraphQLField({ nullable: true }) + email_eq?: string; + + @TypeGraphQLField({ nullable: true }) + email_contains?: string; + + @TypeGraphQLField({ nullable: true }) + email_startsWith?: string; + + @TypeGraphQLField({ nullable: true }) + email_endsWith?: string; + + @TypeGraphQLField(type => [String], { nullable: true }) + email_in?: string[]; +} + +@TypeGraphQLInputType() +export class UserWhereUniqueInput { + @TypeGraphQLField(type => String, { nullable: true }) + id?: string; + + @TypeGraphQLField(type => String, { nullable: true }) + email?: string; +} + +@TypeGraphQLInputType() +export class UserCreateInput { + @TypeGraphQLField() + firstName!: string; + + @TypeGraphQLField({ nullable: true }) + lastName?: string; + + @TypeGraphQLField() + email!: string; +} + +@TypeGraphQLInputType() +export class UserUpdateInput { + @TypeGraphQLField({ nullable: true }) + firstName?: string; + + @TypeGraphQLField({ nullable: true }) + lastName?: string; + + @TypeGraphQLField({ nullable: true }) + email?: string; +} + +@ArgsType() +export class UserWhereArgs extends PaginationArgs { + @TypeGraphQLField(type => UserWhereInput, { nullable: true }) + where?: UserWhereInput; + + @TypeGraphQLField(type => UserOrderByEnum, { nullable: true }) + orderBy?: UserOrderByEnum; +} + +@ArgsType() +export class UserCreateManyArgs { + @TypeGraphQLField(type => [UserCreateInput]) + data!: UserCreateInput[]; +} + +@ArgsType() +export class UserUpdateArgs { + @TypeGraphQLField() data!: UserUpdateInput; + @TypeGraphQLField() where!: UserWhereUniqueInput; +} diff --git a/examples/5-migrations/generated/index.ts b/examples/5-migrations/generated/index.ts new file mode 100644 index 00000000..d7d17c69 --- /dev/null +++ b/examples/5-migrations/generated/index.ts @@ -0,0 +1 @@ +export * from './classes'; diff --git a/examples/5-migrations/generated/ormconfig.ts b/examples/5-migrations/generated/ormconfig.ts new file mode 100644 index 00000000..ff03e9b7 --- /dev/null +++ b/examples/5-migrations/generated/ormconfig.ts @@ -0,0 +1,25 @@ +import { SnakeNamingStrategy } from 'warthog'; + +module.exports = { + cli: { + entitiesDir: 'src/models', + migrationsDir: 'src/migration', + subscribersDir: 'src/subscriber' + }, + database: process.env.TYPEORM_DATABASE, + entities: process.env.TYPEORM_ENTITIES || ['src/**/*.model.ts'], + host: process.env.TYPEORM_HOST || 'localhost', + logger: 'advanced-console', + logging: process.env.TYPEORM_LOGGING || 'all', + migrations: ['src/migration/**/*.ts'], + namingStrategy: new SnakeNamingStrategy(), + password: process.env.TYPEORM_PASSWORD, + port: parseInt(process.env.TYPEORM_PORT || '', 10) || 5432, + subscribers: ['src/**/*.model.ts'], + synchronize: + typeof process.env.TYPEORM_SYNCHRONIZE !== 'undefined' + ? process.env.TYPEORM_SYNCHRONIZE + : process.env.NODE_ENV === 'development', + type: 'postgres', + username: process.env.TYPEORM_USERNAME +}; diff --git a/src/core/server.ts b/src/core/server.ts index 40f17cef..a4130597 100644 --- a/src/core/server.ts +++ b/src/core/server.ts @@ -2,6 +2,7 @@ // import { GraphQLDate, GraphQLDateTime, GraphQLTime } from 'graphql-iso-date'; import { ApolloServer } from 'apollo-server-express'; +import * as dotenv from 'dotenv'; import { Request } from 'express'; import express = require('express'); import { GraphQLSchema } from 'graphql'; @@ -59,6 +60,7 @@ export class Server { if (!process.env.NODE_ENV) { throw new Error("NODE_ENV must be set - use 'development' locally"); } + dotenv.config(); // Ensure that Warthog, TypeORM and TypeGraphQL are all using the same typedi container @@ -126,6 +128,7 @@ export class Server { async start() { await this.establishDBConnection(); + await this.buildGraphQLSchema(); await this.generateFiles(); const contextGetter = diff --git a/src/torm/createConnection.ts b/src/torm/createConnection.ts index 537cf40e..80e457a7 100644 --- a/src/torm/createConnection.ts +++ b/src/torm/createConnection.ts @@ -2,36 +2,14 @@ import { ConnectionOptions, createConnection } from 'typeorm'; import { SnakeNamingStrategy } from './SnakeNamingStrategy'; -const BASE_DB_CONFIG = { - cli: { - entitiesDir: 'src/models', - migrationsDir: 'src/migration', - subscribersDir: 'src/subscriber' - }, - database: process.env.TYPEORM_DATABASE, - entities: process.env.TYPEORM_ENTITIES || ['src/**/*.model.ts'], - host: process.env.TYPEORM_HOST || 'localhost', - logger: 'advanced-console', - logging: process.env.TYPEORM_LOGGING || 'all', - migrations: ['src/migration/**/*.ts'], - namingStrategy: new SnakeNamingStrategy(), - password: process.env.TYPEORM_PASSWORD, - port: parseInt(process.env.TYPEORM_PORT || '', 10) || 5432, - subscribers: ['src/**/*.model.ts'], - synchronize: - typeof process.env.TYPEORM_SYNCHRONIZE !== 'undefined' - ? process.env.TYPEORM_SYNCHRONIZE - : process.env.NODE_ENV === 'development', - type: 'postgres', - username: process.env.TYPEORM_USERNAME -}; - export const createDBConnection = (dbOptions: Partial = {}) => { const config = { - ...BASE_DB_CONFIG, + ...getBaseConfig(), ...dbOptions }; + // console.log('config: ', config); + if (!config.database) { throw new Error("createConnection: 'database' is required"); } @@ -51,3 +29,29 @@ export const mockDBConnection = (dbOptions: Partial = {}) => type: 'sqlite' } as any); }; + +function getBaseConfig() { + return { + cli: { + entitiesDir: 'src/models', + migrationsDir: 'src/migration', + subscribersDir: 'src/subscriber' + }, + database: process.env.TYPEORM_DATABASE, + entities: process.env.TYPEORM_ENTITIES || ['src/**/*.model.ts'], + host: process.env.TYPEORM_HOST || 'localhost', + logger: 'advanced-console', + logging: process.env.TYPEORM_LOGGING || 'all', + migrations: ['src/migration/**/*.ts'], + namingStrategy: new SnakeNamingStrategy(), + password: process.env.TYPEORM_PASSWORD, + port: parseInt(process.env.TYPEORM_PORT || '', 10) || 5432, + subscribers: ['src/**/*.model.ts'], + synchronize: + typeof process.env.TYPEORM_SYNCHRONIZE !== 'undefined' + ? process.env.TYPEORM_SYNCHRONIZE + : process.env.NODE_ENV === 'development', + type: 'postgres', + username: process.env.TYPEORM_USERNAME + }; +}