@budibase/backend-core 2.9.39-alpha.8 → 2.9.39

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 (481) hide show
  1. package/dist/jest.config.js +25 -0
  2. package/dist/jest.config.js.map +1 -0
  3. package/dist/package.json +5 -5
  4. package/dist/src/accounts/accounts.js +75 -92
  5. package/dist/src/accounts/accounts.js.map +1 -7
  6. package/dist/src/accounts/api.js +85 -71
  7. package/dist/src/accounts/api.js.map +1 -7
  8. package/dist/src/accounts/index.js +16 -21
  9. package/dist/src/accounts/index.js.map +1 -7
  10. package/dist/src/auth/auth.js +178 -197
  11. package/dist/src/auth/auth.js.map +1 -7
  12. package/dist/src/auth/index.js +16 -21
  13. package/dist/src/auth/index.js.map +1 -7
  14. package/dist/src/blacklist/blacklist.js +69 -81
  15. package/dist/src/blacklist/blacklist.js.map +1 -7
  16. package/dist/src/blacklist/index.js +16 -21
  17. package/dist/src/blacklist/index.js.map +1 -7
  18. package/dist/src/cache/appMetadata.js +88 -78
  19. package/dist/src/cache/appMetadata.js.map +1 -7
  20. package/dist/src/cache/base/index.js +110 -92
  21. package/dist/src/cache/base/index.js.map +1 -7
  22. package/dist/src/cache/generic.js +26 -64
  23. package/dist/src/cache/generic.js.map +1 -7
  24. package/dist/src/cache/index.js +32 -48
  25. package/dist/src/cache/index.js.map +1 -7
  26. package/dist/src/cache/user.js +115 -91
  27. package/dist/src/cache/user.js.map +1 -7
  28. package/dist/src/cache/writethrough.js +144 -129
  29. package/dist/src/cache/writethrough.js.map +1 -7
  30. package/dist/src/configs/configs.js +265 -215
  31. package/dist/src/configs/configs.js.map +1 -7
  32. package/dist/src/configs/index.js +16 -21
  33. package/dist/src/configs/index.js.map +1 -7
  34. package/dist/src/constants/db.js +64 -104
  35. package/dist/src/constants/db.js.map +1 -7
  36. package/dist/src/constants/index.js +17 -23
  37. package/dist/src/constants/index.js.map +1 -7
  38. package/dist/src/constants/misc.js +53 -91
  39. package/dist/src/constants/misc.js.map +1 -7
  40. package/dist/src/context/Context.js +11 -33
  41. package/dist/src/context/Context.js.map +1 -7
  42. package/dist/src/context/identity.js +60 -75
  43. package/dist/src/context/identity.js.map +1 -7
  44. package/dist/src/context/index.js +31 -42
  45. package/dist/src/context/index.js.map +1 -7
  46. package/dist/src/context/mainContext.js +275 -264
  47. package/dist/src/context/mainContext.js.map +1 -7
  48. package/dist/src/context/types.js +2 -16
  49. package/dist/src/context/types.js.map +1 -7
  50. package/dist/src/db/Replication.js +81 -85
  51. package/dist/src/db/Replication.js.map +1 -7
  52. package/dist/src/db/constants.js +11 -37
  53. package/dist/src/db/constants.js.map +1 -7
  54. package/dist/src/db/couch/DatabaseImpl.js +236 -212
  55. package/dist/src/db/couch/DatabaseImpl.js.map +1 -7
  56. package/dist/src/db/couch/connections.js +81 -96
  57. package/dist/src/db/couch/connections.js.map +1 -7
  58. package/dist/src/db/couch/index.js +25 -42
  59. package/dist/src/db/couch/index.js.map +1 -7
  60. package/dist/src/db/couch/pouchDB.js +93 -103
  61. package/dist/src/db/couch/pouchDB.js.map +1 -7
  62. package/dist/src/db/couch/pouchDump.js +1 -1
  63. package/dist/src/db/couch/pouchDump.js.map +1 -7
  64. package/dist/src/db/couch/utils.js +49 -70
  65. package/dist/src/db/couch/utils.js.map +1 -7
  66. package/dist/src/db/db.js +49 -63
  67. package/dist/src/db/db.js.map +1 -7
  68. package/dist/src/db/errors.js +17 -38
  69. package/dist/src/db/errors.js.map +1 -7
  70. package/dist/src/db/index.js +45 -61
  71. package/dist/src/db/index.js.map +1 -7
  72. package/dist/src/db/lucene.js +655 -582
  73. package/dist/src/db/lucene.js.map +1 -7
  74. package/dist/src/db/searchIndexes/index.js +16 -21
  75. package/dist/src/db/searchIndexes/index.js.map +1 -7
  76. package/dist/src/db/searchIndexes/searchIndexes.js +72 -77
  77. package/dist/src/db/searchIndexes/searchIndexes.js.map +1 -7
  78. package/dist/src/db/utils.js +199 -184
  79. package/dist/src/db/utils.js.map +1 -7
  80. package/dist/src/db/views.js +187 -191
  81. package/dist/src/db/views.js.map +1 -7
  82. package/dist/src/docIds/conversions.js +48 -69
  83. package/dist/src/docIds/conversions.js.map +1 -7
  84. package/dist/src/docIds/ids.js +87 -76
  85. package/dist/src/docIds/ids.js.map +1 -7
  86. package/dist/src/docIds/index.js +17 -23
  87. package/dist/src/docIds/index.js.map +1 -7
  88. package/dist/src/docIds/newid.js +6 -29
  89. package/dist/src/docIds/newid.js.map +1 -7
  90. package/dist/src/docIds/params.js +111 -109
  91. package/dist/src/docIds/params.js.map +1 -7
  92. package/dist/src/docUpdates/index.js +29 -54
  93. package/dist/src/docUpdates/index.js.map +1 -7
  94. package/dist/src/environment.js +81 -152
  95. package/dist/src/environment.js.map +1 -7
  96. package/dist/src/errors/errors.js +77 -106
  97. package/dist/src/errors/errors.js.map +1 -7
  98. package/dist/src/errors/index.js +16 -21
  99. package/dist/src/errors/index.js.map +1 -7
  100. package/dist/src/events/analytics.js +38 -38
  101. package/dist/src/events/analytics.js.map +1 -7
  102. package/dist/src/events/asyncEvents/index.js +17 -23
  103. package/dist/src/events/asyncEvents/index.js.map +1 -7
  104. package/dist/src/events/asyncEvents/publisher.js +24 -36
  105. package/dist/src/events/asyncEvents/publisher.js.map +1 -7
  106. package/dist/src/events/asyncEvents/queue.js +21 -37
  107. package/dist/src/events/asyncEvents/queue.js.map +1 -7
  108. package/dist/src/events/backfill.js +170 -163
  109. package/dist/src/events/backfill.js.map +1 -7
  110. package/dist/src/events/documentId.js +23 -46
  111. package/dist/src/events/documentId.js.map +1 -7
  112. package/dist/src/events/events.js +68 -58
  113. package/dist/src/events/events.js.map +1 -7
  114. package/dist/src/events/identification.js +279 -254
  115. package/dist/src/events/identification.js.map +1 -7
  116. package/dist/src/events/index.js +42 -57
  117. package/dist/src/events/index.js.map +1 -7
  118. package/dist/src/events/processors/AnalyticsProcessor.js +81 -64
  119. package/dist/src/events/processors/AnalyticsProcessor.js.map +1 -7
  120. package/dist/src/events/processors/AuditLogsProcessor.js +79 -90
  121. package/dist/src/events/processors/AuditLogsProcessor.js.map +1 -7
  122. package/dist/src/events/processors/LoggingProcessor.js +39 -50
  123. package/dist/src/events/processors/LoggingProcessor.js.map +1 -7
  124. package/dist/src/events/processors/Processors.js +44 -49
  125. package/dist/src/events/processors/Processors.js.map +1 -7
  126. package/dist/src/events/processors/async/DocumentUpdateProcessor.js +37 -46
  127. package/dist/src/events/processors/async/DocumentUpdateProcessor.js.map +1 -7
  128. package/dist/src/events/processors/index.js +18 -52
  129. package/dist/src/events/processors/index.js.map +1 -7
  130. package/dist/src/events/processors/posthog/PosthogProcessor.js +126 -113
  131. package/dist/src/events/processors/posthog/PosthogProcessor.js.map +1 -7
  132. package/dist/src/events/processors/posthog/index.js +6 -34
  133. package/dist/src/events/processors/posthog/index.js.map +1 -7
  134. package/dist/src/events/processors/posthog/rateLimiting.js +104 -90
  135. package/dist/src/events/processors/posthog/rateLimiting.js.map +1 -7
  136. package/dist/src/events/processors/types.js +2 -28
  137. package/dist/src/events/processors/types.js.map +1 -7
  138. package/dist/src/events/publishers/account.js +37 -43
  139. package/dist/src/events/publishers/account.js.map +1 -7
  140. package/dist/src/events/publishers/app.js +139 -131
  141. package/dist/src/events/publishers/app.js.map +1 -7
  142. package/dist/src/events/publishers/auditLog.js +29 -37
  143. package/dist/src/events/publishers/auditLog.js.map +1 -7
  144. package/dist/src/events/publishers/auth.js +71 -71
  145. package/dist/src/events/publishers/auth.js.map +1 -7
  146. package/dist/src/events/publishers/automation.js +108 -102
  147. package/dist/src/events/publishers/automation.js.map +1 -7
  148. package/dist/src/events/publishers/backfill.js +75 -82
  149. package/dist/src/events/publishers/backfill.js.map +1 -7
  150. package/dist/src/events/publishers/backup.js +36 -44
  151. package/dist/src/events/publishers/backup.js.map +1 -7
  152. package/dist/src/events/publishers/datasource.js +46 -51
  153. package/dist/src/events/publishers/datasource.js.map +1 -7
  154. package/dist/src/events/publishers/email.js +25 -33
  155. package/dist/src/events/publishers/email.js.map +1 -7
  156. package/dist/src/events/publishers/environmentVariable.js +38 -47
  157. package/dist/src/events/publishers/environmentVariable.js.map +1 -7
  158. package/dist/src/events/publishers/group.js +97 -95
  159. package/dist/src/events/publishers/group.js.map +1 -7
  160. package/dist/src/events/publishers/index.js +53 -106
  161. package/dist/src/events/publishers/index.js.map +1 -7
  162. package/dist/src/events/publishers/installation.js +45 -49
  163. package/dist/src/events/publishers/installation.js.map +1 -7
  164. package/dist/src/events/publishers/layout.js +29 -37
  165. package/dist/src/events/publishers/layout.js.map +1 -7
  166. package/dist/src/events/publishers/license.js +67 -68
  167. package/dist/src/events/publishers/license.js.map +1 -7
  168. package/dist/src/events/publishers/org.js +44 -45
  169. package/dist/src/events/publishers/org.js.map +1 -7
  170. package/dist/src/events/publishers/plugin.js +49 -55
  171. package/dist/src/events/publishers/plugin.js.map +1 -7
  172. package/dist/src/events/publishers/query.js +70 -81
  173. package/dist/src/events/publishers/query.js.map +1 -7
  174. package/dist/src/events/publishers/role.js +61 -63
  175. package/dist/src/events/publishers/role.js.map +1 -7
  176. package/dist/src/events/publishers/rows.js +28 -39
  177. package/dist/src/events/publishers/rows.js.map +1 -7
  178. package/dist/src/events/publishers/screen.js +41 -47
  179. package/dist/src/events/publishers/screen.js.map +1 -7
  180. package/dist/src/events/publishers/serve.js +41 -47
  181. package/dist/src/events/publishers/serve.js.map +1 -7
  182. package/dist/src/events/publishers/table.js +69 -71
  183. package/dist/src/events/publishers/table.js.map +1 -7
  184. package/dist/src/events/publishers/user.js +184 -173
  185. package/dist/src/events/publishers/user.js.map +1 -7
  186. package/dist/src/events/publishers/view.js +98 -89
  187. package/dist/src/events/publishers/view.js.map +1 -7
  188. package/dist/src/features/index.js +88 -93
  189. package/dist/src/features/index.js.map +1 -7
  190. package/dist/src/features/installation.js +16 -38
  191. package/dist/src/features/installation.js.map +1 -7
  192. package/dist/src/helpers.js +11 -28
  193. package/dist/src/helpers.js.map +1 -7
  194. package/dist/src/index.js +77 -148
  195. package/dist/src/index.js.map +1 -7
  196. package/dist/src/installation.js +127 -128
  197. package/dist/src/installation.js.map +1 -7
  198. package/dist/src/logging/alerts.js +14 -39
  199. package/dist/src/logging/alerts.js.map +1 -7
  200. package/dist/src/logging/correlation/correlation.js +11 -35
  201. package/dist/src/logging/correlation/correlation.js.map +1 -7
  202. package/dist/src/logging/correlation/index.js +16 -21
  203. package/dist/src/logging/correlation/index.js.map +1 -7
  204. package/dist/src/logging/correlation/middleware.js +13 -33
  205. package/dist/src/logging/correlation/middleware.js.map +1 -7
  206. package/dist/src/logging/index.js +32 -45
  207. package/dist/src/logging/index.js.map +1 -7
  208. package/dist/src/logging/pino/logger.js +206 -185
  209. package/dist/src/logging/pino/logger.js.map +1 -7
  210. package/dist/src/logging/pino/middleware.js +38 -68
  211. package/dist/src/logging/pino/middleware.js.map +1 -7
  212. package/dist/src/logging/system.js +86 -95
  213. package/dist/src/logging/system.js.map +1 -7
  214. package/dist/src/middleware/adminOnly.js +16 -28
  215. package/dist/src/middleware/adminOnly.js.map +1 -7
  216. package/dist/src/middleware/auditLog.js +13 -24
  217. package/dist/src/middleware/auditLog.js.map +1 -7
  218. package/dist/src/middleware/authenticated.js +191 -165
  219. package/dist/src/middleware/authenticated.js.map +1 -7
  220. package/dist/src/middleware/builderOnly.js +29 -44
  221. package/dist/src/middleware/builderOnly.js.map +1 -7
  222. package/dist/src/middleware/builderOrAdmin.js +29 -44
  223. package/dist/src/middleware/builderOrAdmin.js.map +1 -7
  224. package/dist/src/middleware/csrf.js +75 -54
  225. package/dist/src/middleware/csrf.js.map +1 -7
  226. package/dist/src/middleware/errorHandling.js +60 -58
  227. package/dist/src/middleware/errorHandling.js.map +1 -7
  228. package/dist/src/middleware/index.js +61 -91
  229. package/dist/src/middleware/index.js.map +1 -7
  230. package/dist/src/middleware/internalApi.js +27 -36
  231. package/dist/src/middleware/internalApi.js.map +1 -7
  232. package/dist/src/middleware/joi-validator.js +39 -65
  233. package/dist/src/middleware/joi-validator.js.map +1 -7
  234. package/dist/src/middleware/matchers.js +36 -55
  235. package/dist/src/middleware/matchers.js.map +1 -7
  236. package/dist/src/middleware/passport/datasource/google.js +88 -96
  237. package/dist/src/middleware/passport/datasource/google.js.map +1 -7
  238. package/dist/src/middleware/passport/local.js +74 -66
  239. package/dist/src/middleware/passport/local.js.map +1 -7
  240. package/dist/src/middleware/passport/sso/google.js +81 -84
  241. package/dist/src/middleware/passport/sso/google.js.map +1 -7
  242. package/dist/src/middleware/passport/sso/oidc.js +140 -124
  243. package/dist/src/middleware/passport/sso/oidc.js.map +1 -7
  244. package/dist/src/middleware/passport/sso/sso.js +157 -141
  245. package/dist/src/middleware/passport/sso/sso.js.map +1 -7
  246. package/dist/src/middleware/passport/utils.js +60 -56
  247. package/dist/src/middleware/passport/utils.js.map +1 -7
  248. package/dist/src/middleware/querystringToBody.js +26 -42
  249. package/dist/src/middleware/querystringToBody.js.map +1 -7
  250. package/dist/src/middleware/tenancy.js +33 -42
  251. package/dist/src/middleware/tenancy.js.map +1 -7
  252. package/dist/src/migrations/definitions.js +37 -61
  253. package/dist/src/migrations/definitions.js.map +1 -7
  254. package/dist/src/migrations/index.js +17 -23
  255. package/dist/src/migrations/index.js.map +1 -7
  256. package/dist/src/migrations/migrations.js +182 -162
  257. package/dist/src/migrations/migrations.js.map +1 -7
  258. package/dist/src/objectStore/buckets/app.js +65 -57
  259. package/dist/src/objectStore/buckets/app.js.map +1 -7
  260. package/dist/src/objectStore/buckets/global.js +50 -55
  261. package/dist/src/objectStore/buckets/global.js.map +1 -7
  262. package/dist/src/objectStore/buckets/index.js +18 -25
  263. package/dist/src/objectStore/buckets/index.js.map +1 -7
  264. package/dist/src/objectStore/buckets/plugins.js +76 -82
  265. package/dist/src/objectStore/buckets/plugins.js.map +1 -7
  266. package/dist/src/objectStore/cloudfront.js +27 -61
  267. package/dist/src/objectStore/cloudfront.js.map +1 -7
  268. package/dist/src/objectStore/index.js +18 -25
  269. package/dist/src/objectStore/index.js.map +1 -7
  270. package/dist/src/objectStore/objectStore.js +351 -341
  271. package/dist/src/objectStore/objectStore.js.map +1 -7
  272. package/dist/src/objectStore/utils.js +26 -52
  273. package/dist/src/objectStore/utils.js.map +1 -7
  274. package/dist/src/platform/index.js +30 -42
  275. package/dist/src/platform/index.js.map +1 -7
  276. package/dist/src/platform/platformDb.js +7 -30
  277. package/dist/src/platform/platformDb.js.map +1 -7
  278. package/dist/src/platform/tenants.js +126 -110
  279. package/dist/src/platform/tenants.js.map +1 -7
  280. package/dist/src/platform/users.js +84 -92
  281. package/dist/src/platform/users.js.map +1 -7
  282. package/dist/src/plugin/index.js +16 -21
  283. package/dist/src/plugin/index.js.map +1 -7
  284. package/dist/src/plugin/utils.js +129 -137
  285. package/dist/src/plugin/utils.js.map +1 -7
  286. package/dist/src/queue/constants.js +10 -34
  287. package/dist/src/queue/constants.js.map +1 -7
  288. package/dist/src/queue/inMemoryQueue.js +137 -131
  289. package/dist/src/queue/inMemoryQueue.js.map +1 -7
  290. package/dist/src/queue/index.js +17 -23
  291. package/dist/src/queue/index.js.map +1 -7
  292. package/dist/src/queue/listeners.js +170 -161
  293. package/dist/src/queue/listeners.js.map +1 -7
  294. package/dist/src/queue/queue.js +83 -76
  295. package/dist/src/queue/queue.js.map +1 -7
  296. package/dist/src/redis/index.js +34 -45
  297. package/dist/src/redis/index.js.map +1 -7
  298. package/dist/src/redis/init.js +125 -115
  299. package/dist/src/redis/init.js.map +1 -7
  300. package/dist/src/redis/redis.js +302 -244
  301. package/dist/src/redis/redis.js.map +1 -7
  302. package/dist/src/redis/redlockImpl.js +152 -135
  303. package/dist/src/redis/redlockImpl.js.map +1 -7
  304. package/dist/src/redis/utils.js +115 -128
  305. package/dist/src/redis/utils.js.map +1 -7
  306. package/dist/src/security/encryption.js +141 -162
  307. package/dist/src/security/encryption.js.map +1 -7
  308. package/dist/src/security/permissions.js +132 -170
  309. package/dist/src/security/permissions.js.map +1 -7
  310. package/dist/src/security/roles.js +319 -298
  311. package/dist/src/security/roles.js.map +1 -7
  312. package/dist/src/security/sessions.js +106 -120
  313. package/dist/src/security/sessions.js.map +1 -7
  314. package/dist/src/tenancy/db.js +7 -30
  315. package/dist/src/tenancy/db.js.map +1 -7
  316. package/dist/src/tenancy/index.js +17 -23
  317. package/dist/src/tenancy/index.js.map +1 -7
  318. package/dist/src/tenancy/tenancy.js +101 -114
  319. package/dist/src/tenancy/tenancy.js.map +1 -7
  320. package/dist/src/timers/index.js +16 -21
  321. package/dist/src/timers/index.js.map +1 -7
  322. package/dist/src/timers/timers.js +18 -43
  323. package/dist/src/timers/timers.js.map +1 -7
  324. package/dist/src/users/db.js +403 -357
  325. package/dist/src/users/db.js.map +1 -7
  326. package/dist/src/users/events.js +150 -133
  327. package/dist/src/users/events.js.map +1 -7
  328. package/dist/src/users/index.js +21 -34
  329. package/dist/src/users/index.js.map +1 -7
  330. package/dist/src/users/lookup.js +104 -102
  331. package/dist/src/users/lookup.js.map +1 -7
  332. package/dist/src/users/users.js +244 -240
  333. package/dist/src/users/users.js.map +1 -7
  334. package/dist/src/users/utils.js +87 -90
  335. package/dist/src/users/utils.js.map +1 -7
  336. package/dist/src/utils/hashing.js +42 -48
  337. package/dist/src/utils/hashing.js.map +1 -7
  338. package/dist/src/utils/index.js +18 -25
  339. package/dist/src/utils/index.js.map +1 -7
  340. package/dist/src/utils/stringUtils.js +6 -30
  341. package/dist/src/utils/stringUtils.js.map +1 -7
  342. package/dist/src/utils/utils.js +212 -172
  343. package/dist/src/utils/utils.js.map +1 -7
  344. package/dist/tests/core/logging.js +21 -45
  345. package/dist/tests/core/logging.js.map +1 -7
  346. package/dist/tests/core/utilities/index.js +34 -51
  347. package/dist/tests/core/utilities/index.js.map +1 -7
  348. package/dist/tests/core/utilities/jestUtils.js +16 -45
  349. package/dist/tests/core/utilities/jestUtils.js.map +1 -7
  350. package/dist/tests/core/utilities/mocks/alerts.js +27 -38
  351. package/dist/tests/core/utilities/mocks/alerts.js.map +1 -7
  352. package/dist/tests/core/utilities/mocks/date.js +5 -31
  353. package/dist/tests/core/utilities/mocks/date.js.map +1 -7
  354. package/dist/tests/core/utilities/mocks/events.js +141 -131
  355. package/dist/tests/core/utilities/mocks/events.js.map +1 -7
  356. package/dist/tests/core/utilities/mocks/fetch.js +10 -34
  357. package/dist/tests/core/utilities/mocks/fetch.js.map +1 -7
  358. package/dist/tests/core/utilities/mocks/index.js +39 -51
  359. package/dist/tests/core/utilities/mocks/index.js.map +1 -7
  360. package/dist/tests/core/utilities/mocks/licenses.js +67 -107
  361. package/dist/tests/core/utilities/mocks/licenses.js.map +1 -7
  362. package/dist/tests/core/utilities/mocks/posthog.js +6 -6
  363. package/dist/tests/core/utilities/mocks/posthog.js.map +1 -7
  364. package/dist/tests/core/utilities/structures/Chance.js +17 -44
  365. package/dist/tests/core/utilities/structures/Chance.js.map +1 -7
  366. package/dist/tests/core/utilities/structures/accounts.js +61 -131
  367. package/dist/tests/core/utilities/structures/accounts.js.map +1 -7
  368. package/dist/tests/core/utilities/structures/apps.js +21 -44
  369. package/dist/tests/core/utilities/structures/apps.js.map +1 -7
  370. package/dist/tests/core/utilities/structures/common.js +8 -32
  371. package/dist/tests/core/utilities/structures/common.js.map +1 -7
  372. package/dist/tests/core/utilities/structures/db.js +11 -35
  373. package/dist/tests/core/utilities/structures/db.js.map +1 -7
  374. package/dist/tests/core/utilities/structures/documents/index.js +16 -21
  375. package/dist/tests/core/utilities/structures/documents/index.js.map +1 -7
  376. package/dist/tests/core/utilities/structures/documents/platform/index.js +26 -37
  377. package/dist/tests/core/utilities/structures/documents/platform/index.js.map +1 -7
  378. package/dist/tests/core/utilities/structures/documents/platform/installation.js +34 -44
  379. package/dist/tests/core/utilities/structures/documents/platform/installation.js.map +1 -7
  380. package/dist/tests/core/utilities/structures/generator.js +7 -38
  381. package/dist/tests/core/utilities/structures/generator.js.map +1 -7
  382. package/dist/tests/core/utilities/structures/index.js +43 -78
  383. package/dist/tests/core/utilities/structures/index.js.map +1 -7
  384. package/dist/tests/core/utilities/structures/koa.js +7 -39
  385. package/dist/tests/core/utilities/structures/koa.js.map +1 -7
  386. package/dist/tests/core/utilities/structures/licenses.js +121 -156
  387. package/dist/tests/core/utilities/structures/licenses.js.map +1 -7
  388. package/dist/tests/core/utilities/structures/plugins.js +20 -43
  389. package/dist/tests/core/utilities/structures/plugins.js.map +1 -7
  390. package/dist/tests/core/utilities/structures/quotas.js +67 -90
  391. package/dist/tests/core/utilities/structures/quotas.js.map +1 -7
  392. package/dist/tests/core/utilities/structures/scim.js +57 -84
  393. package/dist/tests/core/utilities/structures/scim.js.map +1 -7
  394. package/dist/tests/core/utilities/structures/shared.js +9 -41
  395. package/dist/tests/core/utilities/structures/shared.js.map +1 -7
  396. package/dist/tests/core/utilities/structures/sso.js +112 -124
  397. package/dist/tests/core/utilities/structures/sso.js.map +1 -7
  398. package/dist/tests/core/utilities/structures/tenants.js +6 -29
  399. package/dist/tests/core/utilities/structures/tenants.js.map +1 -7
  400. package/dist/tests/core/utilities/structures/userGroups.js +10 -33
  401. package/dist/tests/core/utilities/structures/userGroups.js.map +1 -7
  402. package/dist/tests/core/utilities/structures/users.js +36 -84
  403. package/dist/tests/core/utilities/structures/users.js.map +1 -7
  404. package/dist/tests/core/utilities/testContainerUtils.js +59 -84
  405. package/dist/tests/core/utilities/testContainerUtils.js.map +1 -7
  406. package/dist/tests/core/utilities/utils/index.js +26 -37
  407. package/dist/tests/core/utilities/utils/index.js.map +1 -7
  408. package/dist/tests/core/utilities/utils/time.js +5 -28
  409. package/dist/tests/core/utilities/utils/time.js.map +1 -7
  410. package/dist/tests/extra/DBTestConfiguration.js +50 -56
  411. package/dist/tests/extra/DBTestConfiguration.js.map +1 -7
  412. package/dist/tests/extra/index.js +30 -39
  413. package/dist/tests/extra/index.js.map +1 -7
  414. package/dist/tests/extra/testEnv.js +105 -84
  415. package/dist/tests/extra/testEnv.js.map +1 -7
  416. package/dist/tests/index.js +16 -21
  417. package/dist/tests/index.js.map +1 -7
  418. package/dist/tests/jestEnv.js +1 -1
  419. package/dist/tests/jestEnv.js.map +1 -7
  420. package/dist/tests/jestSetup.js +20 -33
  421. package/dist/tests/jestSetup.js.map +1 -7
  422. package/package.json +5 -5
  423. package/dist/plugins.js +0 -290
  424. package/dist/plugins.js.map +0 -7
  425. package/dist/plugins.js.meta.json +0 -1
  426. package/dist/src/auth/tests/auth.spec.js +0 -37
  427. package/dist/src/auth/tests/auth.spec.js.map +0 -7
  428. package/dist/src/blacklist/tests/blacklist.spec.js +0 -61
  429. package/dist/src/blacklist/tests/blacklist.spec.js.map +0 -7
  430. package/dist/src/cache/tests/writethrough.spec.js +0 -132
  431. package/dist/src/cache/tests/writethrough.spec.js.map +0 -7
  432. package/dist/src/configs/tests/configs.spec.js +0 -182
  433. package/dist/src/configs/tests/configs.spec.js.map +0 -7
  434. package/dist/src/context/tests/index.spec.js +0 -147
  435. package/dist/src/context/tests/index.spec.js.map +0 -7
  436. package/dist/src/db/tests/index.spec.js +0 -24
  437. package/dist/src/db/tests/index.spec.js.map +0 -7
  438. package/dist/src/db/tests/lucene.spec.js +0 -312
  439. package/dist/src/db/tests/lucene.spec.js.map +0 -7
  440. package/dist/src/db/tests/pouch.spec.js +0 -63
  441. package/dist/src/db/tests/pouch.spec.js.map +0 -7
  442. package/dist/src/db/tests/utils.spec.js +0 -50
  443. package/dist/src/db/tests/utils.spec.js.map +0 -7
  444. package/dist/src/events/processors/posthog/tests/PosthogProcessor.spec.js +0 -154
  445. package/dist/src/events/processors/posthog/tests/PosthogProcessor.spec.js.map +0 -7
  446. package/dist/src/features/tests/featureFlags.spec.js +0 -86
  447. package/dist/src/features/tests/featureFlags.spec.js.map +0 -7
  448. package/dist/src/logging/tests/system.spec.js +0 -59
  449. package/dist/src/logging/tests/system.spec.js.map +0 -7
  450. package/dist/src/middleware/passport/sso/tests/google.spec.js +0 -77
  451. package/dist/src/middleware/passport/sso/tests/google.spec.js.map +0 -7
  452. package/dist/src/middleware/passport/sso/tests/oidc.spec.js +0 -143
  453. package/dist/src/middleware/passport/sso/tests/oidc.spec.js.map +0 -7
  454. package/dist/src/middleware/passport/sso/tests/sso.spec.js +0 -167
  455. package/dist/src/middleware/passport/sso/tests/sso.spec.js.map +0 -7
  456. package/dist/src/middleware/tests/builder.spec.js +0 -169
  457. package/dist/src/middleware/tests/builder.spec.js.map +0 -7
  458. package/dist/src/middleware/tests/matchers.spec.js +0 -120
  459. package/dist/src/middleware/tests/matchers.spec.js.map +0 -7
  460. package/dist/src/migrations/tests/migrations.spec.js +0 -78
  461. package/dist/src/migrations/tests/migrations.spec.js.map +0 -7
  462. package/dist/src/objectStore/buckets/tests/app.spec.js +0 -171
  463. package/dist/src/objectStore/buckets/tests/app.spec.js.map +0 -7
  464. package/dist/src/objectStore/buckets/tests/global.spec.js +0 -87
  465. package/dist/src/objectStore/buckets/tests/global.spec.js.map +0 -7
  466. package/dist/src/objectStore/buckets/tests/plugins.spec.js +0 -123
  467. package/dist/src/objectStore/buckets/tests/plugins.spec.js.map +0 -7
  468. package/dist/src/platform/tests/tenants.spec.js +0 -46
  469. package/dist/src/platform/tests/tenants.spec.js.map +0 -7
  470. package/dist/src/plugin/tests/validation.spec.js +0 -97
  471. package/dist/src/plugin/tests/validation.spec.js.map +0 -7
  472. package/dist/src/security/tests/encryption.spec.js +0 -51
  473. package/dist/src/security/tests/encryption.spec.js.map +0 -7
  474. package/dist/src/security/tests/permissions.spec.js +0 -154
  475. package/dist/src/security/tests/permissions.spec.js.map +0 -7
  476. package/dist/src/security/tests/sessions.spec.js +0 -33
  477. package/dist/src/security/tests/sessions.spec.js.map +0 -7
  478. package/dist/src/tenancy/tests/tenancy.spec.js +0 -154
  479. package/dist/src/tenancy/tests/tenancy.spec.js.map +0 -7
  480. package/dist/src/utils/tests/utils.spec.js +0 -165
  481. package/dist/src/utils/tests/utils.spec.js.map +0 -7
