@budibase/backend-core 2.5.8 → 2.5.10-alpha.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (438) hide show
  1. package/dist/package.json +13 -10
  2. package/dist/src/configs/configs.d.ts +2 -1
  3. package/dist/src/configs/configs.js +9 -1
  4. package/dist/src/configs/configs.js.map +1 -1
  5. package/dist/src/constants/db.d.ts +1 -0
  6. package/dist/src/constants/db.js +1 -0
  7. package/dist/src/constants/db.js.map +1 -1
  8. package/dist/src/constants/misc.d.ts +4 -2
  9. package/dist/src/constants/misc.js +2 -0
  10. package/dist/src/constants/misc.js.map +1 -1
  11. package/dist/src/context/mainContext.d.ts +5 -2
  12. package/dist/src/context/mainContext.js +20 -3
  13. package/dist/src/context/mainContext.js.map +1 -1
  14. package/dist/src/context/types.d.ts +1 -0
  15. package/dist/src/db/couch/DatabaseImpl.js +1 -1
  16. package/dist/src/db/couch/DatabaseImpl.js.map +1 -1
  17. package/dist/src/db/index.d.ts +2 -1
  18. package/dist/src/db/index.js +15 -2
  19. package/dist/src/db/index.js.map +1 -1
  20. package/dist/src/db/lucene.d.ts +8 -14
  21. package/dist/src/db/lucene.js +164 -97
  22. package/dist/src/db/lucene.js.map +1 -1
  23. package/dist/src/db/searchIndexes/index.d.ts +1 -0
  24. package/dist/src/db/searchIndexes/index.js +18 -0
  25. package/dist/src/db/searchIndexes/index.js.map +1 -0
  26. package/dist/src/db/searchIndexes/searchIndexes.d.ts +1 -0
  27. package/dist/src/db/searchIndexes/searchIndexes.js +77 -0
  28. package/dist/src/db/searchIndexes/searchIndexes.js.map +1 -0
  29. package/dist/src/db/utils.d.ts +5 -126
  30. package/dist/src/db/utils.js +20 -232
  31. package/dist/src/db/utils.js.map +1 -1
  32. package/dist/src/db/views.d.ts +3 -1
  33. package/dist/src/db/views.js +61 -30
  34. package/dist/src/db/views.js.map +1 -1
  35. package/dist/src/docIds/conversions.js.map +1 -0
  36. package/dist/src/docIds/ids.d.ts +53 -0
  37. package/dist/src/docIds/ids.js +101 -0
  38. package/dist/src/docIds/ids.js.map +1 -0
  39. package/dist/src/docIds/index.d.ts +2 -0
  40. package/dist/src/docIds/index.js +19 -0
  41. package/dist/src/docIds/index.js.map +1 -0
  42. package/dist/src/docIds/newid.js.map +1 -0
  43. package/dist/src/docIds/params.d.ts +69 -0
  44. package/dist/src/docIds/params.js +138 -0
  45. package/dist/src/docIds/params.js.map +1 -0
  46. package/dist/src/docUpdates/index.d.ts +2 -0
  47. package/dist/src/docUpdates/index.js +36 -0
  48. package/dist/src/docUpdates/index.js.map +1 -0
  49. package/dist/src/environment.d.ts +4 -2
  50. package/dist/src/environment.js +35 -2
  51. package/dist/src/environment.js.map +1 -1
  52. package/dist/src/errors/errors.d.ts +3 -0
  53. package/dist/src/errors/errors.js +8 -1
  54. package/dist/src/errors/errors.js.map +1 -1
  55. package/dist/src/events/asyncEvents/index.d.ts +2 -0
  56. package/dist/src/events/asyncEvents/index.js +19 -0
  57. package/dist/src/events/asyncEvents/index.js.map +1 -0
  58. package/dist/src/events/asyncEvents/publisher.d.ts +2 -0
  59. package/dist/src/events/asyncEvents/publisher.js +27 -0
  60. package/dist/src/events/asyncEvents/publisher.js.map +1 -0
  61. package/dist/src/events/asyncEvents/queue.d.ts +11 -0
  62. package/dist/src/events/asyncEvents/queue.js +26 -0
  63. package/dist/src/events/asyncEvents/queue.js.map +1 -0
  64. package/dist/src/events/documentId.d.ts +2 -0
  65. package/dist/src/events/documentId.js +28 -0
  66. package/dist/src/events/documentId.js.map +1 -0
  67. package/dist/src/events/events.js +9 -0
  68. package/dist/src/events/events.js.map +1 -1
  69. package/dist/src/events/identification.d.ts +1 -0
  70. package/dist/src/events/identification.js +3 -2
  71. package/dist/src/events/identification.js.map +1 -1
  72. package/dist/src/events/index.d.ts +1 -0
  73. package/dist/src/events/index.js +3 -1
  74. package/dist/src/events/index.js.map +1 -1
  75. package/dist/src/events/processors/LoggingProcessor.js +3 -17
  76. package/dist/src/events/processors/LoggingProcessor.js.map +1 -1
  77. package/dist/src/events/processors/Processors.js +9 -3
  78. package/dist/src/events/processors/Processors.js.map +1 -1
  79. package/dist/src/events/processors/async/DocumentUpdateProcessor.d.ts +13 -0
  80. package/dist/src/events/processors/async/DocumentUpdateProcessor.js +44 -0
  81. package/dist/src/events/processors/async/DocumentUpdateProcessor.js.map +1 -0
  82. package/dist/src/events/processors/posthog/PosthogProcessor.js +1 -2
  83. package/dist/src/events/processors/posthog/PosthogProcessor.js.map +1 -1
  84. package/dist/src/events/processors/types.d.ts +1 -11
  85. package/dist/src/events/processors/types.js +0 -6
  86. package/dist/src/events/processors/types.js.map +1 -1
  87. package/dist/src/events/publishers/group.js +6 -0
  88. package/dist/src/events/publishers/group.js.map +1 -1
  89. package/dist/src/events/publishers/license.d.ts +9 -4
  90. package/dist/src/events/publishers/license.js +2 -17
  91. package/dist/src/events/publishers/license.js.map +1 -1
  92. package/dist/src/events/publishers/user.js +4 -0
  93. package/dist/src/events/publishers/user.js.map +1 -1
  94. package/dist/src/index.d.ts +6 -2
  95. package/dist/src/index.js +2 -1
  96. package/dist/src/index.js.map +1 -1
  97. package/dist/src/installation.d.ts +1 -0
  98. package/dist/src/installation.js +7 -6
  99. package/dist/src/installation.js.map +1 -1
  100. package/dist/src/{logging.d.ts → logging/alerts.d.ts} +0 -5
  101. package/dist/src/logging/alerts.js +24 -0
  102. package/dist/src/logging/alerts.js.map +1 -0
  103. package/dist/src/logging/correlation/correlation.d.ts +2 -0
  104. package/dist/src/logging/correlation/correlation.js +17 -0
  105. package/dist/src/logging/correlation/correlation.js.map +1 -0
  106. package/dist/src/logging/correlation/index.d.ts +1 -0
  107. package/dist/src/logging/correlation/index.js +18 -0
  108. package/dist/src/logging/correlation/index.js.map +1 -0
  109. package/dist/src/logging/correlation/middleware.d.ts +2 -0
  110. package/dist/src/logging/correlation/middleware.js +17 -0
  111. package/dist/src/logging/correlation/middleware.js.map +1 -0
  112. package/dist/src/logging/index.d.ts +4 -0
  113. package/dist/src/logging/index.js +36 -0
  114. package/dist/src/logging/index.js.map +1 -0
  115. package/dist/src/logging/pino/logger.d.ts +2 -0
  116. package/dist/src/logging/pino/logger.js +167 -0
  117. package/dist/src/logging/pino/logger.js.map +1 -0
  118. package/dist/src/logging/pino/middleware.d.ts +4 -0
  119. package/dist/src/logging/pino/middleware.js +47 -0
  120. package/dist/src/logging/pino/middleware.js.map +1 -0
  121. package/dist/src/middleware/authenticated.js +6 -3
  122. package/dist/src/middleware/authenticated.js.map +1 -1
  123. package/dist/src/middleware/errorHandling.js +5 -7
  124. package/dist/src/middleware/errorHandling.js.map +1 -1
  125. package/dist/src/middleware/index.d.ts +2 -1
  126. package/dist/src/middleware/index.js +5 -3
  127. package/dist/src/middleware/index.js.map +1 -1
  128. package/dist/src/middleware/passport/sso/oidc.js +2 -5
  129. package/dist/src/middleware/passport/sso/oidc.js.map +1 -1
  130. package/dist/src/middleware/tenancy.d.ts +1 -1
  131. package/dist/src/migrations/migrations.js +4 -4
  132. package/dist/src/migrations/migrations.js.map +1 -1
  133. package/dist/src/objectStore/objectStore.js +3 -3
  134. package/dist/src/objectStore/objectStore.js.map +1 -1
  135. package/dist/src/plugin/utils.js +53 -2
  136. package/dist/src/plugin/utils.js.map +1 -1
  137. package/dist/src/queue/constants.d.ts +2 -1
  138. package/dist/src/queue/constants.js +1 -0
  139. package/dist/src/queue/constants.js.map +1 -1
  140. package/dist/src/redis/redlockImpl.js +4 -1
  141. package/dist/src/redis/redlockImpl.js.map +1 -1
  142. package/dist/src/security/encryption.d.ts +1 -0
  143. package/dist/src/security/encryption.js +2 -1
  144. package/dist/src/security/encryption.js.map +1 -1
  145. package/dist/src/security/permissions.d.ts +35 -2
  146. package/dist/src/security/permissions.js +9 -6
  147. package/dist/src/security/permissions.js.map +1 -1
  148. package/dist/src/tenancy/tenancy.js +1 -5
  149. package/dist/src/tenancy/tenancy.js.map +1 -1
  150. package/dist/src/users.d.ts +11 -1
  151. package/dist/src/users.js +45 -1
  152. package/dist/src/users.js.map +1 -1
  153. package/dist/src/utils/hashing.d.ts +1 -1
  154. package/dist/src/utils/hashing.js +1 -1
  155. package/dist/src/utils/hashing.js.map +1 -1
  156. package/dist/src/utils/index.d.ts +1 -0
  157. package/dist/src/utils/index.js +1 -0
  158. package/dist/src/utils/index.js.map +1 -1
  159. package/dist/src/utils/stringUtils.d.ts +1 -0
  160. package/dist/src/utils/stringUtils.js +9 -0
  161. package/dist/src/utils/stringUtils.js.map +1 -0
  162. package/dist/src/utils/utils.d.ts +0 -1
  163. package/dist/src/utils/utils.js +1 -24
  164. package/dist/src/utils/utils.js.map +1 -1
  165. package/dist/tests/core/logging.js.map +1 -0
  166. package/dist/tests/{utilities → core/utilities}/index.d.ts +1 -2
  167. package/dist/tests/{utilities → core/utilities}/index.js +2 -7
  168. package/dist/tests/core/utilities/index.js.map +1 -0
  169. package/dist/tests/core/utilities/jestUtils.js.map +1 -0
  170. package/dist/tests/core/utilities/mocks/alerts.d.ts +3 -0
  171. package/dist/tests/core/utilities/mocks/alerts.js +30 -0
  172. package/dist/tests/core/utilities/mocks/alerts.js.map +1 -0
  173. package/dist/tests/core/utilities/mocks/date.js.map +1 -0
  174. package/dist/tests/core/utilities/mocks/events.js +144 -0
  175. package/dist/tests/core/utilities/mocks/events.js.map +1 -0
  176. package/dist/tests/core/utilities/mocks/fetch.d.ts +33 -0
  177. package/dist/tests/core/utilities/mocks/fetch.js +15 -0
  178. package/dist/tests/core/utilities/mocks/fetch.js.map +1 -0
  179. package/dist/tests/{utilities → core/utilities}/mocks/index.d.ts +4 -3
  180. package/dist/tests/{utilities → core/utilities}/mocks/index.js +7 -3
  181. package/dist/tests/core/utilities/mocks/index.js.map +1 -0
  182. package/dist/tests/{utilities → core/utilities}/mocks/licenses.d.ts +1 -0
  183. package/dist/tests/{utilities → core/utilities}/mocks/licenses.js +5 -1
  184. package/dist/tests/core/utilities/mocks/licenses.js.map +1 -0
  185. package/dist/tests/core/utilities/mocks/posthog.js.map +1 -0
  186. package/dist/tests/core/utilities/structures/Chance.d.ts +7 -0
  187. package/dist/tests/core/utilities/structures/Chance.js +21 -0
  188. package/dist/tests/core/utilities/structures/Chance.js.map +1 -0
  189. package/dist/tests/{utilities → core/utilities}/structures/accounts.js +3 -25
  190. package/dist/tests/core/utilities/structures/accounts.js.map +1 -0
  191. package/dist/tests/{utilities → core/utilities}/structures/apps.js +1 -1
  192. package/dist/tests/core/utilities/structures/apps.js.map +1 -0
  193. package/dist/tests/core/utilities/structures/common.js.map +1 -0
  194. package/dist/tests/{utilities → core/utilities}/structures/db.js +1 -1
  195. package/dist/tests/core/utilities/structures/db.js.map +1 -0
  196. package/dist/tests/core/utilities/structures/generator.d.ts +2 -0
  197. package/dist/tests/{utilities → core/utilities}/structures/generator.js +2 -2
  198. package/dist/tests/core/utilities/structures/generator.js.map +1 -0
  199. package/dist/tests/{utilities → core/utilities}/structures/index.d.ts +2 -0
  200. package/dist/tests/{utilities → core/utilities}/structures/index.js +3 -1
  201. package/dist/tests/core/utilities/structures/index.js.map +1 -0
  202. package/dist/tests/core/utilities/structures/koa.js.map +1 -0
  203. package/dist/tests/core/utilities/structures/licenses.d.ts +17 -0
  204. package/dist/tests/core/utilities/structures/licenses.js +127 -0
  205. package/dist/tests/core/utilities/structures/licenses.js.map +1 -0
  206. package/dist/tests/core/utilities/structures/plugins.js.map +1 -0
  207. package/dist/tests/core/utilities/structures/quotas.d.ts +2 -0
  208. package/dist/tests/core/utilities/structures/quotas.js +71 -0
  209. package/dist/tests/core/utilities/structures/quotas.js.map +1 -0
  210. package/dist/tests/core/utilities/structures/scim.d.ts +14 -0
  211. package/dist/tests/core/utilities/structures/scim.js +67 -0
  212. package/dist/tests/core/utilities/structures/scim.js.map +1 -0
  213. package/dist/tests/core/utilities/structures/shared.js.map +1 -0
  214. package/dist/tests/core/utilities/structures/sso.js.map +1 -0
  215. package/dist/tests/{utilities → core/utilities}/structures/tenants.js +1 -1
  216. package/dist/tests/core/utilities/structures/tenants.js.map +1 -0
  217. package/dist/tests/core/utilities/structures/userGroups.js.map +1 -0
  218. package/dist/tests/core/utilities/structures/users.js.map +1 -0
  219. package/dist/tests/core/utilities/testContainerUtils.js.map +1 -0
  220. package/dist/tests/core/utilities/utils/index.d.ts +1 -0
  221. package/dist/tests/core/utilities/utils/index.js +28 -0
  222. package/dist/tests/core/utilities/utils/index.js.map +1 -0
  223. package/dist/tests/core/utilities/utils/time.d.ts +1 -0
  224. package/dist/tests/core/utilities/utils/time.js +8 -0
  225. package/dist/tests/core/utilities/utils/time.js.map +1 -0
  226. package/dist/tests/{utilities → extra}/DBTestConfiguration.d.ts +1 -1
  227. package/dist/tests/{utilities → extra}/DBTestConfiguration.js +2 -2
  228. package/dist/tests/extra/DBTestConfiguration.js.map +1 -0
  229. package/dist/tests/extra/index.d.ts +2 -0
  230. package/dist/tests/extra/index.js +33 -0
  231. package/dist/tests/extra/index.js.map +1 -0
  232. package/dist/tests/{utilities → extra}/testEnv.d.ts +1 -1
  233. package/dist/tests/{utilities → extra}/testEnv.js +1 -1
  234. package/dist/tests/extra/testEnv.js.map +1 -0
  235. package/dist/tests/index.d.ts +1 -1
  236. package/dist/tests/index.js +1 -1
  237. package/dist/tests/index.js.map +1 -1
  238. package/dist/tests/jestEnv.js +0 -1
  239. package/dist/tests/jestEnv.js.map +1 -1
  240. package/dist/tests/jestSetup.d.ts +1 -1
  241. package/dist/tests/jestSetup.js +2 -2
  242. package/dist/tests/jestSetup.js.map +1 -1
  243. package/dist/tsconfig.build.tsbuildinfo +1 -1
  244. package/package.json +13 -10
  245. package/src/auth/tests/auth.spec.ts +2 -1
  246. package/src/cache/tests/writethrough.spec.ts +4 -3
  247. package/src/configs/configs.ts +9 -0
  248. package/src/configs/tests/configs.spec.ts +2 -6
  249. package/src/constants/db.ts +1 -0
  250. package/src/constants/misc.ts +2 -0
  251. package/src/context/mainContext.ts +22 -5
  252. package/src/context/tests/index.spec.ts +16 -3
  253. package/src/context/types.ts +1 -0
  254. package/src/db/couch/DatabaseImpl.ts +1 -1
  255. package/src/db/index.ts +2 -1
  256. package/src/db/lucene.ts +156 -99
  257. package/src/db/searchIndexes/index.ts +1 -0
  258. package/src/db/searchIndexes/searchIndexes.ts +62 -0
  259. package/src/db/tests/lucene.spec.ts +101 -1
  260. package/src/db/tests/utils.spec.ts +1 -1
  261. package/src/db/utils.ts +7 -275
  262. package/src/db/views.ts +64 -25
  263. package/src/docIds/ids.ts +102 -0
  264. package/src/docIds/index.ts +2 -0
  265. package/src/docIds/params.ts +174 -0
  266. package/src/docUpdates/index.ts +29 -0
  267. package/src/environment.ts +44 -2
  268. package/src/errors/errors.ts +8 -0
  269. package/src/events/asyncEvents/index.ts +2 -0
  270. package/src/events/asyncEvents/publisher.ts +12 -0
  271. package/src/events/asyncEvents/queue.ts +22 -0
  272. package/src/events/documentId.ts +56 -0
  273. package/src/events/events.ts +10 -1
  274. package/src/events/identification.ts +3 -3
  275. package/src/events/index.ts +2 -0
  276. package/src/events/processors/LoggingProcessor.ts +3 -22
  277. package/src/events/processors/Processors.ts +9 -3
  278. package/src/events/processors/async/DocumentUpdateProcessor.ts +43 -0
  279. package/src/events/processors/posthog/PosthogProcessor.ts +1 -2
  280. package/src/events/processors/posthog/tests/PosthogProcessor.spec.ts +1 -1
  281. package/src/events/processors/types.ts +1 -18
  282. package/src/events/publishers/group.ts +6 -1
  283. package/src/events/publishers/license.ts +12 -13
  284. package/src/events/publishers/user.ts +4 -0
  285. package/src/index.ts +1 -0
  286. package/src/installation.ts +4 -5
  287. package/src/logging/alerts.ts +26 -0
  288. package/src/logging/correlation/correlation.ts +13 -0
  289. package/src/logging/correlation/index.ts +1 -0
  290. package/src/logging/correlation/middleware.ts +17 -0
  291. package/src/logging/index.ts +6 -0
  292. package/src/logging/pino/logger.ts +173 -0
  293. package/src/logging/pino/middleware.ts +45 -0
  294. package/src/middleware/authenticated.ts +9 -3
  295. package/src/middleware/errorHandling.ts +4 -4
  296. package/src/middleware/index.ts +2 -1
  297. package/src/middleware/passport/sso/oidc.ts +2 -9
  298. package/src/middleware/passport/sso/tests/sso.spec.ts +2 -1
  299. package/src/migrations/migrations.ts +4 -8
  300. package/src/migrations/tests/__snapshots__/migrations.spec.ts.snap +1 -1
  301. package/src/migrations/tests/migrations.spec.ts +1 -1
  302. package/src/objectStore/buckets/tests/app.spec.ts +1 -1
  303. package/src/objectStore/buckets/tests/global.spec.ts +1 -1
  304. package/src/objectStore/buckets/tests/plugins.spec.ts +2 -1
  305. package/src/objectStore/objectStore.ts +3 -3
  306. package/src/platform/tests/tenants.spec.ts +2 -1
  307. package/src/plugin/tests/validation.spec.ts +83 -0
  308. package/src/plugin/utils.ts +63 -3
  309. package/src/queue/constants.ts +1 -0
  310. package/src/redis/redlockImpl.ts +9 -3
  311. package/src/security/encryption.ts +1 -1
  312. package/src/security/permissions.ts +5 -5
  313. package/src/security/tests/encryption.spec.ts +31 -0
  314. package/src/security/tests/permissions.spec.ts +145 -0
  315. package/src/tenancy/tenancy.ts +2 -2
  316. package/src/tenancy/tests/tenancy.spec.ts +184 -0
  317. package/src/users.ts +56 -7
  318. package/src/utils/hashing.ts +1 -1
  319. package/src/utils/index.ts +1 -0
  320. package/src/utils/stringUtils.ts +8 -0
  321. package/src/utils/tests/utils.spec.ts +2 -1
  322. package/src/utils/utils.ts +3 -23
  323. package/tests/{utilities → core/utilities}/index.ts +1 -4
  324. package/tests/core/utilities/mocks/alerts.ts +3 -0
  325. package/tests/core/utilities/mocks/events.ts +133 -0
  326. package/tests/core/utilities/mocks/fetch.ts +17 -0
  327. package/tests/{utilities → core/utilities}/mocks/index.ts +4 -3
  328. package/tests/{utilities → core/utilities}/mocks/licenses.ts +4 -0
  329. package/tests/core/utilities/structures/Chance.ts +20 -0
  330. package/tests/{utilities → core/utilities}/structures/accounts.ts +4 -3
  331. package/tests/{utilities → core/utilities}/structures/apps.ts +1 -1
  332. package/tests/{utilities → core/utilities}/structures/db.ts +1 -1
  333. package/tests/{utilities → core/utilities}/structures/generator.ts +1 -1
  334. package/tests/{utilities → core/utilities}/structures/index.ts +2 -0
  335. package/tests/core/utilities/structures/licenses.ts +145 -0
  336. package/tests/core/utilities/structures/quotas.ts +67 -0
  337. package/tests/core/utilities/structures/scim.ts +81 -0
  338. package/tests/{utilities → core/utilities}/structures/tenants.ts +1 -1
  339. package/tests/core/utilities/utils/index.ts +1 -0
  340. package/tests/core/utilities/utils/time.ts +3 -0
  341. package/tests/{utilities → extra}/DBTestConfiguration.ts +2 -2
  342. package/tests/extra/index.ts +2 -0
  343. package/tests/{utilities → extra}/testEnv.ts +1 -1
  344. package/tests/index.ts +1 -1
  345. package/tests/jestEnv.ts +0 -1
  346. package/tests/jestSetup.ts +2 -2
  347. package/tsconfig.build.json +3 -7
  348. package/dist/src/db/conversions.js.map +0 -1
  349. package/dist/src/logging.js +0 -56
  350. package/dist/src/logging.js.map +0 -1
  351. package/dist/src/middleware/logging.d.ts +0 -2
  352. package/dist/src/middleware/logging.js +0 -106
  353. package/dist/src/middleware/logging.js.map +0 -1
  354. package/dist/src/newid.js.map +0 -1
  355. package/dist/tests/logging.js.map +0 -1
  356. package/dist/tests/utilities/DBTestConfiguration.js.map +0 -1
  357. package/dist/tests/utilities/index.js.map +0 -1
  358. package/dist/tests/utilities/jestUtils.js.map +0 -1
  359. package/dist/tests/utilities/mocks/date.js.map +0 -1
  360. package/dist/tests/utilities/mocks/events.js +0 -126
  361. package/dist/tests/utilities/mocks/events.js.map +0 -1
  362. package/dist/tests/utilities/mocks/fetch.d.ts +0 -31
  363. package/dist/tests/utilities/mocks/fetch.js +0 -8
  364. package/dist/tests/utilities/mocks/fetch.js.map +0 -1
  365. package/dist/tests/utilities/mocks/index.js.map +0 -1
  366. package/dist/tests/utilities/mocks/licenses.js.map +0 -1
  367. package/dist/tests/utilities/mocks/posthog.js.map +0 -1
  368. package/dist/tests/utilities/structures/accounts.js.map +0 -1
  369. package/dist/tests/utilities/structures/apps.js.map +0 -1
  370. package/dist/tests/utilities/structures/common.js.map +0 -1
  371. package/dist/tests/utilities/structures/db.js.map +0 -1
  372. package/dist/tests/utilities/structures/generator.d.ts +0 -1
  373. package/dist/tests/utilities/structures/generator.js.map +0 -1
  374. package/dist/tests/utilities/structures/index.js.map +0 -1
  375. package/dist/tests/utilities/structures/koa.js.map +0 -1
  376. package/dist/tests/utilities/structures/licenses.d.ts +0 -5
  377. package/dist/tests/utilities/structures/licenses.js +0 -18
  378. package/dist/tests/utilities/structures/licenses.js.map +0 -1
  379. package/dist/tests/utilities/structures/plugins.js.map +0 -1
  380. package/dist/tests/utilities/structures/shared.js.map +0 -1
  381. package/dist/tests/utilities/structures/sso.js.map +0 -1
  382. package/dist/tests/utilities/structures/tenants.js.map +0 -1
  383. package/dist/tests/utilities/structures/userGroups.js.map +0 -1
  384. package/dist/tests/utilities/structures/users.js.map +0 -1
  385. package/dist/tests/utilities/testContainerUtils.js.map +0 -1
  386. package/dist/tests/utilities/testEnv.js.map +0 -1
  387. package/src/logging.ts +0 -60
  388. package/src/middleware/logging.ts +0 -90
  389. package/tests/utilities/mocks/events.ts +0 -122
  390. package/tests/utilities/mocks/fetch.ts +0 -10
  391. package/tests/utilities/structures/licenses.ts +0 -18
  392. /package/dist/src/{db → docIds}/conversions.d.ts +0 -0
  393. /package/dist/src/{db → docIds}/conversions.js +0 -0
  394. /package/dist/src/{newid.d.ts → docIds/newid.d.ts} +0 -0
  395. /package/dist/src/{newid.js → docIds/newid.js} +0 -0
  396. /package/dist/tests/{logging.d.ts → core/logging.d.ts} +0 -0
  397. /package/dist/tests/{logging.js → core/logging.js} +0 -0
  398. /package/dist/tests/{utilities → core/utilities}/jestUtils.d.ts +0 -0
  399. /package/dist/tests/{utilities → core/utilities}/jestUtils.js +0 -0
  400. /package/dist/tests/{utilities → core/utilities}/mocks/date.d.ts +0 -0
  401. /package/dist/tests/{utilities → core/utilities}/mocks/date.js +0 -0
  402. /package/dist/tests/{utilities → core/utilities}/mocks/events.d.ts +0 -0
  403. /package/dist/tests/{utilities → core/utilities}/mocks/posthog.d.ts +0 -0
  404. /package/dist/tests/{utilities → core/utilities}/mocks/posthog.js +0 -0
  405. /package/dist/tests/{utilities → core/utilities}/structures/accounts.d.ts +0 -0
  406. /package/dist/tests/{utilities → core/utilities}/structures/apps.d.ts +0 -0
  407. /package/dist/tests/{utilities → core/utilities}/structures/common.d.ts +0 -0
  408. /package/dist/tests/{utilities → core/utilities}/structures/common.js +0 -0
  409. /package/dist/tests/{utilities → core/utilities}/structures/db.d.ts +0 -0
  410. /package/dist/tests/{utilities → core/utilities}/structures/koa.d.ts +0 -0
  411. /package/dist/tests/{utilities → core/utilities}/structures/koa.js +0 -0
  412. /package/dist/tests/{utilities → core/utilities}/structures/plugins.d.ts +0 -0
  413. /package/dist/tests/{utilities → core/utilities}/structures/plugins.js +0 -0
  414. /package/dist/tests/{utilities → core/utilities}/structures/shared.d.ts +0 -0
  415. /package/dist/tests/{utilities → core/utilities}/structures/shared.js +0 -0
  416. /package/dist/tests/{utilities → core/utilities}/structures/sso.d.ts +0 -0
  417. /package/dist/tests/{utilities → core/utilities}/structures/sso.js +0 -0
  418. /package/dist/tests/{utilities → core/utilities}/structures/tenants.d.ts +0 -0
  419. /package/dist/tests/{utilities → core/utilities}/structures/userGroups.d.ts +0 -0
  420. /package/dist/tests/{utilities → core/utilities}/structures/userGroups.js +0 -0
  421. /package/dist/tests/{utilities → core/utilities}/structures/users.d.ts +0 -0
  422. /package/dist/tests/{utilities → core/utilities}/structures/users.js +0 -0
  423. /package/dist/tests/{utilities → core/utilities}/testContainerUtils.d.ts +0 -0
  424. /package/dist/tests/{utilities → core/utilities}/testContainerUtils.js +0 -0
  425. /package/src/{db → docIds}/conversions.ts +0 -0
  426. /package/src/{newid.ts → docIds/newid.ts} +0 -0
  427. /package/tests/{logging.ts → core/logging.ts} +0 -0
  428. /package/tests/{utilities → core/utilities}/jestUtils.ts +0 -0
  429. /package/tests/{utilities → core/utilities}/mocks/date.ts +0 -0
  430. /package/tests/{utilities → core/utilities}/mocks/posthog.ts +0 -0
  431. /package/tests/{utilities → core/utilities}/structures/common.ts +0 -0
  432. /package/tests/{utilities → core/utilities}/structures/koa.ts +0 -0
  433. /package/tests/{utilities → core/utilities}/structures/plugins.ts +0 -0
  434. /package/tests/{utilities → core/utilities}/structures/shared.ts +0 -0
  435. /package/tests/{utilities → core/utilities}/structures/sso.ts +0 -0
  436. /package/tests/{utilities → core/utilities}/structures/userGroups.ts +0 -0
  437. /package/tests/{utilities → core/utilities}/structures/users.ts +0 -0
  438. /package/tests/{utilities → core/utilities}/testContainerUtils.ts +0 -0
