Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from Jun 23, 2019
Merged
Changes from all commits
Commits
File filter...
Filter file types
Jump to…
Jump to file or symbol
Failed to load files and symbols.

Always

Just for now

@@ -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
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.