@@ -1,595 +1,668 @@
1
1
  "use strict";
2
- var __create = Object.create;
3
- var __defProp = Object.defineProperty;
4
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
- var __getOwnPropNames = Object.getOwnPropertyNames;
6
- var __getProtoOf = Object.getPrototypeOf;
7
- var __hasOwnProp = Object.prototype.hasOwnProperty;
8
- var __export = (target, all) => {
9
- for (var name in all)
10
- __defProp(target, name, { get: all[name], enumerable: true });
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
11
10
  };
12
- var __copyProps = (to, from, except, desc) => {
13
- if (from && typeof from === "object" || typeof from === "function") {
14
- for (let key of __getOwnPropNames(from))
15
- if (!__hasOwnProp.call(to, key) && key !== except)
16
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
- }
18
- return to;
11
+ var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
12
+ if (kind === "m") throw new TypeError("Private method is not writable");
13
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
14
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
15
+ return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
19
16
  };
20
- var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
- // If the importer is in node compatibility mode or this is not an ESM
22
- // file that has been converted to a CommonJS file using a Babel-
23
- // compatible transform (i.e. "__esModule" has not been set), then set
24
- // "default" to the CommonJS "module.exports" for node compatibility.
25
- isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
- mod
27
- ));
28
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
- var lucene_exports = {};
30
- __export(lucene_exports, {
31
- QueryBuilder: () => QueryBuilder,
32
- fullSearch: () => fullSearch,
33
- paginatedSearch: () => paginatedSearch,
34
- removeKeyNumbering: () => removeKeyNumbering
35
- });
36
- module.exports = __toCommonJS(lucene_exports);
37
- var import_node_fetch = __toESM(require("node-fetch"));
38
- var import_couch = require("./couch");
39
- var import_types = require("@budibase/types");
17
+ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
18
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
19
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
20
+ return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
21
+ };
22
+ var __importDefault = (this && this.__importDefault) || function (mod) {
23
+ return (mod && mod.__esModule) ? mod : { "default": mod };
24
+ };
25
+ var _QueryBuilder_instances, _QueryBuilder_dbName, _QueryBuilder_index, _QueryBuilder_query, _QueryBuilder_limit, _QueryBuilder_sort, _QueryBuilder_bookmark, _QueryBuilder_sortOrder, _QueryBuilder_sortType, _QueryBuilder_includeDocs, _QueryBuilder_version, _QueryBuilder_indexBuilder, _QueryBuilder_noEscaping, _QueryBuilder_skip, _QueryBuilder_skipItems, _QueryBuilder_execute;
26
+ Object.defineProperty(exports, "__esModule", { value: true });
27
+ exports.fullSearch = exports.paginatedSearch = exports.QueryBuilder = exports.removeKeyNumbering = void 0;
28
+ const node_fetch_1 = __importDefault(require("node-fetch"));
29
+ const couch_1 = require("./couch");
30
+ const types_1 = require("@budibase/types");
40
31
  const QUERY_START_REGEX = /\d[0-9]*:/g;
