@genesislcap/mock-server 15.51.1 → 15.53.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.
Files changed (70) hide show
  1. package/README.md +408 -39
  2. package/dist/db/criteria.d.ts +13 -0
  3. package/dist/db/criteria.js +60 -21
  4. package/dist/db/identity.d.ts +11 -0
  5. package/dist/db/identity.js +46 -0
  6. package/dist/db/ordering.d.ts +8 -0
  7. package/dist/db/ordering.js +49 -0
  8. package/dist/db/schema.d.ts +62 -0
  9. package/dist/db/schema.js +490 -0
  10. package/dist/db/store.d.ts +20 -3
  11. package/dist/db/store.js +300 -50
  12. package/dist/handlers/commitEvent.js +47 -9
  13. package/dist/handlers/crudEvents.d.ts +2 -0
  14. package/dist/handlers/crudEvents.js +138 -0
  15. package/dist/handlers/dataLogon.js +105 -6
  16. package/dist/handlers/eventValidation.d.ts +5 -0
  17. package/dist/handlers/eventValidation.js +29 -0
  18. package/dist/handlers/jsonSchema.d.ts +4 -2
  19. package/dist/handlers/jsonSchema.js +222 -61
  20. package/dist/handlers/meta.d.ts +6 -1
  21. package/dist/handlers/meta.js +168 -21
  22. package/dist/handlers/requestReply.js +519 -30
  23. package/dist/handlers/resourceAuth.d.ts +1 -0
  24. package/dist/handlers/resourceAuth.js +14 -0
  25. package/dist/handlers/resources.js +3 -0
  26. package/dist/index.d.ts +3 -2
  27. package/dist/index.js +2 -1
  28. package/dist/protocol/connection.d.ts +2 -0
  29. package/dist/protocol/errors.d.ts +100 -2
  30. package/dist/protocol/errors.js +300 -12
  31. package/dist/protocol/fieldTypes.d.ts +10 -0
  32. package/dist/protocol/fieldTypes.js +107 -0
  33. package/dist/protocol/gsfSchemas.d.ts +9 -0
  34. package/dist/protocol/gsfSchemas.js +423 -0
  35. package/dist/protocol/messageTypes.d.ts +1 -0
  36. package/dist/protocol/messageTypes.js +1 -0
  37. package/dist/protocol/msgNack.d.ts +2 -0
  38. package/dist/protocol/msgNack.js +10 -0
  39. package/dist/protocol/rowUpdate.d.ts +5 -3
  40. package/dist/protocol/rowUpdate.js +33 -15
  41. package/dist/protocol/schemaValidation.d.ts +11 -0
  42. package/dist/protocol/schemaValidation.js +186 -0
  43. package/dist/server.js +95 -29
  44. package/dist/types.d.ts +45 -0
  45. package/package.json +1 -1
  46. package/src/db/criteria.ts +72 -21
  47. package/src/db/identity.ts +53 -0
  48. package/src/db/ordering.ts +56 -0
  49. package/src/db/schema.ts +672 -0
  50. package/src/db/store.ts +343 -50
  51. package/src/handlers/commitEvent.ts +54 -10
  52. package/src/handlers/crudEvents.ts +181 -0
  53. package/src/handlers/dataLogon.ts +127 -7
  54. package/src/handlers/eventValidation.ts +43 -0
  55. package/src/handlers/jsonSchema.ts +302 -69
  56. package/src/handlers/meta.ts +227 -23
  57. package/src/handlers/requestReply.ts +626 -33
  58. package/src/handlers/resourceAuth.ts +18 -0
  59. package/src/handlers/resources.ts +3 -0
  60. package/src/index.ts +17 -1
  61. package/src/protocol/connection.ts +10 -2
  62. package/src/protocol/errors.ts +361 -13
  63. package/src/protocol/fieldTypes.ts +115 -0
  64. package/src/protocol/gsfSchemas.ts +505 -0
  65. package/src/protocol/messageTypes.ts +1 -0
  66. package/src/protocol/msgNack.ts +12 -0
  67. package/src/protocol/rowUpdate.ts +40 -20
  68. package/src/protocol/schemaValidation.ts +208 -0
  69. package/src/server.ts +106 -28
  70. package/src/types.ts +187 -5
