@rebasepro/server-postgresql 0.0.1-canary.f81da60 → 0.1.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 (59) hide show
  1. package/dist/index.es.js +287 -21
  2. package/dist/index.es.js.map +1 -1
  3. package/dist/index.umd.js +287 -21
  4. package/dist/index.umd.js.map +1 -1
  5. package/dist/server-postgresql/src/PostgresBackendDriver.d.ts +2 -1
  6. package/dist/server-postgresql/src/schema/introspect-db-inference.d.ts +5 -0
  7. package/dist/server-postgresql/src/schema/introspect-db-logic.d.ts +44 -9
  8. package/dist/server-postgresql/src/services/EntityPersistService.d.ts +9 -0
  9. package/dist/types/src/controllers/auth.d.ts +8 -2
  10. package/dist/types/src/controllers/client.d.ts +13 -0
  11. package/dist/types/src/controllers/navigation.d.ts +18 -6
  12. package/dist/types/src/controllers/registry.d.ts +9 -1
  13. package/dist/types/src/controllers/side_entity_controller.d.ts +7 -0
  14. package/dist/types/src/rebase_context.d.ts +17 -0
  15. package/dist/types/src/types/collections.d.ts +20 -1
  16. package/dist/types/src/types/component_ref.d.ts +47 -0
  17. package/dist/types/src/types/entity_views.d.ts +2 -1
  18. package/dist/types/src/types/index.d.ts +1 -0
  19. package/dist/types/src/types/properties.d.ts +15 -3
  20. package/dist/types/src/types/translations.d.ts +2 -0
  21. package/examples/sdk-demo/node_modules/esbuild/LICENSE.md +21 -0
  22. package/examples/sdk-demo/node_modules/esbuild/README.md +3 -0
  23. package/examples/sdk-demo/node_modules/esbuild/bin/esbuild +223 -0
  24. package/examples/sdk-demo/node_modules/esbuild/install.js +289 -0
  25. package/examples/sdk-demo/node_modules/esbuild/lib/main.d.ts +716 -0
  26. package/examples/sdk-demo/node_modules/esbuild/lib/main.js +2242 -0
  27. package/examples/sdk-demo/node_modules/esbuild/package.json +49 -0
  28. package/package.json +5 -5
  29. package/src/PostgresBackendDriver.ts +23 -6
  30. package/src/cli.ts +10 -2
  31. package/src/data-transformer.ts +84 -1
  32. package/src/schema/doctor.ts +14 -2
  33. package/src/schema/generate-drizzle-schema-logic.ts +52 -5
  34. package/src/schema/introspect-db-inference.ts +238 -0
  35. package/src/schema/introspect-db-logic.ts +365 -61
  36. package/src/schema/introspect-db.ts +66 -23
  37. package/src/services/EntityFetchService.ts +16 -0
  38. package/src/services/EntityPersistService.ts +88 -12
  39. package/test/generate-drizzle-schema.test.ts +295 -0
  40. package/test/introspect-db-generation.test.ts +32 -10
  41. package/test/property-ordering.test.ts +395 -0
  42. package/jest-all.log +0 -3128
  43. package/jest.log +0 -49
  44. package/scratch.ts +0 -41
  45. package/test-drizzle-bug.ts +0 -18
  46. package/test-drizzle-out/0000_cultured_freak.sql +0 -7
  47. package/test-drizzle-out/0001_tiresome_professor_monster.sql +0 -1
  48. package/test-drizzle-out/meta/0000_snapshot.json +0 -55
  49. package/test-drizzle-out/meta/0001_snapshot.json +0 -63
  50. package/test-drizzle-out/meta/_journal.json +0 -20
  51. package/test-drizzle-prompt.sh +0 -2
  52. package/test-policy-prompt.sh +0 -3
  53. package/test-programmatic.ts +0 -30
  54. package/test-programmatic2.ts +0 -59
  55. package/test-schema-no-policies.ts +0 -12
  56. package/test_drizzle_mock.js +0 -3
  57. package/test_find_changed.mjs +0 -32
  58. package/test_hash.js +0 -14
  59. package/test_output.txt +0 -3145
