@10x-media/analytics 0.1.0-beta.2 → 0.1.0-beta.3

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.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,13 @@
1
1
  # @10x-media/analytics
2
2
 
3
+ ## 0.1.0-beta.3
4
+
5
+ ### Patch Changes
6
+
7
+ - Make the rollup writer's Postgres import opaque to bundlers.
8
+
9
+ The optional `@payloadcms/db-postgres` peer was loaded via a literal dynamic import, which bundlers resolve at build time: a Mongo host on Turbopack fails `next build` with "Module not found" even though the Postgres branch never runs there (and `serverExternalPackages` does not help, since it governs bundling, not resolution). The specifier is now built at runtime, so no bundler resolves it and the package is only touched when the adapter is actually Postgres. Mongo hosts that worked around this with a resolve alias or stub can remove it.
10
+
3
11
  ## 0.1.0-beta.2
4
12
 
5
13
  ### Minor Changes
@@ -1,6 +1,22 @@
1
1
  import { ROLLUPS_SLUG } from "../collections/rollups.js";
2
2
  //#region src/native/rollups/bumpRollup.ts
3
3
  const PG_TABLE_KEY = "analytics_rollups";
4
+ /**
5
+ * Loads the optional Postgres peer without a literal specifier: bundlers (Turbopack, webpack,
6
+ * Vite) resolve literal dynamic imports at build time, which fails a Mongo host's build even
7
+ * though this branch is unreachable there. The function-built specifier is opaque to their
8
+ * analyzers, the ignore comments cover bundlers that would still warn, and the type-only
9
+ * reference above erases at compile time, so the package is touched only when the Postgres
10
+ * branch actually runs.
11
+ */
12
+ const importPostgresSql = () => {
13
+ return import(
14
+ /* webpackIgnore: true */
15
+ /* turbopackIgnore: true */
16
+ /* @vite-ignore */
17
+ ["@payloadcms", "db-postgres"].join("/")
18
+ );
19
+ };
4
20
  const ZERO = {
5
21
  pageviews: 0,
6
22
  events: 0,
@@ -21,7 +37,7 @@ async function bumpRollup(payload, key, inc) {
21
37
  }, { upsert: true });
22
38
  return;
23
39
  }
24
- const { sql } = await import("@payloadcms/db-postgres");
40
+ const { sql } = await importPostgresSql();
25
41
  const db = payload.db;
26
42
  const tableName = db.tableNameMap.get(PG_TABLE_KEY);
27
43
  if (!tableName) throw new Error(`analytics: drizzle table "${PG_TABLE_KEY}" not found`);
