@toonstore/torm 0.1.0 β†’ 0.3.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.
package/README.md CHANGED
@@ -1,17 +1,32 @@
1
1
  # @toonstore/torm
2
2
 
3
- > ToonStore ORM Client for Node.js & TypeScript
3
+ > ToonStoreDB ORM Client for Node.js & TypeScript
4
4
 
5
- A Mongoose-style ORM client for ToonStore, providing type-safe models, validation, and query building.
5
+ A Mongoose-style ORM client for ToonStoreDB, providing type-safe models, validation, and query building.
6
+
7
+ > **What is ToonStoreDB?** ToonStoreDB is a blazingly fast embedded database with a built-in caching layer. This SDK lets you work with ToonStoreDB using a familiar Mongoose-like API.
8
+
9
+ ## 🎯 Quick Summary
10
+
11
+ **What is this?** A TypeScript/JavaScript ORM library (like Mongoose for MongoDB or Drizzle for PostgreSQL) that lets you easily work with ToonStoreDB.
12
+
13
+ **What do I need?**
14
+ - βœ… ToonStoreDB server running (the database)
15
+ - βœ… This npm package (`@toonstore/torm`)
16
+ - βœ… Node.js/TypeScript project
17
+
18
+ **What is TORM Studio?** An optional visual management tool (like phpMyAdmin or MongoDB Compass) for managing your ToonStoreDB data - NOT required for the SDK to work.
6
19
 
7
20
  ## ✨ Features
8
21
 
9
- - βœ… **Type-Safe** - Full TypeScript support
10
- - βœ… **Validation** - Built-in validators (email, URL, min/max, patterns)
11
- - βœ… **Query Builder** - Fluent API for filtering and sorting
12
- - βœ… **Schema Definition** - Define your data models
13
- - βœ… **Async/Await** - Promise-based API
14
- - βœ… **HTTP-Based** - Communicates with TORM Server
22
+ - βœ… **Type-Safe** - Full TypeScript support with generics
23
+ - βœ… **Direct Database Connection** - Connects directly to ToonStoreDB
24
+ - βœ… **Mongoose-Like API** - Familiar API for easy adoption
25
+ - βœ… **Schema Validation** - Built-in validators (email, URL, min/max, patterns, custom)
26
+ - βœ… **Query Builder** - Fluent API for filtering, sorting, and pagination
27
+ - βœ… **Async/Await** - Modern promise-based API
28
+ - βœ… **Auto-Generated IDs** - Automatic document ID generation with timestamps
29
+ - βœ… **Zero Configuration** - Works out of the box with sensible defaults
15
30
 
16
31
  ## πŸ“¦ Installation
17
32
 
@@ -19,37 +34,52 @@ A Mongoose-style ORM client for ToonStore, providing type-safe models, validatio
19
34
  npm install @toonstore/torm
20
35
  # or
21
36
  yarn add @toonstore/torm
37
+ # or
38
+ pnpm add @toonstore/torm
22
39
  ```
23
40
 
24
41
  ## πŸš€ Quick Start
25
42
 
26
43
  ### Prerequisites
27
44
 
28
- Make sure TORM server is running:
45
+ **ToonStoreDB must be running:**
46
+
29
47
  ```bash
30
- cargo run --package torm-server --release
31
- # Server runs on http://localhost:3001
48
+ # Start ToonStoreDB
49
+ ./toonstoredb
50
+ # Default: localhost:6379
51
+
52
+ # Or using Docker
53
+ docker run -d -p 6379:6379 toonstoredb/toonstoredb:latest
54
+
55
+ # Or install from releases
56
+ # Download from: https://github.com/toonstore/toonstoredb/releases
32
57
  ```
33
58
 
59
+ > **Note:** ToonStoreDB is a high-performance database with Redis-compatible protocol. The SDK connects directly to your ToonStoreDB instance. No additional servers or middleware needed.
60
+
34
61
  ### Basic Usage
35
62
 
36
63
  ```typescript
37
64
  import { TormClient } from '@toonstore/torm';
38
65
 
