@rebasepro/server-postgresql 0.5.0 → 0.6.0

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.
Files changed (165) hide show
  1. package/dist/{server-postgresql/src/PostgresAdapter.d.ts → PostgresAdapter.d.ts} +1 -1
  2. package/dist/{server-postgresql/src/PostgresBackendDriver.d.ts → PostgresBackendDriver.d.ts} +2 -2
  3. package/dist/{server-postgresql/src/PostgresBootstrapper.d.ts → PostgresBootstrapper.d.ts} +11 -1
  4. package/dist/{server-postgresql/src/collections → collections}/PostgresCollectionRegistry.d.ts +4 -0
  5. package/dist/index.es.js +10168 -11145
  6. package/dist/index.es.js.map +1 -1
  7. package/dist/index.umd.js +10735 -11429
  8. package/dist/index.umd.js.map +1 -1
  9. package/dist/{server-postgresql/src/services → services}/EntityPersistService.d.ts +0 -14
  10. package/dist/utils/pg-error-utils.d.ts +55 -0
  11. package/package.json +24 -21
  12. package/src/PostgresAdapter.ts +9 -10
  13. package/src/PostgresBackendDriver.ts +134 -121
  14. package/src/PostgresBootstrapper.ts +86 -13
  15. package/src/auth/ensure-tables.ts +28 -5
  16. package/src/auth/services.ts +28 -18
  17. package/src/cli.ts +99 -96
  18. package/src/collections/PostgresCollectionRegistry.ts +7 -0
  19. package/src/connection.ts +11 -6
  20. package/src/data-transformer.ts +16 -14
  21. package/src/databasePoolManager.ts +3 -2
  22. package/src/history/HistoryService.ts +3 -2
  23. package/src/history/ensure-history-table.ts +5 -4
  24. package/src/schema/auth-schema.ts +1 -2
  25. package/src/schema/doctor-cli.ts +2 -1
  26. package/src/schema/doctor.ts +40 -37
  27. package/src/schema/generate-drizzle-schema-logic.ts +56 -18
  28. package/src/schema/generate-drizzle-schema.ts +11 -11
  29. package/src/schema/introspect-db-inference.ts +25 -25
  30. package/src/schema/introspect-db-logic.ts +38 -38
  31. package/src/schema/introspect-db.ts +28 -27
  32. package/src/services/BranchService.ts +14 -0
  33. package/src/services/EntityFetchService.ts +28 -25
  34. package/src/services/EntityPersistService.ts +11 -141
  35. package/src/services/RelationService.ts +57 -37
  36. package/src/services/entity-helpers.ts +6 -2
  37. package/src/services/realtimeService.ts +45 -32
  38. package/src/utils/drizzle-conditions.ts +31 -15
  39. package/src/utils/pg-error-utils.ts +211 -0
  40. package/src/websocket.ts +15 -12
  41. package/test/auth-services.test.ts +36 -19
  42. package/test/batch-many-to-many-regression.test.ts +119 -39
  43. package/test/data-transformer-hardening.test.ts +67 -33
  44. package/test/data-transformer.test.ts +4 -2
  45. package/test/doctor.test.ts +10 -5
  46. package/test/drizzle-conditions.test.ts +59 -6
  47. package/test/generate-drizzle-schema.test.ts +65 -40
  48. package/test/introspect-db-generation.test.ts +179 -81
  49. package/test/introspect-db-utils.test.ts +92 -37
  50. package/test/mocks/chalk.cjs +7 -0
  51. package/test/pg-error-utils.test.ts +221 -0
  52. package/test/postgresDataDriver.test.ts +14 -5
  53. package/test/property-ordering.test.ts +126 -79
  54. package/test/realtimeService.test.ts +6 -2
  55. package/test/relation-pipeline-gaps.test.ts +84 -36
  56. package/test/relations.test.ts +247 -0
  57. package/test/unmapped-tables-safety.test.ts +14 -6
  58. package/test/websocket.test.ts +1 -1
  59. package/tsconfig.json +5 -0
  60. package/tsconfig.prod.json +3 -0
  61. package/vite.config.ts +5 -5
  62. package/dist/common/src/collections/CollectionRegistry.d.ts +0 -56
  63. package/dist/common/src/collections/default-collections.d.ts +0 -9
  64. package/dist/common/src/collections/index.d.ts +0 -2
  65. package/dist/common/src/data/buildRebaseData.d.ts +0 -14
  66. package/dist/common/src/data/query_builder.d.ts +0 -55
  67. package/dist/common/src/index.d.ts +0 -4
  68. package/dist/common/src/util/builders.d.ts +0 -57
  69. package/dist/common/src/util/callbacks.d.ts +0 -6
  70. package/dist/common/src/util/collections.d.ts +0 -11
  71. package/dist/common/src/util/common.d.ts +0 -2
  72. package/dist/common/src/util/conditions.d.ts +0 -26
  73. package/dist/common/src/util/entities.d.ts +0 -58
  74. package/dist/common/src/util/enums.d.ts +0 -3
  75. package/dist/common/src/util/index.d.ts +0 -16
  76. package/dist/common/src/util/navigation_from_path.d.ts +0 -34
  77. package/dist/common/src/util/navigation_utils.d.ts +0 -20
  78. package/dist/common/src/util/parent_references_from_path.d.ts +0 -6
  79. package/dist/common/src/util/paths.d.ts +0 -14
  80. package/dist/common/src/util/permissions.d.ts +0 -14
  81. package/dist/common/src/util/references.d.ts +0 -2
  82. package/dist/common/src/util/relations.d.ts +0 -22
  83. package/dist/common/src/util/resolutions.d.ts +0 -72
  84. package/dist/common/src/util/storage.d.ts +0 -24
  85. package/dist/types/src/controllers/analytics_controller.d.ts +0 -7
  86. package/dist/types/src/controllers/auth.d.ts +0 -104
  87. package/dist/types/src/controllers/client.d.ts +0 -168
  88. package/dist/types/src/controllers/collection_registry.d.ts +0 -46
  89. package/dist/types/src/controllers/customization_controller.d.ts +0 -60
  90. package/dist/types/src/controllers/data.d.ts +0 -207
  91. package/dist/types/src/controllers/data_driver.d.ts +0 -218
  92. package/dist/types/src/controllers/database_admin.d.ts +0 -11
  93. package/dist/types/src/controllers/dialogs_controller.d.ts +0 -36
  94. package/dist/types/src/controllers/effective_role.d.ts +0 -4
  95. package/dist/types/src/controllers/email.d.ts +0 -36
  96. package/dist/types/src/controllers/index.d.ts +0 -18
  97. package/dist/types/src/controllers/local_config_persistence.d.ts +0 -20
  98. package/dist/types/src/controllers/navigation.d.ts +0 -225
  99. package/dist/types/src/controllers/registry.d.ts +0 -63
  100. package/dist/types/src/controllers/side_dialogs_controller.d.ts +0 -67
  101. package/dist/types/src/controllers/side_entity_controller.d.ts +0 -97
  102. package/dist/types/src/controllers/snackbar.d.ts +0 -24
  103. package/dist/types/src/controllers/storage.d.ts +0 -171
  104. package/dist/types/src/index.d.ts +0 -4
  105. package/dist/types/src/rebase_context.d.ts +0 -122
  106. package/dist/types/src/types/auth_adapter.d.ts +0 -301
  107. package/dist/types/src/types/backend.d.ts +0 -571
  108. package/dist/types/src/types/backend_hooks.d.ts +0 -172
  109. package/dist/types/src/types/builders.d.ts +0 -15
  110. package/dist/types/src/types/chips.d.ts +0 -5
  111. package/dist/types/src/types/collections.d.ts +0 -961
  112. package/dist/types/src/types/component_ref.d.ts +0 -47
  113. package/dist/types/src/types/cron.d.ts +0 -102
  114. package/dist/types/src/types/data_source.d.ts +0 -64
  115. package/dist/types/src/types/database_adapter.d.ts +0 -94
  116. package/dist/types/src/types/entities.d.ts +0 -145
  117. package/dist/types/src/types/entity_actions.d.ts +0 -104
  118. package/dist/types/src/types/entity_callbacks.d.ts +0 -173
  119. package/dist/types/src/types/entity_link_builder.d.ts +0 -7
  120. package/dist/types/src/types/entity_overrides.d.ts +0 -10
  121. package/dist/types/src/types/entity_views.d.ts +0 -87
  122. package/dist/types/src/types/export_import.d.ts +0 -21
  123. package/dist/types/src/types/formex.d.ts +0 -40
  124. package/dist/types/src/types/index.d.ts +0 -28
  125. package/dist/types/src/types/locales.d.ts +0 -4
  126. package/dist/types/src/types/modify_collections.d.ts +0 -5
  127. package/dist/types/src/types/plugins.d.ts +0 -282
  128. package/dist/types/src/types/properties.d.ts +0 -1173
  129. package/dist/types/src/types/property_config.d.ts +0 -74
  130. package/dist/types/src/types/relations.d.ts +0 -336
  131. package/dist/types/src/types/slots.d.ts +0 -262
  132. package/dist/types/src/types/translations.d.ts +0 -900
  133. package/dist/types/src/types/user_management_delegate.d.ts +0 -86
  134. package/dist/types/src/types/websockets.d.ts +0 -78
  135. package/dist/types/src/users/index.d.ts +0 -1
  136. package/dist/types/src/users/user.d.ts +0 -50
  137. /package/dist/{server-postgresql/src/auth → auth}/ensure-tables.d.ts +0 -0
  138. /package/dist/{server-postgresql/src/auth → auth}/services.d.ts +0 -0
  139. /package/dist/{server-postgresql/src/cli.d.ts → cli.d.ts} +0 -0
  140. /package/dist/{server-postgresql/src/connection.d.ts → connection.d.ts} +0 -0
  141. /package/dist/{server-postgresql/src/data-transformer.d.ts → data-transformer.d.ts} +0 -0
  142. /package/dist/{server-postgresql/src/databasePoolManager.d.ts → databasePoolManager.d.ts} +0 -0
  143. /package/dist/{server-postgresql/src/history → history}/HistoryService.d.ts +0 -0
  144. /package/dist/{server-postgresql/src/history → history}/ensure-history-table.d.ts +0 -0
  145. /package/dist/{server-postgresql/src/index.d.ts → index.d.ts} +0 -0
  146. /package/dist/{server-postgresql/src/interfaces.d.ts → interfaces.d.ts} +0 -0
  147. /package/dist/{server-postgresql/src/schema → schema}/auth-schema.d.ts +0 -0
  148. /package/dist/{server-postgresql/src/schema → schema}/doctor-cli.d.ts +0 -0
  149. /package/dist/{server-postgresql/src/schema → schema}/doctor.d.ts +0 -0
  150. /package/dist/{server-postgresql/src/schema → schema}/generate-drizzle-schema-logic.d.ts +0 -0
  151. /package/dist/{server-postgresql/src/schema → schema}/generate-drizzle-schema.d.ts +0 -0
  152. /package/dist/{server-postgresql/src/schema → schema}/introspect-db-inference.d.ts +0 -0
  153. /package/dist/{server-postgresql/src/schema → schema}/introspect-db-logic.d.ts +0 -0
  154. /package/dist/{server-postgresql/src/schema → schema}/introspect-db.d.ts +0 -0
  155. /package/dist/{server-postgresql/src/schema → schema}/test-schema.d.ts +0 -0
  156. /package/dist/{server-postgresql/src/services → services}/BranchService.d.ts +0 -0
  157. /package/dist/{server-postgresql/src/services → services}/EntityFetchService.d.ts +0 -0
  158. /package/dist/{server-postgresql/src/services → services}/RelationService.d.ts +0 -0
  159. /package/dist/{server-postgresql/src/services → services}/entity-helpers.d.ts +0 -0
  160. /package/dist/{server-postgresql/src/services → services}/entityService.d.ts +0 -0
  161. /package/dist/{server-postgresql/src/services → services}/index.d.ts +0 -0
  162. /package/dist/{server-postgresql/src/services → services}/realtimeService.d.ts +0 -0
  163. /package/dist/{server-postgresql/src/types.d.ts → types.d.ts} +0 -0
  164. /package/dist/{server-postgresql/src/utils → utils}/drizzle-conditions.d.ts +0 -0
  165. /package/dist/{server-postgresql/src/websocket.d.ts → websocket.d.ts} +0 -0
