index.d.ts 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. // Generated by typings
  2. // Source: https://raw.githubusercontent.com/DefinitelyTyped/DefinitelyTyped/0fa2d359311a996084dc3bb67a80365992e3ba42/pouchdb-core/pouchdb-core.d.ts
  3. declare namespace PouchDB {
  4. namespace Core {
  5. interface Error {
  6. }
  7. interface Callback<E, R> {
  8. (error: E | void, result: R | void): void;
  9. }
  10. type AnyCallback = Callback<any, any>;
  11. type DocumentId = string;
  12. type DocumentKey = string;
  13. type RevisionId = string;
  14. type Availability = 'available' | 'compacted' | 'not compacted' | 'missing';
  15. type Attachment = string | ArrayBuffer;
  16. type Encodable = { [propertyName: string]: any };
  17. interface Options {
  18. ajax?: Configuration.RemoteRequesterConfiguration;
  19. }
  20. interface BasicResponse {
  21. /** `true` if the operation was successful; `false` otherwise */
  22. ok: boolean;
  23. }
  24. interface Response extends BasicResponse {
  25. /** id of the targeted document */
  26. id: DocumentId;
  27. /** resulting revision of the targeted document */
  28. rev: RevisionId;
  29. }
  30. interface DatabaseInfo {
  31. }
  32. interface Revision<Content> {
  33. ok: Document<Content> & RevisionIdMeta;
  34. }
  35. interface RevisionInfo {
  36. rev: RevisionId;
  37. status: Availability;
  38. }
  39. interface IdMeta {
  40. _id: DocumentId;
  41. }
  42. interface RevisionIdMeta {
  43. _rev: RevisionId;
  44. }
  45. interface GetMeta {
  46. /** Conflicting leaf revisions.
  47. *
  48. * Only present if `GetOptions.conflicts` is `true`
  49. */
  50. _conflicts?: RevisionId[];
  51. _rev?: RevisionId;
  52. /** Only present if `GetOptions.revs` is `true` */
  53. _revs_info?: RevisionInfo[];
  54. /** Only present if `GetOptions.revs_info` is `true` */
  55. _revisions?: {
  56. ids: RevisionId[];
  57. start: number;
  58. }
  59. }
  60. type NewDocument<Content extends Encodable> = Content;
  61. type Document<Content extends Encodable> = Content & IdMeta;
  62. type ExistingDocument<Content extends Encodable> =
  63. Document<Content> & RevisionIdMeta;
  64. interface AllDocsOptions extends Options {
  65. /** Include attachment data for each document.
  66. *
  67. * Requires `include_docs` to be `true`.
  68. *
  69. * By default, attachments are Base64-encoded.
  70. * @see binary
  71. */
  72. attachments?: boolean;
  73. /** Return attachments as Buffers.
  74. *
  75. * Requires `include_docs` to be `true`.
  76. * Requires `attachments` to be `true`. */
  77. binary?: boolean;
  78. /** Include conflict information for each document.
  79. *
  80. * Requires `include_docs` to be `true`. */
  81. conflicts?: boolean;
  82. /** Reverse ordering of results. */
  83. descending?: boolean;
  84. /** Include contents for each document. */
  85. include_docs?: boolean;
  86. /** Maximum number of documents to return. */
  87. limit?: number;
  88. /** Number of documents to skip before returning.
  89. *
  90. * Causes poor performance on IndexedDB and LevelDB. */
  91. skip?: number;
  92. }
  93. interface AllDocsWithKeyOptions extends AllDocsOptions {
  94. /** Constrain results to documents matching this key. */
  95. key: DocumentKey;
  96. }
  97. interface AllDocsWithKeysOptions extends AllDocsOptions {
  98. /** Constrains results to documents matching any of these keys. */
  99. keys: DocumentId[];
  100. }
  101. interface AllDocsWithinRangeOptions extends AllDocsOptions {
  102. /** Low end of range, or high end if `descending` is `true`. */
  103. startkey: DocumentKey;
  104. /** High end of range, or low end if `descending` is `true`. */
  105. endkey: DocumentKey;
  106. /** Include any documents identified by `endkey`.
  107. *
  108. * Defaults to `true`. */
  109. inclusive_end?: boolean;
  110. }
  111. interface AllDocsMeta {
  112. _attachments?: {
  113. [attachmentId: string]: Attachment;
  114. };
  115. }
  116. interface AllDocsResponse<Content extends Core.Encodable> {
  117. /** The `skip` if provided, or in CouchDB the actual offset */
  118. offset: number;
  119. total_rows: number;
  120. rows: {
  121. /** Only present if `include_docs` was `true`. */
  122. doc?: Document<Content & AllDocsMeta>;
  123. id: DocumentId;
  124. key: DocumentKey;
  125. value: {
  126. rev: RevisionId;
  127. }
  128. }[];
  129. }
  130. interface DestroyOptions extends Options {
  131. }
  132. interface GetOptions extends Options {
  133. /** Include list of conflicting leaf revisions. */
  134. conflicts?: boolean;
  135. /** Specific revision to fetch */
  136. rev?: RevisionId;
  137. /** Include revision history of the document. */
  138. revs?: boolean;
  139. /** Include a list of revisions of the document, and their
  140. * availability. */
  141. revs_info?: boolean;
  142. }
  143. interface GetOpenRevisions extends Options {
  144. /** Fetch all leaf revisions if open_revs="all" or fetch all leaf
  145. * revisions specified in open_revs array. Leaves will be returned
  146. * in the same order as specified in input array. */
  147. open_revs: 'all' | Core.RevisionId[];
  148. }
  149. /** @todo does this have any other properties? */
  150. interface PutOptions extends Options {
  151. }
  152. interface PostOptions extends PutOptions {
  153. }
  154. interface CompactOptions extends Core.Options {
  155. interval?: number;
  156. }
  157. interface InfoOptions extends Options {
  158. }
  159. }
  160. /**
  161. * Pass this to `PouchDB.plugin()`.
  162. */
  163. export type Plugin = 'This should be passed to PouchDB.plugin()';
  164. namespace Configuration {
  165. interface CommonDatabaseConfiguration {
  166. /**
  167. * Database name.
  168. */
  169. name?: string;
  170. /**
  171. * Database adapter to use.
  172. *
  173. * If unspecified, PouchDB will infer this automatically, preferring
  174. * IndexedDB to WebSQL in browsers that support both (i.e. Chrome,
  175. * Opera and Android 4.4+).
  176. */
  177. adapter?: string;
  178. }
  179. interface LocalDatabaseConfiguration extends CommonDatabaseConfiguration {
  180. /**
  181. * Enables auto compaction, which means compact() is called after
  182. * every change to the database.
  183. *
  184. * Defaults to false.
  185. */
  186. auto_compaction?: boolean;
  187. /**
  188. * How many old revisions we keep track (not a copy) of.
  189. */
  190. revs_limit?: number;
  191. }
  192. interface RemoteRequesterConfiguration {
  193. /**
  194. * Time before HTTP requests time out (in ms).
  195. */
  196. timeout?: number;
  197. /**
  198. * Appends a random string to the end of all HTTP GET requests to avoid
  199. * them being cached on IE. Set this to true to prevent this happening.
  200. */
  201. cache?: boolean;
  202. /**
  203. * HTTP headers to add to requests.
  204. */
  205. headers?: {
  206. [name: string]: string;
  207. }
  208. username?: string;
  209. password?: string;
  210. /**
  211. * Enables transferring cookies and HTTP Authorization information.
  212. *
  213. * Defaults to true.
  214. */
  215. withCredentials?: boolean;
  216. /**
  217. * Disables automatic creation of databases.
  218. */
  219. skip_setup?: boolean;
  220. }
  221. interface RemoteDatabaseConfiguration extends CommonDatabaseConfiguration {
  222. ajax?: RemoteRequesterConfiguration;
  223. }
  224. type DatabaseConfiguration = LocalDatabaseConfiguration |
  225. RemoteDatabaseConfiguration;
  226. }
  227. interface Static {
  228. plugin(plugin: Plugin): Static;
  229. new<Content extends Core.Encodable>(name?: string,
  230. options?: Configuration.DatabaseConfiguration): Database<Content>;
  231. }
  232. interface Database<Content extends Core.Encodable> {
  233. /** Fetch all documents matching the given key. */
  234. allDocs(options: Core.AllDocsWithKeyOptions):
  235. Promise<Core.AllDocsResponse<Content>>;
  236. /** Fetch all documents matching any of the given keys. */
  237. allDocs(options: Core.AllDocsWithKeysOptions):
  238. Promise<Core.AllDocsResponse<Content>>;
  239. /** Fetch all documents matching the given key range. */
  240. allDocs(options: Core.AllDocsWithinRangeOptions):
  241. Promise<Core.AllDocsResponse<Content>>;
  242. /** Fetch all documents. */
  243. allDocs(options?: Core.AllDocsOptions):
  244. Promise<Core.AllDocsResponse<Content>>;
  245. bulkDocs(docs: Core.Document<Content>[],
  246. options: Core.PutOptions | void,
  247. callback: Core.Callback<Core.Error, Core.Response[]>): void;
  248. bulkDocs(docs: Core.Document<Content>[],
  249. options?: Core.PutOptions): Promise<Core.Response[]>;
  250. /** Compact the database */
  251. compact(options?: Core.CompactOptions): Promise<Core.Response>;
  252. compact(options: Core.CompactOptions,
  253. callback: Core.Callback<Core.Error, Core.Response>): void;
  254. /** Destroy the database */
  255. destroy(options: Core.DestroyOptions | void,
  256. callback: Core.AnyCallback): void;
  257. destroy(options?: Core.DestroyOptions | void): Promise<void>;
  258. /** Fetch a document */
  259. get(docId: Core.DocumentId,
  260. options: Core.GetOpenRevisions): Promise<Core.Revision<Content>[]>;
  261. get(docId: Core.DocumentId,
  262. options: Core.GetOpenRevisions,
  263. callback: Core.Callback<any,
  264. Core.Revision<Content>[]>): void;
  265. get(docId: Core.DocumentId,
  266. options: Core.GetOptions
  267. ): Promise<Core.Document<Content> & Core.GetMeta>;
  268. get(docId: Core.DocumentId,
  269. options: Core.GetOptions,
  270. callback: Core.Callback<any, Core.Document<Content> & Core.GetMeta>
  271. ): void;
  272. get(docId: Core.DocumentId,
  273. options: void,
  274. callback: Core.Callback<any, Core.Document<Content>>): void;
  275. get(docId: Core.DocumentId): Promise<Core.Document<Content>>;
  276. /** Create a new document without providing an id.
  277. *
  278. * You should prefer put() to post(), because when you post(), you are
  279. * missing an opportunity to use allDocs() to sort documents by _id
  280. * (because your _ids are random).
  281. *
  282. * @see {@link https://pouchdb.com/2014/06/17/12-pro-tips-for-better-code-with-pouchdb.html|PouchDB Pro Tips}
  283. * */
  284. post(doc: Core.NewDocument<Content>,
  285. options: Core.PostOptions | void,
  286. callback: Core.Callback<Core.Error, Core.Response>): void;
  287. post(doc: Core.NewDocument<Content>,
  288. options?: Core.PostOptions): Promise<Core.Response>;
  289. /** Create a new document or update an existing document.
  290. *
  291. * If the document already exists, you must specify its revision _rev,
  292. * otherwise a conflict will occur.
  293. * There are some restrictions on valid property names of the documents.
  294. * If you try to store non-JSON data (for instance Date objects) you may
  295. * see inconsistent results. */
  296. put(doc: Core.Document<Content>,
  297. id: Core.DocumentId | void,
  298. revision: Core.RevisionId | void,
  299. options: Core.PutOptions | void,
  300. callback: Core.Callback<Core.Error, Core.Response>): void;
  301. put(doc: Core.Document<Content>,
  302. id?: Core.DocumentId,
  303. revision?: Core.RevisionId,
  304. options?: Core.PutOptions): Promise<Core.Response>;
  305. /** Remove a doc from the database */
  306. remove(doc: Core.Document<Content>,
  307. options: Core.Options,
  308. callback: Core.Callback<Core.Error, Core.Response>): void;
  309. remove(docId: Core.DocumentId,
  310. revision: Core.RevisionId,
  311. options: Core.Options,
  312. callback: Core.Callback<Core.Error, Core.Response>): void;
  313. remove(doc: Core.Document<Content>,
  314. options?: Core.Options): Promise<Core.Response>;
  315. remove(docId: Core.DocumentId,
  316. revision: Core.RevisionId,
  317. options?: Core.Options): Promise<Core.Response>;
  318. /** Get database information */
  319. info(options: Core.InfoOptions | void,
  320. callback: Core.Callback<any, Core.DatabaseInfo>): void;
  321. info(options?: Core.InfoOptions): Promise<Core.DatabaseInfo>;
  322. }
  323. }
  324. declare module 'pouchdb-core' {
  325. const PouchDb: PouchDB.Static;
  326. export = PouchDb;
  327. }
  328. declare var PouchDB: PouchDB.Static;