semola 0.5.4 → 0.6.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.
Files changed (50) hide show
  1. package/README.md +35 -50
  2. package/dist/lib/api/index.cjs +1 -523
  3. package/dist/lib/api/index.d.cts +1 -271
  4. package/dist/lib/api/index.d.mts +80 -84
  5. package/dist/lib/api/index.mjs +1 -521
  6. package/dist/lib/cache/index.cjs +1 -74
  7. package/dist/lib/cache/index.d.cts +1 -48
  8. package/dist/lib/cache/index.d.mts +3 -24
  9. package/dist/lib/cache/index.mjs +1 -73
  10. package/dist/lib/cron/index.cjs +1 -735
  11. package/dist/lib/cron/index.d.cts +1 -146
  12. package/dist/lib/cron/index.d.mts +99 -36
  13. package/dist/lib/cron/index.mjs +1 -726
  14. package/dist/lib/errors/index.cjs +1 -30
  15. package/dist/lib/errors/index.d.cts +1 -2
  16. package/dist/lib/errors/index.d.mts +12 -1
  17. package/dist/lib/errors/index.mjs +1 -26
  18. package/dist/lib/i18n/index.cjs +1 -42
  19. package/dist/lib/i18n/index.d.cts +1 -28
  20. package/dist/lib/i18n/index.mjs +1 -41
  21. package/dist/lib/logging/index.cjs +1 -387
  22. package/dist/lib/logging/index.d.cts +1 -108
  23. package/dist/lib/logging/index.mjs +1 -374
  24. package/dist/lib/orm/index.cjs +1 -0
  25. package/dist/lib/orm/index.d.cts +1 -0
  26. package/dist/lib/orm/index.d.mts +404 -0
  27. package/dist/lib/orm/index.mjs +1 -0
  28. package/dist/lib/policy/index.cjs +1 -285
  29. package/dist/lib/policy/index.d.cts +1 -77
  30. package/dist/lib/policy/index.mjs +1 -265
  31. package/dist/lib/prompts/index.cjs +6 -769
  32. package/dist/lib/prompts/index.d.cts +1 -96
  33. package/dist/lib/prompts/index.d.mts +12 -33
  34. package/dist/lib/prompts/index.mjs +6 -763
  35. package/dist/lib/pubsub/index.cjs +1 -117
  36. package/dist/lib/pubsub/index.d.cts +1 -41
  37. package/dist/lib/pubsub/index.d.mts +3 -18
  38. package/dist/lib/pubsub/index.mjs +1 -116
  39. package/dist/lib/queue/index.cjs +1 -182
  40. package/dist/lib/queue/index.d.cts +1 -74
  41. package/dist/lib/queue/index.d.mts +11 -4
  42. package/dist/lib/queue/index.mjs +1 -181
  43. package/dist/lib/workflow/index.cjs +1 -534
  44. package/dist/lib/workflow/index.d.cts +1 -85
  45. package/dist/lib/workflow/index.d.mts +76 -11
  46. package/dist/lib/workflow/index.mjs +1 -533
  47. package/dist/rolldown-runtime-CMqjfN_6.cjs +1 -0
  48. package/package.json +17 -5
  49. package/dist/index-BhGNDjPq.d.mts +0 -13
  50. package/dist/index-DxSbeGP-.d.cts +0 -13
package/README.md CHANGED
@@ -26,12 +26,12 @@ Type-safe APIs, Redis queues, pub/sub, i18n, caching & auth with tree-shakeable
26
26
  | **🔐 Policy** | Policy-based authorization with type-safe guards | `semola/policy` |
27
27
  | **🌍 i18n** | Compile-time validated internationalization | `semola/i18n` |
28
28
  | **💾 Cache** | Redis cache wrapper with TTL & automatic serialization | `semola/cache` |
29
- | **⏰ Cron** | In-memory cron scheduler for periodic task execution | `semola/cron` |
30
- | **🔁 Workflow** | Durable resumable workflows with step persistence | `semola/workflow` |
29
+ | **⏰ Cron** | In-memory and OS cron scheduler for periodic task execution | `semola/cron` |
30
+ | **🔁 Workflow** | Durable resumable workflows with retries and hooks | `semola/workflow` |
31
31
  | **⚠️ Errors** | Result-based error handling without try/catch | `semola/errors` |
32
32
  | **📃 Logging** | A simple logging utility | `semola/logging` |
33
33
  | **⌨️ Prompts** | Interactive zero-dependency CLI prompts | `semola/prompts` |
34
- | **🗄️ ORM** | Type-safe data layer with query APIs + migrations | `semola/orm` |
34
+ | **🗄️ ORM** | Type-safe data layer with query APIs | `semola/orm` |
35
35
 
36
36
  ---
37
37
 
@@ -122,16 +122,14 @@ const pubsub = new PubSub({
122
122
  });
123
123
 
124
124
  // Subscribe to messages