39
- // 1. Create client
66
+ // 1. Connect to ToonStoreDB
40
67
  const torm = new TormClient({
41
- baseURL: 'http://localhost:3001',
42
- timeout: 5000,
68
+ host: 'localhost',
69
+ port: 6379,
70
+ // Or use connection URL:
71
+ // url: 'toonstoredb://localhost:6379'
43
72
  });
44
73
 
45
- // 2. Define model with schema
74
+ // 2. Define your model interface
46
75
  interface User {
47
- id: string;
48
76
  name: string;
49
77
  email: string;
50
78
  age: number;
79
+ website?: string;
51
80
  }
52
81
 
82
+ // 3. Create model with validation schema
53
83
  const User = torm.model<User>('User', {
54
84
  name: {
55
85
  type: 'string',
@@ -68,35 +98,118 @@ const User = torm.model<User>('User', {
68
98
  min: 13,
69
99
  max: 120,
70
100
  },
101
+ website: {
102
+ type: 'string',
103
+ url: true,
104
+ },
71
105
  });
72
106
 
73
- // 3. Create document
107
+ // 4. Create document (ID auto-generated)
74
108
  const user = await User.create({
75
- id: 'user:1',
76
109
  name: 'Alice',
77
110
  email: 'alice@example.com',
78
111
  age: 30,
112
+ website: 'https://alice.dev',
79
113
  });
114
+ console.log(user._id); // Auto-generated: "1736076000000-abc123xyz"
80
115
 
81
- // 4. Find documents
116
+ // 5. Find documents
82
117
  const allUsers = await User.find();
83
- const specificUser = await User.findById('user:1');
118
+ const specificUser = await User.findById(user._id);
119
+ const foundUser = await User.findOne({ email: 'alice@example.com' });
84
120
 
85
- // 5. Query with filters
121
+ // 6. Query with filters
86
122
  const adults = await User.query()
87
- .filter('age', 'gte', 18)
123
+ .where('age', 'gte', 18)
88
124
  .sort('name', 'asc')
89
125
  .limit(10)
90
126
  .exec();
91
127
 
92
- // 6. Update document
93
- await User.update('user:1', { age: 31 });
128
+ // 7. Update document
129
+ const updated = await User.update(user._id, { age: 31 });
94
130
 
95
- // 7. Delete document
96
- await User.delete('user:1');
131
+ // 8. Delete document
132
+ await User.delete(user._id);
97
133
 
98
- // 8. Count documents
134
+ // 9. Delete many documents
135
+ await User.deleteMany({ age: { $lt: 18 } });
136
+
137
+ // 10. Count documents
99
138
  const count = await User.count();
139
+
140
+ // 11. Disconnect when done
141
+ await torm.disconnect();
142
+ ```
143
+
144
+ ## πŸ› οΈ CLI Commands
145
+
146
+ TORM includes a powerful CLI for database management and development workflows:
147
+
148
+ ### TORM Studio
149
+
150
+ Launch the visual database manager (bundled with the SDK):
151
+
152
+ ```bash
153
+ # Start TORM Studio
154
+ npx torm studio
155
+ ```
156
+
157
+ **First Run:** If no `torm.config.ts` exists, it will be auto-generated. Edit it with your database credentials, then run `npx torm studio` again.
158
+
159
+ **Access:** http://localhost:4983
160
+
161
+ **Configuration:**
162
+ Create or edit `torm.config.ts` in your project root:
163
+ ```typescript
164
+ export default {
165
+ dbCredentials: {
166
+ host: 'localhost', // or cloud host
167
+ port: 6379,
168
+ // For cloud/auth:
169
+ // password: 'your-password',
170
+ // url: 'redis://user:pass@host:port',
171
+ },
172
+ studio: {
173
+ port: 4983, // default
174
+ },
175
+ };
176
+ ```
177
+
178
+ **Features:**
179
+ - πŸ“Š Visual database browser - browse collections and documents
180
+ - ✏️ Create, read, update, delete records with UI
181
+ - πŸ” Search and filter data
182
+ - πŸ“ˆ Real-time database statistics
183
+ - 🎨 Modern dark-themed interface
184
+ - βœ… No separate installation - fully bundled with npm package!
185
+ - 🌐 Works with local or cloud-hosted ToonStoreDB
186
+
187
+ **Custom Port:**
188
+ ```bash
189
+ npx torm studio --port 8080
190
+ ```
191
+
192
+ ### Migrations (Coming Soon)
193
+
194
+ ```bash
195
+ # Generate migration from schema changes
196
+ npx torm generate
197
+
198
+ # Run pending migrations
199
+ npx torm migrate
200
+
201
+ # Check migration status
202
+ npx torm migrate:status
203
+ ```
204
+
205
+ ### Type Generation (Coming Soon)
206
+
207
+ ```bash
208
+ # Generate TypeScript types from your schema
209
+ npx torm generate
210
+
211
+ # Watch mode for development
212
+ npx torm generate --watch
100
213
  ```
101
214
 
102
215
  ## πŸ“– API Documentation
@@ -110,32 +223,45 @@ const torm = new TormClient(options?: TormClientOptions);
110
223
  ```
111
224
 
112
225
  **Options:**
113
- - `baseURL` (string) - TORM server URL (default: `http://localhost:3001`)
114
- - `timeout` (number) - Request timeout in ms (default: `5000`)
226
+ - `host` (string) - ToonStoreDB host (default: `localhost`)
227
+ - `port` (number) - ToonStoreDB port (default: `6379`)
228
+ - `url` (string) - ToonStoreDB connection URL (e.g., `toonstoredb://localhost:6379`)
229
+ - All standard connection options are supported
115
230
 
116
231
  #### Methods
117
232
 
118
233
  - `model<T>(name, schema?, options?)` - Create a model
119
- - `health()` - Check server health
120
- - `info()` - Get server info
234
+ - `health()` - Check database connection health
235
+ - `disconnect()` - Close database connection
236
+ - `isConnected()` - Check if connected to database
237
+ - `getClient()` - Get underlying database client instance
121
238
 
122
239
  ### Model
123
240
 
241
+ All documents automatically include:
242
+ - `_id` - Auto-generated unique ID
243
+ - `_createdAt` - ISO timestamp of creation
244
+ - `_updatedAt` - ISO timestamp of last update
245
+
124
246
  #### CRUD Operations
125
247
 
126
248
  ```typescript
127
- // Create
249
+ // Create (ID auto-generated)
128
250
  const doc = await Model.create(data);
251
+ // Returns: { ...data, _id, _createdAt, _updatedAt }
129
252
 
130
253
  // Read
131
254
  const all = await Model.find();
132
255
  const one = await Model.findById(id);
256
+ const match = await Model.findOne({ email: 'user@example.com' });
133
257
 
134
258
  // Update
135
259
  const updated = await Model.update(id, data);
136
260
 
137
261
  // Delete
138
262
  const deleted = await Model.delete(id);
263
+ await Model.deleteMany(); // Delete all
264
+ await Model.deleteMany({ status: 'inactive' }); // Delete matching
139
265
 
140
266
  // Count
141
267
  const count = await Model.count();
@@ -145,16 +271,16 @@ const count = await Model.count();
145
271
 
146
272
  ```typescript
147
273
  const results = await Model.query()
148
- .filter(field, operator, value) // Add filter
149
- .where(field, value) // Shorthand for .filter(field, 'eq', value)
150
- .sort(field, order) // Sort results
274
+ .where(field, operator, value) // Add filter
275
+ .equals(field, value) // Shorthand for .where(field, 'eq', value)
276
+ .sort(field, order) // Sort results ('asc' | 'desc')
151
277
  .limit(n) // Limit results
152
- .skip(n) // Skip results
278
+ .skip(n) // Skip results (pagination)
153
279
  .exec(); // Execute query
154
280
 
155
281
  // Count with filters
156
282
  const count = await Model.query()
157
- .filter('status', 'eq', 'active')
283
+ .where('status', 'eq', 'active')
158
284
  .count();
159
285
  ```
160
286
 
@@ -214,16 +340,26 @@ age: { type: 'number', min: 0, max: 120 }
214
340
  // Pattern matching
215
341
  phone: { type: 'string', pattern: /^\d{10}$/ }
216
342
 
217
- // Custom validation
343
+ // Custom validation (sync or async)
218
344
  username: {
219
345
  type: 'string',
220
346
  validate: (value) => /^[a-z0-9_]+$/.test(value),
221
347
  }
348
+
349
+ // Async custom validation
350
+ email: {
351
+ type: 'string',
352
+ validate: async (value) => {
353
+ // Check if email is already taken
354
+ const existing = await User.findOne({ email: value });
355
+ return !existing;
356
+ },
357
+ }
222
358
  ```
223
359
 
224
360
  ## πŸ“ Examples
225
361
 
226
- ### Example 1: User Management
362
+ ### Example 1: User Management System
227
363
 
228
364
  ```typescript
229
365
  import { TormClient } from '@toonstore/torm';
@@ -231,10 +367,10 @@ import { TormClient } from '@toonstore/torm';
231
367
  const torm = new TormClient();
232
368
 
233
369
  interface User {
234
- id: string;
235
370
  username: string;
236
371
  email: string;
237
372
  age: number;
373
+ role: 'admin' | 'user';
238
374
  }
239
375
 
240
376
  const User = torm.model<User>('User', {
@@ -253,33 +389,43 @@ const User = torm.model<User>('User', {
253
389
  type: 'number',
254
390
  min: 18,
255
391
  },
392
+ role: {
393
+ type: 'string',
394
+ required: true,
395
+ },
256
396
  });
257
397
 
258
398
  // Create users
259
- await User.create({
260
- id: 'user:1',
399
+ const alice = await User.create({
261
400
  username: 'alice',
262
401
  email: 'alice@example.com',
263
402
  age: 30,
403
+ role: 'admin',
264
404
  });
265
405
 
266
- // Find adults
267
- const adults = await User.query()
268
- .filter('age', 'gte', 18)
406
+ // Find by role
407
+ const admins = await User.query()
408
+ .where('role', 'eq', 'admin')
269
409
  .sort('username', 'asc')
270
410
  .exec();
411
+
412
+ // Find adults sorted by age
413
+ const adults = await User.query()
414
+ .where('age', 'gte', 18)
415
+ .sort('age', 'desc')
416
+ .exec();
271
417
  ```
272
418
 
273
- ### Example 2: Blog Posts
419
+ ### Example 2: Blog Posts with Tags
274
420
 
275
421
  ```typescript
276
422
  interface Post {
277
- id: string;
278
423
  title: string;
279
424
  content: string;
280
- author_id: string;
425
+ authorId: string;
281
426
  published: boolean;
282
427
  tags: string[];
428
+ views: number;
283
429
  }
284
430
 
285
431
  const Post = torm.model<Post>('Post', {
@@ -294,7 +440,7 @@ const Post = torm.model<Post>('Post', {
294
440
  required: true,
295
441
  minLength: 10,
296
442
  },
297
- author_id: {
443
+ authorId: {
298
444
  type: 'string',
299
445
  required: true,
300
446
  },
@@ -304,41 +450,48 @@ const Post = torm.model<Post>('Post', {
304
450
  tags: {
305
451
  type: 'array',
306
452
  },
453
+ views: {
454
+ type: 'number',
455
+ min: 0,
456
+ },
307
457
  });
308
458
 
309
459
  // Create post
310
- await Post.create({
311
- id: 'post:1',
460
+ const post = await Post.create({
312
461
  title: 'Getting Started with TORM',
313
- content: 'TORM is an amazing ORM for ToonStore...',
314
- author_id: 'user:1',
462
+ content: 'TORM is an amazing ORM for ToonStoreDB with type-safe models and validation...',
463
+ authorId: alice._id,
315
464
  published: true,
316
- tags: ['orm', 'database', 'toonstore'],
465
+ tags: ['orm', 'database', 'toonstore', 'redis'],
466
+ views: 0,
317
467
  });
318
468
 
319
469
  // Find published posts
320
- const published = await Post.query()
321
- .filter('published', 'eq', true)
322
- .sort('created_at', 'desc')
470
+ const publishedPosts = await Post.query()
471
+ .where('published', 'eq', true)
472
+ .sort('_createdAt', 'desc')
323
473
  .limit(10)
324
474
  .exec();
325
475
 
326
476
  // Find posts by author
327
- const authorPosts = await Post.query()
328
- .filter('author_id', 'eq', 'user:1')
477
+ const alicePosts = await Post.query()
478
+ .where('authorId', 'eq', alice._id)
329
479
  .exec();
480
+
481
+ // Update view count
482
+ await Post.update(post._id, { views: post.views + 1 });
330
483
  ```
331
484
 
332
- ### Example 3: E-Commerce Products
485
+ ### Example 3: E-Commerce Product Catalog
333
486
 
334
487
  ```typescript
335
488
  interface Product {
336
- id: string;
337
489
  name: string;
338
490
  price: number;
339
491
  stock: number;
340
492
  sku: string;
341
493
  category: string;
494
+ description?: string;
342
495
  }
343
496
 
344
497
  const Product = torm.model<Product>('Product', {
@@ -363,31 +516,82 @@ const Product = torm.model<Product>('Product', {
363
516
  },
364
517
  });
365
518
 
366
- // Create product
367
- await Product.create({
368
- id: 'product:1',
369
- name: 'Laptop',
370
- price: 999.99,
371
- stock: 50,
519
+ // Create products
520
+ const laptop = await Product.create({
521
+ name: 'Gaming Laptop',
522
+ price: 1299.99,
523
+ stock: 15,
372
524
  sku: 'LAP-12345',
373
525
  category: 'electronics',
526
+ description: 'High-performance gaming laptop with RTX 4070',
374
527
  });
375
528
 
376
529
  // Find products in stock
377
530
  const inStock = await Product.query()
378
- .filter('stock', 'gt', 0)
531
+ .where('stock', 'gt', 0)
379
532
  .sort('price', 'asc')
380
533
  .exec();
381
534
 
535
+ // Find by category with pagination
536
+ const electronics = await Product.query()
537
+ .where('category', 'eq', 'electronics')
538
+ .sort('name', 'asc')
539
+ .skip(0)
540
+ .limit(20)
541
+ .exec();
542
+
382
543
  // Find expensive products
383
- const expensive = await Product.query()
384
- .filter('price', 'gte', 1000)
544
+ const premium = await Product.query()
545
+ .where('price', 'gte', 1000)
546
+ .sort('price', 'desc')
547
+ .exec();
548
+
549
+ // Low stock alert
550
+ const lowStock = await Product.query()
551
+ .where('stock', 'lt', 10)
385
552
  .exec();
386
553
  ```
387
554
 
555
+ ## πŸ”„ Migration from v0.1.x
556
+
557
+ Version 0.2.0 introduces breaking changes:
558
+
559
+ ### What Changed
560
+ - **No more HTTP server required** - SDK now connects directly to ToonStoreDB
561
+ - **Auto-generated IDs** - No need to provide `id` field, use `_id` instead
562
+ - **New connection options** - Use `host`, `port`, or `url` for ToonStoreDB connection
563
+
564
+ ### Migration Steps
565
+
566
+ ```typescript
567
+ // v0.1.x (OLD - required TORM server via HTTP)
568
+ const torm = new TormClient({
569
+ baseURL: 'http://localhost:3001',
570
+ });
571
+
572
+ const user = await User.create({
573
+ id: 'user:1',
574
+ name: 'Alice',
575
+ });
576
+
577
+ // v0.2.x (NEW - direct ToonStoreDB connection)
578
+ const torm = new TormClient({
579
+ host: 'localhost',
580
+ port: 6379,
581
+ });
582
+
583
+ const user = await User.create({
584
+ name: 'Alice', // No id needed
585
+ });
586
+ console.log(user._id); // Auto-generated
587
+ ```
588
+
388
589
  ## πŸ§ͺ Running Examples
389
590
 
390
591
  ```bash
592
+ # Make sure ToonStoreDB is running
593
+ ./toonstoredb
594
+
391
595
  # Install dependencies
392
596
  npm install
393
597
 
@@ -398,7 +602,7 @@ npm run build
398
602
  npm run example
399
603
  ```
400
604
 
401
- ## πŸ› οΈ Development
605
+ ## πŸ—οΈ Development
402
606
 
403
607
  ```bash
404
608
  # Install dependencies
@@ -407,15 +611,93 @@ npm install
407
611
  # Build TypeScript
408
612
  npm run build
409
613
 
410
- # Watch mode (auto-rebuild)
614
+ # Watch mode (auto-rebuild on changes)
411
615
  npm run dev
616
+
617
+ # Run tests
618
+ npm test
619
+ ```
620
+
621
+ ## 🌟 Why TORM?
622
+
623
+ - **Standalone SDK** - No TORM server needed, just ToonStoreDB
624
+ - **Type Safety** - Full TypeScript support with generics
625
+ - **Familiar API** - If you know Mongoose, you know TORM
626
+ - **High Performance** - Leverages ToonStoreDB's blazing fast speed (5.28M ops/sec)
627
+ - **Validation Built-in** - Comprehensive validation without extra libraries
628
+ - **Modern Async/Await** - Clean, readable promise-based code
629
+
630
+ ## 🏒 Architecture
631
+
412
632
  ```
633
+ Your Node.js App β†’ @toonstore/torm SDK β†’ ToonStoreDB
634
+ ```
635
+
636
+ **What you need to run:**
637
+ - βœ… ToonStoreDB server (the database)
638
+ - βœ… Your Node.js app with this SDK
639
+
640
+ **What you DON'T need:**
641
+ - ❌ TORM Server (that's only for TORM Studio UI - a separate optional visual management tool)
642
+ - ❌ Any HTTP server or API layer
643
+ - ❌ Redis installation (ToonStoreDB is the database, it just uses Redis-compatible protocol internally)
644
+
645
+ ## πŸ—ΊοΈ Roadmap
646
+
647
+ **SDK Features:**
648
+ - [ ] Relationships (references between models)
649
+ - [ ] Hooks (pre/post save, update, delete)
650
+ - [ ] Indexes and search optimization
651
+ - [ ] Transactions support
652
+ - [ ] Aggregation pipelines
653
+ - [ ] Virtual fields
654
+ - [ ] Plugins system
655
+
656
+ **CLI Tools:**
657
+ - [x] TORM Studio (bundled Node.js server) βœ…
658
+ - [ ] Schema migrations (`torm generate`, `torm migrate`)
659
+ - [ ] Type generation from schemas
660
+ - [ ] Database seeding
661
+ - [ ] Schema validation and linting
662
+
663
+ **Studio Enhancements:**
664
+ - [ ] Custom domain (local.torm.studio via DNS) - no hosts file needed
665
+ - [ ] Advanced query builder UI
666
+ - [ ] Export/import data
667
+ - [ ] Real-time collaboration
668
+ - [ ] Schema visualization
669
+
670
+ **Migration System (Planned):**
671
+ ```bash
672
+ # Generate migration from model changes
673
+ torm generate --name add_users_table
674
+
675
+ # Run migrations
676
+ torm migrate
677
+
678
+ # Rollback
679
+ torm migrate:rollback
680
+
681
+ # Check status
682
+ torm migrate:status
683
+ ```
684
+
685
+ ## 🀝 Contributing
686
+
687
+ Contributions are welcome! Please feel free to submit a Pull Request.
413
688
 
414
689
  ## πŸ“„ License
415
690
 
416
691
  MIT
417
692
 
418
- ## πŸ™ Credits
693
+ ## πŸ”— Links
694
+
695
+ - [ToonStoreDB](https://github.com/toonstore/toonstoredb) - The blazingly fast database
696
+ - [TORM](https://github.com/toonstore/torm) - Multi-language ORM with Studio
697
+ - [TOON Format](https://github.com/toon-format/toon) - The data format specification
698
+ - [NPM Package](https://www.npmjs.com/package/@toonstore/torm)
699
+ - [TORM Studio Guide](https://github.com/toonstore/torm/blob/main/docs/STUDIO.md)
700
+
701
+ ## πŸ’¬ Support
419
702
 
420
- - Inspired by [Mongoose](https://mongoosejs.com/)
421
- - Built for [ToonStore](https://github.com/toon-format/toon)
703
+ For questions and support, please open an issue on GitHub.
package/dist/cli.d.ts ADDED
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env node
2
+ /**
3
+ * TORM CLI
4
+ * Command-line interface for ToonStoreDB ORM
5
+ */
6
+ export {};
7
+ //# sourceMappingURL=cli.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"cli.d.ts","sourceRoot":"","sources":["../src/cli.ts"],"names":[],"mappings":";AAEA;;;GAGG"}