@nextlytics/core 0.3.0-canary.80 → 0.3.0-canary.81

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.
@@ -3,49 +3,41 @@ import 'next/dist/server/web/spec-extension/cookies';
3
3
  import 'next/server';
4
4
 
5
5
  declare const tableColumns: readonly [{
6
- readonly name: "event_id";
7
- readonly pgType: "TEXT PRIMARY KEY";
8
- readonly chType: "String";
9
- }, {
10
- readonly name: "parent_event_id";
11
- readonly pgType: "TEXT";
12
- readonly chType: "Nullable(String)";
13
- }, {
14
6
  readonly name: "timestamp";
15
- readonly pgType: "TIMESTAMPTZ";
7
+ readonly pgType: "TIMESTAMPTZ NOT NULL";
16
8
  readonly chType: "DateTime64(3)";
17
9
  }, {
18
10
  readonly name: "type";
19
- readonly pgType: "TEXT";
11
+ readonly pgType: "TEXT NOT NULL";
20
12
  readonly chType: "LowCardinality(String)";
21
13
  }, {
22
- readonly name: "anonymous_user_id";
14
+ readonly name: "host";
23
15
  readonly pgType: "TEXT";
24
- readonly chType: "Nullable(String)";
16
+ readonly chType: "LowCardinality(String)";
25
17
  }, {
26
- readonly name: "user_id";
18
+ readonly name: "path";
27
19
  readonly pgType: "TEXT";
28
- readonly chType: "Nullable(String)";
20
+ readonly chType: "String";
29
21
  }, {
30
- readonly name: "user_email";
22
+ readonly name: "method";
31
23
  readonly pgType: "TEXT";
32
- readonly chType: "Nullable(String)";
24
+ readonly chType: "LowCardinality(String)";
33
25
  }, {
34
- readonly name: "user_name";
26
+ readonly name: "user_id";
35
27
  readonly pgType: "TEXT";
36
28
  readonly chType: "Nullable(String)";
37
29
  }, {
38
- readonly name: "host";
30
+ readonly name: "anonymous_user_id";
39
31
  readonly pgType: "TEXT";
40
- readonly chType: "LowCardinality(String)";
32
+ readonly chType: "Nullable(String)";
41
33
  }, {
42
- readonly name: "method";
34
+ readonly name: "user_email";
43
35
  readonly pgType: "TEXT";
44
- readonly chType: "LowCardinality(String)";
36
+ readonly chType: "Nullable(String)";
45
37
  }, {
46
- readonly name: "path";
38
+ readonly name: "user_name";
47
39
  readonly pgType: "TEXT";
48
- readonly chType: "String";
40
+ readonly chType: "Nullable(String)";
49
41
  }, {
50
42
  readonly name: "ip";
51
43
  readonly pgType: "INET";
@@ -62,6 +54,14 @@ declare const tableColumns: readonly [{
62
54
  readonly name: "locale";
63
55
  readonly pgType: "TEXT";
64
56
  readonly chType: "LowCardinality(Nullable(String))";
57
+ }, {
58
+ readonly name: "event_id";
59
+ readonly pgType: "TEXT PRIMARY KEY";
60
+ readonly chType: "String";
61
+ }, {
62
+ readonly name: "parent_event_id";
63
+ readonly pgType: "TEXT";
64
+ readonly chType: "Nullable(String)";
65
65
  }, {
66
66
  readonly name: "server_context";
67
67
  readonly pgType: "JSONB";
@@ -97,21 +97,21 @@ declare function generateChCreateTableSQL(database: string, tableName: string):
97
97
  declare function isChTableNotFoundError(text: string): boolean;
98
98
  /** Row type returned from analytics table queries */
99
99
  interface AnalyticsEventRow {
100
- event_id: string;
101
- parent_event_id: string | null;
102
100
  timestamp: string;
103
101
  type: string;
104
- anonymous_user_id: string | null;
102
+ host: string;
103
+ path: string;
104
+ method: string;
105
105
  user_id: string | null;
106
+ anonymous_user_id: string | null;
106
107
  user_email: string | null;
107
108
  user_name: string | null;
108
- host: string;
109
- method: string;
110
- path: string;
111
109
  ip: string | null;
112
110
  referer: string | null;
113
111
  user_agent: string | null;
114
112
  locale: string | null;
113
+ event_id: string;
114
+ parent_event_id: string | null;
115
115
  server_context: Record<string, unknown>;
116
116
  client_context: Record<string, unknown>;
117
117
  user_traits: Record<string, unknown>;
@@ -29,21 +29,21 @@ __export(db_exports, {
29
29
  });
30
30
  module.exports = __toCommonJS(db_exports);
31
31
  const tableColumns = [
32
- { name: "event_id", pgType: "TEXT PRIMARY KEY", chType: "String" },
33
- { name: "parent_event_id", pgType: "TEXT", chType: "Nullable(String)" },
34
- { name: "timestamp", pgType: "TIMESTAMPTZ", chType: "DateTime64(3)" },
35
- { name: "type", pgType: "TEXT", chType: "LowCardinality(String)" },
36
- { name: "anonymous_user_id", pgType: "TEXT", chType: "Nullable(String)" },
32
+ { name: "timestamp", pgType: "TIMESTAMPTZ NOT NULL", chType: "DateTime64(3)" },
33
+ { name: "type", pgType: "TEXT NOT NULL", chType: "LowCardinality(String)" },
34
+ { name: "host", pgType: "TEXT", chType: "LowCardinality(String)" },
35
+ { name: "path", pgType: "TEXT", chType: "String" },
36
+ { name: "method", pgType: "TEXT", chType: "LowCardinality(String)" },
37
37
  { name: "user_id", pgType: "TEXT", chType: "Nullable(String)" },
38
+ { name: "anonymous_user_id", pgType: "TEXT", chType: "Nullable(String)" },
38
39
  { name: "user_email", pgType: "TEXT", chType: "Nullable(String)" },
39
40
  { name: "user_name", pgType: "TEXT", chType: "Nullable(String)" },
40
- { name: "host", pgType: "TEXT", chType: "LowCardinality(String)" },
41
- { name: "method", pgType: "TEXT", chType: "LowCardinality(String)" },
42
- { name: "path", pgType: "TEXT", chType: "String" },
43
41
  { name: "ip", pgType: "INET", chType: "Nullable(IPv6)" },
44
42
  { name: "referer", pgType: "TEXT", chType: "Nullable(String)" },
45
43
  { name: "user_agent", pgType: "TEXT", chType: "Nullable(String)" },
46
44
  { name: "locale", pgType: "TEXT", chType: "LowCardinality(Nullable(String))" },
45
+ { name: "event_id", pgType: "TEXT PRIMARY KEY", chType: "String" },
46
+ { name: "parent_event_id", pgType: "TEXT", chType: "Nullable(String)" },
47
47
  { name: "server_context", pgType: "JSONB", chType: "JSON" },
48
48
  { name: "client_context", pgType: "JSONB", chType: "JSON" },
49
49
  { name: "user_traits", pgType: "JSONB", chType: "JSON" },
@@ -77,21 +77,21 @@ function extractCommonFields(event) {
77
77
  return Object.keys(rest).length > 0 ? rest : null;
78
78
  })() : null;
79
79
  return {
80
- event_id: event.eventId,
81
- parent_event_id: event.parentEventId ?? null,
82
80
  timestamp: event.collectedAt,
83
81
  type: event.type,
84
- anonymous_user_id: event.anonymousUserId ?? null,
82
+ host,
83
+ path,
84
+ method,
85
85
  user_id: event.userContext?.userId ?? null,
86
+ anonymous_user_id: event.anonymousUserId ?? null,
86
87
  user_email: event.userContext?.traits?.email ?? null,
87
88
  user_name: event.userContext?.traits?.name ?? null,
88
- host,
89
- method,
90
- path,
91
89
  ip: ip || null,
92
90
  referer: clientCtx.referer ?? null,
93
91
  user_agent: clientCtx.user_agent ?? null,
94
92
  locale: clientCtx.locale ?? null,
93
+ event_id: event.eventId,
94
+ parent_event_id: event.parentEventId ?? null,
95
95
  serverContextRest,
96
96
  clientContextRest: clientCtx.rest,
97
97
  userTraitsRest,
@@ -118,8 +118,8 @@ function eventToJsonRow(event) {
118
118
  };
119
119
  }
120
120
  function generatePgCreateTableSQL(tableName) {
121
- const pk = tableColumns[0];
122
- const alters = tableColumns.slice(1).map((col) => `ALTER TABLE ${tableName} ADD COLUMN IF NOT EXISTS ${col.name} ${col.pgType};`).join("\n");
121
+ const pk = tableColumns.find((c) => c.pgType.includes("PRIMARY KEY"));
122
+ const alters = tableColumns.filter((c) => c !== pk).map((col) => `ALTER TABLE ${tableName} ADD COLUMN IF NOT EXISTS ${col.name} ${col.pgType};`).join("\n");
123
123
  return `CREATE TABLE IF NOT EXISTS ${tableName} (${pk.name} ${pk.pgType});
124
124
  ${alters}`;
125
125
  }
@@ -129,7 +129,7 @@ function isPgTableNotFoundError(err) {
129
129
  function generateChCreateTableSQL(database, tableName) {
130
130
  const fullTable = `${database}.${tableName}`;
131
131
  const createCols = tableColumns.filter((c) => c.name === "event_id" || c.name === "timestamp").map((c) => `${c.name} ${c.chType}`).join(", ");
132
- const create = `CREATE TABLE IF NOT EXISTS ${fullTable} (${createCols}) ENGINE = ReplacingMergeTree() PARTITION BY toYYYYMM(timestamp) ORDER BY event_id;`;
132
+ const create = `CREATE TABLE IF NOT EXISTS ${fullTable} (${createCols}) ENGINE = ReplacingMergeTree() PARTITION BY toYYYYMM(timestamp) ORDER BY (timestamp, event_id);`;
133
133
  const alters = tableColumns.filter((c) => c.name !== "event_id" && c.name !== "timestamp").map((c) => `ALTER TABLE ${fullTable} ADD COLUMN IF NOT EXISTS ${c.name} ${c.chType};`).join("\n");
134
134
  return `${create}
135
135
  ${alters}`;
@@ -73,10 +73,17 @@ function neonBackend(config) {
73
73
  }
74
74
  if (sets.length > 0) {
75
75
  params.push(eventId);
76
- await sql.query(
77
- `UPDATE ${table} SET ${sets.join(", ")} WHERE event_id = $${paramIndex}`,
78
- params
79
- );
76
+ try {
77
+ await sql.query(
78
+ `UPDATE ${table} SET ${sets.join(", ")} WHERE event_id = $${paramIndex}`,
79
+ params
80
+ );
81
+ } catch (err) {
82
+ if ((0, import_db.isPgTableNotFoundError)(err)) {
83
+ printCreateTableStatement();
84
+ }
85
+ throw err;
86
+ }
80
87
  }
81
88
  }
82
89
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nextlytics/core",
3
- "version": "0.3.0-canary.80",
3
+ "version": "0.3.0-canary.81",
4
4
  "description": "Analytics library for Next.js",
5
5
  "license": "MIT",
6
6
  "repository": {