Skip to content

Commit d5dc94d

Browse files
committed
feat(plugin-js-microlib): consistent read
1 parent 232f944 commit d5dc94d

File tree

5 files changed

+62
-3
lines changed

5 files changed

+62
-3
lines changed

packages/plugin-js-lambda-microlib/src/MicroserviceType.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -478,6 +478,7 @@ export default class MicroserviceType {
478478
const conditionCode = this.buildConditionCode(condition, conditionNot, requirements);
479479
let call: string|undefined = undefined;
480480
if ('@get' === type) return ` ${conditionCode || ''}Object.assign(query, await service.get(query.id, ${this.stringifyForHook(config['fields'] || [], options)}));`;
481+
if ('@get-consistent' === type) return ` ${conditionCode || ''}Object.assign(query, await service.getConsistent(query.id, ${this.stringifyForHook(config['fields'] || [], options)}));`;
481482
const rawOpts = !!Object.keys(opts).length ? `, ${this.stringifyForHook(opts, options)}` : '';
482483
if ('@lambda/event' === type) {
483484
requirements['lambdaEvent'] = true;
@@ -776,4 +777,4 @@ export default class MicroserviceType {
776777
});
777778
return x ? authorizations[x] : undefined;
778779
}
779-
}
780+
}

packages/plugin-js-lambda-microlib/src/MicroserviceTypeOperation.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,12 @@ export default class MicroserviceTypeOperation {
9898
this.hasHooks('dynamics', opType, microserviceType, name) && microserviceType.registerHook(name, 'postpopulate', {type: '@dynamics', config: {}});
9999
this.hasHooks('requires', opType, microserviceType, name) && microserviceType.registerHook(name, 'init', {type: '@requires', config: {}});
100100
break;
101+
case 'getConsistent':
102+
this.hasHooks('convert', opType, microserviceType, name) && microserviceType.registerHook(name, 'convert', {type: '@convert', config: {}});
103+
this.hasHooks('prefetch-read', opType, microserviceType, name) && microserviceType.registerHook(name, 'init', {type: '@prefetch', config: {mode: 'requires-only'}});
104+
this.hasHooks('dynamics', opType, microserviceType, name) && microserviceType.registerHook(name, 'postpopulate', {type: '@dynamics', config: {}});
105+
this.hasHooks('requires', opType, microserviceType, name) && microserviceType.registerHook(name, 'init', {type: '@requires', config: {}});
106+
break;
101107
case 'find':
102108
this.hasHooks('convert', opType, microserviceType, name) && microserviceType.registerHook(name, 'convert', {type: '@convert', config: {mode: 'page'}});
103109
this.hasHooks('dynamics', opType, microserviceType, name) && microserviceType.registerHook(name, 'postpopulate', {type: '@dynamics', config: {mode: 'page'}});
@@ -179,4 +185,4 @@ export default class MicroserviceTypeOperation {
179185
async generate(vars: any = {}): Promise<{[key: string]: Function}> {
180186
return this.handler ? this.handler.generate(vars) : Promise.resolve({});
181187
}
182-
}
188+
}

packages/plugin-js-lambda-microlib/src/SchemaGraphqlModel.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,14 @@ export class SchemaGraphqlModel {
229229
],
230230
gqlType: op.gqlType,
231231
}; break;
232+
case 'getConsistent': x = {
233+
name,
234+
type: 'getConsistent',
235+
args: [
236+
{name: 'id', gqlType: 'ID', required: true}
237+
],
238+
gqlType: op.gqlType,
239+
}; break;
232240
default:
233241
let z: any = {};
234242
if (others[name]) z = this.parseHandlerValue(others[name], {parentType: {name: 'Query'}, parentField: name, handler: name});
@@ -414,6 +422,11 @@ export class SchemaGraphqlModel {
414422
return {operationNature, operationType: pageType, operationGqlType: pageType, operationArgs: [
415423
{name: cfg?.vars?.default || cfg?.vars?.field || 'value', type: 'string', gqlType: 'String', required: true},
416424
]};
425+
case 'getConsistentBy':
426+
pageType = this.camelCase(msType);
427+
return {operationNature, operationType: pageType, operationGqlType: pageType, operationArgs: [
428+
{name: cfg?.vars?.default || cfg?.vars?.field || 'value', type: 'string', gqlType: 'String', required: true},
429+
]};
417430
case 'findInIndexByHashKey':
418431
pageType = `${this.camelCase(msType)}Page`;
419432
return {operationNature, operationType: pageType, operationGqlType: pageType, operationArgs: [
@@ -527,6 +540,12 @@ export class SchemaGraphqlModel {
527540
{name: cfg?.vars?.default || cfg?.vars?.field || 'value', type: 'string', gqlType: 'String', required: true},
528541
]};
529542
}
543+
if (/^getConsistentBy/.test(operationNature || '')) {
544+
pageType = this.camelCase(msType);
545+
return {operationNature, operationType: pageType, operationGqlType: pageType, operationArgs: [
546+
{name: cfg?.vars?.default || cfg?.vars?.field || 'value', type: 'string', gqlType: 'String', required: true},
547+
]};
548+
}
530549
if (origOp?.rawOutputType) {
531550
const ogt = origOp.rawOutputType.replace('{{gqlType}}', this.camelCase(msType));
532551
return {...(origOp?.rawOutputType ? {operationGqlType: ogt, operationType: ogt} : {})};
@@ -702,4 +721,4 @@ export class SchemaGraphqlModel {
702721
}
703722

704723
// noinspection JSUnusedGlobalSymbols
705-
export default SchemaGraphqlConfig
724+
export default SchemaGraphqlConfig
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
async: true
2+
3+
require:
4+
- find
5+
6+
inputs:
7+
type:
8+
main: true
9+
10+
args:
11+
- field
12+
- value
13+
- fields = []
14+
- query
15+
16+
code: |
17+
if (undefined === value || null === value || '' === value) throw new (require('@ohoareau/errors/lib/DocumentNotFoundError').default)('<%- type %>', '*empty*', field);
18+
const docs = await service.find({...query, consistent: true, index: field, hashKey: value, limit: 1, fields, selections: {items: {fields, selections: query.selections}}});
19+
if (!docs || !docs.items || (1 > docs.items.length)) throw new (require('@ohoareau/errors/lib/DocumentNotFoundError').default)('<%- type %>', value, field);
20+
return docs.items.shift();
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
require:
2+
- getConsistentBy
3+
4+
inputs:
5+
field:
6+
main: true
7+
8+
wrap:
9+
- getConsistentBy
10+
- "'<%- field %>'"
11+
- query.<%- field %>
12+
- query.fields || []
13+
- query

0 commit comments

Comments
 (0)