@@ -4,6 +4,8 @@ import { FilterValues, WhereFilterOp, Relation, JoinStep, LogicalCondition, Filt
4
4
  import { getColumnName, resolveCollectionRelations } from "@rebasepro/common";
5
5
  import { PostgresCollectionRegistry } from "../collections/PostgresCollectionRegistry";
6
6
  import { ConditionBuilderStatic } from "../interfaces";
7
+ import { logger } from "@rebasepro/server-core";
8
+ import { getColumnMeta } from "../services/entity-helpers";
7
9
 
8
10
  /** Drizzle dynamic query builder — accepts innerJoin + where chaining */
9
11
 
@@ -48,7 +50,7 @@ export class DrizzleConditionBuilder {
48
50
  }
49
51
 
50
52
  if (!fieldColumn) {
51
- console.warn(`Filtering by field '${field}', but it does not exist in table for collection '${collectionPath}'`);
53
+ logger.warn(`Filtering by field '${field}', but it does not exist in table for collection '${collectionPath}'`);
52
54
  continue;
53
55
  }
54
56
 
@@ -90,7 +92,7 @@ export class DrizzleConditionBuilder {
90
92
  }
91
93
  }
92
94
  if (!fieldColumn) {
93
- console.warn(`Filtering by field '${cond.column}', but it does not exist in table for collection '${collectionPath}'`);
95
+ logger.warn(`Filtering by field '${cond.column}', but it does not exist in table for collection '${collectionPath}'`);
94
96
  return null;
95
97
  }
