@medyll/idae-api 0.121.1 → 0.123.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.
Files changed (3) hide show
  1. package/README.md +314 -314
  2. package/dist/idae-api.d.ts +18 -18
  3. package/package.json +2 -2
package/README.md CHANGED
@@ -1,314 +1,314 @@
1
- # @medyll/idae-api
2
-
3
- A flexible and extensible Node.js API, based on the [@medyll/idae-db](https://www.npmjs.com/package/@medyll/idae-db) library, allowing you to manage multiple types of databases (MongoDB, MySQL, etc.) and providing a complete TypeScript client to interact with your endpoints.
4
-
5
- ---
6
-
7
- ## Table of Contents
8
-
9
- - [Overview](#overview)
10
- - [Installation](#installation)
11
- - [Main Features](#main-features)
12
- - [Server Usage](#server-usage)
13
- - [Configuration](#configuration)
14
- - [Adding Custom Routes](#adding-custom-routes)
15
- - [Database Management](#database-management)
16
- - [Error Handling](#error-handling)
17
- - [Client Usage](#client-usage)
18
- - [Client Configuration](#client-configuration)
19
- - [Available Methods](#available-methods)
20
- - [Usage Examples](#usage-examples)
21
- - [Full List of Methods (inherited from idae-db)](#full-list-of-methods-inherited-from-idae-db)
22
- - [Contributing](#contributing)
23
- - [License](#license)
24
-
25
- ---
26
-
27
- ## Overview
28
-
29
-
30
- `@medyll/idae-api` is a modular Node.js API framework designed to work with multiple database types thanks to the [@medyll/idae-db](https://www.npmjs.com/package/@medyll/idae-db) library. It offers:
31
-
32
- - Modular architecture (routes, middlewares, dynamic connection management)
33
- - TypeScript/JavaScript client for easy API consumption
34
- - Extension system to support other databases
35
- - Native support for MongoDB and MySQL (via idae-db)
36
- - Hooks/events on CRUD operations
37
- - **MongoDB-like API syntax:** All query and update operations use a syntax very close to MongoDB (operators like `$in`, `$gt`, `$set`, etc.), making it intuitive for developers familiar with MongoDB.
38
-
39
- ## Installation
40
-
41
- ```bash
42
- npm install @medyll/idae-api
43
-
44
- npm install @medyll/idae-api --latest # to install the latest version
45
- npm install @medyll/idae-api --next # for the next version (if available)
46
-
47
- ```
48
-
49
- ---
50
-
51
- ## Main Features
52
-
53
- - Multi-database management (MongoDB, MySQL, etc.)
54
- - Flexible routing and custom route addition
55
- - Optional authentication middleware
56
- - Centralized error handling
57
- - Hooks/events on all CRUD operations
58
- - Complete TypeScript client for API consumption
59
- - Inherits all advanced methods from [@medyll/idae-db](https://www.npmjs.com/package/@medyll/idae-db)
60
-
61
- ---
62
-
63
- ## Server Usage
64
-
65
- ### Configuration
66
-
67
- ```typescript
68
- import { idaeApi } from '@medyll/idae-api';
69
-
70
- idaeApi.setOptions({
71
- port: 3000,
72
- enableAuth: true, // or false
73
- jwtSecret: 'your-secret-key',
74
- tokenExpiration: '1h',
75
- onInUse: 'reboot', // behavior if the port is already in use
76
- routes: customRoutes // optional
77
- });
78
-
79
- idaeApi.start();
80
- ```
81
-
82
- #### Example of custom routes
83
-
84
- ```typescript
85
- const customRoutes = [
86
- {
87
- method: 'get',
88
- path: '/custom/hello',
89
- handler: async () => ({ message: 'Hello from custom route!' }),
90
- requiresAuth: false
91
- }
92
- ];
93
-
94
- idaeApi.router.addRoutes(customRoutes);
95
- ```
96
-
97
- ### Database Management
98
-
99
- `idae-api` relies on `@medyll/idae-db` for connection and database operation management. You can:
100
-
101
- - Add new database adapters (see the `DatabaseAdapter` interface in idae-db)
102
- - Use all hooks/events from idae-db (see below)
103
-
104
- ### Error Handling
105
-
106
- An error handling middleware is included. You can customize it via the `configureErrorHandling` method in the `IdaeApi` class.
107
-
108
- ---
109
-
110
- ## Client Usage
111
-
112
- ### Client Configuration
113
-
114
- ```typescript
115
- import { IdaeApiClientConfig } from '@medyll/idae-api';
116
-
117
- IdaeApiClientConfig.setOptions({
118
- host: 'localhost',
119
- port: 3000,
120
- method: 'http',
121
- defaultDb: 'idae_base',
122
- separator: '//'
123
- });
124
- ```
125
-
126
- ### Creating a client instance
127
-
128
- ```typescript
129
- import { IdaeApiClient } from '@medyll/idae-api';
130
-
131
- const client = new IdaeApiClient();
132
- ```
133
-
134
- ### Available Methods
135
-
136
- #### Database and Collection Management
137
-
138
- - `getDbList()` : List available databases
139
- - `getCollections(dbName)` : List collections in a database
140
- - `db(dbName)` : Select a database (returns an object with `collection` and `getCollections`)
141
- - `collection(collectionName, dbName?)` : Select a collection (optionally in a given database)
142
-
143
- #### CRUD Operations on a Collection
144
-
145
- All the following methods are available via the `IdaeApiClientCollection` object:
146
-
147
- - `findAll(params?)` : List all documents (with filters, sorting, pagination)
148
- - `findById(id)` : Get a document by its ID
149
- - `findOne(params)` : Get a document by a filter (inherited from idae-db)
150
- - `create(body)` : Create a document
151
- - `update(id, body)` : Update a document
152
- - `deleteById(id)` : Delete a document by its ID
153
- - `deleteManyByQuery(params)` : Delete multiple documents by filter
154
-
155
- #### Event/Hook Management (inherited from idae-db)
156
-
157
- You can register hooks on all operations:
158
-
159
- ```typescript
160
- const usersCollection = client.collection('user');
161
-
162
- usersCollection.registerEvents({
163
- findById: {
164
- pre: (id) => console.log(`Before searching for ID: ${id}`),
165
- post: (result, id) => console.log(`Result for ${id}:`, result),
166
- error: (error) => console.error('Error on findById:', error)
167
- },
168
- // ... same for update, create, etc.
169
- });
170
- ```
171
-
172
- ### Usage Examples
173
-
174
- ### MongoDB-like Query Syntax
175
-
176
- All query and update operations use a syntax very close to MongoDB. For example, you can use operators like `$in`, `$gt`, `$set`, etc. in your queries and updates.
177
-
178
- ---
179
-
180
- #### List databases
181
-
182
- ```typescript
183
- const dbList = await client.getDbList();
184
- console.log('Databases:', dbList);
185
- ```
186
-
187
- #### List collections in a database
188
-
189
- ```typescript
190
- const collections = await client.getCollections('idae_base');
191
- console.log('Collections:', collections);
192
- ```
193
-
194
- #### Find documents with advanced query (MongoDB-like)
195
-
196
- ```typescript
197
- const userCollection = client.db('app').collection('user');
198
- const users = await userCollection.find({
199
- email: { $in: ["Karin@example.com", "Test@Value"] },
200
- age: 31,
201
- });
202
- console.log(users);
203
- ```
204
-
205
- #### Create a new document
206
-
207
- ```typescript
208
- const newUser = await userCollection.create({
209
- name: "new user",
210
- email: "Test@Value",
211
- });
212
- console.log(newUser);
213
- ```
214
-
215
- #### Find all documents in a collection
216
-
217
- ```typescript
218
- const allDocs = await userCollection.findAll();
219
- console.log(allDocs);
220
- ```
221
-
222
- #### Find a specific document by ID
223
-
224
- ```typescript
225
- const foundDoc = await userCollection.findById(newUser._id);
226
- console.log(foundDoc);
227
- ```
228
-
229
- #### Update a document (MongoDB-like update syntax)
230
-
231
- ```typescript
232
- const updatedDoc = await userCollection.update(newUser._id, {
233
- $set: { value: "Updated Value" },
234
- });
235
- console.log(updatedDoc);
236
- ```
237
-
238
- #### Delete a document
239
-
240
- ```typescript
241
- const deleteResult = await userCollection.deleteById(newUser._id);
242
- console.log(deleteResult);
243
- ```
244
-
245
- #### Use a specific database and collection
246
-
247
- ```typescript
248
- const appSchemeCollection = client.db('idae_base').collection('appscheme');
249
- const docs = await appSchemeCollection.findAll();
250
- console.log(docs);
251
- ```
252
-
253
- #### Delete many documents by query (be careful with this!)
254
-
255
- ```typescript
256
- const deleteResult2 = await client
257
- .collection('appscheme_base')
258
- .deleteManyByQuery({ testField: 'testValue' });
259
- console.log(deleteResult2);
260
- ```
261
-
262
- #### Register hooks/events on collection methods
263
-
264
- ```typescript
265
- const usersCollection = client.collection('user');
266
-
267
- usersCollection.registerEvents({
268
- findById: {
269
- pre: (id) => console.log(`Before searching for ID: ${id}`),
270
- post: (result, id) => console.log(`Result for ${id}:`, result),
271
- error: (error) => console.error('Error on findById:', error)
272
- },
273
- // ... same for update, create, etc.
274
- });
275
- ```
276
-
277
- ---
278
-
279
- ## Full List of Methods (inherited from idae-db)
280
-
281
- ### Collection Methods
282
-
283
- - `find(params)` : Advanced search (filters, sorting, pagination)
284
- - `findOne(params)` : Find a single document by filter
285
- - `findById(id)` : Find by ID
286
- - `create(data)` : Create
287
- - `update(id, data)` : Update
288
- - `deleteById(id)` : Delete by ID
289
- - `deleteManyByQuery(params)` : Bulk delete
290
- - `registerEvents(events)` : Register hooks (pre/post/error) on each method
291
-
292
- ### Connection Management
293
-
294
- - `closeAllConnections()` : Close all active connections
295
-
296
- ### Useful Types
297
-
298
- - `RequestParams` : `Record<string, unknown>`
299
- - `HttpMethod` : `'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH'`
300
- - `RouteNamespace` : ``methods/${'dbs' | 'collections'}``
301
- - `UrlParams` : `{ dbName?: string; collectionName?: string; slug?: string; params?: Record<string, string>; routeNamespace?: RouteNamespace }`
302
- - `RequestOptions<T>` : `UrlParams & { baseUrl?: string; method?: HttpMethod; body?: T; headers?: RequestInit['headers'] }`
303
-
304
- ---
305
-
306
- ## Contributing
307
-
308
- Contributions are welcome! Feel free to submit a Pull Request.
309
-
310
- ---
311
-
312
- ## License
313
-
314
- This project is licensed under the MIT License.
1
+ # @medyll/idae-api
2
+
3
+ A flexible and extensible Node.js API, based on the [@medyll/idae-db](https://www.npmjs.com/package/@medyll/idae-db) library, allowing you to manage multiple types of databases (MongoDB, MySQL, etc.) and providing a complete TypeScript client to interact with your endpoints.
4
+
5
+ ---
6
+
7
+ ## Table of Contents
8
+
9
+ - [Overview](#overview)
10
+ - [Installation](#installation)
11
+ - [Main Features](#main-features)
12
+ - [Server Usage](#server-usage)
13
+ - [Configuration](#configuration)
14
+ - [Adding Custom Routes](#adding-custom-routes)
15
+ - [Database Management](#database-management)
16
+ - [Error Handling](#error-handling)
17
+ - [Client Usage](#client-usage)
18
+ - [Client Configuration](#client-configuration)
19
+ - [Available Methods](#available-methods)
20
+ - [Usage Examples](#usage-examples)
21
+ - [Full List of Methods (inherited from idae-db)](#full-list-of-methods-inherited-from-idae-db)
22
+ - [Contributing](#contributing)
23
+ - [License](#license)
24
+
25
+ ---
26
+
27
+ ## Overview
28
+
29
+
30
+ `@medyll/idae-api` is a modular Node.js API framework designed to work with multiple database types thanks to the [@medyll/idae-db](https://www.npmjs.com/package/@medyll/idae-db) library. It offers:
31
+
32
+ - Modular architecture (routes, middlewares, dynamic connection management)
33
+ - TypeScript/JavaScript client for easy API consumption
34
+ - Extension system to support other databases
35
+ - Native support for MongoDB and MySQL (via idae-db)
36
+ - Hooks/events on CRUD operations
37
+ - **MongoDB-like API syntax:** All query and update operations use a syntax very close to MongoDB (operators like `$in`, `$gt`, `$set`, etc.), making it intuitive for developers familiar with MongoDB.
38
+
39
+ ## Installation
40
+
41
+ ```bash
42
+ npm install @medyll/idae-api
43
+
44
+ npm install @medyll/idae-api --latest # to install the latest version
45
+ npm install @medyll/idae-api --next # for the next version (if available)
46
+
47
+ ```
48
+
49
+ ---
50
+
51
+ ## Main Features
52
+
53
+ - Multi-database management (MongoDB, MySQL, etc.)
54
+ - Flexible routing and custom route addition
55
+ - Optional authentication middleware
56
+ - Centralized error handling
57
+ - Hooks/events on all CRUD operations
58
+ - Complete TypeScript client for API consumption
59
+ - Inherits all advanced methods from [@medyll/idae-db](https://www.npmjs.com/package/@medyll/idae-db)
60
+
61
+ ---
62
+
63
+ ## Server Usage
64
+
65
+ ### Configuration
66
+
67
+ ```typescript
68
+ import { idaeApi } from '@medyll/idae-api';
69
+
70
+ idaeApi.setOptions({
71
+ port: 3000,
72
+ enableAuth: true, // or false
73
+ jwtSecret: 'your-secret-key',
74
+ tokenExpiration: '1h',
75
+ onInUse: 'reboot', // behavior if the port is already in use
76
+ routes: customRoutes // optional
77
+ });
78
+
79
+ idaeApi.start();
80
+ ```
81
+
82
+ #### Example of custom routes
83
+
84
+ ```typescript
85
+ const customRoutes = [
86
+ {
87
+ method: 'get',
88
+ path: '/custom/hello',
89
+ handler: async () => ({ message: 'Hello from custom route!' }),
90
+ requiresAuth: false
91
+ }
92
+ ];
93
+
94
+ idaeApi.router.addRoutes(customRoutes);
95
+ ```
96
+
97
+ ### Database Management
98
+
99
+ `idae-api` relies on `@medyll/idae-db` for connection and database operation management. You can:
100
+
101
+ - Add new database adapters (see the `DatabaseAdapter` interface in idae-db)
102
+ - Use all hooks/events from idae-db (see below)
103
+
104
+ ### Error Handling
105
+
106
+ An error handling middleware is included. You can customize it via the `configureErrorHandling` method in the `IdaeApi` class.
107
+
108
+ ---
109
+
110
+ ## Client Usage
111
+
112
+ ### Client Configuration
113
+
114
+ ```typescript
115
+ import { IdaeApiClientConfig } from '@medyll/idae-api';
116
+
117
+ IdaeApiClientConfig.setOptions({
118
+ host: 'localhost',
119
+ port: 3000,
120
+ method: 'http',
121
+ defaultDb: 'idae_base',
122
+ separator: '//'
123
+ });
124
+ ```
125
+
126
+ ### Creating a client instance
127
+
128
+ ```typescript
129
+ import { IdaeApiClient } from '@medyll/idae-api';
130
+
131
+ const client = new IdaeApiClient();
132
+ ```
133
+
134
+ ### Available Methods
135
+
136
+ #### Database and Collection Management
137
+
138
+ - `getDbList()` : List available databases
139
+ - `getCollections(dbName)` : List collections in a database
140
+ - `db(dbName)` : Select a database (returns an object with `collection` and `getCollections`)
141
+ - `collection(collectionName, dbName?)` : Select a collection (optionally in a given database)
142
+
143
+ #### CRUD Operations on a Collection
144
+
145
+ All the following methods are available via the `IdaeApiClientCollection` object:
146
+
147
+ - `findAll(params?)` : List all documents (with filters, sorting, pagination)
148
+ - `findById(id)` : Get a document by its ID
149
+ - `findOne(params)` : Get a document by a filter (inherited from idae-db)
150
+ - `create(body)` : Create a document
151
+ - `update(id, body)` : Update a document
152
+ - `deleteById(id)` : Delete a document by its ID
153
+ - `deleteManyByQuery(params)` : Delete multiple documents by filter
154
+
155
+ #### Event/Hook Management (inherited from idae-db)
156
+
157
+ You can register hooks on all operations:
158
+
159
+ ```typescript
160
+ const usersCollection = client.collection('user');
161
+
162
+ usersCollection.registerEvents({
163
+ findById: {
164
+ pre: (id) => console.log(`Before searching for ID: ${id}`),
165
+ post: (result, id) => console.log(`Result for ${id}:`, result),
166
+ error: (error) => console.error('Error on findById:', error)
167
+ },
168
+ // ... same for update, create, etc.
169
+ });
170
+ ```
171
+
172
+ ### Usage Examples
173
+
174
+ ### MongoDB-like Query Syntax
175
+
176
+ All query and update operations use a syntax very close to MongoDB. For example, you can use operators like `$in`, `$gt`, `$set`, etc. in your queries and updates.
177
+
178
+ ---
179
+
180
+ #### List databases
181
+
182
+ ```typescript
183
+ const dbList = await client.getDbList();
184
+ console.log('Databases:', dbList);
185
+ ```
186
+
187
+ #### List collections in a database
188
+
189
+ ```typescript
190
+ const collections = await client.getCollections('idae_base');
191
+ console.log('Collections:', collections);
192
+ ```
193
+
194
+ #### Find documents with advanced query (MongoDB-like)
195
+
196
+ ```typescript
197
+ const userCollection = client.db('app').collection('user');
198
+ const users = await userCollection.find({
199
+ email: { $in: ["Karin@example.com", "Test@Value"] },
200
+ age: 31,
201
+ });
202
+ console.log(users);
203
+ ```
204
+
205
+ #### Create a new document
206
+
207
+ ```typescript
208
+ const newUser = await userCollection.create({
209
+ name: "new user",
210
+ email: "Test@Value",
211
+ });
212
+ console.log(newUser);
213
+ ```
214
+
215
+ #### Find all documents in a collection
216
+
217
+ ```typescript
218
+ const allDocs = await userCollection.findAll();
219
+ console.log(allDocs);
220
+ ```
221
+
222
+ #### Find a specific document by ID
223
+
224
+ ```typescript
225
+ const foundDoc = await userCollection.findById(newUser._id);
226
+ console.log(foundDoc);
227
+ ```
228
+
229
+ #### Update a document (MongoDB-like update syntax)
230
+
231
+ ```typescript
232
+ const updatedDoc = await userCollection.update(newUser._id, {
233
+ $set: { value: "Updated Value" },
234
+ });
235
+ console.log(updatedDoc);
236
+ ```
237
+
238
+ #### Delete a document
239
+
240
+ ```typescript
241
+ const deleteResult = await userCollection.deleteById(newUser._id);
242
+ console.log(deleteResult);
243
+ ```
244
+
245
+ #### Use a specific database and collection
246
+
247
+ ```typescript
248
+ const appSchemeCollection = client.db('idae_base').collection('appscheme');
249
+ const docs = await appSchemeCollection.findAll();
250
+ console.log(docs);
251
+ ```
252
+
253
+ #### Delete many documents by query (be careful with this!)
254
+
255
+ ```typescript
256
+ const deleteResult2 = await client
257
+ .collection('appscheme_base')
258
+ .deleteManyByQuery({ testField: 'testValue' });
259
+ console.log(deleteResult2);
260
+ ```
261
+
262
+ #### Register hooks/events on collection methods
263
+
264
+ ```typescript
265
+ const usersCollection = client.collection('user');
266
+
267
+ usersCollection.registerEvents({
268
+ findById: {
269
+ pre: (id) => console.log(`Before searching for ID: ${id}`),
270
+ post: (result, id) => console.log(`Result for ${id}:`, result),
271
+ error: (error) => console.error('Error on findById:', error)
272
+ },
273
+ // ... same for update, create, etc.
274
+ });
275
+ ```
276
+
277
+ ---
278
+
279
+ ## Full List of Methods (inherited from idae-db)
280
+
281
+ ### Collection Methods
282
+
283
+ - `find(params)` : Advanced search (filters, sorting, pagination)
284
+ - `findOne(params)` : Find a single document by filter
285
+ - `findById(id)` : Find by ID
286
+ - `create(data)` : Create
287
+ - `update(id, data)` : Update
288
+ - `deleteById(id)` : Delete by ID
289
+ - `deleteManyByQuery(params)` : Bulk delete
290
+ - `registerEvents(events)` : Register hooks (pre/post/error) on each method
291
+
292
+ ### Connection Management
293
+
294
+ - `closeAllConnections()` : Close all active connections
295
+
296
+ ### Useful Types
297
+
298
+ - `RequestParams` : `Record<string, unknown>`
299
+ - `HttpMethod` : `'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH'`
300
+ - `RouteNamespace` : ``methods/${'dbs' | 'collections'}``
301
+ - `UrlParams` : `{ dbName?: string; collectionName?: string; slug?: string; params?: Record<string, string>; routeNamespace?: RouteNamespace }`
302
+ - `RequestOptions<T>` : `UrlParams & { baseUrl?: string; method?: HttpMethod; body?: T; headers?: RequestInit['headers'] }`
303
+
304
+ ---
305
+
306
+ ## Contributing
307
+
308
+ Contributions are welcome! Feel free to submit a Pull Request.
309
+
310
+ ---
311
+
312
+ ## License
313
+
314
+ This project is licensed under the MIT License.
@@ -1,18 +1,18 @@
1
- // packages\idae-api\src\lib\idae-api.d.ts
2
-
3
- import mongoose from "mongoose";
4
- import { IdaeDb, IdaeDbAdapter } from "@medyll/idae-db";
5
-
6
- declare global {
7
- namespace Express {
8
- interface Request {
9
- dbConnection?: mongoose.Connection;
10
- collectionName?: string;
11
- dbName?: string;
12
- idaeDb?: IdaeDb;
13
- connectedCollection: IdaeDbAdapter;
14
- }
15
- }
16
- }
17
-
18
- export {};
1
+ // packages\idae-api\src\lib\idae-api.d.ts
2
+
3
+ import mongoose from "mongoose";
4
+ import { IdaeDb, IdaeDbAdapter } from "@medyll/idae-db";
5
+
6
+ declare global {
7
+ namespace Express {
8
+ interface Request {
9
+ dbConnection?: mongoose.Connection;
10
+ collectionName?: string;
11
+ dbName?: string;
12
+ idaeDb?: IdaeDb;
13
+ connectedCollection: IdaeDbAdapter;
14
+ }
15
+ }
16
+ }
17
+
18
+ export {};
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@medyll/idae-api",
3
3
  "scope": "@medyll",
4
- "version": "0.121.1",
4
+ "version": "0.123.0",
5
5
  "main": "./dist/index.js",
6
6
  "module": "./dist/index.js",
7
7
  "description": "A flexible and extensible API framework for Node.js, designed to work with multiple database types and configurations. It features a modular architecture, dynamic database connection management, and a flexible routing system. The framework supports TypeScript for improved robustness and maintainability.",
@@ -46,7 +46,7 @@
46
46
  "sequelize": "^6.37.7"
47
47
  },
48
48
  "devDependencies": {
49
- "@medyll/idae-db": "^0.89.1",
49
+ "@medyll/idae-db": "^0.91.0",
50
50
  "@medyll/idae-prettier-config": "next",
51
51
  "@sveltejs/adapter-auto": "^6.0.1",
52
52
  "@sveltejs/kit": "^2.21.2",