11import type { ClientContext , NextlyticsEvent , ServerEventContext } from "../../types" ;
22
33export const tableColumns = [
4- { name : "event_id " , pgType : "TEXT PRIMARY KEY " , chType : "String " } ,
5- { name : "parent_event_id " , pgType : "TEXT" , chType : "Nullable (String)" } ,
6- { name : "timestamp " , pgType : "TIMESTAMPTZ " , chType : "DateTime64(3 )" } ,
7- { name : "type " , pgType : "TEXT" , chType : "LowCardinality( String) " } ,
8- { name : "anonymous_user_id " , pgType : "TEXT" , chType : "Nullable (String)" } ,
4+ { name : "timestamp " , pgType : "TIMESTAMPTZ NOT NULL " , chType : "DateTime64(3) " } ,
5+ { name : "type " , pgType : "TEXT NOT NULL " , chType : "LowCardinality (String)" } ,
6+ { name : "host " , pgType : "TEXT " , chType : "LowCardinality(String )" } ,
7+ { name : "path " , pgType : "TEXT" , chType : "String" } ,
8+ { name : "method " , pgType : "TEXT" , chType : "LowCardinality (String)" } ,
99 { name : "user_id" , pgType : "TEXT" , chType : "Nullable(String)" } ,
10+ { name : "anonymous_user_id" , pgType : "TEXT" , chType : "Nullable(String)" } ,
1011 { name : "user_email" , pgType : "TEXT" , chType : "Nullable(String)" } ,
1112 { name : "user_name" , pgType : "TEXT" , chType : "Nullable(String)" } ,
12- { name : "host" , pgType : "TEXT" , chType : "LowCardinality(String)" } ,
13- { name : "method" , pgType : "TEXT" , chType : "LowCardinality(String)" } ,
14- { name : "path" , pgType : "TEXT" , chType : "String" } ,
1513 { name : "ip" , pgType : "INET" , chType : "Nullable(IPv6)" } ,
1614 { name : "referer" , pgType : "TEXT" , chType : "Nullable(String)" } ,
1715 { name : "user_agent" , pgType : "TEXT" , chType : "Nullable(String)" } ,
1816 { name : "locale" , pgType : "TEXT" , chType : "LowCardinality(Nullable(String))" } ,
17+ { name : "event_id" , pgType : "TEXT PRIMARY KEY" , chType : "String" } ,
18+ { name : "parent_event_id" , pgType : "TEXT" , chType : "Nullable(String)" } ,
1919 { name : "server_context" , pgType : "JSONB" , chType : "JSON" } ,
2020 { name : "client_context" , pgType : "JSONB" , chType : "JSON" } ,
2121 { name : "user_traits" , pgType : "JSONB" , chType : "JSON" } ,
@@ -70,21 +70,21 @@ function extractCommonFields(event: NextlyticsEvent) {
7070 : null ;
7171
7272 return {
73- event_id : event . eventId ,
74- parent_event_id : event . parentEventId ?? null ,
7573 timestamp : event . collectedAt ,
7674 type : event . type ,
77- anonymous_user_id : event . anonymousUserId ?? null ,
75+ host,
76+ path,
77+ method,
7878 user_id : event . userContext ?. userId ?? null ,
79+ anonymous_user_id : event . anonymousUserId ?? null ,
7980 user_email : event . userContext ?. traits ?. email ?? null ,
8081 user_name : event . userContext ?. traits ?. name ?? null ,
81- host,
82- method,
83- path,
8482 ip : ip || null ,
8583 referer : clientCtx . referer ?? null ,
8684 user_agent : clientCtx . user_agent ?? null ,
8785 locale : clientCtx . locale ?? null ,
86+ event_id : event . eventId ,
87+ parent_event_id : event . parentEventId ?? null ,
8888 serverContextRest,
8989 clientContextRest : clientCtx . rest ,
9090 userTraitsRest,
@@ -119,9 +119,9 @@ export function eventToJsonRow(event: NextlyticsEvent): Record<ColumnName, unkno
119119
120120// Postgres
121121export function generatePgCreateTableSQL ( tableName : string ) : string {
122- const pk = tableColumns [ 0 ] ;
122+ const pk = tableColumns . find ( ( c ) => c . pgType . includes ( "PRIMARY KEY" ) ) ! ;
123123 const alters = tableColumns
124- . slice ( 1 )
124+ . filter ( ( c ) => c !== pk )
125125 . map ( ( col ) => `ALTER TABLE ${ tableName } ADD COLUMN IF NOT EXISTS ${ col . name } ${ col . pgType } ;` )
126126 . join ( "\n" ) ;
127127
@@ -142,7 +142,7 @@ export function generateChCreateTableSQL(database: string, tableName: string): s
142142 . join ( ", " ) ;
143143 const create =
144144 `CREATE TABLE IF NOT EXISTS ${ fullTable } (${ createCols } ) ` +
145- `ENGINE = ReplacingMergeTree() PARTITION BY toYYYYMM(timestamp) ORDER BY event_id;` ;
145+ `ENGINE = ReplacingMergeTree() PARTITION BY toYYYYMM(timestamp) ORDER BY (timestamp, event_id) ;` ;
146146
147147 const alters = tableColumns
148148 . filter ( ( c ) => c . name !== "event_id" && c . name !== "timestamp" )
@@ -158,21 +158,21 @@ export function isChTableNotFoundError(text: string): boolean {
158158
159159/** Row type returned from analytics table queries */
160160export interface AnalyticsEventRow {
161- event_id : string ;
162- parent_event_id : string | null ;
163161 timestamp : string ;
164162 type : string ;
165- anonymous_user_id : string | null ;
163+ host : string ;
164+ path : string ;
165+ method : string ;
166166 user_id : string | null ;
167+ anonymous_user_id : string | null ;
167168 user_email : string | null ;
168169 user_name : string | null ;
169- host : string ;
170- method : string ;
171- path : string ;
172170 ip : string | null ;
173171 referer : string | null ;
174172 user_agent : string | null ;
175173 locale : string | null ;
174+ event_id : string ;
175+ parent_event_id : string | null ;
176176 server_context : Record < string , unknown > ;
177177 client_context : Record < string , unknown > ;
178178 user_traits : Record < string , unknown > ;
0 commit comments