{"id":36625735,"url":"https://github.com/aixinyongheng/sqlconds","last_synced_at":"2026-01-12T09:31:52.992Z","repository":{"id":65052913,"uuid":"581109369","full_name":"aixinyongheng/sqlconds","owner":"aixinyongheng","description":"sql 查询条件组织","archived":false,"fork":false,"pushed_at":"2023-03-06T05:43:21.000Z","size":88,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-09-29T19:52:02.495Z","etag":null,"topics":["common","node","sql"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","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/aixinyongheng.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}},"created_at":"2022-12-22T09:49:43.000Z","updated_at":"2022-12-29T05:39:29.000Z","dependencies_parsed_at":"2023-02-02T04:01:24.234Z","dependency_job_id":null,"html_url":"https://github.com/aixinyongheng/sqlconds","commit_stats":null,"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"purl":"pkg:github/aixinyongheng/sqlconds","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aixinyongheng%2Fsqlconds","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aixinyongheng%2Fsqlconds/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aixinyongheng%2Fsqlconds/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aixinyongheng%2Fsqlconds/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/aixinyongheng","download_url":"https://codeload.github.com/aixinyongheng/sqlconds/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aixinyongheng%2Fsqlconds/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28337728,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-12T06:09:07.588Z","status":"ssl_error","status_checked_at":"2026-01-12T06:05:18.301Z","response_time":98,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.5: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":["common","node","sql"],"created_at":"2026-01-12T09:31:52.290Z","updated_at":"2026-01-12T09:31:52.987Z","avatar_url":"https://github.com/aixinyongheng.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Sqlconds 简介\n前后台约定通用查询条件\n\n功能点：  \n\u0026nbsp; \u0026nbsp; ◇sql 通用查询条件组织  \n\u0026nbsp; \u0026nbsp; ◇sql 通用统计条件组织  \n\u0026nbsp; \u0026nbsp; ◇将数组对象转换为insert/update语句。\n\n# Install 安装\nnpm install sqlconds\n# Usage 使用\n\n## 通用查询条件组织：\n```javascript\n // github仓库地址: https://github.com/aixinyongheng/sqlconds\n // cjs 引入方式\n const Sqlconds = require('sqlconds');\n // es6 引入方式\n import Sqlconds from \"sqlconds\";\n \n const sqlconds =new Sqlconds(\"postgres\"); // postgres/mysql/oracle ...\n // 1.筛选条件组织\n const sqlres= sqlconds.condPackage( [{\"operator\":\"EQ\",\"field\":\"bm\",\"value\":\"zrzhczt_ggfwss_xx\"},{\"operator\":\"OBD\",\"field\":\"px\"}] );\n\n console.log(sqlres.cond); // and   (  \"bm\"   =   'zrzhczt_ggfwss_xx' )\n console.log(sqlres.order); // order by   \"px\" desc\n console.log(`select * from tableA where 1=1 ${sqlres.cond} ${sqlres.order}`);\n\n```\n## 分组条件组织（待完善）\n```javascript\n// 2. 分组条件组织\n// 2.1 分组group by 字段\nconst groupbyRes=sqlconds.groupCondPackage('[{ \"type\":\"CG\", \"field\":\"field1\", \"rename\":\"newfield1\" },{ \"type\":\"SUB\", \"field\":\"field2,1,4\", \"rename\":\"newfield2\" }]');\n\nconsole.log(groupbyRes.groupbycond); //  group by \"field1\",substring(field2,1,4) \nconsole.log(groupbyRes.fields); //  \"field1\" AS \"newfield1\",substring(field2,1,4) AS \"newfield2\" \nconsole.log(`select count(*),${groupbyRes.fields} from tableA where 1=1 ${sqlres.cond} ${groupbyRes.groupbycond}`);\n```\n## 聚合统计条件组织（待完善）\n```javascript\n // 2.2 聚合统计函数组织\n  const statiscondRes = sqlconds.statisCondPackage([{\"field\":\"field1\",\"type\":\"ZDZ\",\"rename\":\"最大值\",\"dpoint\":0}]);\n  console.log(statiscondRes.statiscond); //  max(\"field1\") AS \"最大值\" \n\n  console.log(`select count(*),${groupbyRes.fields},${statiscondRes.statiscond} from tableA where 1=1 ${sqlres.cond} ${groupbyRes.groupbycond}`);\n```\n## 数值对象转为insert/update语句\n```javascript\n const Sqlconds = require('sqlconds');\n const sqlconds = new Sqlconds(\"postgres\");\n    const res = sqlconds.objtosql(\"tableA\", [{\n      \"id\": \"111\", \"name\": \"test\", \"geom\": {\n        \"coordinates\": [112.3257164817677,\n          35.342031483036436\n        ],\n        \"type\": \"Point\"\n      }\n    }], { geomfields: 'geom' });\n    console.log(res.sql);// insert into \"tableA\" (\"id\",\"name\",\"geom\") values ('111','test',public.ST_SetSRID(public.st_geomfromgeojson('{\"coordinates\":[112.3257164817677,35.342031483036436],\"type\":\"Point\"}'),4490)) RETURNING *;\n\n```\n\n# Grammer 参数语法\n## condPackage 查询过滤条件组织/排序条件\n| params |require |paramname | description      |\n|:--------:|--|:--------: |-------------:|\n|conds|yes|组织sql筛选条件||\n|conds.field|yes|字段名||\n|conds.operator|yes|操作类型|EQ: ' = ', EQN: '!=', EQ_D: ' = ', GT: ' \u003e ', LT: '\u003c', GTE: ' \u003e= ', LTE: ' \u003c= ', FQ: ' like ' (field like '%$value%'), FQL: ' like '(field like '%$value'), FQR: ' like '(field like '$value%'), INULL: ' is null ', INNULL: ' is not null ', IN: ' in ', INN: 'not in ',JSONIN: ' ? ', GEOMINTER: '=', GEOMNOTINTER: '=' OBA: ' order by ', OBD: ' order by  ', OB: ' order by  '\n|conds.value|no|条件值||\n|conds.whereLinker|no|条件连接符|默认 and|\n|conds.condition|no|条件|传参内容为conds|\n|conds.whereLinkerCondition|no|内部条件连接符|当condition存在时，连接其条件的连接符，默认and|\n|rntable|no|表名||\n \n## groupCondPackage 分组查询条件组织\n| params |require |paramname | description |\n|:--------:|--|:--------: |-------------:|\n|conds|yes|组织group by 字段条件|1.支持支持字段名,分隔  2.支持数组对象，具体参数如下|\n|conds.type|yes|操作类型|CG:常规 field正常为字段名  SUB: substring 函数，field中为substring的函数内部的内容|\n|conds.field|yes|字段名|当type为CG时，传入字段名；当type为SUB时，为substring函数内部内容|\n|conds.rename|yes|重命名字段名|返回分组查询字段时重命名|\n\n\n## statisCondPackage 聚合统计条件组织\n| params |require |paramname | description |\n|:--------:|--|:--------: |-------------:|\n|conds|yes|组织聚合统计条件||\n|conds.type|yes|聚合统计类型|ZDZ: '最大值', ZXZ: '最小值', PJZ: '平均值', BZC: '标准差', FC:'方差',QH:'求和（sum）',STR:(string_agg ,分隔 ),ARR:(array_agg),JIA,JIAN,CHENG,CHU|\n|conds.field|yes|字段名||\n|conds.rename|yes|重命名字段名|返回分组查询字段时重命名|\n|conds.dppoint|no|保留精度（小数点后几位）|返回统计字段时保留精度|\n\n## objtosql 数组对象转为insert/update语句\n|num| params |require |paramname | description |\n|--|:--------:|--|:--------: |-------------:|\n|1|tablename|yes|表名|插入/更新表名|\n|2|DataList|yes|数组对象|eg: [{\"field1\":\"value1\",\"field2\":\"value2\"}]|\n|3|config|no|配置||\n|3|config.idfield|no|主键，更新时条件字段|\n|3|config.pattern|no|插入模式|模式 【insert/auto】   insert 时，只生成insert语句，  auto时，会根据数据对象中是否存在idfield去生成 insert/update 语句|\n|3|config.timefields|no|时间字段配置|,分隔字段名，配置为时间字段的数值为-1时，会使用数据库时间now()赋值|\n|3|config.geomfields|no|空间类型字段设置（postgres时支持）|,分隔字段名|\n|3|config.geomsrid|no|空间字段坐标系类型 （postgres时支持） 默认4490 ||\n\n\n\n# example 示例\n1.支持高级查询条件\n2.支持pg的空间相交函数\n\n复杂查询示例：\n|require|conds|result|\n|---|:--------:|-------------:|\n|查询表中field1为11，并且field2同时为1和2的条件| [{\"operator\":\"EQ\",\"field\":\"field1\",\"value\":\"11\",\"condition\":[{\"operator\":\"EQ\",\"field\":\"field2\",\"value\":\"1\"},{\"whereLinker\":\"or\",\"operator\":\"EQ\",\"field\":\"field2\",\"value\":\"2\"}]}] |and \"field1\" ='11' and ( \"field2\"='1' or field2='2' )|\n|pg中查询与114,32点位相交的数据|[{\"operator\":\"GEOMINTER\",\"field\":\"geom\",\"value\":{\"type\":\"Point\",\"coordinates\":[118.530982355499,28.6730332199371]}}]| and   ( st_intersects( \"geom\" , st_setsrid(st_geomfromgeojson('{\"type\":\"Point\",\"coordinates\":[118.530982355499,28.6730332199371]}'),4490)) =  true )|\n\n\n待完善：\nsql拼接防止sql注入（todo）\n支持更多数据库类型（todo）\n支持拓展自定义查询插值条件（todo）\n\n\n\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Faixinyongheng%2Fsqlconds","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Faixinyongheng%2Fsqlconds","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Faixinyongheng%2Fsqlconds/lists"}