Model

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

export type ProductJsonModel = {
  price: number,
  stock: number
}

export type ProductMongoModel = {
  price: number,
  stock: number
}

Creating new model

Using CLI Command with options

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

soap new model -n "Product" -e "shop" -p "price:number,stock:number" -t "json, mongo" -w -f

Options explained:

  • -n: Name of the model (e.g., "product").

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

  • -p: Properties of the model in "name:type" format, separated by commas.

  • -t: Types of the model, 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 model details, simply run:

Follow the prompts to enter your model's details.

Using JSON Configuration

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

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.

  • --help: display help for command

File Structure

After creating your models, your file structure (assuming default configuration settings) should look like this:

This structure helps maintain a clean separation of your domain logic, keeping your models organized within the data/dtos/ directory.

Last updated