96
98
  return this.buildSingleFilterCondition(fieldColumn, cond.operator as WhereFilterOp, cond.value);
@@ -129,25 +131,39 @@ export class DrizzleConditionBuilder {
129
131
  return inArray(column, value);
130
132
  }
131
133
  return null;
132
- case "array-contains":
134
+ case "array-contains": {
135
+ const meta = getColumnMeta(column);
136
+ if (meta.dataType === "array" || meta.columnType === "PgArray") {
137
+ return sql`${column} @> ARRAY[${value}]`;
138
+ }
133
139
  // For JSONB arrays: checks if the column contains the given value
134
140
  return sql`${column} @> ${JSON.stringify([value])}`;
135
- case "array-contains-any":
136
- // For JSONB arrays: checks if the column contains any of the given values
141
+ }
142
+ case "array-contains-any": {
143
+ const meta = getColumnMeta(column);
144
+ const isNativeArray = meta.dataType === "array" || meta.columnType === "PgArray";
137
145
  if (Array.isArray(value) && value.length > 0) {
138
- // Use the ?| operator for JSONB overlap with text array
139
- const textValues = value.map(v => String(v));
140
- return sql`${column} ?| array[${sql.join(textValues.map(v => sql`${v}`), sql`, `)}]`;
146
+ if (isNativeArray) {
147
+ return sql`${column} && ARRAY[${sql.join(value.map(v => sql`${v}`), sql`, `)}]`;
148
+ } else {
149
+ // Use the ?| operator for JSONB overlap with text array
150
+ const textValues = value.map(v => String(v));
151
+ return sql`${column} ?| array[${sql.join(textValues.map(v => sql`${v}`), sql`, `)}]`;
152
+ }
141
153
  }
142
154
  // Single value fallback: treat as array-contains
155
+ if (isNativeArray) {
156
+ return sql`${column} @> ARRAY[${value}]`;
157
+ }
143
158
  return sql`${column} @> ${JSON.stringify([value])}`;
159
+ }
144
160
  case "not-in":
