Skip to content
Permalink
Browse files

feat(cli): adds db:create and db:drop (#133)

  • Loading branch information...
goldcaddy77 committed Jun 23, 2019
1 parent ceb0fff commit f5b8b103605867a81b87b81eec50be58077f2a2a
Showing with 371 additions and 56 deletions.
  1. +2 −1 bin/warthog
  2. +3 −0 cli/cli.ts
  3. +9 −0 cli/commands/dbCreate.ts
  4. +9 −0 cli/commands/dbDrop.ts
  5. +13 −0 cli/config.ts
  6. +0 −17 cli/extensions/cli-extension.ts
  7. +50 −0 cli/extensions/db-extension.ts
  8. +8 −0 cli/types.ts
  9. +1 −0 examples/1-simple-model/.gitignore
  10. +2 −3 examples/1-simple-model/package.json
  11. +1 −0 examples/2-complex-example/.gitignore
  12. +2 −3 examples/2-complex-example/package.json
  13. +1 −0 examples/3-one-to-many-relationship/.gitignore
  14. +2 −3 examples/3-one-to-many-relationship/package.json
  15. BIN examples/3-one-to-many-relationship/warthog.sqlite.tmp
  16. +1 −0 examples/4-many-to-many-relationship/.gitignore
  17. +2 −3 examples/4-many-to-many-relationship/package.json
  18. +1 −0 examples/5-migrations/.gitignore
  19. +41 −0 examples/5-migrations/generated/binding.ts
  20. +1 −1 examples/5-migrations/generated/classes.ts
  21. +1 −1 examples/5-migrations/generated/ormconfig.ts
  22. +41 −0 examples/5-migrations/generated/schema.graphql
  23. +2 −3 examples/5-migrations/package.json
  24. +1 −0 examples/6-base-service/.gitignore
  25. +2 −3 examples/6-base-service/package.json
  26. +1 −0 examples/7-feature-flags/.gitignore
  27. +2 −3 examples/7-feature-flags/package.json
  28. +5 −0 examples/7-feature-flags/warthog.config.js
  29. +1 −0 examples/8-performance/.gitignore
  30. +3 −4 examples/8-performance/package.json
  31. +1 −1 examples/8-performance/tools/seed.ts
  32. +3 −0 package.json
  33. +1 −3 src/torm/createConnection.ts
  34. +2 −1 tsconfig.json
  35. +1 −1 tsconfig.test.json
  36. +1 −0 typings/typings.d.ts
  37. +154 −5 yarn.lock
@@ -1,4 +1,5 @@
#!/usr/bin/env node
const path = require('path');

/* tslint:disable */
// check if we're running in dev mode
@@ -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);
}
@@ -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')
@@ -0,0 +1,9 @@
import { WarthogGluegunToolbox } from '../types';

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

module.exports = {
name: 'db:drop',
run: async (toolbox: WarthogGluegunToolbox) => {
const { db } = toolbox;
await db.drop();
}
};
@@ -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
};
}

This file was deleted.

@@ -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
};
}
@@ -0,0 +1,8 @@
import { GluegunToolbox } from 'gluegun';

export interface WarthogGluegunToolbox extends GluegunToolbox {
db: {
create: Function;
drop: Function;
};
}
@@ -0,0 +1 @@
warthog.sqlite.tmp
@@ -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",
@@ -0,0 +1 @@
warthog.sqlite.tmp
@@ -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",
@@ -0,0 +1 @@
warthog.sqlite.tmp
@@ -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",
Binary file not shown.
@@ -0,0 +1 @@
warthog.sqlite.tmp
@@ -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",
@@ -0,0 +1 @@
warthog.sqlite.tmp
@@ -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
@@ -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
@@ -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 {
@@ -11,7 +11,7 @@ import {
shouldSchronizeDatabaseSchema,
getDatabaseType,
getDatabaseUsername,
SnakeNamingStrategy } from 'undefined';
SnakeNamingStrategy } from '../../../src';

module.exports = {
cli: {
@@ -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.
"""
@@ -62,6 +86,12 @@ type User implements BaseGraphQLObject {
email: String!
}

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

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

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

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

input UserWhereUniqueInput {
id: String
email: String
}
@@ -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",
@@ -0,0 +1 @@
warthog.sqlite.tmp
@@ -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",
@@ -0,0 +1 @@
warthog.sqlite.tmp
@@ -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",
@@ -0,0 +1,5 @@
module.exports = {
cli: {
generatePath: './src/{kebabName}'
}
};
@@ -0,0 +1 @@
warthog.sqlite.tmp

0 comments on commit f5b8b10

Please sign in to comment.
You can’t perform that action at this time.