-
Notifications
You must be signed in to change notification settings - Fork 2
Project Structure
The project consists of the following files and directories:
-
app: The main directory of the project. -
migrations: The directory that contains the database migrations. -
tests: The directory that contains the tests. -
utils: The directory that contains the utility functions. -
settings.ts: The file that contains the project settings. -
database.ts: The file that contains the database connection. -
datasource.ts: The file that contains the data source connection. -
index.ts: The main file of the project. -
log.ts: The file that contains the logging functions.
The app directory contains the files related to your application. We don't recommend a particular structure of how the code needs to be organized inside this. We prefer structuring the code, based on the features, entities and modules. This way it is easier to find the files.
The migrations directory contains the files that are used to create
the database schema. We use the TypeORM library to
manage the database schema. The datasource.ts file is used to provide connection
to typeorm to generate migrations. The migrations are created using the following
command:
npm run migration-generate -- -n ./src/migrations/NameOfTheMigrationThis will create a migration file with the commands generated in the migraitons directory.
Auto-generated migrations are not always correct and i would recommend reviewing them before adding them to the database connection.
The tests directory contains the tests for the project. We use the Mocha library to run the test. Chai.js is used for assertions. The tests are run using the following command:
npm run testThe utils directory contains the utility functions that are used in the project. These functions are used to perform common tasks that are used in the project.
The settings.ts file contains the project settings. These settings are used to configure the project. The settings are loaded from the environment variables. It contains a class extending the Settings class from the @smoke-trees/postgres-backend package.
The database.ts file contains the database connection. The database connection is created using the TypeORM library. The database connection is created using the settings from the settings.ts file.
You also register the entities and migrations in the database connection in this file.
Read more about database here.
The log.ts file contains the logging functions. The logging functions are used to log the messages to the console. I would suggest to use the export from this file to ensure the logs are consistent across the project. You can implement your own custom logger. We have used winston for logging.
The index.ts file is the main file of the project. This file is used to start the server. We initilize the Application, DAO, Services and Controller in this file. Sometime this files will get large, in this case you can split the initialization of the components into separate files.
The server is started using the run() funtion of the Applicaiton class. Read more about Application class here .
With ❤️ from Smoketrees Digital LLP