Skip to content

Commit

Permalink
feat(cli): adds db:create and db:drop (goldcaddy77#133)
Browse files Browse the repository at this point in the history
  • Loading branch information
goldcaddy77 committed Jun 23, 2019
1 parent 27a8ab2 commit 61cc092
Show file tree
Hide file tree
Showing 37 changed files with 371 additions and 56 deletions.
3 changes: 2 additions & 1 deletion bin/warthog
@@ -1,4 +1,5 @@
#!/usr/bin/env node
const path = require('path');

/* tslint:disable */
// check if we're running in dev mode
Expand All @@ -12,7 +13,7 @@ if (wantsCompiled || !devMode) {
} else {
// this runs from the typescript source (for dev only)
// hook into ts-node so we can run typescript on the fly
require('ts-node').register({ project: `${__dirname}/../tsconfig.json` });
require('ts-node').register({ project: path.join(process.cwd(), 'tsconfig.json') });
// run the CLI with the current process arguments
require(`${__dirname}/../cli/cli`).run(process.argv);
}
3 changes: 3 additions & 0 deletions cli/cli.ts
@@ -1,9 +1,12 @@
import { build } from 'gluegun';
import * as dotenv from 'dotenv';

/**
* Create the cli and kick it off
*/
async function run(argv: string[]) {
dotenv.load();

// create a CLI runtime
const cli = build()
.brand('warthog')
Expand Down
9 changes: 9 additions & 0 deletions cli/commands/dbCreate.ts
@@ -0,0 +1,9 @@
import { WarthogGluegunToolbox } from '../types';

module.exports = {
name: 'db:create',
run: async (toolbox: WarthogGluegunToolbox) => {
const { db } = toolbox;
await db.create();
}
};
9 changes: 9 additions & 0 deletions cli/commands/dbDrop.ts
@@ -0,0 +1,9 @@
import { WarthogGluegunToolbox } from '../types';

module.exports = {
name: 'db:drop',
run: async (toolbox: WarthogGluegunToolbox) => {
const { db } = toolbox;
await db.drop();
}
};
13 changes: 13 additions & 0 deletions cli/config.ts
@@ -0,0 +1,13 @@
import * as cosmiconfig from 'cosmiconfig';
import { getBaseConfig } from '../src';

export async function getConfig() {
const explorer = cosmiconfig('warthog');
const config = await explorer.search();
const baseConfig = getBaseConfig();

return {
...config,
...baseConfig
};
}
17 changes: 0 additions & 17 deletions cli/extensions/cli-extension.ts

This file was deleted.

50 changes: 50 additions & 0 deletions cli/extensions/db-extension.ts
@@ -0,0 +1,50 @@
import { GluegunToolbox } from 'gluegun';
// TypeORM doesn't allow for the creation of DBs via QueryRunner, so use pgtools
// @ts-ignore
import * as pgtools from 'pgtools';
import * as util from 'util';

import { getConfig } from '../config';

module.exports = (toolbox: GluegunToolbox) => {
toolbox.db = {
create: async function create() {
const config = await getConfig();
const createDb = util.promisify(pgtools.createdb) as Function;

try {
await createDb(getPgConfig(), config.database);
} catch (error) {
if (error.message.indexOf('duplicate') > 0) {
toolbox.print.error(`Database '${config.database}' already exists`);
return;
}
}
toolbox.print.info(`Database '${config.database}' created!`);
},
drop: async function create() {
const config = await getConfig();
const dropDb = util.promisify(pgtools.dropdb) as Function;

try {
await dropDb(getPgConfig(), config.database);
} catch (error) {
if (error.name === 'invalid_catalog_name') {
toolbox.print.error(`Database '${config.database}' does not exist`);
return;
}
}
toolbox.print.info(`Database '${config.database}' dropped!`);
}
};
};

async function getPgConfig() {
const config = await getConfig();
return {
host: config.host,
user: config.username,
password: config.password,
port: config.port
};
}
8 changes: 8 additions & 0 deletions cli/types.ts
@@ -0,0 +1,8 @@
import { GluegunToolbox } from 'gluegun';

export interface WarthogGluegunToolbox extends GluegunToolbox {
db: {
create: Function;
drop: Function;
};
}
1 change: 1 addition & 0 deletions examples/1-simple-model/.gitignore
@@ -0,0 +1 @@
warthog.sqlite.tmp
5 changes: 2 additions & 3 deletions examples/1-simple-model/package.json
Expand Up @@ -2,10 +2,9 @@
"name": "example1",
"version": "0.0.0",
"scripts": {
"//": "make sure to install top-level packages, too",
"bootstrap": "cd ../.. && yarn && cd - && yarn && yarn db:create && yarn generate:code",
"db:create": "createdbjs $(dotenv -p TYPEORM_DATABASE) 2>&1 || :",
"db:drop": "dropdbjs $(dotenv -p TYPEORM_DATABASE) 2>&1 || :",
"db:create": "warthog db:create",
"db:drop": "warthog db:drop",
"generate:code": "ts-node tools/generate.ts",
"playground:open": "open http://localhost:$(dotenv -p APP_PORT)/graphql",
"start": "yarn start:ts",
Expand Down
1 change: 1 addition & 0 deletions examples/2-complex-example/.gitignore
@@ -0,0 +1 @@
warthog.sqlite.tmp
5 changes: 2 additions & 3 deletions examples/2-complex-example/package.json
Expand Up @@ -3,10 +3,9 @@
"version": "0.0.0",
"license": "MIT",
"scripts": {
"//": "make sure to install top-level packages, too",
"bootstrap": "cd ../.. && yarn && cd - && yarn && yarn db:create && yarn generate:code && yarn db:seed:dev",
"db:create": "createdbjs $(dotenv -p TYPEORM_DATABASE) 2>&1 || :",
"db:drop": "dropdbjs $(dotenv -p TYPEORM_DATABASE) 2>&1 || :",
"db:create": "warthog db:create",
"db:drop": "warthog db:drop",
"db:seed:dev": "dotenv -- ts-node tools/seed.ts",
"generate:code": "ts-node tools/generate.ts",
"playground:open": "open http://localhost:$(dotenv -p APP_PORT)/playground",
Expand Down
1 change: 1 addition & 0 deletions examples/3-one-to-many-relationship/.gitignore
@@ -0,0 +1 @@
warthog.sqlite.tmp
5 changes: 2 additions & 3 deletions examples/3-one-to-many-relationship/package.json
Expand Up @@ -2,10 +2,9 @@
"name": "example3",
"version": "0.0.0",
"scripts": {
"//": "make sure to install top-level packages, too",
"bootstrap": "cd ../.. && yarn && cd - && yarn && yarn db:create && yarn generate:code",
"db:create": "createdbjs $(dotenv -p TYPEORM_DATABASE) 2>&1 || :",
"db:drop": "dropdbjs $(dotenv -p TYPEORM_DATABASE) 2>&1 || :",
"db:create": "warthog db:create",
"db:drop": "warthog db:drop",
"generate:code": "ts-node tools/generate.ts",
"playground:open": "open http://localhost:$(dotenv -p APP_PORT)/graphql",
"start": "yarn start:ts",
Expand Down
Binary file not shown.
1 change: 1 addition & 0 deletions examples/4-many-to-many-relationship/.gitignore
@@ -0,0 +1 @@
warthog.sqlite.tmp
5 changes: 2 additions & 3 deletions examples/4-many-to-many-relationship/package.json
Expand Up @@ -2,10 +2,9 @@
"name": "example4",
"version": "0.0.0",
"scripts": {
"//": "make sure to install top-level packages, too",
"bootstrap": "cd ../.. && yarn && cd - && yarn && yarn db:create && yarn generate:code",
"db:create": "createdbjs $(dotenv -p TYPEORM_DATABASE) 2>&1 || :",
"db:drop": "dropdbjs $(dotenv -p TYPEORM_DATABASE) 2>&1 || :",
"db:create": "warthog db:create",
"db:drop": "warthog db:drop",
"generate:code": "ts-node tools/generate.ts",
"playground:open": "open http://localhost:$(dotenv -p APP_PORT)/graphql",
"start": "yarn start:ts",
Expand Down
1 change: 1 addition & 0 deletions examples/5-migrations/.gitignore
@@ -0,0 +1 @@
warthog.sqlite.tmp
41 changes: 41 additions & 0 deletions examples/5-migrations/generated/binding.ts
Expand Up @@ -48,6 +48,42 @@ export type UserOrderByInput = 'createdAt_ASC' |
'email_ASC' |
'email_DESC'

export interface BaseWhereInput {
id_eq?: String | null
id_in?: String[] | String | null
createdAt_eq?: String | null
createdAt_lt?: String | null
createdAt_lte?: String | null
createdAt_gt?: String | null
createdAt_gte?: String | null
createdById_eq?: String | null
updatedAt_eq?: String | null
updatedAt_lt?: String | null
updatedAt_lte?: String | null
updatedAt_gt?: String | null
updatedAt_gte?: String | null
updatedById_eq?: String | null
deletedAt_all?: Boolean | null
deletedAt_eq?: String | null
deletedAt_lt?: String | null
deletedAt_lte?: String | null
deletedAt_gt?: String | null
deletedAt_gte?: String | null
deletedById_eq?: String | null
}

export interface UserCreateInput {
firstName: String
lastName?: String | null
email: String
}

export interface UserUpdateInput {
firstName?: String | null
lastName?: String | null
email?: String | null
}

export interface UserWhereInput {
id_eq?: String | null
id_in?: String[] | String | null
Expand Down Expand Up @@ -87,6 +123,11 @@ export interface UserWhereInput {
email_in?: String[] | String | null
}

export interface UserWhereUniqueInput {
id?: String | null
email?: String | null
}

export interface BaseGraphQLObject {
id: ID_Output
createdAt: DateTime
Expand Down
2 changes: 1 addition & 1 deletion examples/5-migrations/generated/classes.ts
Expand Up @@ -14,7 +14,7 @@ import { registerEnumType } from "type-graphql";
// eslint-disable-next-line @typescript-eslint/no-var-requires
const { GraphQLJSONObject } = require("graphql-type-json");

import { BaseWhereInput, PaginationArgs } from "warthog";
import { BaseWhereInput, PaginationArgs } from "../../../src";
import { User } from "../src/user.model";

export enum UserOrderByEnum {
Expand Down
2 changes: 1 addition & 1 deletion examples/5-migrations/generated/ormconfig.ts
Expand Up @@ -11,7 +11,7 @@ import {
shouldSchronizeDatabaseSchema,
getDatabaseType,
getDatabaseUsername,
SnakeNamingStrategy } from 'undefined';
SnakeNamingStrategy } from '../../../src';

module.exports = {
cli: {
Expand Down
41 changes: 41 additions & 0 deletions examples/5-migrations/generated/schema.graphql
Expand Up @@ -31,6 +31,30 @@ type BaseModelUUID implements BaseGraphQLObject {
version: Int!
}

input BaseWhereInput {
id_eq: String
id_in: [String!]
createdAt_eq: String
createdAt_lt: String
createdAt_lte: String
createdAt_gt: String
createdAt_gte: String
createdById_eq: String
updatedAt_eq: String
updatedAt_lt: String
updatedAt_lte: String
updatedAt_gt: String
updatedAt_gte: String
updatedById_eq: String
deletedAt_all: Boolean
deletedAt_eq: String
deletedAt_lt: String
deletedAt_lte: String
deletedAt_gt: String
deletedAt_gte: String
deletedById_eq: String
}

"""
The javascript `Date` as string. Type represents date and time as the ISO Date string.
"""
Expand Down Expand Up @@ -62,6 +86,12 @@ type User implements BaseGraphQLObject {
email: String!
}

input UserCreateInput {
firstName: String!
lastName: String
email: String!
}

enum UserOrderByInput {
createdAt_ASC
createdAt_DESC
Expand All @@ -77,6 +107,12 @@ enum UserOrderByInput {
email_DESC
}

input UserUpdateInput {
firstName: String
lastName: String
email: String
}

input UserWhereInput {
id_eq: String
id_in: [String!]
Expand Down Expand Up @@ -115,3 +151,8 @@ input UserWhereInput {
email_endsWith: String
email_in: [String!]
}

input UserWhereUniqueInput {
id: String
email: String
}
5 changes: 2 additions & 3 deletions examples/5-migrations/package.json
Expand Up @@ -2,10 +2,9 @@
"name": "example5",
"version": "0.0.0",
"scripts": {
"//": "make sure to install top-level packages, too",
"bootstrap": "cd ../.. && yarn && cd - && yarn && yarn db:create && yarn generate:code",
"db:create": "createdbjs $(dotenv -p TYPEORM_DATABASE) 2>&1 || :",
"db:drop": "dropdbjs $(dotenv -p TYPEORM_DATABASE) 2>&1 || :",
"db:create": "warthog db:create",
"db:drop": "warthog db:drop",
"generate:code": "dotenv -- ts-node tools/generate.ts",
"playground:open": "open http://localhost:$(dotenv -p APP_PORT)/graphql",
"start": "yarn start:ts",
Expand Down
1 change: 1 addition & 0 deletions examples/6-base-service/.gitignore
@@ -0,0 +1 @@
warthog.sqlite.tmp
5 changes: 2 additions & 3 deletions examples/6-base-service/package.json
Expand Up @@ -3,10 +3,9 @@
"version": "0.0.0",
"license": "MIT",
"scripts": {
"//": "make sure to install top-level packages, too",
"bootstrap": "cd ../.. && yarn && cd - && yarn && yarn db:create && yarn generate:code && yarn db:seed:dev",
"db:create": "createdbjs $(dotenv -p TYPEORM_DATABASE) 2>&1 || :",
"db:drop": "dropdbjs $(dotenv -p TYPEORM_DATABASE) 2>&1 || :",
"db:create": "warthog db:create",
"db:drop": "warthog db:drop",
"db:seed:dev": "dotenv -- ts-node tools/seed.ts",
"generate:code": "dotenv -- ts-node tools/generate.ts",
"playground:open": "open http://localhost:$(dotenv -p APP_PORT)/playground",
Expand Down
1 change: 1 addition & 0 deletions examples/7-feature-flags/.gitignore
@@ -0,0 +1 @@
warthog.sqlite.tmp
5 changes: 2 additions & 3 deletions examples/7-feature-flags/package.json
Expand Up @@ -3,10 +3,9 @@
"version": "0.0.0",
"license": "MIT",
"scripts": {
"//": "make sure to install top-level packages, too",
"bootstrap": "cd ../.. && yarn && cd - && yarn && yarn db:create && yarn generate:code && yarn db:seed:dev",
"db:create": "createdbjs $(dotenv -p TYPEORM_DATABASE) 2>&1 || :",
"db:drop": "dropdbjs $(dotenv -p TYPEORM_DATABASE) 2>&1 || :",
"db:create": "warthog db:create",
"db:drop": "warthog db:drop",
"db:seed:dev": "ts-node tools/seed.ts",
"generate:code": "ts-node tools/generate.ts",
"playground:open": "open http://localhost:$(dotenv -p APP_PORT)/playground",
Expand Down
5 changes: 5 additions & 0 deletions examples/7-feature-flags/warthog.config.js
@@ -0,0 +1,5 @@
module.exports = {
cli: {
generatePath: './src/{kebabName}'
}
};
1 change: 1 addition & 0 deletions examples/8-performance/.gitignore
@@ -0,0 +1 @@
warthog.sqlite.tmp

0 comments on commit 61cc092

Please sign in to comment.