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

Commit

Permalink
feat: add CouchDB JavaScript context
Browse files Browse the repository at this point in the history
implement #232
  • Loading branch information
fox1t committed Feb 28, 2020
1 parent c028cb0 commit b999929
Show file tree
Hide file tree
Showing 5 changed files with 153 additions and 1 deletion.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"scripts": {
"commit": "npx git-cz",
"build": "npm run clean && tsc -p ./tsconfig.json",
"build:designs": "tsc -p ./src/db/tsconfig.json",
"start": "node dist",
"clean": "rimraf dist",
"dev:db": "pouchdb-server --config ./db/config.json",
Expand Down
93 changes: 93 additions & 0 deletions src/db/context.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/**
* Emits a key-value pair for further processing by CouchDB after the map function is done.
* @param {string} key - The view key
* @param {any} value - The key’s associated value
* @returns {void}
*/
declare function emit(key: string, value: any): void
/**
* @deprecated since version 2.0
* Extracts the next row from a related view result.
* @returns {Object} - View result row
*/
declare function getRow<T>(): T

/**
* A helper function to check if the provided value is an Array.
* @param {Object} obj - Any JavaScript value
* @returns {boolean}
*/
declare function isArray<T>(obj: T): boolean

/**
* Log a message to the CouchDB log (at the INFO level).
* @param {string} message - Message to be logged
* @returns {void}
*/
declare function log(message: string): void

/**
* @deprecated since version 2.0
* Registers callable handler for specified MIME key.
* @param {string} key - MIME key previously defined by registerType()
* @param {Function} value - MIME type handler
* @returns {void}
*/
declare function provides(key: string, func: Function): void

/**
* @deprecated since version 2.0
* Registers list of MIME types by associated key.
* @param {string} key - MIME types
* @param {string[]} mimes - MMIME types enumeration
* @returns {void}
*/
declare function registerType(key: string, mimes: string[]): void

/**
* @deprecated since version 2.0
* Sends a single string chunk in response.
* @param {string} chunk - Text chunk
* @returns {void}
*/
declare function send(chunk: string): void

interface InitResp {
code: number
json: object
body: string
base64: string
headers: any
stop: boolean
}

/**
* @deprecated since version 2.0
* Initiates chunked response. As an option, a custom response object may be sent at this point. For list-functions only!
* @param {Object} init_resp - InitResp object
* @returns {void}
*/
// eslint-disable-next-line
declare function start(init_resp?: InitResp): void

/**
* Sum arr’s items.
* @param {number[]} arr - Array of numbers
* @returns {number}
*/
declare function sum(arr: number[]): number

/**
* Encodes obj to JSON string. This is an alias for the JSON.stringify method.
* @param {any} obj - Array of numbers
* @returns {string}
*/
declare function toJSON(obj: any): string

/**
* 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.
* Rereduce is used for additional reduce values list, so when it is true there is no information about related keys (first argument is null).
* @param {string[] | null} keys - Array of pairs of docid-key for related map function results. Always null if rereduce is running (has true value).
* @returns {void}
*/
declare type redfun = (keys: [string, string][] | null, values: any[], rereduce?: boolean) => any
40 changes: 40 additions & 0 deletions src/db/design.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
export interface Design<T> {
_id: string
_rev?: string
language: 'javascript'
views?: {
[key: string]: {
/**
* Map functions accept a single document as the argument and (optionally) emit() key/value pairs that are stored in a view.
* Since version 1.1.0, map supports CommonJS modules and the require() function.
* @param {Object} doc - The document that is being processed
* @returns {void}
*/
map?(doc: T): void
/**
* 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.
* Rereduce is used for additional reduce values list, so when it is true there is no information about related keys (first argument is null).
* @param {string[] | null} keys - Array of pairs of docid-key for related map function results. Always null if rereduce is running (has true value).
* @returns {any}
*/
reduce?(keys: [string, string][] | null, values: any[], rereduce?: boolean): any
}
}
shows?: {
[key: string]: () => void
}
lists?: {
[key: string]: () => void
}
updates?: {
[key: string]: () => void
}
filters?: {
[key: string]: () => void
}
validate_doc_update?: {
[key: string]: () => void
}
}

export default Design
17 changes: 17 additions & 0 deletions src/db/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// we need this custom tsconfig.json to handle CouchDB Javascript Context locally to this folder
{
"extends": "../../tsconfig.json",
"include": [
"."
],
"exclude": [
"node_modules",
],
"compilerOptions": {
"typeRoots": [
"../../node_modules/@types",
"./context.d.ts"
],
"outDir": "../../dist/db",
}
}
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
],
"exclude": [
"node_modules",
"dist"
"dist",
"src/db"
],
"compilerOptions": {
"typeRoots": [
Expand Down

0 comments on commit b999929

Please sign in to comment.