@decaf-ts/for-couchdb 0.10.0 → 0.12.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/cjs/adapter.cjs +38 -32
- package/lib/cjs/adapter.cjs.map +1 -0
- package/lib/cjs/constants.cjs +1 -0
- package/lib/cjs/constants.cjs.map +1 -0
- package/lib/cjs/decorators.cjs +1 -0
- package/lib/cjs/decorators.cjs.map +1 -0
- package/lib/cjs/errors.cjs +1 -0
- package/lib/cjs/errors.cjs.map +1 -0
- package/lib/cjs/index.cjs +2 -1
- package/lib/cjs/index.cjs.map +1 -0
- package/lib/cjs/indexes/generator.cjs +17 -16
- package/lib/cjs/indexes/generator.cjs.map +1 -0
- package/lib/cjs/indexes/index.cjs +1 -0
- package/lib/cjs/indexes/index.cjs.map +1 -0
- package/lib/cjs/metadata.cjs +1 -0
- package/lib/cjs/metadata.cjs.map +1 -0
- package/lib/cjs/query/Paginator.cjs +1 -0
- package/lib/cjs/query/Paginator.cjs.map +1 -0
- package/lib/cjs/query/Statement.cjs +37 -36
- package/lib/cjs/query/Statement.cjs.map +1 -0
- package/lib/cjs/query/constants.cjs +1 -0
- package/lib/cjs/query/constants.cjs.map +1 -0
- package/lib/cjs/query/index.cjs +1 -0
- package/lib/cjs/query/index.cjs.map +1 -0
- package/lib/cjs/query/translate.cjs +3 -2
- package/lib/cjs/query/translate.cjs.map +1 -0
- package/lib/cjs/repository.cjs +8 -7
- package/lib/cjs/repository.cjs.map +1 -0
- package/lib/cjs/types.cjs +1 -0
- package/lib/cjs/types.cjs.map +1 -0
- package/lib/cjs/utils.cjs +9 -8
- package/lib/cjs/utils.cjs.map +1 -0
- package/lib/cjs/views/generator.cjs +9 -8
- package/lib/cjs/views/generator.cjs.map +1 -0
- package/lib/cjs/views/index.cjs +1 -0
- package/lib/cjs/views/index.cjs.map +1 -0
- package/lib/cjs/views/types.cjs +1 -0
- package/lib/cjs/views/types.cjs.map +1 -0
- package/lib/esm/index.js +1 -1
- package/lib/types/adapter.d.cts +296 -0
- package/lib/types/adapter.d.mts +296 -0
- package/lib/types/constants.d.cts +42 -0
- package/lib/types/constants.d.mts +42 -0
- package/lib/types/decorators.d.cts +8 -0
- package/lib/types/decorators.d.mts +8 -0
- package/lib/types/errors.d.cts +21 -0
- package/lib/types/errors.d.mts +21 -0
- package/lib/types/index.d.cts +28 -0
- package/lib/types/index.d.mts +28 -0
- package/lib/types/index.d.ts +1 -1
- package/lib/types/indexes/generator.d.cts +51 -0
- package/lib/types/indexes/generator.d.mts +51 -0
- package/lib/types/indexes/index.d.cts +1 -0
- package/lib/types/indexes/index.d.mts +1 -0
- package/lib/types/metadata.d.cts +4 -0
- package/lib/types/metadata.d.mts +4 -0
- package/lib/types/query/Paginator.d.cts +105 -0
- package/lib/types/query/Paginator.d.mts +105 -0
- package/lib/types/query/Statement.d.cts +195 -0
- package/lib/types/query/Statement.d.mts +195 -0
- package/lib/types/query/constants.d.cts +47 -0
- package/lib/types/query/constants.d.mts +47 -0
- package/lib/types/query/index.d.cts +4 -0
- package/lib/types/query/index.d.mts +4 -0
- package/lib/types/query/translate.d.cts +34 -0
- package/lib/types/query/translate.d.mts +34 -0
- package/lib/types/repository.d.cts +39 -0
- package/lib/types/repository.d.mts +39 -0
- package/lib/types/types.d.cts +166 -0
- package/lib/types/types.d.mts +166 -0
- package/lib/types/utils.d.cts +112 -0
- package/lib/types/utils.d.mts +112 -0
- package/lib/types/views/generator.d.cts +13 -0
- package/lib/types/views/generator.d.mts +13 -0
- package/lib/types/views/index.d.cts +2 -0
- package/lib/types/views/index.d.mts +2 -0
- package/lib/types/views/types.d.cts +30 -0
- package/lib/types/views/types.d.mts +30 -0
- package/package.json +4 -4
- package/lib/cjs/adapter.js.map +0 -1
- package/lib/cjs/constants.js.map +0 -1
- package/lib/cjs/decorators.js.map +0 -1
- package/lib/cjs/errors.js.map +0 -1
- package/lib/cjs/index.js.map +0 -1
- package/lib/cjs/indexes/generator.js.map +0 -1
- package/lib/cjs/indexes/index.js.map +0 -1
- package/lib/cjs/metadata.js.map +0 -1
- package/lib/cjs/query/Paginator.js.map +0 -1
- package/lib/cjs/query/Statement.js.map +0 -1
- package/lib/cjs/query/constants.js.map +0 -1
- package/lib/cjs/query/index.js.map +0 -1
- package/lib/cjs/query/translate.js.map +0 -1
- package/lib/cjs/repository.js.map +0 -1
- package/lib/cjs/types.js.map +0 -1
- package/lib/cjs/utils.js.map +0 -1
- package/lib/cjs/views/generator.js.map +0 -1
- package/lib/cjs/views/index.js.map +0 -1
- package/lib/cjs/views/types.js.map +0 -1
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
import { Adapter, AdapterFlags, Condition, ContextOf, SelectSelector, Statement, UnsupportedError, ViewKind } from "@decaf-ts/core";
|
|
2
|
+
import { MangoQuery, MangoSelector, ViewResponse } from "../types.js";
|
|
3
|
+
import { Model } from "@decaf-ts/decorator-validation";
|
|
4
|
+
import { CouchDBViewMetadata } from "../views/types.js";
|
|
5
|
+
import { CouchDBAdapter } from "../adapter.js";
|
|
6
|
+
type CouchDBViewDescriptor = {
|
|
7
|
+
ddoc: string;
|
|
8
|
+
view: string;
|
|
9
|
+
options: Record<string, any>;
|
|
10
|
+
};
|
|
11
|
+
type CouchDBAggregateInfo = {
|
|
12
|
+
kind: ViewKind;
|
|
13
|
+
meta: CouchDBViewMetadata;
|
|
14
|
+
descriptor: CouchDBViewDescriptor;
|
|
15
|
+
countDistinct?: boolean;
|
|
16
|
+
} | {
|
|
17
|
+
kind: "avg";
|
|
18
|
+
attribute: string;
|
|
19
|
+
sumDescriptor: CouchDBViewDescriptor;
|
|
20
|
+
countDescriptor: CouchDBViewDescriptor;
|
|
21
|
+
};
|
|
22
|
+
/**
|
|
23
|
+
* @description Statement builder for CouchDB Mango queries
|
|
24
|
+
* @summary Provides a fluent interface for building CouchDB Mango queries with type safety
|
|
25
|
+
* @template M - The model type that extends Model
|
|
26
|
+
* @template R - The result type
|
|
27
|
+
* @param adapter - The CouchDB adapter
|
|
28
|
+
* @class CouchDBStatement
|
|
29
|
+
* @example
|
|
30
|
+
* // Example of using CouchDBStatement
|
|
31
|
+
* const adapter = new MyCouchDBAdapter(scope);
|
|
32
|
+
* const statement = new CouchDBStatement<User, User[]>(adapter);
|
|
33
|
+
*
|
|
34
|
+
* // Build a query
|
|
35
|
+
* const users = await statement
|
|
36
|
+
* .from(User)
|
|
37
|
+
* .where(Condition.attribute<User>('age').gt(18))
|
|
38
|
+
* .orderBy('lastName', 'asc')
|
|
39
|
+
* .limit(10)
|
|
40
|
+
* .execute();
|
|
41
|
+
*/
|
|
42
|
+
export declare class CouchDBStatement<M extends Model, A extends Adapter<any, any, MangoQuery, any>, R> extends Statement<M, A, R, MangoQuery> {
|
|
43
|
+
protected manualAggregation?: CouchDBAggregateInfo;
|
|
44
|
+
protected attributeTypeCache: Map<string, string | undefined>;
|
|
45
|
+
constructor(adapter: A, overrides?: Partial<AdapterFlags>);
|
|
46
|
+
/**
|
|
47
|
+
* @description Builds a CouchDB Mango query from the statement
|
|
48
|
+
* @summary Converts the statement's conditions, selectors, and options into a CouchDB Mango query
|
|
49
|
+
* @return {MangoQuery} The built Mango query
|
|
50
|
+
* @throws {Error} If there are invalid query conditions
|
|
51
|
+
* @mermaid
|
|
52
|
+
* sequenceDiagram
|
|
53
|
+
* participant Statement
|
|
54
|
+
* participant Repository
|
|
55
|
+
* participant parseCondition
|
|
56
|
+
*
|
|
57
|
+
* Statement->>Statement: build()
|
|
58
|
+
* Note over Statement: Initialize selectors
|
|
59
|
+
* Statement->>Repository: Get table name
|
|
60
|
+
* Repository-->>Statement: Return table name
|
|
61
|
+
* Statement->>Statement: Create base query
|
|
62
|
+
*
|
|
63
|
+
* alt Has selectSelector
|
|
64
|
+
* Statement->>Statement: Add fields to query
|
|
65
|
+
* end
|
|
66
|
+
*
|
|
67
|
+
* alt Has whereCondition
|
|
68
|
+
* Statement->>Statement: Create combined condition with table
|
|
69
|
+
* Statement->>parseCondition: Parse condition
|
|
70
|
+
* parseCondition-->>Statement: Return parsed condition
|
|
71
|
+
*
|
|
72
|
+
* alt Is group operator
|
|
73
|
+
* alt Is AND operator
|
|
74
|
+
* Statement->>Statement: Flatten nested AND conditions
|
|
75
|
+
* else Is OR operator
|
|
76
|
+
* Statement->>Statement: Combine with table condition
|
|
77
|
+
* else
|
|
78
|
+
* Statement->>Statement: Throw error
|
|
79
|
+
* end
|
|
80
|
+
* else
|
|
81
|
+
* Statement->>Statement: Merge conditions with existing selector
|
|
82
|
+
* end
|
|
83
|
+
* end
|
|
84
|
+
*
|
|
85
|
+
* alt Has orderBySelector
|
|
86
|
+
* Statement->>Statement: Add sort to query
|
|
87
|
+
* Statement->>Statement: Ensure field exists in selector
|
|
88
|
+
* end
|
|
89
|
+
*
|
|
90
|
+
* alt Has limitSelector
|
|
91
|
+
* Statement->>Statement: Set limit
|
|
92
|
+
* else
|
|
93
|
+
* Statement->>Statement: Use default limit
|
|
94
|
+
* end
|
|
95
|
+
*
|
|
96
|
+
* alt Has offsetSelector
|
|
97
|
+
* Statement->>Statement: Set skip
|
|
98
|
+
* end
|
|
99
|
+
*
|
|
100
|
+
* Statement-->>Statement: Return query
|
|
101
|
+
*/
|
|
102
|
+
protected build(): MangoQuery;
|
|
103
|
+
protected attachDefaultQueryIndex(query: MangoQuery): void;
|
|
104
|
+
protected collectStartsWithRangeAttrs(selector: MangoSelector): Set<string>;
|
|
105
|
+
/**
|
|
106
|
+
* @description Processes a record from CouchDB
|
|
107
|
+
* @summary Extracts the ID from a CouchDB document and reverts it to a model instance
|
|
108
|
+
* @param {any} r - The raw record from CouchDB
|
|
109
|
+
* @param pkAttr - The primary key attribute of the model
|
|
110
|
+
* @param {"Number" | "BigInt" | undefined} sequenceType - The type of the sequence
|
|
111
|
+
* @return {any} The processed record
|
|
112
|
+
*/
|
|
113
|
+
protected processRecord(record: any, ctx: ContextOf<A>): M;
|
|
114
|
+
/**
|
|
115
|
+
* @description Executes a raw Mango query
|
|
116
|
+
* @summary Sends a raw Mango query to CouchDB and processes the results
|
|
117
|
+
* @template R - The result type
|
|
118
|
+
* @param {MangoQuery} rawInput - The raw Mango query to execute
|
|
119
|
+
* @return {Promise<R>} A promise that resolves to the query results
|
|
120
|
+
*/
|
|
121
|
+
raw<R>(rawInput: MangoQuery, ...args: any[]): Promise<R>;
|
|
122
|
+
/**
|
|
123
|
+
* @description Parses a condition into a CouchDB Mango query selector
|
|
124
|
+
* @summary Converts a Condition object into a CouchDB Mango query selector structure
|
|
125
|
+
* @param {Condition<M>} condition - The condition to parse
|
|
126
|
+
* @return {MangoQuery} The Mango query with the parsed condition as its selector
|
|
127
|
+
* @mermaid
|
|
128
|
+
* sequenceDiagram
|
|
129
|
+
* participant Statement
|
|
130
|
+
* participant translateOperators
|
|
131
|
+
* participant merge
|
|
132
|
+
*
|
|
133
|
+
* Statement->>Statement: parseCondition(condition)
|
|
134
|
+
*
|
|
135
|
+
* Note over Statement: Extract condition parts
|
|
136
|
+
*
|
|
137
|
+
* alt Simple comparison operator
|
|
138
|
+
* Statement->>translateOperators: translateOperators(operator)
|
|
139
|
+
* translateOperators-->>Statement: Return CouchDB operator
|
|
140
|
+
* Statement->>Statement: Create selector with attribute and operator
|
|
141
|
+
* else NOT operator
|
|
142
|
+
* Statement->>Statement: parseCondition(attr1)
|
|
143
|
+
* Statement->>translateOperators: translateOperators(Operator.NOT)
|
|
144
|
+
* translateOperators-->>Statement: Return CouchDB NOT operator
|
|
145
|
+
* Statement->>Statement: Create negated selector
|
|
146
|
+
* else AND/OR operator
|
|
147
|
+
* Statement->>Statement: parseCondition(attr1)
|
|
148
|
+
* Statement->>Statement: parseCondition(comparison)
|
|
149
|
+
* Statement->>translateOperators: translateOperators(operator)
|
|
150
|
+
* translateOperators-->>Statement: Return CouchDB group operator
|
|
151
|
+
* Statement->>merge: merge(operator, op1, op2)
|
|
152
|
+
* merge-->>Statement: Return merged selector
|
|
153
|
+
* end
|
|
154
|
+
*
|
|
155
|
+
* Statement-->>Statement: Return query with selector
|
|
156
|
+
*/
|
|
157
|
+
protected buildAggregateInfo(): CouchDBAggregateInfo | undefined;
|
|
158
|
+
protected createAggregateDescriptor(kind: ViewKind, attribute?: string): Extract<CouchDBAggregateInfo, {
|
|
159
|
+
kind: ViewKind;
|
|
160
|
+
}> | undefined;
|
|
161
|
+
protected createAggregateQuery(info: CouchDBAggregateInfo): MangoQuery & {
|
|
162
|
+
aggregate: true;
|
|
163
|
+
aggregateInfo: CouchDBAggregateInfo;
|
|
164
|
+
};
|
|
165
|
+
protected shouldUseManualAggregation(): boolean;
|
|
166
|
+
protected executeAggregate<R>(info: CouchDBAggregateInfo, ctx: ContextOf<A>): Promise<R>;
|
|
167
|
+
protected handleAverage<R>(info: CouchDBAggregateInfo, ctx: ContextOf<A>): Promise<R>;
|
|
168
|
+
protected executeManualAggregation<R>(docs: any[], info: CouchDBAggregateInfo, ctx: ContextOf<A>): R;
|
|
169
|
+
protected computeCount<R>(docs: any[], attribute?: string): R;
|
|
170
|
+
protected computeDistinctCount<R>(docs: any[], attribute?: string): R;
|
|
171
|
+
protected computeDistinctValues<R>(docs: any[], attribute?: string): R;
|
|
172
|
+
protected computeSum<R>(docs: any[], attribute?: string): R;
|
|
173
|
+
protected computeAverage<R>(docs: any[], attribute?: string): R;
|
|
174
|
+
protected computeMinMax<R>(docs: any[], attribute: string | undefined, mode: "min" | "max"): R;
|
|
175
|
+
protected computeGroupBy<R>(docs: any[], attribute: string): R;
|
|
176
|
+
protected groupSelectResults(docs: any[]): Record<string, any[]>;
|
|
177
|
+
protected collectValues(docs: any[], attribute: string): any[];
|
|
178
|
+
protected convertValueByAttribute(attribute: string, value: any): any;
|
|
179
|
+
protected getAttributeType(attribute?: string): string | undefined;
|
|
180
|
+
protected normalizeMetaType(metaType: any): string | undefined;
|
|
181
|
+
protected normalizeComparable(value: any): number | null;
|
|
182
|
+
protected groupKey(value: any): string;
|
|
183
|
+
protected toNumericValue(value: any, field: string, context: string): number;
|
|
184
|
+
protected convertAggregateValue(attribute: string | undefined, value: any): any;
|
|
185
|
+
protected resolveSelectorAttribute(selector?: SelectSelector<M> | null): string | undefined;
|
|
186
|
+
protected missingDecorator(kind: ViewKind | "avg", attribute?: string): UnsupportedError;
|
|
187
|
+
protected decoratorForKind(kind: ViewKind | "avg"): string;
|
|
188
|
+
protected processViewResponse<R>(info: CouchDBAggregateInfo, response: ViewResponse): R;
|
|
189
|
+
protected isViewAggregate(info: CouchDBAggregateInfo): info is Extract<CouchDBAggregateInfo, {
|
|
190
|
+
kind: ViewKind;
|
|
191
|
+
}>;
|
|
192
|
+
protected getCouchAdapter(): CouchDBAdapter<any, any, any>;
|
|
193
|
+
protected parseCondition(condition: Condition<M>): MangoQuery;
|
|
194
|
+
}
|
|
195
|
+
export {};
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { MangoOperator } from "../types.cjs";
|
|
2
|
+
/**
|
|
3
|
+
* @description Default query limit for CouchDB queries
|
|
4
|
+
* @summary Maximum number of documents to return in a single query
|
|
5
|
+
* @const CouchDBQueryLimit
|
|
6
|
+
* @memberOf module:for-couchdb
|
|
7
|
+
*/
|
|
8
|
+
export declare const CouchDBQueryLimit = 250;
|
|
9
|
+
/**
|
|
10
|
+
* @description Mapping of operator names to CouchDB Mango query operators
|
|
11
|
+
* @summary Constants for CouchDB comparison operators used in Mango queries
|
|
12
|
+
* @typedef {Object} CouchDBOperatorType
|
|
13
|
+
* @property {string} EQUAL - Equality operator ($eq)
|
|
14
|
+
* @property {string} DIFFERENT - Inequality operator ($ne)
|
|
15
|
+
* @property {string} BIGGER - Greater than operator ($gt)
|
|
16
|
+
* @property {string} BIGGER_EQ - Greater than or equal operator ($gte)
|
|
17
|
+
* @property {string} SMALLER - Less than operator ($lt)
|
|
18
|
+
* @property {string} SMALLER_EQ - Less than or equal operator ($lte)
|
|
19
|
+
* @property {string} NOT - Negation operator ($not)
|
|
20
|
+
* @property {string} IN - In array operator ($in)
|
|
21
|
+
* @property {string} REGEXP - Regular expression operator ($regex)
|
|
22
|
+
* @const CouchDBOperator
|
|
23
|
+
* @type {CouchDBOperatorType}
|
|
24
|
+
* @memberOf module:for-couchdb
|
|
25
|
+
*/
|
|
26
|
+
export declare const CouchDBOperator: Record<string, MangoOperator>;
|
|
27
|
+
/**
|
|
28
|
+
* @description Mapping of logical operator names to CouchDB Mango query operators
|
|
29
|
+
* @summary Constants for CouchDB logical operators used in Mango queries
|
|
30
|
+
* @typedef {Object} CouchDBGroupOperatorType
|
|
31
|
+
* @property {string} AND - Logical AND operator ($and)
|
|
32
|
+
* @property {string} OR - Logical OR operator ($or)
|
|
33
|
+
* @const CouchDBGroupOperator
|
|
34
|
+
* @type {CouchDBGroupOperatorType}
|
|
35
|
+
* @memberOf module:for-couchdb
|
|
36
|
+
*/
|
|
37
|
+
export declare const CouchDBGroupOperator: Record<string, MangoOperator>;
|
|
38
|
+
/**
|
|
39
|
+
* @description Special constant values used in CouchDB queries
|
|
40
|
+
* @summary String constants representing special values in CouchDB
|
|
41
|
+
* @typedef {Object} CouchDBConstType
|
|
42
|
+
* @property {string} NULL - String representation of null value
|
|
43
|
+
* @const CouchDBConst
|
|
44
|
+
* @type {CouchDBConstType}
|
|
45
|
+
* @memberOf module:for-couchdb
|
|
46
|
+
*/
|
|
47
|
+
export declare const CouchDBConst: Record<string, string>;
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { MangoOperator } from "../types.js";
|
|
2
|
+
/**
|
|
3
|
+
* @description Default query limit for CouchDB queries
|
|
4
|
+
* @summary Maximum number of documents to return in a single query
|
|
5
|
+
* @const CouchDBQueryLimit
|
|
6
|
+
* @memberOf module:for-couchdb
|
|
7
|
+
*/
|
|
8
|
+
export declare const CouchDBQueryLimit = 250;
|
|
9
|
+
/**
|
|
10
|
+
* @description Mapping of operator names to CouchDB Mango query operators
|
|
11
|
+
* @summary Constants for CouchDB comparison operators used in Mango queries
|
|
12
|
+
* @typedef {Object} CouchDBOperatorType
|
|
13
|
+
* @property {string} EQUAL - Equality operator ($eq)
|
|
14
|
+
* @property {string} DIFFERENT - Inequality operator ($ne)
|
|
15
|
+
* @property {string} BIGGER - Greater than operator ($gt)
|
|
16
|
+
* @property {string} BIGGER_EQ - Greater than or equal operator ($gte)
|
|
17
|
+
* @property {string} SMALLER - Less than operator ($lt)
|
|
18
|
+
* @property {string} SMALLER_EQ - Less than or equal operator ($lte)
|
|
19
|
+
* @property {string} NOT - Negation operator ($not)
|
|
20
|
+
* @property {string} IN - In array operator ($in)
|
|
21
|
+
* @property {string} REGEXP - Regular expression operator ($regex)
|
|
22
|
+
* @const CouchDBOperator
|
|
23
|
+
* @type {CouchDBOperatorType}
|
|
24
|
+
* @memberOf module:for-couchdb
|
|
25
|
+
*/
|
|
26
|
+
export declare const CouchDBOperator: Record<string, MangoOperator>;
|
|
27
|
+
/**
|
|
28
|
+
* @description Mapping of logical operator names to CouchDB Mango query operators
|
|
29
|
+
* @summary Constants for CouchDB logical operators used in Mango queries
|
|
30
|
+
* @typedef {Object} CouchDBGroupOperatorType
|
|
31
|
+
* @property {string} AND - Logical AND operator ($and)
|
|
32
|
+
* @property {string} OR - Logical OR operator ($or)
|
|
33
|
+
* @const CouchDBGroupOperator
|
|
34
|
+
* @type {CouchDBGroupOperatorType}
|
|
35
|
+
* @memberOf module:for-couchdb
|
|
36
|
+
*/
|
|
37
|
+
export declare const CouchDBGroupOperator: Record<string, MangoOperator>;
|
|
38
|
+
/**
|
|
39
|
+
* @description Special constant values used in CouchDB queries
|
|
40
|
+
* @summary String constants representing special values in CouchDB
|
|
41
|
+
* @typedef {Object} CouchDBConstType
|
|
42
|
+
* @property {string} NULL - String representation of null value
|
|
43
|
+
* @const CouchDBConst
|
|
44
|
+
* @type {CouchDBConstType}
|
|
45
|
+
* @memberOf module:for-couchdb
|
|
46
|
+
*/
|
|
47
|
+
export declare const CouchDBConst: Record<string, string>;
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { GroupOperator, Operator } from "@decaf-ts/core";
|
|
2
|
+
import { MangoOperator } from "../types.cjs";
|
|
3
|
+
/**
|
|
4
|
+
* @description Translates core operators to CouchDB Mango operators
|
|
5
|
+
* @summary Converts Decaf.ts core operators to their equivalent CouchDB Mango query operators
|
|
6
|
+
* @param {GroupOperator | Operator} operator - The core operator to translate
|
|
7
|
+
* @return {MangoOperator} The equivalent CouchDB Mango operator
|
|
8
|
+
* @throws {QueryError} If no translation exists for the given operator
|
|
9
|
+
* @function translateOperators
|
|
10
|
+
* @memberOf module:for-couchdb
|
|
11
|
+
* @mermaid
|
|
12
|
+
* sequenceDiagram
|
|
13
|
+
* participant Caller
|
|
14
|
+
* participant translateOperators
|
|
15
|
+
* participant CouchDBOperator
|
|
16
|
+
* participant CouchDBGroupOperator
|
|
17
|
+
*
|
|
18
|
+
* Caller->>translateOperators: operator
|
|
19
|
+
*
|
|
20
|
+
* translateOperators->>CouchDBOperator: Check for match
|
|
21
|
+
* alt Found in CouchDBOperator
|
|
22
|
+
* CouchDBOperator-->>translateOperators: Return matching operator
|
|
23
|
+
* translateOperators-->>Caller: Return MangoOperator
|
|
24
|
+
* else Not found
|
|
25
|
+
* translateOperators->>CouchDBGroupOperator: Check for match
|
|
26
|
+
* alt Found in CouchDBGroupOperator
|
|
27
|
+
* CouchDBGroupOperator-->>translateOperators: Return matching operator
|
|
28
|
+
* translateOperators-->>Caller: Return MangoOperator
|
|
29
|
+
* else Not found
|
|
30
|
+
* translateOperators-->>Caller: Throw QueryError
|
|
31
|
+
* end
|
|
32
|
+
* end
|
|
33
|
+
*/
|
|
34
|
+
export declare function translateOperators(operator: GroupOperator | Operator): MangoOperator;
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { GroupOperator, Operator } from "@decaf-ts/core";
|
|
2
|
+
import { MangoOperator } from "../types.js";
|
|
3
|
+
/**
|
|
4
|
+
* @description Translates core operators to CouchDB Mango operators
|
|
5
|
+
* @summary Converts Decaf.ts core operators to their equivalent CouchDB Mango query operators
|
|
6
|
+
* @param {GroupOperator | Operator} operator - The core operator to translate
|
|
7
|
+
* @return {MangoOperator} The equivalent CouchDB Mango operator
|
|
8
|
+
* @throws {QueryError} If no translation exists for the given operator
|
|
9
|
+
* @function translateOperators
|
|
10
|
+
* @memberOf module:for-couchdb
|
|
11
|
+
* @mermaid
|
|
12
|
+
* sequenceDiagram
|
|
13
|
+
* participant Caller
|
|
14
|
+
* participant translateOperators
|
|
15
|
+
* participant CouchDBOperator
|
|
16
|
+
* participant CouchDBGroupOperator
|
|
17
|
+
*
|
|
18
|
+
* Caller->>translateOperators: operator
|
|
19
|
+
*
|
|
20
|
+
* translateOperators->>CouchDBOperator: Check for match
|
|
21
|
+
* alt Found in CouchDBOperator
|
|
22
|
+
* CouchDBOperator-->>translateOperators: Return matching operator
|
|
23
|
+
* translateOperators-->>Caller: Return MangoOperator
|
|
24
|
+
* else Not found
|
|
25
|
+
* translateOperators->>CouchDBGroupOperator: Check for match
|
|
26
|
+
* alt Found in CouchDBGroupOperator
|
|
27
|
+
* CouchDBGroupOperator-->>translateOperators: Return matching operator
|
|
28
|
+
* translateOperators-->>Caller: Return MangoOperator
|
|
29
|
+
* else Not found
|
|
30
|
+
* translateOperators-->>Caller: Throw QueryError
|
|
31
|
+
* end
|
|
32
|
+
* end
|
|
33
|
+
*/
|
|
34
|
+
export declare function translateOperators(operator: GroupOperator | Operator): MangoOperator;
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { MaybeContextualArg, Repository } from "@decaf-ts/core";
|
|
2
|
+
import { Model } from "@decaf-ts/decorator-validation";
|
|
3
|
+
import { Constructor } from "@decaf-ts/decoration";
|
|
4
|
+
import type { CouchDBAdapter } from "./adapter.cjs";
|
|
5
|
+
import { ContextOf } from "@decaf-ts/core";
|
|
6
|
+
import type { PrimaryKeyType } from "@decaf-ts/db-decorators";
|
|
7
|
+
export declare class CouchDBRepository<M extends Model, A extends CouchDBAdapter<any, any, any>> extends Repository<M, A> {
|
|
8
|
+
constructor(adapter: A, model: Constructor<M>, force?: boolean);
|
|
9
|
+
protected assignMetadata(model: M, source?: M): M;
|
|
10
|
+
protected assignMetadata(models: M[], source?: M[]): M[];
|
|
11
|
+
create(model: M, ...args: MaybeContextualArg<ContextOf<A>>): Promise<M>;
|
|
12
|
+
createAll(models: M[], ...args: MaybeContextualArg<ContextOf<A>>): Promise<M[]>;
|
|
13
|
+
read(id: PrimaryKeyType, ...args: MaybeContextualArg<ContextOf<A>>): Promise<M>;
|
|
14
|
+
readAll(ids: PrimaryKeyType[], ...args: MaybeContextualArg<ContextOf<A>>): Promise<M[]>;
|
|
15
|
+
update(model: M, ...args: MaybeContextualArg<ContextOf<A>>): Promise<M>;
|
|
16
|
+
updateAll(models: M[], ...args: MaybeContextualArg<ContextOf<A>>): Promise<M[]>;
|
|
17
|
+
delete(id: PrimaryKeyType, ...args: MaybeContextualArg<ContextOf<A>>): Promise<M>;
|
|
18
|
+
deleteAll(ids: PrimaryKeyType[], ...args: MaybeContextualArg<ContextOf<A>>): Promise<M[]>;
|
|
19
|
+
/**
|
|
20
|
+
* @description Prepares a model for update.
|
|
21
|
+
* @summary Validates the model and prepares it for update in the database.
|
|
22
|
+
* @param {M} model - The model to update.
|
|
23
|
+
* @param {...any[]} args - Additional arguments.
|
|
24
|
+
* @return The prepared model and context arguments.
|
|
25
|
+
* @throws {InternalError} If the model has no primary key value.
|
|
26
|
+
* @throws {ValidationError} If the model fails validation.
|
|
27
|
+
*/
|
|
28
|
+
protected updatePrefix(model: M, ...args: MaybeContextualArg<ContextOf<A>>): Promise<[M, ...args: any[], ContextOf<A>, M | undefined]>;
|
|
29
|
+
/**
|
|
30
|
+
* @description Prepares multiple models for update.
|
|
31
|
+
* @summary Validates multiple models and prepares them for update in the database.
|
|
32
|
+
* @param {M[]} models - The models to update.
|
|
33
|
+
* @param {...any[]} args - Additional arguments.
|
|
34
|
+
* @return {Promise<any[]>} The prepared models and context arguments.
|
|
35
|
+
* @throws {InternalError} If any model has no primary key value.
|
|
36
|
+
* @throws {ValidationError} If any model fails validation.
|
|
37
|
+
*/
|
|
38
|
+
protected updateAllPrefix(models: M[], ...args: MaybeContextualArg<ContextOf<A>>): Promise<[M[], ...args: any[], ContextOf<A>, M[] | undefined]>;
|
|
39
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { MaybeContextualArg, Repository } from "@decaf-ts/core";
|
|
2
|
+
import { Model } from "@decaf-ts/decorator-validation";
|
|
3
|
+
import { Constructor } from "@decaf-ts/decoration";
|
|
4
|
+
import type { CouchDBAdapter } from "./adapter.js";
|
|
5
|
+
import { ContextOf } from "@decaf-ts/core";
|
|
6
|
+
import type { PrimaryKeyType } from "@decaf-ts/db-decorators";
|
|
7
|
+
export declare class CouchDBRepository<M extends Model, A extends CouchDBAdapter<any, any, any>> extends Repository<M, A> {
|
|
8
|
+
constructor(adapter: A, model: Constructor<M>, force?: boolean);
|
|
9
|
+
protected assignMetadata(model: M, source?: M): M;
|
|
10
|
+
protected assignMetadata(models: M[], source?: M[]): M[];
|
|
11
|
+
create(model: M, ...args: MaybeContextualArg<ContextOf<A>>): Promise<M>;
|
|
12
|
+
createAll(models: M[], ...args: MaybeContextualArg<ContextOf<A>>): Promise<M[]>;
|
|
13
|
+
read(id: PrimaryKeyType, ...args: MaybeContextualArg<ContextOf<A>>): Promise<M>;
|
|
14
|
+
readAll(ids: PrimaryKeyType[], ...args: MaybeContextualArg<ContextOf<A>>): Promise<M[]>;
|
|
15
|
+
update(model: M, ...args: MaybeContextualArg<ContextOf<A>>): Promise<M>;
|
|
16
|
+
updateAll(models: M[], ...args: MaybeContextualArg<ContextOf<A>>): Promise<M[]>;
|
|
17
|
+
delete(id: PrimaryKeyType, ...args: MaybeContextualArg<ContextOf<A>>): Promise<M>;
|
|
18
|
+
deleteAll(ids: PrimaryKeyType[], ...args: MaybeContextualArg<ContextOf<A>>): Promise<M[]>;
|
|
19
|
+
/**
|
|
20
|
+
* @description Prepares a model for update.
|
|
21
|
+
* @summary Validates the model and prepares it for update in the database.
|
|
22
|
+
* @param {M} model - The model to update.
|
|
23
|
+
* @param {...any[]} args - Additional arguments.
|
|
24
|
+
* @return The prepared model and context arguments.
|
|
25
|
+
* @throws {InternalError} If the model has no primary key value.
|
|
26
|
+
* @throws {ValidationError} If the model fails validation.
|
|
27
|
+
*/
|
|
28
|
+
protected updatePrefix(model: M, ...args: MaybeContextualArg<ContextOf<A>>): Promise<[M, ...args: any[], ContextOf<A>, M | undefined]>;
|
|
29
|
+
/**
|
|
30
|
+
* @description Prepares multiple models for update.
|
|
31
|
+
* @summary Validates multiple models and prepares them for update in the database.
|
|
32
|
+
* @param {M[]} models - The models to update.
|
|
33
|
+
* @param {...any[]} args - Additional arguments.
|
|
34
|
+
* @return {Promise<any[]>} The prepared models and context arguments.
|
|
35
|
+
* @throws {InternalError} If any model has no primary key value.
|
|
36
|
+
* @throws {ValidationError} If any model fails validation.
|
|
37
|
+
*/
|
|
38
|
+
protected updateAllPrefix(models: M[], ...args: MaybeContextualArg<ContextOf<A>>): Promise<[M[], ...args: any[], ContextOf<A>, M[] | undefined]>;
|
|
39
|
+
}
|
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @description Response from a CouchDB Mango query
|
|
3
|
+
* @summary Contains the matching documents and additional metadata about the query execution
|
|
4
|
+
* @interface MangoResponse
|
|
5
|
+
* @template D - The document type
|
|
6
|
+
* @memberOf module:for-couchdb
|
|
7
|
+
* @see Docs: {@link https://docs.couchdb.org/en/latest/api/database/find.html#db-find}
|
|
8
|
+
*/
|
|
9
|
+
export interface MangoResponse<D> {
|
|
10
|
+
/** Array of documents matching the search.
|
|
11
|
+
*
|
|
12
|
+
* In each matching document, the fields specified in the fields part of the request body are listed, along with
|
|
13
|
+
* their values. */
|
|
14
|
+
docs: (D & {
|
|
15
|
+
_id: string;
|
|
16
|
+
_rev: string;
|
|
17
|
+
})[];
|
|
18
|
+
/** A string that enables you to specify which page of results you require.
|
|
19
|
+
*
|
|
20
|
+
* Used for paging through result sets. */
|
|
21
|
+
bookmark?: string;
|
|
22
|
+
/** Execution warnings */
|
|
23
|
+
warning?: string;
|
|
24
|
+
/** Basic execution statistics for a specific request. */
|
|
25
|
+
execution_stats?: MangoExecutionStats;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* @description Statistics about the execution of a Mango query
|
|
29
|
+
* @summary Provides detailed metrics about query execution including document and key examination counts
|
|
30
|
+
* @interface MangoExecutionStats
|
|
31
|
+
* @memberOf module:for-couchdb
|
|
32
|
+
* @see Docs: {@link http://docs.couchdb.org/en/latest/api/database/find.html#execution-statistics}
|
|
33
|
+
*/
|
|
34
|
+
export interface MangoExecutionStats {
|
|
35
|
+
/** Number of index keys examined. Currently always 0. */
|
|
36
|
+
total_keys_examined: number;
|
|
37
|
+
/** Number of documents fetched from the database / index.
|
|
38
|
+
*
|
|
39
|
+
* Equivalent to using include_docs = true in a view. */
|
|
40
|
+
total_docs_examined: number;
|
|
41
|
+
/** Number of documents fetched from the database using an out-of-band document fetch.
|
|
42
|
+
*
|
|
43
|
+
* This is only non-zero when read quorum > 1 is specified in the query parameters. */
|
|
44
|
+
total_quorum_docs_examined: number;
|
|
45
|
+
/** Number of results returned from the query. */
|
|
46
|
+
results_returned: number;
|
|
47
|
+
/** Total execution time in milliseconds as measured by the database. */
|
|
48
|
+
execution_time_ms: number;
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* @description Parameters for creating a CouchDB Mango index
|
|
52
|
+
* @summary Defines the structure and configuration for a new Mango index
|
|
53
|
+
* @interface CreateIndexRequest
|
|
54
|
+
* @memberOf module:for-couchdb
|
|
55
|
+
* @see Docs: {@link http://docs.couchdb.org/en/latest/api/database/find.html#db-index}
|
|
56
|
+
*/
|
|
57
|
+
export interface CreateIndexRequest {
|
|
58
|
+
/** JSON object describing the index to create */
|
|
59
|
+
index: {
|
|
60
|
+
/** Array of field names following the sort syntax. */
|
|
61
|
+
fields: SortOrder[];
|
|
62
|
+
/** A selector to apply to documents at indexing time, creating a partial index. */
|
|
63
|
+
partial_filter_selector?: MangoSelector;
|
|
64
|
+
};
|
|
65
|
+
/** Name of the design document in which the index will be created. */
|
|
66
|
+
ddoc?: string;
|
|
67
|
+
/** Name of the index. If no name is provided, a name will be generated automatically. */
|
|
68
|
+
name?: string;
|
|
69
|
+
/** Can be "json" or "text".
|
|
70
|
+
*
|
|
71
|
+
* @default "json" */
|
|
72
|
+
type?: "json" | "text";
|
|
73
|
+
/** This field sets whether the created index will be a partitioned or global index. */
|
|
74
|
+
partitioned?: boolean;
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* @description Represents the possible value types in a Mango query
|
|
78
|
+
* @summary Union type of all possible value types that can be used in Mango queries
|
|
79
|
+
* @typedef {(number|string|Date|boolean|object|null)} MangoValue
|
|
80
|
+
* @memberOf module:for-couchdb
|
|
81
|
+
*/
|
|
82
|
+
export type MangoValue = number | string | Date | boolean | object | null;
|
|
83
|
+
/**
|
|
84
|
+
* @description Operators available in Mango queries
|
|
85
|
+
* @summary Union type of all possible operators that can be used in Mango queries
|
|
86
|
+
* @typedef {string} MangoOperator
|
|
87
|
+
* @memberOf module:for-couchdb
|
|
88
|
+
*/
|
|
89
|
+
export type MangoOperator = "$lt" | "$lte" | "$eq" | "$ne" | "$gte" | "$gt" | "$exists" | "$type" | "$in" | "$nin" | "$size" | "$mod" | "$regex" | "$or" | "$and" | "$nor" | "$not" | "$all" | "$allMatch" | "$elemMatch";
|
|
90
|
+
/**
|
|
91
|
+
* @description Represents a CouchDB Mango query selector
|
|
92
|
+
* @summary Type for defining query conditions in Mango queries
|
|
93
|
+
* @typedef {Object} MangoSelector
|
|
94
|
+
* @memberOf module:for-couchdb
|
|
95
|
+
* @see Docs: {@link http://docs.couchdb.org/en/latest/api/database/find.html#selector-syntax}
|
|
96
|
+
*/
|
|
97
|
+
export type MangoSelector = {
|
|
98
|
+
[K in MangoOperator | string]: MangoSelector | MangoSelector[] | MangoValue | MangoValue[];
|
|
99
|
+
};
|
|
100
|
+
/**
|
|
101
|
+
* @description Represents a sort order specification in Mango queries
|
|
102
|
+
* @summary Type for defining sort order in Mango queries
|
|
103
|
+
* @typedef {(string|string[]|Object)} SortOrder
|
|
104
|
+
* @memberOf module:for-couchdb
|
|
105
|
+
* @see Docs: {@link http://docs.couchdb.org/en/latest/api/database/find.html#sort-syntax}
|
|
106
|
+
*/
|
|
107
|
+
export type SortOrder = string | string[] | {
|
|
108
|
+
[key: string]: "asc" | "desc";
|
|
109
|
+
};
|
|
110
|
+
/**
|
|
111
|
+
* @description Represents a CouchDB Mango query
|
|
112
|
+
* @summary Interface for defining complete Mango queries with selectors, sorting, pagination, and other options
|
|
113
|
+
* @interface MangoQuery
|
|
114
|
+
* @memberOf module:for-couchdb
|
|
115
|
+
* @see Docs: {@link https://docs.couchdb.org/en/latest/api/database/find.html#db-find}
|
|
116
|
+
*/
|
|
117
|
+
export interface MangoQuery {
|
|
118
|
+
/** JSON object describing criteria used to select documents. */
|
|
119
|
+
selector: MangoSelector;
|
|
120
|
+
/** Maximum number of results returned. @default 25 */
|
|
121
|
+
limit?: number;
|
|
122
|
+
/** Skip the first 'n' results, where 'n' is the value specified. */
|
|
123
|
+
skip?: number;
|
|
124
|
+
/** JSON array following sort syntax. */
|
|
125
|
+
sort?: SortOrder[];
|
|
126
|
+
/** JSON array specifying which fields of each object should be returned.
|
|
127
|
+
*
|
|
128
|
+
* If it is omitted, the entire object is returned.
|
|
129
|
+
*
|
|
130
|
+
* @see Docs: {@link http://docs.couchdb.org/en/latest/api/database/find.html#filtering-fields} */
|
|
131
|
+
fields?: string[];
|
|
132
|
+
use_index?: string | [string, string];
|
|
133
|
+
/** Read quorum needed for the result.
|
|
134
|
+
*
|
|
135
|
+
* @default 1 */
|
|
136
|
+
r?: number;
|
|
137
|
+
/** A string that enables you to specify which page of results you require.
|
|
138
|
+
*
|
|
139
|
+
* Used for paging through result sets. */
|
|
140
|
+
bookmark?: string;
|
|
141
|
+
/** Whether to update the index prior to returning the result.
|
|
142
|
+
*
|
|
143
|
+
* @default true */
|
|
144
|
+
update?: boolean;
|
|
145
|
+
/** Whether or not the view results should be returned from a “stable” set of shards. */
|
|
146
|
+
stable?: boolean;
|
|
147
|
+
/** Combination of update = false and stable = true options.
|
|
148
|
+
*
|
|
149
|
+
* Possible options: "ok", false (default). */
|
|
150
|
+
stale?: "ok" | false;
|
|
151
|
+
/** Include execution statistics in the query response.
|
|
152
|
+
*
|
|
153
|
+
* Optional, default: false. */
|
|
154
|
+
execution_stats?: boolean;
|
|
155
|
+
}
|
|
156
|
+
export interface ViewRow<D = any> {
|
|
157
|
+
id?: string;
|
|
158
|
+
key?: any;
|
|
159
|
+
value?: any;
|
|
160
|
+
doc?: D;
|
|
161
|
+
}
|
|
162
|
+
export interface ViewResponse<D = any> {
|
|
163
|
+
total_rows?: number;
|
|
164
|
+
offset?: number;
|
|
165
|
+
rows: ViewRow<D>[];
|
|
166
|
+
}
|