Swiftpack.co - ViktorChernykh/db-query as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
See all packages published by ViktorChernykh.
ViktorChernykh/db-query 0.0.5
Library on swift for serialize SQL query
⭐️ 0
🕓 Yesterday
macOS
.package(url: "https://github.com/ViktorChernykh/db-query.git", from: "0.0.5")

DBQuery

Swift 5.5 Vapor 4 Swift Package Manager Platforms OS X | Linux

Overview

DBQuery an API for building and serializing SQL queries in Swift. DBQuery is a SQLKit wrapper that implements CRUD for Postgres. For database creation and migrations, use Fluent or SQLKit.

Getting started

You need to add library to Package.swift file:

  • add package to dependencies:
.package(url: "https://github.com/ViktorChernykh/db-query.git", from: "0.0.1")
  • and add product to your target:
.target(name: "App", dependencies: [
    . . .
    .product(name: "DBQuery", package: "db-query")
])

Use case:

extension Request {
    /// Returns Fluent.Database as SQLKit.SQLDatabase
    public var sql: SQLDatabase {
        guard let sql = self.db as? SQLDatabase else {
            fatalError("The database is not sql.")
        }
        return sql
    }
}

/// Example of model
final class User: DBModel {
    static let alias = v1.alias
    static let schema = v1.schema

    var id: UUID
    var name: String
    var password: String

    init(
        id: UUID = UUID(),
        name: String,
        password: String
    )

    struct v1 {
        static let schema = "users"
        static let alias = "u"

        static let id: Column = "id"
        static let name: Column = "name"
        static let password: Column = "password"
    }

    struct Public: Codable {
        let id: UUID
        let name: String
    }
}

CRUD

import DBQuery
import Vapor

    typealias u = User.v1

    let id = UUID()
    let name = "Ray"
    let password = "******"

    /// Create
    try await User.create(on: req.sql)
        .values(id, name, password)
        .run()

    /// Read
    let users = try await User.select(on: req.sql)
        .fields(u.id, u.name)
        .filter(u.name == "Ray")
        .all(decode: User.Public)

    /// Update
    try await User.update(on: req.sql)
        .filter(u.id == id)
        .set(u.name == "Ray2")  // or .set(u.name, to: "Ray2")
        .run()

    /// Delete
    let deleted = try await User.delete(on: req.sql)
        .filter(u.id ~~ [id1, id2])
        .returning(id)
        .all(decode: Deleted.self)
        .map(\.id)

License

This project is released under the MIT license. See LICENSE for details.

GitHub

link
Stars: 0
Last commit: Yesterday
jonrohan Something's broken? Yell at me @ptrpavlik. Praise and feedback (and money) is also welcome.

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics