@caitken-com/simple-sql-builder

中文文档
1.1.0发布于 2023/10/12MITCommonJS零依赖近 30 天下载 1856

Simple Query Builder

原始使用文档
文档目录14 节
  1. Public methods
  2. select
  3. insert
  4. delete
  5. update
  6. joins
  7. order
  8. group
  9. where
  10. having
  11. limit
  12. params
  13. fromJson
  14. build

Simple SQL Builder

Generates query strings ready for execution, with built-in parameter escaping & quoting, and helpful clause builder.

Public methods

  • select
  • insert
  • delete
  • update
  • joins
  • order
  • group
  • where
  • having
  • limit
  • params
  • fromJson
  • build

select

Takes an {object} with the following keys:

paramtypecomment
table{string/object}'table_name' / {'table_name': 'alias'} ^
columns{string[]}List of columns to fetch
query{object}[optional] Pre-built inner query. ^table isn't required with this option. {'alias': 'pre-built query'}

Returns self, chainable.

// Simple query example
let query = new SimpleSqlBuilder()
.select({
    'table': {'users': 'user'},
    'columns': [
        'user.first_name',
        'user.last_name',
        'YEAR(user.date_added) AS alumni'
    ]
})
.build();

/** Output:
SELECT `user`.`first_name`, `user`.`last_name`, YEAR(`user`.`date_added`) AS `alumni`
FROM `users` AS `user`
*/
---


// Inner query example
let inner = new SimpleSqlBuilder()
.select({
    'table': {'orders': 't'},
    'columns': [
        'MAX(t.date_added) AS date_added',
    ]
})
.where([
    ['t.type', '=', '?'],
])
.group([
    't.type',
])
.params([
    'Fish',
])
.build();

// Main/outer query
let query = new SimpleSqlBuilder()
.select({
    'query': {'joiner': inner},
    'columns': [
        'order.*',
    ]
})
.joins([
    {
        'type': 'inner',
        'table': { 'orders': 'order' },
        'using': [
            'date_added'
        ]
    }
])
.where([
    ['order.type', '=', '?'],
])
.group([
    'order.type'
])
.params([
    'Fish'
])
.build();

/* Output:
SELECT `order`.*
FROM (
    SELECT MAX(`t`.`date_added`) AS `date_added`
    FROM `orders` AS `t`
    WHERE `t`.`type` = 'Fish'
    GROUP BY `t`.`type`
) AS `joiner`
INNER JOIN `orders` AS `order` USING (`date_added`)
WHERE `order`.`type` = 'Fish'
GROUP BY `order`.`type`
*/

insert

Takes an {object} with the following keys:

paramtypecomment
table{string}'table_name'
columns{object}{column: value} pairs
duplicates{string}[optional] Update clause on duplicate. Caution: string not escaped

Returns self, chainable.

let query = new SimpleSqlBuilder()
.insert({
    'table': 'users',
    'columns': {
        'first_name', '?',
        'last_name', '?',
        'age': '?'
    },
    'duplicates': 'age = age + 1'
})
.params([
    'John',
    'Doe',
    30
]);

delete

Takes an {object} with the following keys:

paramtypecomment
table{string/object}'table_name' / {'table_name': 'alias'}

Returns self, chainable.

let query = new SimpleSqlBuilder()
.delete({
    'table': {'users': 'user'}
})
.where([
    ['user.id','=','?'],
])
.params([
    47,
]);

update

Takes an {object} with the following keys:

paramtypecomment
table{string/object}'table_name' / {'table_name': 'alias'}
columns{object}{column: value} pairs

Returns self, chainable.

let query = new SimpleSqlBuilder()
.update({
    'table': {'users': 'user'},
    'columns': {
        'age': '?'
    }
})
.where([
    ['user.id','=','?'],
])
.params([
    47,
    30,
]);

joins

Takes an {object[]} with the following keys:

paramtypecomment
type{string}inner / outer / left / right / cross
table{string/object}'table_name' / {'table_name': 'alias'}
conditions{array}@see where for more information ^
using{string[]}[Optional] List of columns from inner query. ^conditions not required with this option.

Returns self, chainable.

let query = new SimpleSqlBuilder()
.select({
    'table': {'users': 'user'},
    'columns': ['user.*']
})
.joins([
    {
        'type': 'left',
        'table': {'orders': 'order'},
        'conditions': [
            ['order.user_id', '=', 'user.id']
        ]
    },
    // Repeat for each join...
]);

order

Takes an {array} of strings or objects:

typecomment
{string[] / object[]}'alias.column' / {'alias.column': 'asc' / 'desc'}

Returns self, chainable.

let query = new SimpleSqlBuilder()
.order([
    'user.id',
    {'user.first_name': 'desc'},
]);

group

Takes an {array} of strings:

typecomment
{string[]}'alias.column'

Returns self, chainable.

let query = new SimpleSqlBuilder()
.group([
    'user.age',
    'user.last_name',
]);

where

Takes an {array} of any of the following conditions:

