@frogfish/k2db 1.0.1
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/LICENSE +674 -0
- package/README.md +1 -0
- package/config.d.ts +0 -0
- package/config.js +1 -0
- package/db.d.ts +181 -0
- package/db.js +566 -0
- package/package.json +15 -0
package/db.d.ts
ADDED
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
import { ObjectId } from "mongodb";
|
|
2
|
+
export interface HostConfig {
|
|
3
|
+
host: string;
|
|
4
|
+
port?: number;
|
|
5
|
+
}
|
|
6
|
+
export interface DatabaseConfig {
|
|
7
|
+
name: string;
|
|
8
|
+
user?: string;
|
|
9
|
+
password?: string;
|
|
10
|
+
hosts?: HostConfig[];
|
|
11
|
+
replicaset?: string;
|
|
12
|
+
}
|
|
13
|
+
export interface BaseDocument {
|
|
14
|
+
_id?: ObjectId;
|
|
15
|
+
_uuid: string;
|
|
16
|
+
_created: number;
|
|
17
|
+
_updated: number;
|
|
18
|
+
_owner: string;
|
|
19
|
+
_deleted?: boolean;
|
|
20
|
+
[key: string]: any;
|
|
21
|
+
}
|
|
22
|
+
export declare class DB {
|
|
23
|
+
private conf;
|
|
24
|
+
private db;
|
|
25
|
+
private connection;
|
|
26
|
+
constructor(conf: DatabaseConfig);
|
|
27
|
+
/**
|
|
28
|
+
* Initializes the MongoDB connection.
|
|
29
|
+
*/
|
|
30
|
+
init(): Promise<void>;
|
|
31
|
+
/**
|
|
32
|
+
* Retrieves a collection from the database.
|
|
33
|
+
* @param collectionName - Name of the collection.
|
|
34
|
+
*/
|
|
35
|
+
private getCollection;
|
|
36
|
+
get(collectionName: string, uuid: string): Promise<BaseDocument>;
|
|
37
|
+
/**
|
|
38
|
+
* Retrieves a single document by UUID.
|
|
39
|
+
* @param collectionName - Name of the collection.
|
|
40
|
+
* @param uuid - UUID of the document.
|
|
41
|
+
* @param objectTypeName - Optional object type name.
|
|
42
|
+
* @param fields - Optional array of fields to include.
|
|
43
|
+
*/
|
|
44
|
+
findOne(collectionName: string, criteria: any, fields?: Array<string>): Promise<BaseDocument | null>;
|
|
45
|
+
/**
|
|
46
|
+
* Finds documents based on parameters with pagination support.
|
|
47
|
+
* @param collectionName - Name of the collection.
|
|
48
|
+
* @param filter - Criteria to filter the documents.
|
|
49
|
+
* @param params - Optional search parameters (for sorting, including/excluding fields).
|
|
50
|
+
* @param skip - Number of documents to skip (for pagination).
|
|
51
|
+
* @param limit - Maximum number of documents to return.
|
|
52
|
+
*/
|
|
53
|
+
find(collectionName: string, filter: any, params?: any, skip?: number, limit?: number): Promise<BaseDocument[]>;
|
|
54
|
+
/**
|
|
55
|
+
* Aggregates documents based on criteria with pagination support.
|
|
56
|
+
* @param collectionName - Name of the collection.
|
|
57
|
+
* @param criteria - Aggregation pipeline criteria.
|
|
58
|
+
* @param skip - Number of documents to skip (for pagination).
|
|
59
|
+
* @param limit - Maximum number of documents to return.
|
|
60
|
+
*/
|
|
61
|
+
aggregate(collectionName: string, criteria: any[], skip?: number, limit?: number): Promise<BaseDocument[]>;
|
|
62
|
+
/**
|
|
63
|
+
* Creates a new document in the collection.
|
|
64
|
+
* @param collectionName - Name of the collection.
|
|
65
|
+
* @param owner - Owner of the document.
|
|
66
|
+
* @param data - Data to insert.
|
|
67
|
+
*/
|
|
68
|
+
create(collectionName: string, owner: string, data: Partial<BaseDocument>): Promise<{
|
|
69
|
+
id: string;
|
|
70
|
+
}>;
|
|
71
|
+
/**
|
|
72
|
+
* Updates multiple documents based on criteria.
|
|
73
|
+
* Can either replace the documents or patch them.
|
|
74
|
+
* @param collectionName - Name of the collection.
|
|
75
|
+
* @param criteria - Update criteria.
|
|
76
|
+
* @param values - Values to update or replace with.
|
|
77
|
+
* @param replace - If true, replaces the entire document (PUT), otherwise patches (PATCH).
|
|
78
|
+
*/
|
|
79
|
+
updateAll(collectionName: string, criteria: any, values: Partial<BaseDocument>, replace?: boolean): Promise<any>;
|
|
80
|
+
/**
|
|
81
|
+
* Updates a single document by UUID.
|
|
82
|
+
* Can either replace the document or patch it.
|
|
83
|
+
* @param collectionName - Name of the collection.
|
|
84
|
+
* @param id - UUID string to identify the document.
|
|
85
|
+
* @param data - Data to update or replace with.
|
|
86
|
+
* @param replace - If true, replaces the entire document (PUT), otherwise patches (PATCH).
|
|
87
|
+
* @param objectTypeName - Optional object type name.
|
|
88
|
+
*/
|
|
89
|
+
update(collectionName: string, id: string, data: Partial<BaseDocument>, replace?: boolean, objectTypeName?: string): Promise<any>;
|
|
90
|
+
/**
|
|
91
|
+
* Removes (soft deletes) multiple documents based on criteria.
|
|
92
|
+
* @param collectionName - Name of the collection.
|
|
93
|
+
* @param criteria - Removal criteria.
|
|
94
|
+
*/
|
|
95
|
+
deleteAll(collectionName: string, criteria: any): Promise<any>;
|
|
96
|
+
/**
|
|
97
|
+
* Removes (soft deletes) a single document by UUID.
|
|
98
|
+
* @param collectionName - Name of the collection.
|
|
99
|
+
* @param id - UUID of the document.
|
|
100
|
+
*/
|
|
101
|
+
delete(collectionName: string, id: string): Promise<{
|
|
102
|
+
id: string;
|
|
103
|
+
}>;
|
|
104
|
+
/**
|
|
105
|
+
* Permanently deletes a document that has been soft-deleted.
|
|
106
|
+
* @param collectionName - Name of the collection.
|
|
107
|
+
* @param id - UUID of the document.
|
|
108
|
+
*/
|
|
109
|
+
purge(collectionName: string, id: string): Promise<{
|
|
110
|
+
id: string;
|
|
111
|
+
}>;
|
|
112
|
+
/**
|
|
113
|
+
* Restores a soft-deleted document.
|
|
114
|
+
* @param collectionName - Name of the collection.
|
|
115
|
+
* @param criteria - Criteria to identify the document.
|
|
116
|
+
*/
|
|
117
|
+
restore(collectionName: string, criteria: any): Promise<{
|
|
118
|
+
status: string;
|
|
119
|
+
modified: number;
|
|
120
|
+
}>;
|
|
121
|
+
/**
|
|
122
|
+
* Counts documents based on criteria.
|
|
123
|
+
* @param collectionName - Name of the collection.
|
|
124
|
+
* @param criteria - Counting criteria.
|
|
125
|
+
*/
|
|
126
|
+
count(collectionName: string, criteria: any): Promise<{
|
|
127
|
+
count: number;
|
|
128
|
+
}>;
|
|
129
|
+
/**
|
|
130
|
+
* Drops an entire collection.
|
|
131
|
+
* @param collectionName - Name of the collection.
|
|
132
|
+
*/
|
|
133
|
+
drop(collectionName: string): Promise<{
|
|
134
|
+
status: string;
|
|
135
|
+
}>;
|
|
136
|
+
/**
|
|
137
|
+
* Sanitizes aggregation criteria.
|
|
138
|
+
* @param criteria - Aggregation stage criteria.
|
|
139
|
+
*/
|
|
140
|
+
private static sanitiseCriteria;
|
|
141
|
+
/**
|
|
142
|
+
* Optional: Executes a transaction with the provided operations.
|
|
143
|
+
* @param operations - A function that performs operations within a transaction session.
|
|
144
|
+
*/
|
|
145
|
+
executeTransaction(operations: (session: any) => Promise<void>): Promise<void>;
|
|
146
|
+
/**
|
|
147
|
+
* Optional: Creates an index on the specified collection.
|
|
148
|
+
* @param collectionName - Name of the collection.
|
|
149
|
+
* @param indexSpec - Specification of the index.
|
|
150
|
+
* @param options - Optional index options.
|
|
151
|
+
*/
|
|
152
|
+
createIndex(collectionName: string, indexSpec: any, options?: any): Promise<void>;
|
|
153
|
+
/**
|
|
154
|
+
* Releases the MongoDB connection.
|
|
155
|
+
*/
|
|
156
|
+
release(): Promise<void>;
|
|
157
|
+
/**
|
|
158
|
+
* Closes the MongoDB connection.
|
|
159
|
+
*/
|
|
160
|
+
close(): void;
|
|
161
|
+
/**
|
|
162
|
+
* Drops the entire database.
|
|
163
|
+
*/
|
|
164
|
+
dropDatabase(): Promise<void>;
|
|
165
|
+
/**
|
|
166
|
+
* Validates the MongoDB collection name.
|
|
167
|
+
* @param collectionName - The name of the collection to validate.
|
|
168
|
+
* @throws {K2Error} - If the collection name is invalid.
|
|
169
|
+
*/
|
|
170
|
+
private validateCollectionName;
|
|
171
|
+
/**
|
|
172
|
+
* Optional: Checks the health of the database connection.
|
|
173
|
+
*/
|
|
174
|
+
isHealthy(): Promise<boolean>;
|
|
175
|
+
/**
|
|
176
|
+
* Utility to normalize the error type.
|
|
177
|
+
* @param err - The caught error of type `unknown`.
|
|
178
|
+
* @returns A normalized error of type `Error`.
|
|
179
|
+
*/
|
|
180
|
+
private normalizeError;
|
|
181
|
+
}
|