Skip to content
This repository was archived by the owner on Jan 9, 2023. It is now read-only.

Commit b999929

Browse files
committed
feat: add CouchDB JavaScript context
implement #232
1 parent c028cb0 commit b999929

File tree

5 files changed

+153
-1
lines changed

5 files changed

+153
-1
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
"scripts": {
1111
"commit": "npx git-cz",
1212
"build": "npm run clean && tsc -p ./tsconfig.json",
13+
"build:designs": "tsc -p ./src/db/tsconfig.json",
1314
"start": "node dist",
1415
"clean": "rimraf dist",
1516
"dev:db": "pouchdb-server --config ./db/config.json",

src/db/context.ts

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/**
2+
* Emits a key-value pair for further processing by CouchDB after the map function is done.
3+
* @param {string} key - The view key
4+
* @param {any} value - The key’s associated value
5+
* @returns {void}
6+
*/
7+
declare function emit(key: string, value: any): void
8+
/**
9+
* @deprecated since version 2.0
10+
* Extracts the next row from a related view result.
11+
* @returns {Object} - View result row
12+
*/
13+
declare function getRow<T>(): T
14+
15+
/**
16+
* A helper function to check if the provided value is an Array.
17+
* @param {Object} obj - Any JavaScript value
18+
* @returns {boolean}
19+
*/
20+
declare function isArray<T>(obj: T): boolean
21+
22+
/**
23+
* Log a message to the CouchDB log (at the INFO level).
24+
* @param {string} message - Message to be logged
25+
* @returns {void}
26+
*/
27+
declare function log(message: string): void
28+
29+
/**
30+
* @deprecated since version 2.0
31+
* Registers callable handler for specified MIME key.
32+
* @param {string} key - MIME key previously defined by registerType()
33+
* @param {Function} value - MIME type handler
34+
* @returns {void}
35+
*/
36+
declare function provides(key: string, func: Function): void
37+
38+
/**
39+
* @deprecated since version 2.0
40+
* Registers list of MIME types by associated key.
41+
* @param {string} key - MIME types
42+
* @param {string[]} mimes - MMIME types enumeration
43+
* @returns {void}
44+
*/
45+
declare function registerType(key: string, mimes: string[]): void
46+
47+
/**
48+
* @deprecated since version 2.0
49+
* Sends a single string chunk in response.
50+
* @param {string} chunk - Text chunk
51+
* @returns {void}
52+
*/
53+
declare function send(chunk: string): void
54+
55+
interface InitResp {
56+
code: number
57+
json: object
58+
body: string
59+
base64: string
60+
headers: any
61+
stop: boolean
62+
}
63+
64+
/**
65+
* @deprecated since version 2.0
66+
* Initiates chunked response. As an option, a custom response object may be sent at this point. For list-functions only!
67+
* @param {Object} init_resp - InitResp object
68+
* @returns {void}
69+
*/
70+
// eslint-disable-next-line
71+
declare function start(init_resp?: InitResp): void
72+
73+
/**
74+
* Sum arr’s items.
75+
* @param {number[]} arr - Array of numbers
76+
* @returns {number}
77+
*/
78+
declare function sum(arr: number[]): number
79+
80+
/**
81+
* Encodes obj to JSON string. This is an alias for the JSON.stringify method.
82+
* @param {any} obj - Array of numbers
83+
* @returns {string}
84+
*/
85+
declare function toJSON(obj: any): string
86+
87+
/**
88+
* Reduce functions take two required arguments of keys and values lists - the result of the related map function - and an optional third value which indicates if rereduce mode is active or not.
89+
* Rereduce is used for additional reduce values list, so when it is true there is no information about related keys (first argument is null).
90+
* @param {string[] | null} keys - Array of pairs of docid-key for related map function results. Always null if rereduce is running (has true value).
91+
* @returns {void}
92+
*/
93+
declare type redfun = (keys: [string, string][] | null, values: any[], rereduce?: boolean) => any

src/db/design.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
export interface Design<T> {
2+
_id: string
3+
_rev?: string
4+
language: 'javascript'
5+
views?: {
6+
[key: string]: {
7+
/**
8+
* Map functions accept a single document as the argument and (optionally) emit() key/value pairs that are stored in a view.
9+
* Since version 1.1.0, map supports CommonJS modules and the require() function.
10+
* @param {Object} doc - The document that is being processed
11+
* @returns {void}
12+
*/
13+
map?(doc: T): void
14+
/**
15+
* Reduce functions take two required arguments of keys and values lists - the result of the related map function - and an optional third value which indicates if rereduce mode is active or not.
16+
* Rereduce is used for additional reduce values list, so when it is true there is no information about related keys (first argument is null).
17+
* @param {string[] | null} keys - Array of pairs of docid-key for related map function results. Always null if rereduce is running (has true value).
18+
* @returns {any}
19+
*/
20+
reduce?(keys: [string, string][] | null, values: any[], rereduce?: boolean): any
21+
}
22+
}
23+
shows?: {
24+
[key: string]: () => void
25+
}
26+
lists?: {
27+
[key: string]: () => void
28+
}
29+
updates?: {
30+
[key: string]: () => void
31+
}
32+
filters?: {
33+
[key: string]: () => void
34+
}
35+
validate_doc_update?: {
36+
[key: string]: () => void
37+
}
38+
}
39+
40+
export default Design

src/db/tsconfig.json

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// we need this custom tsconfig.json to handle CouchDB Javascript Context locally to this folder
2+
{
3+
"extends": "../../tsconfig.json",
4+
"include": [
5+
"."
6+
],
7+
"exclude": [
8+
"node_modules",
9+
],
10+
"compilerOptions": {
11+
"typeRoots": [
12+
"../../node_modules/@types",
13+
"./context.d.ts"
14+
],
15+
"outDir": "../../dist/db",
16+
}
17+
}

tsconfig.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
],
55
"exclude": [
66
"node_modules",
7-
"dist"
7+
"dist",
8+
"src/db"
89
],
910
"compilerOptions": {
1011
"typeRoots": [

0 commit comments

Comments
 (0)