@frogfish/k2db 1.0.5 → 1.0.6

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.
Files changed (4) hide show
  1. package/package.json +31 -4
  2. package/data.d.ts +0 -92
  3. package/data.js +0 -118
  4. package/db.d.ts +0 -181
package/package.json CHANGED
@@ -1,11 +1,36 @@
1
1
  {
2
2
  "name": "@frogfish/k2db",
3
- "version": "1.0.5",
3
+ "version": "1.0.6",
4
4
  "description": "A data handling library for K2 applications.",
5
- "main": "data.js",
6
- "types": "data.d.ts",
5
+ "main": "data.js",
6
+ "types": "data.d.ts",
7
+ "scripts": {
8
+ "test": "jest",
9
+ "test:coverage": "jest --coverage",
10
+ "dist": "tsc && jest && gulp && npm publish --access public",
11
+ "build": "tsc",
12
+ "build:watch": "tsc --watch"
13
+ },
7
14
  "author": "El'Diablo",
8
15
  "license": "GPL-3.0-only",
16
+ "devDependencies": {
17
+ "@types/axios": "^0.9.36",
18
+ "@types/debug": "^4.1.12",
19
+ "@types/jest": "^29.5.13",
20
+ "@types/mongodb": "^4.0.6",
21
+ "@types/uuid": "^10.0.0",
22
+ "jest": "^29.7.0",
23
+ "mongodb-memory-server": "^10.0.1",
24
+ "ts-jest": "^29.2.5",
25
+ "ts-node": "^10.9.2",
26
+ "typescript": "^5.6.2",
27
+ "gulp": "^5.0.0",
28
+ "gulp-bump": "^3.2.0",
29
+ "gulp-clean": "^0.4.0",
30
+ "gulp-json-transform": "^0.5.0",
31
+ "gulp-tsc": "^1.3.2",
32
+ "gulp-typescript": "^6.0.0-alpha.1"
33
+ },
9
34
  "dependencies": {
10
35
  "@frogfish/k2error": "^1.0.1",
11
36
  "debug": "^4.3.7",
@@ -14,6 +39,8 @@
14
39
  },
15
40
  "files": [
16
41
  "data.d.ts",
17
- "db.d.ts"
42
+ "data.js",
43
+ "db.d.ts",
44
+ "db.js"
18
45
  ]
19
46
  }