145
161
  if (Array.isArray(value) && value.length > 0) {
146
162
  return sql`${column} NOT IN (${sql.join(value.map(v => sql`${v}`), sql`, `)})`;
147
163
  }
148
164
  return null;
149
165
  default:
150
- console.warn(`Unsupported filter operation: ${op}`);
166
+ logger.warn(`Unsupported filter operation: ${op}`);
151
167
  return null;
152
168
  }
153
169
  }
@@ -247,7 +263,7 @@ export class DrizzleConditionBuilder {
247
263
  );
248
264
  whereConditions.push(simpleCondition);
249
265
  } else {
250
- console.error("🔍 [buildRelationConditions] Failed to find junction table info and no foreign key specified");
266
+ logger.error("🔍 [buildRelationConditions] Failed to find junction table info and no foreign key specified");
251
267
  throw new Error(`Cannot resolve inverse many relation '${relation.relationName}'. Either specify 'through' property, ensure corresponding owning relation exists with junction table configuration, or specify 'foreignKeyOnTarget' for one-to-many relationships.`);
252
268
  }
253
269
  } else {
@@ -711,9 +727,9 @@ export class DrizzleConditionBuilder {
711
727
  const fieldColumn = table[key as keyof typeof table] as AnyPgColumn;
712
728
  if (fieldColumn) {
713
729
  // Verify that the underlying database column supports string pattern-matching
714
- const supportsILike =
715
- fieldColumn instanceof PgVarchar ||
716
- fieldColumn instanceof PgText ||
730
+ const supportsILike =
731
+ fieldColumn instanceof PgVarchar ||
732
+ fieldColumn instanceof PgText ||
717
733
  fieldColumn instanceof PgChar ||
718
734
  (fieldColumn && typeof fieldColumn === "object" && !("columnType" in fieldColumn));
719
735
  if (supportsILike) {
@@ -1058,7 +1074,7 @@ export class DrizzleConditionBuilder {
1058
1074
  console.debug("🔍 [findCorrespondingJunctionTable] Returning junction info:", result);
1059
1075
  return result;
1060
1076
  } catch (error) {
1061
- console.error(`🔍 [findCorrespondingJunctionTable] Error finding corresponding junction table for relation '${relation.relationName}':`, error);
1077
+ logger.error(`🔍 [findCorrespondingJunctionTable] Error finding corresponding junction table for relation '${relation.relationName}'`, { error: error });
1062
1078
  return null;
1063
1079
  }
1064
1080
  }
@@ -1108,7 +1124,7 @@ export class DrizzleConditionBuilder {
1108
1124
  filter: vectorSearch.threshold != null
1109
1125
  ? sql`(${column} ${sql.raw(operator)} ${sql.raw(vectorLiteral)}) < ${vectorSearch.threshold}`
1110
1126
  : undefined,
1111
- distanceSelect: sql`(${column} ${sql.raw(operator)} ${sql.raw(vectorLiteral)})`,
1127
+ distanceSelect: sql`(${column} ${sql.raw(operator)} ${sql.raw(vectorLiteral)})`
1112
1128
  };
1113
1129
  }
1114
1130
  }
@@ -0,0 +1,211 @@
1
+ /**
2
+ * Shared PostgreSQL error extraction and user-friendly message formatting.
3
+ *
4
+ * Drizzle wraps native PG errors in a `.cause` chain. These utilities
5
+ * unwrap that chain to get the real PostgreSQL error (identified by a
6
+ * 5-character alphanumeric `code` such as `42P01`) and translate it into
7
+ * a message that is safe and helpful to show to end-users.
8
+ */
9
+
10
+ import { logger } from "@rebasepro/server-core";
11
+
12
+ /** Shape of PostgreSQL errors with diagnostic metadata. */
13
+ export interface PostgresError extends Error {
14
+ code?: string;
15
+ detail?: string;
16
+ hint?: string;
17
+ constraint?: string;
18
+ column?: string;
19
+ table?: string;
20
+ dataType?: string;
21
+ cause?: unknown;
22
+ }
23
+
24
+ /**
25
+ * Extract the underlying PostgreSQL error from a Drizzle wrapper.
26
+ * Drizzle wraps PG errors in a `cause` property — this function
27
+ * recursively walks the chain until it finds an object with a PG
28
+ * error code (5-char alphanumeric, e.g. `42P01`).
29
+ */
30
+ export function extractPgError(error: unknown): PostgresError | null {
31
+ if (!error || typeof error !== "object") return null;
32
+ if (!(error instanceof Error)) {
33
+ // Check non-Error objects for a cause chain (Drizzle sometimes wraps oddly)
34
+ if ("cause" in error && (error as Record<string, unknown>).cause && typeof (error as Record<string, unknown>).cause === "object") {
35
+ return extractPgError((error as Record<string, unknown>).cause);
36
+ }
37
+ return null;
38
+ }
39
+
40
+ // Check if the error itself has a PG error code
41
+ if ("code" in error && typeof (error as PostgresError).code === "string" && /^[0-9A-Z]{5}$/.test((error as PostgresError).code!)) {
42
+ return error as PostgresError;
43
+ }
44
+
45
+ // Check the cause chain (Drizzle wraps PG errors)
46
+ if (error.cause && typeof error.cause === "object") {
47
+ return extractPgError(error.cause);
48
+ }
49
+
50
+ return null;
51
+ }
52
+
53
+ /**
54
+ * Walk the error cause chain and return the deepest meaningful message.
55
+ */
56
+ export function extractCauseMessage(error: unknown): string | null {
57
+ if (!error || typeof error !== "object") return null;
58
+ if (!(error instanceof Error)) return null;
59
+
60
+ if (error.cause && typeof error.cause === "object") {
61
+ const deeper = extractCauseMessage(error.cause);
62
+ if (deeper) return deeper;
63
+ // The cause itself has a message
64
+ if (error.cause instanceof Error && error.cause.message) {
65
+ return error.cause.message;
66
+ }
67
+ }
68
+ return null;
69
+ }
70
+
71
+ /**
72
+ * Translate a raw PostgreSQL error into a user-friendly message.
73
+ *
74
+ * @param pgError - The extracted PostgreSQL error (from {@link extractPgError})
75
+ * @param context - A human-readable context string (e.g. collection slug or path)
76
+ * @returns An object with a `message` safe for the client and the PG `code`.
77
+ */
78
+ export function pgErrorToFriendlyMessage(pgError: PostgresError, context: string): { message: string; code: string } {
79
+ const detail = pgError.detail as string | undefined;
80
+ const hint = pgError.hint as string | undefined;
81
+ const constraint = pgError.constraint as string | undefined;
82
+ const column = pgError.column as string | undefined;
83
+ const table = pgError.table as string | undefined;
84
+ const dataType = pgError.dataType as string | undefined;
85
+ const pgMessage = pgError.message || "Unknown database error";
86
+ const code = pgError.code || "UNKNOWN";
87
+
88
+ const suffix = hint ? ` Hint: ${hint}` : "";
89
+ const tableRef = table ?? context;
90
+
91
+ switch (pgError.code) {
92
+ case "23503": // foreign_key_violation
93
+ return {
94
+ message: detail
95
+ ? `Foreign key constraint violated: ${detail}${suffix}`
96
+ : `Cannot complete operation: a foreign key constraint${constraint ? ` (${constraint})` : ""} was violated in "${context}".${suffix}`,
97
+ code
98
+ };
99
+ case "23505": // unique_violation
100
+ return {
101
+ message: detail
102
+ ? `Duplicate value: ${detail}${suffix}`
103
+ : `Cannot complete operation: a unique constraint${constraint ? ` (${constraint})` : ""} was violated in "${context}".${suffix}`,
104
+ code
105
+ };
106
+ case "23502": // not_null_violation
107
+ return {
108
+ message: `Missing required field: "${column ?? "unknown"}" in "${tableRef}" cannot be empty.${suffix}`,
109
+ code
110
+ };
111
+ case "23514": // check_violation
112
+ return {
113
+ message: `Validation failed: a check constraint${constraint ? ` (${constraint})` : ""} was violated in "${context}".${suffix}`,
114
+ code
115
+ };
116
+ case "22P02": // invalid_text_representation (e.g. invalid UUID, wrong enum value)
117
+ return {
118
+ message: `Invalid data format in "${context}": ${pgMessage}${suffix}`,
119
+ code
120
+ };
121
+ case "22001": // string_data_right_truncation (value too long)
122
+ return {
123
+ message: `Value too long for column "${column ?? "unknown"}" in "${tableRef}": ${pgMessage}${suffix}`,
124
+ code
125
+ };
126
+ case "22003": // numeric_value_out_of_range
127
+ return {
128
+ message: `Numeric value out of range for column "${column ?? "unknown"}" in "${tableRef}": ${pgMessage}${suffix}`,
129
+ code
130
+ };
131
+ case "42703": // undefined_column
132
+ return {
133
+ message: `Unknown column in "${tableRef}": ${pgMessage}. Check if your schema is up to date (run migrations).${suffix}`,
134
+ code
135
+ };
136
+ case "42P01": // undefined_table
137
+ return {
138
+ message: `Table not found for "${context}": ${pgMessage}. Check if your schema is up to date (run migrations).${suffix}`,
139
+ code
140
+ };
141
+ case "42501": // insufficient_privilege
142
+ return {
143
+ message: `Permission denied on "${tableRef}". Check your database credentials and RLS policies.${suffix}`,
144
+ code
145
+ };
146
+ case "28000": // invalid_authorization_specification
147
+ return {
148
+ message: `Authorization failed for "${context}". Check your database credentials.${suffix}`,
149
+ code
150
+ };
151
+ default: {
152
+ // Unhandled PG code — still surface the actual database message
153
+ const parts = [`Database error in "${context}" [${code}]: ${pgMessage}`];
154
+ if (detail) parts.push(`Detail: ${detail}`);
155
+ if (column) parts.push(`Column: ${column}`);
156
+ if (dataType) parts.push(`Data type: ${dataType}`);
157
+ if (constraint) parts.push(`Constraint: ${constraint}`);
158
+ if (hint) parts.push(`Hint: ${hint}`);
159
+ return { message: parts.join(". "), code };
160
+ }
161
+ }
162
+ }
163
+
164
+ /**
165
+ * Sanitize any error into a message safe and helpful for the client.
166
+ *
167
+ * Extracts the PG error from the Drizzle cause chain when possible;
168
+ * falls back to a generic message that doesn't leak SQL.
169
+ *
170
+ * @param error - The raw caught error
171
+ * @param context - A human-readable context string (e.g. collection path)
172
+ * @returns An object with `message` (user-friendly) and optional `code` (PG code).
173
+ */
174
+ export function sanitizeErrorForClient(error: unknown, context: string): { message: string; code?: string } {
175
+ // ── Always log the full, unsanitized error server-side ──────────
176
+ const pgError = extractPgError(error);
177
+
178
+ if (pgError) {
179
+ logger.error(`[PG ${pgError.code}] Error in "${context}"`, {
180
+ code: pgError.code,
181
+ message: pgError.message,
182
+ detail: pgError.detail,
183
+ hint: pgError.hint,
184
+ column: pgError.column,
185
+ table: pgError.table,
186
+ constraint: pgError.constraint,
187
+ dataType: pgError.dataType,
188
+ // Also log the outer Drizzle wrapper message for full context
189
+ drizzleMessage: error instanceof Error ? error.message : String(error)
190
+ });
191
+ return pgErrorToFriendlyMessage(pgError, context);
192
+ }
193
+
194
+ // No PG error found — log the raw error as-is
195
+ logger.error(`Database error in "${context}" (no PG error extracted)`, {
196
+ error: error instanceof Error ? error.message : String(error),
197
+ stack: error instanceof Error ? error.stack : undefined,
198
+ cause: error instanceof Error && error.cause
199
+ ? (error.cause instanceof Error ? error.cause.message : String(error.cause))
200
+ : undefined
201
+ });
202
+
203
+ // Try to get the deepest cause message
204
+ const causeMessage = extractCauseMessage(error);
205
+ if (causeMessage) {
206
+ return { message: `Database error in "${context}": ${causeMessage}` };
207
+ }
208
+
209
+ // Last resort — generic message, never leak raw SQL
210
+ return { message: `Could not load data for "${context}". Check server logs for details.` };
211
+ }
package/src/websocket.ts CHANGED
@@ -7,6 +7,7 @@ import { WebSocketServer, WebSocket } from "ws";
7
7
  import { Server } from "http";
8
8
  import { inspect } from "util";
9
9
  import { extractUserFromToken, AccessTokenPayload } from "@rebasepro/server-core";
10
+ import { logger } from "@rebasepro/server-core";
10
11
 
11
12
  /** Minimal subset of RebaseAuthConfig used by the WebSocket layer. */
12
13
  interface WsAuthConfig {
@@ -33,7 +34,6 @@ interface ClientSession {
33
34
  }
34
35
 
35
36
 
36
-
37
37
  /** Maximum messages per client per window */
38
38
  const WS_RATE_LIMIT = 2000;
39
39
  /** Rate limit window in milliseconds (60 seconds) */
@@ -105,7 +105,7 @@ export function createPostgresWebSocket(
105
105
  // Silently absorbed — listenWithPortRetry will retry the next port
106
106
  return;
107
107
  }
108
- console.error("❌ [WebSocket Server] Error:", err);
108
+ logger.error("❌ [WebSocket Server] Error", { error: err });
109
109
  });
