appflare 0.2.47 → 0.2.49

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.
@@ -22,6 +22,8 @@ function columnTypeIcon(type: string): string {
22
22
  return "mdi:toggle-switch-outline";
23
23
  case "date":
24
24
  return "mdi:calendar";
25
+ case "json":
26
+ return "mdi:code-braces";
25
27
  default:
26
28
  return "mdi:format-text";
27
29
  }
@@ -52,13 +54,19 @@ export function buildColumnHeaders(
52
54
  .join("");
53
55
  }
54
56
 
55
- export function buildRowCells(columns: string[], primaryKey?: string): string {
57
+ export function buildRowCells(table: DiscoveredTable, columns: string[], primaryKey?: string): string {
56
58
  return columns
57
59
  .map((column) => {
60
+ const col = table.columns.find((c) => c.name === column);
61
+
58
62
  if (primaryKey && column === primaryKey) {
59
63
  return `<td><button type="button" class="truncate max-w-[200px] text-sm font-mono text-xs opacity-70 hover:opacity-100 cursor-copy text-left" title="Click to copy: \${String((row as any).${column} ?? '')}" data-copy-value="\${String((row as any).${column} ?? '')}" onclick="navigator.clipboard?.writeText(this.dataset.copyValue || '')">\${String((row as any).${column} ?? '')}</button></td>`;
60
64
  }
61
65
 
66
+ if (col?.type === "json") {
67
+ return `<td>\${raw((() => { const __v = (row as any).${column}; if (__v === null || __v === undefined) return '<span class="opacity-30 text-xs">null</span>'; if (Array.isArray(__v)) { if (__v.length === 0) return '<span class="badge badge-ghost badge-sm font-mono text-xs">[ ]</span>'; return '<div class="flex flex-wrap gap-1 max-w-[200px]">' + __v.slice(0, 3).map((item) => '<span class="badge badge-ghost badge-xs font-mono">' + (typeof item === 'object' && item !== null ? JSON.stringify(item).slice(0, 20) + (JSON.stringify(item).length > 20 ? '…' : '') : String(item ?? '')) + '</span>').join('') + (__v.length > 3 ? '<span class="badge badge-ghost badge-xs opacity-50">+' + (__v.length - 3) + '</span>' : '') + '</div>'; } if (typeof __v === 'object') { const __s = JSON.stringify(__v); return '<span class="badge badge-outline badge-sm font-mono text-[10px]">' + __s.slice(0, 50) + (__s.length > 50 ? '…' : '') + '</span>'; } return String(__v ?? ''); })())}</td>`;
68
+ }
69
+
62
70
  return `<td><div class="truncate max-w-[200px] text-sm" title="\${String((row as any).${column} ?? '')}">\${String((row as any).${column} ?? '')}</div></td>`;
63
71
  })
64
72
  .join("");
@@ -104,6 +112,24 @@ export function buildFieldInput(
104
112
  \t\t`;
105
113
  }
106
114
 
115
+ if (column.type === "json") {
116
+ if (mode === "edit") {
117
+ return `
118
+ \t\t<div class="form-control">
119
+ \t\t\t<label class="label"><span class="label-text text-sm font-medium">${columnName}</span></label>
120
+ \t\t\t<textarea name="${columnName}" class="textarea textarea-bordered w-full font-mono text-xs h-32"${requiredAttr}>\${typeof (row as any).${columnName} === 'string' ? (row as any).${columnName} : JSON.stringify((row as any).${columnName} ?? null, null, 2)}</textarea>
121
+ \t\t</div>
122
+ \t\t`;
123
+ }
124
+
125
+ return `
126
+ \t\t<div class="form-control">
127
+ \t\t\t<label class="label"><span class="label-text text-sm font-medium">${columnName}</span></label>
128
+ \t\t\t<textarea name="${columnName}" class="textarea textarea-bordered w-full font-mono text-xs h-32" placeholder="null"${requiredAttr}></textarea>
129
+ \t\t</div>
130
+ \t\t`;
131
+ }
132
+
107
133
  if (mode === "edit") {
108
134
  const valueExpression =
109
135
  column.type === "date"
@@ -180,6 +206,20 @@ export function buildPayloadAssignments(
180
206
  \t\t`;
181
207
  }
182
208
 
209
+ if (column.type === "json") {
210
+ return `
211
+ \t\tconst raw_${field} = getValue(body['${field}']);
212
+ \t\t${requiredCheck}
213
+ \t\tif (raw_${field} !== '') {
214
+ \t\t\ttry {
215
+ \t\t\t\tpayload.${field} = JSON.parse(raw_${field});
216
+ \t\t\t} catch (e) {
217
+ \t\t\t\treturn c.text('${field} must be valid JSON', 400);
218
+ \t\t\t}
219
+ \t\t}
220
+ \t\t`;
221
+ }
222
+
183
223
  if (column.type === "date") {
184
224
  return `
185
225
  const raw_${field} = getValue(body['${field}']);
@@ -31,7 +31,7 @@ export function buildTableRoute(table: DiscoveredTable): string {
31
31
  );
32
32
  const searchConditions = buildSearchConditions(table);
33
33
  const headers = buildColumnHeaders(table, columns);
34
- const rowCells = buildRowCells(columns, primaryKey);
34
+ const rowCells = buildRowCells(table, columns, primaryKey);
35
35
  const createInputs = createColumns
36
36
  .map((columnName) => buildFieldInput(table, columnName, "create"))
37
37
  .join("");
@@ -27,6 +27,8 @@ export function handleOperationError(
27
27
  error: unknown,
28
28
  validationMessage: string,
29
29
  ): Response {
30
+ console.log(error);
31
+
30
32
  if (error instanceof AppflareHandledError) {
31
33
  return c.json(error.payload, error.status as any);
32
34
  }
@@ -15,6 +15,8 @@ import {
15
15
  isNotNull,
16
16
  getTableColumns,
17
17
  sql,
18
+ asc,
19
+ desc,
18
20
  type InferInsertModel,
19
21
  type InferSelectModel,
20
22
  type SQL,
@@ -37,8 +37,8 @@ export function generateQueryRuntimeReadSection(): string {
37
37
  (item: Record<string, unknown>) => {
38
38
  const column = item.column as string;
39
39
  const direction = (item.direction as string) ?? "asc";
40
- const col = (table as Record<string, { asc: () => unknown; desc: () => unknown }>)[column];
41
- return direction === "desc" ? col.desc() : col.asc();
40
+ const col = (table as Record<string, any>)[column];
41
+ return direction === "desc" ? desc(col) : asc(col);
42
42
  },
43
43
  )
44
44
  : undefined;
@@ -114,8 +114,8 @@ export function generateQueryRuntimeReadSection(): string {
114
114
  (item: Record<string, unknown>) => {
115
115
  const column = item.column as string;
116
116
  const direction = (item.direction as string) ?? "asc";
117
- const col = (table as Record<string, { asc: () => unknown; desc: () => unknown }>)[column];
118
- return direction === "desc" ? col.desc() : col.asc();
117
+ const col = (table as Record<string, any>)[column];
118
+ return direction === "desc" ? desc(col) : asc(col);
119
119
  },
120
120
  )
121
121
  : undefined;
@@ -155,3 +155,4 @@ export function generateQueryRuntimeReadSection(): string {
155
155
  },
156
156
  `;
157
157
  }
158
+
@@ -5,7 +5,7 @@ import type { LoadedAppflareConfig } from "../types";
5
5
  export type DiscoveredColumn = {
6
6
  name: string;
7
7
  expression: string;
8
- type: "string" | "number" | "boolean" | "date" | "unknown";
8
+ type: "string" | "number" | "boolean" | "date" | "json" | "unknown";
9
9
  optional: boolean;
10
10
  primaryKey: boolean;
11
11
  autoIncrement: boolean;
@@ -203,6 +203,9 @@ function inferColumnType(expression: string): DiscoveredColumn["type"] {
203
203
  if (/\.(int|integer|real|numeric|decimal|float|double)\s*\(/.test(lowered)) {
204
204
  return "number";
205
205
  }
206
+ if (/mode\s*:\s*["'`]json["'`]/.test(lowered) || /\.json\s*\(/.test(lowered)) {
207
+ return "json";
208
+ }
206
209
  if (/\.(text|varchar|char)\s*\(/.test(lowered)) {
207
210
  return "string";
208
211
  }