SoapJS
  • SoapJS Framework
  • Clean Architecture
  • Quick Start
  • Components
    • Entity
    • Model
    • Repository
    • Collection
    • Mapper
    • Use Case
    • Controller
    • Route
    • RouteIO
    • Route Model
    • Router
    • Dependencies
    • Service
    • Toolset
  • Configuration
    • Introduction to Configuration Files
    • Plugin Configuration Structure Overview
    • Modifying Project Structure and Components
  • Examples
    • Simple API
    • Database Interaction Strategies
      • Using Where Conditions
      • Using QueryBuilder
      • Using Dedicated Repository Method
  • Plugins
    • Node.js
      • Soap Express
      • Soap AWS
      • Soap NestJS
      • Soap MongoDB
      • Soap Redis
      • Soap Mysql
    • Dart
      • Soap Dart
  • Troubleshooting and Support
  • Community and Contributions
  • Contact us
  • Sponsors / Partners
  • Release Notes
Powered by GitBook
On this page
  • Creating new entity
  • Using CLI Command with options
  • Using Interactive Form
  • Using JSON Configuration
  • File Structure
  1. Components

Entity

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

export class Customer {
  constructor(
    public readonly orders: Order[],
    public readonly address: Address,
    public readonly loyaltyPoints: number) {}
}

Creating new entity

Using CLI Command with options

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

soap new entity -n "Customer" -e "shop" -p "orders:Array<Entity<Order>>, address:Entity<Address>, loyaltyPoints:number" -w -f

Options explained:

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

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

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

  • -w: Generate with dependencies included.

  • -f: Force the creation, overwrite files if necessary.

  • --help: display help for command

Using Interactive Form

If you prefer to use an interactive form to specify your entity details, simply run:

soap new entity -w -f

Follow the prompts to enter your entity's details.

Using JSON Configuration

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

{
  "entities": [
    {
      "name": "Customer",
      "endpoint": "shop",
      "has_model": false,
      "props": [
        {
          "name": "orders",
          "type": "Array<Entity<Order>>"
        },
        {
          "name": "address",
          "type": "Entity<Address>"
        },
        {
          "name": "loyaltyPoints",
          "type": "number"
        }
      ]
    }
  ]
}

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.

  • --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

File Structure

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

src/
  - shop/
    - domain/
      - entities/
        - customer.ts
        - order.ts
        - address.ts
        - index.ts
...

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

PreviousComponentsNextModel

Last updated 1 year ago