Skip to content

Commit ae61ea3

Browse files
authored
Add functions to create single incremental/delete reader (#406)
1 parent e5ffd97 commit ae61ea3

File tree

4 files changed

+147
-2
lines changed

4 files changed

+147
-2
lines changed

src/graphql/graphql.ts

Lines changed: 70 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -943,8 +943,8 @@ interface IncrementalReadersConfig {
943943
readonly graph: string;
944944
readonly pageSize: number;
945945
readonly graphSchema: gql.GraphQLSchema;
946-
readonly avoidCollisions: boolean;
947-
readonly scalarsOnly: boolean;
946+
readonly avoidCollisions?: boolean;
947+
readonly scalarsOnly?: boolean;
948948
}
949949

950950
export function createIncrementalReadersV2(
@@ -970,6 +970,74 @@ export function createIncrementalReadersV2(
970970
return result;
971971
}
972972

973+
export interface IncrementalReaderConfig {
974+
readonly model: string;
975+
readonly client: FarosClient;
976+
readonly graph: string;
977+
readonly graphSchema: gql.GraphQLSchema;
978+
readonly pageSize: number;
979+
readonly avoidCollisions: boolean;
980+
readonly scalarsOnly: boolean;
981+
}
982+
983+
export function createIncrementalReader(
984+
cfg: IncrementalReaderConfig
985+
): Reader | undefined {
986+
const type = cfg.graphSchema.getType(cfg.model);
987+
if (isV2ModelType(type)) {
988+
const avoidCollisions = cfg.avoidCollisions ?? true;
989+
const scalarsOnly = cfg.scalarsOnly ?? false;
990+
return readerFromQuery({
991+
client: cfg.client,
992+
graph: cfg.graph,
993+
graphSchema: cfg.graphSchema,
994+
pageSize: cfg.pageSize,
995+
incremental: true,
996+
query: buildIncrementalQueryV2({
997+
type,
998+
avoidCollisions,
999+
scalarsOnly,
1000+
}),
1001+
});
1002+
}
1003+
return undefined;
1004+
}
1005+
1006+
export interface DeleteReaderConfig {
1007+
readonly model: string;
1008+
readonly client: FarosClient;
1009+
readonly graph: string;
1010+
readonly graphSchema: gql.GraphQLSchema;
1011+
readonly pageSize: number;
1012+
}
1013+
1014+
export function createDeleteReader(
1015+
cfg: DeleteReaderConfig
1016+
): Reader | undefined {
1017+
const type = cfg.graphSchema.getType(cfg.model);
1018+
if (!isV2ModelType(type)) {
1019+
return undefined;
1020+
}
1021+
1022+
const deleteQuery = `
1023+
query delete($from: timestamptz!, $to: timestamptz!) {
1024+
${cfg.model}_history(where: {_and: [
1025+
{action: {_eq: "delete"}},
1026+
{actionAt: {_gte: $from, _lt: $to}}
1027+
]}) {
1028+
id
1029+
}
1030+
}`;
1031+
const [deleteReader] = createNonIncrementalReaders({
1032+
client: cfg.client,
1033+
graph: cfg.graph,
1034+
pageSize: cfg.pageSize,
1035+
graphSchema: cfg.graphSchema,
1036+
queries: [{gql: deleteQuery, name: cfg.model}],
1037+
});
1038+
return deleteReader;
1039+
}
1040+
9731041
interface IncrementalQueriesConfig {
9741042
readonly graphSchema: gql.GraphQLSchema;
9751043
readonly primaryKeys?: Dictionary<ReadonlyArray<string>>;

src/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,16 @@ export {
4646
} from './graphql/query-builder';
4747
export {
4848
AnyRecord,
49+
DeleteReaderConfig,
4950
FlattenContext,
51+
IncrementalReaderConfig,
5052
PaginatedQuery,
5153
Reader,
5254
RecordIterable,
5355
buildIncrementalQueryV2,
56+
createDeleteReader,
5457
createIncrementalQueriesV2,
58+
createIncrementalReader,
5559
createIncrementalReadersV2,
5660
createNonIncrementalReaders,
5761
crossMerge,

test/graphql.test.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import 'jest-extended';
22

33
import * as gql from 'graphql';
44

5+
import {FarosClient} from '../src/client';
56
import * as sut from '../src/graphql/graphql';
67
import {
78
graphSchemaV2,
@@ -442,4 +443,37 @@ describe('graphql', () => {
442443
path: ['cicd_Build'],
443444
});
444445
});
446+
447+
test('create incremental reader', () => {
448+
const reader = sut.createIncrementalReader({
449+
model: 'cicd_Build',
450+
client: {} as unknown as FarosClient,
451+
graph: 'graph',
452+
graphSchema: graphSchemaV2,
453+
pageSize: 1,
454+
avoidCollisions: false,
455+
scalarsOnly: true,
456+
});
457+
458+
expect(reader?.metadata).toMatchObject({
459+
name: 'cicd_Build',
460+
modelKeys: ['id'],
461+
incremental: true,
462+
});
463+
});
464+
465+
test('create delete reader', () => {
466+
const reader = sut.createDeleteReader({
467+
model: 'cicd_Build',
468+
client: {} as unknown as FarosClient,
469+
graph: 'graph',
470+
graphSchema: graphSchemaV2,
471+
pageSize: 1,
472+
});
473+
474+
expect(reader?.metadata).toMatchObject({
475+
name: 'cicd_Build',
476+
incremental: false,
477+
});
478+
});
445479
});

test/resources/schemas/schema-v2.gql

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,14 @@ type query_root {
5454
"""limit the number of rows returned"""
5555
limit: Int
5656
): [cicd_Artifact!]!
57+
58+
"""
59+
fetch data from the table: "cicd_Build_history"
60+
"""
61+
cicd_Build_history(
62+
"""limit the number of rows returned"""
63+
limit: Int
64+
): [cicd_Build_history!]!
5765
}
5866

5967
type ims_Incident {
@@ -286,3 +294,34 @@ type cicd_Artifact {
286294
uid: String!
287295
}
288296

297+
"""
298+
columns and relationships of "cicd_Build_history"
299+
"""
300+
type cicd_Build_history {
301+
action: String
302+
actionAt: timestamptz!
303+
actionBy: String
304+
agentId: String
305+
createdAt: timestamptz
306+
endedAt: timestamptz
307+
environmentVariables(
308+
"""JSON select path"""
309+
path: String
310+
): jsonb
311+
id: String!
312+
isPhantom: Boolean
313+
name: String
314+
number: Int
315+
origin: String
316+
pipelineId: String
317+
refreshedAt: timestamptz!
318+
startedAt: timestamptz
319+
status(
320+
"""JSON select path"""
321+
path: String
322+
): jsonb
323+
statusCategory: String
324+
statusDetail: String
325+
uid: String!
326+
url: String
327+
}

0 commit comments

Comments
 (0)