Repository
In this guide, we'll go through the steps to create a new repository in your SoapJS project. You can create repositories using CLI commands, interactive forms, or by defining them in a JSON file.
In this example, the CustomerRepository
is an abstraction layer for accessing customer data. The concrete implementation, CustomerRepositoryImpl
, utilizes a MongoDB context defined by the DataContext
type. This context includes the collection source, the mapper from entities to MongoDB models, and a query factory for creating database queries.
In SoapJS projects, repositories play a crucial role in abstracting the data access layer. They provide a clean separation between the application's business logic and data access mechanisms. When using Inversion of Control (IoC) containers, such as InversifyJS, repositories can be integrated seamlessly into your application's dependency injection framework.
It's important to note that creating a separate implementation class for your repository, such as CustomerRepositoryImpl
, is not always necessary. If you do not require additional methods beyond those provided by the base Repository
class, or if you are only interacting with a single database type, you can directly use a generic repository implementation RepositoryImpl
.
Below is an example of how a CustomerRepository
is added to the Dependencies
class, which acts as the configurator for our application's dependencies. This step is essential if you've chosen an IoC option in your project configuration.
Creating new repository
Using CLI Command with options
To create a new repository directly via the CLI, use the following command:
Options explained:
-n
: Name of the repository (e.g., "Customer").-e
: Endpoint associated with the entity (e.g., "shop").-t
: Name the entity associated with the repository. If not set uses repository name.-m
: Name the model associated with the repository. If not set uses repository name.-s
: Storage type(s) (e.g., mongo), separated by commas.-c
: Generate custom collection implementation-i
: Generate custom repository implementation--no-tests
: Skip test generation.--no-rel
: Skip generating related files. You can also specify specific groups.--force
: Force the creation, overwrite files if necessary.--patch
: Add content to the files if they exists.--help
: display help for command
Using Interactive Form
If you prefer to use an interactive form to specify your repository details, simply run:
Follow the prompts to enter your repository's details.
Using JSON Configuration
Alternatively, you can define your repositories in a JSON file. Here is an example structure:
In the JSON structure, you can use strings with database aliases instead of objects in contexts
Save this to a file, for example api.json
, and run:
Options:
--json
: Path to your JSON configuration file.-w
: Generate with dependencies included.-f
: Force the creation, overwrite files if necessary.
File Structure
After creating your repository, your file structure (assuming default configuration settings) should look like this:
Last updated