Skip to content

Commit

Permalink
createTenant module alongside test setup
Browse files Browse the repository at this point in the history
  • Loading branch information
deye9 committed Jan 15, 2024
1 parent feb5615 commit 9a79017
Show file tree
Hide file tree
Showing 24 changed files with 702 additions and 314 deletions.
Binary file removed .DS_Store
Binary file not shown.
1 change: 0 additions & 1 deletion .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

**/.classpath
**/.dockerignore
**/.env
**/.git
**/.gitignore
**/.project
Expand Down
5 changes: 5 additions & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
NODE_ENV=production
MANAGE_TENANTS=true
POSTGRES_PASSWORD=postgres
POSTGRES_DB=multiTenant_dev
DATABASE_URL=postgres://postgres:postgres@postgres:5432/${POSTGRES_DB}
10 changes: 8 additions & 2 deletions .github/workflows/coverage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,14 @@ jobs:
with:
node-version: ${{ matrix.node-version }}

- name: Install pnpm
run: npm install -g pnpm

- name: Install dependencies
run: npm install
run: pnpm install

- name: Install docker
run: sudo apt-get install docker-ce docker-ce-cli containerd.io

- name: Run the tests
run: npm test -- --coverage
run: pnpm docker:test
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# DB related files
data/
pgadmin/
db/

# Logs
Expand All @@ -18,9 +20,10 @@ node_modules/
.node_repl_history

# dotenv environment variables file
.env
./.env

# ignore selected files / folders
*.txt
*.npmignore
dist
.DS_Store
2 changes: 1 addition & 1 deletion .husky/pre-push
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env sh
. "$(dirname -- "$0")/_/husky.sh"

git fetch && pnpm push:test
git fetch && pnpm docker:test
60 changes: 8 additions & 52 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,80 +1,36 @@
# syntax=docker/dockerfile:1

# Comments are provided throughout this file to help you get started.
# If you need more help, visit the Dockerfile reference guide at
# https://docs.docker.com/engine/reference/builder/

ARG NODE_VERSION=20.10.0
ARG PNPM_VERSION=8.12.1

################################################################################
# Use node image for base image for all stages.
FROM node:${NODE_VERSION}-alpine as base

# Set working directory for all build stages.
WORKDIR /usr/src/app

# Install pnpm.
RUN --mount=type=cache,target=/root/.npm \
npm install -g pnpm@${PNPM_VERSION}

################################################################################
# Create a stage for installing production dependecies.
FROM base as deps

# Download dependencies as a separate step to take advantage of Docker's caching.
# Leverage a cache mount to /root/.local/share/pnpm/store to speed up subsequent builds.
# Leverage bind mounts to package.json and pnpm-lock.yaml to avoid having to copy them
# into this layer.
RUN --mount=type=bind,source=package.json,target=package.json \
--mount=type=bind,source=pnpm-lock.yaml,target=pnpm-lock.yaml \
--mount=type=cache,target=/root/.local/share/pnpm/store \
pnpm install --frozen-lockfile
RUN npm install -g pnpm@${PNPM_VERSION}

################################################################################
# Create a stage for building the application.
FROM deps as build

# Download additional development dependencies before building, as some projects require
# "devDependencies" to be installed to build. If you don't need this, remove this step.
# RUN --mount=type=bind,source=package.json,target=package.json \
# --mount=type=bind,source=pnpm-lock.yaml,target=pnpm-lock.yaml \
# --mount=type=cache,target=/root/.local/share/pnpm/store \
# pnpm install --frozen-lockfile

### Prisma section
COPY prisma/schema.prisma ./prisma/
# Copy package.json so that package manager commands can be used.
COPY package.json pnpm-lock.yaml ./

RUN pnpx prisma generate
###
RUN pnpm install --frozen-lockfile

# Copy the rest of the source files into the image.
COPY . .

# RUN the prisma generate script.
RUN pnpm prisma generate

# Run the build script.
RUN pnpm run build

################################################################################
# Create a new stage to run the application with minimal runtime dependencies
# where the necessary files are copied from the build stage.
FROM base as final

# Use production node environment by default.
ENV NODE_ENV production

# Run the application as a non-root user.
USER node

# Copy package.json so that package manager commands can be used.
COPY package.json .

# Copy the production dependencies from the deps stage and also
# the built application from the build stage into the image.
COPY --from=deps /usr/src/app/node_modules ./node_modules
COPY --from=build /usr/src/app/dist ./dist

# Expose the port that the application listens on.
EXPOSE 5000

# Run the application.
CMD pnpm start
CMD pnpm start
19 changes: 13 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ Suitable for all developers / companies or start-ups building the next software

## Dependencies

This application relies heavily on sequelizejs for its database connections. Dialects supported as of now are MySQL, SQLite, PostgreSQL and MSSQL. You can read up here http://docs.sequelizejs.com/manual/installation/usage.html#dialects. Also the following packages are required dependencies
This application relies heavily on Prisma and Knex for its database connections. You can read up here https://www.prisma.io/docs/orm/overview/databases as regards the databases supported. Also the following packages are required dependencies alongside the minimum versions of the respective packages used

```
- "auto-bind": "^2.0.0",
- "pg": "^7.8.1",
- "sequelize": "^4.42.0",
- "sequelize-cli": "^5.4.0",
- "dotenv": "^6.2.0"
- "@prisma/client": "^5.7.1",
- "knex": "^3.1.0",
- "nanoid": "3",
- "pg": "^8.11.3",
- "typescript": "^5.3.3"
```

<!-- ## Caveats -->
Expand All @@ -38,14 +38,21 @@ This application relies heavily on sequelizejs for its database connections. Dia

```sh
$ npm i node-multi-tenant
$ yarn i node-multi-tenant
$ pnpm i node-multi-tenant
```

**or**

```sh
$ npm install --save https://github.com/deye9/node-multi-tenant
$ yarn install --save https://github.com/deye9/node-multi-tenant
$ pnpm install --save https://github.com/deye9/node-multi-tenant
```

## Flow
1. Tenant db_name is optional. If supplied the value given will be used to create the database name else it will be assigned a random *16* string value.

## Before you start

Drop all migrations for the tenants in the tenants folder.
Expand Down
5 changes: 5 additions & 0 deletions __tests__/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
NODE_ENV=test
MANAGE_TENANTS=true
POSTGRES_PASSWORD=postgres
POSTGRES_DB=multiTenant_test
DATABASE_URL=postgres://postgres:postgres@testdb:5432/${POSTGRES_DB}
7 changes: 0 additions & 7 deletions __tests__/index.test.ts

This file was deleted.

Loading

0 comments on commit 9a79017

Please sign in to comment.