Skip to content

A tiny example for building multi-tenancy using LoopBack and PostgreSQL

License

Notifications You must be signed in to change notification settings

mbnoimi/multitenancy

Repository files navigation

multitenancy

This application is generated using LoopBack 4 CLI with the initial project layout.

Install dependencies

By default, dependencies were installed when this application was generated. Whenever dependencies in package.json are changed, run the following command:

npm install

To only install resolved dependencies in package-lock.json:

npm ci

Prepare the application

  1. Create User & UserCredentials for integrating JWT
$ lb4 model User
? Please select the model base class Entity (A persisted model with an ID)
? Allow additional (free-form) properties? No
Model User will be created in src/models/user.model.ts

Let's add a property to User
Enter an empty property name when done

? Enter the property name:
   create src/models/user.model.ts
   update src/models/index.ts

Model User was/were created in src/models
$ lb4 model UserCredentials
? Please select the model base class Entity (A persisted model with an ID)
? Allow additional (free-form) properties? No
Model UserCredentials will be created in src/models/user-credentials.model.ts

Let's add a property to UserCredentials
Enter an empty property name when done

? Enter the property name:
   create src/models/user-credentials.model.ts
   update src/models/index.ts

Model UserCredentials was/were created in src/models
  1. Replace models/user.model.ts & models/user-credentials.model.ts by user.model.ts & user-credentials.model.ts

  2. Create repositories for User and UserCredentials

$ lb4 repository
? Please select the datasource PostgresqlDatasource
? Select the model(s) you want to generate a repository for UserCredentials, User
? Please select the repository base class DefaultCrudRepository (Juggler bridge)
? Please enter the name of the ID property for UserCredentials: id
? Please enter the name of the ID property for User: id
   create src/repositories/user-credentials.repository.ts
   create src/repositories/user.repository.ts
   update src/repositories/index.ts
   update src/repositories/index.ts

Repositories UserCredentialsRepository, UserRepository was/were created in src/repositories
  1. Run npm run build

  2. Modify datasources/postgresql.datasource.ts to meet the proper PostgreSQL configurations then create a new PostgreSQL database depending on your configurations.

  3. Run npm run migrate

  4. Delete the following files:

  • models/user.model.ts
  • models/user-credentials.model.ts
  • repositories/user.repository.ts
  • repositories/user-credentials.repository.ts
  1. Modify models/index.ts by deleting the following lines:
export * from './user.model';
export * from './user-credentials.model';
  1. Modify repositories/index.ts by deleting the following lines:
export * from './user-credentials.repository';
export * from './user.repository';

Run the application

npm start

You can also run node . to skip the build step.

Open http://127.0.0.1:3000 in your browser.

Rebuild the project

To incrementally build the project:

npm run build

To force a full build by cleaning up cached artifacts:

npm run rebuild

Fix code style and formatting issues

npm run lint

To automatically fix such issues:

npm run lint:fix

Other useful commands

  • npm run migrate: Migrate database schemas for models
  • npm run openapi-spec: Generate OpenAPI spec into a file
  • npm run docker:build: Build a Docker image for this application
  • npm run docker:run: Run this application inside a Docker container

Tests

npm test

What's next

Please check out LoopBack 4 documentation to understand how you can continue to add features to this application.

LoopBack