Skip to content

用于nodejs, 一个模仿mybatis写的xml风格的sql组装器

License

Notifications You must be signed in to change notification settings

FurryWolfX/x-sql

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

29 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

example

待优化 for 里面不能再嵌套其他标签。

<root namespace="test">
    <query name="getUser">
        select * from user where 1=1
        <if condition="@flag === 1">
            and name = @name
            and id in (
            <for separator="," array="idList">
                @id
            </for>
            )
        </if>
    </query>
</root>

注意:如果不希望自动判断变量类型,使用 @@param 代替 @param

const path = require("path");
const Builder = require("@wolfx/x-sql").default;

const builder = new Builder({
  dir: path.resolve(__dirname, "./xml"),
  debug: true,
  debugCallback: log => {
    console.log(log);
  },
});

const result = builder.build("test", "getUser", {
  flag: 1,
  name: "wolfx",
  idList: [
    {
      id: 3,
    },
    {
      id: 5,
    },
  ],
});
console.log(result);

简易 API

interface SimpleBuilder {
  MYSQL: string;
  MSSQL: string;
  NONE: string;
  setDialect(value: string): void;
  escapeId(value: string): string;
  select(table: string, cols: string[], whereObject: any, op: string = "AND", orderBy?: string, limit?: number[]): string;
  count(table: string, whereObject: any, op: string = "AND"): string;
  insert(table: string, data: any): string;
  update(table: string, data: any, whereObject: any, op: string = "AND"): string;
  delete(table: string, whereObject: any, op: string = "AND"): string;
}
Builder.setDialect(Builder.MYSQL);
console.log(Builder.select("user", ["name", "age"], { id: 1, name: "wolfx" }));
console.log(Builder.count("user", {}));
console.log(Builder.insert("user", { id: 1, name: "wolfx" }));
console.log(Builder.update("user", { id: 1, name: "wolfx" }, { id: 2, name: "wolfx2" }));
console.log(Builder.delete("user", { id: 2, name: "wolfx2" }));

About

用于nodejs, 一个模仿mybatis写的xml风格的sql组装器

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published