Skip to content

Commit

Permalink
chore(dev-server): Make db type configurable from args
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelbromley committed Apr 25, 2019
1 parent 406ab28 commit 94bd276
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 22 deletions.
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
"precommit": "lint-staged",
"postcommit": "git update-index --again",
"prepush": "yarn test:all && cd admin-ui && yarn build --prod",
"dev-server": "cd packages/dev-server && yarn start",
"dev-server:mysql": "cd packages/dev-server && yarn start --db=mysql",
"dev-server:postgres": "cd packages/dev-server && yarn start --db=postgres",
"dev-server:sqlite": "cd packages/dev-server && yarn start --db=sqlite",
"dev-server:populate": "cd packages/dev-server && yarn populate",
"test:all": "cd admin-ui && yarn test --watch=false --browsers=ChromeHeadlessCI --progress=false && cd ../ && yarn test:common && yarn test:core && yarn test:email-plugin && yarn test:e2e",
"test:common": "jest --config packages/common/jest.config.js",
Expand Down
1 change: 1 addition & 0 deletions packages/dev-server/.gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
assets
vendure
test-emails
vendure.sqlite
16 changes: 15 additions & 1 deletion packages/dev-server/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,22 @@ This package is not published to npm. It is used in development of the Vendure s

### Running

To run the server, run the `start` script.
To run the server, run the `start` script. The database configuration can be specified by the `--db=<type>` flag:

```bash
yarn start --db=mysql
yarn start --db=postgres
yarn start --db=sqlite
```

The default if no db is specified is mysql.

### Populating data

Test data can be populated by running the `populate` script. This uses the same sample data as is used by the Vendure CLI when running `init`, albeit with the additional step of populating some sample customer & address data too.

Specify the database as above to populate that database:

```bash
yarn populate --db=sqlite
```
58 changes: 40 additions & 18 deletions packages/dev-server/dev-config.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
/* tslint:disable:no-console */
import { AdminUiPlugin } from '@vendure/admin-ui-plugin';
import { AssetServerPlugin } from '@vendure/asset-server-plugin';
import { ADMIN_API_PATH, API_PORT, SHOP_API_PATH } from '@vendure/common/lib/shared-constants';
import { DefaultLogger, DefaultSearchPlugin, examplePaymentHandler, LogLevel, VendureConfig } from '@vendure/core';
import { defaultEmailHandlers, EmailPlugin } from '@vendure/email-plugin';
import path from 'path';
import { ConnectionOptions } from 'typeorm';

/**
* Config settings used during development
Expand All @@ -20,29 +22,13 @@ export const devConfig: VendureConfig = {
dbConnectionOptions: {
synchronize: false,
logging: false,

type: 'mysql',
host: '192.168.99.100',
port: 3306,
username: 'root',
password: '',
database: 'vendure-dev',

// type: 'sqlite',
// database: path.join(__dirname, 'vendure.sqlite'),

// type: 'postgres',
// host: '127.0.0.1',
// port: 5432,
// username: 'postgres',
// password: 'Be70',
// database: 'vendure',
...getDbConfig(),
},
paymentOptions: {
paymentMethodHandlers: [examplePaymentHandler],
},
customFields: {},
logger: new DefaultLogger({ level: LogLevel.Debug }),
logger: new DefaultLogger({ level: LogLevel.Info }),
importExportOptions: {
importAssetsDir: path.join(__dirname, 'import-assets'),
},
Expand Down Expand Up @@ -70,3 +56,39 @@ export const devConfig: VendureConfig = {
}),
],
};

function getDbConfig(): ConnectionOptions {
const match = process.argv
.filter(arg => arg.match(/--db=/))
.map(arg => arg.replace(/^--db=/, ''));
const dbType = match.length ? match[0] : 'mysql';
switch (dbType) {
case 'postgres':
console.log('Using postgres connection');
return {
type: 'postgres',
host: '127.0.0.1',
port: 5432,
username: 'postgres',
password: 'Be70',
database: 'vendure',
};
case 'sqlite':
console.log('Using sqlite connection');
return {
type: 'sqlite',
database: path.join(__dirname, 'vendure.sqlite'),
};
case 'mysql':
default:
console.log('Using mysql connection');
return {
type: 'mysql',
host: '192.168.99.100',
port: 3306,
username: 'root',
password: '',
database: 'vendure-dev',
};
}
}
4 changes: 3 additions & 1 deletion packages/dev-server/nodemon-debug.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,7 @@
"watch": ["index.ts", "dev-config.ts"],
"ext": "ts",
"ignore": ["src/**/*.spec.ts", "mock-data/**/*"],
"exec": "node --inspect=5858 -r ts-node/register index.ts"
"execMap": {
"ts": "node --inspect=5858 -r ts-node/register {{filename}}"
}
}
2 changes: 1 addition & 1 deletion packages/dev-server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"private": true,
"scripts": {
"populate": "node -r ts-node/register populate-dev-server.ts",
"start": "nodemon --config nodemon-debug.json"
"start": "nodemon --config nodemon-debug.json index.ts"
},
"dependencies": {
"@vendure/common": "~0.1.0",
Expand Down

0 comments on commit 94bd276

Please sign in to comment.