@hitchy/plugin-odem-socket.io 0.2.0 → 0.2.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.
- package/api/services/odem/websocket-provider.js +147 -148
- package/package.json +8 -8
|
@@ -20,11 +20,13 @@ module.exports = function() {
|
|
|
20
20
|
api.once( "websocket", io => {
|
|
21
21
|
const namespace = io.of( "/hitchy/odem" );
|
|
22
22
|
|
|
23
|
-
|
|
24
|
-
const
|
|
23
|
+
namespace.on( "connection", socket => {
|
|
24
|
+
for ( const modelName of Object.keys( api.models ) ) {
|
|
25
|
+
const model = api.models[modelName];
|
|
25
26
|
|
|
26
|
-
|
|
27
|
-
|
|
27
|
+
this.manageModel( namespace, socket, modelName, model );
|
|
28
|
+
}
|
|
29
|
+
} );
|
|
28
30
|
} );
|
|
29
31
|
}
|
|
30
32
|
|
|
@@ -33,14 +35,15 @@ module.exports = function() {
|
|
|
33
35
|
* provided server-side websocket.
|
|
34
36
|
*
|
|
35
37
|
* @param {Server} io server-side websocket
|
|
38
|
+
* @param {WebSocket} socket socket representing established incoming connection
|
|
36
39
|
* @param {string} modelName name of model to manage
|
|
37
40
|
* @param {class<Hitchy.Plugin.Odem.Model>} model ODM-based model instance to manage
|
|
38
41
|
* @returns {void}
|
|
39
42
|
*/
|
|
40
|
-
static manageModel( io, modelName, model ) {
|
|
43
|
+
static manageModel( io, socket, modelName, model ) {
|
|
41
44
|
const { crud, notifications } = api.config.socket.odem;
|
|
42
45
|
|
|
43
|
-
this.handleModelRequests( crud,
|
|
46
|
+
this.handleModelRequests( crud, socket, modelName, model );
|
|
44
47
|
this.forwardModelNotifications( notifications, io, modelName, model );
|
|
45
48
|
}
|
|
46
49
|
|
|
@@ -49,168 +52,164 @@ module.exports = function() {
|
|
|
49
52
|
* model-related actions.
|
|
50
53
|
*
|
|
51
54
|
* @param {boolean} enabled true if command handling is enabled in configuration
|
|
52
|
-
* @param {
|
|
55
|
+
* @param {WebSocket} socket socket representing established incoming connection
|
|
53
56
|
* @param {string} modelName name of model to manage
|
|
54
57
|
* @param {class<Hitchy.Plugin.Odem.Model>} model ODM-based model instance to manage
|
|
55
58
|
* @returns {void}
|
|
56
59
|
*/
|
|
57
|
-
static handleModelRequests( enabled,
|
|
60
|
+
static handleModelRequests( enabled, socket, modelName, model ) {
|
|
58
61
|
const prefix = api.utility.case.pascalToKebab( modelName );
|
|
59
62
|
|
|
60
63
|
if ( !enabled ) {
|
|
61
64
|
logDebug( "disabling socket-based action requests for model %s (%s)", modelName, prefix );
|
|
62
65
|
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
.on( `${prefix}:delete`, ( _, __, response ) => reject( response ) );
|
|
71
|
-
} );
|
|
66
|
+
socket
|
|
67
|
+
.on( `${prefix}:list`, ( _, __, ___, response ) => reject( response ) )
|
|
68
|
+
.on( `${prefix}:find`, ( _, __, ___, ____, response ) => reject( response ) )
|
|
69
|
+
.on( `${prefix}:create`, ( _, __, response ) => reject( response ) )
|
|
70
|
+
.on( `${prefix}:read`, ( _, __, response ) => reject( response ) )
|
|
71
|
+
.on( `${prefix}:update`, ( _, __, ___, response ) => reject( response ) )
|
|
72
|
+
.on( `${prefix}:delete`, ( _, __, response ) => reject( response ) );
|
|
72
73
|
|
|
73
74
|
return;
|
|
74
75
|
}
|
|
75
76
|
|
|
76
77
|
logDebug( "enabling socket-based action requests for model %s (%s)", modelName, prefix );
|
|
77
78
|
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
instance[propName] = properties[propName];
|
|
131
|
-
}
|
|
79
|
+
socket
|
|
80
|
+
.on( `${prefix}:list`, async( sessionId, queryOptions, loadRecords, response ) => {
|
|
81
|
+
logDebug( "request for listing %s instances w/ queryOptions %j and loadRecords %j", modelName, queryOptions, Boolean( loadRecords ) );
|
|
82
|
+
|
|
83
|
+
try {
|
|
84
|
+
const meta = {};
|
|
85
|
+
const items = await model.list( queryOptions, { loadRecords: Boolean( loadRecords ), metaCollector: meta } );
|
|
86
|
+
|
|
87
|
+
response( {
|
|
88
|
+
success: true,
|
|
89
|
+
count: meta.count,
|
|
90
|
+
items: items.map( item => ( loadRecords ? item.toObject( { serialized: true } ) : { uuid: item.uuid } ) ),
|
|
91
|
+
} );
|
|
92
|
+
} catch ( error ) {
|
|
93
|
+
logError( "listing %s instances failed: %s", modelName, error.stack );
|
|
94
|
+
|
|
95
|
+
response( {
|
|
96
|
+
error: error.message,
|
|
97
|
+
} );
|
|
98
|
+
}
|
|
99
|
+
} )
|
|
100
|
+
.on( `${prefix}:find`, async( sessionId, query, queryOptions, loadRecords, response ) => {
|
|
101
|
+
logDebug( "request for finding %s instances matching %j w/ queryOptions %j and uuidsOnly %j", modelName, query, queryOptions, Boolean( loadRecords ) );
|
|
102
|
+
|
|
103
|
+
try {
|
|
104
|
+
const meta = {};
|
|
105
|
+
const items = await model.find( query, queryOptions, { loadRecords: Boolean( loadRecords ), metaCollector: meta } );
|
|
106
|
+
|
|
107
|
+
response( {
|
|
108
|
+
success: true,
|
|
109
|
+
count: meta.count,
|
|
110
|
+
items: items.map( item => ( loadRecords ? item.toObject( { serialized: true } ) : { uuid: item.uuid } ) ),
|
|
111
|
+
} );
|
|
112
|
+
} catch ( error ) {
|
|
113
|
+
logError( "finding %s instances matching %j failed: %s", modelName, error.stack );
|
|
114
|
+
|
|
115
|
+
response( {
|
|
116
|
+
error: error.message,
|
|
117
|
+
} );
|
|
118
|
+
}
|
|
119
|
+
} )
|
|
120
|
+
.on( `${prefix}:create`, async( sessionId, properties, response ) => {
|
|
121
|
+
logDebug( "request for creating %s instance with %j", modelName, properties );
|
|
122
|
+
|
|
123
|
+
try {
|
|
124
|
+
const schema = model.schema;
|
|
125
|
+
const instance = new model; // eslint-disable-line new-cap
|
|
126
|
+
|
|
127
|
+
for ( const propName of Object.keys( properties ) ) {
|
|
128
|
+
if ( schema.props.hasOwnProperty( propName ) ||
|
|
129
|
+
schema.computed.hasOwnProperty( propName ) ) {
|
|
130
|
+
instance[propName] = properties[propName];
|
|
132
131
|
}
|
|
133
|
-
|
|
134
|
-
await instance.save();
|
|
135
|
-
|
|
136
|
-
response( {
|
|
137
|
-
success: true,
|
|
138
|
-
properties: instance.toObject( { serialized: true } ),
|
|
139
|
-
} );
|
|
140
|
-
} catch ( error ) {
|
|
141
|
-
logError( "creating %s instance failed: %s", modelName, error.stack );
|
|
142
|
-
|
|
143
|
-
response( {
|
|
144
|
-
error: error.message,
|
|
145
|
-
} );
|
|
146
132
|
}
|
|
147
|
-
} )
|
|
148
|
-
.on( `${prefix}:read`, async( sessionId, uuid, response ) => {
|
|
149
|
-
logError( "request for reading %s instance %s", modelName, uuid );
|
|
150
|
-
|
|
151
|
-
try {
|
|
152
|
-
const instance = new model( uuid ); // eslint-disable-line new-cap
|
|
153
|
-
await instance.load();
|
|
154
|
-
|
|
155
|
-
response( {
|
|
156
|
-
success: true,
|
|
157
|
-
properties: instance.toObject( { serialized: true } ),
|
|
158
|
-
} );
|
|
159
|
-
} catch ( error ) {
|
|
160
|
-
logError( "reading %s instance failed: %s", modelName, error.stack );
|
|
161
|
-
|
|
162
|
-
response( {
|
|
163
|
-
error: error.message,
|
|
164
|
-
} );
|
|
165
|
-
}
|
|
166
|
-
} )
|
|
167
|
-
.on( `${prefix}:update`, async( sessionId, uuid, properties, response ) => {
|
|
168
|
-
logError( "request for updating %s instance %s with %j", modelName, uuid, properties );
|
|
169
|
-
|
|
170
|
-
try {
|
|
171
|
-
const instance = new model( uuid ); // eslint-disable-line new-cap
|
|
172
|
-
await instance.load();
|
|
173
133
|
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
134
|
+
await instance.save();
|
|
135
|
+
|
|
136
|
+
response( {
|
|
137
|
+
success: true,
|
|
138
|
+
properties: instance.toObject( { serialized: true } ),
|
|
139
|
+
} );
|
|
140
|
+
} catch ( error ) {
|
|
141
|
+
logError( "creating %s instance failed: %s", modelName, error.stack );
|
|
142
|
+
|
|
143
|
+
response( {
|
|
144
|
+
error: error.message,
|
|
145
|
+
} );
|
|
146
|
+
}
|
|
147
|
+
} )
|
|
148
|
+
.on( `${prefix}:read`, async( sessionId, uuid, response ) => {
|
|
149
|
+
logDebug( "request for reading %s instance %s", modelName, uuid );
|
|
150
|
+
|
|
151
|
+
try {
|
|
152
|
+
const instance = new model( uuid ); // eslint-disable-line new-cap
|
|
153
|
+
await instance.load();
|
|
154
|
+
|
|
155
|
+
response( {
|
|
156
|
+
success: true,
|
|
157
|
+
properties: instance.toObject( { serialized: true } ),
|
|
158
|
+
} );
|
|
159
|
+
} catch ( error ) {
|
|
160
|
+
logError( "reading %s instance failed: %s", modelName, error.stack );
|
|
161
|
+
|
|
162
|
+
response( {
|
|
163
|
+
error: error.message,
|
|
164
|
+
} );
|
|
165
|
+
}
|
|
166
|
+
} )
|
|
167
|
+
.on( `${prefix}:update`, async( sessionId, uuid, properties, response ) => {
|
|
168
|
+
logDebug( "request for updating %s instance %s with %j", modelName, uuid, properties );
|
|
169
|
+
|
|
170
|
+
try {
|
|
171
|
+
const instance = new model( uuid ); // eslint-disable-line new-cap
|
|
172
|
+
await instance.load();
|
|
173
|
+
|
|
174
|
+
for ( const propName of Object.keys( properties ) ) {
|
|
175
|
+
if ( model.schema.props.hasOwnProperty( propName ) || model.schema.computed.hasOwnProperty( propName ) ) {
|
|
176
|
+
instance[propName] = properties[propName];
|
|
178
177
|
}
|
|
179
|
-
|
|
180
|
-
await instance.save();
|
|
181
|
-
|
|
182
|
-
response( {
|
|
183
|
-
success: true,
|
|
184
|
-
properties: instance.toObject( { serialized: true } ),
|
|
185
|
-
} );
|
|
186
|
-
} catch ( error ) {
|
|
187
|
-
logError( "updating %s instance failed: %s", modelName, error.stack );
|
|
188
|
-
|
|
189
|
-
response( {
|
|
190
|
-
error: error.message,
|
|
191
|
-
} );
|
|
192
178
|
}
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
}
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
179
|
+
|
|
180
|
+
await instance.save();
|
|
181
|
+
|
|
182
|
+
response( {
|
|
183
|
+
success: true,
|
|
184
|
+
properties: instance.toObject( { serialized: true } ),
|
|
185
|
+
} );
|
|
186
|
+
} catch ( error ) {
|
|
187
|
+
logError( "updating %s instance failed: %s", modelName, error.stack );
|
|
188
|
+
|
|
189
|
+
response( {
|
|
190
|
+
error: error.message,
|
|
191
|
+
} );
|
|
192
|
+
}
|
|
193
|
+
} )
|
|
194
|
+
.on( `${prefix}:delete`, async( sessionId, uuid, response ) => {
|
|
195
|
+
logDebug( "request for deleting %s instance %s", modelName, uuid );
|
|
196
|
+
|
|
197
|
+
try {
|
|
198
|
+
const instance = new model( uuid ); // eslint-disable-line new-cap
|
|
199
|
+
await instance.remove();
|
|
200
|
+
|
|
201
|
+
response( {
|
|
202
|
+
success: true,
|
|
203
|
+
properties: { uuid },
|
|
204
|
+
} );
|
|
205
|
+
} catch ( error ) {
|
|
206
|
+
logError( "deleting %s instance %s failed: %s", modelName, uuid, error.stack );
|
|
207
|
+
|
|
208
|
+
response( {
|
|
209
|
+
error: error.message,
|
|
210
|
+
} );
|
|
211
|
+
}
|
|
212
|
+
} );
|
|
214
213
|
}
|
|
215
214
|
|
|
216
215
|
/**
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hitchy/plugin-odem-socket.io",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.2",
|
|
4
4
|
"description": "exposing Hitchy's ODM via websocket",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -35,13 +35,13 @@
|
|
|
35
35
|
"@hitchy/plugin-socket.io": ">=0.1.0"
|
|
36
36
|
},
|
|
37
37
|
"devDependencies": {
|
|
38
|
-
"@hitchy/server-dev-tools": "^0.4.
|
|
39
|
-
"c8": "^
|
|
40
|
-
"eslint": "^8.
|
|
41
|
-
"eslint-config-cepharum": "^1.0.
|
|
42
|
-
"eslint-plugin-promise": "^6.
|
|
43
|
-
"mocha": "^
|
|
38
|
+
"@hitchy/server-dev-tools": "^0.4.5",
|
|
39
|
+
"c8": "^8.0.1",
|
|
40
|
+
"eslint": "^8.47.0",
|
|
41
|
+
"eslint-config-cepharum": "^1.0.13",
|
|
42
|
+
"eslint-plugin-promise": "^6.1.1",
|
|
43
|
+
"mocha": "^10.2.0",
|
|
44
44
|
"should": "^13.2.3",
|
|
45
|
-
"socket.io-client": "^4.
|
|
45
|
+
"socket.io-client": "^4.7.2"
|
|
46
46
|
}
|
|
47
47
|
}
|