Skip to content

Commit

Permalink
fix(bugs): fix bugs introduced by previous commits
Browse files Browse the repository at this point in the history
  • Loading branch information
goldcaddy77 committed Jan 26, 2019
1 parent 23d6b61 commit 89eb4b8
Show file tree
Hide file tree
Showing 9 changed files with 196 additions and 38 deletions.
3 changes: 0 additions & 3 deletions examples/1-simple-model/src/index.ts
Original file line number Diff line number Diff line change
@@ -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() {
Expand Down
4 changes: 0 additions & 4 deletions examples/2-complex-example/src/index.ts
Original file line number Diff line number Diff line change
@@ -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();
Expand Down
3 changes: 0 additions & 3 deletions examples/3-one-to-many-relationship/src/index.ts
Original file line number Diff line number Diff line change
@@ -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() {
Expand Down
3 changes: 0 additions & 3 deletions examples/4-many-to-many-relationship/src/index.ts
Original file line number Diff line number Diff line change
@@ -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() {
Expand Down
138 changes: 138 additions & 0 deletions examples/5-migrations/generated/classes.ts
Original file line number Diff line number Diff line change
@@ -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;
}
1 change: 1 addition & 0 deletions examples/5-migrations/generated/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './classes';
25 changes: 25 additions & 0 deletions examples/5-migrations/generated/ormconfig.ts
Original file line number Diff line number Diff line change
@@ -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
};
3 changes: 3 additions & 0 deletions src/core/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -59,6 +60,7 @@ export class Server<C extends BaseContext> {
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

Expand Down Expand Up @@ -126,6 +128,7 @@ export class Server<C extends BaseContext> {

async start() {
await this.establishDBConnection();
await this.buildGraphQLSchema();
await this.generateFiles();

const contextGetter =
Expand Down
54 changes: 29 additions & 25 deletions src/torm/createConnection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<ConnectionOptions> = {}) => {
const config = {
...BASE_DB_CONFIG,
...getBaseConfig(),
...dbOptions
};

// console.log('config: ', config);

if (!config.database) {
throw new Error("createConnection: 'database' is required");
}
Expand All @@ -51,3 +29,29 @@ export const mockDBConnection = (dbOptions: Partial<ConnectionOptions> = {}) =>
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
};
}

0 comments on commit 89eb4b8

Please sign in to comment.