bunsane 0.1.0 → 0.1.2

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 (82) hide show
  1. package/.github/workflows/deploy-docs.yml +57 -0
  2. package/LICENSE.md +1 -1
  3. package/README.md +2 -28
  4. package/TODO.md +8 -1
  5. package/bun.lock +3 -0
  6. package/config/upload.config.ts +135 -0
  7. package/core/App.ts +168 -4
  8. package/core/ArcheType.ts +122 -0
  9. package/core/BatchLoader.ts +100 -0
  10. package/core/ComponentRegistry.ts +4 -3
  11. package/core/Components.ts +2 -2
  12. package/core/Decorators.ts +15 -8
  13. package/core/Entity.ts +193 -14
  14. package/core/EntityCache.ts +15 -0
  15. package/core/EntityHookManager.ts +855 -0
  16. package/core/EntityManager.ts +12 -2
  17. package/core/ErrorHandler.ts +64 -7
  18. package/core/FileValidator.ts +284 -0
  19. package/core/Query.ts +503 -85
  20. package/core/RequestContext.ts +24 -0
  21. package/core/RequestLoaders.ts +89 -0
  22. package/core/SchedulerManager.ts +710 -0
  23. package/core/UploadManager.ts +261 -0
  24. package/core/components/UploadComponent.ts +206 -0
  25. package/core/decorators/EntityHooks.ts +190 -0
  26. package/core/decorators/ScheduledTask.ts +83 -0
  27. package/core/events/EntityLifecycleEvents.ts +177 -0
  28. package/core/processors/ImageProcessor.ts +423 -0
  29. package/core/storage/LocalStorageProvider.ts +290 -0
  30. package/core/storage/StorageProvider.ts +112 -0
  31. package/database/DatabaseHelper.ts +183 -58
  32. package/database/index.ts +5 -5
  33. package/database/sqlHelpers.ts +7 -0
  34. package/docs/README.md +149 -0
  35. package/docs/_coverpage.md +36 -0
  36. package/docs/_sidebar.md +23 -0
  37. package/docs/api/core.md +568 -0
  38. package/docs/api/hooks.md +554 -0
  39. package/docs/api/index.md +222 -0
  40. package/docs/api/query.md +678 -0
  41. package/docs/api/service.md +744 -0
  42. package/docs/core-concepts/archetypes.md +512 -0
  43. package/docs/core-concepts/components.md +498 -0
  44. package/docs/core-concepts/entity.md +314 -0
  45. package/docs/core-concepts/hooks.md +683 -0
  46. package/docs/core-concepts/query.md +588 -0
  47. package/docs/core-concepts/services.md +647 -0
  48. package/docs/examples/code-examples.md +425 -0
  49. package/docs/getting-started.md +337 -0
  50. package/docs/index.html +97 -0
  51. package/gql/Generator.ts +58 -35
  52. package/gql/decorators/Upload.ts +176 -0
  53. package/gql/helpers.ts +67 -0
  54. package/gql/index.ts +65 -31
  55. package/gql/types.ts +1 -1
  56. package/index.ts +79 -11
  57. package/package.json +19 -10
  58. package/rest/Generator.ts +3 -0
  59. package/rest/index.ts +22 -0
  60. package/service/Service.ts +1 -1
  61. package/service/ServiceRegistry.ts +10 -6
  62. package/service/index.ts +12 -1
  63. package/tests/bench/insert.bench.ts +59 -0
  64. package/tests/bench/relations.bench.ts +269 -0
  65. package/tests/bench/sorting.bench.ts +415 -0
  66. package/tests/component-hooks.test.ts +1409 -0
  67. package/tests/component.test.ts +338 -0
  68. package/tests/errorHandling.test.ts +155 -0
  69. package/tests/hooks.test.ts +666 -0
  70. package/tests/query-sorting.test.ts +101 -0
  71. package/tests/relations.test.ts +169 -0
  72. package/tests/scheduler.test.ts +724 -0
  73. package/tsconfig.json +35 -34
  74. package/types/graphql.types.ts +87 -0
  75. package/types/hooks.types.ts +141 -0
  76. package/types/scheduler.types.ts +165 -0
  77. package/types/upload.types.ts +184 -0
  78. package/upload/index.ts +140 -0
  79. package/utils/UploadHelper.ts +305 -0
  80. package/utils/cronParser.ts +366 -0
  81. package/utils/errorMessages.ts +151 -0
  82. package/core/Events.ts +0 -0