@@ -1,26 +1,296 @@
1
- import { deriveFields } from '../db/metadata.ts';
1
+ import {
2
+ eventDefaultOf,
3
+ eventFields,
4
+ inferEventTable,
5
+ knownValues,
6
+ isDeleteEventName,
7
+ querySchema,
8
+ requestFieldsOf,
9
+ requestReplyReplySchema,
10
+ requestReplySourceSchema,
11
+ resolveEventTarget,
12
+ resolveSourceSchema,
13
+ withTypeOverrides,
14
+ type EventTarget,
15
+ type ResolvedField,
16
+ } from '../db/schema.ts';
2
17
  import type { Store } from '../db/store.ts';
3
18
  import type { Connection } from '../protocol/connection.ts';
4
19
  import { isLegacyFidelity } from '../protocol/fidelity.ts';
20
+ import {
21
+ BIGDECIMAL_PATTERN,
22
+ enumClassOf,
23
+ jsonTypeOf,
24
+ jvmClassOf,
25
+ rangeOf,
26
+ titleCase,
27
+ } from '../protocol/fieldTypes.ts';
28
+ import {
29
+ dataLogonReplySchema,
30
+ dataLogonSchema,
31
+ eventInboundProperties,
32
+ eventReplySchema,
33
+ GENESIS_META_SCHEMA,
34
+ REQUEST_DETAILS_SCHEMA,
35
+ } from '../protocol/gsfSchemas.ts';
5
36
  import { ERROR_CODE, MESSAGE_TYPE } from '../protocol/messageTypes.ts';
6
37
  import { resourceNotMappedNack } from '../protocol/msgNack.ts';
7
- import type { FieldMetadata, GenesisMessage, MockServerConfig, Row } from '../types.ts';
38
+ import type {
39
+ FieldMetadata,
40
+ GenesisMessage,
41
+ MockServerConfig,
42
+ QueryDef,
43
+ RequestReplyDef,
44
+ Row,
45
+ } from '../types.ts';
46
+
47
+ // Kept here for existing importers; the resolver lives in db/schema.ts.
48
+ export { inferEventTable };
8
49
 
9
50
  // JSON_SCHEMA_REQUEST shares its request envelope with META_REQUEST (same
10
51
  // DETAILS.FEATURE resource-name field) but its reply is a completely
11
52
  // different shape: Connect.getJSONSchema doesn't read response.DETAILS at
12
53
  // all — the response *is* the SchemaResponse ({ INBOUND, OUTBOUND }, each a
