SqliteWorkflowDao.swift 47 lignes · 1519 octets
import Foundation

struct SqliteWorkflowDao: WorkflowDao {
    let db: SqliteDatabase

    private static let columns = "serverId, nom, description, colonnesJson, creePar, createdAt"

    private static func map(_ row: SqliteRow) -> WorkflowEntity {
        var e = WorkflowEntity(serverId: row.text(0))
        e.nom = row.text(1)
        e.description = row.text(2)
        e.colonnesJson = row.text(3)
        e.creePar = row.text(4)
        e.createdAt = row.int64(5)
        return e
    }

    func upsert(_ entity: WorkflowEntity) async throws {
        try await db.write(
            "INSERT OR REPLACE INTO workflows (\(Self.columns)) VALUES (?, ?, ?, ?, ?, ?)",
            [
                .text(entity.serverId),
                .text(entity.nom),
                .text(entity.description),
                .text(entity.colonnesJson),
                .text(entity.creePar),
                .int(entity.createdAt),
            ]
        )
    }

    func getByServerId(_ serverId: String) async throws -> WorkflowEntity? {
        try await db.query(
            "SELECT \(Self.columns) FROM workflows WHERE serverId = ?",
            [.text(serverId)],
            map: Self.map
        ).first
    }

    func deleteByServerId(_ serverId: String) async throws {
        try await db.write("DELETE FROM workflows WHERE serverId = ?", [.text(serverId)])
    }

    func listAll() async throws -> [WorkflowEntity] {
        try await db.query("SELECT \(Self.columns) FROM workflows", map: Self.map)
    }
}