Giter Club home page Giter Club logo

questdb-nio's Introduction

QuestDB + SwiftNIO

A QuestDB REST client designed to work with server-side Swift applications with Codable support.

Install (SPM)

Add the package to your dependencies in Package.swift:

dependencies: [
  .package(url: "https://github.com/swift-glide/questdb-nio.git", .from("0.1.0"))
]

Usage

Start by creating a client instance:

let client = QuestDBClient()

When no configuration is passed, the client will look for the QUESTDB_URL environment variable and use it if available. Otherwise, it will default to http://localhost:9000.

You can pass a configuration object as well as an NIO HTTP client during initialization:

let client = QuestDBClient(
  config: QuestDBConfig(url: "http://localhost:9000"),
  httpClient: HTTPClient(
    eventLoopGroupProvider: .createNew
  )
)

Once you have a client, you can call the execute instance method to execute your queries:

client.execute(
  options: ExecuteOptions(
    query: """
    CREATE TABLE readings(
        db_ts timestamp,
        device_ts timestamp,
        device_name symbol,
        reading int)
    timestamp(db_ts);
    """
  )
)

If you want the query to execute on a specific event loop, you can pass it as an argument as well:

client.execute(
  on: someEventLoop, 
  options: ...
)

This method returns an event loop future wrapping a Codable type.

If the return type can't be inferred from the call site, you can specify it as follows:

client.execute(
  on: someEventLoop, 
  options: ...,
  returning: SomeDecodableType.self
)

Many requests return a QuestOperationResponse object, so use it accordingly.

You can further customize your request using the ExecuteOptions type, including count, limit, nm, and timings. Please refer to the official QuestDB docs for more information.

Supported Endpoints

  • /exec
  • /imp
  • /exp

Vapor + QuestDB

If you plan to use this package with a Vapor 4 app, here are some snippets to get you started. We first create a service type inside Application:

import Vapor
import QuestDB

extension Application {
  struct QuestDB {
    let app: Application

    struct Key: StorageKey {
      typealias Value = QuestDBClient
    }

    var client: QuestDBClient {
      get {
        guard let client = self.app.storage[Key.self] else {
          fatalError("QuestDBClient is not setup. Use application.quest.client to set it up.")
        }

        return client
      }

      nonmutating set {
        self.app.storage.set(Key.self, to: newValue) {
          try $0.syncShutdown()
        }
      }
    }
  }

  var quest: QuestDB {
    .init(app: self)
  }

  var questClient: QuestDBClient {
    quest.client
  }
}

Then we do the same with Request:

extension Request {
  struct QuestDB {
    var client: QuestDBClient {
      return request.application.quest.client
    }

    let request: Request
  }

  var quest: QuestDB { .init(request: self) }

  var questClient: QuestDBClient {
    quest.client
  }
}

When configuring your app instance:

let client = QuestDBClient(httpClient: app.http.client.shared)
app.quest.client = client

For operations that you do frequently, you can extend the client to keep things DRY:

extension QuestDBClient {
  func createTable(
    on eventLoop: EventLoop? = nil
  ) -> Future<QuestOperationResponse> {
    execute(
      on: eventLoop,
      options: .init(
        query: """
        ...
        """
      )
    )
  }

Project Status & Contributions

The package handles all the use cases that it was initially designed for. That being said, PRs are very welcome, especially if they tackle some of the following:

  • Adding missing endpoints.
  • Adding a test suite.

License

See LICENSE.

questdb-nio's People

Contributors

kaishin avatar

Stargazers

 avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.