Skip to content

Commit

Permalink
Init
Browse files Browse the repository at this point in the history
  • Loading branch information
ban-am committed Apr 5, 2024
0 parents commit 9dd92d3
Show file tree
Hide file tree
Showing 19 changed files with 4,829 additions and 0 deletions.
10 changes: 10 additions & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
APP_ENV=dev
COOKIE_SECRET=k0gtl6d5aar
SUPERADMIN_USERNAME=superadmin
SUPERADMIN_PASSWORD=superadmin
DB_HOST=localhost
DB_PORT=5432
DB_NAME=vendure2
DB_USERNAME=postgres
DB_PASSWORD=root
DB_SCHEMA=public
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
node_modules
dist
admin-ui
static/assets
static/email/test-emails
9 changes: 9 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
FROM node:16

WORKDIR /usr/src/app

COPY package.json ./
COPY yarn.lock ./
RUN yarn --production
COPY . .
RUN yarn build
124 changes: 124 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
# order-line-relation-price-error

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 Discord community](https://www.vendure.io/community)
- [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.
* `/static` contains static (non-code) files such as assets (e.g. uploaded images) and email templates.

## Development

```
yarn dev
```

will start the Vendure server and [worker](https://www.vendure.io/docs/developer-guide/vendure-worker/) processes from
the `src` directory.

## Build

```
yarn build
```

will compile the TypeScript sources into the `/dist` directory.

## Production

For production, there are many possibilities which depend on your operational requirements as well as your production
hosting environment.

### Running directly

You can run the built files directly with the `start` script:

```
yarn start
```

You could also consider using a process manager like [pm2](https://pm2.keymetrics.io/) to run and manage
the server & worker processes.

### Using Docker

We've included a sample [Dockerfile](./Dockerfile) which you can build with the following command:

```
docker build -t vendure .
```

This builds an image and tags it with the name "vendure". We can then run it with:

```
# Run the server
docker run -dp 3000:3000 -e "DB_HOST=host.docker.internal" --name vendure-server vendure npm run start:server
# Run the worker
docker run -dp 3000:3000 -e "DB_HOST=host.docker.internal" --name vendure-worker vendure npm run start:worker
```

Here is a breakdown of the command used above:

- `docker run` - run the image we created with `docker build`
- `-dp 3000:3000` - the `-d` flag means to run in "detached" mode, so it runs in the background and does not take
control of your terminal. `-p 3000:3000` means to expose port 3000 of the container (which is what Vendure listens
on by default) as port 3000 on your host machine.
- `-e "DB_HOST=host.docker.internal"` - the `-e` option allows you to define environment variables. In this case we
are setting the `DB_HOST` to point to a special DNS name that is created by Docker desktop which points to the IP of
the host machine. Note that `host.docker.internal` only exists in a Docker Desktop environment and thus should only be
used in development.
- `--name vendure-server` - we give the container a human-readable name.
- `vendure` - we are referencing the tag we set up during the build.
- `npm run start:server` - this last part is the actual command that should be run inside the container.

### Docker compose

We've included a sample [docker-compose.yml](./docker-compose.yml) file which demonstrates how the server, worker, and
database may be orchestrated with Docker Compose.

## 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
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:

```
yarn migration:generate [name]
```

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

You can revert the most recently-applied migration with:

```
yarn migration:revert
```
39 changes: 39 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
version: "3"
services:
server:
build:
context: .
dockerfile: Dockerfile
ports:
- 3000:3000
command: ["yarn", "start:server"]
volumes:
- /usr/src/app
environment:
DB_HOST: database
DB_PORT: 5432
DB_NAME: vendure
DB_USERNAME: postgres
DB_PASSWORD: password
worker:
build:
context: .
dockerfile: Dockerfile
command: ["yarn", "start:worker"]
volumes:
- /usr/src/app
environment:
DB_HOST: database
DB_PORT: 5432
DB_NAME: vendure
DB_USERNAME: postgres
DB_PASSWORD: password
database:
image: postgres
volumes:
- /var/lib/postgresql/data
ports:
- 5432:5432
environment:
POSTGRES_PASSWORD: password
POSTGRES_DB: vendure
27 changes: 27 additions & 0 deletions migration.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { generateMigration, revertLastMigration, runMigrations } from '@vendure/core';
import program from 'commander';

import { config } from './src/vendure-config';

program
.command('generate <name>')
.description('Generate a new migration file with the given name')
.action(name => {
return generateMigration(config, { name, outputDir: './src/migrations' });
});

program
.command('run')
.description('Run all pending migrations')
.action(() => {
return runMigrations(config);
});

program
.command('revert')
.description('Revert the last applied migration')
.action(() => {
return revertLastMigration(config);
});

program.parse(process.argv);
30 changes: 30 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"name": "order-line-relation-price-error",
"version": "0.1.0",
"private": true,
"scripts": {
"dev:server": "ts-node ./src/index.ts",
"dev:worker": "ts-node ./src/index-worker.ts",
"dev": "concurrently yarn:dev:*",
"build": "tsc",
"start:server": "node ./dist/index.js",
"start:worker": "node ./dist/index-worker.js",
"start": "concurrently yarn:start:*",
"migration:generate": "ts-node migration generate",
"migration:run": "ts-node migration run",
"migration:revert": "ts-node migration revert"
},
"dependencies": {
"@vendure/admin-ui-plugin": "2.1.9",
"@vendure/asset-server-plugin": "2.1.9",
"@vendure/core": "2.1.9",
"@vendure/email-plugin": "2.1.9",
"dotenv": "16.4.5",
"pg": "8.11.5",
"typescript": "4.9.5"
},
"devDependencies": {
"concurrently": "8.2.2",
"ts-node": "10.9.2"
}
}
20 changes: 20 additions & 0 deletions src/environment.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
export {};

// Here we declare the members of the process.env object, so that we
// can use them in our application code in a type-safe manner.
declare global {
namespace NodeJS {
interface ProcessEnv {
APP_ENV: string;
COOKIE_SECRET: string;
SUPERADMIN_USERNAME: string;
SUPERADMIN_PASSWORD: string;
DB_HOST: string;
DB_PORT: number;
DB_NAME: string;
DB_USERNAME: string;
DB_PASSWORD: string;
DB_SCHEMA: string;
}
}
}
8 changes: 8 additions & 0 deletions src/index-worker.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { bootstrapWorker } from '@vendure/core';
import { config } from './vendure-config';

bootstrapWorker(config)
.then(worker => worker.startJobQueue())
.catch(err => {
console.log(err);
});
8 changes: 8 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { bootstrap, runMigrations } from '@vendure/core';
import { config } from './vendure-config';

runMigrations(config)
.then(() => bootstrap(config))
.catch(err => {
console.log(err);
});
98 changes: 98 additions & 0 deletions src/vendure-config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
import {
dummyPaymentHandler,
DefaultJobQueuePlugin,
DefaultSearchPlugin,
VendureConfig,
} from '@vendure/core';
import { defaultEmailHandlers, EmailPlugin } from '@vendure/email-plugin';
import { AssetServerPlugin } from '@vendure/asset-server-plugin';
import { AdminUiPlugin } from '@vendure/admin-ui-plugin';
import 'dotenv/config';
import path from 'path';

const IS_DEV = process.env.APP_ENV === 'dev';

export const config: VendureConfig = {
apiOptions: {
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' },
},
adminApiDebug: true,
shopApiPlayground: {
settings: { 'request.credentials': 'include' },
},
shopApiDebug: true,
} : {}),
},
authOptions: {
tokenMethod: ['bearer', 'cookie'],
superadminCredentials: {
identifier: process.env.SUPERADMIN_USERNAME,
password: process.env.SUPERADMIN_PASSWORD,
},
cookieOptions: {
secret: process.env.COOKIE_SECRET,
},
},
dbConnectionOptions: {
type: 'postgres',
// See the README.md "Migrations" section for an explanation of
// the `synchronize` and `migrations` options.
synchronize: false,
migrations: [path.join(__dirname, './migrations/*.+(js|ts)')],
logging: false,
database: process.env.DB_NAME,
schema: process.env.DB_SCHEMA,
host: process.env.DB_HOST,
port: +process.env.DB_PORT,
username: process.env.DB_USERNAME,
password: process.env.DB_PASSWORD,
},
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({
route: 'assets',
assetUploadDir: path.join(__dirname, '../static/assets'),
// For local dev, the correct value for assetUrlPrefix should
// be guessed correctly, but for production it will usually need
// to be set manually to match your production url.
assetUrlPrefix: IS_DEV ? undefined : 'https://www.my-shop.com/assets/',
}),
DefaultJobQueuePlugin.init({ useDatabaseForBuffer: true }),
DefaultSearchPlugin.init({ bufferUpdates: false, indexStockStatus: true }),
EmailPlugin.init({
devMode: true,
outputPath: path.join(__dirname, '../static/email/test-emails'),
route: 'mailbox',
handlers: defaultEmailHandlers,
templatePath: path.join(__dirname, '../static/email/templates'),
globalTemplateVars: {
// The following variables will change depending on your storefront implementation.
// Here we are assuming a storefront running at http://localhost:8080.
fromAddress: '"example" <noreply@example.com>',
verifyEmailAddressUrl: 'http://localhost:8080/verify',
passwordResetUrl: 'http://localhost:8080/password-reset',
changeEmailAddressUrl: 'http://localhost:8080/verify-email-address-change'
},
}),
AdminUiPlugin.init({
route: 'admin',
port: 3002,
adminUiConfig: {
apiPort: 3000,
},
}),
],
};
Loading

0 comments on commit 9dd92d3

Please sign in to comment.