{"id":21062723,"url":"https://github.com/phanxgames/mysql-suspend","last_synced_at":"2025-03-14T01:15:04.813Z","repository":{"id":57153660,"uuid":"88720473","full_name":"phanxgames/mysql-suspend","owner":"phanxgames","description":"MySQL wrapper for Node.js Using Es6 Generator control flow","archived":false,"fork":false,"pushed_at":"2017-04-19T08:40:17.000Z","size":30,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-03-25T23:23:49.769Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"gpl-3.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/phanxgames.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":"2017-04-19T08:28:32.000Z","updated_at":"2019-04-29T03:43:08.000Z","dependencies_parsed_at":"2022-09-07T08:41:23.603Z","dependency_job_id":null,"html_url":"https://github.com/phanxgames/mysql-suspend","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/phanxgames%2Fmysql-suspend","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/phanxgames%2Fmysql-suspend/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/phanxgames%2Fmysql-suspend/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/phanxgames%2Fmysql-suspend/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/phanxgames","download_url":"https://codeload.github.com/phanxgames/mysql-suspend/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243505964,"owners_count":20301619,"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":[],"created_at":"2024-11-19T17:40:22.750Z","updated_at":"2025-03-14T01:15:04.788Z","avatar_url":"https://github.com/phanxgames.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"mysql-suspend\n=================\nMySQL database wrapper that provides helpers to query the database.\n\n* Select, Insert, Update and Delete Query Builders\n* Merge Command allowing insert or update in one command\n* Suspend integration for generator-based async control-flow\n* Idle Connection Auto Closer \n* No transpiling required\n\n### requirements\n\n* ECMAScript 2015 (ES6)\n* Node.JS 6.2.2 or later (tested on 6.2.2)\n\n\n### install\n\n```\nnpm install mysql-suspend\n```\n\nCopy the dbConfig.ex.json file into your project source folder, rename to dbConfig.json,\nand update with your database connection information.\n\n\n### asynchronous nature\n\nAll methods that include a callback (cb) have been designed to be used with the\n suspend library and may be placed behind a yield command. Be sure to leave the cb\n parameter null to use the suspend.resume functionality automatically.\n \nIMPORTANT: You will also need to set the resume reference in the constructor or the\n setResume() method, before the suspend.resume functionality will\n  be enabled.\n \nIf you do provide a callback, the 3rd parameter, \"next\" (ex: cb(err,result, next))\n will be the suspend.resume function reference so you may resume execution\n to move past the next yield command.\n\n \n### basic example\n\n```\n\tvar suspend = require(\"suspend\");\n\tvar MysqlSuspend = require(\"mysql-suspend\");\n\n\t//Attach your dbConfig to the mysql-suspend module\n\tMysqlSuspend.config = require('./dbConfig.json');\n\n\tvar db = new MysqlSuspend(suspend.resume);\n\n\tsuspend(function*() {\n\t   yield db.start();\n\t   \n\t   //... place query methods here ...\n\t   \n\t   yield db.end();\n\t})();\n```\n\n \n### accessing rows\n\nThere are 3 ways to loop over the resulting rows of a query.\n\n1: Standard callback.\n\n```\n\tyield db.query(\"select email from friends where user_id=? ;\",[userid],\n\t\tfunction(err,rows,cbResume) {\n\t\t\tfor (let row of rows) {\n\t\t\t\tconsole.log(row.email);\n\t\t\t}\n\t\t\t\n\t\t\t//move past the yield\n\t\t\tcbResume();\n\t});\n```\n\n2: Rows Getter.\n\n```\n\tyield db.query(\"select email from friends where user_id=? ;\",[userid]);\t\n\t\n\tfor (let row of db.rows) {\n\t\tconsole.log(row.email);\n\t}\n```\n\n3: Async Looping (non-blocking)\n\n```\n\tyield db.query(\"select email from friends where user_id=? ;\",[userid]);\n\t\n\tyield db.asyncForEach(function(index,row,cbNext) {\n\t\tconsole.log(row.email);\n\t\tcbNext();\n\t});\n\n```\n\n### checking for errors and rowcount\n\nAfter every query you should check if it was an error.\n```\n\tif (db.error()) {\n\t\tconsole.error(\"Database error: \",db.error());\n\t\treturn;\n\t}\n```\n\nAnd you may also want to check how many rows were returned before looping.\n```\n\tif (db.rowCount \u003e 0) {\n\t\t//.. loop\n\t\t\n\t} else {\n\t\tconsole.log(\"No rows found.\");\n\t}\n```\n\t\t\n\n### auto closer\n\nEnabling auto closer in the dbConfig.json file allows database connections that you\nleave open to be automatically close after a timeout interval provided in minutes.\n\nBy default this is not enabled, however you may want to keep this enabled and watch the\nconsole to see if the auto closer picks up on any open connections so you can address\nit and proprely close it when you are done.\n\n\n### merge utility\n\nConditionally insert or update a row.\nIf the row exists, update the columns; otherwise, insert as a new row.\n\nSee Merge Helper below.\n\n\n### query helpers examples\n\nThe Helpers will help you build SQL statements and provide parameterized values which\nsafeguard your queries from SQL injections. Any property that is labled as \"value\" will\nbe converted to a parameter internally.\n\n##### Select Helper\n\n```\n\t//..\n\tyield db.selectHelper({\n\t\t table:\"users\",\n\t\t columns:[\"username\",\"email\"],\n\t\t where: db.whereHelper({\n\t\t\t \"username -like\":\"t%\"\n\t\t }),\n\t\t orderBy: db.orderByHelper({\n\t\t\t \"email\":\"ASC\"\n\t\t })\n\t});\n\t\t \n\tfor (let row of db.rows) {\n\t\t console.log(row);\n\t\t //Output example: {username:\"Tester\",email:\"test@test.com\"}\n\t}\n\t//..\n```\n\n##### Insert Helper\n```\n\t//..\n\tyield db.insertHelper({\n\t   table:\"users\",\n\t   columns:{\n\t\t   \"username\":\"tester\",\n\t\t   \"email\":\"oldemail@test.com\"\n\t   }\n\t});\n\t//..\n```\n \n##### Update Helper\n```\n\t//..\n\tyield db.updateHelper({\n\t  table: \"users\",\n\t  columns: {\n\t\t  \"email\":\"newemail@test.com\"\n\t  },\n\t  where: db.whereHelper({\n\t\t  \"username\":\"tester\"\n\t  })\n\t});\n\t//..\n```\n\n##### Delete Helper\n```\n\t//..\n\tyield db.deleteHelper({\n\t  table: \"users\",\n\t  where: db.whereHelper({\n\t\t  \"username\":\"tester\"\n\t  })\n\t});\n\t//..\n```\n\n##### Merge Helper\n```\n\t//..\n\tyield db.mergeHelper({\n\t  table: \"users\",\n\t  columns: {\n\t\t  \"username\":\"tester\",\n\t\t  \"email\":\"test@test.com\"\n\t  },\n\t  where: db.whereHelper({\n\t\t  \"username\":\"tester\"\n\t  })\n\t});\n\t//..\n```\n\n\n### Module Dependencies\n\n- [mysql](https://github.com/mysqljs/mysql)\n- [suspend](https://github.com/jmar777/suspend)\n- [dictionaryjs](https://github.com/phanxgames/dictionaryjs)\n\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fphanxgames%2Fmysql-suspend","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fphanxgames%2Fmysql-suspend","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fphanxgames%2Fmysql-suspend/lists"}