110
110
 
111
111
  // Auth is required when either: an adapter is present (secure by default),
@@ -170,14 +170,14 @@ code } }
170
170
  const adapterUser = authAdapter.verifyToken
171
171
  ? await authAdapter.verifyToken(token)
172
172
  : await authAdapter.verifyRequest(new Request("http://localhost/_ws_auth", {
173
- headers: { Authorization: `Bearer ${token}` },
173
+ headers: { Authorization: `Bearer ${token}` }
174
174
  }));
175
175
 
176
176
  if (adapterUser) {
177
177
  verifiedUser = {
178
178
  userId: adapterUser.uid,
179
179
  roles: adapterUser.roles,
180
- isAdmin: adapterUser.isAdmin,
180
+ isAdmin: adapterUser.isAdmin
181
181
  };
182
182
  }
183
183
  } catch {
@@ -190,7 +190,7 @@ code } }
190
190
  verifiedUser = {
191
191
  userId: jwtPayload.userId,
192
192
  roles: jwtPayload.roles ?? [],
193
- isAdmin: (jwtPayload.roles ?? []).some((r: string) => r === "admin"),
193
+ isAdmin: (jwtPayload.roles ?? []).some((r: string) => r === "admin")
194
194
  };
195
195
  }
196
196
  }
@@ -205,7 +205,8 @@ code } }
205
205
  ws.send(JSON.stringify({
206
206
  type: "AUTH_SUCCESS",
207
207
  requestId,
208
- payload: { userId: verifiedUser.userId, roles: verifiedUser.roles }
208
+ payload: { userId: verifiedUser.userId,
209
+ roles: verifiedUser.roles }
209
210
  }));
