netzo / fresh-netzo

Full-stack Deno Fresh meta-framework for building business web apps like internal tools, dashboards, admin panels and automated workflows.
https://netzo.io
MIT License
51 stars 2 forks source link

[modules] add `restdb` module to serve a REST API for Deno KV #10

Closed miguelrk closed 10 months ago

miguelrk commented 1 year ago

Add a deno fresh plugin restdb to inject REST API routes (prescribe a simple document database of collections grouping records by ID, so that it remainsRESTful). Refer to the generateFreshHandlers from denoland/kv_api for how this should be implemented. In summary, the plugin should dynamically generate fresh the fresh handler based on the config object paths (containing e.g. a list of resources and its available methods), the plugin should then inject them under a configurable prefix.

Implementation

The RestdbOptions should provide sensible defaults (zero-config by default) but allow overriding for each service. Also, if should be possible to provide common configurations at root e.g. idField, but overwrite it per service if necessary. A schema field should be provided for built-in zod/valibot validation.

// netzo.config.ts
export default defineNetzoConfig({
  modules: {
    restdb: {
        idField: "id",
        methods: ["find", "get", "create", "update", "patch", "delete"],
        services: {
          users: {
            idField: "_id",
            methods: ["find", "get"], // allow reading only
            schema: userSchema
          },
          companies: {
            schema: companySchema
          },
          // ...more services
        },
      }
  }
})

Route overview

The plugin should be able to handle both patterns (KV and REST) simoultaneously. The following routes would allow that:

REST operations

HTTP Method Path Deno KV operation
GET /kv/:resource list
GET /kv/:resource/:id get
POST /kv/:resource set
PUT /kv/:resource/:id set
PATCH /kv/:resource/:id set
DELETE /kv/:resource/:id delete

KV operations

HTTP Method Path Deno KV operation
GET /kv?op=list list
GET /kv get
POST /kv set
DELETE /kv delete
POST /kv?op=sum sum
POST /kv?op=min min
POST /kv?op=max max

See also kv_toolbox for common utilities and anywhichway/denodata which might be useful.

miguelrk commented 10 months ago

Closing as completed (only the REST operations)