@@ -1 +1 @@
1
- {"version":3,"file":"bumpRollup.js","names":[],"sources":["../../../src/native/rollups/bumpRollup.ts"],"sourcesContent":["import type { Payload } from 'payload'\nimport { ROLLUPS_SLUG } from '../collections/rollups'\nimport type { RollupKey, RollupMetric } from './deltas'\n\nconst PG_TABLE_KEY = 'analytics_rollups'\n\n// payload.db raw-access shapes are intentionally loose; these narrow casts reach the\n// Mongoose driver collection / Drizzle instance for atomic upserts (no public typed API).\ntype MongoDb = {\n\tname: 'mongoose'\n\tcollections: Record<\n\t\tstring,\n\t\t{ collection: { updateOne: (f: object, u: object, o: object) => Promise<unknown> } }\n\t>\n}\ntype PgDb = {\n\tname: 'postgres'\n\tdrizzle: {\n\t\tinsert: (t: unknown) => {\n\t\t\tvalues: (v: unknown) => { onConflictDoUpdate: (c: unknown) => Promise<unknown> }\n\t\t}\n\t}\n\ttables: Record<string, Record<string, unknown>>\n\ttableNameMap: Map<string, string>\n}\n\nconst ZERO: Record<RollupMetric, number> = {\n\tpageviews: 0,\n\tevents: 0,\n\tdurationMs: 0,\n\tsamples: 0,\n\tvisitors: 0,\n\tsessions: 0,\n}\n\n// Always seed every metric on insert: Payload's defaultValue is applied app-side, not as\n// a SQL default, so a raw insert that omits a NOT NULL metric column would fail on Postgres.\nexport async function bumpRollup(\n\tpayload: Payload,\n\tkey: RollupKey,\n\tinc: Partial<Record<RollupMetric, number>>\n): Promise<void> {\n\tif (payload.db.name === 'mongoose') {\n\t\tconst db = payload.db as unknown as MongoDb\n\t\tconst model = db.collections[ROLLUPS_SLUG]\n\t\tif (!model) throw new Error(`analytics: mongoose collection \"${ROLLUPS_SLUG}\" not found`)\n\t\tconst setOnInsert: Record<string, unknown> = { ...key }\n\t\tfor (const [metric, zero] of Object.entries(ZERO)) {\n\t\t\tif (!(metric in inc)) setOnInsert[metric] = zero\n\t\t}\n\t\tawait model.collection.updateOne(\n\t\t\tkey,\n\t\t\t{ $inc: inc, $setOnInsert: setOnInsert },\n\t\t\t{ upsert: true }\n\t\t)\n\t\treturn\n\t}\n\tconst { sql } = await import('@payloadcms/db-postgres')\n\tconst db = payload.db as unknown as PgDb\n\tconst tableName = db.tableNameMap.get(PG_TABLE_KEY)\n\tif (!tableName) throw new Error(`analytics: drizzle table \"${PG_TABLE_KEY}\" not found`)\n\tconst table = db.tables[tableName]\n\tif (!table) throw new Error(`analytics: drizzle table object for \"${tableName}\" not found`)\n\tconst set: Record<string, unknown> = {}\n\tfor (const [metric, amount] of Object.entries(inc)) {\n\t\tset[metric] = sql`${table[metric]} + ${amount}`\n\t}\n\t// The conflict target must match the unique index exactly, which includes the\n\t// scope column only in scoped installs (mirrored by a scope key on RollupKey).\n\tconst target = [table.granularity, table.period, table.path, table.dimension, table.dimvalue]\n\tif (key.scope !== undefined) {\n\t\ttarget.push(table.scope)\n\t}\n\tawait db.drizzle\n\t\t.insert(table)\n\t\t.values({ ...key, ...ZERO, ...inc })\n\t\t.onConflictDoUpdate({ target, set })\n}\n"],"mappings":";;AAIA,MAAM,eAAe;AAsBrB,MAAM,OAAqC;CAC1C,WAAW;CACX,QAAQ;CACR,YAAY;CACZ,SAAS;CACT,UAAU;CACV,UAAU;AACX;AAIA,eAAsB,WACrB,SACA,KACA,KACgB;CAChB,IAAI,QAAQ,GAAG,SAAS,YAAY;EAEnC,MAAM,QADK,QAAQ,GACF,YAAY;EAC7B,IAAI,CAAC,OAAO,MAAM,IAAI,MAAM,mCAAmC,aAAa,YAAY;EACxF,MAAM,cAAuC,EAAE,GAAG,IAAI;EACtD,KAAK,MAAM,CAAC,QAAQ,SAAS,OAAO,QAAQ,IAAI,GAC/C,IAAI,EAAE,UAAU,MAAM,YAAY,UAAU;EAE7C,MAAM,MAAM,WAAW,UACtB,KACA;GAAE,MAAM;GAAK,cAAc;EAAY,GACvC,EAAE,QAAQ,KAAK,CAChB;EACA;CACD;CACA,MAAM,EAAE,QAAQ,MAAM,OAAO;CAC7B,MAAM,KAAK,QAAQ;CACnB,MAAM,YAAY,GAAG,aAAa,IAAI,YAAY;CAClD,IAAI,CAAC,WAAW,MAAM,IAAI,MAAM,6BAA6B,aAAa,YAAY;CACtF,MAAM,QAAQ,GAAG,OAAO;CACxB,IAAI,CAAC,OAAO,MAAM,IAAI,MAAM,wCAAwC,UAAU,YAAY;CAC1F,MAAM,MAA+B,CAAC;CACtC,KAAK,MAAM,CAAC,QAAQ,WAAW,OAAO,QAAQ,GAAG,GAChD,IAAI,UAAU,GAAG,GAAG,MAAM,QAAQ,KAAK;CAIxC,MAAM,SAAS;EAAC,MAAM;EAAa,MAAM;EAAQ,MAAM;EAAM,MAAM;EAAW,MAAM;CAAQ;CAC5F,IAAI,IAAI,UAAU,KAAA,GACjB,OAAO,KAAK,MAAM,KAAK;CAExB,MAAM,GAAG,QACP,OAAO,KAAK,EACZ,OAAO;EAAE,GAAG;EAAK,GAAG;EAAM,GAAG;CAAI,CAAC,EAClC,mBAAmB;EAAE;EAAQ;CAAI,CAAC;AACrC"}
1
+ {"version":3,"file":"bumpRollup.js","names":[],"sources":["../../../src/native/rollups/bumpRollup.ts"],"sourcesContent":["import type { Payload } from 'payload'\nimport { ROLLUPS_SLUG } from '../collections/rollups'\nimport type { RollupKey, RollupMetric } from './deltas'\n\nconst PG_TABLE_KEY = 'analytics_rollups'\n\ntype PostgresSqlModule = { sql: typeof import('@payloadcms/db-postgres')['sql'] }\n\n/**\n * Loads the optional Postgres peer without a literal specifier: bundlers (Turbopack, webpack,\n * Vite) resolve literal dynamic imports at build time, which fails a Mongo host's build even\n * though this branch is unreachable there. The function-built specifier is opaque to their\n * analyzers, the ignore comments cover bundlers that would still warn, and the type-only\n * reference above erases at compile time, so the package is touched only when the Postgres\n * branch actually runs.\n */\nconst importPostgresSql = (): Promise<PostgresSqlModule> => {\n\tconst specifier = ['@payloadcms', 'db-postgres'].join('/')\n\treturn import(\n\t\t/* webpackIgnore: true */ /* turbopackIgnore: true */ /* @vite-ignore */ specifier\n\t) as Promise<PostgresSqlModule>\n}\n\n// payload.db raw-access shapes are intentionally loose; these narrow casts reach the\n// Mongoose driver collection / Drizzle instance for atomic upserts (no public typed API).\ntype MongoDb = {\n\tname: 'mongoose'\n\tcollections: Record<\n\t\tstring,\n\t\t{ collection: { updateOne: (f: object, u: object, o: object) => Promise<unknown> } }\n\t>\n}\ntype PgDb = {\n\tname: 'postgres'\n\tdrizzle: {\n\t\tinsert: (t: unknown) => {\n\t\t\tvalues: (v: unknown) => { onConflictDoUpdate: (c: unknown) => Promise<unknown> }\n\t\t}\n\t}\n\ttables: Record<string, Record<string, unknown>>\n\ttableNameMap: Map<string, string>\n}\n\nconst ZERO: Record<RollupMetric, number> = {\n\tpageviews: 0,\n\tevents: 0,\n\tdurationMs: 0,\n\tsamples: 0,\n\tvisitors: 0,\n\tsessions: 0,\n}\n\n// Always seed every metric on insert: Payload's defaultValue is applied app-side, not as\n// a SQL default, so a raw insert that omits a NOT NULL metric column would fail on Postgres.\nexport async function bumpRollup(\n\tpayload: Payload,\n\tkey: RollupKey,\n\tinc: Partial<Record<RollupMetric, number>>\n): Promise<void> {\n\tif (payload.db.name === 'mongoose') {\n\t\tconst db = payload.db as unknown as MongoDb\n\t\tconst model = db.collections[ROLLUPS_SLUG]\n\t\tif (!model) throw new Error(`analytics: mongoose collection \"${ROLLUPS_SLUG}\" not found`)\n\t\tconst setOnInsert: Record<string, unknown> = { ...key }\n\t\tfor (const [metric, zero] of Object.entries(ZERO)) {\n\t\t\tif (!(metric in inc)) setOnInsert[metric] = zero\n\t\t}\n\t\tawait model.collection.updateOne(\n\t\t\tkey,\n\t\t\t{ $inc: inc, $setOnInsert: setOnInsert },\n\t\t\t{ upsert: true }\n\t\t)\n\t\treturn\n\t}\n\tconst { sql } = await importPostgresSql()\n\tconst db = payload.db as unknown as PgDb\n\tconst tableName = db.tableNameMap.get(PG_TABLE_KEY)\n\tif (!tableName) throw new Error(`analytics: drizzle table \"${PG_TABLE_KEY}\" not found`)\n\tconst table = db.tables[tableName]\n\tif (!table) throw new Error(`analytics: drizzle table object for \"${tableName}\" not found`)\n\tconst set: Record<string, unknown> = {}\n\tfor (const [metric, amount] of Object.entries(inc)) {\n\t\tset[metric] = sql`${table[metric]} + ${amount}`\n\t}\n\t// The conflict target must match the unique index exactly, which includes the\n\t// scope column only in scoped installs (mirrored by a scope key on RollupKey).\n\tconst target = [table.granularity, table.period, table.path, table.dimension, table.dimvalue]\n\tif (key.scope !== undefined) {\n\t\ttarget.push(table.scope)\n\t}\n\tawait db.drizzle\n\t\t.insert(table)\n\t\t.values({ ...key, ...ZERO, ...inc })\n\t\t.onConflictDoUpdate({ target, set })\n}\n"],"mappings":";;AAIA,MAAM,eAAe;;;;;;;;;AAYrB,MAAM,0BAAsD;CAE3D,OAAO;;;;EADW,CAAC,eAAe,aAAa,EAAE,KAAK,GAE4B;;AAEnF;AAsBA,MAAM,OAAqC;CAC1C,WAAW;CACX,QAAQ;CACR,YAAY;CACZ,SAAS;CACT,UAAU;CACV,UAAU;AACX;AAIA,eAAsB,WACrB,SACA,KACA,KACgB;CAChB,IAAI,QAAQ,GAAG,SAAS,YAAY;EAEnC,MAAM,QADK,QAAQ,GACF,YAAY;EAC7B,IAAI,CAAC,OAAO,MAAM,IAAI,MAAM,mCAAmC,aAAa,YAAY;EACxF,MAAM,cAAuC,EAAE,GAAG,IAAI;EACtD,KAAK,MAAM,CAAC,QAAQ,SAAS,OAAO,QAAQ,IAAI,GAC/C,IAAI,EAAE,UAAU,MAAM,YAAY,UAAU;EAE7C,MAAM,MAAM,WAAW,UACtB,KACA;GAAE,MAAM;GAAK,cAAc;EAAY,GACvC,EAAE,QAAQ,KAAK,CAChB;EACA;CACD;CACA,MAAM,EAAE,QAAQ,MAAM,kBAAkB;CACxC,MAAM,KAAK,QAAQ;CACnB,MAAM,YAAY,GAAG,aAAa,IAAI,YAAY;CAClD,IAAI,CAAC,WAAW,MAAM,IAAI,MAAM,6BAA6B,aAAa,YAAY;CACtF,MAAM,QAAQ,GAAG,OAAO;CACxB,IAAI,CAAC,OAAO,MAAM,IAAI,MAAM,wCAAwC,UAAU,YAAY;CAC1F,MAAM,MAA+B,CAAC;CACtC,KAAK,MAAM,CAAC,QAAQ,WAAW,OAAO,QAAQ,GAAG,GAChD,IAAI,UAAU,GAAG,GAAG,MAAM,QAAQ,KAAK;CAIxC,MAAM,SAAS;EAAC,MAAM;EAAa,MAAM;EAAQ,MAAM;EAAM,MAAM;EAAW,MAAM;CAAQ;CAC5F,IAAI,IAAI,UAAU,KAAA,GACjB,OAAO,KAAK,MAAM,KAAK;CAExB,MAAM,GAAG,QACP,OAAO,KAAK,EACZ,OAAO;EAAE,GAAG;EAAK,GAAG;EAAM,GAAG;CAAI,CAAC,EAClC,mBAAmB;EAAE;EAAQ;CAAI,CAAC;AACrC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@10x-media/analytics",
3
- "version": "0.1.0-beta.2",
3
+ "version": "0.1.0-beta.3",
4
4
  "description": "Adapter-based analytics for Payload: providers, a native engine, dashboard widgets, and portable fields.",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -134,9 +134,9 @@
134
134
  "tsdown": "0.22.1",
135
135
  "typescript": "5.9.3",
136
136
  "vitest": "4.1.7",
137
+ "@10x-media/payload-test-harness": "0.0.0",
137
138
  "@10x-media/tsconfig": "0.0.0",
138
139
  "@10x-media/tsdown-config": "0.0.0",
139
- "@10x-media/payload-test-harness": "0.0.0",
140
140
  "@10x-media/vitest-config": "0.0.0"
141
141
  },
142
142
  "publishConfig": {