41
32
  function removeKeyNumbering(key) {
42
- if (typeof key === "string" && key.match(QUERY_START_REGEX) != null) {
43
- const parts = key.split(":");
44
- parts.shift();
45
- return parts.join(":");
46
- } else {
47
- return key;
48
- }
33
+ if (typeof key === "string" && key.match(QUERY_START_REGEX) != null) {
34
+ const parts = key.split(":");
35
+ // remove the number
36
+ parts.shift();
37
+ return parts.join(":");
38
+ }
39
+ else {
40
+ return key;
41
+ }
49
42
  }
43
+ exports.removeKeyNumbering = removeKeyNumbering;
44
+ /**
45
+ * Class to build lucene query URLs.
46
+ * Optionally takes a base lucene query object.
47
+ */
50
48
  class QueryBuilder {
51
- #dbName;
52
- #index;
53
- #query;
54
- #limit;
55
- #sort;
56
- #bookmark;
57
- #sortOrder;
58
- #sortType;
59
- #includeDocs;
60
- #version;
61
- #indexBuilder;
62
- #noEscaping = false;
63
- #skip;
64
- static {
65
- this.maxLimit = 200;
66
- }
67
- constructor(dbName, index, base) {
68
- this.#dbName = dbName;
69
- this.#index = index;
70
- this.#query = {
71
- allOr: false,
72
- onEmptyFilter: import_types.EmptyFilterOption.RETURN_ALL,
73
- string: {},
74
- fuzzy: {},
75
- range: {},
76
- equal: {},
77
- notEqual: {},
78
- empty: {},
79
- notEmpty: {},
80
- oneOf: {},
81
- contains: {},
82
- notContains: {},
83
- containsAny: {},
84
- ...base
85
- };
86
- this.#limit = 50;
87
- this.#sortOrder = "ascending";
88
- this.#sortType = "string";
89
- this.#includeDocs = true;
90
- }
91
- disableEscaping() {
92
- this.#noEscaping = true;
93
- return this;
94
- }
95
- setIndexBuilder(builderFn) {
96
- this.#indexBuilder = builderFn;
97
- return this;
98
- }
99
- setVersion(version) {
100
- if (version != null) {
101
- this.#version = version;
102
- }
103
- return this;
104
- }
105
- setTable(tableId) {
106
- this.#query.equal.tableId = tableId;
107
- return this;
108
- }
109
- setLimit(limit) {
110
- if (limit != null) {
111
- this.#limit = limit;
112
- }
113
- return this;
114
- }
115
- setSort(sort) {
116
- if (sort != null) {
117
- this.#sort = sort;
118
- }
119
- return this;
120
- }
121
- setSortOrder(sortOrder) {
122
- if (sortOrder != null) {
123
- this.#sortOrder = sortOrder;
124
- }
125
- return this;
126
- }
127
- setSortType(sortType) {
128
- if (sortType != null) {
129
- this.#sortType = sortType;
130
- }
131
- return this;
132
- }
133
- setBookmark(bookmark) {
134
- if (bookmark != null) {
135
- this.#bookmark = bookmark;
136
- }
137
- return this;
138
- }
139
- setSkip(skip) {
140
- this.#skip = skip;
141
- return this;
142
- }
143
- excludeDocs() {
144
- this.#includeDocs = false;
145
- return this;
146
- }
147
- includeDocs() {
148
- this.#includeDocs = true;
149
- return this;
150
- }
151
- addString(key, partial) {
152
- this.#query.string[key] = partial;
153
- return this;
154
- }
155
- addFuzzy(key, fuzzy) {
156
- this.#query.fuzzy[key] = fuzzy;
157
- return this;
158
- }
159
- addRange(key, low, high) {
160
- this.#query.range[key] = {
161
- low,
162
- high
163
- };
164
- return this;
165
- }
166
- addEqual(key, value) {
167
- this.#query.equal[key] = value;
168
- return this;
169
- }
170
- addNotEqual(key, value) {
171
- this.#query.notEqual[key] = value;
172
- return this;
173
- }
174
- addEmpty(key, value) {
175
- this.#query.empty[key] = value;
176
- return this;
177
- }
178
- addNotEmpty(key, value) {
179
- this.#query.notEmpty[key] = value;
180
- return this;
181
- }
182
- addOneOf(key, value) {
183
- this.#query.oneOf[key] = value;
184
- return this;
185
- }
186
- addContains(key, value) {
187
- this.#query.contains[key] = value;
188
- return this;
189
- }
190
- addNotContains(key, value) {
191
- this.#query.notContains[key] = value;
192
- return this;
193
- }
194
- addContainsAny(key, value) {
195
- this.#query.containsAny[key] = value;
196
- return this;
197
- }
198
- setAllOr() {
199
- this.#query.allOr = true;
200
- }
201
- setOnEmptyFilter(value) {
202
- this.#query.onEmptyFilter = value;
203
- }
204
- handleSpaces(input) {
205
- if (this.#noEscaping) {
206
- return input;
207
- } else {
208
- return input.replace(/ /g, "_");
209
- }
210
- }
211
- /**
212
- * Preprocesses a value before going into a lucene search.
213
- * Transforms strings to lowercase and wraps strings and bools in quotes.
214
- * @param value The value to process
215
- * @param options The preprocess options
216
- * @returns {string|*}
217
- */
218
- preprocess(value, { escape, lowercase, wrap, type } = {}) {
219
- const hasVersion = !!this.#version;
220
- const originalType = typeof value;
221
- if (value && lowercase) {
222
- value = value.toLowerCase ? value.toLowerCase() : value;
223
- }
224
- if (!this.#noEscaping && escape && originalType === "string") {
225
- value = `${value}`.replace(/[ \/#+\-&|!(){}\]^"~*?:\\]/g, "\\$&");
226
- }
227
- if (originalType === "string" && !isNaN(value) && !type) {
228
- value = `"${value}"`;
229
- } else if (hasVersion && wrap) {
230
- value = originalType === "number" ? value : `"${value}"`;
231
- }
232
- return value;
233
- }
234
- isMultiCondition() {
235
- let count = 0;
236
- for (let filters of Object.values(this.#query)) {
237
- if (typeof filters === "object") {
238
- count += Object.keys(filters).length;
239
- }
240
- }
241
- return count > 1;
242
- }
243
- compressFilters(filters) {
244
- const compressed = {};
245
- for (let key of Object.keys(filters)) {
246
- const finalKey = removeKeyNumbering(key);
247
- if (compressed[finalKey]) {
248
- compressed[finalKey] = compressed[finalKey].concat(filters[key]);
249
- } else {
250
- compressed[finalKey] = filters[key];
251
- }
252
- }
253
- const final = {};
254
- let count = 1;
255
- for (let [key, value] of Object.entries(compressed)) {
256
- final[`${count++}:${key}`] = value;
257
- }
258
- return final;
259
- }
260
- buildSearchQuery() {
261
- const builder = this;
262
- let allOr = this.#query && this.#query.allOr;
263
- let query = allOr ? "" : "*:*";
264
- let allFiltersEmpty = true;
265
- const allPreProcessingOpts = { escape: true, lowercase: true, wrap: true };
266
- let tableId = "";
267
- if (this.#query.equal.tableId) {
268
- tableId = this.#query.equal.tableId;
269
- delete this.#query.equal.tableId;
270
- }
271
- const equal = (key, value) => {
272
- if (!value && value !== 0) {
273
- return null;
274
- }
275
- return `${key}:${builder.preprocess(value, allPreProcessingOpts)}`;
276
- };
277
- const contains = (key, value, mode = "AND") => {
278
- if (!value || Array.isArray(value) && value.length === 0) {
279
- return null;
280
- }
281
- if (!Array.isArray(value)) {
282
- return `${key}:${value}`;
283
- }
284
- let statement = `${builder.preprocess(value[0], { escape: true })}`;
285
- for (let i = 1; i < value.length; i++) {
286
- statement += ` ${mode} ${builder.preprocess(value[i], {
287
- escape: true
288
- })}`;
289
- }
290
- return `${key}:(${statement})`;
291
- };
292
- const fuzzy = (key, value) => {
293
- if (!value) {
294
- return null;
295
- }
296
- value = builder.preprocess(value, {
297
- escape: true,
298
- lowercase: true,
299
- type: "fuzzy"
300
- });
301
- return `${key}:/.*${value}.*/`;
302
- };
303
- const notContains = (key, value) => {
304
- const allPrefix = allOr ? "*:* AND " : "";
305
- const mode = allOr ? "AND" : void 0;
306
- return allPrefix + "NOT " + contains(key, value, mode);
307
- };
308
- const containsAny = (key, value) => {
309
- return contains(key, value, "OR");
310
- };
311
- const oneOf = (key, value) => {
312
- if (!value) {
313
- return `*:*`;
314
- }
315
- if (!Array.isArray(value)) {
316
- if (typeof value === "string") {
317
- value = value.split(",");
318
- } else {
319
- return "";
320
- }
321
- }
322
- let orStatement = `${builder.preprocess(value[0], allPreProcessingOpts)}`;
323
- for (let i = 1; i < value.length; i++) {
324
- orStatement += ` OR ${builder.preprocess(
325
- value[i],
326
- allPreProcessingOpts
327
- )}`;
328
- }
329
- return `${key}:(${orStatement})`;
330
- };
331
- function build(structure, queryFn, opts) {
332
- let built = "";
333
- for (let [key, value] of Object.entries(structure)) {
334
- key = removeKeyNumbering(key);
335
- key = builder.preprocess(builder.handleSpaces(key), {
336
- escape: true
337
- });
338
- let expression = queryFn(key, value);
339
- if (expression == null) {
340
- continue;
341
- }
342
- if (built.length > 0 || query.length > 0) {
343
- const mode = opts?.mode ? opts.mode : allOr ? "OR" : "AND";
344
- built += ` ${mode} `;
345
- }
346
- built += expression;
347
- if (typeof value !== "string" && value != null || typeof value === "string" && value !== tableId && value !== "") {
348
- allFiltersEmpty = false;
349
- }
350
- }
351
- if (opts?.returnBuilt) {
352
- return built;
353
- } else {
354
- query += built;
355
- }
356
- }
357
- if (this.#query.string) {
358
- build(this.#query.string, (key, value) => {
359
- if (!value) {
360
- return null;
361
- }
362
- value = builder.preprocess(value, {
363
- escape: true,
364
- lowercase: true,
365
- type: "string"
49
+ constructor(dbName, index, base) {
50
+ _QueryBuilder_instances.add(this);
51
+ _QueryBuilder_dbName.set(this, void 0);
52
+ _QueryBuilder_index.set(this, void 0);
53
+ _QueryBuilder_query.set(this, void 0);
54
+ _QueryBuilder_limit.set(this, void 0);
55
+ _QueryBuilder_sort.set(this, void 0);
56
+ _QueryBuilder_bookmark.set(this, void 0);
57
+ _QueryBuilder_sortOrder.set(this, void 0);
58
+ _QueryBuilder_sortType.set(this, void 0);
59
+ _QueryBuilder_includeDocs.set(this, void 0);
60
+ _QueryBuilder_version.set(this, void 0);
61
+ _QueryBuilder_indexBuilder.set(this, void 0);
62
+ _QueryBuilder_noEscaping.set(this, false);
63
+ _QueryBuilder_skip.set(this, void 0);
64
+ __classPrivateFieldSet(this, _QueryBuilder_dbName, dbName, "f");
65
+ __classPrivateFieldSet(this, _QueryBuilder_index, index, "f");
66
+ __classPrivateFieldSet(this, _QueryBuilder_query, Object.assign({ allOr: false, onEmptyFilter: types_1.EmptyFilterOption.RETURN_ALL, string: {}, fuzzy: {}, range: {}, equal: {}, notEqual: {}, empty: {}, notEmpty: {}, oneOf: {}, contains: {}, notContains: {}, containsAny: {} }, base), "f");
67
+ __classPrivateFieldSet(this, _QueryBuilder_limit, 50, "f");
68
+ __classPrivateFieldSet(this, _QueryBuilder_sortOrder, "ascending", "f");
69
+ __classPrivateFieldSet(this, _QueryBuilder_sortType, "string", "f");
70
+ __classPrivateFieldSet(this, _QueryBuilder_includeDocs, true, "f");
71
+ }
72
+ disableEscaping() {
73
+ __classPrivateFieldSet(this, _QueryBuilder_noEscaping, true, "f");
74
+ return this;
75
+ }
76
+ setIndexBuilder(builderFn) {
77
+ __classPrivateFieldSet(this, _QueryBuilder_indexBuilder, builderFn, "f");
78
+ return this;
79
+ }
80
+ setVersion(version) {
81
+ if (version != null) {
82
+ __classPrivateFieldSet(this, _QueryBuilder_version, version, "f");
83
+ }
84
+ return this;
85
+ }
86
+ setTable(tableId) {
87
+ __classPrivateFieldGet(this, _QueryBuilder_query, "f").equal.tableId = tableId;
88
+ return this;
89
+ }
90
+ setLimit(limit) {
91
+ if (limit != null) {
92
+ __classPrivateFieldSet(this, _QueryBuilder_limit, limit, "f");
93
+ }
94
+ return this;
95
+ }
96
+ setSort(sort) {
97
+ if (sort != null) {
98
+ __classPrivateFieldSet(this, _QueryBuilder_sort, sort, "f");
99
+ }
100
+ return this;
101
+ }
102
+ setSortOrder(sortOrder) {
103
+ if (sortOrder != null) {
104
+ __classPrivateFieldSet(this, _QueryBuilder_sortOrder, sortOrder, "f");
105
+ }
106
+ return this;
107
+ }
108
+ setSortType(sortType) {
109
+ if (sortType != null) {
110
+ __classPrivateFieldSet(this, _QueryBuilder_sortType, sortType, "f");
111
+ }
112
+ return this;
113
+ }
114
+ setBookmark(bookmark) {
115
+ if (bookmark != null) {
116
+ __classPrivateFieldSet(this, _QueryBuilder_bookmark, bookmark, "f");
117
+ }
118
+ return this;
119
+ }
120
+ setSkip(skip) {
121
+ __classPrivateFieldSet(this, _QueryBuilder_skip, skip, "f");
122
+ return this;
123
+ }
124
+ excludeDocs() {
125
+ __classPrivateFieldSet(this, _QueryBuilder_includeDocs, false, "f");
126
+ return this;
127
+ }
128
+ includeDocs() {
129
+ __classPrivateFieldSet(this, _QueryBuilder_includeDocs, true, "f");
130
+ return this;
131
+ }
132
+ addString(key, partial) {
133
+ __classPrivateFieldGet(this, _QueryBuilder_query, "f").string[key] = partial;
134
+ return this;
135
+ }
136
+ addFuzzy(key, fuzzy) {
137
+ __classPrivateFieldGet(this, _QueryBuilder_query, "f").fuzzy[key] = fuzzy;
138
+ return this;
139
+ }
140
+ addRange(key, low, high) {
141
+ __classPrivateFieldGet(this, _QueryBuilder_query, "f").range[key] = {
142
+ low,
143
+ high,
144
+ };
145
+ return this;
146
+ }
147
+ addEqual(key, value) {
148
+ __classPrivateFieldGet(this, _QueryBuilder_query, "f").equal[key] = value;
149
+ return this;
150
+ }
151
+ addNotEqual(key, value) {
152
+ __classPrivateFieldGet(this, _QueryBuilder_query, "f").notEqual[key] = value;
153
+ return this;
154
+ }
155
+ addEmpty(key, value) {
156
+ __classPrivateFieldGet(this, _QueryBuilder_query, "f").empty[key] = value;
157
+ return this;
158
+ }
159
+ addNotEmpty(key, value) {
160
+ __classPrivateFieldGet(this, _QueryBuilder_query, "f").notEmpty[key] = value;
161
+ return this;
162
+ }
163
+ addOneOf(key, value) {
164
+ __classPrivateFieldGet(this, _QueryBuilder_query, "f").oneOf[key] = value;
165
+ return this;
166
+ }
167
+ addContains(key, value) {
168
+ __classPrivateFieldGet(this, _QueryBuilder_query, "f").contains[key] = value;
169
+ return this;
170
+ }
171
+ addNotContains(key, value) {
172
+ __classPrivateFieldGet(this, _QueryBuilder_query, "f").notContains[key] = value;
173
+ return this;
174
+ }
175
+ addContainsAny(key, value) {
176
+ __classPrivateFieldGet(this, _QueryBuilder_query, "f").containsAny[key] = value;
177
+ return this;
178
+ }
179
+ setAllOr() {
180
+ __classPrivateFieldGet(this, _QueryBuilder_query, "f").allOr = true;
181
+ }
182
+ setOnEmptyFilter(value) {
183
+ __classPrivateFieldGet(this, _QueryBuilder_query, "f").onEmptyFilter = value;
184
+ }
185
+ handleSpaces(input) {
186
+ if (__classPrivateFieldGet(this, _QueryBuilder_noEscaping, "f")) {
187
+ return input;
188
+ }
189
+ else {
190
+ return input.replace(/ /g, "_");
191
+ }
192
+ }
193
+ /**
194
+ * Preprocesses a value before going into a lucene search.
195
+ * Transforms strings to lowercase and wraps strings and bools in quotes.
196
+ * @param value The value to process
197
+ * @param options The preprocess options
198
+ * @returns {string|*}
199
+ */
200
+ preprocess(value, { escape, lowercase, wrap, type } = {}) {
201
+ const hasVersion = !!__classPrivateFieldGet(this, _QueryBuilder_version, "f");
202
+ // Determine if type needs wrapped
203
+ const originalType = typeof value;
204
+ // Convert to lowercase
205
+ if (value && lowercase) {
206
+ value = value.toLowerCase ? value.toLowerCase() : value;
207
+ }
208
+ // Escape characters
209
+ if (!__classPrivateFieldGet(this, _QueryBuilder_noEscaping, "f") && escape && originalType === "string") {
210
+ value = `${value}`.replace(/[ \/#+\-&|!(){}\]^"~*?:\\]/g, "\\$&");
211
+ }
212
+ // Wrap in quotes
213
+ if (originalType === "string" && !isNaN(value) && !type) {
214
+ value = `"${value}"`;
215
+ }
216
+ else if (hasVersion && wrap) {
217
+ value = originalType === "number" ? value : `"${value}"`;
218
+ }
219
+ return value;
220
+ }
221
+ isMultiCondition() {
222
+ let count = 0;
223
+ for (let filters of Object.values(__classPrivateFieldGet(this, _QueryBuilder_query, "f"))) {
224
+ // not contains is one massive filter in allOr mode
225
+ if (typeof filters === "object") {
226
+ count += Object.keys(filters).length;
227
+ }
228
+ }
229
+ return count > 1;
230
+ }
231
+ compressFilters(filters) {
232
+ const compressed = {};
233
+ for (let key of Object.keys(filters)) {
234
+ const finalKey = removeKeyNumbering(key);
235
+ if (compressed[finalKey]) {
236
+ compressed[finalKey] = compressed[finalKey].concat(filters[key]);
237
+ }
238
+ else {
239
+ compressed[finalKey] = filters[key];
240
+ }
241
+ }
242
+ // add prefixes back
243
+ const final = {};
244
+ let count = 1;
245
+ for (let [key, value] of Object.entries(compressed)) {
246
+ final[`${count++}:${key}`] = value;
247
+ }
248
+ return final;
249
+ }
250
+ buildSearchQuery() {
251
+ var _a;
252
+ const builder = this;
253
+ let allOr = __classPrivateFieldGet(this, _QueryBuilder_query, "f") && __classPrivateFieldGet(this, _QueryBuilder_query, "f").allOr;
254
+ let query = allOr ? "" : "*:*";
255
+ let allFiltersEmpty = true;
256
+ const allPreProcessingOpts = { escape: true, lowercase: true, wrap: true };
257
+ let tableId = "";
258
+ if (__classPrivateFieldGet(this, _QueryBuilder_query, "f").equal.tableId) {
259
+ tableId = __classPrivateFieldGet(this, _QueryBuilder_query, "f").equal.tableId;
260
+ delete __classPrivateFieldGet(this, _QueryBuilder_query, "f").equal.tableId;
261
+ }
262
+ const equal = (key, value) => {
263
+ // 0 evaluates to false, which means we would return all rows if we don't check it
264
+ if (!value && value !== 0) {
265
+ return null;
266
+ }
267
+ return `${key}:${builder.preprocess(value, allPreProcessingOpts)}`;
268
+ };
269
+ const contains = (key, value, mode = "AND") => {
270
+ if (!value || (Array.isArray(value) && value.length === 0)) {
271
+ return null;
272
+ }
273
+ if (!Array.isArray(value)) {
274
+ return `${key}:${value}`;
275
+ }
276
+ let statement = `${builder.preprocess(value[0], { escape: true })}`;
277
+ for (let i = 1; i < value.length; i++) {
278
+ statement += ` ${mode} ${builder.preprocess(value[i], {
279
+ escape: true,
280
+ })}`;
281
+ }
282
+ return `${key}:(${statement})`;
283
+ };
284
+ const fuzzy = (key, value) => {
285
+ if (!value) {
286
+ return null;
287
+ }
288
+ value = builder.preprocess(value, {
289
+ escape: true,
290
+ lowercase: true,
291
+ type: "fuzzy",
292
+ });
293
+ return `${key}:/.*${value}.*/`;
294
+ };
295
+ const notContains = (key, value) => {
296
+ const allPrefix = allOr ? "*:* AND " : "";
297
+ const mode = allOr ? "AND" : undefined;
298
+ return allPrefix + "NOT " + contains(key, value, mode);
299
+ };
300
+ const containsAny = (key, value) => {
301
+ return contains(key, value, "OR");
302
+ };
303
+ const oneOf = (key, value) => {
304
+ if (!value) {
305
+ return `*:*`;
306
+ }
307
+ if (!Array.isArray(value)) {
308
+ if (typeof value === "string") {
309
+ value = value.split(",");
310
+ }
311
+ else {
312
+ return "";
313
+ }
314
+ }
315
+ let orStatement = `${builder.preprocess(value[0], allPreProcessingOpts)}`;
316
+ for (let i = 1; i < value.length; i++) {
317
+ orStatement += ` OR ${builder.preprocess(value[i], allPreProcessingOpts)}`;
318
+ }
319
+ return `${key}:(${orStatement})`;
320
+ };
321
+ function build(structure, queryFn, opts) {
322
+ let built = "";
323
+ for (let [key, value] of Object.entries(structure)) {
324
+ // check for new format - remove numbering if needed
325
+ key = removeKeyNumbering(key);
326
+ key = builder.preprocess(builder.handleSpaces(key), {
327
+ escape: true,
328
+ });
329
+ let expression = queryFn(key, value);
330
+ if (expression == null) {
331
+ continue;
332
+ }
333
+ if (built.length > 0 || query.length > 0) {
334
+ const mode = (opts === null || opts === void 0 ? void 0 : opts.mode) ? opts.mode : allOr ? "OR" : "AND";
335
+ built += ` ${mode} `;
336
+ }
337
+ built += expression;
338
+ if ((typeof value !== "string" && value != null) ||
339
+ (typeof value === "string" && value !== tableId && value !== "")) {
340
+ allFiltersEmpty = false;
341
+ }
342
+ }
343
+ if (opts === null || opts === void 0 ? void 0 : opts.returnBuilt) {
344
+ return built;
345
+ }
346
+ else {
347
+ query += built;
348
+ }
349
+ }
350
+ // Construct the actual lucene search query string from JSON structure
351
+ if (__classPrivateFieldGet(this, _QueryBuilder_query, "f").string) {
352
+ build(__classPrivateFieldGet(this, _QueryBuilder_query, "f").string, (key, value) => {
353
+ if (!value) {
354
+ return null;
355
+ }
356
+ value = builder.preprocess(value, {
357
+ escape: true,
358
+ lowercase: true,
359
+ type: "string",
360
+ });
361
+ return `${key}:${value}*`;
362
+ });
363
+ }
364
+ if (__classPrivateFieldGet(this, _QueryBuilder_query, "f").range) {
365
+ build(__classPrivateFieldGet(this, _QueryBuilder_query, "f").range, (key, value) => {
366
+ if (!value) {
367
+ return null;
368
+ }
369
+ if (value.low == null || value.low === "") {
370
+ return null;
371
+ }
372
+ if (value.high == null || value.high === "") {
373
+ return null;
374
+ }
375
+ const low = builder.preprocess(value.low, allPreProcessingOpts);
376
+ const high = builder.preprocess(value.high, allPreProcessingOpts);
377
+ return `${key}:[${low} TO ${high}]`;
378
+ });
379
+ }
380
+ if (__classPrivateFieldGet(this, _QueryBuilder_query, "f").fuzzy) {
381
+ build(__classPrivateFieldGet(this, _QueryBuilder_query, "f").fuzzy, fuzzy);
382
+ }
383
+ if (__classPrivateFieldGet(this, _QueryBuilder_query, "f").equal) {
384
+ build(__classPrivateFieldGet(this, _QueryBuilder_query, "f").equal, equal);
385
+ }
386
+ if (__classPrivateFieldGet(this, _QueryBuilder_query, "f").notEqual) {
387
+ build(__classPrivateFieldGet(this, _QueryBuilder_query, "f").notEqual, (key, value) => {
388
+ if (!value) {
389
+ return null;
390
+ }
391
+ if (typeof value === "boolean") {
392
+ return `(*:* AND !${key}:${value})`;
393
+ }
394
+ return `!${key}:${builder.preprocess(value, allPreProcessingOpts)}`;
395
+ });
396
+ }
397
+ if (__classPrivateFieldGet(this, _QueryBuilder_query, "f").empty) {
398
+ build(__classPrivateFieldGet(this, _QueryBuilder_query, "f").empty, (key) => `(*:* -${key}:["" TO *])`);
399
+ }
400
+ if (__classPrivateFieldGet(this, _QueryBuilder_query, "f").notEmpty) {
401
+ build(__classPrivateFieldGet(this, _QueryBuilder_query, "f").notEmpty, (key) => `${key}:["" TO *]`);
402
+ }
403
+ if (__classPrivateFieldGet(this, _QueryBuilder_query, "f").oneOf) {
404
+ build(__classPrivateFieldGet(this, _QueryBuilder_query, "f").oneOf, oneOf);
405
+ }
406
+ if (__classPrivateFieldGet(this, _QueryBuilder_query, "f").contains) {
407
+ build(__classPrivateFieldGet(this, _QueryBuilder_query, "f").contains, contains);
408
+ }
409
+ if (__classPrivateFieldGet(this, _QueryBuilder_query, "f").notContains) {
410
+ build(this.compressFilters(__classPrivateFieldGet(this, _QueryBuilder_query, "f").notContains), notContains);
411
+ }
412
+ if (__classPrivateFieldGet(this, _QueryBuilder_query, "f").containsAny) {
413
+ build(__classPrivateFieldGet(this, _QueryBuilder_query, "f").containsAny, containsAny);
414
+ }
415
+ // make sure table ID is always added as an AND
416
+ if (tableId) {
417
+ query = this.isMultiCondition() ? `(${query})` : query;
418
+ allOr = false;
419
+ build({ tableId }, equal);
420
+ }
421
+ if (allFiltersEmpty) {
422
+ if (__classPrivateFieldGet(this, _QueryBuilder_query, "f").onEmptyFilter === types_1.EmptyFilterOption.RETURN_NONE) {
423
+ return "";
424
+ }
425
+ else if ((_a = __classPrivateFieldGet(this, _QueryBuilder_query, "f")) === null || _a === void 0 ? void 0 : _a.allOr) {
426
+ return query.replace("()", "(*:*)");
427
+ }
428
+ }
429
+ return query;
430
+ }
431
+ buildSearchBody() {
432
+ let body = {
433
+ q: this.buildSearchQuery(),
434
+ limit: Math.min(__classPrivateFieldGet(this, _QueryBuilder_limit, "f"), QueryBuilder.maxLimit),
435
+ include_docs: __classPrivateFieldGet(this, _QueryBuilder_includeDocs, "f"),
436
+ };
437
+ if (__classPrivateFieldGet(this, _QueryBuilder_bookmark, "f")) {
438
+ body.bookmark = __classPrivateFieldGet(this, _QueryBuilder_bookmark, "f");
439
+ }
440
+ if (__classPrivateFieldGet(this, _QueryBuilder_sort, "f")) {
441
+ const order = __classPrivateFieldGet(this, _QueryBuilder_sortOrder, "f") === "descending" ? "-" : "";
442
+ const type = `<${__classPrivateFieldGet(this, _QueryBuilder_sortType, "f")}>`;
443
+ body.sort = `${order}${this.handleSpaces(__classPrivateFieldGet(this, _QueryBuilder_sort, "f"))}${type}`;
444
+ }
445
+ return body;
446
+ }
447
+ run() {
448
+ return __awaiter(this, void 0, void 0, function* () {
449
+ if (__classPrivateFieldGet(this, _QueryBuilder_skip, "f")) {
450
+ yield __classPrivateFieldGet(this, _QueryBuilder_instances, "m", _QueryBuilder_skipItems).call(this, __classPrivateFieldGet(this, _QueryBuilder_skip, "f"));
451
+ }
452
+ return yield __classPrivateFieldGet(this, _QueryBuilder_instances, "m", _QueryBuilder_execute).call(this);
366
453
  });
367
- return `${key}:${value}*`;
368
- });
369
- }
370
- if (this.#query.range) {
371
- build(this.#query.range, (key, value) => {
372
- if (!value) {
373
- return null;
374
- }
375
- if (value.low == null || value.low === "") {
376
- return null;
377
- }
378
- if (value.high == null || value.high === "") {
379
- return null;
380
- }
381
- const low = builder.preprocess(value.low, allPreProcessingOpts);
382
- const high = builder.preprocess(value.high, allPreProcessingOpts);
383
- return `${key}:[${low} TO ${high}]`;
384
- });
385
- }
386
- if (this.#query.fuzzy) {
387
- build(this.#query.fuzzy, fuzzy);
388
- }
389
- if (this.#query.equal) {
390
- build(this.#query.equal, equal);
391
- }
392
- if (this.#query.notEqual) {
393
- build(this.#query.notEqual, (key, value) => {
394
- if (!value) {
395
- return null;
396
- }
397
- if (typeof value === "boolean") {
398
- return `(*:* AND !${key}:${value})`;
399
- }
400
- return `!${key}:${builder.preprocess(value, allPreProcessingOpts)}`;
401
- });
402
- }
403
- if (this.#query.empty) {
404
- build(this.#query.empty, (key) => `(*:* -${key}:["" TO *])`);
405
- }
406
- if (this.#query.notEmpty) {
407
- build(this.#query.notEmpty, (key) => `${key}:["" TO *]`);
408
- }
409
- if (this.#query.oneOf) {
410
- build(this.#query.oneOf, oneOf);
411
- }
412
- if (this.#query.contains) {
413
- build(this.#query.contains, contains);
414
- }
415
- if (this.#query.notContains) {
416
- build(this.compressFilters(this.#query.notContains), notContains);
417
- }
418
- if (this.#query.containsAny) {
419
- build(this.#query.containsAny, containsAny);
420
- }
421
- if (tableId) {
422
- query = this.isMultiCondition() ? `(${query})` : query;
423
- allOr = false;
424
- build({ tableId }, equal);
425
- }
426
- if (allFiltersEmpty) {
427
- if (this.#query.onEmptyFilter === import_types.EmptyFilterOption.RETURN_NONE) {
428
- return "";
429
- } else if (this.#query?.allOr) {
430
- return query.replace("()", "(*:*)");
431
- }
432
- }
433
- return query;
434
- }
435
- buildSearchBody() {
436
- let body = {
437
- q: this.buildSearchQuery(),
438
- limit: Math.min(this.#limit, QueryBuilder.maxLimit),
439
- include_docs: this.#includeDocs
440
- };
441
- if (this.#bookmark) {
442
- body.bookmark = this.#bookmark;
443
- }
444
- if (this.#sort) {
445
- const order = this.#sortOrder === "descending" ? "-" : "";
446
- const type = `<${this.#sortType}>`;
447
- body.sort = `${order}${this.handleSpaces(this.#sort)}${type}`;
448
- }
449
- return body;
450
- }
451
- async run() {
452
- if (this.#skip) {
453
- await this.#skipItems(this.#skip);
454
- }
455
- return await this.#execute();
456
- }
457
- /**
458
- * Lucene queries do not support pagination and use bookmarks instead.
459
- * For the given builder, walk through pages using bookmarks until the desired
460
- * page has been met.
461
- */
462
- async #skipItems(skip) {
463
- const prevIncludeDocs = this.#includeDocs;
464
- const prevLimit = this.#limit;
465
- this.excludeDocs();
466
- let skipRemaining = skip;
467
- let iterationFetched = 0;
468
- do {
469
- const toSkip = Math.min(QueryBuilder.maxLimit, skipRemaining);
470
- this.setLimit(toSkip);
471
- const { bookmark, rows } = await this.#execute();
472
- this.setBookmark(bookmark);
473
- iterationFetched = rows.length;
474
- skipRemaining -= rows.length;
475
- } while (skipRemaining > 0 && iterationFetched > 0);
476
- this.#includeDocs = prevIncludeDocs;
477
- this.#limit = prevLimit;
478
- }
479
- async #execute() {
480
- const { url, cookie } = (0, import_couch.getCouchInfo)();
481
- const fullPath = `${url}/${this.#dbName}/_design/database/_search/${this.#index}`;
482
- const body = this.buildSearchBody();
483
- try {
484
- return await runQuery(fullPath, body, cookie);
485
- } catch (err) {
486
- if (err.status === 404 && this.#indexBuilder) {
487
- await this.#indexBuilder();
488
- return await runQuery(fullPath, body, cookie);
489
- } else {
490
- throw err;
491
- }
492
- }
493
- }
454
+ }
494
455
  }
495
- async function runQuery(url, body, cookie) {
496
- const response = await (0, import_node_fetch.default)(url, {
497
- body: JSON.stringify(body),
498
- method: "POST",
499
- headers: {
500
- Authorization: cookie
501
- }
502
- });
503
- if (response.status === 404) {
504
- throw response;
505
- }
506
- const json = await response.json();
507
- let output = {
508
- rows: [],
509
- totalRows: 0
510
- };
511
- if (json.rows != null && json.rows.length > 0) {
512
- output.rows = json.rows.map((row) => row.doc);
513
- }
514
- if (json.bookmark) {
515
- output.bookmark = json.bookmark;
516
- }
517
- if (json.total_rows) {
518
- output.totalRows = json.total_rows;
519
- }
520
- return output;
456
+ exports.QueryBuilder = QueryBuilder;
457
+ _QueryBuilder_dbName = new WeakMap(), _QueryBuilder_index = new WeakMap(), _QueryBuilder_query = new WeakMap(), _QueryBuilder_limit = new WeakMap(), _QueryBuilder_sort = new WeakMap(), _QueryBuilder_bookmark = new WeakMap(), _QueryBuilder_sortOrder = new WeakMap(), _QueryBuilder_sortType = new WeakMap(), _QueryBuilder_includeDocs = new WeakMap(), _QueryBuilder_version = new WeakMap(), _QueryBuilder_indexBuilder = new WeakMap(), _QueryBuilder_noEscaping = new WeakMap(), _QueryBuilder_skip = new WeakMap(), _QueryBuilder_instances = new WeakSet(), _QueryBuilder_skipItems = function _QueryBuilder_skipItems(skip) {
458
+ return __awaiter(this, void 0, void 0, function* () {
459
+ // Lucene does not support pagination.
460
+ // Handle pagination by finding the right bookmark
461
+ const prevIncludeDocs = __classPrivateFieldGet(this, _QueryBuilder_includeDocs, "f");
462
+ const prevLimit = __classPrivateFieldGet(this, _QueryBuilder_limit, "f");
463
+ this.excludeDocs();
464
+ let skipRemaining = skip;
465
+ let iterationFetched = 0;
466
+ do {
467
+ const toSkip = Math.min(QueryBuilder.maxLimit, skipRemaining);
468
+ this.setLimit(toSkip);
469
+ const { bookmark, rows } = yield __classPrivateFieldGet(this, _QueryBuilder_instances, "m", _QueryBuilder_execute).call(this);
470
+ this.setBookmark(bookmark);
471
+ iterationFetched = rows.length;
472
+ skipRemaining -= rows.length;
473
+ } while (skipRemaining > 0 && iterationFetched > 0);
474
+ __classPrivateFieldSet(this, _QueryBuilder_includeDocs, prevIncludeDocs, "f");
475
+ __classPrivateFieldSet(this, _QueryBuilder_limit, prevLimit, "f");
476
+ });
477
+ }, _QueryBuilder_execute = function _QueryBuilder_execute() {
478
+ return __awaiter(this, void 0, void 0, function* () {
479
+ const { url, cookie } = (0, couch_1.getCouchInfo)();
480
+ const fullPath = `${url}/${__classPrivateFieldGet(this, _QueryBuilder_dbName, "f")}/_design/database/_search/${__classPrivateFieldGet(this, _QueryBuilder_index, "f")}`;
481
+ const body = this.buildSearchBody();
482
+ try {
483
+ return yield runQuery(fullPath, body, cookie);
484
+ }
485
+ catch (err) {
486
+ if (err.status === 404 && __classPrivateFieldGet(this, _QueryBuilder_indexBuilder, "f")) {
487
+ yield __classPrivateFieldGet(this, _QueryBuilder_indexBuilder, "f").call(this);
488
+ return yield runQuery(fullPath, body, cookie);
489
+ }
490
+ else {
491
+ throw err;
492
+ }
493
+ }
494
+ });
495
+ };
496
+ QueryBuilder.maxLimit = 200;
497
+ /**
498
+ * Executes a lucene search query.
499
+ * @param url The query URL
500
+ * @param body The request body defining search criteria
501
+ * @param cookie The auth cookie for CouchDB
502
+ * @returns {Promise<{rows: []}>}
503
+ */
504
+ function runQuery(url, body, cookie) {
505
+ return __awaiter(this, void 0, void 0, function* () {
506
+ const response = yield (0, node_fetch_1.default)(url, {
507
+ body: JSON.stringify(body),
508
+ method: "POST",
509
+ headers: {
510
+ Authorization: cookie,
511
+ },
512
+ });
513
+ if (response.status === 404) {
514
+ throw response;
515
+ }
516
+ const json = yield response.json();
517
+ let output = {
518
+ rows: [],
519
+ totalRows: 0,
520
+ };
521
+ if (json.rows != null && json.rows.length > 0) {
522
+ output.rows = json.rows.map((row) => row.doc);
523
+ }
524
+ if (json.bookmark) {
525
+ output.bookmark = json.bookmark;
526
+ }
527
+ if (json.total_rows) {
528
+ output.totalRows = json.total_rows;
529
+ }
530
+ return output;
531
+ });
521
532
  }
522
- async function recursiveSearch(dbName, index, query, params) {
523
- const bookmark = params.bookmark;
524
- const rows = params.rows || [];
525
- if (rows.length >= params.limit) {
526
- return rows;
527
- }
528
- let pageSize = QueryBuilder.maxLimit;
529
- if (rows.length > params.limit - QueryBuilder.maxLimit) {
530
- pageSize = params.limit - rows.length;
531
- }
532
- const page = await new QueryBuilder(dbName, index, query).setVersion(params.version).setTable(params.tableId).setBookmark(bookmark).setLimit(pageSize).setSort(params.sort).setSortOrder(params.sortOrder).setSortType(params.sortType).run();
533
- if (!page.rows.length) {
534
- return rows;
535
- }
536
- if (page.rows.length < QueryBuilder.maxLimit) {
537
- return [...rows, ...page.rows];
538
- }
539
- const newParams = {
540
- ...params,
541
- bookmark: page.bookmark,
542
- rows: [...rows, ...page.rows]
543
- };
544
- return await recursiveSearch(dbName, index, query, newParams);
533
+ /**
534
+ * Gets round the fixed limit of 200 results from a query by fetching as many
535
+ * pages as required and concatenating the results. This recursively operates
536
+ * until enough results have been found.
537
+ * @param dbName {string} Which database to run a lucene query on
538
+ * @param index {string} Which search index to utilise
539
+ * @param query {object} The JSON query structure
540
+ * @param params {object} The search params including:
541
+ * tableId {string} The table ID to search
542
+ * sort {string} The sort column
543
+ * sortOrder {string} The sort order ("ascending" or "descending")
544
+ * sortType {string} Whether to treat sortable values as strings or
545
+ * numbers. ("string" or "number")
546
+ * limit {number} The number of results to fetch
547
+ * bookmark {string|null} Current bookmark in the recursive search
548
+ * rows {array|null} Current results in the recursive search
549
+ * @returns {Promise<*[]|*>}
550
+ */
551
+ function recursiveSearch(dbName, index, query, params) {
552
+ return __awaiter(this, void 0, void 0, function* () {
553
+ const bookmark = params.bookmark;
554
+ const rows = params.rows || [];
555
+ if (rows.length >= params.limit) {
556
+ return rows;
557
+ }
558
+ let pageSize = QueryBuilder.maxLimit;
559
+ if (rows.length > params.limit - QueryBuilder.maxLimit) {
560
+ pageSize = params.limit - rows.length;
561
+ }
562
+ const page = yield new QueryBuilder(dbName, index, query)
563
+ .setVersion(params.version)
564
+ .setTable(params.tableId)
565
+ .setBookmark(bookmark)
566
+ .setLimit(pageSize)
567
+ .setSort(params.sort)
568
+ .setSortOrder(params.sortOrder)
569
+ .setSortType(params.sortType)
570
+ .run();
571
+ if (!page.rows.length) {
572
+ return rows;
573
+ }
574
+ if (page.rows.length < QueryBuilder.maxLimit) {
575
+ return [...rows, ...page.rows];
576
+ }
577
+ const newParams = Object.assign(Object.assign({}, params), { bookmark: page.bookmark, rows: [...rows, ...page.rows] });
578
+ return yield recursiveSearch(dbName, index, query, newParams);
579
+ });
545
580
  }
546
- async function paginatedSearch(dbName, index, query, params) {
547
- let limit = params.limit;
548
- if (limit == null || isNaN(limit) || limit < 0) {
549
- limit = 50;
550
- }
551
- limit = Math.min(limit, QueryBuilder.maxLimit);
552
- const search = new QueryBuilder(dbName, index, query);
553
- if (params.version) {
554
- search.setVersion(params.version);
555
- }
556
- if (params.tableId) {
557
- search.setTable(params.tableId);
558
- }
559
- if (params.sort) {
560
- search.setSort(params.sort).setSortOrder(params.sortOrder).setSortType(params.sortType);
561
- }
562
- if (params.indexer) {
563
- search.setIndexBuilder(params.indexer);
564
- }
565
- if (params.disableEscaping) {
566
- search.disableEscaping();
567
- }
568
- const searchResults = await search.setBookmark(params.bookmark).setLimit(limit).run();
569
- search.setBookmark(searchResults.bookmark).setLimit(1);
570
- if (params.tableId) {
571
- search.setTable(params.tableId);
572
- }
573
- const nextResults = await search.run();
574
- return {
575
- ...searchResults,
576
- hasNextPage: nextResults.rows && nextResults.rows.length > 0
577
- };
581
+ /**
582
+ * Performs a paginated search. A bookmark will be returned to allow the next
583
+ * page to be fetched. There is a max limit off 200 results per page in a
584
+ * paginated search.
585
+ * @param dbName {string} Which database to run a lucene query on
586
+ * @param index {string} Which search index to utilise
587
+ * @param query {object} The JSON query structure
588
+ * @param params {object} The search params including:
589
+ * tableId {string} The table ID to search
590
+ * sort {string} The sort column
591
+ * sortOrder {string} The sort order ("ascending" or "descending")
592
+ * sortType {string} Whether to treat sortable values as strings or
593
+ * numbers. ("string" or "number")
594
+ * limit {number} The desired page size
595
+ * bookmark {string} The bookmark to resume from
596
+ * @returns {Promise<{hasNextPage: boolean, rows: *[]}>}
597
+ */
598
+ function paginatedSearch(dbName, index, query, params) {
599
+ return __awaiter(this, void 0, void 0, function* () {
600
+ let limit = params.limit;
601
+ if (limit == null || isNaN(limit) || limit < 0) {
602
+ limit = 50;
603
+ }
604
+ limit = Math.min(limit, QueryBuilder.maxLimit);
605
+ const search = new QueryBuilder(dbName, index, query);
606
+ if (params.version) {
607
+ search.setVersion(params.version);
608
+ }
609
+ if (params.tableId) {
610
+ search.setTable(params.tableId);
611
+ }
612
+ if (params.sort) {
613
+ search
614
+ .setSort(params.sort)
615
+ .setSortOrder(params.sortOrder)
616
+ .setSortType(params.sortType);
617
+ }
618
+ if (params.indexer) {
619
+ search.setIndexBuilder(params.indexer);
620
+ }
621
+ if (params.disableEscaping) {
622
+ search.disableEscaping();
623
+ }
624
+ const searchResults = yield search
625
+ .setBookmark(params.bookmark)
626
+ .setLimit(limit)
627
+ .run();
628
+ // Try fetching 1 row in the next page to see if another page of results
629
+ // exists or not
630
+ search.setBookmark(searchResults.bookmark).setLimit(1);
631
+ if (params.tableId) {
632
+ search.setTable(params.tableId);
633
+ }
634
+ const nextResults = yield search.run();
635
+ return Object.assign(Object.assign({}, searchResults), { hasNextPage: nextResults.rows && nextResults.rows.length > 0 });
636
+ });
578
637
  }
579
- async function fullSearch(dbName, index, query, params) {
580
- let limit = params.limit;
581
- if (limit == null || isNaN(limit) || limit < 0) {
582
- limit = 1e3;
583
- }
584
- params.limit = Math.min(limit, 1e3);
585
- const rows = await recursiveSearch(dbName, index, query, params);
586
- return { rows };
638
+ exports.paginatedSearch = paginatedSearch;
639
+ /**
640
+ * Performs a full search, fetching multiple pages if required to return the
641
+ * desired amount of results. There is a limit of 1000 results to avoid
642
+ * heavy performance hits, and to avoid client components breaking from
643
+ * handling too much data.
644
+ * @param dbName {string} Which database to run a lucene query on
645
+ * @param index {string} Which search index to utilise
646
+ * @param query {object} The JSON query structure
647
+ * @param params {object} The search params including:
648
+ * tableId {string} The table ID to search
649
+ * sort {string} The sort column
650
+ * sortOrder {string} The sort order ("ascending" or "descending")
651
+ * sortType {string} Whether to treat sortable values as strings or
652
+ * numbers. ("string" or "number")
653
+ * limit {number} The desired number of results
654
+ * @returns {Promise<{rows: *}>}
655
+ */
656
+ function fullSearch(dbName, index, query, params) {
657
+ return __awaiter(this, void 0, void 0, function* () {
658
+ let limit = params.limit;
659
+ if (limit == null || isNaN(limit) || limit < 0) {
660
+ limit = 1000;
661
+ }
662
+ params.limit = Math.min(limit, 1000);
663
+ const rows = yield recursiveSearch(dbName, index, query, params);
664
+ return { rows };
665
+ });
587
666
  }
588
- // Annotate the CommonJS export names for ESM import in node:
589
- 0 && (module.exports = {
590
- QueryBuilder,
591
- fullSearch,
592
- paginatedSearch,
593
- removeKeyNumbering
594
- });
595
- //# sourceMappingURL=lucene.js.map
667
+ exports.fullSearch = fullSearch;
668
+ //# sourceMappingURL=lucene.js.map