jotdb 0.1.5 → 0.1.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.
- package/README.md +105 -27
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,16 +1,56 @@
|
|
|
1
1
|
# JotDB
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
[](https://deploy.workers.cloudflare.com/?url=https://github.com/acoyfellow/jotdb)
|
|
4
|
+
|
|
5
|
+
A lightweight, schema-less database built on Cloudflare Durable Objects. Think of it as Firestore's security rules, but with Zod validation built-in. Perfect for both internal and external APIs, with automatic type safety and validation.
|
|
6
|
+
|
|
7
|
+
> **Cloudflare Products**: JotDB works with any Cloudflare product that supports Durable Objects:
|
|
8
|
+
> - Cloudflare Workers
|
|
9
|
+
> - Cloudflare Pages (with Functions)
|
|
10
|
+
> - Cloudflare Workflows
|
|
11
|
+
> - Cloudflare Queues
|
|
12
|
+
> - Cloudflare Cron Triggers
|
|
4
13
|
|
|
5
14
|
## Why JotDB?
|
|
6
15
|
|
|
7
|
-
|
|
16
|
+
JotDB combines the best of both worlds: the simplicity of NoSQL with the safety of schema validation. Here's what makes it special:
|
|
17
|
+
|
|
18
|
+
- **Built-in Type Safety**: Automatic Zod validation ensures your data is always in the right shape
|
|
19
|
+
- **Edge-Native**: Runs directly on Cloudflare's edge network, with sub-millisecond latency
|
|
20
|
+
- **RPC-First**: Direct method calls instead of HTTP endpoints (though you can easily wrap it in HTTP)
|
|
21
|
+
- **Durable Storage**: Built on Durable Objects for reliable, consistent storage
|
|
22
|
+
- **Zero Setup**: No database configuration, no connection strings, just instantiate and go
|
|
23
|
+
- **Perfect for APIs**: Use it as an internal database or wrap it with auth for external APIs
|
|
24
|
+
- **Real-time Ready**: Durable Objects provide strong consistency guarantees
|
|
25
|
+
|
|
26
|
+
Perfect for:
|
|
27
|
+
- Quick prototypes that need data validation
|
|
28
|
+
- Small to medium applications that need reliable storage
|
|
29
|
+
- Serverless environments where you want type safety
|
|
30
|
+
- Real-time data storage with strong consistency
|
|
31
|
+
- Collaborative applications that need data validation
|
|
32
|
+
- APIs that need both flexibility and safety
|
|
33
|
+
|
|
34
|
+
## Design Patterns
|
|
8
35
|
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
36
|
+
JotDB uses Cloudflare Durable Objects under the hood, which means you can organize your data in several ways:
|
|
37
|
+
|
|
38
|
+
1. **Global Store**: Use a single instance for your entire application
|
|
39
|
+
```typescript
|
|
40
|
+
const db = env.JOTDB.get(env.JOTDB.idFromName("global"));
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
2. **Per-User Store**: Create a separate instance for each user
|
|
44
|
+
```typescript
|
|
45
|
+
const userDb = env.JOTDB.get(env.JOTDB.idFromName(`user:${userId}`));
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
3. **Per-Event Store**: Create temporary stores for events or sessions
|
|
49
|
+
```typescript
|
|
50
|
+
const eventDb = env.JOTDB.get(env.JOTDB.idFromName(`event:${eventId}`));
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
Each instance is isolated and can have its own schema and options. This follows the Actor Model pattern, where each instance is an independent actor that manages its own state.
|
|
14
54
|
|
|
15
55
|
## Installation
|
|
16
56
|
|
|
@@ -25,24 +65,47 @@ yarn add jotdb
|
|
|
25
65
|
pnpm add jotdb
|
|
26
66
|
```
|
|
27
67
|
|
|
68
|
+
### Configure wrangler.jsonc
|
|
69
|
+
|
|
70
|
+
```jsonc
|
|
71
|
+
{
|
|
72
|
+
"durable_objects": {
|
|
73
|
+
"bindings": [
|
|
74
|
+
{
|
|
75
|
+
"name": "JOTDB",
|
|
76
|
+
"class_name": "JotDB"
|
|
77
|
+
}
|
|
78
|
+
]
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
```
|
|
82
|
+
|
|
28
83
|
## Full Example
|
|
29
84
|
|
|
30
85
|
```typescript
|
|
31
86
|
import { JotDB } from 'jotdb';
|
|
32
87
|
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
// Set a value
|
|
38
|
-
await db.set("user:123", { name: "John", age: 30 });
|
|
39
|
-
|
|
40
|
-
// Get a value
|
|
41
|
-
const user = await db.get("user:123");
|
|
42
|
-
console.log(user); // { name: "John", age: 30 }
|
|
88
|
+
export interface Env {
|
|
89
|
+
JOTDB: DurableObjectNamespace;
|
|
90
|
+
}
|
|
43
91
|
|
|
44
|
-
|
|
45
|
-
|
|
92
|
+
export default {
|
|
93
|
+
async fetch(request: Request, env: Env) {
|
|
94
|
+
// Initialize the database
|
|
95
|
+
const jotId = env.JOTDB.idFromName("my-db");
|
|
96
|
+
const db = env.JOTDB.get(jotId);
|
|
97
|
+
|
|
98
|
+
// Example operations
|
|
99
|
+
await db.set("user:123", { name: "John", age: 30 });
|
|
100
|
+
const user = await db.get("user:123");
|
|
101
|
+
await db.delete("user:123");
|
|
102
|
+
|
|
103
|
+
// Return the result
|
|
104
|
+
return new Response(JSON.stringify({ user }), {
|
|
105
|
+
headers: { 'Content-Type': 'application/json' }
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
};
|
|
46
109
|
```
|
|
47
110
|
|
|
48
111
|
## API Reference
|
|
@@ -52,19 +115,34 @@ await db.delete("user:123");
|
|
|
52
115
|
| `set(key, value)` | Store a value | `key: string`, `value: any` | `Promise<void>` |
|
|
53
116
|
| `get(key)` | Retrieve a value | `key: string` | `Promise<any>` |
|
|
54
117
|
| `delete(key)` | Remove a value | `key: string` | `Promise<void>` |
|
|
55
|
-
| `
|
|
56
|
-
|
|
57
|
-
|
|
118
|
+
| `clear()` | Remove all values | none | `Promise<void>` |
|
|
119
|
+
| `keys()` | Get all keys | none | `Promise<string[]>` |
|
|
120
|
+
| `has(key)` | Check if key exists | `key: string` | `Promise<boolean>` |
|
|
121
|
+
| `getAll()` | Get all data | none | `Promise<Record<string, unknown> \| unknown[]>` |
|
|
122
|
+
| `setAll(objOrArr)` | Set all data at once | `objOrArr: Record<string, unknown> \| unknown[]` | `Promise<void>` |
|
|
123
|
+
| `push(item)` | Add item to array | `item: unknown` | `Promise<void>` |
|
|
124
|
+
| `getSchema()` | Get current schema | none | `Promise<SchemaDefinition>` |
|
|
125
|
+
| `setSchema(schema)` | Set data schema | `schema: SchemaDefinition` | `Promise<void>` |
|
|
126
|
+
| `getOptions()` | Get current options | none | `Promise<JotDBOptions>` |
|
|
127
|
+
| `setOptions(opts)` | Set database options | `opts: Partial<JotDBOptions>` | `Promise<void>` |
|
|
128
|
+
| `getAuditLog()` | Get audit log entries | none | `Promise<AuditLogEntry[]>` |
|
|
129
|
+
| `clearAuditLog()` | Clear audit log | none | `Promise<void>` |
|
|
130
|
+
|
|
131
|
+
### Options
|
|
58
132
|
|
|
59
133
|
```typescript
|
|
60
|
-
interface
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
delete(key: string): Promise<void>;
|
|
64
|
-
list(prefix?: string): Promise<string[]>;
|
|
134
|
+
interface JotDBOptions {
|
|
135
|
+
autoStrip: boolean; // Automatically strip unknown fields
|
|
136
|
+
readOnly: boolean; // Enable read-only mode
|
|
65
137
|
}
|
|
66
138
|
```
|
|
67
139
|
|
|
140
|
+
### Schema Types
|
|
141
|
+
|
|
142
|
+
```typescript
|
|
143
|
+
type SchemaType = "string" | "number" | "boolean" | "email" | "array" | "object" | "any";
|
|
144
|
+
```
|
|
145
|
+
|
|
68
146
|
## License
|
|
69
147
|
|
|
70
148
|
MIT License - feel free to use this in your own projects!
|