codehooks-js 1.3.14 → 1.3.15

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 +108 -52
  2. package/package.json +1 -1
  3. package/types/index.d.ts +34 -34
package/README.md CHANGED
@@ -1,10 +1,11 @@
1
- # codehooks-js
1
+ # codehooks-js
2
2
 
3
3
  The official JavaScript/TypeScript library for [Codehooks.io](https://codehooks.io) - a serverless backend platform that provides everything you need to build and deploy backend applications.
4
4
 
5
5
  ## What is Codehooks.io?
6
6
 
7
7
  Codehooks.io is a complete serverless backend platform that combines:
8
+
8
9
  - **Database** - NoSQL document storage with MongoDB-like queries
9
10
  - **API Server** - Express.js-style routing and middleware
10
11
  - **Background Jobs** - Queues, workers, and scheduled tasks
@@ -16,7 +17,7 @@ All in one platform with zero infrastructure management.
16
17
 
17
18
  ## Quick Start
18
19
 
19
- Install the [Codehooks CLI](https://codehooks.io/docs/cli):
20
+ Install the [Codehooks CLI](https://codehooks.io/docs/cli):
20
21
 
21
22
  ```shell
22
23
  $ npm install -g codehooks
@@ -34,59 +35,65 @@ Example code for a serverless backend API and NoSQL database:
34
35
 
35
36
  ```javascript
36
37
  /*
37
- * REST API with NoSQL database storage.
38
- * Codehooks (c) example code.
39
- */
38
+ * REST API with NoSQL database storage.
39
+ * Codehooks (c) example code.
40
+ */
40
41
 
41
- import {app, datastore} from 'codehooks-js';
42
+ import { app, datastore } from 'codehooks-js';
42
43
 
43
44
  // Example GET route and a NoSQL database insert operation
44
45
  app.get('/myroute', async (req, res) => {
45
- console.log("GET")
46
- const conn = await datastore.open()
47
- const doc = await conn.insertOne('greetings', {"message": "Hello World!", "when": new Date()})
48
- res.json({...doc})
46
+ console.log('GET');
47
+ const conn = await datastore.open();
48
+ const doc = await conn.insertOne('greetings', {
49
+ message: 'Hello World!',
50
+ when: new Date(),
51
+ });
52
+ res.json({ ...doc });
49
53
  });
50
54
 
51
55
  // Serve web content from the uploaded directory /static
52
- app.static({directory: "/static"})
56
+ app.static({ directory: '/static' });
53
57
 
54
58
  // CRUD REST API for any collection
55
- app.crudlify({}, {prefix: "/"})
59
+ app.crudlify({}, { prefix: '/' });
56
60
 
57
61
  // return app to serverless runtime engine
58
- export default app.init()
62
+ export default app.init();
59
63
  ```
60
64
 
61
65
  ## TypeScript development
62
66
 
63
67
  Rename the index.js to index.ts or create a new index.ts file.
64
- Start developing using TypeScript and strong types shown in the example code below.
68
+ Start developing using TypeScript and strong types shown in the example code below.
65
69
 
66
70
  ```typescript
67
71
  /*
68
- * REST API with NoSQL database storage.
69
- * Codehooks (c) example code in TypeScript.
70
- */
72
+ * REST API with NoSQL database storage.
73
+ * Codehooks (c) example code in TypeScript.
74
+ */
71
75
 
72
- import {app, datastore, httpResponse, httpRequest} from 'codehooks-js';
76
+ import { app, datastore, httpResponse, httpRequest } from 'codehooks-js';
73
77
 
74
78
  // Example GET route and a NoSQL database insert operation
75
79
  app.get('/myroute', async (req: httpRequest, res: httpResponse) => {
76
- console.log("GET")
77
- const conn = await datastore.open()
78
- const doc = await conn.insertOne('greetings', {"message": "Hello World!", "when": new Date()})
79
- res.json({...doc})
80
+ console.log('GET');
81
+ const conn = await datastore.open();
82
+ const doc = await conn.insertOne('greetings', {
83
+ message: 'Hello World!',
84
+ when: new Date(),
85
+ });
86
+ res.json({ ...doc });
80
87
  });
81
88
 
82
89
  // Serve web content from the uploaded directory /static
83
- app.static({directory: "/static"})
90
+ app.static({ directory: '/static' });
84
91
 
85
92
  // CRUD REST API for any collection
86
- app.crudlify({}, {prefix: "/"})
93
+ app.crudlify({}, { prefix: '/' });
87
94
 
88
95
  // return app to serverless runtime engine
89
- export default app.init()
96
+ export default app.init();
90
97
  ```
91
98
 
92
99
  ## Compile
@@ -95,16 +102,11 @@ When running the `coho compile` command, it will automatically create a `tsconfi
95
102
 
96
103
  ```json
97
104
  {
98
- "files": [
99
- "./index.ts"
100
- ],
101
- "compilerOptions": {
102
- "allowJs": true,
103
- "lib": [
104
- "ES6",
105
- "dom"
106
- ]
107
- }
105
+ "files": ["./index.ts"],
106
+ "compilerOptions": {
107
+ "allowJs": true,
108
+ "lib": ["ES6", "dom"]
109
+ }
108
110
  }
109
111
  ```
110
112
 
@@ -131,7 +133,8 @@ Your backend application is now available at your project's endpoint URL. For ex
131
133
 
132
134
  ## Deploy
133
135
 
134
- From the project directory run:
136
+ From the project directory run:
137
+
135
138
  ```shell
136
139
  $ codehooks deploy
137
140
  ```
@@ -147,44 +150,50 @@ For complete documentation, visit [https://codehooks.io/docs](https://codehooks.
147
150
  The Codehooks class provides a comprehensive backend application framework with the following APIs:
148
151
 
149
152
  ### **HTTP Routing APIs**
153
+
150
154
  - **`post(path, ...hook)`** - Register POST route handlers
151
- - **`get(path, ...hook)`** - Register GET route handlers
155
+ - **`get(path, ...hook)`** - Register GET route handlers
152
156
  - **`put(path, ...hook)`** - Register PUT route handlers
153
157
  - **`patch(path, ...hook)`** - Register PATCH route handlers
154
158
  - **`delete(path, ...hook)`** - Register DELETE route handlers
155
159
  - **`all(path, ...hook)`** - Register handlers for all HTTP methods
156
160
 
157
161
  ### **Middleware & Authentication APIs**
162
+
158
163
  - **`use(...hook)`** - Register global middleware (supports string paths, RegExp, or function)
159
164
  - **`useRoute(route, ...hook)`** - Register route-specific middleware
160
165
  - **`auth(path, ...hook)`** - Register authentication middleware for specific paths
161
166
 
162
167
  ### **Background Processing APIs**
168
+
163
169
  - **`queue(topic, ...hook)`** - Register queue handlers for background processing
164
170
  - **`worker(name, ...hook)`** - Register worker functions (also adds to queues for legacy support)
165
171
  - **`job(cronExpression, ...hook)`** - Register scheduled cron jobs
166
172
 
167
173
  ### **Static File Serving APIs**
174
+
168
175
  - **`static(options, hook)`** - Serve static files from source code directory
169
176
  - **`storage(options, hook)`** - Serve files from blob storage directory
170
177
 
171
178
  ### **Template & Configuration APIs**
179
+
172
180
  - **`set(key, val)`** - Set application configuration settings
173
181
  - **`render(view, data, cb)`** - Render templates with data
174
182
  - **`crudlify(schema, options)`** - Auto-generate CRUD REST API endpoints
175
183
 
176
184
  ### **Real-time Communication APIs**
185
+
177
186
  - **`realtime(path, ...hook)`** - Set up server-sent events (SSE) channels for real-time communication
178
187
 
179
188
  ### **Workflow Management APIs**
189
+
180
190
  - **`createWorkflow(name, description, steps, options)`** - Create and register a new workflow instance
181
191
 
182
192
  ### **Application Lifecycle APIs**
193
+
183
194
  - **`init(hook)`** - Initialize the application and return manifest
184
195
  - **`start(hook)`** - Alias for `init()` method
185
196
 
186
-
187
-
188
197
  The Codehooks class serves as a comprehensive backend application framework that combines HTTP routing, background processing, real-time communication, and workflow management capabilities in a single, cohesive API.
189
198
 
190
199
  ## Datastore API Reference
@@ -192,18 +201,21 @@ The Codehooks class serves as a comprehensive backend application framework that
192
201
  The Datastore provides a unified interface for both NoSQL document storage and Key-Value operations. It supports MongoDB-like query syntax and provides streaming capabilities for large datasets.
193
202
 
194
203
  ### **Connection & Collection Management**
204
+
195
205
  - **`open()`** - Connect to the Datastore and return the API interface
196
206
  - **`collection(name)`** - Get a NoSQL collection by name for document operations
197
207
 
198
208
  ### **NoSQL Document Operations**
199
209
 
200
210
  #### **Read Operations**
211
+
201
212
  - **`getOne(collection, query)`** - Get a single document by ID or query
202
213
  - **`findOne(collection, query)`** - Alias for getOne
203
214
  - **`getMany(collection, query?, options?)`** - Get a stream of documents matching query
204
215
  - **`find(collection, query?, options?)`** - Alias for getMany
205
216
 
206
217
  #### **Write Operations**
218
+
207
219
  - **`insertOne(collection, document)`** - Insert a new document into a collection
208
220
  - **`updateOne(collection, query, document, updateOperators?, options?)`** - Update one document (patches existing data)
209
221
  - **`updateMany(collection, query, document, updateOperators?)`** - Update multiple documents
@@ -211,32 +223,38 @@ The Datastore provides a unified interface for both NoSQL document storage and K
211
223
  - **`replaceMany(collection, query, document, options?)`** - Replace multiple documents
212
224
 
213
225
  #### **Delete Operations**
226
+
214
227
  - **`removeOne(collection, query)`** - Remove one document by ID or query
215
228
  - **`removeMany(collection, query)`** - Remove multiple documents matching query
216
229
 
217
230
  #### **Schema Management**
231
+
218
232
  - **`createSchema(collection, schema)`** - Validate collection data against JSON-Schema
219
233
  - **`setSchema(collection, schema)`** - Alias for createSchema
220
234
  - **`removeSchema(collection)`** - Remove JSON-schema validation for collection
221
235
  - **`getSchema(collection)`** - Get JSON-schema for collection
222
236
 
223
237
  #### **Utility Operations**
238
+
224
239
  - **`count(collection)`** - Count documents in a collection
225
240
 
226
241
  ### **Key-Value Operations**
227
242
 
228
243
  #### **Basic Key-Value Operations**
244
+
229
245
  - **`set(key, value, options?)`** - Set a key-value pair
230
246
  - **`get(key, options?)`** - Get a value by key
231
247
  - **`getAll(keyPattern, options?)`** - Get all key-value pairs matching pattern
232
- - **`del(key, value)`** - Delete a key-value pair
248
+ - **`del(key, options?)`** - Delete a key-value pair
233
249
  - **`delAll(keyPattern, options?)`** - Delete all key-value pairs matching pattern
234
250
 
235
251
  #### **Numeric Operations**
252
+
236
253
  - **`incr(key, value, options?)`** - Increment a numeric value
237
254
  - **`decr(key, value, options?)`** - Decrement a numeric value
238
255
 
239
256
  ### **Queue Operations**
257
+
240
258
  - **`enqueue(topic, document, options?)`** - Add a job to a queue for background processing
241
259
  - **`enqueueFromQuery(collection, query, topic, options?)`** - Queue each item from a database query
242
260
 
@@ -258,7 +276,10 @@ import { datastore } from 'codehooks-js';
258
276
  const conn = await datastore.open();
259
277
 
260
278
  // NoSQL operations
261
- const doc = await conn.insertOne('users', { name: 'John', email: 'john@example.com' });
279
+ const doc = await conn.insertOne('users', {
280
+ name: 'John',
281
+ email: 'john@example.com',
282
+ });
262
283
  const user = await conn.getOne('users', { _id: doc._id });
263
284
  const users = await conn.getMany('users', { active: true }).toArray();
264
285
  await conn.updateOne('users', { _id: doc._id }, { lastLogin: new Date() });
@@ -269,7 +290,10 @@ const session = await conn.get('user:123:session');
269
290
  await conn.incr('visits', 1);
270
291
 
271
292
  // Queue operations
272
- await conn.enqueue('emailWorker', { to: 'user@example.com', template: 'welcome' });
293
+ await conn.enqueue('emailWorker', {
294
+ to: 'user@example.com',
295
+ template: 'welcome',
296
+ });
273
297
  ```
274
298
 
275
299
  ### **Query Syntax**
@@ -278,22 +302,48 @@ The Datastore supports MongoDB-like query syntax:
278
302
 
279
303
  ```javascript
280
304
  // Simple equality
281
- { status: 'active' }
305
+ {
306
+ status: 'active';
307
+ }
282
308
 
283
309
  // Comparison operators
284
- { age: { $gt: 18 } }
285
- { price: { $lte: 100 } }
310
+ {
311
+ age: {
312
+ $gt: 18;
313
+ }
314
+ }
315
+ {
316
+ price: {
317
+ $lte: 100;
318
+ }
319
+ }
286
320
 
287
321
  // Logical operators
288
- { $and: [{ status: 'active' }, { age: { $gte: 18 } }] }
289
- { $or: [{ category: 'A' }, { category: 'B' }] }
322
+ {
323
+ $and: [{ status: 'active' }, { age: { $gte: 18 } }];
324
+ }
325
+ {
326
+ $or: [{ category: 'A' }, { category: 'B' }];
327
+ }
290
328
 
291
329
  // Array operations
292
- { tags: { $in: ['javascript', 'nodejs'] } }
293
- { tags: { $all: ['javascript', 'nodejs'] } }
330
+ {
331
+ tags: {
332
+ $in: ['javascript', 'nodejs'];
333
+ }
334
+ }
335
+ {
336
+ tags: {
337
+ $all: ['javascript', 'nodejs'];
338
+ }
339
+ }
294
340
 
295
341
  // Regular expressions
296
- { name: { $regex: /john/i } }
342
+ {
343
+ name: {
344
+ $regex: /john/i;
345
+ }
346
+ }
297
347
  ```
298
348
 
299
349
  ### **Options**
@@ -302,14 +352,20 @@ Many operations accept an options object for additional configuration:
302
352
 
303
353
  ```javascript
304
354
  // Upsert option for updates
305
- await conn.updateOne('users', { email: 'john@example.com' },
306
- { lastLogin: new Date() }, {}, { upsert: true });
355
+ await conn.updateOne(
356
+ 'users',
357
+ { email: 'john@example.com' },
358
+ { lastLogin: new Date() },
359
+ {},
360
+ { upsert: true }
361
+ );
307
362
 
308
363
  // Key-Value options
309
364
  await conn.set('key', 'value', { ttl: 3600000 }); // 1 hour TTL
310
365
  ```
311
366
 
312
367
  ### **Key Features:**
368
+
313
369
  1. **Express-style routing** with support for all HTTP methods
314
370
  2. **Middleware system** with global and route-specific middleware
315
371
  3. **Background processing** with queues, workers, and scheduled jobs
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "codehooks-js",
3
- "version": "1.3.14",
3
+ "version": "1.3.15",
4
4
  "type": "module",
5
5
  "description": "Codehooks.io official library - provides express.JS like syntax",
6
6
  "main": "index.js",
package/types/index.d.ts CHANGED
@@ -401,7 +401,7 @@ export type DatastoreAPI = {
401
401
  /**
402
402
  * - Delete a key-value pair in the current Datastore
403
403
  */
404
- del: (key: string, value: object) => Promise<any>;
404
+ del: (key: string, options?: object) => Promise<any>;
405
405
  /**
406
406
  * - Delete a range of key-value pairs in the current Datastore
407
407
  */
@@ -466,11 +466,11 @@ export type DatastoreAPI = {
466
466
  * @returns Promise with result
467
467
  */
468
468
  getSchema: (collection: string) => Promise<object>;
469
- /**
470
- * Count database collection items
471
- * @returns Promise result
472
- */
473
- count: (collection: string) => Promise<object>;
469
+ /**
470
+ * Count database collection items
471
+ * @returns Promise result
472
+ */
473
+ count: (collection: string) => Promise<object>;
474
474
  };
475
475
  /**
476
476
  * Persistent NoSql and Kev-Value datastore
@@ -956,8 +956,8 @@ export class Codehooks {
956
956
  * })
957
957
  * @returns void
958
958
  */
959
- static: (options: any,
960
- ...hook: ((
959
+ static: (options: any,
960
+ ...hook: ((
961
961
  request: httpRequest,
962
962
  response: httpResponse,
963
963
  next: nextFunction
@@ -967,12 +967,12 @@ export class Codehooks {
967
967
  * @param {Object} options - {route: "/documents", directory: "/docs"}
968
968
  * @returns void
969
969
  */
970
- storage: (options: any,
970
+ storage: (options: any,
971
971
  ...hook: ((
972
- request: httpRequest,
973
- response: httpResponse,
974
- next: nextFunction
975
- ) => any)[]) => void;
972
+ request: httpRequest,
973
+ response: httpResponse,
974
+ next: nextFunction
975
+ ) => any)[]) => void;
976
976
  /**
977
977
  * Configuration settings
978
978
  * @param {string} key
@@ -1067,31 +1067,31 @@ export type WorkflowEvents = {
1067
1067
  * @event
1068
1068
  */
1069
1069
  'workflowCreated': { name: string; description: string };
1070
-
1070
+
1071
1071
  /**
1072
1072
  * Emitted when a new workflow instance is started
1073
1073
  * @event
1074
1074
  */
1075
1075
  'workflowStarted': { name: string; initialState: any };
1076
-
1076
+
1077
1077
  /**
1078
1078
  * Emitted when a step begins execution
1079
1079
  * @event
1080
1080
  */
1081
1081
  'stepStarted': { workflowName: string; step: string; state: any; instanceId: string };
1082
-
1082
+
1083
1083
  /**
1084
1084
  * Emitted when a step's state is updated
1085
1085
  * @event
1086
1086
  */
1087
1087
  'stateUpdated': { workflowName: string; state: any; instanceId: string };
1088
-
1088
+
1089
1089
  /**
1090
1090
  * Emitted when a step is enqueued for execution
1091
1091
  * @event
1092
1092
  */
1093
1093
  'stepEnqueued': { workflowName: string; step: string; state: any; instanceId: string };
1094
-
1094
+
1095
1095
  /**
1096
1096
  * Emitted when a workflow instance is continued after waiting
1097
1097
  * @event
@@ -1103,7 +1103,7 @@ export type WorkflowEvents = {
1103
1103
  * @event
1104
1104
  */
1105
1105
  'completed': { message: string; state: any };
1106
-
1106
+
1107
1107
  /**
1108
1108
  * Emitted when a workflow instance is cancelled
1109
1109
  * @event
@@ -1121,9 +1121,9 @@ export type WorkflowEvents = {
1121
1121
  * Definition of a workflow step function
1122
1122
  */
1123
1123
  export type WorkflowDefinition = Record<string, (
1124
- state: any,
1125
- callback: (nextStep: string | string[] | null, newState: any, options?: any) => void,
1126
- waiterFunction?: (waiter?: any) => void
1124
+ state: any,
1125
+ callback: (nextStep: string | string[] | null, newState: any, options?: any) => void,
1126
+ waiterFunction?: (waiter?: any) => void
1127
1127
  ) => Promise<void>>;
1128
1128
 
1129
1129
  /**
@@ -1255,14 +1255,14 @@ export type Workflow = {
1255
1255
  * @returns Promise with the registered workflow name
1256
1256
  */
1257
1257
  register: (app: Codehooks) => Promise<string>;
1258
-
1258
+
1259
1259
  /**
1260
1260
  * Start a new workflow instance
1261
1261
  * @param initialState - Initial state for the workflow instance
1262
1262
  * @returns Promise with the workflow instance
1263
1263
  */
1264
1264
  start: (initialState: any) => Promise<any>;
1265
-
1265
+
1266
1266
  /**
1267
1267
  * Update the state of a workflow instance
1268
1268
  * @param instanceId - ID of the workflow instance
@@ -1271,7 +1271,7 @@ export type Workflow = {
1271
1271
  * @returns Promise with the updated state
1272
1272
  */
1273
1273
  updateState: (instanceId: string, state: any, options?: UpdateOptions) => Promise<any>;
1274
-
1274
+
1275
1275
  /**
1276
1276
  * Set the complete state of a workflow instance
1277
1277
  * @param instanceId - ID of the workflow instance
@@ -1279,7 +1279,7 @@ export type Workflow = {
1279
1279
  * @returns Promise<void>
1280
1280
  */
1281
1281
  setState: (instanceId: string, stateData: { _id: string; state: any }) => Promise<void>;
1282
-
1282
+
1283
1283
  /**
1284
1284
  * Continue a paused workflow instance
1285
1285
  * @param instanceId - ID of the workflow instance
@@ -1287,21 +1287,21 @@ export type Workflow = {
1287
1287
  * @returns Promise with the queue ID for the continued step
1288
1288
  */
1289
1289
  continue: (instanceId: string, reset?: boolean) => Promise<{ instanceId: string }>;
1290
-
1290
+
1291
1291
  /**
1292
1292
  * Get the status of a workflow instance
1293
1293
  * @param id - ID of the workflow instance
1294
1294
  * @returns Promise with the workflow status
1295
1295
  */
1296
1296
  getStepsStatus: (id: string) => Promise<any>;
1297
-
1297
+
1298
1298
  /**
1299
1299
  * Get all workflow instances matching a filter
1300
1300
  * @param filter - Filter criteria for workflows
1301
1301
  * @returns Promise with list of workflow instances
1302
1302
  */
1303
1303
  getInstances: (filter: any) => Promise<any[]>;
1304
-
1304
+
1305
1305
  /**
1306
1306
  * Cancel a workflow instance
1307
1307
  * @param id - ID of the workflow instance to cancel
@@ -1319,21 +1319,21 @@ export type Workflow = {
1319
1319
  * });
1320
1320
  */
1321
1321
  on: (event: WorkflowEvent, listener: (data: WorkflowEventData) => void) => Workflow;
1322
-
1322
+
1323
1323
  /**
1324
1324
  * Register a one-time event listener
1325
1325
  * @param event - Name of the event to listen for
1326
1326
  * @param listener - Callback function to handle the event
1327
1327
  */
1328
1328
  once: (event: WorkflowEvent, listener: (data: WorkflowEventData) => void) => Workflow;
1329
-
1329
+
1330
1330
  /**
1331
1331
  * Remove an event listener
1332
1332
  * @param event - Name of the event
1333
1333
  * @param listener - Callback function to remove
1334
1334
  */
1335
1335
  off: (event: WorkflowEvent, listener: (data: WorkflowEventData) => void) => Workflow;
1336
-
1336
+
1337
1337
  /**
1338
1338
  * Emit an event
1339
1339
  * @param event - Name of the event to emit
@@ -1345,7 +1345,7 @@ export type Workflow = {
1345
1345
  * Continue all timed out workflow instances
1346
1346
  * @returns Promise with array of results containing queue IDs for continued workflows
1347
1347
  */
1348
- continueAllTimedOut: () => Promise<Array<{instanceId: string}>>;
1348
+ continueAllTimedOut: () => Promise<Array<{ instanceId: string }>>;
1349
1349
 
1350
1350
  /**
1351
1351
  * Check if a specific step in a workflow instance has timed out
@@ -1427,7 +1427,7 @@ export type UpdateOptions = {
1427
1427
  /**
1428
1428
  * Workflow event types
1429
1429
  */
1430
- export type WorkflowEvent =
1430
+ export type WorkflowEvent =
1431
1431
  | 'workflowCreated'
1432
1432
  | 'workflowStarted'
1433
1433
  | 'workflowContinued'