125
- const [, unsubscribeHandler] = await pubsub.subscribe((message) => {
125
+ const unsubscribe = await pubsub.subscribe((message) => {
126
126
  console.log("Received:", message);
127
127
  });
128
128
 
129
129
  // Publish a message
130
130
  await pubsub.publish({ userId: 123, text: "New alert!" });
131
131
 
132
- if (unsubscribeHandler) {
133
- await unsubscribeHandler();
134
- }
132
+ await unsubscribe();
135
133
  ```
136
134
 
137
135
  ### Cache Data with TTL
@@ -148,84 +146,71 @@ const cache = new Cache({
148
146
  await cache.set("user:123", { name: "John", age: 30 });
149
147
 
150
148
  // Retrieve data
151
- const [error, user] = await cache.get("user:123");
152
- if (!error) console.log(user);
149
+ const user = await cache.get("user:123");
150
+ console.log(user);
153
151
  ```
154
152
 
155
153
  ### Schedule Recurring Tasks
156
154
 
157
155
  ```typescript
158
- import { Cron } from "semola/cron";
156
+ import { Cron, RetryCronJob } from "semola/cron";
159
157
 
160
158
  const cleanup = new Cron({
161
159
  name: "daily-cleanup",
162
- schedule: "0 0 * * *", // Daily at midnight
160
+ schedule: "@daily",
163
161
  handler: async () => {
164
162
  await deleteOldLogs();
165
163
  await archiveInactiveUsers();
166
164
  },
165
+ retry: new RetryCronJob({
166
+ maxAttempts: 2,
167
+ onError: (err) => console.log(`An error: ${err.error.message}`),
168
+ onFailedAttempt: async ({ attemptNumber, delay, error, retriesLeft }) => {
169
+ console.log(
170
+ `Attempt ${attemptNumber} failed. Retrying in ${delay}ms. ${retriesLeft} retries left.`,
171
+ );
172
+
173
+ await recover();
174
+ },
175
+ }),
167
176
  });
168
177
 
169
- cleanup.start();
178
+ cleanup.run();
179
+
170
180
  ```
171
181
 
172
182
  ### Query a Database
173
183
 
174
184
  ```typescript
175
- import { createOrm, createTable, string, uuid } from "semola/orm";
185
+ import { createOrm, defineTable, json, string, uuid } from "semola/orm";
176
186
 
177
- const users = createTable("users", {
178
- id: uuid("id").primaryKey(),
187
+ const users = defineTable("users", {
188
+ id: uuid("id").primaryKey().notNull(),
179
189
  name: string("name").notNull(),
180
190
  email: string("email").unique().notNull(),
191
+ metadata: json<{ plan: string }>("metadata"),
181
192
  });
182
193
 
183
194
  const db = createOrm({
184
- url: "sqlite::memory:",
195
+ adapter: "sqlite",
196
+ url: ":memory:",
185
197
  tables: { users },
186
198
  });
187
199
 
188
- // Result-pattern query
189
- const [findErr, rows] = await db.users.findMany({
200
+ const rows = await db.users.findMany({
190
201
  where: { name: { contains: "John" } },
191
202
  take: 10,
192
203
  });
193
204
 
194
- if (findErr) {
195
- console.error(findErr.type, findErr.message);
196
- }
197
-
198
- // Create record (result pattern)
199
- const [createErr, user] = await db.users.create({
205
+ const user = await db.users.create({
200
206
  data: {
207
+ id: "1",
201
208
  name: "John Doe",
202
209
  email: "john@example.com",
203
210
  },
204
211
  });
205
212
 
206
- // Low-level SQL-style methods are also available
207
- const insertedRows = await db.users.insert({
208
- data: {
209
- name: "Jane Doe",
210
- email: "jane@example.com",
211
- },
212
- returning: true,
213
- });
214
-
215
- console.log(rows, user, createErr, insertedRows);
216
- ```
217
-
218
- ### Run ORM Migrations
219
-
220
- ```bash
221
- # create migration from schema diff
222
- semola orm migrations create add-users
223
-
224
- # apply pending migrations
225
- semola orm migrations apply
226
-
227
- # rollback last applied migration
228
- semola orm migrations rollback
213
+ console.log(rows, user);
229
214
  ```
230
215
 
231
216
  ### Check Permissions
@@ -344,15 +329,15 @@ _Higher is better for req/sec, lower is better for latency._
344
329
  - [API Framework](./docs/api.md) - Type-safe REST API framework with OpenAPI
345
330
  - [Queue](./docs/queue.md) - Redis-backed job queue with timeouts & concurrency
346
331
  - [PubSub](./docs/pubsub.md) - Type-safe Redis pub/sub
347
- - [Cron](./docs/cron.md) - In-memory cron scheduler for periodic task execution
348
- - [Workflow](./docs/workflow.md) - Durable and resumable workflows with step persistence
332
+ - [Cron](./docs/cron.md) - In-memory and OS cron scheduler for periodic task execution
333
+ - [Workflow](./docs/workflow.md) - Durable and resumable workflows with retries and hooks
349
334
  - [Policy](./docs/policy.md) - Policy-based authorization
350
335
  - [i18n](./docs/i18n.md) - Type-safe internationalization
351
336
  - [Cache](./docs/cache.md) - Redis cache wrapper with TTL
352
337
  - [Errors](./docs/errors.md) - Result-based error handling
353
338
  - [Logging](./docs/logging.md) - Logging utility
354
339
  - [Prompts](./docs/prompts.md) - Interactive CLI prompts
355
- - [ORM](./docs/orm.md) - Type-safe data layer, result-pattern DX, and migrations
340
+ - [ORM](./docs/orm.md) - Type-safe data layer with SQLite support
356
341
 
357
342
  ---
358
343