@@ -0,0 +1,568 @@
1
+ # Core API Reference
2
+
3
+ This page provides detailed API reference for BunSane's core classes and functionality.
4
+
5
+ ## 🏗️ Entity Class
6
+
7
+ The `Entity` class is the fundamental building block of BunSane's ECS architecture.
8
+
9
+ ### Constructor
10
+
11
+ ```typescript
12
+ new Entity(id?: string)
13
+ ```
14
+
15
+ **Parameters:**
16
+ - `id` (optional): String - Unique identifier for the entity
17
+
18
+ ### Static Methods
19
+
20
+ #### `Entity.Create()`
21
+
22
+ Creates a new entity instance with auto-generated ID.
23
+
24
+ ```typescript
25
+ static Create(): Entity
26
+ ```
27
+
28
+ **Returns:** `Entity` - New entity instance
29
+
30
+ **Example:**
31
+ ```typescript
32
+ const user = Entity.Create();
33
+ // Entity has auto-generated ID like "01HXXXXXXXXXXXXXXXXXXXXX"
34
+ ```
35
+
36
+ #### `Entity.FindById(id)`
37
+
38
+ Finds an entity by its ID.
39
+
40
+ ```typescript
41
+ static async FindById(id: string): Promise<Entity | null>
42
+ ```
43
+
44
+ **Parameters:**
45
+ - `id`: String - Entity ID to find
46
+
47
+ **Returns:** `Promise<Entity | null>` - Found entity or null
48
+
49
+ **Example:**
50
+ ```typescript
51
+ const user = await Entity.FindById('01HXXXXXXXXXXXXXXXXXXXXX');
52
+ if (user) {
53
+ console.log('User found:', user.id);
54
+ }
55
+ ```
56
+
57
+ #### `Entity.LoadMultiple(ids)`
58
+
59
+ Loads multiple entities by their IDs.
60
+
61
+ ```typescript
62
+ static async LoadMultiple(ids: string[]): Promise<Entity[]>
63
+ ```
64
+
65
+ **Parameters:**
66
+ - `ids`: String[] - Array of entity IDs
67
+
68
+ **Returns:** `Promise<Entity[]>` - Array of found entities
69
+
70
+ **Example:**
71
+ ```typescript
72
+ const userIds = ['01HXXX...', '01HYYY...'];
73
+ const users = await Entity.LoadMultiple(userIds);
74
+ console.log(`Loaded ${users.length} users`);
75
+ ```
76
+
77
+ ### Instance Methods
78
+
79
+ #### `entity.add(component, data)`
80
+
81
+ Adds a component to the entity.
82
+
83
+ ```typescript
84
+ add<T extends BaseComponent>(
85
+ ctor: new (...args: any[]) => T,
86
+ data: Partial<ComponentDataType<T>>
87
+ ): this
88
+ ```
89
+
90
+ **Type Parameters:**
91
+ - `T`: Component class extending BaseComponent
92
+
93
+ **Parameters:**
94
+ - `ctor`: Component constructor
95
+ - `data`: Partial component data
96
+
97
+ **Returns:** `this` - Entity instance for chaining
98
+
99
+ **Example:**
100
+ ```typescript
101
+ const user = Entity.Create();
102
+ user.add(UserProfile, {
103
+ name: 'John Doe',
104
+ email: 'john@example.com'
105
+ });
106
+ ```
107
+
108
+ #### `entity.get(component)`
109
+
110
+ Gets a component from the entity.
111
+
112
+ ```typescript
113
+ get<T extends BaseComponent>(ctor: new (...args: any[]) => T): T
114
+ ```
115
+
116
+ **Type Parameters:**
117
+ - `T`: Component class
118
+
119
+ **Parameters:**
120
+ - `ctor`: Component constructor
121
+
122
+ **Returns:** `T` - Component instance
123
+
124
+ **Throws:** Error if component not found
125
+
126
+ **Example:**
127
+ ```typescript
128
+ const profile = user.get(UserProfile);
129
+ console.log(profile.name); // 'John Doe'
130
+ ```
131
+
132
+ #### `entity.has(component)`
133
+
134
+ Checks if entity has a specific component.
135
+
136
+ ```typescript
137
+ has<T extends BaseComponent>(ctor: new (...args: any[]) => T): boolean
138
+ ```
139
+
140
+ **Type Parameters:**
141
+ - `T`: Component class
142
+
143
+ **Parameters:**
144
+ - `ctor`: Component constructor
145
+
146
+ **Returns:** `boolean` - True if component exists
147
+
148
+ **Example:**
149
+ ```typescript
150
+ if (user.has(UserProfile)) {
151
+ const profile = user.get(UserProfile);
152
+ console.log('User name:', profile.name);
153
+ }
154
+ ```
155
+
156
+ #### `entity.set(component, data)`
157
+
158
+ Updates or adds a component with new data.
159
+
160
+ ```typescript
161
+ set<T extends BaseComponent>(
162
+ ctor: new (...args: any[]) => T,
163
+ data: Partial<ComponentDataType<T>>
164
+ ): this
165
+ ```
166
+
167
+ **Type Parameters:**
168
+ - `T`: Component class
169
+
170
+ **Parameters:**
171
+ - `ctor`: Component constructor
172
+ - `data`: Partial component data
173
+
174
+ **Returns:** `this` - Entity instance for chaining
175
+
176
+ **Example:**
177
+ ```typescript
178
+ user.set(UserProfile, {
179
+ name: 'Jane Doe',
180
+ email: 'jane@example.com'
181
+ });
182
+ ```
183
+
184
+ #### `entity.save()`
185
+
186
+ Saves the entity and all its components to the database.
187
+
188
+ ```typescript
189
+ async save(): Promise<void>
190
+ ```
191
+
192
+ **Returns:** `Promise<void>`
193
+
194
+ **Example:**
195
+ ```typescript
196
+ await user.save();
197
+ console.log('User saved with ID:', user.id);
198
+ ```
199
+
200
+ #### `entity.delete(force)`
201
+
202
+ Marks the entity as deleted (soft delete) or permanently deletes it.
203
+
204
+ ```typescript
205
+ async delete(force?: boolean): Promise<void>
206
+ ```
207
+
208
+ **Parameters:**
209
+ - `force` (optional): Boolean - If true, permanently delete
210
+
211
+ **Returns:** `Promise<void>`
212
+
213
+ **Example:**
214
+ ```typescript
215
+ // Soft delete
216
+ await user.delete();
217
+
218
+ // Permanent delete
219
+ await user.delete(true);
220
+ ```
221
+
222
+ #### `entity.componentList()`
223
+
224
+ Gets all components attached to the entity.
225
+
226
+ ```typescript
227
+ componentList(): BaseComponent[]
228
+ ```
229
+
230
+ **Returns:** `BaseComponent[]` - Array of component instances
231
+
232
+ **Example:**
233
+ ```typescript
234
+ const components = user.componentList();
235
+ console.log(`Entity has ${components.length} components`);
236
+ ```
237
+
238
+ ## 🧩 BaseComponent Class
239
+
240
+ Base class for all components in BunSane.
241
+
242
+ ### Properties
243
+
244
+ #### `id`
245
+ ```typescript
246
+ id: string
247
+ ```
248
+ Unique identifier for the component instance.
249
+
250
+ #### `_persisted`
251
+ ```typescript
252
+ protected _persisted: boolean = false
253
+ ```
254
+ Whether the component has been saved to the database.
255
+
256
+ #### `_dirty`
257
+ ```typescript
258
+ protected _dirty: boolean = false
259
+ ```
260
+ Whether the component has unsaved changes.
261
+
262
+ ### Methods
263
+
264
+ #### `getTypeID()`
265
+
266
+ Gets the component's type identifier.
267
+
268
+ ```typescript
269
+ getTypeID(): string
270
+ ```
271
+
272
+ **Returns:** `string` - Type identifier (SHA256 hash of class name)
273
+
274
+ **Example:**
275
+ ```typescript
276
+ const typeId = profile.getTypeID();
277
+ console.log(typeId); // 'a1b2c3d4...'
278
+ ```
279
+
280
+ #### `properties()`
281
+
282
+ Gets all property names marked with @CompData.
283
+
284
+ ```typescript
285
+ properties(): string[]
286
+ ```
287
+
288
+ **Returns:** `string[]` - Array of property names
289
+
290
+ **Example:**
291
+ ```typescript
292
+ const props = profile.properties();
293
+ console.log(props); // ['name', 'email', 'username']
294
+ ```
295
+
296
+ #### `data()`
297
+
298
+ Gets all component data as a plain object.
299
+
300
+ ```typescript
301
+ data<T extends this>(): ComponentDataType<T>
302
+ ```
303
+
304
+ **Type Parameters:**
305
+ - `T`: Component class
306
+
307
+ **Returns:** `ComponentDataType<T>` - Plain object with component data
308
+
309
+ **Example:**
310
+ ```typescript
311
+ const data = profile.data();
312
+ console.log(data); // { name: 'John', email: 'john@example.com' }
313
+ ```
314
+
315
+ #### `indexedProperties()`
316
+
317
+ Gets property names marked with @CompData({ indexed: true }).
318
+
319
+ ```typescript
320
+ indexedProperties(): string[]
321
+ ```
322
+
323
+ **Returns:** `string[]` - Array of indexed property names
324
+
325
+ **Example:**
326
+ ```typescript
327
+ const indexed = profile.indexedProperties();
328
+ console.log(indexed); // ['email', 'username']
329
+ ```
330
+
331
+ ## 🏛️ ArcheType Class
332
+
333
+ Template system for creating entities with predefined components.
334
+
335
+ ### Constructor
336
+
337
+ ```typescript
338
+ new ArcheType(components: (new () => BaseComponent)[])
339
+ ```
340
+
341
+ **Parameters:**
342
+ - `components`: Array of component constructors
343
+
344
+ ### Methods
345
+
346
+ #### `createEntity()`
347
+
348
+ Creates a new entity with all archetype components.
349
+
350
+ ```typescript
351
+ createEntity(): Entity
352
+ ```
353
+
354
+ **Returns:** `Entity` - New entity with all components attached
355
+
356
+ **Example:**
357
+ ```typescript
358
+ const userEntity = UserArcheType.createEntity();
359
+ // Entity has UserProfile, UserPreferences, UserStats components
360
+ ```
361
+
362
+ #### `fill(data)`
363
+
364
+ Fills archetype with data for entity creation.
365
+
366
+ ```typescript
367
+ fill(data: object, strict?: boolean): this
368
+ ```
369
+
370
+ **Parameters:**
371
+ - `data`: Object - Data to fill components
372
+ - `strict` (optional): Boolean - Whether to enforce strict data matching
373
+
374
+ **Returns:** `this` - ArcheType instance for chaining
375
+
376
+ **Example:**
377
+ ```typescript
378
+ const userEntity = UserArcheType.fill({
379
+ userProfile: { name: 'John', email: 'john@example.com' },
380
+ userPreferences: { theme: 'dark' }
381
+ }).createEntity();
382
+ ```
383
+
384
+ #### `createAndSaveEntity()`
385
+
386
+ Creates and immediately saves an entity.
387
+
388
+ ```typescript
389
+ async createAndSaveEntity(): Promise<Entity>
390
+ ```
391
+
392
+ **Returns:** `Promise<Entity>` - Saved entity
393
+
394
+ **Example:**
395
+ ```typescript
396
+ const user = await UserArcheType.fill(userData).createAndSaveEntity();
397
+ console.log('Created user:', user.id);
398
+ ```
399
+
400
+ #### `Unwrap(entity, exclude)`
401
+
402
+ Converts entity back to plain object.
403
+
404
+ ```typescript
405
+ async Unwrap(entity: Entity, exclude?: string[]): Promise<Record<string, any>>
406
+ ```
407
+
408
+ **Parameters:**
409
+ - `entity`: Entity - Entity to unwrap
410
+ - `exclude` (optional): String[] - Property names to exclude
411
+
412
+ **Returns:** `Promise<Record<string, any>>` - Plain object with component data
413
+
414
+ **Example:**
415
+ ```typescript
416
+ const userData = await UserArcheType.Unwrap(userEntity);
417
+ console.log(userData);
418
+ // {
419
+ // userProfile: { name: 'John', email: 'john@example.com' },
420
+ // userPreferences: { theme: 'dark' }
421
+ // }
422
+ ```
423
+
424
+ #### `updateEntity(entity, updates)`
425
+
426
+ Updates an existing entity using archetype data structure.
427
+
428
+ ```typescript
429
+ async updateEntity<T>(entity: Entity, updates: Partial<T>): Promise<void>
430
+ ```
431
+
432
+ **Type Parameters:**
433
+ - `T`: Update data type
434
+
435
+ **Parameters:**
436
+ - `entity`: Entity - Entity to update
437
+ - `updates`: Partial<T> - Updates to apply
438
+
439
+ **Returns:** `Promise<void>`
440
+
441
+ **Example:**
442
+ ```typescript
443
+ await UserArcheType.updateEntity(userEntity, {
444
+ userProfile: { name: 'Jane Doe' },
445
+ userPreferences: { theme: 'light' }
446
+ });
447
+ ```
448
+
449
+ ## 🎨 Decorators
450
+
451
+ ### @Component
452
+
453
+ Marks a class as a BunSane component.
454
+
455
+ ```typescript
456
+ @Component(target: any): any
457
+ ```
458
+
459
+ **Parameters:**
460
+ - `target`: Class constructor to decorate
461
+
462
+ **Example:**
463
+ ```typescript
464
+ @Component
465
+ export class UserProfile extends BaseComponent {
466
+ // Component implementation
467
+ }
468
+ ```
469
+
470
+ ### @CompData
471
+
472
+ Marks a property as component data.
473
+
474
+ ```typescript
475
+ @CompData(options?: { indexed?: boolean }): PropertyDecorator
476
+ ```
477
+
478
+ **Parameters:**
479
+ - `options.indexed` (optional): Boolean - Whether to create database index
480
+
481
+ **Example:**
482
+ ```typescript
483
+ @Component
484
+ export class UserProfile extends BaseComponent {
485
+ @CompData()
486
+ name: string = '';
487
+
488
+ @CompData({ indexed: true })
489
+ email: string = '';
490
+ }
491
+ ```
492
+
493
+ ## 📊 Type Definitions
494
+
495
+ ### ComponentDataType<T>
496
+
497
+ Extracts data properties from a component class.
498
+
499
+ ```typescript
500
+ type ComponentDataType<T extends BaseComponent> = {
501
+ [K in keyof T as T[K] extends Function ? never :
502
+ K extends `_${string}` ? never :
503
+ K extends 'id' | 'getTypeID' | 'properties' | 'data' | 'save' | 'insert' | 'update' ? never :
504
+ K]: T[K];
505
+ };
506
+ ```
507
+
508
+ ### ComponentGetter<T>
509
+
510
+ Type for accessing component data and methods.
511
+
512
+ ```typescript
513
+ type ComponentGetter<T extends BaseComponent> = Pick<T, "properties" | "id"> & {
514
+ data(): ComponentDataType<T>;
515
+ };
516
+ ```
517
+
518
+ ## 🚨 Error Types
519
+
520
+ ### Component Errors
521
+
522
+ ```typescript
523
+ class ComponentNotFoundError extends Error {
524
+ constructor(componentName: string) {
525
+ super(`Component ${componentName} not found`);
526
+ }
527
+ }
528
+
529
+ class ComponentNotRegisteredError extends Error {
530
+ constructor(componentName: string) {
531
+ super(`Component ${componentName} is not registered`);
532
+ }
533
+ }
534
+ ```
535
+
536
+ ### Entity Errors
537
+
538
+ ```typescript
539
+ class EntityNotFoundError extends Error {
540
+ constructor(entityId: string) {
541
+ super(`Entity ${entityId} not found`);
542
+ }
543
+ }
544
+
545
+ class EntityValidationError extends Error {
546
+ constructor(message: string, entityId: string) {
547
+ super(`Entity validation failed: ${message}`);
548
+ this.entityId = entityId;
549
+ }
550
+ }
551
+ ```
552
+
553
+ ## 📈 Performance Notes
554
+
555
+ - **Entity Creation**: Use `ArcheType.createEntity()` for consistent component sets
556
+ - **Bulk Operations**: Use `Entity.LoadMultiple()` for loading multiple entities
557
+ - **Component Access**: Cache component references when accessing repeatedly
558
+ - **Database Indexes**: Use `@CompData({ indexed: true })` for frequently queried fields
559
+
560
+ ## 🔗 Related APIs
561
+
562
+ - **[Query API](query.md)** - Database querying
563
+ - **[Service API](service.md)** - Business logic layer
564
+ - **[Hooks API](hooks.md)** - Lifecycle events
565
+
566
+ ---
567
+
568
+ *Need more details? Check the [Query API](query.md) for database operations!* 🚀