@stonyx/orm 0.2.5-alpha.0 → 0.3.1
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 +482 -15
- package/config/environment.js +63 -6
- package/dist/aggregates.d.ts +21 -0
- package/dist/aggregates.js +93 -0
- package/dist/attr.d.ts +2 -0
- package/dist/attr.js +22 -0
- package/dist/belongs-to.d.ts +11 -0
- package/dist/belongs-to.js +59 -0
- package/dist/cli.d.ts +22 -0
- package/dist/cli.js +148 -0
- package/dist/commands.d.ts +7 -0
- package/dist/commands.js +146 -0
- package/dist/db.d.ts +21 -0
- package/dist/db.js +180 -0
- package/dist/exports/db.d.ts +7 -0
- package/{src → dist}/exports/db.js +2 -4
- package/dist/has-many.d.ts +11 -0
- package/dist/has-many.js +58 -0
- package/dist/hooks.d.ts +75 -0
- package/dist/hooks.js +110 -0
- package/dist/index.d.ts +14 -0
- package/dist/index.js +34 -0
- package/dist/main.d.ts +46 -0
- package/dist/main.js +181 -0
- package/dist/manage-record.d.ts +13 -0
- package/dist/manage-record.js +123 -0
- package/dist/meta-request.d.ts +6 -0
- package/dist/meta-request.js +52 -0
- package/dist/migrate.d.ts +2 -0
- package/dist/migrate.js +57 -0
- package/dist/model-property.d.ts +9 -0
- package/dist/model-property.js +29 -0
- package/dist/model.d.ts +15 -0
- package/dist/model.js +18 -0
- package/dist/mysql/connection.d.ts +14 -0
- package/dist/mysql/connection.js +24 -0
- package/dist/mysql/migration-generator.d.ts +45 -0
- package/dist/mysql/migration-generator.js +254 -0
- package/dist/mysql/migration-runner.d.ts +12 -0
- package/dist/mysql/migration-runner.js +88 -0
- package/dist/mysql/mysql-db.d.ts +100 -0
- package/dist/mysql/mysql-db.js +425 -0
- package/dist/mysql/query-builder.d.ts +10 -0
- package/dist/mysql/query-builder.js +44 -0
- package/dist/mysql/schema-introspector.d.ts +19 -0
- package/dist/mysql/schema-introspector.js +257 -0
- package/dist/mysql/type-map.d.ts +21 -0
- package/dist/mysql/type-map.js +36 -0
- package/dist/orm-request.d.ts +38 -0
- package/dist/orm-request.js +475 -0
- package/dist/plural-registry.d.ts +4 -0
- package/dist/plural-registry.js +9 -0
- package/dist/postgres/connection.d.ts +15 -0
- package/dist/postgres/connection.js +32 -0
- package/dist/postgres/migration-generator.d.ts +45 -0
- package/dist/postgres/migration-generator.js +280 -0
- package/dist/postgres/migration-runner.d.ts +10 -0
- package/dist/postgres/migration-runner.js +87 -0
- package/dist/postgres/postgres-db.d.ts +119 -0
- package/dist/postgres/postgres-db.js +477 -0
- package/dist/postgres/query-builder.d.ts +27 -0
- package/dist/postgres/query-builder.js +98 -0
- package/dist/postgres/schema-introspector.d.ts +29 -0
- package/dist/postgres/schema-introspector.js +296 -0
- package/dist/postgres/type-map.d.ts +23 -0
- package/dist/postgres/type-map.js +56 -0
- package/dist/record.d.ts +75 -0
- package/dist/record.js +129 -0
- package/dist/relationships.d.ts +10 -0
- package/dist/relationships.js +41 -0
- package/dist/schema-helpers.d.ts +20 -0
- package/dist/schema-helpers.js +48 -0
- package/dist/serializer.d.ts +17 -0
- package/dist/serializer.js +136 -0
- package/dist/setup-rest-server.d.ts +1 -0
- package/dist/setup-rest-server.js +52 -0
- package/dist/standalone-db.d.ts +58 -0
- package/dist/standalone-db.js +142 -0
- package/dist/store.d.ts +62 -0
- package/dist/store.js +286 -0
- package/dist/timescale/query-builder.d.ts +43 -0
- package/dist/timescale/query-builder.js +115 -0
- package/dist/timescale/timescale-db.d.ts +45 -0
- package/dist/timescale/timescale-db.js +84 -0
- package/dist/transforms.d.ts +2 -0
- package/dist/transforms.js +17 -0
- package/dist/types/orm-types.d.ts +153 -0
- package/dist/types/orm-types.js +1 -0
- package/dist/utils.d.ts +7 -0
- package/dist/utils.js +17 -0
- package/dist/view-resolver.d.ts +8 -0
- package/dist/view-resolver.js +171 -0
- package/dist/view.d.ts +11 -0
- package/dist/view.js +18 -0
- package/package.json +64 -11
- package/src/aggregates.ts +109 -0
- package/src/{attr.js → attr.ts} +2 -2
- package/src/belongs-to.ts +90 -0
- package/src/cli.ts +183 -0
- package/src/commands.ts +179 -0
- package/src/db.ts +232 -0
- package/src/exports/db.ts +7 -0
- package/src/has-many.ts +92 -0
- package/src/hooks.ts +151 -0
- package/src/{index.js → index.ts} +12 -2
- package/src/main.ts +229 -0
- package/src/manage-record.ts +161 -0
- package/src/{meta-request.js → meta-request.ts} +17 -14
- package/src/migrate.ts +72 -0
- package/src/model-property.ts +35 -0
- package/src/model.ts +21 -0
- package/src/mysql/connection.ts +43 -0
- package/src/mysql/migration-generator.ts +337 -0
- package/src/mysql/migration-runner.ts +121 -0
- package/src/mysql/mysql-db.ts +543 -0
- package/src/mysql/query-builder.ts +69 -0
- package/src/mysql/schema-introspector.ts +310 -0
- package/src/mysql/type-map.ts +42 -0
- package/src/orm-request.ts +582 -0
- package/src/plural-registry.ts +12 -0
- package/src/postgres/connection.ts +48 -0
- package/src/postgres/migration-generator.ts +370 -0
- package/src/postgres/migration-runner.ts +115 -0
- package/src/postgres/postgres-db.ts +616 -0
- package/src/postgres/query-builder.ts +148 -0
- package/src/postgres/schema-introspector.ts +360 -0
- package/src/postgres/type-map.ts +61 -0
- package/src/record.ts +186 -0
- package/src/relationships.ts +54 -0
- package/src/schema-helpers.ts +59 -0
- package/src/serializer.ts +161 -0
- package/src/setup-rest-server.ts +62 -0
- package/src/standalone-db.ts +185 -0
- package/src/store.ts +373 -0
- package/src/timescale/query-builder.ts +174 -0
- package/src/timescale/timescale-db.ts +119 -0
- package/src/transforms.ts +20 -0
- package/src/types/mysql2.d.ts +49 -0
- package/src/types/orm-types.ts +158 -0
- package/src/types/pg.d.ts +32 -0
- package/src/types/stonyx-cron.d.ts +5 -0
- package/src/types/stonyx-events.d.ts +4 -0
- package/src/types/stonyx-rest-server.d.ts +16 -0
- package/src/types/stonyx-utils.d.ts +33 -0
- package/src/types/stonyx.d.ts +21 -0
- package/src/utils.ts +22 -0
- package/src/view-resolver.ts +211 -0
- package/src/view.ts +22 -0
- package/.claude/project-structure.md +0 -578
- package/.github/workflows/ci.yml +0 -36
- package/.github/workflows/publish.yml +0 -143
- package/src/belongs-to.js +0 -63
- package/src/db.js +0 -80
- package/src/has-many.js +0 -61
- package/src/main.js +0 -119
- package/src/manage-record.js +0 -103
- package/src/model-property.js +0 -29
- package/src/model.js +0 -9
- package/src/orm-request.js +0 -249
- package/src/record.js +0 -100
- package/src/relationships.js +0 -43
- package/src/serializer.js +0 -138
- package/src/setup-rest-server.js +0 -57
- package/src/store.js +0 -211
- package/src/transforms.js +0 -20
- package/stonyx-bootstrap.cjs +0 -30
package/src/store.js
DELETED
|
@@ -1,211 +0,0 @@
|
|
|
1
|
-
import { relationships } from '@stonyx/orm';
|
|
2
|
-
import { TYPES } from './relationships.js';
|
|
3
|
-
|
|
4
|
-
export default class Store {
|
|
5
|
-
constructor() {
|
|
6
|
-
if (Store.instance) return Store.instance;
|
|
7
|
-
Store.instance = this;
|
|
8
|
-
|
|
9
|
-
this.data = new Map();
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
get(key, id) {
|
|
13
|
-
if (!id) return this.data.get(key);
|
|
14
|
-
|
|
15
|
-
return this.data.get(key)?.get(id);
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
set(key, value) {
|
|
19
|
-
this.data.set(key, value);
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
remove(key, id) {
|
|
23
|
-
if (id) return this.unloadRecord(key, id);
|
|
24
|
-
|
|
25
|
-
this.unloadAllRecords(key);
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
unloadRecord(model, id, options={}) {
|
|
29
|
-
const modelStore = this.data.get(model);
|
|
30
|
-
|
|
31
|
-
if (!modelStore) {
|
|
32
|
-
console.warn(`[Store] Cannot unload record: model "${model}" not found in store`);
|
|
33
|
-
return;
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
const record = modelStore.get(id);
|
|
37
|
-
|
|
38
|
-
if (!record) {
|
|
39
|
-
console.warn(`[Store] Cannot unload record: ${model}:${id} not found in store`);
|
|
40
|
-
return;
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
const { toUnload, visited } = options.includeChildren
|
|
44
|
-
? this._buildUnloadQueue(record, options)
|
|
45
|
-
: { toUnload: [{ record, modelName: model, recordId: id }], visited: new Set([`${model}:${id}`]) };
|
|
46
|
-
|
|
47
|
-
for (const item of toUnload.reverse()) {
|
|
48
|
-
const { record: recordToUnload, modelName, recordId } = item;
|
|
49
|
-
|
|
50
|
-
this._removeFromHasManyArrays(modelName, recordId, visited);
|
|
51
|
-
this._nullifyBelongsToReferences(modelName, recordId, visited);
|
|
52
|
-
this._cleanupRelationshipRegistries(modelName, recordId);
|
|
53
|
-
recordToUnload.clean();
|
|
54
|
-
|
|
55
|
-
this.data.get(modelName).delete(recordId);
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
unloadAllRecords(model, options={}) {
|
|
60
|
-
const modelStore = this.data.get(model);
|
|
61
|
-
|
|
62
|
-
if (!modelStore) {
|
|
63
|
-
console.warn(`[Store] Cannot unload all records: model "${model}" not found in store`);
|
|
64
|
-
return;
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
const recordIds = Array.from(modelStore.keys());
|
|
68
|
-
|
|
69
|
-
for (const id of recordIds) {
|
|
70
|
-
if (modelStore.has(id)) {
|
|
71
|
-
this.unloadRecord(model, id, options);
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
for (const relationshipType of TYPES) relationships.get(relationshipType).delete(model);
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
_removeFromHasManyArrays(modelName, recordId, visited) {
|
|
79
|
-
const hasManyRegistry = relationships.get('hasMany');
|
|
80
|
-
|
|
81
|
-
for (const [sourceModel, targetModels] of hasManyRegistry) {
|
|
82
|
-
const targetModelMap = targetModels.get(modelName);
|
|
83
|
-
if (!targetModelMap) continue;
|
|
84
|
-
|
|
85
|
-
for (const [sourceRecordId, hasManyArray] of targetModelMap) {
|
|
86
|
-
const sourceKey = `${sourceModel}:${sourceRecordId}`;
|
|
87
|
-
|
|
88
|
-
// Don't modify arrays of records being deleted
|
|
89
|
-
if (visited.has(sourceKey)) continue;
|
|
90
|
-
|
|
91
|
-
const index = hasManyArray.findIndex(r => r && r.id === recordId);
|
|
92
|
-
if (index !== -1) hasManyArray.splice(index, 1);
|
|
93
|
-
}
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
_nullifyBelongsToReferences(modelName, recordId, visited) {
|
|
98
|
-
const belongsToRegistry = relationships.get('belongsTo');
|
|
99
|
-
|
|
100
|
-
for (const [sourceModel, targetModels] of belongsToRegistry) {
|
|
101
|
-
const targetModelMap = targetModels.get(modelName);
|
|
102
|
-
if (!targetModelMap) continue;
|
|
103
|
-
|
|
104
|
-
for (const [sourceRecordId, belongsToRecord] of targetModelMap) {
|
|
105
|
-
if (belongsToRecord && belongsToRecord.id === recordId) {
|
|
106
|
-
const sourceKey = `${sourceModel}:${sourceRecordId}`;
|
|
107
|
-
|
|
108
|
-
if (visited.has(sourceKey)) continue;
|
|
109
|
-
targetModelMap.set(sourceRecordId, null);
|
|
110
|
-
|
|
111
|
-
const sourceRecord = this.get(sourceModel, sourceRecordId);
|
|
112
|
-
if (sourceRecord && sourceRecord.__relationships) {
|
|
113
|
-
for (const [key, value] of Object.entries(sourceRecord.__relationships)) {
|
|
114
|
-
if (value && value.id === recordId) {
|
|
115
|
-
sourceRecord.__relationships[key] = null;
|
|
116
|
-
}
|
|
117
|
-
}
|
|
118
|
-
}
|
|
119
|
-
}
|
|
120
|
-
}
|
|
121
|
-
}
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
_cleanupRelationshipRegistries(modelName, recordId) {
|
|
125
|
-
const hasManyMap = relationships.get('hasMany').get(modelName);
|
|
126
|
-
if (hasManyMap) {
|
|
127
|
-
for (const [, recordMap] of hasManyMap) recordMap.delete(recordId);
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
const belongsToMap = relationships.get('belongsTo').get(modelName);
|
|
131
|
-
if (belongsToMap) {
|
|
132
|
-
for (const [, recordMap] of belongsToMap) recordMap.delete(recordId);
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
const pendingMap = relationships.get('pending').get(modelName);
|
|
136
|
-
if (pendingMap) pendingMap.delete(recordId);
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
/**
|
|
140
|
-
* Extracts hasMany and non-bidirectional belongsTo children from a record
|
|
141
|
-
* @private
|
|
142
|
-
*/
|
|
143
|
-
_getChildren(record) {
|
|
144
|
-
const children = [];
|
|
145
|
-
|
|
146
|
-
if (!record.__relationships) return children;
|
|
147
|
-
|
|
148
|
-
for (const [key, value] of Object.entries(record.__relationships)) {
|
|
149
|
-
// hasMany children - always include
|
|
150
|
-
if (Array.isArray(value)) {
|
|
151
|
-
for (const childRecord of value) {
|
|
152
|
-
if (childRecord) children.push({ childRecord, relationshipKey: key, type: 'hasMany' });
|
|
153
|
-
}
|
|
154
|
-
} else if (value && !this._isBidirectionalRelationship(
|
|
155
|
-
record.__model.__name,
|
|
156
|
-
value.__model.__name
|
|
157
|
-
)) {
|
|
158
|
-
children.push({ childRecord: value, relationshipKey: key, type: 'belongsTo' });
|
|
159
|
-
}
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
return children;
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
_isBidirectionalRelationship(sourceModel, targetModel) {
|
|
166
|
-
const hasManyRegistry = relationships.get('hasMany');
|
|
167
|
-
const inverseMap = hasManyRegistry.get(targetModel)?.get(sourceModel);
|
|
168
|
-
|
|
169
|
-
return inverseMap && inverseMap.size > 0;
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
_buildUnloadQueue(record, options) {
|
|
173
|
-
const visited = new Set();
|
|
174
|
-
const toUnload = [];
|
|
175
|
-
const queue = [{
|
|
176
|
-
record,
|
|
177
|
-
modelName: record.__model.__name,
|
|
178
|
-
recordId: record.id,
|
|
179
|
-
isRoot: true,
|
|
180
|
-
depth: 0
|
|
181
|
-
}];
|
|
182
|
-
|
|
183
|
-
while (queue.length > 0) {
|
|
184
|
-
const item = queue.shift();
|
|
185
|
-
const key = `${item.modelName}:${item.recordId}`;
|
|
186
|
-
|
|
187
|
-
if (visited.has(key)) continue;
|
|
188
|
-
visited.add(key);
|
|
189
|
-
|
|
190
|
-
toUnload.push(item);
|
|
191
|
-
|
|
192
|
-
// Add children to queue if includeChildren is enabled
|
|
193
|
-
if (options.includeChildren) {
|
|
194
|
-
const children = this._getChildren(item.record);
|
|
195
|
-
for (const { childRecord } of children) {
|
|
196
|
-
if (childRecord) {
|
|
197
|
-
queue.push({
|
|
198
|
-
record: childRecord,
|
|
199
|
-
modelName: childRecord.__model.__name,
|
|
200
|
-
recordId: childRecord.id,
|
|
201
|
-
isRoot: false,
|
|
202
|
-
depth: item.depth + 1
|
|
203
|
-
});
|
|
204
|
-
}
|
|
205
|
-
}
|
|
206
|
-
}
|
|
207
|
-
}
|
|
208
|
-
|
|
209
|
-
return { toUnload, visited };
|
|
210
|
-
}
|
|
211
|
-
}
|
package/src/transforms.js
DELETED
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
import { getTimestamp } from "@stonyx/utils/date";
|
|
2
|
-
|
|
3
|
-
const transforms = {
|
|
4
|
-
boolean: value => typeof value === 'string' ? value.trim().toLowerCase() === 'true' : !!value,
|
|
5
|
-
date: value => value ? new Date(value) : null,
|
|
6
|
-
float: value => parseFloat(value),
|
|
7
|
-
number: value => parseInt(value),
|
|
8
|
-
passthrough: value => value,
|
|
9
|
-
string: value => String(value),
|
|
10
|
-
timestamp: value => getTimestamp(value),
|
|
11
|
-
trim: value => value?.trim(),
|
|
12
|
-
uppercase: value => value?.toUpperCase(),
|
|
13
|
-
};
|
|
14
|
-
|
|
15
|
-
// Math Proxies
|
|
16
|
-
['ceil', 'floor', 'round'].forEach(method => {
|
|
17
|
-
transforms[method] = value => Math[method](value);
|
|
18
|
-
});
|
|
19
|
-
|
|
20
|
-
export default transforms;
|
package/stonyx-bootstrap.cjs
DELETED
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* commonJS Bootstrap loading - Stonyx must be loaded first, prior to the rest of the application
|
|
3
|
-
*/
|
|
4
|
-
const { default:Stonyx } = require('stonyx');
|
|
5
|
-
const { default:config } = require('./config/environment.js');
|
|
6
|
-
|
|
7
|
-
// Override paths for tests
|
|
8
|
-
Object.assign(config.paths, {
|
|
9
|
-
access: './test/sample/access',
|
|
10
|
-
model: './test/sample/models',
|
|
11
|
-
serializer: './test/sample/serializers',
|
|
12
|
-
transform: './test/sample/transforms'
|
|
13
|
-
})
|
|
14
|
-
|
|
15
|
-
// Override db settings for tests
|
|
16
|
-
Object.assign(config.db, {
|
|
17
|
-
file: './test/sample/db.json',
|
|
18
|
-
schema: './test/sample/db-schema.js'
|
|
19
|
-
});
|
|
20
|
-
|
|
21
|
-
// Create restServer module path for tests
|
|
22
|
-
config.modules = {
|
|
23
|
-
restServer: {
|
|
24
|
-
dir: './test/sample/requests'
|
|
25
|
-
}
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
new Stonyx(config, __dirname);
|
|
29
|
-
|
|
30
|
-
module.exports = Stonyx;
|