regex-query.test.ts 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. import * as assert from 'power-assert'
  2. import * as Mock from '../unit/mock'
  3. import tcb from '../../../src/index'
  4. import * as Config from '../../config.local'
  5. import * as common from '../../common/index'
  6. describe('正则表达式查询', () => {
  7. const app = tcb.init(Config)
  8. const db = app.database()
  9. const collName = 'db-test-regex'
  10. const collection = db.collection(collName)
  11. const initialData = {
  12. name: 'AbCdEfxxxxxxxxxxxxxx1234结尾',
  13. name2: 'fffffffffffffffffff',
  14. array: [1, 2, 3, [4, 5, 6], { a: 1, b: { c: 'fjasklfljkas', d: false } }],
  15. deepObject: {
  16. 'l-02-01': {
  17. 'l-03-01': {
  18. 'l-04-01': {
  19. level: 1,
  20. name: 'l-01',
  21. flag: '0'
  22. }
  23. }
  24. }
  25. }
  26. }
  27. beforeAll(async () => {
  28. await common.safeCollection(db, collName)
  29. })
  30. afterAll(async () => {
  31. await db
  32. .collection(collName)
  33. .where({
  34. _id: /.*/
  35. })
  36. .remove()
  37. })
  38. it('Document - CRUD', async () => {
  39. // Create
  40. const res = await collection.add(initialData)
  41. // const id = res.ids[0]
  42. const id = res.id
  43. console.log(res)
  44. assert(id)
  45. assert(res.requestId)
  46. // Read
  47. // 直接使用正则表达式
  48. let result = await collection
  49. .where({
  50. name: /^abcdef.*\d+结尾$/i
  51. })
  52. .get()
  53. // console.log(result);
  54. assert(result.data.length > 0)
  55. // new db.RegExp
  56. result = await collection
  57. .where({
  58. name: new db.RegExp({
  59. regexp: '^abcdef.*\\d+结尾$',
  60. options: 'i'
  61. })
  62. })
  63. .get()
  64. assert(result.data.length > 0)
  65. // db.RegExp
  66. result = await collection
  67. .where({
  68. name: db.RegExp({
  69. regexp: '^abcdef.*\\d+结尾$',
  70. options: 'i'
  71. })
  72. })
  73. .get()
  74. assert(result.data.length > 0)
  75. // 多字段
  76. result = await collection
  77. .where({
  78. name: db.RegExp({
  79. regexp: '^abcdef.*\\d+结尾$',
  80. options: 'i'
  81. }),
  82. name2: /fff/
  83. })
  84. .get()
  85. assert(result.data.length > 0)
  86. // or
  87. result = await collection
  88. .where(
  89. db.command.or({
  90. name: db.RegExp({
  91. regexp: '^abcdef.*\\d+结尾$',
  92. options: 'i'
  93. }),
  94. name2: db.RegExp({
  95. regexp: 'fffffff',
  96. options: 'i'
  97. })
  98. })
  99. )
  100. .get()
  101. assert(result.data.length > 0)
  102. result = await collection
  103. .where({
  104. name: db.command.or(
  105. new db.RegExp({
  106. regexp: '^abcdef.*\\d+结尾$',
  107. options: 'i'
  108. }),
  109. db.RegExp({
  110. regexp: '^fffffff$',
  111. options: 'i'
  112. })
  113. )
  114. })
  115. .get()
  116. assert(result.data.length > 0)
  117. result = await collection
  118. .where({
  119. name: db.command.or(
  120. db.RegExp({
  121. regexp: '^abcdef.*\\d+结尾$',
  122. options: 'i'
  123. }),
  124. db.RegExp({
  125. regexp: '^fffffff$',
  126. options: 'i'
  127. })
  128. )
  129. })
  130. .update({
  131. name: 'ABCDEFxxxx5678结尾'
  132. })
  133. assert(result.updated > 0)
  134. // Delete
  135. const deleteRes = await collection
  136. .where({
  137. name: db.RegExp({
  138. regexp: '^abcdef.*\\d+结尾$',
  139. options: 'i'
  140. })
  141. })
  142. .remove()
  143. assert(deleteRes.deleted > 0)
  144. })
  145. })