typecomment
{array}[column, operator, value] See below for all operators.
{object}{or / and: []} Closure with keyword of or or and to combine array of conditions.
{string}Allows for advanced SQL functions. Caution: string not escaped.

Returns self, chainable.

let query = new SimpleSqlBuilder()
.where([
    ['user.active', '=', true],
    {
        'or': [
            ['user.first_name', '=', '?'],
            ['user.last_name', '=', '?'],
        ]
    },
])
.params([
    'John',
    'Doe'
]);

// Output: WHERE `user`.`active` = 1 AND (`user`.`first_name` = 'John' OR `user`.`last_name` = 'Doe')

Operators:

  • = Equals. ['user.age', '=', '?'].
  • <= Less than or equals. ['user.age', '<=', '?'].
  • >= More than or equals. ['user.age', '>=', '?'].
  • < Less than. ['user.age', '<', '?'].
  • > More than. ['user.age', '>', '?'].
  • != Not equal. ['user.age', '!=', '?'].
  • <> Not equal. ['user.age', '<>', '?'].
  • is Typically used with value of null. ['user.age', 'is', null].
  • is not Typically used with value of null. ['user.age', 'is not', null].
  • between Value must be an array of ["start", "end"] values. ['user.age', 'between', ['?','?']].
  • in Value must be an {array} ['user.age', 'in', ['?','?','?']].
  • not in Value must be an {array} ['user.age', 'not in', ['?','?','?']].
  • contains Performs a LIKE condition in which column value contains given value value. ['user.age', 'contains', '?'].
  • begins Performs a LIKE condition in which the column value starts with given value. ['user.age', 'beings', '?'].
  • ends Performs a LIKE conditions in which the column value ends with given value. ['user.age', 'ends', '?'].

having

Takes an {array} of conditions. @see where for more information.

Returns self, chainable.

limit

Takes either a {number} / {array}: 10 / [offset, limit]

Returns self, chainable.

// Limit
let query = new SimpleSqlBuilder()
.limit(10);

---

// Offset, limit
let query = new SimpleSqlBuilder()
.limit([100, 15]);

params

Takes either {array} of values, or {object} of key:value pairs.

Returns self, chainable.

// Numeric array of params
let query = new SimpleSqlBuilder()
.where([
    ['user.first_name', '=', '?'],
    ['user.age', '=', '?'],
    ['user.nickname', '=', '?'],
])
.params([
    'John',
    30,
    'John',
]);

---

// Placeholder key-value params
let query = new SimpleSqlBuilder()
.where([
    ['user.first_name', '=', '?:name'],
    ['user.age', '=', '?:age'],
    ['user.nickname', '=', '?:name'],
])
.params({
    'name': 'John',
    'age': 30
});

fromJson

Static method takes {JSON} with any of the above public methods as keys and returns the output of the build method.

Returns {string} The completed SQL statement.

let query = SimpleSqlBuilder.fromJson({
    'select': {
        'table': {'users': 'user'},
        'columns': ['user.*']
    },
    'where': [
        ['user.first_name', '=', '?'],
    ],
    'params': [
        'John'
    ],
    'limit': 10
});

console.log(query);

// Output: SELECT `user`.* FROM `users` AS `user` WHERE `user`.`first_name` = 'John' LIMIT 10

build

Returns {string} The completed SQL statement.

let query = new SimpleSqlBuilder()
.select({
    'table': {'users': 'user'},
    'columns': ['user.*']
})
.where([
    ['user.first_name', '=', '?'],
])
.limit(10)
.params([
    'John',
])
.build();

console.log(query);

// Output: SELECT `user`.* FROM `users` AS `user` WHERE `user`.`first_name` = 'John' LIMIT 10

版本与发布记录

@caitken-com/simple-sql-builder 共发布 3 个版本,首次发布于 2023/10/08;过去 12 个月发布了 0 个版本。

版本发布日期距今
1.1.0latest2023/10/123 年前
1.0.12023/10/083 年前
1.0.02023/10/083 年前

关于 @caitken-com/simple-sql-builder 的常见问题

@caitken-com/simple-sql-builder 是什么?

Simple Query Builder

如何安装 @caitken-com/simple-sql-builder?

在项目目录运行 npm install @caitken-com/simple-sql-builder,或使用 pnpm add / yarn add / bun add。

@caitken-com/simple-sql-builder 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/caitken-com__simple-sql-builder 是否存在。

@caitken-com/simple-sql-builder 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@caitken-com/simple-sql-builder 可以商用吗?

@caitken-com/simple-sql-builder 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@caitken-com/simple-sql-builder 的下载量有多少?

根据 npm 官方下载统计,@caitken-com/simple-sql-builder 在 2026/10/09 – 2026/11/07 这 30 天内被下载 1,856 次,日均约 62 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@caitken-com/simple-sql-builder 还在维护吗?

最新版本 1.1.0 发布于 2023/10/12(3 年前);至今共发布 3 个版本,其中过去 12 个月发布了 0 个;首次发布于 2023/10/08。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。