当前位置: 首页 > Web前端 > HTML

在javascript中编写SQL

时间:2023-03-28 11:51:52 HTML

介绍基于node-postgreSql的sql客户端。什么时候需要用javascript写SQL?避免重复一些SQL,以节省冗余代码,比如根据条件查询数据${bar}`);};数据操作是否符合规范等静态检查://fooIdisarrayconstfooId=awaitconnection.many(sql`SELECTidFROMfooWHEREbar=${bar}`);//throwerrorawaitconnection.query(sql`从baz中删除foo_id=${fooId}`);应用程序在SQL产生错误时释放资源并及时释放资源:constmain=()=>{returnpool.connect((connection)=>{returnconnection.query(sql`SELECTfoo()`);});};connection无论是resolve还是reject都会一直释放资源,所以没必要用try...catch。防止SQL注入阅读更多使用连接到数据库//postgresql://[user[:password]@][host[:port]][/databasename][?name=value[&...]]import{createPool,}来自'slonik';constpool=awaitcreatePool('postgres://');从'slonik'查询导入{createPool,sql,};constpool=awaitcreatePool('postgres://');constmain=async()=>{//查询连接状态pool.getPoolState();//{//activeConnectionCount:0,//ended:false,//idleConnectionCount:0,//waitingClientCount:0,//}awaitpool.connect(()=>{pool.getPoolState();//{//activeConnectionCount:1,//ended:false,//idleConnectionCount:0,//waitingClientCount:0,//}});awaitpool.query(sql`SELECT1`);等待pool.end();};main();mockimport{createMockPool,createMockQueryResult,}from'slonik';typeOverridesType=query:(sql:string,values:PrimitiveValueExpression[],)=>Promise>,};createMockPool(overrides:OverridesType):DatabasePool;createMockQueryResult(rows:QueryResultRow[]):QueryResult;参考slonik|Github