Skip to content

Commit

Permalink
feat(cli): migrations and config updates (#157)
Browse files Browse the repository at this point in the history
  • Loading branch information
goldcaddy77 authored Jul 2, 2019
1 parent 9121c74 commit 9428bfc
Show file tree
Hide file tree
Showing 26 changed files with 212 additions and 198 deletions.
2 changes: 1 addition & 1 deletion examples/1-simple-model/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "example1",
"version": "0.0.0",
"scripts": {
"bootstrap": "cd ../.. && yarn && cd - && yarn && yarn db:create && yarn codegen",
"bootstrap": "cd ../.. && yarn && yarn link && cd - && yarn && yarn db:create && yarn codegen",
"codegen": "warthog codegen",
"db:create": "warthog db:create",
"db:drop": "warthog db:drop",
Expand Down
2 changes: 1 addition & 1 deletion examples/2-complex-example/.env
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ WARTHOG_APP_PORT=4100
WARTHOG_DB_DATABASE=warthog-example-2
WARTHOG_DB_USERNAME=postgres
WARTHOG_DB_PASSWORD=
WARTHOG_DB_SYNCHRONIZE=true
WARTHOG_DB_SYNCHRONIZE=false
WARTHOG_RESOLVERS_PATH=./src/**/*.resolver.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { MigrationInterface, QueryRunner } from 'typeorm';

export class UserModel1562027331912 implements MigrationInterface {
public async up(queryRunner: QueryRunner): Promise<any> {
await queryRunner.query(
`CREATE TABLE "users" ("id" character varying NOT NULL, "created_at" TIMESTAMP NOT NULL DEFAULT now(), "created_by_id" character varying NOT NULL, "updated_at" TIMESTAMP DEFAULT now(), "updated_by_id" character varying, "deleted_at" TIMESTAMP, "deleted_by_id" character varying, "version" integer NOT NULL, "first_name" character varying(30) NOT NULL, "last_name" character varying(50) NOT NULL, "string_enum_field" character varying NOT NULL, "email" character varying NOT NULL, "nick_name" character varying(30), "private_field" character varying, "json_field" jsonb, CONSTRAINT "UQ_97672ac88f789774dd47f7c8be3" UNIQUE ("email"), CONSTRAINT "UQ_8b0d982b1bfea7d0518840535b2" UNIQUE ("first_name", "string_enum_field"), CONSTRAINT "PK_a3ffb1c0c8416b9fc6f907b7433" PRIMARY KEY ("id"))`
);
}

public async down(queryRunner: QueryRunner): Promise<any> {
await queryRunner.query(`DROP TABLE "users"`);
}
}
36 changes: 2 additions & 34 deletions examples/2-complex-example/generated/ormconfig.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,3 @@
import { getBaseConfig } from '../../../src';

import {
getDatabaseName,
getDatabaseEntityPaths,
getDatabaseHost,
getDatabaseLoggingLevel,
getDatabaseMigrationPaths,
getDatabaseSubscriberPaths,
getDatabasePassword,
getDatabasePort,
shouldSchronizeDatabaseSchema,
getDatabaseType,
getDatabaseUsername,
SnakeNamingStrategy } from '../../../src';

module.exports = {
cli: {
entitiesDir: 'src/models',
migrationsDir: 'db/migrations',
subscribersDir: 'src/subscribers'
},
database: getDatabaseName(),
entities: getDatabaseEntityPaths(),
host: getDatabaseHost(),
logger: 'advanced-console',
logging: getDatabaseLoggingLevel(),
migrations: getDatabaseMigrationPaths(),
namingStrategy: new SnakeNamingStrategy(),
password: getDatabasePassword(),
port: getDatabasePort(),
subscribers: getDatabaseSubscriberPaths(),
synchronize: shouldSchronizeDatabaseSchema(),
type: getDatabaseType(),
username: getDatabaseUsername()
};
module.exports = getBaseConfig();
2 changes: 1 addition & 1 deletion examples/2-complex-example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"version": "0.0.0",
"license": "MIT",
"scripts": {
"bootstrap": "cd ../.. && yarn && cd - && yarn && yarn db:create && yarn codegen && yarn db:seed:dev",
"bootstrap": "cd ../.. && yarn && yarn link && cd - && yarn && yarn db:create && yarn codegen && yarn db:seed:dev",
"codegen": "yarn warthog codegen",
"db:create": "yarn warthog db:create",
"db:drop": "yarn warthog db:drop",
Expand Down
2 changes: 1 addition & 1 deletion examples/3-one-to-many-relationship/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "example3",
"version": "0.0.0",
"scripts": {
"bootstrap": "cd ../.. && yarn && cd - && yarn && yarn db:create && yarn codegen",
"bootstrap": "cd ../.. && yarn && yarn link && cd - && yarn && yarn db:create && yarn codegen",
"codegen": "warthog codegen",
"db:create": "warthog db:create",
"db:drop": "warthog db:drop",
Expand Down
2 changes: 1 addition & 1 deletion examples/4-many-to-many-relationship/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "example4",
"version": "0.0.0",
"scripts": {
"bootstrap": "cd ../.. && yarn && cd - && yarn && yarn db:create && yarn codegen",
"bootstrap": "cd ../.. && yarn && yarn link && cd - && yarn && yarn db:create && yarn codegen",
"codegen": "warthog codegen",
"db:create": "warthog db:create",
"db:drop": "warthog db:drop",
Expand Down
3 changes: 2 additions & 1 deletion examples/5-migrations/generated/classes.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
// 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 { GraphQLID as ID } from "graphql";
import {
ArgsType,
Field as TypeGraphQLField,
Float,
ID,
InputType as TypeGraphQLInputType,
Int
} from "type-graphql";
Expand All @@ -15,6 +15,7 @@ import { registerEnumType } from "type-graphql";
const { GraphQLJSONObject } = require("graphql-type-json");

import { BaseWhereInput, PaginationArgs } from "../../../src";
// @ts-ignore
import { User } from "../src/user.model";

export enum UserOrderByEnum {
Expand Down
35 changes: 2 additions & 33 deletions examples/5-migrations/generated/ormconfig.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,4 @@

import {
getDatabaseName,
getDatabaseEntityPaths,
getDatabaseHost,
getDatabaseLoggingLevel,
getDatabaseMigrationPaths,
getDatabaseSubscriberPaths,
getDatabasePassword,
getDatabasePort,
shouldSchronizeDatabaseSchema,
getDatabaseType,
getDatabaseUsername,
SnakeNamingStrategy } from '../../../src';
import { getBaseConfig } from '../../../src';

module.exports = {
cli: {
entitiesDir: 'src/models',
migrationsDir: 'db/migrations',
subscribersDir: 'src/subscribers'
},
database: getDatabaseName(),
entities: getDatabaseEntityPaths(),
host: getDatabaseHost(),
logger: 'advanced-console',
logging: getDatabaseLoggingLevel(),
migrations: getDatabaseMigrationPaths(),
namingStrategy: new SnakeNamingStrategy(),
password: getDatabasePassword(),
port: getDatabasePort(),
subscribers: getDatabaseSubscriberPaths(),
synchronize: shouldSchronizeDatabaseSchema(),
type: getDatabaseType(),
username: getDatabaseUsername()
};
module.exports = getBaseConfig();
6 changes: 3 additions & 3 deletions examples/5-migrations/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
"name": "example5",
"version": "0.0.0",
"scripts": {
"bootstrap": "cd ../.. && yarn && cd - && yarn && yarn db:create && yarn codegen",
"bootstrap": "cd ../.. && yarn && yarn link && cd - && yarn && yarn db:create && yarn codegen",
"codegen": "warthog codegen",
"db:create": "warthog db:create",
"db:drop": "warthog db:drop",
"db:migration:generate": "yarn typeorm:cli migration:generate -n migration1",
"db:migration:run": "yarn typeorm:cli migration:run",
"db:migrate:generate": "warthog db:migrate:generate --name",
"db:migrate": "warthog db:migrate",
"db:query": "yarn typeorm:cli query 'select * from user;'",
"playground": "warthog playground",
"start": "yarn start:ts",
Expand Down
2 changes: 1 addition & 1 deletion examples/6-base-service/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"version": "0.0.0",
"license": "MIT",
"scripts": {
"bootstrap": "cd ../.. && yarn && cd - && yarn && yarn db:create && yarn codegen && yarn db:seed:dev",
"bootstrap": "cd ../.. && yarn && yarn link && cd - && yarn && yarn db:create && yarn codegen && yarn db:seed:dev",
"codegen": "warthog codegen",
"db:create": "warthog db:create",
"db:drop": "warthog db:drop",
Expand Down
2 changes: 1 addition & 1 deletion examples/7-feature-flags/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"version": "0.0.0",
"license": "MIT",
"scripts": {
"bootstrap": "cd ../.. && yarn && cd - && yarn && yarn db:create && yarn codegen && yarn db:seed:dev",
"bootstrap": "cd ../.. && yarn && yarn link && cd - && yarn && yarn db:create && yarn codegen && yarn db:seed:dev",
"codegen": "warthog codegen",
"db:create": "warthog db:create",
"db:drop": "warthog db:drop",
Expand Down
2 changes: 1 addition & 1 deletion examples/8-performance/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "example8",
"version": "0.0.0",
"scripts": {
"bootstrap": "cd ../.. && yarn && cd - && yarn && yarn db:create && yarn codegen && yarn db:seed:dev",
"bootstrap": "cd ../.. && yarn && yarn link && cd - && yarn && yarn db:create && yarn codegen && yarn db:seed:dev",
"codegen": "warthog codegen",
"db:create": "warthog db:create",
"db:drop": "warthog db:drop",
Expand Down
10 changes: 8 additions & 2 deletions src/cli/commands/dbCreate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,13 @@ import { WarthogGluegunToolbox } from '../types';
export default {
name: 'db:create',
run: async (toolbox: WarthogGluegunToolbox) => {
const { db } = toolbox;
await db.create();
const {
db,
config: { load }
} = toolbox;

const config = load();

await db.create(config.get('DB_DATABASE'));
}
};
20 changes: 20 additions & 0 deletions src/cli/commands/migrate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// "db:migration:generate": "yarn typeorm:cli migration:generate -n migration1",
// ts-node ./node_modules/.bin/typeorm -f ./generated/ormconfig.ts

import { WarthogGluegunToolbox } from '../types';

export default {
name: 'db:migrate',
run: async (toolbox: WarthogGluegunToolbox) => {
const {
db,
print: { error }
} = toolbox;

try {
await db.migrate();
} catch (e) {
error(e);
}
}
};
26 changes: 26 additions & 0 deletions src/cli/commands/migrationGenerate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// "db:migration:generate": "yarn typeorm:cli migration:generate -n migration1",
// ts-node ./node_modules/.bin/typeorm -f ./generated/ormconfig.ts

import { WarthogGluegunToolbox } from '../types';

export default {
name: 'db:migrate:generate',
run: async (toolbox: WarthogGluegunToolbox) => {
const {
db,
parameters: { options },
print: { error }
} = toolbox;

if (!options.name) {
error('"name" option is required');
process.exit(1);
}

try {
await db.generateMigration(options.name);
} catch (e) {
error(e);
}
}
};
Loading

0 comments on commit 9428bfc

Please sign in to comment.