@stonyx/orm 0.2.1-beta.4 → 0.2.1-beta.41
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/.claude/index.md +16 -3
- package/.claude/usage-patterns.md +83 -0
- package/.claude/views.md +292 -0
- package/README.md +37 -0
- package/config/environment.js +3 -1
- package/package.json +11 -6
- package/scripts/setup-test-db.sh +21 -0
- package/src/aggregates.js +93 -0
- package/src/index.js +11 -2
- package/src/main.js +47 -3
- package/src/manage-record.js +11 -0
- package/src/model-property.js +2 -2
- package/src/model.js +11 -0
- package/src/mysql/migration-generator.js +103 -5
- package/src/mysql/mysql-db.js +161 -8
- package/src/mysql/schema-introspector.js +167 -4
- package/src/orm-request.js +32 -26
- package/src/plural-registry.js +12 -0
- package/src/record.js +2 -2
- package/src/serializer.js +9 -2
- package/src/setup-rest-server.js +3 -3
- package/src/store.js +130 -1
- package/src/view-resolver.js +183 -0
- package/src/view.js +21 -0
package/.claude/index.md
CHANGED
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
## Detailed Guides
|
|
4
4
|
|
|
5
5
|
- [Usage Patterns](usage-patterns.md) — Model definitions, serializers, transforms, CRUD, DB schema, persistence, access control, REST API, and include parameters
|
|
6
|
+
- [Views](views.md) — Read-only computed views with aggregate helpers, in-memory resolver, and MySQL VIEW auto-generation
|
|
6
7
|
- [Middleware Hooks System](hooks.md) — Before/after hooks for CRUD operations, halting, context object, change detection, and testing
|
|
7
8
|
- [Code Style Rules](code-style-rules.md) — Strict prettier/eslint rules to apply across all Stonyx projects
|
|
8
9
|
|
|
@@ -21,6 +22,7 @@
|
|
|
21
22
|
5. **REST API Generation**: Auto-generated RESTful endpoints with access control
|
|
22
23
|
6. **Data Transformation**: Custom type conversion and formatting
|
|
23
24
|
7. **Middleware Hooks**: Before/after hooks for all CRUD operations with halting capability
|
|
25
|
+
8. **Views**: Read-only computed projections over model data with aggregate helpers (count, avg, sum, min, max)
|
|
24
26
|
|
|
25
27
|
---
|
|
26
28
|
|
|
@@ -38,6 +40,9 @@
|
|
|
38
40
|
8. **Include Logic** (inline in [src/orm-request.js](src/orm-request.js)) - Parses include query params, traverses relationships, collects and deduplicates included records
|
|
39
41
|
9. **Hooks** ([src/hooks.js](src/hooks.js)) - Middleware-based hook registry for CRUD lifecycle
|
|
40
42
|
10. **MySQL Driver** ([src/mysql/mysql-db.js](src/mysql/mysql-db.js)) - MySQL persistence, migrations, schema introspection. Loads records in topological order. `_rowToRawData()` converts TINYINT(1) → boolean, remaps FK columns, strips timestamps.
|
|
43
|
+
11. **View** ([src/view.js](src/view.js)) - Read-only base class for computed views (does NOT extend Model)
|
|
44
|
+
12. **Aggregates** ([src/aggregates.js](src/aggregates.js)) - AggregateProperty class + helper functions (count, avg, sum, min, max)
|
|
45
|
+
13. **ViewResolver** ([src/view-resolver.js](src/view-resolver.js)) - In-memory view resolver (iterates source model, computes aggregates + resolve map)
|
|
41
46
|
|
|
42
47
|
### Project Structure
|
|
43
48
|
|
|
@@ -65,6 +70,10 @@ stonyx-orm/
|
|
|
65
70
|
│ ├── migrate.js # JSON DB mode migration (file <-> directory)
|
|
66
71
|
│ ├── commands.js # CLI commands (db:migrate-*, etc.)
|
|
67
72
|
│ ├── utils.js # Pluralize wrapper for dasherized names
|
|
73
|
+
│ ├── plural-registry.js # Plural name registry (populated at init, supports Model.pluralName overrides)
|
|
74
|
+
│ ├── view.js # View base class (read-only, source, resolve, memory)
|
|
75
|
+
│ ├── aggregates.js # AggregateProperty + count/avg/sum/min/max helpers
|
|
76
|
+
│ ├── view-resolver.js # In-memory view resolver
|
|
68
77
|
│ ├── exports/
|
|
69
78
|
│ │ └── db.js # Convenience re-export of DB instance
|
|
70
79
|
│ └── mysql/
|
|
@@ -85,6 +94,7 @@ stonyx-orm/
|
|
|
85
94
|
│ ├── serializers/ # Example serializers
|
|
86
95
|
│ ├── transforms/ # Custom transforms
|
|
87
96
|
│ ├── access/ # Access control
|
|
97
|
+
│ ├── views/ # Example views
|
|
88
98
|
│ ├── db-schema.js # DB schema
|
|
89
99
|
│ └── payload.js # Test data
|
|
90
100
|
└── package.json
|
|
@@ -102,7 +112,8 @@ config.orm = {
|
|
|
102
112
|
model: './models',
|
|
103
113
|
serializer: './serializers',
|
|
104
114
|
transform: './transforms',
|
|
105
|
-
access: './access'
|
|
115
|
+
access: './access',
|
|
116
|
+
view: './views'
|
|
106
117
|
},
|
|
107
118
|
db: {
|
|
108
119
|
autosave: 'false',
|
|
@@ -150,6 +161,7 @@ The ORM supports two storage modes, configured via `db.mode`:
|
|
|
150
161
|
- Models: `{PascalCase}Model` (e.g., `AnimalModel`)
|
|
151
162
|
- Serializers: `{PascalCase}Serializer` (e.g., `AnimalSerializer`)
|
|
152
163
|
- Transforms: Original filename (e.g., `animal.js`)
|
|
164
|
+
- Plural names: Auto-pluralized by default (e.g., `animal` → `animals`). Override with `static pluralName` on the model class (e.g., `static pluralName = 'people'`). All call sites use `getPluralName()` from the plural registry, **not** `pluralize()` directly.
|
|
153
165
|
|
|
154
166
|
---
|
|
155
167
|
|
|
@@ -227,9 +239,10 @@ The ORM supports two storage modes, configured via `db.mode`:
|
|
|
227
239
|
**Import the ORM:**
|
|
228
240
|
```javascript
|
|
229
241
|
import {
|
|
230
|
-
Orm, Model, Serializer, attr, hasMany, belongsTo,
|
|
242
|
+
Orm, Model, View, Serializer, attr, hasMany, belongsTo,
|
|
231
243
|
createRecord, updateRecord, store,
|
|
232
|
-
beforeHook, afterHook, clearHook, clearAllHooks
|
|
244
|
+
beforeHook, afterHook, clearHook, clearAllHooks,
|
|
245
|
+
count, avg, sum, min, max
|
|
233
246
|
} from '@stonyx/orm';
|
|
234
247
|
```
|
|
235
248
|
|
|
@@ -31,6 +31,23 @@ export default class AnimalModel extends Model {
|
|
|
31
31
|
- Use `hasMany(modelName)` for one-to-many
|
|
32
32
|
- Getters work as computed properties
|
|
33
33
|
- Relationships auto-establish bidirectionally
|
|
34
|
+
- Override auto-pluralization with `static pluralName` (see [Overriding Plural Names](#overriding-plural-names))
|
|
35
|
+
|
|
36
|
+
### Overriding Plural Names
|
|
37
|
+
|
|
38
|
+
By default, model names are auto-pluralized (e.g., `animal` → `animals`) for REST routes, JSON:API URLs, and DB table names. When auto-pluralization produces the wrong result, override it with `static pluralName`:
|
|
39
|
+
|
|
40
|
+
```javascript
|
|
41
|
+
import { Model, attr } from '@stonyx/orm';
|
|
42
|
+
|
|
43
|
+
export default class PersonModel extends Model {
|
|
44
|
+
static pluralName = 'people';
|
|
45
|
+
|
|
46
|
+
name = attr('string');
|
|
47
|
+
}
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
The override is picked up automatically during ORM initialization — no additional registration is needed. All internal call sites (REST routes, JSON:API type references, MySQL table names, foreign key references) use the overridden value.
|
|
34
51
|
|
|
35
52
|
## 2. Serializers (Data Transformation)
|
|
36
53
|
|
|
@@ -215,3 +232,69 @@ GET /scenes/e001-s001?include=slides.dialogue.character
|
|
|
215
232
|
- `traverseIncludePath()` - Recursively traverses relationship paths
|
|
216
233
|
- `collectIncludedRecords()` - Orchestrates traversal and deduplication
|
|
217
234
|
- All implemented in [src/orm-request.js](src/orm-request.js)
|
|
235
|
+
|
|
236
|
+
## 10. Views (Read-Only Computed Data)
|
|
237
|
+
|
|
238
|
+
Views are read-only projections that compute derived data from existing models. They work in both JSON mode (in-memory) and MySQL mode (auto-generated SQL VIEWs). See the full guide at [views.md](views.md).
|
|
239
|
+
|
|
240
|
+
### Defining a View
|
|
241
|
+
|
|
242
|
+
```javascript
|
|
243
|
+
// views/owner-stats.js
|
|
244
|
+
import { View, attr, belongsTo, count, avg } from '@stonyx/orm';
|
|
245
|
+
|
|
246
|
+
export default class OwnerStatsView extends View {
|
|
247
|
+
static source = 'owner'; // Required: model whose records produce view records
|
|
248
|
+
|
|
249
|
+
animalCount = count('pets'); // COUNT of hasMany relationship
|
|
250
|
+
averageAge = avg('pets', 'age'); // AVG of a field on related records
|
|
251
|
+
owner = belongsTo('owner'); // Link back to source record
|
|
252
|
+
}
|
|
253
|
+
```
|
|
254
|
+
|
|
255
|
+
### Aggregate Helpers
|
|
256
|
+
|
|
257
|
+
| Helper | Example | JS Behavior | MySQL |
|
|
258
|
+
|--------|---------|-------------|-------|
|
|
259
|
+
| `count(rel)` | `count('pets')` | `records.length` | `COUNT(table.id)` |
|
|
260
|
+
| `avg(rel, field)` | `avg('pets', 'age')` | Average of values | `AVG(table.field)` |
|
|
261
|
+
| `sum(rel, field)` | `sum('pets', 'age')` | Sum of values | `SUM(table.field)` |
|
|
262
|
+
| `min(rel, field)` | `min('pets', 'age')` | Minimum value | `MIN(table.field)` |
|
|
263
|
+
| `max(rel, field)` | `max('pets', 'age')` | Maximum value | `MAX(table.field)` |
|
|
264
|
+
|
|
265
|
+
### Resolve Map (Escape Hatch)
|
|
266
|
+
|
|
267
|
+
For fields that can't be expressed as aggregates:
|
|
268
|
+
|
|
269
|
+
```javascript
|
|
270
|
+
export default class OwnerStatsView extends View {
|
|
271
|
+
static source = 'owner';
|
|
272
|
+
static resolve = {
|
|
273
|
+
gender: 'gender', // String path from source data
|
|
274
|
+
score: (owner) => owner.__data.age * 10, // Function
|
|
275
|
+
};
|
|
276
|
+
|
|
277
|
+
gender = attr('string'); // Must also define as attr()
|
|
278
|
+
score = attr('number');
|
|
279
|
+
animalCount = count('pets');
|
|
280
|
+
}
|
|
281
|
+
```
|
|
282
|
+
|
|
283
|
+
### Querying Views
|
|
284
|
+
|
|
285
|
+
```javascript
|
|
286
|
+
const stats = await store.findAll('owner-stats');
|
|
287
|
+
const stat = await store.find('owner-stats', ownerId);
|
|
288
|
+
```
|
|
289
|
+
|
|
290
|
+
### Read-Only Enforcement
|
|
291
|
+
|
|
292
|
+
```javascript
|
|
293
|
+
createRecord('owner-stats', data); // Throws: Cannot create records for read-only view
|
|
294
|
+
updateRecord(viewRecord, data); // Throws: Cannot update records for read-only view
|
|
295
|
+
store.remove('owner-stats', id); // Throws: Cannot remove records from read-only view
|
|
296
|
+
```
|
|
297
|
+
|
|
298
|
+
### REST API
|
|
299
|
+
|
|
300
|
+
Only GET endpoints are mounted for views — no POST, PATCH, or DELETE.
|
package/.claude/views.md
ADDED
|
@@ -0,0 +1,292 @@
|
|
|
1
|
+
# Views
|
|
2
|
+
|
|
3
|
+
Views are read-only, model-like structures that compute derived data from existing models. They work in both JSON file mode (in-memory computation) and MySQL mode (auto-generated SQL VIEWs).
|
|
4
|
+
|
|
5
|
+
## What Views Are
|
|
6
|
+
|
|
7
|
+
A View defines a read-only projection over source model data. Use views when you need:
|
|
8
|
+
- Aggregated data (counts, averages, sums) derived from model relationships
|
|
9
|
+
- Computed read-only summaries that shouldn't be persisted as separate records
|
|
10
|
+
- MySQL VIEWs that are auto-generated from your JavaScript definition
|
|
11
|
+
|
|
12
|
+
## Defining a View
|
|
13
|
+
|
|
14
|
+
Views extend the `View` base class and are placed in the `views/` directory (configurable via `paths.view`).
|
|
15
|
+
|
|
16
|
+
```javascript
|
|
17
|
+
// views/owner-stats.js
|
|
18
|
+
import { View, attr, belongsTo, count, avg } from '@stonyx/orm';
|
|
19
|
+
|
|
20
|
+
export default class OwnerStatsView extends View {
|
|
21
|
+
static source = 'owner'; // The model whose records are iterated
|
|
22
|
+
|
|
23
|
+
animalCount = count('pets'); // COUNT of hasMany relationship
|
|
24
|
+
averageAge = avg('pets', 'age'); // AVG of a field on related records
|
|
25
|
+
owner = belongsTo('owner'); // Link back to the source record
|
|
26
|
+
}
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
### File Naming
|
|
30
|
+
|
|
31
|
+
- File: `owner-stats.js` → Class: `OwnerStatsView` → Store name: `'owner-stats'`
|
|
32
|
+
- Directory: configured via `paths.view` (default: `'./views'`)
|
|
33
|
+
- Environment variable: `ORM_VIEW_PATH`
|
|
34
|
+
|
|
35
|
+
### Key Static Properties
|
|
36
|
+
|
|
37
|
+
| Property | Default | Description |
|
|
38
|
+
|----------|---------|-------------|
|
|
39
|
+
| `source` | `undefined` | **(Required)** The model name whose records produce view records |
|
|
40
|
+
| `groupBy` | `undefined` | Field name to group source records by (one view record per unique value) |
|
|
41
|
+
| `resolve` | `undefined` | Optional escape-hatch map for custom derivations |
|
|
42
|
+
| `memory` | `false` | `false` = computed on demand; `true` = cached on startup |
|
|
43
|
+
| `readOnly` | `true` | **Enforced** — cannot be overridden to `false` |
|
|
44
|
+
| `pluralName` | `undefined` | Custom plural name (same as Model) |
|
|
45
|
+
|
|
46
|
+
## Aggregate Helpers
|
|
47
|
+
|
|
48
|
+
Aggregate helpers define fields that compute values from related records. Each helper knows both its JavaScript computation logic and its MySQL SQL translation.
|
|
49
|
+
|
|
50
|
+
| Helper | JS Behavior | MySQL Translation |
|
|
51
|
+
|--------|-------------|-------------------|
|
|
52
|
+
| `count(relationship)` | `relatedRecords.length` | `COUNT(table.id)` |
|
|
53
|
+
| `avg(relationship, field)` | Average of field values | `AVG(table.field)` |
|
|
54
|
+
| `sum(relationship, field)` | Sum of field values | `SUM(table.field)` |
|
|
55
|
+
| `min(relationship, field)` | Minimum field value | `MIN(table.field)` |
|
|
56
|
+
| `max(relationship, field)` | Maximum field value | `MAX(table.field)` |
|
|
57
|
+
|
|
58
|
+
### Empty/Null Handling
|
|
59
|
+
|
|
60
|
+
- `count` with empty/null relationship → `0`
|
|
61
|
+
- `avg` with empty/null relationship → `0`
|
|
62
|
+
- `sum` with empty/null relationship → `0`
|
|
63
|
+
- `min` with empty/null relationship → `null`
|
|
64
|
+
- `max` with empty/null relationship → `null`
|
|
65
|
+
- Non-numeric values are filtered/treated as 0
|
|
66
|
+
|
|
67
|
+
## Resolve Map (Escape Hatch)
|
|
68
|
+
|
|
69
|
+
For computed fields that can't be expressed as aggregates, use `static resolve`:
|
|
70
|
+
|
|
71
|
+
```javascript
|
|
72
|
+
export default class OwnerStatsView extends View {
|
|
73
|
+
static source = 'owner';
|
|
74
|
+
|
|
75
|
+
static resolve = {
|
|
76
|
+
gender: 'gender', // String path: maps from source record data
|
|
77
|
+
score: (owner) => { // Function: custom computation
|
|
78
|
+
return owner.__data.age * 10;
|
|
79
|
+
},
|
|
80
|
+
nestedVal: 'details.nested', // Nested string paths supported
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
// Must also define as attr() for the serializer to process
|
|
84
|
+
gender = attr('string');
|
|
85
|
+
score = attr('number');
|
|
86
|
+
nestedVal = attr('passthrough');
|
|
87
|
+
|
|
88
|
+
animalCount = count('pets');
|
|
89
|
+
}
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
**Important:** Each resolve map entry needs a corresponding `attr()` field definition on the view.
|
|
93
|
+
|
|
94
|
+
## GroupBy Views
|
|
95
|
+
|
|
96
|
+
GroupBy views produce one view record per unique value of a field on the source model, with aggregates computed within each group.
|
|
97
|
+
|
|
98
|
+
### Defining a GroupBy View
|
|
99
|
+
|
|
100
|
+
```javascript
|
|
101
|
+
import { View, attr, count, avg, sum } from '@stonyx/orm';
|
|
102
|
+
|
|
103
|
+
export default class AnimalCountBySizeView extends View {
|
|
104
|
+
static source = 'animal';
|
|
105
|
+
static groupBy = 'size'; // Group animals by their size field
|
|
106
|
+
|
|
107
|
+
id = attr('string'); // The group key becomes the id
|
|
108
|
+
animalCount = count(); // Count records in each group
|
|
109
|
+
averageAge = avg('age'); // Average of 'age' field within each group
|
|
110
|
+
}
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
### Aggregate Helpers in GroupBy Views
|
|
114
|
+
|
|
115
|
+
In groupBy views, aggregate helpers operate on the group's records rather than on relationships:
|
|
116
|
+
|
|
117
|
+
| Helper | GroupBy Behavior | MySQL Translation |
|
|
118
|
+
|--------|-----------------|-------------------|
|
|
119
|
+
| `count()` | Number of records in the group | `COUNT(*)` |
|
|
120
|
+
| `sum('field')` | Sum of field across group records | `SUM(source.field)` |
|
|
121
|
+
| `avg('field')` | Average of field across group records | `AVG(source.field)` |
|
|
122
|
+
| `min('field')` | Minimum field value in the group | `MIN(source.field)` |
|
|
123
|
+
| `max('field')` | Maximum field value in the group | `MAX(source.field)` |
|
|
124
|
+
|
|
125
|
+
Relationship aggregates (e.g., `count('traits')`) also work — they flatten related records across all group members and aggregate the combined set.
|
|
126
|
+
|
|
127
|
+
### Resolve Map in GroupBy Views
|
|
128
|
+
|
|
129
|
+
The resolve map behaves differently in groupBy views:
|
|
130
|
+
- **Function resolvers** receive the **array of group records** (not a single record)
|
|
131
|
+
- **String path resolvers** take the value from the **first record** in the group
|
|
132
|
+
|
|
133
|
+
```javascript
|
|
134
|
+
export default class LeagueStatsView extends View {
|
|
135
|
+
static source = 'game-stats';
|
|
136
|
+
static groupBy = 'competition';
|
|
137
|
+
|
|
138
|
+
static resolve = {
|
|
139
|
+
totalGoals: (groupRecords) => {
|
|
140
|
+
return groupRecords.reduce((sum, r) => {
|
|
141
|
+
const fs = r.__data.finalScore;
|
|
142
|
+
return fs ? sum + fs[0] + fs[1] : sum;
|
|
143
|
+
}, 0);
|
|
144
|
+
},
|
|
145
|
+
};
|
|
146
|
+
|
|
147
|
+
matchCount = count();
|
|
148
|
+
totalGoals = attr('number');
|
|
149
|
+
}
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
### MySQL DDL for GroupBy Views
|
|
153
|
+
|
|
154
|
+
```sql
|
|
155
|
+
CREATE OR REPLACE VIEW `animal-count-by-sizes` AS
|
|
156
|
+
SELECT
|
|
157
|
+
`animals`.`size` AS `id`,
|
|
158
|
+
COUNT(*) AS `animalCount`,
|
|
159
|
+
AVG(`animals`.`age`) AS `averageAge`
|
|
160
|
+
FROM `animals`
|
|
161
|
+
GROUP BY `animals`.`size`
|
|
162
|
+
```
|
|
163
|
+
|
|
164
|
+
## Querying Views
|
|
165
|
+
|
|
166
|
+
Views use the same store API as models:
|
|
167
|
+
|
|
168
|
+
```javascript
|
|
169
|
+
// All view records (computed fresh each call in JSON mode)
|
|
170
|
+
const stats = await store.findAll('owner-stats');
|
|
171
|
+
|
|
172
|
+
// Single view record by source ID
|
|
173
|
+
const stat = await store.find('owner-stats', ownerId);
|
|
174
|
+
|
|
175
|
+
// With conditions
|
|
176
|
+
const filtered = await store.findAll('owner-stats', { animalCount: 5 });
|
|
177
|
+
```
|
|
178
|
+
|
|
179
|
+
## Read-Only Enforcement
|
|
180
|
+
|
|
181
|
+
Views are strictly read-only at all layers:
|
|
182
|
+
|
|
183
|
+
```javascript
|
|
184
|
+
// All of these throw errors:
|
|
185
|
+
createRecord('owner-stats', data); // Error: Cannot create records for read-only view
|
|
186
|
+
updateRecord(viewRecord, data); // Error: Cannot update records for read-only view
|
|
187
|
+
store.remove('owner-stats', id); // Error: Cannot remove records from read-only view
|
|
188
|
+
|
|
189
|
+
// Internal use only — bypasses guard:
|
|
190
|
+
createRecord('owner-stats', data, { isDbRecord: true }); // Used by resolver/loader
|
|
191
|
+
```
|
|
192
|
+
|
|
193
|
+
## REST API Behavior
|
|
194
|
+
|
|
195
|
+
When a view is included in an access configuration, only GET endpoints are mounted:
|
|
196
|
+
|
|
197
|
+
- `GET /view-plural-name` — Returns list of view records
|
|
198
|
+
- `GET /view-plural-name/:id` — Returns single view record
|
|
199
|
+
- `GET /view-plural-name/:id/{relationship}` — Related resources
|
|
200
|
+
- No POST, PATCH, DELETE endpoints
|
|
201
|
+
|
|
202
|
+
## JSON Mode (In-Memory Resolver)
|
|
203
|
+
|
|
204
|
+
In JSON/non-MySQL mode, the ViewResolver:
|
|
205
|
+
|
|
206
|
+
1. Iterates all records of the `source` model from the store
|
|
207
|
+
2. For each source record:
|
|
208
|
+
- Computes aggregate properties from relationships
|
|
209
|
+
- Applies resolve map entries (string paths or functions)
|
|
210
|
+
- Maps regular attr fields from source data
|
|
211
|
+
3. Creates view records via `createRecord` with `isDbRecord: true`
|
|
212
|
+
4. Returns computed array
|
|
213
|
+
|
|
214
|
+
## MySQL Mode
|
|
215
|
+
|
|
216
|
+
In MySQL mode:
|
|
217
|
+
|
|
218
|
+
1. **Schema introspection** generates VIEW metadata via `introspectViews()`
|
|
219
|
+
2. **DDL generation** creates `CREATE OR REPLACE VIEW` SQL from aggregates and relationships
|
|
220
|
+
3. **Queries** use `SELECT * FROM \`view_name\`` just like tables
|
|
221
|
+
4. **Migrations** include `CREATE OR REPLACE VIEW` after table statements
|
|
222
|
+
5. **persist()** is a no-op for views
|
|
223
|
+
6. **loadMemoryRecords()** loads views with `memory: true` from the MySQL VIEW
|
|
224
|
+
|
|
225
|
+
### Generated SQL Example
|
|
226
|
+
|
|
227
|
+
For `OwnerStatsView` with `count('pets')` and `avg('pets', 'age')`:
|
|
228
|
+
|
|
229
|
+
```sql
|
|
230
|
+
CREATE OR REPLACE VIEW `owner-stats` AS
|
|
231
|
+
SELECT
|
|
232
|
+
`owners`.`id` AS `id`,
|
|
233
|
+
COUNT(`animals`.`id`) AS `animalCount`,
|
|
234
|
+
AVG(`animals`.`age`) AS `avgAge`
|
|
235
|
+
FROM `owners`
|
|
236
|
+
LEFT JOIN `animals` ON `animals`.`owner_id` = `owners`.`id`
|
|
237
|
+
GROUP BY `owners`.`id`
|
|
238
|
+
```
|
|
239
|
+
|
|
240
|
+
## Migration Support
|
|
241
|
+
|
|
242
|
+
Views are handled in migrations alongside tables:
|
|
243
|
+
|
|
244
|
+
- **Added views** → `CREATE OR REPLACE VIEW ...` in UP, `DROP VIEW IF EXISTS ...` in DOWN
|
|
245
|
+
- **Removed views** → Commented `DROP VIEW` warning in UP (matching model removal pattern)
|
|
246
|
+
- **Changed views** → `CREATE OR REPLACE VIEW ...` in UP (replaces automatically)
|
|
247
|
+
- Views appear AFTER table statements in migrations (dependency order)
|
|
248
|
+
- Snapshots include view entries with `isView: true` and `viewQuery`
|
|
249
|
+
|
|
250
|
+
## Memory Flag
|
|
251
|
+
|
|
252
|
+
- `static memory = false` (default) — View records are computed fresh on each query
|
|
253
|
+
- `static memory = true` — View records are loaded from MySQL VIEW on startup and cached
|
|
254
|
+
|
|
255
|
+
## Testing Views
|
|
256
|
+
|
|
257
|
+
```javascript
|
|
258
|
+
import QUnit from 'qunit';
|
|
259
|
+
import { store } from '@stonyx/orm';
|
|
260
|
+
|
|
261
|
+
QUnit.test('view returns computed data', async function(assert) {
|
|
262
|
+
// Create source data
|
|
263
|
+
createRecord('owner', { id: 1, name: 'Alice' }, { serialize: false });
|
|
264
|
+
createRecord('animal', { id: 1, age: 3, owner: 1 }, { serialize: false });
|
|
265
|
+
|
|
266
|
+
// Query the view
|
|
267
|
+
const results = await store.findAll('owner-stats');
|
|
268
|
+
const stat = results.find(r => r.id === 1);
|
|
269
|
+
|
|
270
|
+
assert.strictEqual(stat.__data.animalCount, 1);
|
|
271
|
+
});
|
|
272
|
+
```
|
|
273
|
+
|
|
274
|
+
## Architecture
|
|
275
|
+
|
|
276
|
+
### Source Files
|
|
277
|
+
|
|
278
|
+
| File | Purpose |
|
|
279
|
+
|------|---------|
|
|
280
|
+
| `src/view.js` | View base class |
|
|
281
|
+
| `src/aggregates.js` | AggregateProperty class + helper functions |
|
|
282
|
+
| `src/view-resolver.js` | In-memory view resolver |
|
|
283
|
+
| `src/mysql/schema-introspector.js` | `introspectViews()`, `buildViewDDL()` |
|
|
284
|
+
| `src/mysql/migration-generator.js` | `diffViewSnapshots()`, view migration generation |
|
|
285
|
+
|
|
286
|
+
### Key Design Decisions
|
|
287
|
+
|
|
288
|
+
1. **View does NOT extend Model** — conceptually separate; shared behavior is minimal
|
|
289
|
+
2. **Driver-agnostic API** — No SQL in view definitions; MySQL driver generates SQL automatically
|
|
290
|
+
3. **Aggregate helpers follow the transform pattern** — Each knows both JS computation and MySQL mapping
|
|
291
|
+
4. **Resolve map mirrors Serializer.map** — String paths or function resolvers
|
|
292
|
+
5. **View schemas are separate** — `introspectViews()` is separate from `introspectModels()`
|
package/README.md
CHANGED
|
@@ -109,6 +109,22 @@ export default class OwnerModel extends Model {
|
|
|
109
109
|
}
|
|
110
110
|
```
|
|
111
111
|
|
|
112
|
+
### Overriding Plural Names
|
|
113
|
+
|
|
114
|
+
By default, model names are auto-pluralized for REST routes, JSON:API URLs, and DB table names (e.g., `animal` → `animals`). When auto-pluralization produces the wrong result, override it with `static pluralName`:
|
|
115
|
+
|
|
116
|
+
```js
|
|
117
|
+
import { Model, attr } from '@stonyx/orm';
|
|
118
|
+
|
|
119
|
+
export default class PersonModel extends Model {
|
|
120
|
+
static pluralName = 'people';
|
|
121
|
+
|
|
122
|
+
name = attr('string');
|
|
123
|
+
}
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
The override is picked up automatically during ORM initialization. All routes, JSON:API type references, and MySQL table names will use the overridden value.
|
|
127
|
+
|
|
112
128
|
## Serializers
|
|
113
129
|
|
|
114
130
|
Based on the following sample payload structure which represents a poorly structure third-party data source:
|
|
@@ -207,6 +223,27 @@ Set the `MYSQL_HOST` environment variable to enable MySQL persistence. The ORM l
|
|
|
207
223
|
| `stonyx db:migrate:rollback` | Rollback the most recent migration |
|
|
208
224
|
| `stonyx db:migrate:status` | Show migration status |
|
|
209
225
|
|
|
226
|
+
### Running MySQL Tests
|
|
227
|
+
|
|
228
|
+
The ORM includes integration tests that run against a real MySQL database. These are optional — all other tests work without MySQL.
|
|
229
|
+
|
|
230
|
+
**One-time setup:**
|
|
231
|
+
|
|
232
|
+
```bash
|
|
233
|
+
# Requires local MySQL 8.0+ running
|
|
234
|
+
./scripts/setup-test-db.sh
|
|
235
|
+
```
|
|
236
|
+
|
|
237
|
+
This creates a `stonyx_orm_test` database with a `stonyx_test` user. Safe to re-run.
|
|
238
|
+
|
|
239
|
+
**Running tests:**
|
|
240
|
+
|
|
241
|
+
```bash
|
|
242
|
+
npm test
|
|
243
|
+
```
|
|
244
|
+
|
|
245
|
+
MySQL integration tests run automatically when MySQL is available. In CI (where `CI=true`), they skip gracefully.
|
|
246
|
+
|
|
210
247
|
## REST Server Integration
|
|
211
248
|
|
|
212
249
|
The ORM can automatically register REST routes using your access classes.
|
package/config/environment.js
CHANGED
|
@@ -4,6 +4,7 @@ const {
|
|
|
4
4
|
ORM_REST_ROUTE,
|
|
5
5
|
ORM_SERIALIZER_PATH,
|
|
6
6
|
ORM_TRANSFORM_PATH,
|
|
7
|
+
ORM_VIEW_PATH,
|
|
7
8
|
ORM_USE_REST_SERVER,
|
|
8
9
|
DB_AUTO_SAVE,
|
|
9
10
|
DB_FILE,
|
|
@@ -36,7 +37,8 @@ export default {
|
|
|
36
37
|
access: ORM_ACCESS_PATH ?? './access', // Optional for restServer access hooks
|
|
37
38
|
model: ORM_MODEL_PATH ?? './models',
|
|
38
39
|
serializer: ORM_SERIALIZER_PATH ?? './serializers',
|
|
39
|
-
transform: ORM_TRANSFORM_PATH ?? './transforms'
|
|
40
|
+
transform: ORM_TRANSFORM_PATH ?? './transforms',
|
|
41
|
+
view: ORM_VIEW_PATH ?? './views'
|
|
40
42
|
},
|
|
41
43
|
mysql: MYSQL_HOST ? {
|
|
42
44
|
host: MYSQL_HOST ?? 'localhost',
|
package/package.json
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
"stonyx-async",
|
|
5
5
|
"stonyx-module"
|
|
6
6
|
],
|
|
7
|
-
"version": "0.2.1-beta.
|
|
7
|
+
"version": "0.2.1-beta.41",
|
|
8
8
|
"description": "",
|
|
9
9
|
"main": "src/main.js",
|
|
10
10
|
"type": "module",
|
|
@@ -33,21 +33,26 @@
|
|
|
33
33
|
},
|
|
34
34
|
"homepage": "https://github.com/abofs/stonyx-orm#readme",
|
|
35
35
|
"dependencies": {
|
|
36
|
-
"stonyx": "0.2.
|
|
37
|
-
"@stonyx/events": "0.1.1-beta.
|
|
38
|
-
"
|
|
36
|
+
"@stonyx/cron": "0.2.1-beta.15",
|
|
37
|
+
"@stonyx/events": "0.1.1-beta.7",
|
|
38
|
+
"stonyx": "0.2.3-beta.6"
|
|
39
39
|
},
|
|
40
40
|
"peerDependencies": {
|
|
41
|
+
"@stonyx/rest-server": ">=0.2.1-beta.14",
|
|
41
42
|
"mysql2": "^3.0.0"
|
|
42
43
|
},
|
|
43
44
|
"peerDependenciesMeta": {
|
|
44
45
|
"mysql2": {
|
|
45
46
|
"optional": true
|
|
47
|
+
},
|
|
48
|
+
"@stonyx/rest-server": {
|
|
49
|
+
"optional": true
|
|
46
50
|
}
|
|
47
51
|
},
|
|
48
52
|
"devDependencies": {
|
|
49
|
-
"@stonyx/rest-server": "0.2.1-beta.
|
|
50
|
-
"@stonyx/utils": "0.2.3-beta.
|
|
53
|
+
"@stonyx/rest-server": "0.2.1-beta.19",
|
|
54
|
+
"@stonyx/utils": "0.2.3-beta.5",
|
|
55
|
+
"mysql2": "^3.20.0",
|
|
51
56
|
"qunit": "^2.24.1",
|
|
52
57
|
"sinon": "^21.0.0"
|
|
53
58
|
},
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
#!/usr/bin/env bash
|
|
2
|
+
# scripts/setup-test-db.sh
|
|
3
|
+
# Creates the stonyx_orm_test database and stonyx_test user.
|
|
4
|
+
# Idempotent — safe to re-run. Requires local MySQL with root access.
|
|
5
|
+
|
|
6
|
+
set -euo pipefail
|
|
7
|
+
|
|
8
|
+
echo "Setting up stonyx-orm test database..."
|
|
9
|
+
|
|
10
|
+
mysql -u root <<SQL
|
|
11
|
+
CREATE DATABASE IF NOT EXISTS stonyx_orm_test CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
|
|
12
|
+
CREATE USER IF NOT EXISTS 'stonyx_test'@'localhost' IDENTIFIED BY 'stonyx_test';
|
|
13
|
+
GRANT ALL PRIVILEGES ON stonyx_orm_test.* TO 'stonyx_test'@'localhost';
|
|
14
|
+
FLUSH PRIVILEGES;
|
|
15
|
+
SQL
|
|
16
|
+
|
|
17
|
+
echo ""
|
|
18
|
+
echo "Done! Test database 'stonyx_orm_test' is ready."
|
|
19
|
+
echo "User: stonyx_test / Password: stonyx_test"
|
|
20
|
+
echo ""
|
|
21
|
+
echo "Run tests with: npm test"
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
export class AggregateProperty {
|
|
2
|
+
constructor(aggregateType, relationship, field) {
|
|
3
|
+
this.aggregateType = aggregateType;
|
|
4
|
+
this.relationship = relationship;
|
|
5
|
+
this.field = field;
|
|
6
|
+
this.mysqlFunction = aggregateType.toUpperCase();
|
|
7
|
+
this.resultType = aggregateType === 'avg' ? 'float' : 'number';
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
compute(relatedRecords) {
|
|
11
|
+
if (!relatedRecords || !Array.isArray(relatedRecords) || relatedRecords.length === 0) {
|
|
12
|
+
if (this.aggregateType === 'min' || this.aggregateType === 'max') return null;
|
|
13
|
+
return 0;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
switch (this.aggregateType) {
|
|
17
|
+
case 'count':
|
|
18
|
+
return relatedRecords.length;
|
|
19
|
+
|
|
20
|
+
case 'sum':
|
|
21
|
+
return relatedRecords.reduce((acc, record) => {
|
|
22
|
+
const val = parseFloat(record?.__data?.[this.field] ?? record?.[this.field]);
|
|
23
|
+
return acc + (isNaN(val) ? 0 : val);
|
|
24
|
+
}, 0);
|
|
25
|
+
|
|
26
|
+
case 'avg': {
|
|
27
|
+
let sum = 0;
|
|
28
|
+
let count = 0;
|
|
29
|
+
for (const record of relatedRecords) {
|
|
30
|
+
const val = parseFloat(record?.__data?.[this.field] ?? record?.[this.field]);
|
|
31
|
+
if (!isNaN(val)) {
|
|
32
|
+
sum += val;
|
|
33
|
+
count++;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
return count === 0 ? 0 : sum / count;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
case 'min': {
|
|
40
|
+
let min = null;
|
|
41
|
+
for (const record of relatedRecords) {
|
|
42
|
+
const val = parseFloat(record?.__data?.[this.field] ?? record?.[this.field]);
|
|
43
|
+
if (!isNaN(val) && (min === null || val < min)) min = val;
|
|
44
|
+
}
|
|
45
|
+
return min;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
case 'max': {
|
|
49
|
+
let max = null;
|
|
50
|
+
for (const record of relatedRecords) {
|
|
51
|
+
const val = parseFloat(record?.__data?.[this.field] ?? record?.[this.field]);
|
|
52
|
+
if (!isNaN(val) && (max === null || val > max)) max = val;
|
|
53
|
+
}
|
|
54
|
+
return max;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
default:
|
|
58
|
+
return null;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export function count(relationship) {
|
|
64
|
+
return new AggregateProperty('count', relationship);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export function avg(relationshipOrField, field) {
|
|
68
|
+
if (field !== undefined) {
|
|
69
|
+
return new AggregateProperty('avg', relationshipOrField, field);
|
|
70
|
+
}
|
|
71
|
+
return new AggregateProperty('avg', undefined, relationshipOrField);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
export function sum(relationshipOrField, field) {
|
|
75
|
+
if (field !== undefined) {
|
|
76
|
+
return new AggregateProperty('sum', relationshipOrField, field);
|
|
77
|
+
}
|
|
78
|
+
return new AggregateProperty('sum', undefined, relationshipOrField);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
export function min(relationshipOrField, field) {
|
|
82
|
+
if (field !== undefined) {
|
|
83
|
+
return new AggregateProperty('min', relationshipOrField, field);
|
|
84
|
+
}
|
|
85
|
+
return new AggregateProperty('min', undefined, relationshipOrField);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
export function max(relationshipOrField, field) {
|
|
89
|
+
if (field !== undefined) {
|
|
90
|
+
return new AggregateProperty('max', relationshipOrField, field);
|
|
91
|
+
}
|
|
92
|
+
return new AggregateProperty('max', undefined, relationshipOrField);
|
|
93
|
+
}
|