@shetty4l/core 0.1.35 → 0.1.37
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/package.json +1 -1
- package/src/state/collection/decorators.ts +252 -0
- package/src/state/collection/query.ts +259 -0
- package/src/state/collection/types.ts +160 -0
- package/src/state/decorators.ts +4 -1
- package/src/state/index.ts +109 -5
- package/src/state/loader.ts +652 -2
- package/src/state/schema.ts +104 -1
- package/src/state/types.ts +3 -0
package/src/state/index.ts
CHANGED
|
@@ -2,15 +2,17 @@
|
|
|
2
2
|
* State persistence module.
|
|
3
3
|
*
|
|
4
4
|
* Provides decorator-based state persistence with auto-save to SQLite.
|
|
5
|
+
* Supports both singleton state objects (@Persisted) and multi-row
|
|
6
|
+
* collections (@PersistedCollection).
|
|
5
7
|
*
|
|
6
|
-
* @example
|
|
8
|
+
* @example Singleton state (auto-save on property change)
|
|
7
9
|
* ```ts
|
|
8
10
|
* import { Persisted, Field, StateLoader } from '@shetty4l/core/state';
|
|
9
11
|
*
|
|
10
12
|
* @Persisted('my_state')
|
|
11
13
|
* class MyState {
|
|
12
|
-
* @Field() counter: number = 0;
|
|
13
|
-
* @Field(
|
|
14
|
+
* @Field('number') counter: number = 0;
|
|
15
|
+
* @Field('date') lastUpdated: Date | null = null;
|
|
14
16
|
* }
|
|
15
17
|
*
|
|
16
18
|
* const loader = new StateLoader(db);
|
|
@@ -18,14 +20,116 @@
|
|
|
18
20
|
* state.counter += 1; // Auto-saves after 100ms
|
|
19
21
|
* await loader.flush(); // Force immediate save
|
|
20
22
|
* ```
|
|
23
|
+
*
|
|
24
|
+
* @example Collection entities (explicit save)
|
|
25
|
+
* ```ts
|
|
26
|
+
* import {
|
|
27
|
+
* PersistedCollection,
|
|
28
|
+
* CollectionEntity,
|
|
29
|
+
* Id,
|
|
30
|
+
* Field,
|
|
31
|
+
* Index,
|
|
32
|
+
* StateLoader
|
|
33
|
+
* } from '@shetty4l/core/state';
|
|
34
|
+
*
|
|
35
|
+
* @PersistedCollection('users')
|
|
36
|
+
* class User extends CollectionEntity {
|
|
37
|
+
* @Id() id: string = '';
|
|
38
|
+
* @Field('string') @Index() email: string = '';
|
|
39
|
+
* @Field('string') name: string = '';
|
|
40
|
+
*
|
|
41
|
+
* async save(): Promise<void> { throw new Error('Not bound'); }
|
|
42
|
+
* async delete(): Promise<void> { throw new Error('Not bound'); }
|
|
43
|
+
* }
|
|
44
|
+
*
|
|
45
|
+
* const loader = new StateLoader(db);
|
|
46
|
+
* const user = loader.create(User, { id: 'abc', email: 'a@b.com', name: 'Alice' });
|
|
47
|
+
* user.name = 'Alicia';
|
|
48
|
+
* await user.save(); // Explicit save required
|
|
49
|
+
* ```
|
|
21
50
|
*/
|
|
22
51
|
|
|
52
|
+
// --------------------------------------------------------------------------
|
|
53
|
+
// Singleton decorators (@Persisted pattern)
|
|
54
|
+
// --------------------------------------------------------------------------
|
|
55
|
+
|
|
56
|
+
/** Options for the @Field decorator. */
|
|
23
57
|
export type { FieldOptions } from "./decorators";
|
|
24
|
-
|
|
58
|
+
/** Mark a class as persisted to a SQLite table (singleton pattern). */
|
|
59
|
+
/** Mark a property as a persisted field (singleton pattern). */
|
|
25
60
|
export { Field, Persisted } from "./decorators";
|
|
26
61
|
|
|
62
|
+
// --------------------------------------------------------------------------
|
|
63
|
+
// Collection decorators (@PersistedCollection pattern)
|
|
64
|
+
// --------------------------------------------------------------------------
|
|
65
|
+
|
|
66
|
+
/** Mark a class as a persisted collection (multi-row table). */
|
|
67
|
+
/** Mark a property as the primary key field. */
|
|
68
|
+
/** Mark a property as a persisted field in a collection (use with @PersistedCollection). */
|
|
69
|
+
/** Mark column(s) for indexing. */
|
|
70
|
+
export {
|
|
71
|
+
Field as CollectionField,
|
|
72
|
+
Id,
|
|
73
|
+
Index,
|
|
74
|
+
PersistedCollection,
|
|
75
|
+
} from "./collection/decorators";
|
|
76
|
+
|
|
77
|
+
// --------------------------------------------------------------------------
|
|
78
|
+
// Collection base class
|
|
79
|
+
// --------------------------------------------------------------------------
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Abstract base class for collection entities.
|
|
83
|
+
* Provides compile-time type safety to distinguish collection entities
|
|
84
|
+
* from singleton @Persisted classes.
|
|
85
|
+
*/
|
|
86
|
+
export { CollectionEntity } from "./collection/types";
|
|
87
|
+
|
|
88
|
+
// --------------------------------------------------------------------------
|
|
27
89
|
// Loader
|
|
90
|
+
// --------------------------------------------------------------------------
|
|
91
|
+
|
|
92
|
+
/** Manages loading and persisting state objects. */
|
|
28
93
|
export { StateLoader } from "./loader";
|
|
29
94
|
|
|
30
|
-
//
|
|
95
|
+
// --------------------------------------------------------------------------
|
|
96
|
+
// Singleton types
|
|
97
|
+
// --------------------------------------------------------------------------
|
|
98
|
+
|
|
99
|
+
/** Metadata for a @Persisted class. */
|
|
100
|
+
/** Metadata for a single persisted field. */
|
|
101
|
+
/** Supported field types for persistence. */
|
|
31
102
|
export type { ClassMeta, FieldMeta, FieldType } from "./types";
|
|
103
|
+
|
|
104
|
+
// --------------------------------------------------------------------------
|
|
105
|
+
// Collection types
|
|
106
|
+
// --------------------------------------------------------------------------
|
|
107
|
+
|
|
108
|
+
/** Metadata for a @PersistedCollection class. */
|
|
109
|
+
/** Options for find() queries. */
|
|
110
|
+
/**
|
|
111
|
+
* A where value can be a raw value (treated as eq) or a condition object.
|
|
112
|
+
* @example
|
|
113
|
+
* ```ts
|
|
114
|
+
* // Raw value (eq)
|
|
115
|
+
* { status: 'active' }
|
|
116
|
+
*
|
|
117
|
+
* // Condition object
|
|
118
|
+
* { age: { op: 'gte', value: 18 } }
|
|
119
|
+
* ```
|
|
120
|
+
*/
|
|
121
|
+
/** Where clause mapping field names to conditions. */
|
|
122
|
+
/** Comparison operators for WHERE clauses. */
|
|
123
|
+
/** A single where condition with explicit operator. */
|
|
124
|
+
/** Order direction for sorting. */
|
|
125
|
+
/** Order by clause - field name to direction. */
|
|
126
|
+
export type {
|
|
127
|
+
CollectionMeta,
|
|
128
|
+
FindOptions,
|
|
129
|
+
OrderByClause,
|
|
130
|
+
OrderDirection,
|
|
131
|
+
WhereClause,
|
|
132
|
+
WhereCondition,
|
|
133
|
+
WhereOperator,
|
|
134
|
+
WhereValue,
|
|
135
|
+
} from "./collection/types";
|