@@ -0,0 +1,238 @@
1
+ import { humanize } from "./introspect-db-logic";
2
+
3
+ export interface InferenceResult {
4
+ propType?: string; // If the inference changes the base type
5
+ extra?: string; // String to append to the property definition
6
+ }
7
+
8
+ const ISO_8601_REGEX = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d+)?(?:Z|[+-]\d{2}:\d{2})$/;
9
+ const UUID_REGEX = /^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i;
10
+ const CUID_REGEX = /^c[^\s-]{7,}$/i; // Basic CUID check
11
+ const COLOR_HEX_REGEX = /^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$/;
12
+
13
+ export function inferPropertyFromData(
14
+ columnName: string,
15
+ pgDataType: string,
16
+ currentPropType: string,
17
+ sampleValues: unknown[],
18
+ isPk: boolean
19
+ ): InferenceResult {
20
+ let result: InferenceResult = {};
21
+ let extraLines: string[] = [];
22
+
23
+ // Filter out null/undefined for analysis
24
+ const validValues = sampleValues.filter(v => v !== null && v !== undefined && v !== "");
25
+
26
+ if (validValues.length === 0) {
27
+ return result; // Not enough data
28
+ }
29
+
30
+ const colNameLower = columnName.toLowerCase();
31
+
32
+ // ── Number Analysis ──────────────────────────────────────────────────
33
+ if (currentPropType === "number") {
34
+ // Boolean stored as int
35
+ const allZeroOrOne = validValues.every(v => v === 0 || v === 1 || v === "0" || v === "1");
36
+ if (allZeroOrOne) {
37
+ result.propType = "boolean";
38
+ return result; // Don't do min/max if boolean
39
+ }
40
+
41
+ // Min / Max constraints
42
+ const numValues = validValues.map(v => Number(v)).filter(v => !isNaN(v));
43
+ if (numValues.length === validValues.length) {
44
+ const min = Math.min(...numValues);
45
+ const max = Math.max(...numValues);
46
+ // Example heuristic: percentages
47
+ if (min >= 0 && max <= 100 && (colNameLower.includes("percent") || colNameLower.includes("rate") || colNameLower.includes("score"))) {
48
+ extraLines.push(` validation: {\n min: 0,\n max: 100\n }`);
49
+ } else if (min >= 0 && (colNameLower.includes("count") || colNameLower.includes("total") || colNameLower.includes("amount"))) {
50
+ extraLines.push(` validation: {\n min: 0\n }`);
51
+ }
52
+ }
53
+
54
+ // Currency
55
+ if (colNameLower.includes("price") || colNameLower.includes("cost") || colNameLower.includes("amount") || colNameLower.includes("fee") || pgDataType === "money") {
56
+ extraLines.push(` ui: {\n currency: true\n }`);
57
+ }
58
+ }
59
+
60
+ // ── JSON / JSONB Analysis ────────────────────────────────────────────
61
+ if (currentPropType === "map" || pgDataType.includes("json")) {
62
+ // PostGres jsonb can be object or array
63
+ let allArrays = true;
64
+ let allObjects = true;
65
+
66
+ for (const v of validValues) {
67
+ let parsed = v;
68
+ if (typeof v === "string") {
69
+ try { parsed = JSON.parse(v); } catch (e) { allArrays = false; allObjects = false; break; }
70
+ }
71
+ if (Array.isArray(parsed)) {
72
+ allObjects = false;
73
+ } else if (typeof parsed === "object" && parsed !== null) {
74
+ allArrays = false;
75
+ } else {
76
+ allArrays = false;
77
+ allObjects = false;
78
+ }
79
+ }
80
+
81
+ if (allArrays && !allObjects) {
82
+ result.propType = "array";
83
+ // Infer inner type
84
+ let allNumbers = true;
85
+ let allStrings = true;
86
+ for (const v of validValues) {
87
+ let parsed = typeof v === "string" ? JSON.parse(v) : v;
88
+ for (const item of parsed) {
89
+ if (typeof item !== "number") allNumbers = false;
90
+ if (typeof item !== "string") allStrings = false;
91
+ }
92
+ }
93
+ const innerType = allNumbers ? "number" : allStrings ? "string" : "map";
94
+ extraLines.push(` of: { name: "${humanize(columnName)} Item", type: "${innerType}" }`);
95
+ } else {
96
+ result.propType = "map";
97
+
98
+ // Infer inner schema
99
+ if (allObjects && validValues.length > 0) {
100
+ const schema: Record<string, string> = {};
101
+ for (const v of validValues) {
102
+ let parsed = typeof v === "string" ? JSON.parse(v) : v;
103
+ for (const [k, val] of Object.entries(parsed)) {
104
+ if (val === null || val === undefined) continue;
105
+ const type = typeof val;
106
+ if (type === "string" || type === "number" || type === "boolean") {
107
+ if (!schema[k]) schema[k] = type;
108
+ else if (schema[k] !== type) schema[k] = "mixed";
109
+ } else if (Array.isArray(val)) {
110
+ if (!schema[k]) schema[k] = "array";
111
+ else if (schema[k] !== "array") schema[k] = "mixed";
112
+ } else if (type === "object") {
113
+ if (!schema[k]) schema[k] = "map";
114
+ else if (schema[k] !== "map") schema[k] = "mixed";
115
+ }
116
+ }
117
+ }
118
+
119
+ const keys = Object.keys(schema).filter(k => schema[k] !== "mixed");
120
+ if (keys.length > 0) {
121
+ const props = keys.map(k => {
122
+ return `\n ${k}: { name: "${humanize(k)}", type: "${schema[k]}" }`;
123
+ }).join(",");
124
+ extraLines.push(` properties: {${props}\n }`);
125
+ } else {
126
+ extraLines.push(` keyValue: true`);
127
+ }
128
+ } else {
129
+ extraLines.push(` keyValue: true`);
130
+ }
131
+ }
132
+ }
133
+
134
+ // ── String Analysis ──────────────────────────────────────────────────
135
+ if (currentPropType === "string") {
136
+ // Date/Time Strings
137
+ if (validValues.every(v => typeof v === 'string' && ISO_8601_REGEX.test(v))) {
138
+ result.propType = "date";
139
+ return result;
140
+ }
141
+
142
+ // Implicit Enums
143
+ const uniqueValues = new Set(validValues);
144
+ // Determine the maximum length among unique values
145
+ let maxEnumLength = 0;
146
+ for (const v of uniqueValues) {
147
+ if (typeof v === "string" && v.length > maxEnumLength) {
148
+ maxEnumLength = v.length;
149
+ }
150
+ }
151
+
152
+ // Ensure no empty string, max length makes sense, and fewer unique values than total values (unless small total)
153
+ if (uniqueValues.size > 0 && uniqueValues.size <= 5 && maxEnumLength <= 50 && validValues.length > uniqueValues.size && !uniqueValues.has("")) {
154
+ const isLikelyId = isPk || colNameLower.endsWith("_id");
155
+ if (!isLikelyId) {
156
+ const enumEntries = Array.from(uniqueValues).map(v => `{ id: ${JSON.stringify(v)}, label: ${JSON.stringify(humanize(v as string))} }`).join(", ");
157
+ extraLines.push(` enum: [${enumEntries}]`);
158
+ result.extra = extraLines.length > 0 ? "\n" + extraLines.join(",\n") + "," : "";
159
+ return result; // Skip other string checks if it's an enum
160
+ }
161
+ }
162
+
163
+ // UUID / CUID Detection
164
+ const allUuid = validValues.every(v => typeof v === 'string' && UUID_REGEX.test(v));
165
+ const allCuid = validValues.every(v => typeof v === 'string' && CUID_REGEX.test(v));
166
+ if (allUuid) {
167
+ if (isPk) extraLines.push(` isId: "uuid"`);
168
+ } else if (allCuid) {
169
+ if (isPk) extraLines.push(` isId: "cuid"`);
170
+ }
171
+
172
+ // Color Codes
173
+ const allColors = validValues.every(v => typeof v === 'string' && COLOR_HEX_REGEX.test(v));
174
+ if (allColors) {
175
+ extraLines.push(` ui: {\n color: true\n }`);
176
+ }
177
+
178
+ // Text Lengths, Multiline & Markdown
179
+ let maxLength = 0;
180
+ let hasNewlines = false;
181
+ let hasMarkdown = false;
182
+ for (const v of validValues) {
183
+ if (typeof v === "string") {
184
+ if (v.length > maxLength) maxLength = v.length;
185
+ if (v.includes("\n")) hasNewlines = true;
186
+ if (/^#{1,6}\s+.+/m.test(v) || /\*\*.+\*\*/.test(v) || /\[.+\]\(.+\)/.test(v) || /^\s*[-*]\s+.+/m.test(v)) {
187
+ hasMarkdown = true;
188
+ }
189
+ }
190
+ }
191
+
192
+ if (hasMarkdown) {
193
+ extraLines.push(` multiline: true,\n markdown: true`);
194
+ } else if (hasNewlines || maxLength > 100) {
195
+ extraLines.push(` multiline: true`);
196
+ }
197
+
198
+ if (maxLength > 0 && maxLength < 10000) { // arbitrary cap to avoid huge limits
199
+ // Pad the max length slightly for the constraint
200
+ const paddedMax = Math.ceil((maxLength * 1.5) / 10) * 10;
201
+ // Only add length constraint if it seems like a bounded string, not a long text
202
+ if (paddedMax < 255) {
203
+ extraLines.push(` validation: {\n max: ${paddedMax}\n }`);
204
+ }
205
+ }
206
+
207
+ // Storage & Media (Extracted from old logic)
208
+ const isUrl = colNameLower.endsWith("_url") || colNameLower.endsWith("_uri") || colNameLower.endsWith("_link");
209
+ const isMedia = colNameLower.includes("image") || colNameLower.includes("avatar") || colNameLower.includes("photo") || colNameLower.includes("logo") || colNameLower.includes("cover");
210
+
211
+ const allAbsoluteUrls = validValues.every(v => typeof v === 'string' && (v.startsWith("http://") || v.startsWith("https://")));
212
+ if (allAbsoluteUrls) {
213
+ const isImage = validValues.some(v => typeof v === 'string' && v.match(/\.(jpeg|jpg|gif|png|webp|svg)/i));
214
+ if (isImage || isMedia) {
215
+ extraLines.push(` ui: {\n url: "image"\n }`);
216
+ } else {
217
+ extraLines.push(` ui: {\n url: true\n }`);
218
+ }
219
+ } else {
220
+ const hasFileExtension = validValues.some(v => typeof v === 'string' && v.match(/\.[a-zA-Z0-9]+$/));
221
+ if (hasFileExtension) {
222
+ const firstVal = validValues[0] as string;
223
+ const lastSlash = firstVal.lastIndexOf('/');
224
+ let inferredStoragePath = lastSlash > 0 ? firstVal.substring(0, lastSlash) : "files";
225
+ extraLines.push(` storage: {\n storagePath: "${inferredStoragePath}"\n }`);
226
+ } else if (isUrl) {
227
+ if (isMedia) {
228
+ extraLines.push(` ui: {\n url: "image"\n }`);
229
+ } else {
230
+ extraLines.push(` ui: {\n url: true\n }`);
231
+ }
232
+ }
233
+ }
234
+ }
235
+
236
+ result.extra = extraLines.length > 0 ? "\n" + extraLines.join(",\n") + "," : "";
237
+ return result;
238
+ }