query.ts 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610
  1. /* eslint-disable no-unused-vars */
  2. import { OrderByDirection, QueryType } from './constant'
  3. import { Db } from './index'
  4. import { Validate } from './validate'
  5. import { Util } from './util'
  6. // import { Command } from './command';
  7. // import * as isRegExp from 'is-regex'
  8. import { QuerySerializer } from './serializer/query'
  9. import { UpdateSerializer } from './serializer/update'
  10. // import { WSClient } from "./websocket/wsclient"
  11. import { IWatchOptions, DBRealtimeListener } from './typings/index'
  12. import { RealtimeWebSocketClient } from './realtime/websocket-client'
  13. import { ErrorCode } from './constant'
  14. import { getReqOpts, stringifyByEJSON, processReturn } from './utils/utils'
  15. import { ERRORS } from './const/code'
  16. import { EJSON } from 'bson'
  17. interface GetRes {
  18. data: any[]
  19. requestId: string
  20. total: number
  21. limit: number
  22. offset: number
  23. }
  24. interface BaseOption {
  25. timeout?: number // 接口调用超时设置
  26. raw?: boolean // 原生语句查询
  27. }
  28. export interface QueryOption extends BaseOption {
  29. // 查询数量
  30. limit?: number
  31. // 偏移量
  32. offset?: number
  33. // 指定显示或者不显示哪些字段
  34. projection?: Object
  35. // 结果排序
  36. order?: Record<string, any>[]
  37. }
  38. export interface UpdateOption extends BaseOption {
  39. // 是否只影响单条doc
  40. multiple?: boolean
  41. // // 是否插入
  42. // upsert?: boolean
  43. // // 是否replace
  44. // merge?: boolean
  45. }
  46. /**
  47. * 查询模块
  48. *
  49. * @author haroldhu
  50. */
  51. export class Query {
  52. /**
  53. * Db 的引用
  54. *
  55. * @internal
  56. */
  57. protected _db: Db
  58. /**
  59. * Collection name
  60. *
  61. * @internal
  62. */
  63. protected _coll: string
  64. /**
  65. *
  66. * @protected
  67. * @type {string}
  68. * @memberof Query
  69. */
  70. protected _transactionId: string
  71. /**
  72. * 过滤条件
  73. *
  74. * @internal
  75. */
  76. private _fieldFilters: string
  77. // /**
  78. // * 排序条件
  79. // *
  80. // * @internal
  81. // */
  82. // private _fieldOrders: QueryOrder[]
  83. // /**
  84. // * 查询条件
  85. // *
  86. // * @internal
  87. // */
  88. // private _queryOptions: QueryOption
  89. /**
  90. * 统一条件项
  91. *
  92. * @private
  93. * @type {(QueryOption | UpdateOption)}
  94. * @memberof Query
  95. */
  96. public _apiOptions: QueryOption | UpdateOption
  97. /**
  98. * 请求实例
  99. *
  100. * @internal
  101. */
  102. public _request: any
  103. /**
  104. * websocket 参数 pingTimeout
  105. */
  106. // private _pingTimeout: number
  107. /**
  108. * websocket 参数 pongTimeout
  109. */
  110. // private _pongTimeout: number
  111. /**
  112. * websocket 参数 reconnectTimeout
  113. */
  114. // private _reconnectTimeout: number
  115. /**
  116. * websocket 参数 wsURL
  117. */
  118. // private _wsURL: string
  119. /**
  120. * 初始化
  121. *
  122. * @internal
  123. *
  124. * @param db - 数据库的引用
  125. * @param coll - 集合名称
  126. * @param fieldFilters - 过滤条件
  127. * @param fieldOrders - 排序条件
  128. * @param queryOptions - 查询条件
  129. */
  130. public constructor(
  131. db: Db,
  132. coll: string,
  133. fieldFilters?: string,
  134. apiOptions?: QueryOption | UpdateOption,
  135. transactionId?: string
  136. ) {
  137. this._db = db
  138. this._coll = coll
  139. this._fieldFilters = fieldFilters
  140. this._apiOptions = apiOptions || {}
  141. /* eslint-disable new-cap */
  142. this._request = new Db.reqClass(this._db.config)
  143. this._transactionId = transactionId
  144. }
  145. /**
  146. * 发起请求获取文档列表
  147. *
  148. * - 默认获取集合下全部文档数据
  149. * - 可以把通过 `orderBy`、`where`、`skip`、`limit`设置的数据添加请求参数上
  150. */
  151. public async get(): Promise<GetRes> {
  152. /* eslint-disable no-param-reassign */
  153. const order = (this._apiOptions as QueryOption).order
  154. interface Param {
  155. collectionName: string
  156. transactionId?: string
  157. query?: Object
  158. queryType: QueryType
  159. order?: string[]
  160. offset?: number
  161. limit?: number
  162. projection?: Object
  163. }
  164. let param: Param = {
  165. collectionName: this._coll,
  166. queryType: QueryType.WHERE,
  167. transactionId: this._transactionId
  168. }
  169. if (this._fieldFilters) {
  170. param.query = this._fieldFilters
  171. }
  172. if (order) {
  173. param.order = stringifyByEJSON(order)
  174. }
  175. // if (this._queryOptions.offset) {
  176. // param.offset = this._queryOptions.offset
  177. // }
  178. const offset = (this._apiOptions as QueryOption).offset
  179. if (offset) {
  180. param.offset = offset
  181. }
  182. const limit = (this._apiOptions as QueryOption).limit
  183. // if (this._queryOptions.limit) {
  184. // param.limit = this._queryOptions.limit < 1000 ? this._queryOptions.limit : 1000
  185. // } else {
  186. // param.limit = 100
  187. // }
  188. if (limit) {
  189. param.limit = limit < 1000 ? limit : 1000
  190. } else {
  191. param.limit = 100
  192. }
  193. const projection = (this._apiOptions as QueryOption).projection
  194. // if (this._queryOptions.projection) {
  195. // param.projection = this._queryOptions.projection
  196. // }
  197. if (projection) {
  198. param.projection = stringifyByEJSON(projection)
  199. }
  200. const res = await this._request.send(
  201. 'database.getDocument',
  202. param,
  203. getReqOpts(this._apiOptions)
  204. )
  205. if (res.code) {
  206. return res
  207. }
  208. // if (res.code) {
  209. // throw E({ ...res })
  210. // } else {
  211. const list = res.data.list.map(item => EJSON.parse(item))
  212. const documents = Util.formatResDocumentData(list)
  213. const result: any = {
  214. data: documents,
  215. requestId: res.requestId
  216. }
  217. if (res.limit) result.limit = res.limit
  218. if (res.offset) result.offset = res.offset
  219. return result
  220. }
  221. // }
  222. /**
  223. * 获取总数
  224. */
  225. public async count() {
  226. interface Param {
  227. collectionName: string
  228. query?: Object
  229. queryType: QueryType
  230. }
  231. let param: Param = {
  232. collectionName: this._coll,
  233. queryType: QueryType.WHERE
  234. }
  235. if (this._fieldFilters) {
  236. param.query = this._fieldFilters
  237. }
  238. const res = await this._request.send(
  239. 'database.calculateDocument',
  240. param,
  241. getReqOpts(this._apiOptions)
  242. )
  243. if (res.code) {
  244. return res
  245. }
  246. // if (res.code) {
  247. // throw E({ ...res })
  248. // } else {
  249. return {
  250. requestId: res.requestId,
  251. total: res.data.total
  252. }
  253. // }
  254. }
  255. /**
  256. * 查询条件
  257. *
  258. * @param query
  259. */
  260. public where(query: object) {
  261. // query校验 1. 必填对象类型 2. value 不可均为undefiend
  262. if (Object.prototype.toString.call(query).slice(8, -1) !== 'Object') {
  263. throw Error(ErrorCode.QueryParamTypeError)
  264. }
  265. const keys = Object.keys(query)
  266. const checkFlag = keys.some(item => {
  267. return query[item] !== undefined
  268. })
  269. if (keys.length && !checkFlag) {
  270. throw Error(ErrorCode.QueryParamValueError)
  271. }
  272. return new Query(
  273. this._db,
  274. this._coll,
  275. QuerySerializer.encodeEJSON(query, this._apiOptions.raw || false),
  276. this._apiOptions,
  277. this._transactionId
  278. // this._fieldOrders,
  279. // this._queryOptions
  280. )
  281. }
  282. /**
  283. * 设置请求操作项
  284. *
  285. * @param {(QueryOption | UpdateOption)} apiOptions
  286. * @memberof Query
  287. */
  288. public options(apiOptions: QueryOption | UpdateOption) {
  289. // 校验字段是否合规
  290. Validate.isValidOptions(apiOptions)
  291. return new Query(this._db, this._coll, this._fieldFilters, apiOptions, this._transactionId)
  292. }
  293. /**
  294. * 设置排序方式
  295. *
  296. * @param fieldPath - 字段路径
  297. * @param directionStr - 排序方式
  298. */
  299. public orderBy(fieldPath: string, directionStr: OrderByDirection): Query {
  300. Validate.isFieldPath(fieldPath)
  301. Validate.isFieldOrder(directionStr)
  302. const newOrder: Record<string, any> = {
  303. // key: fieldPath,
  304. // direction: directionStr === 'desc' ? -1 : 1
  305. [fieldPath]: directionStr === 'desc' ? -1 : 1
  306. }
  307. // const combinedOrders = this._fieldOrders.concat(newOrder)
  308. const order = (this._apiOptions as QueryOption).order || {}
  309. const newApiOption = Object.assign({}, this._apiOptions, {
  310. // order: order.concat(newOrder)
  311. order: Object.assign({}, order, newOrder)
  312. })
  313. return new Query(this._db, this._coll, this._fieldFilters, newApiOption, this._transactionId)
  314. }
  315. /**
  316. * 设置查询条数
  317. *
  318. * @param limit - 限制条数
  319. */
  320. public limit(limit: number): Query {
  321. Validate.isInteger('limit', limit)
  322. let newApiOption: QueryOption = { ...this._apiOptions }
  323. newApiOption.limit = limit
  324. return new Query(this._db, this._coll, this._fieldFilters, newApiOption, this._transactionId)
  325. }
  326. /**
  327. * 设置偏移量
  328. *
  329. * @param offset - 偏移量
  330. */
  331. public skip(offset: number): Query {
  332. Validate.isInteger('offset', offset)
  333. // let option = { ...this._queryOptions }
  334. let newApiOption: QueryOption = { ...this._apiOptions }
  335. newApiOption.offset = offset
  336. return new Query(this._db, this._coll, this._fieldFilters, newApiOption, this._transactionId)
  337. }
  338. /**
  339. * 发起请求批量更新文档
  340. *
  341. * @param data 数据
  342. */
  343. public async update(data: Object): Promise<any> {
  344. if (!data || typeof data !== 'object') {
  345. return processReturn(this._db.config.throwOnCode, {
  346. ...ERRORS.INVALID_PARAM,
  347. message: '参数必需是非空对象'
  348. })
  349. }
  350. if (data.hasOwnProperty('_id')) {
  351. return processReturn(this._db.config.throwOnCode, {
  352. ...ERRORS.INVALID_PARAM,
  353. message: '不能更新_id的值'
  354. })
  355. }
  356. let { multiple } = this._apiOptions as UpdateOption
  357. const multi = multiple === undefined ? true : multiple // where update 不传multi默认为true
  358. let param: any = {
  359. collectionName: this._coll,
  360. // query: this._fieldFilters,
  361. queryType: QueryType.WHERE,
  362. // query: QuerySerializer.encode(this._fieldFilters),
  363. multi,
  364. merge: true,
  365. upsert: false,
  366. data: UpdateSerializer.encodeEJSON(data, this._apiOptions.raw || false)
  367. }
  368. if (this._fieldFilters) {
  369. param.query = this._fieldFilters
  370. }
  371. const res = await this._request.send(
  372. 'database.modifyDocument',
  373. param,
  374. getReqOpts(this._apiOptions)
  375. )
  376. if (res.code) {
  377. return res
  378. }
  379. // if (res.code) {
  380. // throw E({ ...res })
  381. // } else {
  382. return {
  383. requestId: res.requestId,
  384. updated: res.data.updated,
  385. upsertId: res.data.upsert_id
  386. }
  387. // }
  388. }
  389. /**
  390. * 指定要返回的字段
  391. * project 示例
  392. * 存在doc {a:1, b:2, c: [1,2,3,4], d: [{item: 1}, [item: 2]]}
  393. * 1. 指定返回doc中字段a,b, projection设置为{a: true, b:true}
  394. * 2. 指定返回doc中数组字段c的 前1个元素 projection设置为{c: db.command.project.slice(1)}
  395. * 3. 指定返回doc中数组字段c的 第2,3个元素 projection设置为{c: db.command.project.slice([1,2])}
  396. * 4. 指定返回doc中数组字段d中的 满足属性值item大于1的第一个元素 projections设置为{c: db.command.project.elemMatch({item: db.command.gt(1)})}
  397. *
  398. * @param projection
  399. */
  400. public field(projection: any): Query {
  401. let transformProjection = {}
  402. for (let k in projection) {
  403. // 区分bool类型,number类型 和 Object类型
  404. if (typeof projection[k] === 'boolean') {
  405. transformProjection[k] = projection[k] === true ? 1 : 0
  406. }
  407. if (typeof projection[k] === 'number') {
  408. transformProjection[k] = projection[k] > 0 ? 1 : 0
  409. }
  410. if (typeof projection[k] === 'object') {
  411. transformProjection[k] = projection[k]
  412. }
  413. }
  414. let newApiOption: QueryOption = { ...this._apiOptions }
  415. newApiOption.projection = transformProjection
  416. return new Query(this._db, this._coll, this._fieldFilters, newApiOption, this._transactionId)
  417. }
  418. /**
  419. * 条件删除文档
  420. */
  421. public async remove() {
  422. // if (Object.keys(this._queryOptions).length > 0) {
  423. // console.warn('`offset`, `limit` and `projection` are not supported in remove() operation')
  424. // }
  425. // if (this._fieldOrders.length > 0) {
  426. // console.warn('`orderBy` is not supported in remove() operation')
  427. // }
  428. const { offset, limit, projection, order } = this._apiOptions as QueryOption
  429. if (
  430. offset !== undefined ||
  431. limit !== undefined ||
  432. projection !== undefined ||
  433. order !== undefined
  434. ) {
  435. console.warn(
  436. '`offset`, `limit`, `projection`, `orderBy` are not supported in remove() operation'
  437. )
  438. }
  439. let { multiple } = this._apiOptions as UpdateOption
  440. const multi = multiple === undefined ? true : multiple // where remove 不传multi默认为true
  441. const param = {
  442. collectionName: this._coll,
  443. query: this._fieldFilters,
  444. queryType: QueryType.WHERE,
  445. multi
  446. }
  447. const res = await this._request.send(
  448. 'database.removeDocument',
  449. param,
  450. getReqOpts(this._apiOptions)
  451. )
  452. if (res.code) {
  453. return res
  454. }
  455. // if (res.code) {
  456. // throw E({ ...res })
  457. // } else {
  458. return {
  459. requestId: res.requestId,
  460. deleted: res.data.deleted
  461. }
  462. // }
  463. }
  464. public async updateAndReturn(data: Object): Promise<any> {
  465. if (!data || typeof data !== 'object') {
  466. return processReturn(this._db.config.throwOnCode, {
  467. ...ERRORS.INVALID_PARAM,
  468. message: '参数必需是非空对象'
  469. })
  470. }
  471. if (data.hasOwnProperty('_id')) {
  472. return processReturn(this._db.config.throwOnCode, {
  473. ...ERRORS.INVALID_PARAM,
  474. message: '不能更新_id的值'
  475. })
  476. }
  477. let param: any = {
  478. collectionName: this._coll,
  479. queryType: QueryType.WHERE,
  480. data: UpdateSerializer.encodeEJSON(data, false)
  481. }
  482. if (this._transactionId) {
  483. param.transactionId = this._transactionId
  484. }
  485. if (this._fieldFilters) {
  486. param.query = this._fieldFilters
  487. }
  488. const res = await this._request.send(
  489. 'database.modifyAndReturnDoc',
  490. param,
  491. getReqOpts(this._apiOptions)
  492. )
  493. if (res.code) {
  494. return res
  495. }
  496. return {
  497. requestId: res.requestId,
  498. updated: res.data.updated,
  499. doc: res.data.doc && EJSON.parse(res.data.doc)
  500. }
  501. }
  502. /**
  503. * 监听query对应的doc变化
  504. */
  505. watch = (options: IWatchOptions): DBRealtimeListener => {
  506. if (!Db.ws) {
  507. Db.ws = new RealtimeWebSocketClient({
  508. context: {
  509. appConfig: {
  510. docSizeLimit: 1000,
  511. realtimePingInterval: 10000,
  512. realtimePongWaitTimeout: 5000,
  513. request: this._request
  514. }
  515. }
  516. })
  517. }
  518. const { limit, order } = this._apiOptions as QueryOption
  519. return (Db.ws as RealtimeWebSocketClient).watch({
  520. ...options,
  521. envId: this._db.config.env,
  522. collectionName: this._coll,
  523. query: JSON.stringify(this._fieldFilters), // 实时推送这里需要换成ejson协议,todo
  524. limit,
  525. orderBy: order
  526. ? order.reduce<Record<string, string>>((acc, cur) => {
  527. acc[cur.field] = cur.direction
  528. return acc
  529. }, {})
  530. : undefined
  531. })
  532. }
  533. }