Quellcode durchsuchen

chore(dev-server): Make db type configurable from args

Michael Bromley vor 6 Jahren
Ursprung
Commit
94bd276d69

+ 3 - 1
package.json

@@ -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",

+ 1 - 0
packages/dev-server/.gitignore

@@ -1,3 +1,4 @@
 assets
 vendure
 test-emails
+vendure.sqlite

+ 15 - 1
packages/dev-server/README.md

@@ -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
+```

+ 40 - 18
packages/dev-server/dev-config.ts

@@ -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
@@ -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'),
     },
@@ -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',
+            };
+    }
+}

+ 3 - 1
packages/dev-server/nodemon-debug.json

@@ -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}}"
+  }
 }

+ 1 - 1
packages/dev-server/package.json

@@ -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",