Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support query expression and regex #305

Merged
merged 28 commits into from
Jul 25, 2021
Merged

feat: support query expression and regex #305

merged 28 commits into from
Jul 25, 2021

Conversation

NWYLZW
Copy link
Contributor

@NWYLZW NWYLZW commented Jul 18, 2021

支持 expression

暂时只是在 test utils 中的 mock database 实现了该功能
如果这个语法可以,我会接着这个 pr 将 mysql 与 mongo 实现一遍

@codecov
Copy link

codecov bot commented Jul 18, 2021

Codecov Report

Merging #305 (88b68a2) into develop (d111515) will increase coverage by 0.03%.
The diff coverage is 100.00%.

Impacted file tree graph

@@             Coverage Diff             @@
##           develop     #305      +/-   ##
===========================================
+ Coverage    92.67%   92.71%   +0.03%     
===========================================
  Files           58       58              
  Lines        11274    11333      +59     
  Branches      2336     2357      +21     
===========================================
+ Hits         10448    10507      +59     
  Misses         826      826              
Impacted Files Coverage Δ
packages/koishi-core/src/database.ts 99.03% <100.00%> (+0.12%) ⬆️
packages/koishi-core/src/index.ts 100.00% <100.00%> (ø)
packages/koishi-test-utils/src/memory.ts 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update d111515...88b68a2. Read the comment docs.

@NWYLZW
Copy link
Contributor Author

NWYLZW commented Jul 19, 2021


theme: bricks
background: https://source.unsplash.com/collection/94734566/1920x1080
class: 'text-center'
highlighter: shiki
info: support query expression and regex

koishi orm

有关 koishi orm 对表达式与正则的支持

原 koishi orm 缺陷

原 koishi orm 虽然也有跨平台的功能,但是对于更复杂的搜索并不是支持的很好。

在这个需求上我对 koishi orm 的功能进行了一些扩充。

  • 支持正则表达式
    • mysql 与 mongo 都支持正则,所以实现起来比较简单
  • 支持计算表达式
    • mysql 需要转化为对应的计算表达式
    • mongo 与我的设计是相同的所以是直接兼容

实现效果

定义了一个如下的表结构(从单测中挪出来的)

declare module 'koishi-core' {
  interface Tables {
    foo: FooData
  }
}

interface FooData {
  id?: number
  bar: string
}

Tables.extend('foo')

使用方法

await expect(db.createFoo({ bar: 'awesome foo' }))
  .eventually.to.have.shape({ id: 1 })
await expect(db.createFoo({ bar: 'awesome bar' }))
  .eventually.to.have.shape({ id: 2 })
await expect(db.createFoo({ bar: 'awesome foo bar' }))
  .eventually.to.have.shape({ id: 3 })

// 获取被正则 /^.*foo$/ 匹配到数据
await expect(db.get('foo', {
  bar: /^.*foo$/,
})).eventually.to
  .have.nested.property('[0].bar')
  .equal('awesome foo')

// 获取 id 大于 1 的 foo
await expect(db.get('foo', {
  id: { $gt: 1 },
})).eventually.to
  .have.nested.property('[0].bar')
  .equal('awesome bar')

我还未考虑的问题

由于我目前的需求是对计算表达式与正则表达式的需求,所以我想到了条件语句、嵌套查询、以及外键绑定

@shigma
Copy link
Member

shigma commented Jul 19, 2021

或许可以同时支持 /foo/{ $regex: /foo/ },以及已经实现的 [] 也可以应用到 { $in: [] }

@NWYLZW
Copy link
Contributor Author

NWYLZW commented Jul 20, 2021

或许可以同时支持 /foo/{ $regex: /foo/ },以及已经实现的 [] 也可以应用到 { $in: [] }

前面是同时支持了,后面的这个语法还没有加

我觉得后面的语法类似于 $or,mongo 里面 $or 的语法是这样的

db.query({
  id: { $or: [{ $eq: 1 }, { $eq: 2 }] }
})

我觉得 mongo 的这个语法比较可控一点,也没有引入新的学习成本

@shigma
Copy link
Member

shigma commented Jul 20, 2021

  1. $or 也建议加上。
  2. mongo 官方支持 $in,且 $in$or 实现的性能差距巨大,不建议在可以 $in 的场景使用 $or

@shigma
Copy link
Member

shigma commented Jul 20, 2021

https://docs.mongodb.com/manual/reference/operator/query/#comparison

这里面的大部分操作符都可以实现,但是目前为了快速验证建议只实现一部分,实现 $in 是为了与现有的逻辑进行统一。

@NWYLZW
Copy link
Contributor Author

NWYLZW commented Jul 20, 2021

  1. $or 也建议加上。
  2. mongo 官方支持 $in,且 $in$or 实现的性能差距巨大,不建议在可以 $in 的场景使用 $or

均已实现

@NWYLZW
Copy link
Contributor Author

NWYLZW commented Jul 21, 2021

已支持 mysql,待合并

Copy link
Member

@shigma shigma left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

先提这些,还有些地方没看清之后再看

packages/koishi-core/src/database.ts Outdated Show resolved Hide resolved
packages/koishi-core/src/database.ts Outdated Show resolved Hide resolved
packages/koishi-core/src/database.ts Outdated Show resolved Hide resolved
packages/koishi-core/src/database.ts Outdated Show resolved Hide resolved
packages/koishi-core/src/index.ts Show resolved Hide resolved
packages/koishi-test-utils/src/memory.ts Outdated Show resolved Hide resolved
packages/koishi-test-utils/src/memory.ts Outdated Show resolved Hide resolved
packages/koishi-test-utils/tests/memory.spec.ts Outdated Show resolved Hide resolved
packages/plugin-mysql/tests/database.spec.ts Show resolved Hide resolved
@NWYLZW NWYLZW added the feature 新特性 label Jul 25, 2021
@NWYLZW NWYLZW requested a review from shigma July 25, 2021 11:46
@shigma shigma merged commit 657e4b2 into koishijs:develop Jul 25, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature 新特性
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants