lib.wx.cloud.d.ts 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016
  1. /*! *****************************************************************************
  2. Copyright (c) 2024 Tencent, Inc. All rights reserved.
  3. Permission is hereby granted, free of charge, to any person obtaining a copy of
  4. this software and associated documentation files (the "Software"), to deal in
  5. the Software without restriction, including without limitation the rights to
  6. use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
  7. of the Software, and to permit persons to whom the Software is furnished to do
  8. so, subject to the following conditions:
  9. The above copyright notice and this permission notice shall be included in all
  10. copies or substantial portions of the Software.
  11. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  17. SOFTWARE.
  18. ***************************************************************************** */
  19. /**
  20. * Common interfaces and types
  21. */
  22. interface IAPIError {
  23. errMsg: string
  24. }
  25. interface IAPIParam<T = any> {
  26. config?: ICloudConfig
  27. success?: (res: T) => void
  28. fail?: (err: IAPIError) => void
  29. complete?: (val: T | IAPIError) => void
  30. }
  31. interface IAPISuccessParam {
  32. errMsg: string
  33. }
  34. type IAPICompleteParam = IAPISuccessParam | IAPIError
  35. type IAPIFunction<T, P extends IAPIParam<T>> = (param?: P) => Promise<T>
  36. interface IInitCloudConfig {
  37. env?:
  38. | string
  39. | {
  40. database?: string
  41. functions?: string
  42. storage?: string
  43. }
  44. traceUser?: boolean
  45. }
  46. interface ICloudConfig {
  47. env?: string
  48. traceUser?: boolean
  49. }
  50. interface IICloudAPI {
  51. init: (config?: IInitCloudConfig) => void
  52. [api: string]: AnyFunction | IAPIFunction<any, any>
  53. }
  54. interface ICloudService {
  55. name: string
  56. getAPIs: () => { [name: string]: IAPIFunction<any, any> }
  57. }
  58. interface ICloudServices {
  59. [serviceName: string]: ICloudService
  60. }
  61. interface ICloudMetaData {
  62. session_id: string
  63. }
  64. declare class InternalSymbol {}
  65. interface AnyObject {
  66. [x: string]: any
  67. }
  68. type AnyArray = any[]
  69. type AnyFunction = (...args: any[]) => any
  70. /**
  71. * extend wx with cloud
  72. */
  73. interface WxCloud {
  74. init: (config?: ICloudConfig) => void
  75. callFunction(param: OQ<ICloud.CallFunctionParam>): void
  76. callFunction(
  77. param: RQ<ICloud.CallFunctionParam>
  78. ): Promise<ICloud.CallFunctionResult>
  79. uploadFile(param: OQ<ICloud.UploadFileParam>): WechatMiniprogram.UploadTask
  80. uploadFile(
  81. param: RQ<ICloud.UploadFileParam>
  82. ): Promise<ICloud.UploadFileResult>
  83. downloadFile(
  84. param: OQ<ICloud.DownloadFileParam>
  85. ): WechatMiniprogram.DownloadTask
  86. downloadFile(
  87. param: RQ<ICloud.DownloadFileParam>
  88. ): Promise<ICloud.DownloadFileResult>
  89. getTempFileURL(param: OQ<ICloud.GetTempFileURLParam>): void
  90. getTempFileURL(
  91. param: RQ<ICloud.GetTempFileURLParam>
  92. ): Promise<ICloud.GetTempFileURLResult>
  93. deleteFile(param: OQ<ICloud.DeleteFileParam>): void
  94. deleteFile(
  95. param: RQ<ICloud.DeleteFileParam>
  96. ): Promise<ICloud.DeleteFileResult>
  97. database: (config?: ICloudConfig) => DB.Database
  98. CloudID: ICloud.ICloudIDConstructor
  99. CDN: ICloud.ICDNConstructor
  100. callContainer(param: OQ<ICloud.CallContainerParam>): void
  101. callContainer(
  102. param: RQ<ICloud.CallContainerParam>
  103. ): Promise<ICloud.CallContainerResult>
  104. connectContainer(param: OQ<ICloud.ConnectContainerParam>): void
  105. connectContainer(
  106. param: RQ<ICloud.ConnectContainerParam>
  107. ): Promise<ICloud.ConnectContainerResult>
  108. services: ICloud.CloudServices
  109. }
  110. declare namespace ICloud {
  111. interface ICloudAPIParam<T = any> extends IAPIParam<T> {
  112. config?: ICloudConfig
  113. }
  114. // === API: callFunction ===
  115. type CallFunctionData = AnyObject
  116. interface CallFunctionResult extends IAPISuccessParam {
  117. result: AnyObject | string | undefined
  118. }
  119. interface CallFunctionParam extends ICloudAPIParam<CallFunctionResult> {
  120. name: string
  121. data?: CallFunctionData
  122. slow?: boolean
  123. }
  124. // === end ===
  125. // === API: container ===
  126. type CallContainerData = AnyObject
  127. interface CallContainerResult extends IAPISuccessParam {
  128. data: any
  129. statusCode: number
  130. header: Record<string, any>
  131. callID: string
  132. }
  133. interface CallContainerParam extends ICloudAPIParam<CallContainerResult> {
  134. path: string
  135. service?: string
  136. method?: string
  137. header?: Record<string, any>
  138. data?: any // string, object, ArrayBuffer
  139. dataType?: string
  140. responseType?: string
  141. timeout?: number
  142. verbose?: boolean
  143. followRedirect?: boolean
  144. }
  145. interface ConnectContainerResult extends IAPISuccessParam {
  146. socketTask: WechatMiniprogram.SocketTask
  147. }
  148. interface ConnectSocketOptions extends IAPIParam<void> {
  149. header?: Record<string, string>
  150. protocols?: string[]
  151. tcpNoDelay?: boolean
  152. perMessageDeflate?: boolean
  153. timeout?: number
  154. }
  155. type ConnectContainerParam = Omit<
  156. ConnectSocketOptions,
  157. 'success' | 'fail' | 'complete'
  158. > &
  159. ICloudAPIParam<ConnectContainerResult> & {
  160. service: string
  161. path?: string
  162. }
  163. // === end ===
  164. // === API: services ===
  165. type AsyncSession<T> = T | PromiseLike<T>
  166. interface GatewayCallOptions {
  167. path: string
  168. data: any
  169. shouldSerialize?: boolean
  170. apiVersion?: number
  171. }
  172. interface GatewayInstance {
  173. call: (
  174. param: CallContainerParam & GatewayCallOptions
  175. ) => Promise<CallContainerResult>
  176. refresh: (session: AsyncSession<string>) => Promise<void>
  177. }
  178. interface GatewayConstructOptions {
  179. id: string
  180. appid?: string
  181. domain?: string
  182. keepalive?: boolean
  183. prefetch?: boolean
  184. prefetchOptions?: {
  185. concurrent?: number
  186. enableQuic?: boolean
  187. enableHttp2?: boolean
  188. }
  189. }
  190. interface CloudServices {
  191. Gateway: (opts: GatewayConstructOptions) => GatewayInstance
  192. }
  193. // === end ===
  194. // === API: uploadFile ===
  195. interface UploadFileResult extends IAPISuccessParam {
  196. fileID: string
  197. statusCode: number
  198. }
  199. interface UploadFileParam extends ICloudAPIParam<UploadFileResult> {
  200. cloudPath: string
  201. filePath: string
  202. header?: AnyObject
  203. }
  204. // === end ===
  205. // === API: downloadFile ===
  206. interface DownloadFileResult extends IAPISuccessParam {
  207. tempFilePath: string
  208. statusCode: number
  209. }
  210. interface DownloadFileParam extends ICloudAPIParam<DownloadFileResult> {
  211. fileID: string
  212. cloudPath?: string
  213. }
  214. // === end ===
  215. // === API: getTempFileURL ===
  216. interface GetTempFileURLResult extends IAPISuccessParam {
  217. fileList: GetTempFileURLResultItem[]
  218. }
  219. interface GetTempFileURLResultItem {
  220. fileID: string
  221. tempFileURL: string
  222. maxAge: number
  223. status: number
  224. errMsg: string
  225. }
  226. interface GetTempFileURLParam extends ICloudAPIParam<GetTempFileURLResult> {
  227. fileList: string[]
  228. }
  229. // === end ===
  230. // === API: deleteFile ===
  231. interface DeleteFileResult extends IAPISuccessParam {
  232. fileList: DeleteFileResultItem[]
  233. }
  234. interface DeleteFileResultItem {
  235. fileID: string
  236. status: number
  237. errMsg: string
  238. }
  239. interface DeleteFileParam extends ICloudAPIParam<DeleteFileResult> {
  240. fileList: string[]
  241. }
  242. // === end ===
  243. // === API: CloudID ===
  244. abstract class CloudID {
  245. constructor(cloudID: string)
  246. }
  247. interface ICloudIDConstructor {
  248. new (cloudId: string): CloudID
  249. (cloudId: string): CloudID
  250. }
  251. // === end ===
  252. // === API: CDN ===
  253. abstract class CDN {
  254. target: string | ArrayBuffer | ICDNFilePathSpec
  255. constructor(target: string | ArrayBuffer | ICDNFilePathSpec)
  256. }
  257. interface ICDNFilePathSpec {
  258. type: 'filePath'
  259. filePath: string
  260. }
  261. interface ICDNConstructor {
  262. new (options: string | ArrayBuffer | ICDNFilePathSpec): CDN
  263. (options: string | ArrayBuffer | ICDNFilePathSpec): CDN
  264. }
  265. // === end ===
  266. }
  267. // === Database ===
  268. declare namespace DB {
  269. /**
  270. * The class of all exposed cloud database instances
  271. */
  272. class Database {
  273. readonly config: ICloudConfig
  274. readonly command: DatabaseCommand
  275. readonly Geo: IGeo
  276. readonly serverDate: () => ServerDate
  277. readonly RegExp: IRegExpConstructor
  278. private constructor()
  279. collection(collectionName: string): CollectionReference
  280. }
  281. class CollectionReference extends Query {
  282. readonly collectionName: string
  283. private constructor(name: string, database: Database)
  284. doc(docId: string | number): DocumentReference
  285. add(options: OQ<IAddDocumentOptions>): void
  286. add(options: RQ<IAddDocumentOptions>): Promise<IAddResult>
  287. }
  288. class DocumentReference {
  289. private constructor(docId: string | number, database: Database)
  290. field(object: Record<string, any>): this
  291. get(options: OQ<IGetDocumentOptions>): void
  292. get(options?: RQ<IGetDocumentOptions>): Promise<IQuerySingleResult>
  293. set(options: OQ<ISetSingleDocumentOptions>): void
  294. set(options?: RQ<ISetSingleDocumentOptions>): Promise<ISetResult>
  295. update(options: OQ<IUpdateSingleDocumentOptions>): void
  296. update(
  297. options?: RQ<IUpdateSingleDocumentOptions>
  298. ): Promise<IUpdateResult>
  299. remove(options: OQ<IRemoveSingleDocumentOptions>): void
  300. remove(
  301. options?: RQ<IRemoveSingleDocumentOptions>
  302. ): Promise<IRemoveResult>
  303. watch(options: IWatchOptions): RealtimeListener
  304. }
  305. class RealtimeListener {
  306. // "And Now His Watch Is Ended"
  307. close: () => Promise<void>
  308. }
  309. class Query {
  310. where(condition: IQueryCondition): Query
  311. orderBy(fieldPath: string, order: string): Query
  312. limit(max: number): Query
  313. skip(offset: number): Query
  314. field(object: Record<string, any>): Query
  315. get(options: OQ<IGetDocumentOptions>): void
  316. get(options?: RQ<IGetDocumentOptions>): Promise<IQueryResult>
  317. count(options: OQ<ICountDocumentOptions>): void
  318. count(options?: RQ<ICountDocumentOptions>): Promise<ICountResult>
  319. watch(options: IWatchOptions): RealtimeListener
  320. }
  321. interface DatabaseCommand {
  322. eq(val: any): DatabaseQueryCommand
  323. neq(val: any): DatabaseQueryCommand
  324. gt(val: any): DatabaseQueryCommand
  325. gte(val: any): DatabaseQueryCommand
  326. lt(val: any): DatabaseQueryCommand
  327. lte(val: any): DatabaseQueryCommand
  328. in(val: any[]): DatabaseQueryCommand
  329. nin(val: any[]): DatabaseQueryCommand
  330. geoNear(options: IGeoNearCommandOptions): DatabaseQueryCommand
  331. geoWithin(options: IGeoWithinCommandOptions): DatabaseQueryCommand
  332. geoIntersects(
  333. options: IGeoIntersectsCommandOptions
  334. ): DatabaseQueryCommand
  335. and(
  336. ...expressions: Array<DatabaseLogicCommand | IQueryCondition>
  337. ): DatabaseLogicCommand
  338. or(
  339. ...expressions: Array<DatabaseLogicCommand | IQueryCondition>
  340. ): DatabaseLogicCommand
  341. nor(
  342. ...expressions: Array<DatabaseLogicCommand | IQueryCondition>
  343. ): DatabaseLogicCommand
  344. not(expression: DatabaseLogicCommand): DatabaseLogicCommand
  345. exists(val: boolean): DatabaseQueryCommand
  346. mod(divisor: number, remainder: number): DatabaseQueryCommand
  347. all(val: any[]): DatabaseQueryCommand
  348. elemMatch(val: any): DatabaseQueryCommand
  349. size(val: number): DatabaseQueryCommand
  350. set(val: any): DatabaseUpdateCommand
  351. remove(): DatabaseUpdateCommand
  352. inc(val: number): DatabaseUpdateCommand
  353. mul(val: number): DatabaseUpdateCommand
  354. min(val: number): DatabaseUpdateCommand
  355. max(val: number): DatabaseUpdateCommand
  356. rename(val: string): DatabaseUpdateCommand
  357. bit(val: number): DatabaseUpdateCommand
  358. push(...values: any[]): DatabaseUpdateCommand
  359. pop(): DatabaseUpdateCommand
  360. shift(): DatabaseUpdateCommand
  361. unshift(...values: any[]): DatabaseUpdateCommand
  362. addToSet(val: any): DatabaseUpdateCommand
  363. pull(val: any): DatabaseUpdateCommand
  364. pullAll(val: any): DatabaseUpdateCommand
  365. project: {
  366. slice(val: number | [number, number]): DatabaseProjectionCommand
  367. }
  368. aggregate: {
  369. __safe_props__?: Set<string>
  370. abs(val: any): DatabaseAggregateCommand
  371. add(val: any): DatabaseAggregateCommand
  372. addToSet(val: any): DatabaseAggregateCommand
  373. allElementsTrue(val: any): DatabaseAggregateCommand
  374. and(val: any): DatabaseAggregateCommand
  375. anyElementTrue(val: any): DatabaseAggregateCommand
  376. arrayElemAt(val: any): DatabaseAggregateCommand
  377. arrayToObject(val: any): DatabaseAggregateCommand
  378. avg(val: any): DatabaseAggregateCommand
  379. ceil(val: any): DatabaseAggregateCommand
  380. cmp(val: any): DatabaseAggregateCommand
  381. concat(val: any): DatabaseAggregateCommand
  382. concatArrays(val: any): DatabaseAggregateCommand
  383. cond(val: any): DatabaseAggregateCommand
  384. convert(val: any): DatabaseAggregateCommand
  385. dateFromParts(val: any): DatabaseAggregateCommand
  386. dateToParts(val: any): DatabaseAggregateCommand
  387. dateFromString(val: any): DatabaseAggregateCommand
  388. dateToString(val: any): DatabaseAggregateCommand
  389. dayOfMonth(val: any): DatabaseAggregateCommand
  390. dayOfWeek(val: any): DatabaseAggregateCommand
  391. dayOfYear(val: any): DatabaseAggregateCommand
  392. divide(val: any): DatabaseAggregateCommand
  393. eq(val: any): DatabaseAggregateCommand
  394. exp(val: any): DatabaseAggregateCommand
  395. filter(val: any): DatabaseAggregateCommand
  396. first(val: any): DatabaseAggregateCommand
  397. floor(val: any): DatabaseAggregateCommand
  398. gt(val: any): DatabaseAggregateCommand
  399. gte(val: any): DatabaseAggregateCommand
  400. hour(val: any): DatabaseAggregateCommand
  401. ifNull(val: any): DatabaseAggregateCommand
  402. in(val: any): DatabaseAggregateCommand
  403. indexOfArray(val: any): DatabaseAggregateCommand
  404. indexOfBytes(val: any): DatabaseAggregateCommand
  405. indexOfCP(val: any): DatabaseAggregateCommand
  406. isArray(val: any): DatabaseAggregateCommand
  407. isoDayOfWeek(val: any): DatabaseAggregateCommand
  408. isoWeek(val: any): DatabaseAggregateCommand
  409. isoWeekYear(val: any): DatabaseAggregateCommand
  410. last(val: any): DatabaseAggregateCommand
  411. let(val: any): DatabaseAggregateCommand
  412. literal(val: any): DatabaseAggregateCommand
  413. ln(val: any): DatabaseAggregateCommand
  414. log(val: any): DatabaseAggregateCommand
  415. log10(val: any): DatabaseAggregateCommand
  416. lt(val: any): DatabaseAggregateCommand
  417. lte(val: any): DatabaseAggregateCommand
  418. ltrim(val: any): DatabaseAggregateCommand
  419. map(val: any): DatabaseAggregateCommand
  420. max(val: any): DatabaseAggregateCommand
  421. mergeObjects(val: any): DatabaseAggregateCommand
  422. meta(val: any): DatabaseAggregateCommand
  423. min(val: any): DatabaseAggregateCommand
  424. millisecond(val: any): DatabaseAggregateCommand
  425. minute(val: any): DatabaseAggregateCommand
  426. mod(val: any): DatabaseAggregateCommand
  427. month(val: any): DatabaseAggregateCommand
  428. multiply(val: any): DatabaseAggregateCommand
  429. neq(val: any): DatabaseAggregateCommand
  430. not(val: any): DatabaseAggregateCommand
  431. objectToArray(val: any): DatabaseAggregateCommand
  432. or(val: any): DatabaseAggregateCommand
  433. pow(val: any): DatabaseAggregateCommand
  434. push(val: any): DatabaseAggregateCommand
  435. range(val: any): DatabaseAggregateCommand
  436. reduce(val: any): DatabaseAggregateCommand
  437. reverseArray(val: any): DatabaseAggregateCommand
  438. rtrim(val: any): DatabaseAggregateCommand
  439. second(val: any): DatabaseAggregateCommand
  440. setDifference(val: any): DatabaseAggregateCommand
  441. setEquals(val: any): DatabaseAggregateCommand
  442. setIntersection(val: any): DatabaseAggregateCommand
  443. setIsSubset(val: any): DatabaseAggregateCommand
  444. setUnion(val: any): DatabaseAggregateCommand
  445. size(val: any): DatabaseAggregateCommand
  446. slice(val: any): DatabaseAggregateCommand
  447. split(val: any): DatabaseAggregateCommand
  448. sqrt(val: any): DatabaseAggregateCommand
  449. stdDevPop(val: any): DatabaseAggregateCommand
  450. stdDevSamp(val: any): DatabaseAggregateCommand
  451. strcasecmp(val: any): DatabaseAggregateCommand
  452. strLenBytes(val: any): DatabaseAggregateCommand
  453. strLenCP(val: any): DatabaseAggregateCommand
  454. substr(val: any): DatabaseAggregateCommand
  455. substrBytes(val: any): DatabaseAggregateCommand
  456. substrCP(val: any): DatabaseAggregateCommand
  457. subtract(val: any): DatabaseAggregateCommand
  458. sum(val: any): DatabaseAggregateCommand
  459. switch(val: any): DatabaseAggregateCommand
  460. toBool(val: any): DatabaseAggregateCommand
  461. toDate(val: any): DatabaseAggregateCommand
  462. toDecimal(val: any): DatabaseAggregateCommand
  463. toDouble(val: any): DatabaseAggregateCommand
  464. toInt(val: any): DatabaseAggregateCommand
  465. toLong(val: any): DatabaseAggregateCommand
  466. toObjectId(val: any): DatabaseAggregateCommand
  467. toString(val: any): DatabaseAggregateCommand
  468. toLower(val: any): DatabaseAggregateCommand
  469. toUpper(val: any): DatabaseAggregateCommand
  470. trim(val: any): DatabaseAggregateCommand
  471. trunc(val: any): DatabaseAggregateCommand
  472. type(val: any): DatabaseAggregateCommand
  473. week(val: any): DatabaseAggregateCommand
  474. year(val: any): DatabaseAggregateCommand
  475. zip(val: any): DatabaseAggregateCommand
  476. }
  477. }
  478. class DatabaseAggregateCommand {}
  479. enum LOGIC_COMMANDS_LITERAL {
  480. AND = 'and',
  481. OR = 'or',
  482. NOT = 'not',
  483. NOR = 'nor'
  484. }
  485. class DatabaseLogicCommand {
  486. and(...expressions: DatabaseLogicCommand[]): DatabaseLogicCommand
  487. or(...expressions: DatabaseLogicCommand[]): DatabaseLogicCommand
  488. nor(...expressions: DatabaseLogicCommand[]): DatabaseLogicCommand
  489. not(expression: DatabaseLogicCommand): DatabaseLogicCommand
  490. }
  491. enum QUERY_COMMANDS_LITERAL {
  492. // comparison
  493. EQ = 'eq',
  494. NEQ = 'neq',
  495. GT = 'gt',
  496. GTE = 'gte',
  497. LT = 'lt',
  498. LTE = 'lte',
  499. IN = 'in',
  500. NIN = 'nin',
  501. // geo
  502. GEO_NEAR = 'geoNear',
  503. GEO_WITHIN = 'geoWithin',
  504. GEO_INTERSECTS = 'geoIntersects',
  505. // element
  506. EXISTS = 'exists',
  507. // evaluation
  508. MOD = 'mod',
  509. // array
  510. ALL = 'all',
  511. ELEM_MATCH = 'elemMatch',
  512. SIZE = 'size'
  513. }
  514. class DatabaseQueryCommand extends DatabaseLogicCommand {
  515. eq(val: any): DatabaseLogicCommand
  516. neq(val: any): DatabaseLogicCommand
  517. gt(val: any): DatabaseLogicCommand
  518. gte(val: any): DatabaseLogicCommand
  519. lt(val: any): DatabaseLogicCommand
  520. lte(val: any): DatabaseLogicCommand
  521. in(val: any[]): DatabaseLogicCommand
  522. nin(val: any[]): DatabaseLogicCommand
  523. exists(val: boolean): DatabaseLogicCommand
  524. mod(divisor: number, remainder: number): DatabaseLogicCommand
  525. all(val: any[]): DatabaseLogicCommand
  526. elemMatch(val: any): DatabaseLogicCommand
  527. size(val: number): DatabaseLogicCommand
  528. geoNear(options: IGeoNearCommandOptions): DatabaseLogicCommand
  529. geoWithin(options: IGeoWithinCommandOptions): DatabaseLogicCommand
  530. geoIntersects(
  531. options: IGeoIntersectsCommandOptions
  532. ): DatabaseLogicCommand
  533. }
  534. enum PROJECTION_COMMANDS_LITERAL {
  535. SLICE = 'slice'
  536. }
  537. class DatabaseProjectionCommand {}
  538. enum UPDATE_COMMANDS_LITERAL {
  539. // field
  540. SET = 'set',
  541. REMOVE = 'remove',
  542. INC = 'inc',
  543. MUL = 'mul',
  544. MIN = 'min',
  545. MAX = 'max',
  546. RENAME = 'rename',
  547. // bitwise
  548. BIT = 'bit',
  549. // array
  550. PUSH = 'push',
  551. POP = 'pop',
  552. SHIFT = 'shift',
  553. UNSHIFT = 'unshift',
  554. ADD_TO_SET = 'addToSet',
  555. PULL = 'pull',
  556. PULL_ALL = 'pullAll'
  557. }
  558. class DatabaseUpdateCommand {}
  559. class Batch {}
  560. /**
  561. * A contract that all API provider must adhere to
  562. */
  563. class APIBaseContract<
  564. PromiseReturn,
  565. CallbackReturn,
  566. Param extends IAPIParam,
  567. Context = any
  568. > {
  569. getContext(param: Param): Context
  570. /**
  571. * In case of callback-style invocation, this function will be called
  572. */
  573. getCallbackReturn(param: Param, context: Context): CallbackReturn
  574. getFinalParam<T extends Param>(param: Param, context: Context): T
  575. run<T extends Param>(param: T): Promise<PromiseReturn>
  576. }
  577. interface IGeoPointConstructor {
  578. new (longitude: number, latitide: number): GeoPoint
  579. new (geojson: IGeoJSONPoint): GeoPoint
  580. (longitude: number, latitide: number): GeoPoint
  581. (geojson: IGeoJSONPoint): GeoPoint
  582. }
  583. interface IGeoMultiPointConstructor {
  584. new (points: GeoPoint[] | IGeoJSONMultiPoint): GeoMultiPoint
  585. (points: GeoPoint[] | IGeoJSONMultiPoint): GeoMultiPoint
  586. }
  587. interface IGeoLineStringConstructor {
  588. new (points: GeoPoint[] | IGeoJSONLineString): GeoLineString
  589. (points: GeoPoint[] | IGeoJSONLineString): GeoLineString
  590. }
  591. interface IGeoMultiLineStringConstructor {
  592. new (
  593. lineStrings: GeoLineString[] | IGeoJSONMultiLineString
  594. ): GeoMultiLineString
  595. (
  596. lineStrings: GeoLineString[] | IGeoJSONMultiLineString
  597. ): GeoMultiLineString
  598. }
  599. interface IGeoPolygonConstructor {
  600. new (lineStrings: GeoLineString[] | IGeoJSONPolygon): GeoPolygon
  601. (lineStrings: GeoLineString[] | IGeoJSONPolygon): GeoPolygon
  602. }
  603. interface IGeoMultiPolygonConstructor {
  604. new (polygons: GeoPolygon[] | IGeoJSONMultiPolygon): GeoMultiPolygon
  605. (polygons: GeoPolygon[] | IGeoJSONMultiPolygon): GeoMultiPolygon
  606. }
  607. interface IGeo {
  608. Point: IGeoPointConstructor
  609. MultiPoint: IGeoMultiPointConstructor
  610. LineString: IGeoLineStringConstructor
  611. MultiLineString: IGeoMultiLineStringConstructor
  612. Polygon: IGeoPolygonConstructor
  613. MultiPolygon: IGeoMultiPolygonConstructor
  614. }
  615. interface IGeoJSONPoint {
  616. type: 'Point'
  617. coordinates: [number, number]
  618. }
  619. interface IGeoJSONMultiPoint {
  620. type: 'MultiPoint'
  621. coordinates: Array<[number, number]>
  622. }
  623. interface IGeoJSONLineString {
  624. type: 'LineString'
  625. coordinates: Array<[number, number]>
  626. }
  627. interface IGeoJSONMultiLineString {
  628. type: 'MultiLineString'
  629. coordinates: Array<Array<[number, number]>>
  630. }
  631. interface IGeoJSONPolygon {
  632. type: 'Polygon'
  633. coordinates: Array<Array<[number, number]>>
  634. }
  635. interface IGeoJSONMultiPolygon {
  636. type: 'MultiPolygon'
  637. coordinates: Array<Array<Array<[number, number]>>>
  638. }
  639. type IGeoJSONObject =
  640. | IGeoJSONPoint
  641. | IGeoJSONMultiPoint
  642. | IGeoJSONLineString
  643. | IGeoJSONMultiLineString
  644. | IGeoJSONPolygon
  645. | IGeoJSONMultiPolygon
  646. abstract class GeoPoint {
  647. longitude: number
  648. latitude: number
  649. constructor(longitude: number, latitude: number)
  650. toJSON(): Record<string, any>
  651. toString(): string
  652. }
  653. abstract class GeoMultiPoint {
  654. points: GeoPoint[]
  655. constructor(points: GeoPoint[])
  656. toJSON(): IGeoJSONMultiPoint
  657. toString(): string
  658. }
  659. abstract class GeoLineString {
  660. points: GeoPoint[]
  661. constructor(points: GeoPoint[])
  662. toJSON(): IGeoJSONLineString
  663. toString(): string
  664. }
  665. abstract class GeoMultiLineString {
  666. lines: GeoLineString[]
  667. constructor(lines: GeoLineString[])
  668. toJSON(): IGeoJSONMultiLineString
  669. toString(): string
  670. }
  671. abstract class GeoPolygon {
  672. lines: GeoLineString[]
  673. constructor(lines: GeoLineString[])
  674. toJSON(): IGeoJSONPolygon
  675. toString(): string
  676. }
  677. abstract class GeoMultiPolygon {
  678. polygons: GeoPolygon[]
  679. constructor(polygons: GeoPolygon[])
  680. toJSON(): IGeoJSONMultiPolygon
  681. toString(): string
  682. }
  683. type GeoInstance =
  684. | GeoPoint
  685. | GeoMultiPoint
  686. | GeoLineString
  687. | GeoMultiLineString
  688. | GeoPolygon
  689. | GeoMultiPolygon
  690. interface IGeoNearCommandOptions {
  691. geometry: GeoPoint
  692. maxDistance?: number
  693. minDistance?: number
  694. }
  695. interface IGeoWithinCommandOptions {
  696. geometry: GeoPolygon | GeoMultiPolygon
  697. }
  698. interface IGeoIntersectsCommandOptions {
  699. geometry:
  700. | GeoPoint
  701. | GeoMultiPoint
  702. | GeoLineString
  703. | GeoMultiLineString
  704. | GeoPolygon
  705. | GeoMultiPolygon
  706. }
  707. interface IServerDateOptions {
  708. offset: number
  709. }
  710. abstract class ServerDate {
  711. readonly options: IServerDateOptions
  712. constructor(options?: IServerDateOptions)
  713. }
  714. interface IRegExpOptions {
  715. regexp: string
  716. options?: string
  717. }
  718. interface IRegExpConstructor {
  719. new (options: IRegExpOptions): RegExp
  720. (options: IRegExpOptions): RegExp
  721. }
  722. abstract class RegExp {
  723. readonly regexp: string
  724. readonly options: string
  725. constructor(options: IRegExpOptions)
  726. }
  727. type DocumentId = string | number
  728. interface IDocumentData {
  729. _id?: DocumentId
  730. [key: string]: any
  731. }
  732. type IDBAPIParam = IAPIParam
  733. interface IAddDocumentOptions extends IDBAPIParam {
  734. data: IDocumentData
  735. }
  736. type IGetDocumentOptions = IDBAPIParam
  737. type ICountDocumentOptions = IDBAPIParam
  738. interface IUpdateDocumentOptions extends IDBAPIParam {
  739. data: IUpdateCondition
  740. }
  741. interface IUpdateSingleDocumentOptions extends IDBAPIParam {
  742. data: IUpdateCondition
  743. }
  744. interface ISetDocumentOptions extends IDBAPIParam {
  745. data: IUpdateCondition
  746. }
  747. interface ISetSingleDocumentOptions extends IDBAPIParam {
  748. data: IUpdateCondition
  749. }
  750. interface IRemoveDocumentOptions extends IDBAPIParam {
  751. query: IQueryCondition
  752. }
  753. type IRemoveSingleDocumentOptions = IDBAPIParam
  754. interface IWatchOptions {
  755. // server realtime data init & change event
  756. onChange: (snapshot: ISnapshot) => void
  757. // error while connecting / listening
  758. onError: (error: any) => void
  759. }
  760. interface ISnapshot {
  761. id: number
  762. docChanges: ISingleDBEvent[]
  763. docs: Record<string, any>
  764. type?: SnapshotType
  765. }
  766. type SnapshotType = 'init'
  767. interface ISingleDBEvent {
  768. id: number
  769. dataType: DataType
  770. queueType: QueueType
  771. docId: string
  772. doc: Record<string, any>
  773. updatedFields?: Record<string, any>
  774. removedFields?: string[]
  775. }
  776. type DataType = 'init' | 'update' | 'replace' | 'add' | 'remove' | 'limit'
  777. type QueueType = 'init' | 'enqueue' | 'dequeue' | 'update'
  778. interface IQueryCondition {
  779. [key: string]: any
  780. }
  781. type IStringQueryCondition = string
  782. interface IQueryResult extends IAPISuccessParam {
  783. data: IDocumentData[]
  784. }
  785. interface IQuerySingleResult extends IAPISuccessParam {
  786. data: IDocumentData
  787. }
  788. interface IUpdateCondition {
  789. [key: string]: any
  790. }
  791. type IStringUpdateCondition = string
  792. interface IAddResult extends IAPISuccessParam {
  793. _id: DocumentId
  794. }
  795. interface IUpdateResult extends IAPISuccessParam {
  796. stats: {
  797. updated: number
  798. // created: number,
  799. }
  800. }
  801. interface ISetResult extends IAPISuccessParam {
  802. _id: DocumentId
  803. stats: {
  804. updated: number
  805. created: number
  806. }
  807. }
  808. interface IRemoveResult extends IAPISuccessParam {
  809. stats: {
  810. removed: number
  811. }
  812. }
  813. interface ICountResult extends IAPISuccessParam {
  814. total: number
  815. }
  816. }
  817. type Optional<T> = { [K in keyof T]+?: T[K] }
  818. type OQ<
  819. T extends Optional<
  820. Record<'complete' | 'success' | 'fail', (...args: any[]) => any>
  821. >
  822. > =
  823. | (RQ<T> & Required<Pick<T, 'success'>>)
  824. | (RQ<T> & Required<Pick<T, 'fail'>>)
  825. | (RQ<T> & Required<Pick<T, 'complete'>>)
  826. | (RQ<T> & Required<Pick<T, 'success' | 'fail'>>)
  827. | (RQ<T> & Required<Pick<T, 'success' | 'complete'>>)
  828. | (RQ<T> & Required<Pick<T, 'fail' | 'complete'>>)
  829. | (RQ<T> & Required<Pick<T, 'fail' | 'complete' | 'success'>>)
  830. type RQ<
  831. T extends Optional<
  832. Record<'complete' | 'success' | 'fail', (...args: any[]) => any>
  833. >
  834. > = Pick<T, Exclude<keyof T, 'complete' | 'success' | 'fail'>>