210
211
  wsDebug(`🔐 [WebSocket Server] Client ${clientId} authenticated as ${verifiedUser.userId}`);
211
212
  } else {
@@ -277,7 +278,7 @@ code } }
277
278
  };
278
279
  return await driver.withAuth(userForAuth);
279
280
  } catch (e) {
280
- console.error("Failed to create RLS scoped delegate for WS request", e);
281
+ logger.error("Failed to create RLS scoped delegate for WS request", { error: e });
281
282
  throw new Error("Internal authentication error");
282
283
  }
283
284
  }
@@ -576,8 +577,10 @@ colors: true }));
576
577
  // Attach auth context from the WS session so RLS-aware refetches work
577
578
  const session = clientSessions.get(clientId);
578
579
  const authContext = session?.user
579
- ? { userId: session.user.userId, roles: session.user.roles ?? [] }
580
- : { userId: "anon", roles: ["anon"] };
580
+ ? { userId: session.user.userId,
581
+ roles: session.user.roles ?? [] }
582
+ : { userId: "anon",
583
+ roles: ["anon"] };
581
584
  // Let RealtimeService handle these messages
582
585
  await realtimeService.handleClientMessage(clientId, {
583
586
  type,
@@ -588,12 +591,12 @@ colors: true }));
588
591
  }
