Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

hard to use in readme's code such as ... #312

Open
ijry opened this issue Oct 9, 2022 · 6 comments
Open

hard to use in readme's code such as ... #312

ijry opened this issue Oct 9, 2022 · 6 comments
Labels
enhancement New feature or request

Comments

@ijry
Copy link

ijry commented Oct 9, 2022

a full example code is better

@ijry ijry added the enhancement New feature or request label Oct 9, 2022
@0xTim
Copy link
Member

0xTim commented Oct 10, 2022

What is missing for you?

@pwascheul
Copy link

@0xTim I share the request, a full example of integrated MySQL-kit would be great. all functions, handlers are described but give me gray hairs to put everything together , I should admit that I am also quite amateur in swift which does not help. was looking to create a small macOS app to fill personal need.
eg: I would like to see how to manage the MySQLConfiguration and dependencies for the Connect later on.

@Freelenzer
Copy link

I experience the same issues.

  • What does "database: "vapor_database" exactly do? Do I need to change that to some real name?
  • What can I use as EventLoopGroup = ...?
  • Same with let mysql = pool.database(logger: ...) // MySQLDatabase
  • How do I structure the code in vapor so the setup is in the "configure.swift" file and I can use the database from the "routes.swift" or so?

Even as an exerienced swift developer I find it hard to create a running example without a ton of work.
A full working minimal example would be great.
Once I get it running I can try to create it.
But I'm far from that.

@coocy
Copy link

coocy commented Apr 24, 2023

Same issues, I spent a whole evening and can't figout how to use this codes.

@alteredtech
Copy link

I think part of the issue is that there is just "empty" code with ...

let eventLoopGroup: EventLoopGroup = ...
defer { try! eventLoopGroup.syncShutdown() }

let pools = EventLoopGroupConnectionPool(
    source: MySQLConnectionSource(configuration: configuration), 
    on: eventLoopGroup
)
defer { pools.shutdown() }
let eventLoop: EventLoop = ...
let pool = pools.pool(for: eventLoop)

pool.withConnection { conn
    print(conn) // MySQLConnection on eventLoop
}

Even on MySQLNIO it happens

import MySQLNIO

let eventLoop: EventLoop = ...
let conn = try MySQLConnection(
    to: .makeAddressResolvingHost("my.mysql.server", port: 5432),
    username: "test_username",
    database: "test_database",
    password: "test_password",
    on: eventLoop
).wait()
import MySQLNIO

let db: MySQLDatabase = ...
// now we can use client to do queries

I have tried finding generic examples, even tried to use chatGPT to wrangle something, to show what to put there when just getting something to work.

This was my attempt at getting it working. It gives me this error Thread Performance Checker: Thread running at QOS_CLASS_USER_INTERACTIVE waiting on a lower QoS thread running at QOS_CLASS_DEFAULT. Investigate ways to avoid priority inversions. Something of a simple hand hold of getting started file that doesnt do anything fancy but has everything for it to work.

import Foundation
import MySQLKit

func testMysql(hostname: String, port: Int, username: String, password: String, database: String) {
    
    let mysqlConfig = MySQLConfiguration(
        hostname: hostname,
        port: port,
        username: username,
        password: password,
        database: database)
    client(mysqlConfig: mysqlConfig)

}

func createMySQLConnectionPool(configuration: MySQLConfiguration) -> EventLoopGroupConnectionPool<MySQLConnectionSource> {
    let eventLoopGroup = MultiThreadedEventLoopGroup(numberOfThreads: System.coreCount)
     defer {
         do {
             try eventLoopGroup.syncShutdownGracefully()
         } catch {
             fatalError("Failed to shut down the EventLoopGroup: \(error)")
         }
     }

     let connectionSource = MySQLConnectionSource(configuration: configuration)
     let pool = EventLoopGroupConnectionPool(source: connectionSource, on: eventLoopGroup)
     defer { pool.shutdown() }

     return pool
}

func client(mysqlConfig: MySQLConfiguration) {
    let connectionPool = createMySQLConnectionPool(configuration: mysqlConfig)
    // Getting a pool for a specific event loop
    let eventLoopGroup = MultiThreadedEventLoopGroup(numberOfThreads: System.coreCount)
    defer {
        do {
            try eventLoopGroup.syncShutdownGracefully()
        } catch {
            fatalError("Failed to shut down the EventLoopGroup: \(error)")
        }
    }

    let eventLoop = eventLoopGroup.next()

    let pool = connectionPool.pool(for: eventLoop)

    // Creating a MySQLDatabase instance
    let logger = Logger(label: "com.example.app")
    let mysql = pool.database(logger: logger) // MySQLDatabase

    // Executing queries
    let rows = try? mysql.simpleQuery("SELECT @@version;").wait()

}

@pannous
Copy link

pannous commented Jan 17, 2024

Agreed the readme nees some serious love.

After many wasted hours it turns out it has to run on specific background queue:

let eventLoop:EventLoop=MultiThreadedEventLoopGroup.singleton.any()
let queue = DispatchQueue.global(qos: .background)
queue.async {
   Task {
   		let mysql = MySQLConnectionSource(configuration: configuration)
	        dbConnection=try mysql.makeConnection(logger: logger, on: eventLoop).wait()
       }
  }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

7 participants