lib.wx.cloud.d.ts 29 KB

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