Skip to content

Data Gateway

There are currently 3 different data gateway implementation.

  1. A MongoDB Gateway
  2. A KV Gateway
  3. A R2 Gateway

Those are the supported data storage types for your Codedraw generated worker. The library will keep expanding. Each of the following middlewares will store the data gateway implementation to the request context. To access it:

typescript
// mongodb
import { 
  MongoDBDocument as Doc,
  ResourceManager as _, 
  MongoDBArguments as Arg 
} from '@coderpunktech/codedraw-core'
const mongoDBGateway: DataGateway<Doc, _, Arg> = _context.get('dataGateway')

// kv
import { 
  KVArguments as Arg
} from '@coderpunktech/codedraw-core'
const kvGateway: DataGateway<any, KVNamespace, Arg> = _context.get('dataGateway')

// R2
import {
  R2Arguments as Arg
} from '@coderpunktech/codedraw-core'
const r2Gateway: DataGateway<any, R2Bucket, Arg> = _context.get('dataGateway')

Interface

typescript
export interface DataGateway<E, C, A> {
  create: <T extends E>(data: T, args: A) => Promise<T>
  set: <T extends E>(data: T, args: A) => Promise<T>
  get: <T extends E>(args: A) => Promise<T | null>
  list: <T extends E>(args: A) => Promise<T[]>
  remove: (args: A) => Promise<string>
  getClient(): C
}

export declare type MongoDBArguments = {
  collectionName: string
  query?: {
    filter: globalThis.Realm.Services.MongoDB.Filter,
    options?: any
  }
}

export declare type KVArguments = {
  key: string
  options?: any
}

export declare type R2Arguments = {
  key: string
  options?: any
}

MongoDB

It allows to interact with your MongoDB database.

Available when your worker has mongoDB selected as its main data source

Interface

typescript
import { Context } from 'hono';
import { HonoApp } from '../sys/types';
export declare const InstallMongoDBGateway: (context: Context<HonoApp<{}, {}>, any, any>, next: Function) => Promise<any>;

KV

It allows to interact with your KV database.

Available when your worker has KV selected as its main data source

Interface

typescript
import { Context } from 'hono';
import { HonoApp } from '../sys/types';
export declare const InstallKVGateway: (context: Context<HonoApp<{}, {}>, any, any>, next: Function) => Promise<any>;

R2

It allows to interact with your R2 bucket.

Available when your worker has R2 selected as its main data source

Interface

typescript
import { Context } from 'hono';
import { HonoApp } from '../sys/types';
export declare const InstallR2Gateway: (context: Context<HonoApp<{}, {}>, any, any>, next: Function) => Promise<any>;