589
592
 
590
593
  default:
591
- console.error("❌ [WebSocket Server] Unknown message type:", type);
594
+ logger.error("❌ [WebSocket Server] Unknown message type", { detail: type });
592
595
  }
593
596
  } catch (error: unknown) {
594
- console.error("💥 [WebSocket Server] Error handling message:", error);
597
+ logger.error("💥 [WebSocket Server] Error handling message", { error: error });
595
598
  if (error instanceof Error) {
596
- console.error("Stack trace:", error.stack);
599
+ logger.error("Stack trace", { detail: error.stack });
597
600
  }
598
601
  const errorMessage = process.env.NODE_ENV === "production"
599
602
  ? "An unexpected error occurred"
@@ -8,7 +8,9 @@ jest.mock("drizzle-orm", () => {
8
8
  const actual = jest.requireActual("drizzle-orm");
9
9
  return {
10
10
  ...actual,
11
- eq: jest.fn((field, value) => ({ field, value, type: "eq" })),
11
+ eq: jest.fn((field, value) => ({ field,
12
+ value,
13
+ type: "eq" })),
12
14
  sql: Object.assign(
13
15
  jest.fn((strings: TemplateStringsArray, ...values: unknown[]) => ({
14
16
  strings,
@@ -16,8 +18,11 @@ jest.mock("drizzle-orm", () => {
16
18
  type: "sql"
17
19
  })),
18
20
  {
19
- raw: jest.fn((val: string) => ({ val, type: "sql-raw" })),
20
- join: jest.fn((parts: unknown[], separator: unknown) => ({ parts, separator, type: "sql-join" }))
21
+ raw: jest.fn((val: string) => ({ val,
22
+ type: "sql-raw" })),
23
+ join: jest.fn((parts: unknown[], separator: unknown) => ({ parts,
24
+ separator,
25
+ type: "sql-join" }))
21
26
  }
22
27
  ),
23
28
  relations: jest.fn(() => ({}))
@@ -133,11 +138,11 @@ describe("Auth Services", () => {
133
138
  email: "test@example.com",
134
139
  displayName: "Test User"
135
140
  };
136
- const dbReturnedUser = {
141
+ const dbReturnedUser = {
137
142
  id: "user-123",
138
143
  ...newUser,
139
144
  createdAt: new Date(),
140
- updatedAt: new Date()
145
+ updatedAt: new Date()
141
146
  };
142
147
  mockInsertReturning.mockResolvedValueOnce([dbReturnedUser]);
143
148
 
@@ -154,7 +159,8 @@ describe("Auth Services", () => {
154
159
 
155
160
  describe("getUserById", () => {
156
161
  it("should return user when found", async () => {
157
- const mockUser = { id: "user-123", email: "test@example.com" };
162
+ const mockUser = { id: "user-123",
163
+ email: "test@example.com" };
158
164
  mockSelectWhere.mockResolvedValueOnce([mockUser]);
159
165
 
160
166
  const result = await userService.getUserById("user-123");
@@ -174,7 +180,8 @@ describe("Auth Services", () => {
174
180
 
175
181
  describe("getUserByEmail", () => {
176
182
  it("should return user when found by email", async () => {
177
- const mockUser = { id: "user-123", email: "test@example.com" };
183
+ const mockUser = { id: "user-123",
184
+ email: "test@example.com" };
178
185
  mockSelectWhere.mockResolvedValueOnce([mockUser]);
179
186
 
180
187
  const result = await userService.getUserByEmail("test@example.com");
@@ -194,13 +201,15 @@ describe("Auth Services", () => {
194
201
 
195
202
  describe("getUserByIdentity", () => {
196
203
  it("should fetch user by identity", async () => {
197
- const mockUser = { id: "user-123", email: "test@example.com" };
204
+ const mockUser = { id: "user-123",
205
+ email: "test@example.com" };
198
206
  mockSelectWhere.mockResolvedValueOnce([{ user: mockUser }]);
199
207
 
200
208
  const result = await userService.getUserByIdentity("google", "google-abc");
201
209
 
202
210
  expect(db.select).toHaveBeenCalled();
203
- expect(result).toEqual(expect.objectContaining({ id: "user-123", email: "test@example.com" }));
211
+ expect(result).toEqual(expect.objectContaining({ id: "user-123",
212
+ email: "test@example.com" }));
204
213
  });
205
214
  });
206
215
 
@@ -223,10 +232,10 @@ describe("Auth Services", () => {
223
232
 
224
233
  describe("updateUser", () => {
225
234
  it("should update user and return updated record", async () => {
226
- const updatedUser = {
235
+ const updatedUser = {
227
236
  id: "user-123",
228
237
  email: "test@example.com",
229
- displayName: "Updated Name"
238
+ displayName: "Updated Name"
230
239
  };
231
240
  mockUpdateReturning.mockResolvedValueOnce([updatedUser]);
232
241
 
@@ -261,8 +270,10 @@ describe("Auth Services", () => {
261
270
  describe("listUsers", () => {
262
271
  it("should return all users", async () => {
263
272
  const mockUsers = [
264
- { id: "user-1", email: "user1@example.com" },
265
- { id: "user-2", email: "user2@example.com" }
273
+ { id: "user-1",
274
+ email: "user1@example.com" },
275
+ { id: "user-2",
276
+ email: "user2@example.com" }
266
277
  ];
267
278
  mockSelectFrom.mockReturnValueOnce(Promise.resolve(mockUsers));
268
279
 
@@ -270,8 +281,10 @@ describe("Auth Services", () => {
270
281
 
271
282
  expect(db.select).toHaveBeenCalled();
272
283
  expect(result).toEqual([
273
- mockUserData({ id: "user-1", email: "user1@example.com" }),
274
- mockUserData({ id: "user-2", email: "user2@example.com" })
284
+ mockUserData({ id: "user-1",
285
+ email: "user1@example.com" }),
286
+ mockUserData({ id: "user-2",
287
+ email: "user2@example.com" })
275
288
  ]);
276
289
  });
277
290
  });
@@ -332,7 +345,8 @@ describe("Auth Services", () => {
332
345
 
333
346
  describe("getUserByVerificationToken", () => {
334
347
  it("should find user by verification token", async () => {
335
- const mockUser = { id: "user-123", email: "test@example.com" };
348
+ const mockUser = { id: "user-123",
349
+ email: "test@example.com" };
336
350
  mockSelectWhere.mockResolvedValueOnce([mockUser]);
337
351
 
338
352
  const result = await userService.getUserByVerificationToken("token-abc");
@@ -396,7 +410,8 @@ describe("Auth Services", () => {
396
410
 
397
411
  describe("getUserWithRoles", () => {
398
412
  it("should return user with roles", async () => {
399
- const mockUser = { id: "user-123", email: "test@example.com" };
413
+ const mockUser = { id: "user-123",
414
+ email: "test@example.com" };
400
415
  mockSelectWhere.mockResolvedValueOnce([mockUser]);
401
416
  mockExecute.mockResolvedValueOnce({
402
417
  rows: [{ roles: ["admin"] }]
@@ -427,7 +442,8 @@ describe("Auth Services", () => {
427
442
  it("should return paginated and filtered users list", async () => {
428
443
  mockExecute
429
444
  .mockResolvedValueOnce({ rows: [{ total: 1 }] })
430
- .mockResolvedValueOnce({ rows: [{ id: "user-123", email: "test@example.com" }] });
445
+ .mockResolvedValueOnce({ rows: [{ id: "user-123",
446
+ email: "test@example.com" }] });
431
447
 
432
448
  const result = await userService.listUsersPaginated({
433
449
  limit: 10,
@@ -439,7 +455,8 @@ describe("Auth Services", () => {
439
455
 
440
456
  expect(mockExecute).toHaveBeenCalledTimes(2);
441
457
  expect(result).toEqual({
442
- users: [mockUserData({ id: "user-123", email: "test@example.com" })],
458
+ users: [mockUserData({ id: "user-123",
459
+ email: "test@example.com" })],
443
460
  total: 1,
444
461
  limit: 10,
445
462
  offset: 0