package/src/db/lucene.ts CHANGED
@@ -1,12 +1,14 @@
1
1
  import fetch from "node-fetch"
2
2
  import { getCouchInfo } from "./couch"
3
3
  import { SearchFilters, Row } from "@budibase/types"
4
+ import { createUserIndex } from "./searchIndexes/searchIndexes"
4
5
 
5
6
  const QUERY_START_REGEX = /\d[0-9]*:/g
6
7
 
7
8
  interface SearchResponse<T> {
8
9
  rows: T[] | any[]
9
- bookmark: string
10
+ bookmark?: string
11
+ totalRows: number
10
12
  }
11
13
 
12
14
  interface PaginatedSearchResponse<T> extends SearchResponse<T> {
@@ -42,23 +44,26 @@ export function removeKeyNumbering(key: any): string {
42
44
  * Optionally takes a base lucene query object.
43
45
  */
44
46
  export class QueryBuilder<T> {
45
- dbName: string
46
- index: string
47
- query: SearchFilters
48
- limit: number
49
- sort?: string
50
- bookmark?: string
51
- sortOrder: string
52
- sortType: string
53
- includeDocs: boolean
54
- version?: string
55
- indexBuilder?: () => Promise<any>
56
- noEscaping = false
47
+ #dbName: string
48
+ #index: string
49
+ #query: SearchFilters
50
+ #limit: number
51
+ #sort?: string
52
+ #bookmark?: string
53
+ #sortOrder: string
54
+ #sortType: string
55
+ #includeDocs: boolean
56
+ #version?: string
57
+ #indexBuilder?: () => Promise<any>
58
+ #noEscaping = false
59
+ #skip?: number
60
+
61
+ static readonly maxLimit = 200
57
62
 
58
63
  constructor(dbName: string, index: string, base?: SearchFilters) {
59
- this.dbName = dbName
60
- this.index = index
61
- this.query = {
64
+ this.#dbName = dbName
65
+ this.#index = index
66
+ this.#query = {
62
67
  allOr: false,
63
68
  string: {},
64
69
  fuzzy: {},
@@ -73,86 +78,96 @@ export class QueryBuilder<T> {
73
78
  containsAny: {},
74
79
  ...base,
75
80
  }
76
- this.limit = 50
77
- this.sortOrder = "ascending"
78
- this.sortType = "string"
79
- this.includeDocs = true
81
+ this.#limit = 50
82
+ this.#sortOrder = "ascending"
83
+ this.#sortType = "string"
84
+ this.#includeDocs = true
80
85
  }
81
86
 
82
87
  disableEscaping() {
83
- this.noEscaping = true
88
+ this.#noEscaping = true
84
89
  return this
85
90
  }
86
91
 
87
92
  setIndexBuilder(builderFn: () => Promise<any>) {
88
- this.indexBuilder = builderFn
93
+ this.#indexBuilder = builderFn
89
94
  return this
90
95
  }
91
96
 
92
97
  setVersion(version?: string) {
93
98
  if (version != null) {
94
- this.version = version
99
+ this.#version = version
95
100
  }
96
101
  return this
97
102
  }
98
103
 
99
104
  setTable(tableId: string) {
100
- this.query.equal!.tableId = tableId
105
+ this.#query.equal!.tableId = tableId
101
106
  return this
102
107
  }
103
108
 
104
109
  setLimit(limit?: number) {
105
110
  if (limit != null) {
106
- this.limit = limit
111
+ this.#limit = limit
107
112
  }
108
113
  return this
109
114
  }
110
115
 
111
116
  setSort(sort?: string) {
112
117
  if (sort != null) {
113
- this.sort = sort
118
+ this.#sort = sort
114
119
  }
115
120
  return this
116
121
  }
117
122
 
118
123
  setSortOrder(sortOrder?: string) {
119
124
  if (sortOrder != null) {
120
- this.sortOrder = sortOrder
125
+ this.#sortOrder = sortOrder
121
126
  }
122
127
  return this
123
128
  }
124
129
 
125
130
  setSortType(sortType?: string) {
126
131
  if (sortType != null) {
127
- this.sortType = sortType
132
+ this.#sortType = sortType
128
133
  }
129
134
  return this
130
135
  }
131
136
 
132
137
  setBookmark(bookmark?: string) {
133
138
  if (bookmark != null) {
134
- this.bookmark = bookmark
139
+ this.#bookmark = bookmark
135
140
  }
136
141
  return this
137
142
  }
138
143
 
144
+ setSkip(skip: number | undefined) {
145
+ this.#skip = skip
146
+ return this
147
+ }
148
+
139
149
  excludeDocs() {
140
- this.includeDocs = false
150
+ this.#includeDocs = false
151
+ return this
152
+ }
153
+
154
+ includeDocs() {
155
+ this.#includeDocs = true
141
156
  return this
142
157
  }
143
158
 
144
159
  addString(key: string, partial: string) {
145
- this.query.string![key] = partial
160
+ this.#query.string![key] = partial
146
161
  return this
147
162
  }
148
163
 
149
164
  addFuzzy(key: string, fuzzy: string) {
150
- this.query.fuzzy![key] = fuzzy
165
+ this.#query.fuzzy![key] = fuzzy
151
166
  return this
152
167
  }
153
168
 
154
169
  addRange(key: string, low: string | number, high: string | number) {
155
- this.query.range![key] = {
170
+ this.#query.range![key] = {
156
171
  low,
157
172
  high,
158
173
  }
@@ -160,51 +175,51 @@ export class QueryBuilder<T> {
160
175
  }
161
176
 
162
177
  addEqual(key: string, value: any) {
163
- this.query.equal![key] = value
178
+ this.#query.equal![key] = value
164
179
  return this
165
180
  }
166
181
 
167
182
  addNotEqual(key: string, value: any) {
168
- this.query.notEqual![key] = value
183
+ this.#query.notEqual![key] = value
169
184
  return this
170
185
  }
171
186
 
172
187
  addEmpty(key: string, value: any) {
173
- this.query.empty![key] = value
188
+ this.#query.empty![key] = value
174
189
  return this
175
190
  }
176
191
 
177
192
  addNotEmpty(key: string, value: any) {
178
- this.query.notEmpty![key] = value
193
+ this.#query.notEmpty![key] = value
179
194
  return this
180
195
  }
181
196
 
182
197
  addOneOf(key: string, value: any) {
183
- this.query.oneOf![key] = value
198
+ this.#query.oneOf![key] = value
184
199
  return this
185
200
  }
186
201
 
187
202
  addContains(key: string, value: any) {
188
- this.query.contains![key] = value
203
+ this.#query.contains![key] = value
189
204
  return this
190
205
  }
191
206
 
192
207
  addNotContains(key: string, value: any) {
193
- this.query.notContains![key] = value
208
+ this.#query.notContains![key] = value
194
209
  return this
195
210
  }
196
211
 
197
212
  addContainsAny(key: string, value: any) {
198
- this.query.containsAny![key] = value
213
+ this.#query.containsAny![key] = value
199
214
  return this
200
215
  }
201
216
 
202
217
  setAllOr() {
203
- this.query.allOr = true
218
+ this.#query.allOr = true
204
219
  }
205
220
 
206
221
  handleSpaces(input: string) {
207
- if (this.noEscaping) {
222
+ if (this.#noEscaping) {
208
223
  return input
209
224
  } else {
210
225
  return input.replace(/ /g, "_")
@@ -219,7 +234,7 @@ export class QueryBuilder<T> {
219
234
  * @returns {string|*}
220
235
  */
221
236
  preprocess(value: any, { escape, lowercase, wrap, type }: any = {}) {
222
- const hasVersion = !!this.version
237
+ const hasVersion = !!this.#version
223
238
  // Determine if type needs wrapped
224
239
  const originalType = typeof value
225
240
  // Convert to lowercase
@@ -227,8 +242,8 @@ export class QueryBuilder<T> {
227
242
  value = value.toLowerCase ? value.toLowerCase() : value
228
243
  }
229
244
  // Escape characters
230
- if (!this.noEscaping && escape && originalType === "string") {
231
- value = `${value}`.replace(/[ #+\-&|!(){}\]^"~*?:\\]/g, "\\$&")
245
+ if (!this.#noEscaping && escape && originalType === "string") {
246
+ value = `${value}`.replace(/[ \/#+\-&|!(){}\]^"~*?:\\]/g, "\\$&")
232
247
  }
233
248
 
234
249
  // Wrap in quotes
@@ -242,7 +257,7 @@ export class QueryBuilder<T> {
242
257
 
243
258
  isMultiCondition() {
244
259
  let count = 0
245
- for (let filters of Object.values(this.query)) {
260
+ for (let filters of Object.values(this.#query)) {
246
261
  // not contains is one massive filter in allOr mode
247
262
  if (typeof filters === "object") {
248
263
  count += Object.keys(filters).length
@@ -272,13 +287,13 @@ export class QueryBuilder<T> {
272
287
 
273
288
  buildSearchQuery() {
274
289
  const builder = this
275
- let allOr = this.query && this.query.allOr
290
+ let allOr = this.#query && this.#query.allOr
276
291
  let query = allOr ? "" : "*:*"
277
292
  const allPreProcessingOpts = { escape: true, lowercase: true, wrap: true }
278
293
  let tableId
279
- if (this.query.equal!.tableId) {
280
- tableId = this.query.equal!.tableId
281
- delete this.query.equal!.tableId
294
+ if (this.#query.equal!.tableId) {
295
+ tableId = this.#query.equal!.tableId
296
+ delete this.#query.equal!.tableId
282
297
  }
283
298
 
284
299
  const equal = (key: string, value: any) => {
@@ -305,6 +320,18 @@ export class QueryBuilder<T> {
305
320
  return `${key}:(${statement})`
306
321
  }
307
322
 
323
+ const fuzzy = (key: string, value: any) => {
324
+ if (!value) {
325
+ return null
326
+ }
327
+ value = builder.preprocess(value, {
328
+ escape: true,
329
+ lowercase: true,
330
+ type: "fuzzy",
331
+ })
332
+ return `${key}:/.*${value}.*/`
333
+ }
334
+
308
335
  const notContains = (key: string, value: any) => {
309
336
  const allPrefix = allOr ? "*:* AND " : ""
310
337
  const mode = allOr ? "AND" : undefined
@@ -363,8 +390,8 @@ export class QueryBuilder<T> {
363
390
  }
364
391
 
365
392
  // Construct the actual lucene search query string from JSON structure
366
- if (this.query.string) {
367
- build(this.query.string, (key: string, value: any) => {
393
+ if (this.#query.string) {
394
+ build(this.#query.string, (key: string, value: any) => {
368
395
  if (!value) {
369
396
  return null
370
397
  }
@@ -376,8 +403,8 @@ export class QueryBuilder<T> {
376
403
  return `${key}:${value}*`
377
404
  })
378
405
  }
379
- if (this.query.range) {
380
- build(this.query.range, (key: string, value: any) => {
406
+ if (this.#query.range) {
407
+ build(this.#query.range, (key: string, value: any) => {
381
408
  if (!value) {
382
409
  return null
383
410
  }
@@ -392,47 +419,37 @@ export class QueryBuilder<T> {
392
419
  return `${key}:[${low} TO ${high}]`
393
420
  })
394
421
  }
395
- if (this.query.fuzzy) {
396
- build(this.query.fuzzy, (key: string, value: any) => {
397
- if (!value) {
398
- return null
399
- }
400
- value = builder.preprocess(value, {
401
- escape: true,
402
- lowercase: true,
403
- type: "fuzzy",
404
- })
405
- return `${key}:${value}~`
406
- })
422
+ if (this.#query.fuzzy) {
423
+ build(this.#query.fuzzy, fuzzy)
407
424
  }
408
- if (this.query.equal) {
409
- build(this.query.equal, equal)
425
+ if (this.#query.equal) {
426
+ build(this.#query.equal, equal)
410
427
  }
411
- if (this.query.notEqual) {
412
- build(this.query.notEqual, (key: string, value: any) => {
428
+ if (this.#query.notEqual) {
429
+ build(this.#query.notEqual, (key: string, value: any) => {
413
430
  if (!value) {
414
431
  return null
415
432
  }
416
433
  return `!${key}:${builder.preprocess(value, allPreProcessingOpts)}`
417
434
  })
418
435
  }
419
- if (this.query.empty) {
420
- build(this.query.empty, (key: string) => `!${key}:["" TO *]`)
436
+ if (this.#query.empty) {
437
+ build(this.#query.empty, (key: string) => `(*:* -${key}:["" TO *])`)
421
438
  }
422
- if (this.query.notEmpty) {
423
- build(this.query.notEmpty, (key: string) => `${key}:["" TO *]`)
439
+ if (this.#query.notEmpty) {
440
+ build(this.#query.notEmpty, (key: string) => `${key}:["" TO *]`)
424
441
  }
425
- if (this.query.oneOf) {
426
- build(this.query.oneOf, oneOf)
442
+ if (this.#query.oneOf) {
443
+ build(this.#query.oneOf, oneOf)
427
444
  }
428
- if (this.query.contains) {
429
- build(this.query.contains, contains)
445
+ if (this.#query.contains) {
446
+ build(this.#query.contains, contains)
430
447
  }
431
- if (this.query.notContains) {
432
- build(this.compressFilters(this.query.notContains), notContains)
448
+ if (this.#query.notContains) {
449
+ build(this.compressFilters(this.#query.notContains), notContains)
433
450
  }
434
- if (this.query.containsAny) {
435
- build(this.query.containsAny, containsAny)
451
+ if (this.#query.containsAny) {
452
+ build(this.#query.containsAny, containsAny)
436
453
  }
437
454
  // make sure table ID is always added as an AND
438
455
  if (tableId) {
@@ -446,29 +463,65 @@ export class QueryBuilder<T> {
446
463
  buildSearchBody() {
447
464
  let body: any = {
448
465
  q: this.buildSearchQuery(),
449
- limit: Math.min(this.limit, 200),
450
- include_docs: this.includeDocs,
466
+ limit: Math.min(this.#limit, QueryBuilder.maxLimit),
467
+ include_docs: this.#includeDocs,
451
468
  }
452
- if (this.bookmark) {
453
- body.bookmark = this.bookmark
469
+ if (this.#bookmark) {
470
+ body.bookmark = this.#bookmark
454
471
  }
455
- if (this.sort) {
456
- const order = this.sortOrder === "descending" ? "-" : ""
457
- const type = `<${this.sortType}>`
458
- body.sort = `${order}${this.handleSpaces(this.sort)}${type}`
472
+ if (this.#sort) {
473
+ const order = this.#sortOrder === "descending" ? "-" : ""
474
+ const type = `<${this.#sortType}>`
475
+ body.sort = `${order}${this.handleSpaces(this.#sort)}${type}`
459
476
  }
460
477
  return body
461
478
  }
462
479
 
463
480
  async run() {
481
+ if (this.#skip) {
482
+ await this.#skipItems(this.#skip)
483
+ }
484
+ return await this.#execute()
485
+ }
486
+
487
+ /**
488
+ * Lucene queries do not support pagination and use bookmarks instead.
489
+ * For the given builder, walk through pages using bookmarks until the desired
490
+ * page has been met.
491
+ */
492
+ async #skipItems(skip: number) {
493
+ // Lucene does not support pagination.
494
+ // Handle pagination by finding the right bookmark
495
+ const prevIncludeDocs = this.#includeDocs
496
+ const prevLimit = this.#limit
497
+
498
+ this.excludeDocs()
499
+ let skipRemaining = skip
500
+ let iterationFetched = 0
501
+ do {
502
+ const toSkip = Math.min(QueryBuilder.maxLimit, skipRemaining)
503
+ this.setLimit(toSkip)
504
+ const { bookmark, rows } = await this.#execute()
505
+ this.setBookmark(bookmark)
506
+ iterationFetched = rows.length
507
+ skipRemaining -= rows.length
508
+ } while (skipRemaining > 0 && iterationFetched > 0)
509
+
510
+ this.#includeDocs = prevIncludeDocs
511
+ this.#limit = prevLimit
512
+ }
513
+
514
+ async #execute() {
464
515
  const { url, cookie } = getCouchInfo()
465
- const fullPath = `${url}/${this.dbName}/_design/database/_search/${this.index}`
516
+ const fullPath = `${url}/${this.#dbName}/_design/database/_search/${
517
+ this.#index
518
+ }`
466
519
  const body = this.buildSearchBody()
467
520
  try {
468
521
  return await runQuery<T>(fullPath, body, cookie)
469
522
  } catch (err: any) {
470
- if (err.status === 404 && this.indexBuilder) {
471
- await this.indexBuilder()
523
+ if (err.status === 404 && this.#indexBuilder) {
524
+ await this.#indexBuilder()
472
525
  return await runQuery<T>(fullPath, body, cookie)
473
526
  } else {
474
527
  throw err
@@ -502,8 +555,9 @@ async function runQuery<T>(
502
555
  }
503
556
  const json = await response.json()
504
557
 
505
- let output: any = {
558
+ let output: SearchResponse<T> = {
506
559
  rows: [],
560
+ totalRows: 0,
507
561
  }
508
562
  if (json.rows != null && json.rows.length > 0) {
509
563
  output.rows = json.rows.map((row: any) => row.doc)
@@ -511,6 +565,9 @@ async function runQuery<T>(
511
565
  if (json.bookmark) {
512
566
  output.bookmark = json.bookmark
513
567
  }
568
+ if (json.total_rows) {
569
+ output.totalRows = json.total_rows
570
+ }
514
571
  return output
515
572
  }
516
573
 
@@ -543,8 +600,8 @@ async function recursiveSearch<T>(
543
600
  if (rows.length >= params.limit) {
544
601
  return rows
545
602
  }
546
- let pageSize = 200
547
- if (rows.length > params.limit - 200) {
603
+ let pageSize = QueryBuilder.maxLimit
604
+ if (rows.length > params.limit - QueryBuilder.maxLimit) {
548
605
  pageSize = params.limit - rows.length
549
606
  }
550
607
  const page = await new QueryBuilder<T>(dbName, index, query)
@@ -559,7 +616,7 @@ async function recursiveSearch<T>(
559
616
  if (!page.rows.length) {
560
617
  return rows
561
618
  }
562
- if (page.rows.length < 200) {
619
+ if (page.rows.length < QueryBuilder.maxLimit) {
563
620
  return [...rows, ...page.rows]
564
621
  }
565
622
  const newParams = {
@@ -597,7 +654,7 @@ export async function paginatedSearch<T>(
597
654
  if (limit == null || isNaN(limit) || limit < 0) {
598
655
  limit = 50
599
656
  }
600
- limit = Math.min(limit, 200)
657
+ limit = Math.min(limit, QueryBuilder.maxLimit)
601
658
  const search = new QueryBuilder<T>(dbName, index, query)
602
659
  if (params.version) {
603
660
  search.setVersion(params.version)
@@ -0,0 +1 @@
1
+ export * from "./searchIndexes"
@@ -0,0 +1,62 @@
1
+ import { User, SearchIndex } from "@budibase/types"
2
+ import { getGlobalDB } from "../../context"
3
+
4
+ export async function createUserIndex() {
5
+ const db = getGlobalDB()
6
+ let designDoc
7
+ try {
8
+ designDoc = await db.get("_design/database")
9
+ } catch (err: any) {
10
+ if (err.status === 404) {
11
+ designDoc = { _id: "_design/database" }
12
+ }
13
+ }
14
+
15
+ const fn = function (user: User) {
16
+ if (user._id && !user._id.startsWith("us_")) {
17
+ return
18
+ }
19
+ const ignoredFields = [
20
+ "_id",
21
+ "_rev",
22
+ "password",
23
+ "account",
24
+ "license",
25
+ "budibaseAccess",
26
+ "accountPortalAccess",
27
+ "csrfToken",
28
+ ]
29
+
30
+ function idx(input: Record<string, any>, prev?: string) {
31
+ for (let key of Object.keys(input)) {
32
+ if (ignoredFields.includes(key)) {
33
+ continue
34
+ }
35
+ let idxKey = prev != null ? `${prev}.${key}` : key
36
+ if (typeof input[key] === "string") {
37
+ // eslint-disable-next-line no-undef
38
+ // @ts-ignore
39
+ index(idxKey, input[key].toLowerCase(), { facet: true })
40
+ } else if (typeof input[key] !== "object") {
41
+ // eslint-disable-next-line no-undef
42
+ // @ts-ignore
43
+ index(idxKey, input[key], { facet: true })
44
+ } else {
45
+ idx(input[key], idxKey)
46
+ }
47
+ }
48
+ }
49
+ idx(user)
50
+ }
51
+
52
+ designDoc.indexes = {
53
+ [SearchIndex.USER]: {
54
+ index: fn.toString(),
55
+ analyzer: {
56
+ default: "keyword",
57
+ name: "perfield",
58
+ },
59
+ },
60
+ }
61
+ await db.put(designDoc)
62
+ }
@@ -1,4 +1,4 @@
1
- import { newid } from "../../newid"
1
+ import { newid } from "../../docIds/newid"
2
2
  import { getDB } from "../db"
3
3
  import { Database } from "@budibase/types"
4
4
  import { QueryBuilder, paginatedSearch, fullSearch } from "../lucene"
@@ -136,6 +136,106 @@ describe("lucene", () => {
136
136
  const resp = await builder.run()
137
137
  expect(resp.rows.length).toBe(2)
138
138
  })
139
+
140
+ describe("skip", () => {
141
+ const skipDbName = `db-${newid()}`
142
+ let docs: {
143
+ _id: string
144
+ property: string
145
+ array: string[]
146
+ }[]
147
+
148
+ beforeAll(async () => {
149
+ const db = getDB(skipDbName)
150
+
151
+ docs = Array(QueryBuilder.maxLimit * 2.5)
152
+ .fill(0)
153
+ .map((_, i) => ({
154
+ _id: i.toString().padStart(3, "0"),
155
+ property: `value_${i.toString().padStart(3, "0")}`,
156
+ array: [],
157
+ }))
158
+ await db.bulkDocs(docs)
159
+
160
+ await db.put({
161
+ _id: "_design/database",
162
+ indexes: {
163
+ [INDEX_NAME]: {
164
+ index: index,
165
+ analyzer: "standard",
166
+ },
167
+ },
168
+ })
169
+ })
170
+
171
+ it("should be able to apply skip", async () => {
172
+ const builder = new QueryBuilder(skipDbName, INDEX_NAME)
173
+ const firstResponse = await builder.run()
174
+ builder.setSkip(40)
175
+ const secondResponse = await builder.run()
176
+
177
+ // Return the default limit
178
+ expect(firstResponse.rows.length).toBe(50)
179
+ expect(secondResponse.rows.length).toBe(50)
180
+
181
+ // Should have the expected overlap
182
+ expect(firstResponse.rows.slice(40)).toEqual(
183
+ secondResponse.rows.slice(0, 10)
184
+ )
185
+ })
186
+
187
+ it("should handle limits", async () => {
188
+ const builder = new QueryBuilder(skipDbName, INDEX_NAME)
189
+ builder.setLimit(10)
190
+ builder.setSkip(50)
191
+ builder.setSort("_id")
192
+
193
+ const resp = await builder.run()
194
+ expect(resp.rows.length).toBe(10)
195
+ expect(resp.rows).toEqual(
196
+ docs.slice(50, 60).map(expect.objectContaining)
197
+ )
198
+ })
199
+
200
+ it("should be able to skip searching through multiple responses", async () => {
201
+ const builder = new QueryBuilder(skipDbName, INDEX_NAME)
202
+ // Skipping 2 max limits plus a little bit more
203
+ const skip = QueryBuilder.maxLimit * 2 + 37
204
+ builder.setSkip(skip)
205
+ builder.setSort("_id")
206
+ const resp = await builder.run()
207
+
208
+ expect(resp.rows.length).toBe(50)
209
+ expect(resp.rows).toEqual(
210
+ docs.slice(skip, skip + resp.rows.length).map(expect.objectContaining)
211
+ )
212
+ })
213
+
214
+ it("should not return results if skipping all docs", async () => {
215
+ const builder = new QueryBuilder(skipDbName, INDEX_NAME)
216
+ // Skipping 2 max limits plus a little bit more
217
+ const skip = docs.length + 1
218
+ builder.setSkip(skip)
219
+
220
+ const resp = await builder.run()
221
+
222
+ expect(resp.rows.length).toBe(0)
223
+ })
224
+
225
+ it("skip should respect with filters", async () => {
226
+ const builder = new QueryBuilder(skipDbName, INDEX_NAME)
227
+ builder.setLimit(10)
228
+ builder.setSkip(50)
229
+ builder.addString("property", "value_1")
230
+ builder.setSort("property")
231
+
232
+ const resp = await builder.run()
233
+ expect(resp.rows.length).toBe(10)
234
+ expect(resp.rows).toEqual(
235
+ docs.slice(150, 160).map(expect.objectContaining)
236
+ )
237
+ })
238
+ })
139
239
  })
140
240
 
141
241
  describe("paginated search", () => {
@@ -3,7 +3,7 @@ import {
3
3
  getProdAppID,
4
4
  isDevAppID,
5
5
  isProdAppID,
6
- } from "../conversions"
6
+ } from "../../docIds/conversions"
7
7
  import { generateAppID } from "../utils"
8
8
 
9
9
  describe("utils", () => {