17
17
import * as sqlFormatter from "sql-formatter" ;
18
18
19
19
import * as logs from "../logs" ;
20
- import {
21
- firestoreToBQTable ,
22
- timestampField ,
23
- } from "./schema" ;
20
+ import { firestoreToBQTable , timestampField } from "./schema" ;
24
21
25
- const excludeFields : string [ ] = [
26
- "document_name" ,
27
- ] ;
22
+ const excludeFields : string [ ] = [ "document_name" ] ;
28
23
29
24
export const latestConsistentSnapshotView = (
30
25
datasetId : string ,
31
- tableName : string ,
26
+ tableName : string
32
27
) => ( {
33
- query : buildLatestSnapshotViewQuery ( datasetId , tableName , timestampField . name ,
28
+ query : buildLatestSnapshotViewQuery (
29
+ datasetId ,
30
+ tableName ,
31
+ timestampField . name ,
34
32
firestoreToBQTable ( )
35
- . map ( field => field . name )
36
- . filter ( name => excludeFields . indexOf ( name ) == - 1 ) ) ,
33
+ . map ( ( field ) => field . name )
34
+ . filter ( ( name ) => excludeFields . indexOf ( name ) == - 1 )
35
+ ) ,
37
36
useLegacySql : false ,
38
37
} ) ;
39
38
40
39
export function buildLatestSnapshotViewQuery (
41
40
datasetId : string ,
42
41
tableName : string ,
43
42
timestampColumnName : string ,
44
- groupByColumns : string [ ] ,
43
+ groupByColumns : string [ ]
45
44
) : string {
46
45
if ( datasetId === "" || tableName === "" || timestampColumnName === "" ) {
47
46
throw Error ( `Missing some query parameters!` ) ;
@@ -58,23 +57,29 @@ export function buildLatestSnapshotViewQuery(
58
57
-- event_id: The id of the event that triggered the cloud function mirrored the event.
59
58
-- data: A raw JSON payload of the current state of the document.
60
59
SELECT
61
- document_name${ groupByColumns . length > 0 ? `,` : `` }
60
+ document_name${ groupByColumns . length > 0 ? `,` : `` }
62
61
${ groupByColumns . join ( "," ) }
63
62
FROM (
64
63
SELECT
65
64
document_name,
66
- ${ groupByColumns . map ( columnName =>
67
- `FIRST_VALUE(${ columnName } )
65
+ ${ groupByColumns
66
+ . map (
67
+ ( columnName ) =>
68
+ `FIRST_VALUE(${ columnName } )
68
69
OVER(PARTITION BY document_name ORDER BY ${ timestampColumnName } DESC)
69
- AS ${ columnName } ` ) . join ( ',' ) } ${ groupByColumns . length > 0 ? `,` : `` }
70
+ AS ${ columnName } `
71
+ )
72
+ . join ( "," ) } ${ groupByColumns . length > 0 ? `,` : `` }
70
73
FIRST_VALUE(operation)
71
74
OVER(PARTITION BY document_name ORDER BY ${ timestampColumnName } DESC) = "DELETE"
72
75
AS is_deleted
73
76
FROM \`${ process . env . PROJECT_ID } .${ datasetId } .${ tableName } \`
74
77
ORDER BY document_name, ${ timestampColumnName } DESC
75
78
)
76
79
WHERE NOT is_deleted
77
- GROUP BY document_name${ groupByColumns . length > 0 ? `, ` : `` } ${ groupByColumns . join ( "," ) } `
80
+ GROUP BY document_name${
81
+ groupByColumns . length > 0 ? `, ` : ``
82
+ } ${ groupByColumns . join ( "," ) } `
78
83
) ;
79
84
return query ;
80
85
}
0 commit comments