@hitchy/plugin-odem-socket.io 0.1.0
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/LICENSE +21 -0
- package/api/services/odem/websocket-provider.js +272 -0
- package/config/socket.js +22 -0
- package/hitchy.json +8 -0
- package/index.js +11 -0
- package/package.json +47 -0
- package/readme.md +166 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2022 cepharum GmbH
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,272 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
module.exports = function() {
|
|
4
|
+
const api = this;
|
|
5
|
+
|
|
6
|
+
const logDebug = api.log( "hitchy:odem:socket.io:debug" );
|
|
7
|
+
const logError = api.log( "hitchy:odem:socket.io:error" );
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Implements API for controlling and monitoring server-side ODM via
|
|
11
|
+
* websocket.
|
|
12
|
+
*/
|
|
13
|
+
class OdemWebsocketProvider {
|
|
14
|
+
/**
|
|
15
|
+
* Integrates this provider with server-side websocket.
|
|
16
|
+
*
|
|
17
|
+
* @returns {void}
|
|
18
|
+
*/
|
|
19
|
+
static start() {
|
|
20
|
+
api.once( "websocket", io => {
|
|
21
|
+
const namespace = io.of( "/hitchy/odem" );
|
|
22
|
+
|
|
23
|
+
for ( const modelName of Object.keys( api.models ) ) {
|
|
24
|
+
const model = api.models[modelName];
|
|
25
|
+
|
|
26
|
+
this.manageModel( namespace, modelName, model );
|
|
27
|
+
}
|
|
28
|
+
} );
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Initializes manager for controlling and monitoring named model via
|
|
33
|
+
* provided server-side websocket.
|
|
34
|
+
*
|
|
35
|
+
* @param {Server} io server-side websocket
|
|
36
|
+
* @param {string} modelName name of model to manage
|
|
37
|
+
* @param {class<Hitchy.Plugin.Odem.Model>} model ODM-based model instance to manage
|
|
38
|
+
* @returns {void}
|
|
39
|
+
*/
|
|
40
|
+
static manageModel( io, modelName, model ) {
|
|
41
|
+
const { crud, notifications } = api.config.socket.odem;
|
|
42
|
+
|
|
43
|
+
this.handleModelRequests( crud, io, modelName, model );
|
|
44
|
+
this.forwardModelNotifications( notifications, io, modelName, model );
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Registers listeners for handling client-side events requesting
|
|
49
|
+
* model-related actions.
|
|
50
|
+
*
|
|
51
|
+
* @param {boolean} enabled true if command handling is enabled in configuration
|
|
52
|
+
* @param {Server} io server-side websocket
|
|
53
|
+
* @param {string} modelName name of model to manage
|
|
54
|
+
* @param {class<Hitchy.Plugin.Odem.Model>} model ODM-based model instance to manage
|
|
55
|
+
* @returns {void}
|
|
56
|
+
*/
|
|
57
|
+
static handleModelRequests( enabled, io, modelName, model ) {
|
|
58
|
+
const prefix = api.utility.case.pascalToKebab( modelName );
|
|
59
|
+
|
|
60
|
+
if ( !enabled ) {
|
|
61
|
+
logDebug( "disabling socket-based action requests for model %s (%s)", modelName, prefix );
|
|
62
|
+
|
|
63
|
+
io.on( "connection", socket => {
|
|
64
|
+
socket
|
|
65
|
+
.on( `${prefix}:list`, ( _, __, ___, response ) => reject( response ) )
|
|
66
|
+
.on( `${prefix}:find`, ( _, __, ___, ____, response ) => reject( response ) )
|
|
67
|
+
.on( `${prefix}:create`, ( _, __, response ) => reject( response ) )
|
|
68
|
+
.on( `${prefix}:read`, ( _, __, response ) => reject( response ) )
|
|
69
|
+
.on( `${prefix}:update`, ( _, __, ___, response ) => reject( response ) )
|
|
70
|
+
.on( `${prefix}:delete`, ( _, __, response ) => reject( response ) );
|
|
71
|
+
} );
|
|
72
|
+
|
|
73
|
+
return;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
logDebug( "enabling socket-based action requests for model %s (%s)", modelName, prefix );
|
|
77
|
+
|
|
78
|
+
io.on( "connection", socket => {
|
|
79
|
+
socket
|
|
80
|
+
.on( `${prefix}:list`, async( sessionId, queryOptions, uuidsOnly, response ) => {
|
|
81
|
+
logError( "request for listing %s instances w/ queryOptions %j and uuidsOnly %j", modelName, queryOptions, uuidsOnly );
|
|
82
|
+
|
|
83
|
+
try {
|
|
84
|
+
const meta = {};
|
|
85
|
+
const items = await model.list( queryOptions, { loadRecords: !uuidsOnly, metaCollector: meta } );
|
|
86
|
+
|
|
87
|
+
response( {
|
|
88
|
+
success: true,
|
|
89
|
+
count: meta.count,
|
|
90
|
+
items: items.map( item => ( uuidsOnly ? { uuid: item.uuid } : item.toObject( { serialized: true } ) ) ),
|
|
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, uuidsOnly, response ) => {
|
|
101
|
+
logError( "request for finding %s instances matching %j w/ queryOptions %j and uuidsOnly %j", modelName, query, queryOptions, uuidsOnly );
|
|
102
|
+
|
|
103
|
+
try {
|
|
104
|
+
const meta = {};
|
|
105
|
+
const items = await model.find( query, queryOptions, { loadRecords: !uuidsOnly, metaCollector: meta } );
|
|
106
|
+
|
|
107
|
+
response( {
|
|
108
|
+
success: true,
|
|
109
|
+
count: meta.count,
|
|
110
|
+
items: items.map( item => ( uuidsOnly ? { uuid: item.uuid } : item.toObject( { serialized: true } ) ) ),
|
|
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
|
+
logError( "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];
|
|
131
|
+
}
|
|
132
|
+
}
|
|
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
|
+
}
|
|
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
|
+
|
|
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];
|
|
177
|
+
}
|
|
178
|
+
}
|
|
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
|
+
logError( "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
|
+
} );
|
|
213
|
+
} );
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
/**
|
|
217
|
+
* Sets up listeners for model-related notifications in ODM and forwards
|
|
218
|
+
* them as broadcast events to connected clients.
|
|
219
|
+
*
|
|
220
|
+
* @param {boolean} enabled true if command handling is enabled in configuration
|
|
221
|
+
* @param {Server} io server-side websocket
|
|
222
|
+
* @param {string} modelName name of model to manage
|
|
223
|
+
* @param {class<Hitchy.Plugin.Odem.Model>} model ODM-based model instance to manage
|
|
224
|
+
* @returns {void}
|
|
225
|
+
*/
|
|
226
|
+
static forwardModelNotifications( enabled, io, modelName, model ) {
|
|
227
|
+
if ( enabled ) {
|
|
228
|
+
const prefix = api.utility.case.pascalToKebab( modelName );
|
|
229
|
+
|
|
230
|
+
model.notifications
|
|
231
|
+
.on( "created", async( uuid, newRecord, asyncGeneratorFn ) => {
|
|
232
|
+
const event = {
|
|
233
|
+
change: "created",
|
|
234
|
+
properties: ( await asyncGeneratorFn() ).toObject( { serialized: true } ),
|
|
235
|
+
};
|
|
236
|
+
|
|
237
|
+
io.emit( `${prefix}:changed`, event );
|
|
238
|
+
} )
|
|
239
|
+
.on( "changed", async( uuid, newRecord, oldRecord, asyncGeneratorFn ) => {
|
|
240
|
+
const event = {
|
|
241
|
+
change: "updated",
|
|
242
|
+
properties: ( await asyncGeneratorFn() ).toObject( { serialized: true } ),
|
|
243
|
+
};
|
|
244
|
+
|
|
245
|
+
io.emit( `${prefix}:changed`, event );
|
|
246
|
+
} )
|
|
247
|
+
.on( "removed", uuid => {
|
|
248
|
+
const event = {
|
|
249
|
+
change: "deleted",
|
|
250
|
+
properties: { uuid: model.formatUUID( uuid ) },
|
|
251
|
+
};
|
|
252
|
+
|
|
253
|
+
io.emit( `${prefix}:changed`, event );
|
|
254
|
+
} );
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
return OdemWebsocketProvider;
|
|
260
|
+
};
|
|
261
|
+
|
|
262
|
+
/**
|
|
263
|
+
* Commonly responds to action requests in case of having disabled them.
|
|
264
|
+
*
|
|
265
|
+
* @param {function(response: any):void} respondFn callback to be invoked for responding to a peer event
|
|
266
|
+
* @returns {void}
|
|
267
|
+
*/
|
|
268
|
+
function reject( respondFn ) {
|
|
269
|
+
respondFn( {
|
|
270
|
+
error: "requested action is not available due to runtime configuration",
|
|
271
|
+
} );
|
|
272
|
+
}
|
package/config/socket.js
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
module.exports = {
|
|
4
|
+
socket: {
|
|
5
|
+
odem: {
|
|
6
|
+
/**
|
|
7
|
+
* Controls if server-side socket is handling client-side requests
|
|
8
|
+
* for creating, reading, updating and deleting (CRUD) model items.
|
|
9
|
+
*
|
|
10
|
+
* Currently, this is false by default due to the lack of obeying
|
|
11
|
+
* authorization control.
|
|
12
|
+
*/
|
|
13
|
+
crud: false,
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Controls if server-side socket is broadcasting events on
|
|
17
|
+
* notifications emitted by either server-side model on change.
|
|
18
|
+
*/
|
|
19
|
+
notifications: true,
|
|
20
|
+
},
|
|
21
|
+
},
|
|
22
|
+
};
|
package/hitchy.json
ADDED
package/index.js
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@hitchy/plugin-odem-socket.io",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "exposing Hitchy's ODM via websocket",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"lint": "eslint .",
|
|
8
|
+
"test": "hitchy-pm odem socket.io --exec mocha --ui=tdd 'test/**/*.spec.js*'",
|
|
9
|
+
"coverage": "hitchy-pm odem socket.io --exec c8 mocha --ui=tdd 'test/**/*.spec.js*'"
|
|
10
|
+
},
|
|
11
|
+
"repository": {
|
|
12
|
+
"type": "git",
|
|
13
|
+
"url": "git+https://gitlab.com/hitchy/plugin-odem-socket.io.git"
|
|
14
|
+
},
|
|
15
|
+
"keywords": [
|
|
16
|
+
"Hitchy",
|
|
17
|
+
"socket.io",
|
|
18
|
+
"ODM"
|
|
19
|
+
],
|
|
20
|
+
"author": "cepharum GmbH",
|
|
21
|
+
"license": "MIT",
|
|
22
|
+
"bugs": {
|
|
23
|
+
"url": "https://gitlab.com/hitchy/plugin-odem-socket.io/issues"
|
|
24
|
+
},
|
|
25
|
+
"homepage": "https://gitlab.com/hitchy/plugin-odem-socket.io#readme",
|
|
26
|
+
"files": [
|
|
27
|
+
"api",
|
|
28
|
+
"config",
|
|
29
|
+
"hitchy.json",
|
|
30
|
+
"index.js"
|
|
31
|
+
],
|
|
32
|
+
"peerDependencies": {
|
|
33
|
+
"@hitchy/core": ">=0.7.2",
|
|
34
|
+
"@hitchy/plugin-odem": ">=0.7.1",
|
|
35
|
+
"@hitchy/plugin-socket.io": ">=0.1.0"
|
|
36
|
+
},
|
|
37
|
+
"devDependencies": {
|
|
38
|
+
"@hitchy/server-dev-tools": "^0.4.3",
|
|
39
|
+
"c8": "^7.11.2",
|
|
40
|
+
"eslint": "^8.14.0",
|
|
41
|
+
"eslint-config-cepharum": "^1.0.12",
|
|
42
|
+
"eslint-plugin-promise": "^6.0.0",
|
|
43
|
+
"mocha": "^9.2.2",
|
|
44
|
+
"should": "^13.2.3",
|
|
45
|
+
"socket.io-client": "^4.5.0"
|
|
46
|
+
}
|
|
47
|
+
}
|
package/readme.md
ADDED
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
# @hitchy/plugin-odem-socket.io
|
|
2
|
+
|
|
3
|
+
_exposing Hitchy's ODM via websocket_
|
|
4
|
+
|
|
5
|
+
## License
|
|
6
|
+
|
|
7
|
+
[MIT](LICENSE)
|
|
8
|
+
|
|
9
|
+
## Installation
|
|
10
|
+
|
|
11
|
+
```bash
|
|
12
|
+
npm i @hitchy/plugin-odem-socket.io
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
This plugin relies on additional plugins to be installed as well:
|
|
16
|
+
|
|
17
|
+
```bash
|
|
18
|
+
npm i @hitchy/plugin-odem @hitchy/plugin-socket.io
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
## Usage
|
|
23
|
+
|
|
24
|
+
### Basics
|
|
25
|
+
|
|
26
|
+
This plugin is establishing an API via websocket for controlling and monitoring models of server-side ODM. Relying on particular features of [socket.io](https://socket.io/), this API is exposed in [namespace](https://socket.io/docs/v4/namespaces/) `/hitchy/odem`.
|
|
27
|
+
|
|
28
|
+
For every server-side model, request listeners and/or notification broadcasters are set up. Either model's name is converted into its kebab-case variant and used as colon-separated prefix in event names emitted either by the server or by a client.
|
|
29
|
+
|
|
30
|
+
> In the following sections, the placeholder `<modelName>` is used instead of that kebab-case variant of a model's name.
|
|
31
|
+
|
|
32
|
+
### Control API
|
|
33
|
+
|
|
34
|
+
> **Attention!**
|
|
35
|
+
>
|
|
36
|
+
> As of v0.1.0, the control API is disabled by default due to the lack of having authorization checks implemented on server-side. You have to enable it explicitly by setting runtime configuration parameter `config.socket.odem.crud` which defaults to `false`.
|
|
37
|
+
|
|
38
|
+
All action events listed in this section are to be sent by a client. The server is setting up listeners and directly responds to either received event. Thus, the common pattern on client side looks like this:
|
|
39
|
+
|
|
40
|
+
```javascript
|
|
41
|
+
const socket = io( "/hitchy/odem" );
|
|
42
|
+
|
|
43
|
+
socket.on( "connect", () => {
|
|
44
|
+
socket.emit( actionName, sessionId, arg1, arg2, arg3, response => {
|
|
45
|
+
// process the response here
|
|
46
|
+
} );
|
|
47
|
+
} );
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
> socket.io requires all arguments to be provided no matter what. Thus, you can't omit any of the arguments given in examples below but have to provide values assumed to be default, at least.
|
|
51
|
+
|
|
52
|
+
The following code excerpts are focusing on the inner part of emitting an action event and processing the response.
|
|
53
|
+
|
|
54
|
+
In all these examples, a _sessionId_ is given. This is the current user's session ID usually found in a cookie set by a preceding HTTP request to authenticate the user. It is required on all requests to re-authenticate the user and to apply authorization checks to either request. It is okay to provide `null` here, however this might cause requests to be rejected based on server-side configuration.
|
|
55
|
+
|
|
56
|
+
#### <modelName>:list
|
|
57
|
+
|
|
58
|
+
This request is the websocket variant of [Model.list()](https://odem.hitchy.org/api/model.html#model-list).
|
|
59
|
+
|
|
60
|
+
First argument following the session ID is forwarded as first argument to [Model.list()](https://odem.hitchy.org/api/model.html#model-list). The following argument is a boolean controlling if resulting list should provide UUID per listed match, only.
|
|
61
|
+
|
|
62
|
+
```javascript
|
|
63
|
+
socket.emit( "<modelName>:list", sessionId, { limit: 10 }, false, response => {
|
|
64
|
+
// - check for `response.success` or `response.error`
|
|
65
|
+
// - process total count of items in `response.count`
|
|
66
|
+
// - process requested excerpt of items in `response.items`
|
|
67
|
+
} );
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
The `response` is an object consisting of truthy property `success` on success or `error` message in case of a failure. On success, property `count` is providing total count of matches and `items` is the list of matching items' properties.
|
|
71
|
+
|
|
72
|
+
#### <modelName>:find
|
|
73
|
+
|
|
74
|
+
This request is searching for instances of selected model matching some query. It is invoking [Model.find()](https://odem.hitchy.org/api/model.html#model-find) internally passing the first two arguments following the session ID as-is. The third argument is limited to a boolean controlling whether only UUIDs should be listed or not.
|
|
75
|
+
|
|
76
|
+
```javascript
|
|
77
|
+
socket.emit( "<modelName>:find", sessionId, {}, { limit: 10 }, true, response => {
|
|
78
|
+
// - check for `response.success` or `response.error`
|
|
79
|
+
// - process total count of items in `response.count`
|
|
80
|
+
// - process requested excerpt of items in `response.items`
|
|
81
|
+
} );
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
The `response` is an object consisting of truthy property `success` on success or `error` message in case of a failure. On success, property `count` is providing total count of matches and `items` is the selected excerpt from that list of matching items' each given by its properties including its UUID.
|
|
85
|
+
|
|
86
|
+
#### <modelName>:create
|
|
87
|
+
|
|
88
|
+
This request is creating another instance of selected model assigning provided properties as initial values. Properties of instance to create are provided in event argument following the session ID.
|
|
89
|
+
|
|
90
|
+
```javascript
|
|
91
|
+
socket.emit( "<modelName>:create", sessionId, { title: "important things" }, response => {
|
|
92
|
+
// - check for `response.success` or `response.error`
|
|
93
|
+
// - process properties of eventually created instance in `response.properties`
|
|
94
|
+
// - created instance's UUID is available as `response.properties.uuid`
|
|
95
|
+
} );
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
The `response` is an object consisting of truthy property `success` on success or `error` message in case of a failure. On success, `properties` of created instance are returned. These may be different from provided ones due to server-side constraints.
|
|
99
|
+
|
|
100
|
+
#### <modelName>:read
|
|
101
|
+
|
|
102
|
+
This request is fetching a single instance's properties.
|
|
103
|
+
|
|
104
|
+
```javascript
|
|
105
|
+
socket.emit( "<modelName>:create", sessionId, "12345678-1234-1234-1234-123456789012", response => {
|
|
106
|
+
// - check for `response.success` or `response.error`
|
|
107
|
+
// - process properties of fetched instance in `response.properties`
|
|
108
|
+
} );
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
The `response` is an object consisting of truthy property `success` on success or `error` message in case of a failure. On success, `properties` of selected instance are returned.
|
|
112
|
+
|
|
113
|
+
#### <modelName>:update
|
|
114
|
+
|
|
115
|
+
This request is updating an existing instance of model. Following the session ID, the selected item's UUID and the values per property to be assigned are provided in follow-up arguments.
|
|
116
|
+
|
|
117
|
+
```javascript
|
|
118
|
+
socket.emit( "<modelName>:update", sessionId, "12345678-1234-1234-1234-123456789012", { prio: 1 }, response => {
|
|
119
|
+
// - check for `response.success` or `response.error`
|
|
120
|
+
// - process properties of eventually created instance in `response.properties`
|
|
121
|
+
// - created instance's UUID is available as `response.properties.uuid`
|
|
122
|
+
} );
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
The `response` is an object consisting of truthy property `success` on success or `error` message in case of a failure. On success, `properties` of updated instance are returned. These may be different from provided ones due to server-side constraints.
|
|
126
|
+
|
|
127
|
+
#### <modelName>:delete
|
|
128
|
+
|
|
129
|
+
This request is eventually completing the CRUD-support of this API by deleting an existing instance of model selected by its UUID in first argument.
|
|
130
|
+
|
|
131
|
+
```javascript
|
|
132
|
+
socket.emit( "<modelName>:delete", sessionId, "12345678-1234-1234-1234-123456789012", response => {
|
|
133
|
+
// - check for `response.success` or `response.error`
|
|
134
|
+
// - process properties of eventually created instance in `response.properties`
|
|
135
|
+
// - created instance's UUID is available as `response.properties.uuid`
|
|
136
|
+
} );
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
The `response` is an object consisting of truthy property `success` on success or `error` message in case of a failure. On success, `properties` of updated instance are returned.
|
|
140
|
+
|
|
141
|
+
|
|
142
|
+
### Notifications
|
|
143
|
+
|
|
144
|
+
[ODM notifications](https://odem.hitchy.org/api/model.html#model-notifications) are forwarded as broadcast events named `<modelName>:changed`. Either event consists of
|
|
145
|
+
|
|
146
|
+
- a type of change and
|
|
147
|
+
- the changed item's properties (limited to its UUID on deletion).
|
|
148
|
+
|
|
149
|
+
The type of change is
|
|
150
|
+
|
|
151
|
+
* `created` on notifications about having created another instance of model,
|
|
152
|
+
* `updated` on notifications about having adjusted one or more properties of an existing instance of model and
|
|
153
|
+
* `deleted` on notifications about having removed an existing instance of model.
|
|
154
|
+
|
|
155
|
+
In last case the provided set of properties is limited to the instance's UUID. The common pattern on client-side for listening to server-side notifications looks like this:
|
|
156
|
+
|
|
157
|
+
```javascript
|
|
158
|
+
const socket = io( "/hitchy/odem" );
|
|
159
|
+
|
|
160
|
+
socket.on( "<modelName>:changed", info => {
|
|
161
|
+
// type of change is in `info.change`, e.g. "updated"
|
|
162
|
+
// process properties of changed instance in `info.properties`
|
|
163
|
+
} );
|
|
164
|
+
```
|
|
165
|
+
|
|
166
|
+
> There may be server-side restriction applying per event or per instance of model, thus some or all notifications might be omitted.
|