jfilby / nexus

A Nim web framework with batteries included
Apache License 2.0
101 stars 3 forks source link

New types for context objects (rework existing DB only types) #33

Closed jfilby closed 2 years ago

jfilby commented 2 years ago

Currently module types (e.g. MyModule) are auto-generated for DB context vars. They hold the DB connection and any cache objects.

These should be renamed as DB context types (e.g. MyDbContext), contained within a module type. This new module type will only be a starter, with a the DB var and a WebContext var (if relevant) to which users can add additional context vars:

Type changes:

# File: types/my_db_context.nim
# Auto-generated per: nexus gen models
import tables

type
  MyDbContext* = object
    dbConn*: DbConn
    ordersCache*: Table[string, Order]
# File: types/my_context.nim
# Only the starter code is auto-generated.
# After that, add any context vars you may need.
import options
import my_db_context

type
  MyContext* = object
    db*: MyDbContext
    web*: WebContext

    # Add your own context vars here
    currentOrderId*: Option[int64]

Calling a DB proc would then look like this:

let orders =
      filterOrders(
        myContext.db,
        whereFields = @[ "order_id" ],
        whereValues = @[ $myContext.currentOrderId ])