{"id":22433804,"url":"https://github.com/gavinning/mysql-batch","last_synced_at":"2025-07-20T07:14:22.736Z","repository":{"id":147923335,"uuid":"64634856","full_name":"gavinning/mysql-batch","owner":"gavinning","description":"批量更新数据库","archived":false,"fork":false,"pushed_at":"2016-08-01T04:04:53.000Z","size":2,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-27T07:48:55.667Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/gavinning.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":null,"funding":null,"license":null,"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}},"created_at":"2016-08-01T04:04:18.000Z","updated_at":"2016-08-01T04:04:54.000Z","dependencies_parsed_at":"2023-05-27T23:44:42.161Z","dependency_job_id":null,"html_url":"https://github.com/gavinning/mysql-batch","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/gavinning/mysql-batch","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gavinning%2Fmysql-batch","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gavinning%2Fmysql-batch/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gavinning%2Fmysql-batch/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gavinning%2Fmysql-batch/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/gavinning","download_url":"https://codeload.github.com/gavinning/mysql-batch/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gavinning%2Fmysql-batch/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":266081349,"owners_count":23873517,"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-12-05T22:15:56.800Z","updated_at":"2025-07-20T07:14:22.519Z","avatar_url":"https://github.com/gavinning.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"Mysql-batch\n---\n批量更新数据库，Update and Insert\n\n### Install\n```sh\nnpm i mysql-batch --save\n```\n\nTest Data\n```json\n[\n    [\n        \"1000\",\n        \"1\",\n        \"爱星座\",\n        \"1\",\n        \"1\",\n        \"2\",\n        \"2\",\n        \"3\",\n        \"3\"\n    ],\n    [\n        \"1001\",\n        \"1\",\n        \"爱电影\",\n        \"1\",\n        \"1\",\n        \"2\",\n        \"2\",\n        \"3\",\n        \"3\"\n    ],\n    [\n        \"abc\",\n        \"1\",\n        \"爱直播\",\n        \"1\",\n        \"1\",\n        \"2\",\n        \"2\",\n        \"3\",\n        \"3\"\n    ],\n    [\n        \"1003\",\n        \"\",\n        \"爱游戏\",\n        \"1\",\n        \"1\",\n        \"2\",\n        \"2\",\n        \"3\",\n        \"3\"\n    ]\n]\n```\n\n### Usage\n```js\nvar db = require('./db');\nvar Batch = require('mysql-batch');\n\nvar header = [\n    \"list_id\",\n    \"status\",\n    \"memo\",\n    \"text1\",\n    \"url1\",\n    \"text2\",\n    \"url2\",\n    \"text3\",\n    \"url3\"\n]\n\nvar batch = new Batch({\n    // 主键，用于断定 Update or Insert\n    key: 'list_id',\n    // 表名\n    name: 'table_name',\n    // 数据对应字段名\n    header: header,\n    // 需要检查的必须字段\n    required: [\n        'list_id',\n        'status'\n    ],\n    // mysql连接池\n    db: require('./db')\n})\n\nbatch\n    .get(data)\n    // [[]] =\u003e [{}]\n    // 数据结构更新\n    .pipe(batch.parse())\n    // 过滤不符合条件的数据\n    .pipe(batch.filter())\n    // [{}] =\u003e [{ sql }]\n    // 数据结构更新，格式化sql语句\n    .pipe(batch.format())\n    // ==\u003e db\n    // 更新数据库\n    // @err     app级错误信息\n    // @sure\t成功的信息\n    // @fail\t失败的信息\n    // @filter  被过滤的信息\n    .dest((err, sure, fail, filter) =\u003e {\n        console.log(sure)\n        console.log(fail)\n        console.log(filter)\n        console.log('done')\n    })\n```\n\nTerm Log\n```js\n[ { code: 0, type: 'update', key: '1000' },\n  { code: 0, type: 'insert', key: '1001' } ]\n[ { code: 1,\n    type: 'insert',\n    msg: 'ER_TRUNCATED_WRONG_VALUE_FOR_FIELD: Incorrect integer value: \\'abc\\' for column \\'list_id\\' at row 1',\n    err: [Error Object],\n    key: 'abc' } ]\n[ { code: 2,\n    type: 'filter',\n    msg: '缺少关键字段',\n    required: [ 'list_id', 'status' ] } ]\ndone\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgavinning%2Fmysql-batch","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgavinning%2Fmysql-batch","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgavinning%2Fmysql-batch/lists"}