How do EK9 operators map to HTTP methods for CRUD?

← Web Services · Ref: Q200

EK9 maps operators to HTTP methods semantically. This makes CRUD operations natural and consistent with the language's operator design.

OPERATOR TO HTTP MAPPING

Named methods and operators map to HTTP verbs:

  listAll() :/             GET (read)
  operator += :/           POST (create/add)
  operator -= :/{id}       DELETE (remove)
  operator :~: :/{id}      PATCH (merge/partial update)
  operator :^: :/{id}      PUT (replace)

WHY OPERATORS

The mapping is semantic, not arbitrary:

  += adds to a collection (POST creates a resource)
  -= removes from a collection (DELETE removes a resource)
  :~: merges into existing (PATCH partially updates)
  :^: replaces entirely (PUT replaces completely)

EACH RETURNS HTTPResponse
All CRUD methods return an HTTPResponse with status, content, and headers.

See Q199 for GET endpoints. See Q201 for HTTP responses. See Q202 for parameter binding. See Q96 for operator semantics.

See Q660 for CRUD operator patterns. See Q658 for path parameters.

Example

defines module qa.web.crud

  defines service

    Items :/items open

      // GET /items — list all items
      listAll() :/
        <- response as HTTPResponse: () with trait HTTPResponse
          override content()
            <- rtn as String: `["item1", "item2"]`
          override status() as pure
            <- rtn as Integer: 200
          override contentType() as pure
            <- rtn as String: "application/json"
          override cacheControl() as pure
            <- rtn as String: "no-cache"
          override contentLanguage() as pure
            <- rtn as String: "en"
          default operator ?

      // POST /items — add new item
      operator += :/
        -> content as String :=: CONTENT
        <- response as HTTPResponse: () with trait HTTPResponse
          override content()
            <- rtn as String: `{"created": true}`
          override status() as pure
            <- rtn as Integer: 201
          override contentType() as pure
            <- rtn as String: "application/json"
          override cacheControl() as pure
            <- rtn as String: "no-store"
          override contentLanguage() as pure
            <- rtn as String: "en"
          default operator ?

      // DELETE /items/{id} — remove item
      operator -= :/{id}
        -> id as String
        <- response as HTTPResponse: () with trait HTTPResponse
          override content()
            <- rtn as String: ""
          override status() as pure
            <- rtn as Integer: 204
          override contentType() as pure
            <- rtn as String: "text/plain"
          override cacheControl() as pure
            <- rtn as String: "no-store"
          override contentLanguage() as pure
            <- rtn as String: "en"
          default operator ?

  defines application

    CrudApp
      register Items()

  defines program

    CrudOperatorsDemo()
      stdout <- Stdout()

      stdout.println("CRUD operator mapping:")
      stdout.println("  += maps to POST (create)")
      stdout.println("  -= maps to DELETE (remove)")
      stdout.println("  :~: maps to PATCH (merge)")
      stdout.println("  :^: maps to PUT (replace)")

Common mistakes

E50060 — Stdout does not have a display() method. The correct method is println(). Calling a non-existent method triggers E50060 — method not resolved. See ek9 -h E50060 for details.

Incorrect:

stdout.display("CRUD operator mapping:")

Correct:

stdout.println("CRUD operator mapping:")
Other ways to ask this
  • How do I create POST, PUT, DELETE endpoints in EK9?
  • What operators map to HTTP methods in EK9 services?
  • How does EK9 map CRUD to service operators?

Coming from another language?

Java: JAX-RS @POST/@PUT/@DELETE annotations. Python: Flask methods=['POST','PUT','DELETE']. Rust: actix-web .post()/.put()/.delete(). Go: manual method checking in handler. Kotlin: Ktor post/put/delete route blocks. EK9: operators (+=, -=, :~:, :^:) map semantically to POST, DELETE, PATCH, PUT.

Keywords: delete, dict, method, patch, crud, service, rest, http, post, put, mapping, operator