Mapper

In this guide, we'll go through the steps to create a new mapper in your SoapJS project. You can create mappers using CLI commands, interactive forms, or by defining them in a JSON file.

export class CustomerMongoMapper extends MongoMapper<Customer, CustomerMongoModel> {
  public toEntity(model: CustomerMongoModel): Customer {
    const { ... } = model;
    // parsing model types to entity types ...
    return new Customer(...);
  }
  
  public fromEntity(entity:Customer): CustomerMongoModel {
    const { ... } = entity;
    // parsing entity types to model types ...
    return removeUndefinedProperties({ ... });
  }
}

Creating new mapper

Using CLI Command with options

To create a new mapper directly via the CLI, use the following command:

soap new mapper -n "Customer" -e "shop" -t "Customer" -m "Customer" -s "mongo" -w -f

Options explained:

  • -n: Name of the mapper (e.g., "Customer").

  • -e: Endpoint associated with the mapper (e.g., "shop").

  • -t: Name the entity associated with the collection. If not set uses collection name.

  • -m: Name the model associated with the collection. If not set uses collection name.

  • -s: Storage type(s) (e.g., mongo), separated by commas.

  • --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 mapper details, simply run:

soap new mapper -w -f

Follow the prompts to enter your mapper's details.

Using JSON Configuration

Alternatively, you can define your mapper in a JSON file. Here is an example structure:

{
  "mappers": [
    {
      "name": "Customer",
      "endpoint": "shop",
      "types": ["mongo"],
      "model": "Customer",
      "entity": "Customer"
    }
  ]
}

Remember that the model and entity name should be the same as the base mapper name. In this case, the model and entity values are unnecessary. The data is provided here only as an example. Save this to a file, for example api.json, and run:

soap new --json "./path/to/api.json" -w -f

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 collection, your file structure (assuming default configuration settings) should look like this:

src/
  - shop/
    - data/
      - dtos/
        - customer.dtos.ts
        - index.ts
      - mappers/
        - customer.mongo.mapper.ts
        - index.ts
    - domain/
      - entities/
        - customer.ts
...

Last updated