{"id":26601337,"url":"https://github.com/philippmdoerner/tinypool","last_synced_at":"2025-04-09T16:34:09.082Z","repository":{"id":37442819,"uuid":"452408703","full_name":"PhilippMDoerner/TinyPool","owner":"PhilippMDoerner","description":"A minimalistic database connection pool for sqlite/postgres/mysql","archived":false,"fork":false,"pushed_at":"2023-01-20T22:04:26.000Z","size":36,"stargazers_count":27,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-23T18:50:51.434Z","etag":null,"topics":["connection-pool","database","mysql","nim","nim-lang","postgres","sql","sqlite"],"latest_commit_sha":null,"homepage":"","language":"Nim","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/PhilippMDoerner.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2022-01-26T19:18:30.000Z","updated_at":"2024-09-12T13:20:30.000Z","dependencies_parsed_at":"2023-02-12T06:01:40.145Z","dependency_job_id":null,"html_url":"https://github.com/PhilippMDoerner/TinyPool","commit_stats":null,"previous_names":[],"tags_count":8,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PhilippMDoerner%2FTinyPool","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PhilippMDoerner%2FTinyPool/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PhilippMDoerner%2FTinyPool/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PhilippMDoerner%2FTinyPool/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/PhilippMDoerner","download_url":"https://codeload.github.com/PhilippMDoerner/TinyPool/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248067762,"owners_count":21042349,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["connection-pool","database","mysql","nim","nim-lang","postgres","sql","sqlite"],"created_at":"2025-03-23T18:39:19.759Z","updated_at":"2025-04-09T16:34:09.044Z","avatar_url":"https://github.com/PhilippMDoerner.png","language":"Nim","funding_links":[],"categories":[],"sub_categories":[],"readme":"# TinyPool\n\nTinypool is a minimalistic connection pool with support for multi-threaded access. It supports sqlite, postgres (new with v1.0.0) and mysql (new with v1.0.0).\nIt's essentially a global-variable `seq[DbConn]` associated with a `lock` that can be accessed in a thread-safe manner.\nIt also grows and shrinks as configured, but more on that down the line.\n\nNote: Tinypool now also explicitly supports nim 1.9.1 (and thus should work on nim 2.0 in general).\n## Installation\n\n`nimble install tinypool`\n\n## Quickstart\n\nYour 2 main ways of interacting with tinypool after initialization are either:\n\n1. `withDbConn(someConnectionVariableName)` (recommended)\n2. `borrowConnection` and `recycleConnection`\n\n### 1. withDbConn\n\n```nim\nimport tinypool/sqlitePool #For convenience reasons, sqlitePool also exports std/db_sqlite since you'll need that either way\n\nlet databasePath = \":memory:\"\nlet defaultPoolSize = 2\ninitConnectionPool(databasePath, defaultPoolSize)\n\nvar rows: seq[Row]\n\nwithDbConn(myCon):\n  myCon.exec(sql\"\"\"CREATE TABLE \"auth_user\" (\"id\" integer NOT NULL PRIMARY KEY AUTOINCREMENT, \"username\" varchar(150) NOT NULL UNIQUE);\"\"\")\n\n  myCon.exec(sql\"\"\"INSERT INTO auth_user (username) VALUES ('henry');\"\"\")\n\n  let rows = myCon.getAllRows(sql\"\"\"SELECT * FROM auth_user WHERE username LIKE 'Henry';\"\"\")\n\nassert rows.len() == 1\nassert rows[0].username == \"henry\"\n\ndestroyConnectionPool()\n```\n\n### 2. borrowConnection and recycleConnection\n\n```nim\nimport tinypool/sqlitePool #For convenience reasons, sqlitePool also exports std/db_sqlite since you'll need that either way\n\nlet databasePath = \":memory:\"\nlet defaultPoolSize = 2\ninitConnectionPool(databasePath, defaultPoolSize)\n\n\nlet myCon: DbConn = borrowConnection()\n\nmyCon.exec(sql\"\"\"CREATE TABLE \"auth_user\" (\"id\" integer NOT NULL PRIMARY KEY AUTOINCREMENT, \"username\" varchar(150) NOT NULL UNIQUE);\"\"\")\n\nmyCon.exec(sql\"\"\"INSERT INTO auth_user (username) VALUES ('henry');\"\"\")\n\nlet rows = myCon.getAllRows(sql\"\"\"SELECT * FROM auth_user WHERE username LIKE 'Henry';\"\"\")\nassert rows.len() == 1\nassert rows[0].username == \"henry\"\n\nmyCon.recycleConnection()\n\ndestroyConnectionPool()\n```\n\n## Initialization/Configuration and Destruction\n\nIn order for tinypool to work, it needs to be told how to connect to the desired database and how many connections it needs. In terms of how to connect to the database, this differs slightly between sqlite and postgres/mysql. As for how many connections, if you're uncertain, I'd recommend ~4 connections.\n\nTo initialize the pool e.g. on startup of your application, just call `initConnectionPool`.\nTo destroy the pool e.g. on shutdown of your application, just call `destroyConnectionPool`.\n\n#### Initializing sqlite pool\nFor sqlite, it suffices to provide a path to the database file. The application then builds a proc to create connections to that database file itself.\n\n```nim\nimport tinypool/sqlitePool #For convenience reasons, sqlitePool also exports std/db_sqlite since you'll need that either way\n\nlet databasePath = \":memory:\"\nlet defaultPoolSize = 2\ninitConnectionPool(databasePath, defaultPoolSize)\n```\n \n#### Initializing postgres/mysql pools\nFor postgres and mysql it is required to provide a proc can create a database connection.\nThis proc must have this signature: `proc(): DbConn`.\ntinypool will use that proc to create connections for the pool as needed.\n\n```nim\nimport tinypool/postgresPool #For convenience reasons, postgresPool also exports std/db_postgres since you'll need that either way\n\nproc createConnection(): DbConn = open(\"\", \"default\", \"1234\", \"host=localhost port=5432 dbname=default\")\nlet defaultPoolSize = 2\ninitConnectionPool(createConnection, defaultPoolSize)\n```\n\n## burstMode\n\nIf more connections are needed than it has, the pool will temporarily go into \"burst mode\" and automatically refill with a new batch of connections, the amount of which is determined by the `poolSize`.\n\nWhile the pool is in burst mode it can hold an unlimited amount of connections.\n\nWhile the pool is not in burst mode, any superfluous connection that is returned to it gets closed.\n\nBurst mode ends after a specified duration (30 minutes), though that gets extended if the connections from the added batch are still needed.\n\n## Enabling Logging\nTinypool logs a variety of messages, but does not supply its own Logger, so it will use whatever logger your application defines.\nHowever, it is up to you whether you want to enable logging of tinypool or not.\nIt is not enabled by default.\nIn order to see tinypool's log messages you will have to supply the compilerflag `-d:enableTinyPoolLogging`.","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fphilippmdoerner%2Ftinypool","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fphilippmdoerner%2Ftinypool","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fphilippmdoerner%2Ftinypool/lists"}