Browse Source

feat(create): Include migrations & general DX improvements

Michael Bromley 3 years ago
parent
commit
2af85d0cc2

+ 8 - 4
packages/create/src/create-vendure-app.ts

@@ -109,10 +109,13 @@ async function createApp(
         version: '0.1.0',
         private: true,
         scripts: {
-            'run:server': 'ts-node ./src/index.ts',
-            'run:worker': 'ts-node ./src/index-worker.ts',
-            start: useYarn ? 'concurrently yarn:run:*' : 'concurrently npm:run:*',
+            'dev:server': 'ts-node ./src/index.ts',
+            'dev:worker': 'ts-node ./src/index-worker.ts',
+            dev: useYarn ? 'concurrently yarn:dev:*' : 'concurrently npm:dev:*',
             build: 'tsc',
+            'start:server': 'node ./dist/index.js',
+            'start:worker': 'node ./dist/index-worker.js',
+            start: useYarn ? 'concurrently yarn:start:*' : 'concurrently npm:start:*',
             'migration:generate': 'ts-node migration generate',
             'migration:run': 'ts-node migration run',
             'migration:revert': 'ts-node migration revert',
@@ -181,6 +184,7 @@ async function createApp(
                             .then(() => fs.writeFile(srcPathScript('index-worker'), indexWorkerSource))
                             .then(() => fs.writeFile(rootPathScript('migration'), migrationSource))
                             .then(() => fs.writeFile(path.join(root, 'README.md'), readmeSource))
+                            .then(() => fs.mkdir(path.join(root, 'src/plugins')))
                             .then(() =>
                                 fs.copyFile(assetPath('gitignore.template'), path.join(root, '.gitignore')),
                             )
@@ -284,7 +288,7 @@ async function createApp(
     } catch (e) {
         process.exit(1);
     }
-    const startCommand = useYarn ? 'yarn start' : 'npm run start';
+    const startCommand = useYarn ? 'yarn dev' : 'npm run dev';
     console.log();
     console.log(chalk.green(`Success! Created a new Vendure server at ${root}`));
     console.log();

+ 6 - 4
packages/create/templates/index.hbs

@@ -1,6 +1,8 @@
-import { bootstrap } from '@vendure/core';
+import { bootstrap, runMigrations } from '@vendure/core';
 import { config } from './vendure-config';
 
-bootstrap(config).catch(err => {
-    console.log(err);
-});
+runMigrations(config)
+    .then(() => bootstrap(config))
+    .catch(err => {
+        console.log(err);
+    });

+ 29 - 5
packages/create/templates/readme.hbs

@@ -2,6 +2,13 @@
 
 This project was generated with [`@vendure/create`](https://github.com/vendure-ecommerce/vendure/tree/master/packages/create).
 
+Useful links:
+
+- [Vendure docs](https://www.vendure.io/docs)
+- [Vendure Slack community](https://join.slack.com/t/vendure-ecommerce/shared_invite/enQtNzA1NTcyMDY3NTg0LTMzZGQzNDczOWJiMTU2YjAyNWJlMzdmZGE3ZDY5Y2RjMGYxZWNlYTI4NmU4Y2Q1MDNlYzE4MzQ5ODcyYTdmMGU)
+- [Vendure on GitHub](https://github.com/vendure-ecommerce/vendure)
+- [Vendure plugin template](https://github.com/vendure-ecommerce/plugin-template)
+
 ## Directory structure
 
 * `/src` contains the source code of your Vendure server. All your custom code and plugins should reside here.
@@ -10,9 +17,9 @@ This project was generated with [`@vendure/create`](https://github.com/vendure-e
 ## Development
 
 ```
-yarn start
+yarn dev
 # or
-npm run start
+npm run dev
 ```
 
 will start the Vendure server and [worker](https://www.vendure.io/docs/developer-guide/vendure-worker/) processes from
@@ -28,9 +35,15 @@ npm run build
 
 will compile the TypeScript sources into the `/dist` directory.
 
+## Plugins
+
+In Vendure, your custom functionality will live in [plugins](https://www.vendure.io/docs/plugins/).
+These should be located in the `./src/plugins` directory.
+
 ## Migrations
 
-[Migrations](https://www.vendure.io/docs/developer-guide/migrations/) allow safe updates to the database schema.
+[Migrations](https://www.vendure.io/docs/developer-guide/migrations/) allow safe updates to the database schema. Migrations
+will be required whenever you make changes to the `customFields` config or define new entities in a plugin.
 
 The following npm scripts can be used to generate migrations:
 
@@ -40,7 +53,18 @@ yarn migration:generate [name]
 npm run migration:generate [name]
 ```
 
-run any pending migrations that have been generated:
+The generated migration file will be found in the `./src/migrations/` directory, and should be committed to source control.
+Next time you start the server, and outstanding migrations found in that directory will be run by the `runMigrations()`
+function in the [index.ts file](./src/index.ts).
+
+If, during initial development, you do not wish to manually generate a migration on each change to customFields etc, you
+can set `dbConnectionOptions.synchronize` to `true`. This will cause the database schema to get automatically updated
+on each start, removing the need for migration files. Note that this is **not** recommended once you have production
+data that you cannot lose.
+
+---
+
+You can also run any pending migrations manually, without starting the server by running:
 
 ```
 yarn migration:run
@@ -48,7 +72,7 @@ yarn migration:run
 npm run migration:run
 ```
 
-and revert the most recently-applied migration:
+You can revert the most recently-applied migration with:
 
 ```
 yarn migration:revert

+ 2 - 1
packages/create/templates/tsconfig.template.json

@@ -5,7 +5,8 @@
     "esModuleInterop": true,
     "emitDecoratorMetadata": true,
     "experimentalDecorators": true,
-    "target": "es2017",
+    "strictPropertyInitialization": false,
+    "target": "es2019",
     "strict": true,
     "sourceMap": false,
     "skipLibCheck": true,

+ 9 - 2
packages/create/templates/vendure-config.hbs

@@ -17,6 +17,9 @@ export const config: VendureConfig = {
         port: 3000,
         adminApiPath: 'admin-api',
         shopApiPath: 'shop-api',
+        // The following options are useful in development mode,
+        // but are best turned off for production for security
+        // reasons.
         ...(IS_DEV ? {
             adminApiPlayground: {
                 settings: { 'request.credentials': 'include' } as any,
@@ -40,7 +43,10 @@ export const config: VendureConfig = {
     },
     dbConnectionOptions: {
         type: '{{ dbType }}',
-        synchronize: true, // turn this off for production
+        // See the README.md "Migrations" section for an explanation of
+        // the `synchronize` and `migrations` options.
+        synchronize: false,
+        migrations: [path.join(__dirname, '../migrations/*.ts')],
         logging: false,
         database: {{#if isSQLjs}}new Uint8Array([]){{else if isSQLite}}path.join(__dirname, '../vendure.sqlite'){{else}}process.env.DB_NAME{{/if}},
         {{#if dbSchema}}
@@ -56,11 +62,12 @@ export const config: VendureConfig = {
         username: process.env.DB_USERNAME,
         password: process.env.DB_PASSWORD,
         {{/if}}
-        migrations: [path.join(__dirname, '../migrations/*.ts')],
     },
     paymentOptions: {
         paymentMethodHandlers: [dummyPaymentHandler],
     },
+    // When adding or altering custom field definitions, the database will
+    // need to be updated. See the "Migrations" section in README.md.
     customFields: {},
     plugins: [
         AssetServerPlugin.init({