index.d.ts 27 KB

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