13
- // draft-07 JSON Schema). @genesislcap/foundation-entity-management's
14
- // <entity-management> calls this for every create/update form (fields come
15
- // from INBOUND.properties.DETAILS.properties) and for delete confirmation
16
- // (it reads Object.keys(...properties) to know which fields to copy from the
17
- // selected row into the commit DETAILS). Route this to handleMetaRequest by
18
- // mistake and forms silently render with zero fields, and delete can never
19
- // build a payload — no error, just a dead button. The real success reply is
20
- // JSON_SCHEMA_FEATURE_ACK with INBOUND/OUTBOUND at the top level (legacy mode
21
- // echoed JSON_SCHEMA_REQUEST).
22
-
23
- function jsonSchemaType(fieldType: string): string {
54
+ // JSON Schema). Who reads what:
55
+ // - foundation-forms' <foundation-form> (and so entity-management's create
56
+ // and update forms) renders INBOUND.properties.DETAILS of an EVENT: its
57
+ // properties, required, enum, default, title, and genesisType / the
58
+ // org.joda.time.DateTime description for date pickers.
59
+ // - entity-management's delete copies Object.keys(INBOUND...DETAILS
60
+ // .properties) from the selected row into the commit DETAILS.
61
+ // - foundation-forms' <foundation-filters> renders a QUERY's or REQUEST
62
+ // SERVER's rows: OUTBOUND.properties.REPLY.items, else
63
+ // OUTBOUND.$defs[...QueryRow].
64
+ // Route this to handleMetaRequest by mistake and forms silently render with
65
+ // zero fields, and delete can never build a payload — no error, just a dead
66
+ // button.
67
+
68
+ // --- events -----------------------------------------------------------------
69
+
70
+ // One DETAILS property of an event's INBOUND schema. Nullable fields are
71
+ // oneOf [schema, null]; BigDecimal is a string with a pattern and no
72
+ // genesisType; dates are integers (epoch millis) described as DateTime. Only
73
+ // a table event's DAO class has titles, lengths, ranges and genesisTypes
74
+ // (see meta.ts eventMetaField).
75
+ function eventProperty(field: ResolvedField, annotated: boolean, appName?: string): Row {
76
+ const type = field.type;
77
+ const property: Row = {};
78
+ if (annotated && type === 'STRING') {
79
+ if (field.minLength !== undefined) property.minLength = field.minLength;
80
+ if (field.maxSize !== undefined) property.maxLength = field.maxSize;
81
+ }
82
+ if (type === 'BIGDECIMAL') property.pattern = BIGDECIMAL_PATTERN;
83
+ const range = annotated ? rangeOf(type) : undefined;
84
+ if (range) [property.minimum, property.maximum] = range;
85
+ property.readOnly = false;
86
+ if (type === 'ENUM' && knownValues(field)) property.enum = knownValues(field);
87
+ property.description =
88
+ field.description ?? jvmClassOf(type, () => enumClassOf(field.table, field.name, appName));
89
+ const fieldDefault = eventDefaultOf(field);
90
+ if (fieldDefault !== undefined) property.default = fieldDefault;
91
+ if (annotated) property.title = field.title ?? titleCase(field.name);
92
+ if (annotated && type !== 'BIGDECIMAL') property.genesisType = type;
93
+ property.type = jsonTypeOf(type);
94
+ return field.nullable ? { oneOf: [property, { type: 'null' }] } : property;
95
+ }
96
+
97
+ // required: the fields that aren't optional (JsonSchema.kt:478), as
98
+ // eventFields resolved them. A sequence key is optional: the database fills
99
+ // it in; a nullable DTO field with no default is still required.
100
+ function isRequired(field: ResolvedField): boolean {
101
+ return !field.optional;
102
+ }
103
+
104
+ // An event's INBOUND schema: the message, with DETAILS as the event's class.
105
+ // It is also what an inbound event is validated against
106
+ // (handlers/eventValidation.ts), as on the real server.
107
+ export function eventInboundSchema(
108
+ eventName: string,
109
+ target: EventTarget,
110
+ fields: ResolvedField[],
111
+ appName: string | undefined,
112
+ ): Row {
113
+ const annotated = target.kind === 'table';
114
+ const details = {
115
+ properties: Object.fromEntries(
116
+ fields.map((field) => [field.name, eventProperty(field, annotated, appName)]),
117
+ ),
118
+ required: fields.filter(isRequired).map((field) => field.name),
119
+ additionalProperties: false,
120
+ type: 'object',
121
+ };
122
+ return {
123
+ properties: eventInboundProperties(eventName, details),
124
+ required: ['MESSAGE_TYPE', 'SOURCE_REF', 'USER_NAME', 'DETAILS'],
125
+ additionalProperties: true,
126
+ description: target.className,
127
+ ...(target.title ? { title: target.title } : {}),
128
+ $defs: {},
129
+ $schema: GENESIS_META_SCHEMA,
130
+ type: 'object',
131
+ };
132
+ }
133
+
134
+ function eventSchema(
135
+ eventName: string,
136
+ target: EventTarget,
137
+ fields: ResolvedField[],
138
+ appName: string | undefined,
139
+ ): { INBOUND: Row; OUTBOUND: Row } {
140
+ return {
141
+ INBOUND: eventInboundSchema(eventName, target, fields, appName),
142
+ OUTBOUND: eventReplySchema(),
143
+ };
144
+ }
145
+
146
+ // --- dataservers and request servers ---------------------------------------
147
+
148
+ function typeSchema(field: ResolvedField): Row {
149
+ return {
150
+ type: jsonTypeOf(field.type),
151
+ ...(field.type === 'ENUM' && knownValues(field) ? { enum: knownValues(field) } : {}),
152
+ };
153
+ }
154
+
155
+ // A row property in a reply schema: nullable as oneOf [null, schema], with
156
+ // an empty description and the Genesis type (BigDecimal included).
157
+ function rowProperty(field: ResolvedField, nullable: boolean): Row {
158
+ return nullable
159
+ ? { oneOf: [{ type: 'null' }, typeSchema(field)], description: '', genesisType: field.type }
160
+ : { ...typeSchema(field), description: '', genesisType: field.type };
161
+ }
162
+
163
+ // Every QueryRow property is nullable: a live MODIFY carries only the fields
164
+ // that changed.
165
+ function dataserverSchema(
166
+ queryName: string,
167
+ query: QueryDef,
168
+ config: MockServerConfig,
169
+ store: Store,
170
+ ): { INBOUND: Row; OUTBOUND: Row } {
171
+ const fields = querySchema(query, config, store).fields;
172
+ return {
173
+ INBOUND: dataLogonSchema(queryName),
174
+ OUTBOUND: dataLogonReplySchema(
175
+ Object.fromEntries(fields.map((field) => [field.name, rowProperty(field, true)])),
176
+ ),
177
+ };
178
+ }
179
+
180
+ function requestServerSchema(
181
+ name: string,
182
+ requestReply: RequestReplyDef,
183
+ config: MockServerConfig,
184
+ store: Store,
185
+ ): { INBOUND: Row; OUTBOUND: Row } {
186
+ const entityFields = requestReplySourceSchema(requestReply, config, store).fields;
187
+ const requestProperties = Object.fromEntries(
188
+ requestFieldsOf(requestReply, store).map((fieldName) => {
189
+ const field = entityFields.find((candidate) => candidate.name === fieldName);
190
+ const type = field?.type ?? 'STRING';
191
+ return [
192
+ fieldName,
193
+ {
194
+ readOnly: false,
195
+ ...(type === 'ENUM' && field && knownValues(field) ? { enum: knownValues(field) } : {}),
196
+ description: '',
197
+ genesisType: type,
198
+ type: jsonTypeOf(type),
199
+ },
200
+ ];
201
+ }),
202
+ );
203
+ const request = { properties: requestProperties, additionalProperties: false, type: 'object' };
204
+ // The reply block, else the source's fields then RECORD_ID and TIMESTAMP,
205
+ // as REPLY_FIELD (see requestReplyReplySchema).
206
+ const fields = requestReplyReplySchema(requestReply, config, store).fields;
207
+ return {
208
+ INBOUND: {
209
+ properties: {
210
+ MESSAGE_TYPE: { const: `REQ_${name}`, description: 'Message type', type: 'string' },
211
+ SOURCE_REF: { description: 'Source reference', type: 'string' },
212
+ USER_NAME: { description: 'User name', type: 'string' },
213
+ REQUEST: { oneOf: [request, { items: request, type: 'array' }] },
214
+ DETAILS: REQUEST_DETAILS_SCHEMA,
215
+ },
216
+ required: ['MESSAGE_TYPE', 'SOURCE_REF', 'USER_NAME', 'REQUEST'],
217
+ additionalProperties: false,
218
+ description: `GPAL Request definition for ${name}`,
219
+ $defs: {},
220
+ type: 'object',
221
+ },
222
+ OUTBOUND: {
223
+ description: `GPAL Reply definition for ${name}`,
224
+ type: 'object',
225
+ properties: {
226
+ MESSAGE_TYPE: { const: `REP_${name}` },
227
+ SOURCE_REF: { type: 'string' },
228
+ REPLY: {
229
+ type: 'array',
230
+ items: {
231
+ type: 'object',
232
+ properties: Object.fromEntries(
233
+ fields.map((field) => [field.name, rowProperty(field, field.nullable)]),
234
+ ),
235
+ additionalProperties: false,
236
+ required: [],
237
+ },
238
+ },
239
+ NEXT_VIEW: { type: 'number' },
240
+ },
241
+ required: ['MESSAGE_TYPE', 'SOURCE_REF'],
242
+ additionalProperties: false,
243
+ },
244
+ };
245
+ }
246
+
247
+ // The FEATURE resolves, in order, as: an entry in config.eventSchemas, a
248
+ // dataserver query, a request server (by resource name, without REQ_), an
249
+ // EVENT_<TABLE>_<VERB> event, and finally a bare table name — the last is
250
+ // this engine's own fallback for clients that ask by table (the real server
251
+ // answers 404 there). Anything else gets the router's 404.
252
+ export function handleJsonSchemaRequest(
253
+ message: GenesisMessage,
254
+ connection: Connection,
255
+ config: MockServerConfig,
256
+ store: Store,
257
+ ): void {
258
+ const feature: string | undefined = message.DETAILS?.FEATURE;
259
+ if (isLegacyFidelity(config)) {
260
+ handleLegacyJsonSchemaRequest(message, connection, config, store, feature);
261
+ return;
262
+ }
263
+
264
+ const overridden = feature !== undefined && !!config.eventSchemas?.[feature];
265
+ const query = !overridden && feature ? config.queries?.[feature] : undefined;
266
+ const requestReply = !overridden && feature ? config.requestReplies?.[feature] : undefined;
267
+ let schema: { INBOUND: Row; OUTBOUND: Row } | undefined;
268
+ if (query) {
269
+ schema = dataserverSchema(feature!, query, config, store);
270
+ } else if (requestReply) {
271
+ schema = requestServerSchema(feature!, requestReply, config, store);
272
+ } else {
273
+ const event = resolveEventTarget(feature, config, { bareTable: true });
274
+ if (event) {
275
+ schema = eventSchema(feature!, event, eventFields(event, config, store), config.appName);
276
+ }
277
+ }
278
+
279
+ if (!schema) {
280
+ connection.reply(message, resourceNotMappedNack(feature));
281
+ return;
282
+ }
283
+ connection.reply(message, { MESSAGE_TYPE: MESSAGE_TYPE.JSON_SCHEMA_FEATURE_ACK, ...schema });
284
+ }
285
+
286
+ // --- fidelity: 'legacy' -----------------------------------------------------
287
+ // Up to 15.47 every FEATURE resolved to a table (or view) and got the same
288
+ // minimal schema as INBOUND and OUTBOUND: {type, properties: {DETAILS:
289
+ // {type, properties: {<field>: {type}}, required}}}, `required` being the key
290
+ // on deletes and empty otherwise. A query or request-server name resolved
291
+ // through its `source`. The reply echoed JSON_SCHEMA_REQUEST.
292
+
293
+ function legacyJsonSchemaType(fieldType: string): string {
24
294
  switch (fieldType) {
25
295
  case 'LONG':
26
296
  return 'integer';
@@ -33,10 +303,10 @@ function jsonSchemaType(fieldType: string): string {
33
303
  }
34
304
  }
35
305
 
36
- function buildSchema(fields: FieldMetadata[], required: string[]): Row {
306
+ function legacySchema(fields: FieldMetadata[], required: string[]): Row {
37
307
  const properties: Row = {};
38
308
  for (const field of fields) {
39
- properties[field.NAME] = { type: jsonSchemaType(field.TYPE) };
309
+ properties[field.NAME] = { type: legacyJsonSchemaType(field.TYPE) };
40
310
  }
41
311
  return {
42
312
  type: 'object',
@@ -46,76 +316,39 @@ function buildSchema(fields: FieldMetadata[], required: string[]): Row {
46
316
  };
47
317
  }
48
318
 
49
- const DELETE_EVENT_RE = /_DELETE(_|$)/;
50
-
51
- // Zero-config default: EVENT_<TABLE>_<VERB...> maps to <TABLE> if it's a
52
- // registered table name (longest match wins, so e.g. a hypothetical
53
- // COUNTERPARTY_AUTH table can't shadow COUNTERPARTY). Covers every event
54
- // naming pattern in this codebase (INSERT, MODIFY, DELETE, plus suffixed
55
- // variants like INSERT_WITH_APPROVAL, DEMO_INSERT, MODIFY_PRICE, INSERT_FAIL)
56
- // without hand-maintaining a table per event. Override via
57
- // config.eventSchemas for names that don't follow this convention.
58
- // Exported for unit tests only.
59
- export function inferEventTable(
60
- eventName: string | undefined,
61
- tableNames: string[],
62
- ): string | undefined {
63
- if (!eventName) return undefined;
64
- // Not every caller asks by event name: entity-management resolves a grid's
65
- // schema from the bare resource/table name (FEATURE: 'TRADE'), so accept an
66
- // exact table match before requiring the EVENT_ prefix. Without this the
67
- // request errors with UNKNOWN_RESOURCE and the form renders no fields.
68
- if (tableNames.includes(eventName)) return eventName;
69
- if (!eventName.startsWith('EVENT_')) return undefined;
70
- const rest = eventName.slice('EVENT_'.length);
71
- const candidates = tableNames.filter((name) => rest === name || rest.startsWith(`${name}_`));
72
- candidates.sort((a, b) => b.length - a.length);
73
- return candidates[0];
74
- }
75
-
76
- export function handleJsonSchemaRequest(
319
+ function handleLegacyJsonSchemaRequest(
77
320
  message: GenesisMessage,
78
321
  connection: Connection,
79
322
  config: MockServerConfig,
80
323
  store: Store,
324
+ feature: string | undefined,
81
325
  ): void {
82
- const eventName: string | undefined = message.DETAILS?.FEATURE;
83
- const override = eventName ? config.eventSchemas?.[eventName] : undefined;
84
- // A DATASERVER/REQUEST_SERVER resource name (e.g. 'ALL_USERS') resolves
85
- // through its own definition to the backing table/view. The FAST-Element
86
- // showcase's entity-management grids ask by query name, not by table or
87
- // event name, so without this their create/update forms render empty.
88
- const resourceSource = eventName
89
- ? (config.queries?.[eventName]?.source ?? config.requestReplies?.[eventName]?.source)
326
+ const override = feature ? config.eventSchemas?.[feature] : undefined;
327
+ const resourceSource = feature
328
+ ? (config.queries?.[feature]?.source ?? config.requestReplies?.[feature]?.source)
90
329
  : undefined;
91
330
  const table =
92
- override?.table ??
93
- resourceSource ??
94
- inferEventTable(eventName, Object.keys(config.tables ?? {}));
331
+ override?.table ?? resourceSource ?? inferEventTable(feature, Object.keys(config.tables ?? {}));
95
332
 
96
- const legacy = isLegacyFidelity(config);
97
333
  if (!table) {
98
- connection.reply(
99
- message,
100
- legacy
101
- ? {
102
- MESSAGE_TYPE: message.MESSAGE_TYPE,
103
- ERROR: [{ CODE: ERROR_CODE.UNKNOWN_RESOURCE, TEXT: `Unknown resource: ${eventName}` }],
104
- }
105
- : resourceNotMappedNack(eventName),
106
- );
334
+ connection.reply(message, {
335
+ MESSAGE_TYPE: message.MESSAGE_TYPE,
336
+ ERROR: [{ CODE: ERROR_CODE.UNKNOWN_RESOURCE, TEXT: `Unknown resource: ${feature}` }],
337
+ });
107
338
  return;
108
339
  }
109
340
 
110
- const pkOnly = override?.pkOnly ?? DELETE_EVENT_RE.test(eventName!);
341
+ const pkOnly = override?.pkOnly ?? isDeleteEventName(feature!);
111
342
  const pkFields = store.getPkFields(table);
112
- let fields = deriveFields(store.getMetadataRows(table), override?.fieldTypes);
343
+ let fields: FieldMetadata[] = withTypeOverrides(
344
+ resolveSourceSchema(table, config, store),
345
+ override?.fieldTypes,
346
+ ).fields.map((field) => ({ NAME: field.name, TYPE: field.type }));
113
347
  if (pkOnly) fields = fields.filter((field) => pkFields.includes(field.NAME));
114
348
 
115
- const schema = buildSchema(fields, pkOnly ? pkFields : []);
116
-
349
+ const schema = legacySchema(fields, pkOnly ? pkFields : []);
117
350
  connection.reply(message, {
118
- MESSAGE_TYPE: legacy ? message.MESSAGE_TYPE : MESSAGE_TYPE.JSON_SCHEMA_FEATURE_ACK,
351
+ MESSAGE_TYPE: message.MESSAGE_TYPE,
119
352
  INBOUND: schema,
120
353
  OUTBOUND: schema,
121
354
  });