package/data.d.ts DELETED
@@ -1,92 +0,0 @@
1
- import { DB, BaseDocument } from "./db";
2
- export declare class Data {
3
- private db;
4
- private owner;
5
- constructor(db: DB, owner: string);
6
- /**
7
- * Retrieves a single document by UUID.
8
- * @param collectionName - Name of the collection.
9
- * @param uuid - UUID of the document.
10
- */
11
- get(collectionName: string, uuid: string): Promise<BaseDocument>;
12
- /**
13
- * Retrieves a single document matching the criteria.
14
- * @param collectionName - Name of the collection.
15
- * @param criteria - Search criteria.
16
- * @param fields - Optional fields to include.
17
- */
18
- findOne(collectionName: string, criteria: any, fields?: Array<string>): Promise<BaseDocument | null>;
19
- /**
20
- * Finds documents based on filter with optional parameters and pagination.
21
- */
22
- find(collectionName: string, filter: any, params?: any, skip?: number, limit?: number): Promise<BaseDocument[]>;
23
- /**
24
- * Aggregates documents based on criteria with pagination support.
25
- */
26
- aggregate(collectionName: string, criteria: any[], skip?: number, limit?: number): Promise<BaseDocument[]>;
27
- /**
28
- * Creates a new document in the collection.
29
- */
30
- create(collectionName: string, data: Partial<BaseDocument>): Promise<{
31
- id: string;
32
- }>;
33
- /**
34
- * Updates multiple documents based on criteria.
35
- */
36
- updateAll(collectionName: string, criteria: any, values: Partial<BaseDocument>, replace?: boolean): Promise<any>;
37
- /**
38
- * Updates a single document by UUID.
39
- */
40
- update(collectionName: string, id: string, data: Partial<BaseDocument>, replace?: boolean, objectTypeName?: string): Promise<any>;
41
- /**
42
- * Removes (soft deletes) multiple documents based on criteria.
43
- */
44
- deleteAll(collectionName: string, criteria: any): Promise<any>;
45
- /**
46
- * Removes (soft deletes) a single document by UUID.
47
- */
48
- delete(collectionName: string, id: string): Promise<{
49
- id: string;
50
- }>;
51
- /**
52
- * Permanently deletes a document that has been soft-deleted.
53
- */
54
- purge(collectionName: string, id: string): Promise<{
55
- id: string;
56
- }>;
57
- /**
58
- * Restores a soft-deleted document.
59
- */
60
- restore(collectionName: string, criteria: any): Promise<{
61
- status: string;
62
- modified: number;
63
- }>;
64
- /**
65
- * Counts documents based on criteria.
66
- */
67
- count(collectionName: string, criteria: any): Promise<{
68
- count: number;
69
- }>;
70
- /**
71
- * Drops an entire collection.
72
- */
73
- drop(collectionName: string): Promise<{
74
- status: string;
75
- }>;
76
- /**
77
- * Executes a transaction with the provided operations.
78
- */
79
- executeTransaction(operations: (session: any) => Promise<void>): Promise<void>;
80
- /**
81
- * Creates an index on the specified collection.
82
- */
83
- createIndex(collectionName: string, indexSpec: any, options?: any): Promise<void>;
84
- /**
85
- * Drops the entire database.
86
- */
87
- dropDatabase(): Promise<void>;
88
- /**
89
- * Checks the health of the database connection.
90
- */
91
- isHealthy(): Promise<boolean>;
92
- }
package/data.js DELETED
@@ -1,118 +0,0 @@
1
- "use strict";
2
- // src/Data.ts
3
- Object.defineProperty(exports, "__esModule", { value: true });
4
- exports.Data = void 0;
5
- class Data {
6
- constructor(db, owner) {
7
- this.db = db;
8
- this.owner = owner;
9
- }
10
- /**
11
- * Retrieves a single document by UUID.
12
- * @param collectionName - Name of the collection.
13
- * @param uuid - UUID of the document.
14
- */
15
- async get(collectionName, uuid) {
16
- return this.db.get(collectionName, uuid);
17
- }
18
- /**
19
- * Retrieves a single document matching the criteria.
20
- * @param collectionName - Name of the collection.
21
- * @param criteria - Search criteria.
22
- * @param fields - Optional fields to include.
23
- */
24
- async findOne(collectionName, criteria, fields) {
25
- return this.db.findOne(collectionName, criteria, fields);
26
- }
27
- /**
28
- * Finds documents based on filter with optional parameters and pagination.
29
- */
30
- async find(collectionName, filter, params, skip, limit) {
31
- return this.db.find(collectionName, filter, params, skip, limit);
32
- }
33
- /**
34
- * Aggregates documents based on criteria with pagination support.
35
- */
36
- async aggregate(collectionName, criteria, skip, limit) {
37
- return this.db.aggregate(collectionName, criteria, skip, limit);
38
- }
39
- /**
40
- * Creates a new document in the collection.
41
- */
42
- async create(collectionName, data) {
43
- return this.db.create(collectionName, this.owner, data);
44
- }
45
- /**
46
- * Updates multiple documents based on criteria.
47
- */
48
- async updateAll(collectionName, criteria, values, replace) {
49
- return this.db.updateAll(collectionName, criteria, values, replace);
50
- }
51
- /**
52
- * Updates a single document by UUID.
53
- */
54
- async update(collectionName, id, data, replace, objectTypeName) {
55
- return this.db.update(collectionName, id, data, replace, objectTypeName);
56
- }
57
- /**
58
- * Removes (soft deletes) multiple documents based on criteria.
59
- */
60
- async deleteAll(collectionName, criteria) {
61
- return this.db.deleteAll(collectionName, criteria);
62
- }
63
- /**
64
- * Removes (soft deletes) a single document by UUID.
65
- */
66
- async delete(collectionName, id) {
67
- return this.db.delete(collectionName, id);
68
- }
69
- /**
70
- * Permanently deletes a document that has been soft-deleted.
71
- */
72
- async purge(collectionName, id) {
73
- return this.db.purge(collectionName, id);
74
- }
75
- /**
76
- * Restores a soft-deleted document.
77
- */
78
- async restore(collectionName, criteria) {
79
- return this.db.restore(collectionName, criteria);
80
- }
81
- /**
82
- * Counts documents based on criteria.
83
- */
84
- async count(collectionName, criteria) {
85
- return this.db.count(collectionName, criteria);
86
- }
87
- /**
88
- * Drops an entire collection.
89
- */
90
- async drop(collectionName) {
91
- return this.db.drop(collectionName);
92
- }
93
- /**
94
- * Executes a transaction with the provided operations.
95
- */
96
- async executeTransaction(operations) {
97
- return this.db.executeTransaction(operations);
98
- }
99
- /**
100
- * Creates an index on the specified collection.
101
- */
102
- async createIndex(collectionName, indexSpec, options) {
103
- return this.db.createIndex(collectionName, indexSpec, options);
104
- }
105
- /**
106
- * Drops the entire database.
107
- */
108
- async dropDatabase() {
109
- return this.db.dropDatabase();
110
- }
111
- /**
112
- * Checks the health of the database connection.
113
- */
114
- async isHealthy() {
115
- return this.db.isHealthy();
116
- }
117
- }
118
- exports.Data = Data;
package/db.d.ts DELETED
@@ -1,181 +0,0 @@
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
- validateCollectionName(collectionName: string): void;
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
- }