{"id":49692803,"url":"https://github.com/pardnio/node-mysql-pool","last_synced_at":"2026-05-07T19:02:34.146Z","repository":{"id":295440069,"uuid":"990118044","full_name":"pardnio/node-mysql-pool","owner":"pardnio","description":"A MySQL connection pool solution designed specifically for PHP, featuring intuitive chaining syntax that integrates query builder capabilities with read-write separation.","archived":false,"fork":false,"pushed_at":"2025-05-28T03:29:22.000Z","size":73,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"main","last_synced_at":"2026-04-24T17:35:29.436Z","etag":null,"topics":["chain-syntax","mysql","nodejs","pardnchiu","typescript"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","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/pardnio.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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2025-05-25T14:41:16.000Z","updated_at":"2026-04-05T18:07:06.000Z","dependencies_parsed_at":"2025-05-25T17:24:29.472Z","dependency_job_id":null,"html_url":"https://github.com/pardnio/node-mysql-pool","commit_stats":null,"previous_names":["pardnltd-tools/nodejs-mysqlpool","pardnchiu/nodejs-mysqlpool","pardnchiu/nodejs-mysql-pool","pardnio/node-mysql-pool"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/pardnio/node-mysql-pool","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pardnio%2Fnode-mysql-pool","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pardnio%2Fnode-mysql-pool/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pardnio%2Fnode-mysql-pool/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pardnio%2Fnode-mysql-pool/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/pardnio","download_url":"https://codeload.github.com/pardnio/node-mysql-pool/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pardnio%2Fnode-mysql-pool/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":32751758,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-07T02:14:30.463Z","status":"ssl_error","status_checked_at":"2026-05-07T02:14:29.405Z","response_time":62,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.6:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"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":["chain-syntax","mysql","nodejs","pardnchiu","typescript"],"created_at":"2026-05-07T19:02:33.246Z","updated_at":"2026-05-07T19:02:34.140Z","avatar_url":"https://github.com/pardnio.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# MySQLPool\n\n\u003e A MySQL connection pool solution designed specifically for Nodejs, featuring intuitive chaining syntax that integrates query builder capabilities with read-write separation.\n\n[![npm](https://img.shields.io/npm/v/@pardnchiu/mysql-pool)](https://www.npmjs.com/package/@pardnchiu/mysql-pool)\n\n## Features\n\n- **Dual Connection Pools**: Separate read and write database connections\n- **Query Builder**: Fluent interface for building SQL queries\n- **Environment Configuration**: Easy setup using environment variables\n- **Connection Management**: Automatic connection pooling and cleanup\n- **Slow Query Detection**: Automatic logging of queries taking over 20ms\n- **JOIN Operations**: Support for INNER, LEFT, and RIGHT joins\n- **CRUD Operations**: Complete Create, Read, Update, Delete functionality\n- **UPSERT Support**: Insert or update on duplicate key\n\n## Installation\n\n```bash\nnpm install @pardnchiu/mysql-pool\n```\n\n## Environment Configuration\n\nSet up your database connections using environment variables:\n\n### Read Database (Optional)\n```env\nDB_READ_HOST=localhost\nDB_READ_PORT=3306\nDB_READ_USER=read_user\nDB_READ_PASSWORD=read_password\nDB_READ_DATABASE=your_database\nDB_READ_CHARSET=utf8mb4\nDB_READ_CONNECTION=8\n```\n\n### Write Database (Required for write operations)\n```env\nDB_WRITE_HOST=localhost\nDB_WRITE_PORT=3306\nDB_WRITE_USER=write_user\nDB_WRITE_PASSWORD=write_password\nDB_WRITE_DATABASE=your_database\nDB_WRITE_CHARSET=utf8mb4\nDB_WRITE_CONNECTION=4\n```\n\n## Quick Start\n\n```javascript\nimport MySQLPool from \"@pardnchiu/mysql-pool\";\n\n// init pool\nawait MySQLPool.init();\n\n// simple query\nconst users = await MySQLPool\n  .table(\"users\")\n  .where(\"status\", \"active\")\n  .get();\n\n// Close connections when done\nawait MySQLPool.close();\n```\n\n## API Reference\n\n### Initialization\n\n#### `init(): Promise\u003cvoid\u003e`\nInitialize the database connection pools.\n\n```javascript\nawait MySQLPool.init();\n```\n\n#### `close(): Promise\u003cvoid\u003e`\nClose all database connections.\n\n```javascript\nawait MySQLPool.close();\n```\n\n### Query Builder Methods\n\n#### `table(tableName: string, target?: \"read\" | \"write\"): MySQLPool`\nSet the target table and target pool\n\n```javascript\nMySQLPool.table(\"users\");           // use read pool\nMySQLPool.table(\"users\", \"write\");  // use write pool\n```\n\n#### `select(...fields: string[]): MySQLPool`\nSpecify columns to select.\n\n```javascript\nMySQLPool.table(\"users\").select(\"id\", \"name\", \"email\");\nMySQLPool.table(\"users\").select(\"COUNT(*) as total\");\n```\n\n#### `where(column: string, operator: any, value?: any): MySQLPool`\nAdd WHERE conditions.\n\n```javascript\n// Basic where\nMySQLPool.table(\"users\").where(\"id\", 1);\nMySQLPool.table(\"users\").where(\"age\", \"\u003e\", 18);\n\n// LIKE operator (automatically adds % wildcards)\nMySQLPool.table(\"users\").where('name', \"LIKE\", \"John\");\n\n// IN operator\nMySQLPool.table(\"users\").where(\"id\", \"IN\", [1, 2, 3]);\n```\n\n#### JOIN Operations\n\n```javascript\n// INNER JOIN\nMySQLPool.table(\"users\")\n  .innerJoin(\"profiles\", \"users.id\", \"profiles.user_id\");\n\n// LEFT JOIN\nMySQLPool.table(\"users\")\n  .leftJoin(\"orders\", \"users.id\", \"orders.user_id\");\n\n// RIGHT JOIN with custom operator\nMySQLPool.table(\"users\")\n  .rightJoin(\"posts\", \"users.id\", \"!=\", \"posts.author_id\");\n```\n\n#### `orderBy(column: string, direction?: \"ASC\" | \"DESC\"): MySQLPool`\nAdd ORDER BY clause.\n\n```javascript\nMySQLPool.table(\"users\").orderBy(\"created_at\", \"DESC\");\nMySQLPool.table(\"users\").orderBy(\"name\"); // Defaults to ASC\n```\n\n#### `limit(num: number): MySQLPool`\nLimit the number of results.\n\n```javascript\nMySQLPool.table(\"users\").limit(10);\n```\n\n#### `offset(num: number): MySQLPool`\nSkip a number of records.\n\n```javascript\nMySQLPool.table(\"users\").offset(20).limit(10); // Pagination\n```\n\n#### `total(): MySQLPool`\nInclude total count in results (adds COUNT(*) OVER()).\n\n```javascript\nconst results = await MySQLPool\n  .table(\"users\")\n  .total()\n  .limit(10)\n  .get();\n// Results will include 'total' field with complete count\n```\n\n### Data Operations\n\n#### `get\u003cT\u003e(): Promise\u003cT[]\u003e`\nExecute SELECT query and return results.\n\n```javascript\nconst users = await MySQLPool\n  .table(\"users\")\n  .where(\"status\", \"active\")\n  .orderBy(\"created_at\", \"DESC\")\n  .get();\n```\n\n#### `insert(data: Record\u003cstring, any\u003e): Promise\u003cnumber | null\u003e`\nInsert a new record.\n\n```javascript\nconst userId = await MySQLPool\n  .table(\"users\")\n  .insert({\n    name: \"John Doe\",\n    email: \"john@example.com\",\n    created_at: \"NOW()\"\n  });\n```\n\n#### `update(data?: Record\u003cstring, any\u003e): Promise\u003cResultSetHeader\u003e`\nUpdate existing records.\n\n```javascript\n// Update with data\nconst result = await MySQLPool\n  .table(\"users\")\n  .where(\"id\", 1)\n  .update({\n    name: \"Jane Doe\",\n    updated_at: \"NOW()\"\n  });\n\n// Update using increase() method\nawait MySQLPool\n  .table(\"users\")\n  .where(\"id\", 1)\n  .increase(\"login_count\", 1)\n  .update();\n```\n\n#### `upsert(data: Record\u003cstring, any\u003e, updateData?: Record\u003cstring, any\u003e | string): Promise\u003cnumber | null\u003e`\nInsert or update on duplicate key.\n\n```javascript\n// Simple upsert (updates all fields)\nconst id = await MySQLPool\n  .table(\"users\")\n  .upsert({\n    email: \"john@example.com\",\n    name: \"John Doe\",\n    login_count: 1\n  });\n\n// Upsert with specific update data\nconst id2 = await MySQLPool\n  .table(\"users\")\n  .upsert(\n    { email: \"john@example.com\", name: \"John Doe\", login_count: 1 },\n    { updated_at: \"NOW()\" }\n  );\n```\n\n### Raw Queries\n\n#### `read\u003cT\u003e(query: string, params?: any[]): Promise\u003cT\u003e`\nExecute read query using read pool.\n\n```javascript\nconst users = await MySQLPool.read(`\nSELECT COUNT(*) as total \nFROM users \nWHERE status = ?\n`, ['active']);\n```\n\n#### `write\u003cT\u003e(query: string, params?: any[]): Promise\u003cT\u003e`\nExecute write query using write pool.\n\n```javascript\nconst result = await MySQLPool.write(`\nUPDATE users \nSET last_login = NOW() \nWHERE id = ?\n`, [userId]);\n```\n\n## Advanced Examples\n\n### Complex Query with Joins and Conditions\n\n```javascript\nconst reports = await MySQLPool\n  .table(\"orders\")\n  .select(\"orders.id\", \"users.name\", \"products.title\", \"orders.total\")\n  .innerJoin(\"users\", \"orders.user_id\", \"users.id\")\n  .leftJoin(\"order_items\", \"orders.id\", \"order_items.order_id\")\n  .leftJoin(\"products\", \"order_items.product_id\", \"products.id\")\n  .where(\"orders.status\", \"completed\")\n  .where(\"orders.created_at\", \"\u003e\", \"2023-01-01\")\n  .orderBy(\"orders.created_at\", \"DESC\")\n  .limit(50)\n  .get();\n```\n\n### Pagination with Total Count\n\n```javascript\nconst page = 1;\nconst perPage = 10;\n\nconst results = await MySQLPool\n  .table(\"users\")\n  .select(\"id\", \"name\", \"email\", \"created_at\")\n  .where(\"status\", \"active\")\n  .total()\n  .orderBy(\"created_at\", \"DESC\")\n  .limit(perPage)\n  .offset((page - 1) * perPage)\n  .get();\n\nconsole.log(`total: ${results[0]?.total || 0}`);\nconsole.log(`page: ${page}`);\n```\n\n### Transaction-like Operations\n\n```javascript\ntry {\n  // Create user\n  const userId = await MySQLPool\n    .table(\"users\", \"write\")\n    .insert({\n      name: \"John Doe\",\n      email: \"john@example.com\"\n    });\n\n  // Create user profile\n  await MySQLPool\n    .table(\"profiles\", \"write\")\n    .insert({\n      user_id: userId,\n      bio: \"Software Developer\",\n      avatar: \"avatar.jpg\"\n    });\n\n  console.log(\"created successfully\");\n} catch (error) {\n  console.error(\"failed to create:\", error);\n}\n```\n\n## Supported MySQL Functions\n\nThe following MySQL functions are supported in update operations:\n\n- `NOW()`\n- `CURRENT_TIMESTAMP`\n- `UUID()`\n- `RAND()`\n- `CURDATE()`\n- `CURTIME()`\n- `UNIX_TIMESTAMP()`\n- `UTC_TIMESTAMP()`\n- `SYSDATE()`\n- `LOCALTIME()`\n- `LOCALTIMESTAMP()`\n- `PI()`\n- `DATABASE()`\n- `USER()`\n- `VERSION()`\n\n## Error Handling\n\n```javascript\ntry {\n  await MySQLPool.init();\n  \n  const users = await MySQLPool\n    .table(\"users\")\n    .where(\"invalid_column\", \"value\")\n    .get();\n    \n} catch (error) {\n  console.error(\"error:\", error.message);\n} finally {\n  await MySQLPool.close();\n}\n```\n\n## Performance Features\n\n- **Connection Pooling**: Automatic connection reuse\n- **Slow Query Detection**: Queries over 20ms are automatically logged\n- **Prepared Statements**: All queries use parameterized statements to prevent SQL injection\n- **Connection Management**: Automatic connection release after each query\n\n## License\n\nThis source code project is licensed under the [MIT](https://github.com/pardnltd-tools/blob/main/LICENSE) license.\n\n## Creator\n\n\u003cimg src=\"https://avatars.githubusercontent.com/u/25631760\" align=\"left\" width=\"96\" height=\"96\" style=\"margin-right: 0.5rem;\"\u003e\n\n\u003ch4 style=\"padding-top: 0\"\u003e邱敬幃 Pardn Chiu\u003c/h4\u003e\n\n\u003ca href=\"mailto:dev@pardn.io\" target=\"_blank\"\u003e\n    \u003cimg src=\"https://pardn.io/image/email.svg\" width=\"48\" height=\"48\"\u003e\n\u003c/a\u003e \u003ca href=\"https://linkedin.com/in/pardnchiu\" target=\"_blank\"\u003e\n    \u003cimg src=\"https://pardn.io/image/linkedin.svg\" width=\"48\" height=\"48\"\u003e\n\u003c/a\u003e\n\n***\n\n©️ 2025 [邱敬幃 Pardn Chiu](https://pardn.io)","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpardnio%2Fnode-mysql-pool","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpardnio%2Fnode-mysql-pool","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpardnio%2Fnode-mysql-pool/lists"}