akanjs 3.0.0-alpha.71 → 3.0.0-alpha.73
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/client/cookie.ts +21 -34
- package/client/csrTypes.ts +0 -12
- package/client/frameConfig.ts +13 -0
- package/common/Logger.ts +13 -6
- package/common/TrustedProxy.ts +134 -0
- package/common/clamp.ts +2 -0
- package/common/dynamicRecord.ts +12 -0
- package/common/index.ts +5 -1
- package/common/isValidDate.ts +1 -1
- package/common/requestOrigin.ts +32 -0
- package/common/{httpClient.ts → restClient.ts} +16 -9
- package/constant/types.ts +22 -0
- package/dictionary/base.dictionary.ts +4 -0
- package/dictionary/trans.ts +24 -5
- package/document/dataLoader.ts +0 -4
- package/document/filterMeta.ts +2 -2
- package/document/loaderInfo.ts +4 -1
- package/fetch/client/fetchClient.ts +65 -88
- package/fetch/client/fetchHandle.ts +57 -0
- package/fetch/client/httpClient.ts +42 -14
- package/fetch/client/sliceInitHandle.ts +101 -0
- package/fetch/fetchType/appliedReturn.type.ts +64 -1
- package/fetch/fetchType/sliceFetch.type.ts +5 -5
- package/fetch/requestStorage.ts +19 -5
- package/index.ts +16 -2
- package/local/apps/serverLifecycle/serverLifecycle-local.db-shm +0 -0
- package/local/apps/serverLifecycle/serverLifecycle-local_solid.db-shm +0 -0
- package/package.json +1 -1
- package/server/akanApp.ts +3 -19
- package/server/akanAppHeaders.ts +10 -4
- package/server/akanOption.ts +14 -1
- package/server/akanServer.ts +3 -0
- package/server/cachePolicy.ts +2 -11
- package/server/decorators.ts +12 -3
- package/server/devtools/constantSerializer.ts +8 -1
- package/server/di/diLifecycle.ts +21 -1
- package/server/di/resolveHierarchy.ts +3 -3
- package/server/imageOptimizer.ts +2 -17
- package/server/mcp/McpDispatcher.ts +8 -2
- package/server/resolver/database.resolver.ts +13 -8
- package/server/resolver/signal.resolver.ts +3 -3
- package/server/routeTreeBuilder.ts +1 -15
- package/server/routing/apiRouter.ts +4 -5
- package/server/routing/appWsData.ts +9 -6
- package/server/staticPath.ts +34 -0
- package/server/webRouter.ts +35 -41
- package/service/adapt.ts +1 -5
- package/service/predefinedAdaptor/cache.adaptor.ts +8 -4
- package/service/predefinedAdaptor/database.adaptor.ts +37 -1766
- package/service/predefinedAdaptor/sql/PendingStoreEnsures.ts +22 -0
- package/service/predefinedAdaptor/sql/QueryCompiler.ts +225 -0
- package/service/predefinedAdaptor/sql/SqlDocumentStore.ts +812 -0
- package/service/predefinedAdaptor/sql/UpdateCompiler.ts +67 -0
- package/service/predefinedAdaptor/sql/dialect/postgres.ts +151 -0
- package/service/predefinedAdaptor/sql/dialect/sqlite.ts +134 -0
- package/service/predefinedAdaptor/sql/driver/bunSqlite.ts +29 -0
- package/service/predefinedAdaptor/sql/driver/libsql.ts +37 -0
- package/service/predefinedAdaptor/sql/driver/postgres.ts +42 -0
- package/service/predefinedAdaptor/sql/types.ts +251 -0
- package/service/predefinedAdaptor/sql/values.ts +91 -0
- package/signal/CrossSiteGuard.ts +71 -0
- package/signal/SignalFailure.ts +51 -0
- package/signal/endpoint.ts +2 -2
- package/signal/exception.ts +43 -6
- package/signal/index.ts +2 -0
- package/signal/intercept.ts +5 -2
- package/signal/internal.ts +10 -2
- package/signal/serializer/fetch.serializer.ts +11 -1
- package/signal/signalContext.ts +25 -28
- package/signal/slice.ts +1 -1
- package/store/SliceRequest.ts +23 -0
- package/store/action.ts +177 -114
- package/store/agent/AgentBridge.ts +3 -1
- package/store/agentic/StExposeBuilder.ts +7 -4
- package/store/agentic/StStateBuilder.ts +9 -4
- package/store/agentic/StToolBuilder.ts +21 -7
- package/store/store.ts +1 -1
- package/store/storeInstance.ts +3 -3
- package/store/storeRegistry.ts +1 -1
- package/types/client/cookie.d.ts +13 -2
- package/types/client/csrTypes.d.ts +0 -1
- package/types/client/frameConfig.d.ts +1 -0
- package/types/common/TrustedProxy.d.ts +24 -0
- package/types/common/clamp.d.ts +2 -0
- package/types/common/dynamicRecord.d.ts +14 -0
- package/types/common/index.d.ts +5 -1
- package/types/common/requestOrigin.d.ts +14 -0
- package/types/common/restClient.d.ts +27 -0
- package/types/constant/types.d.ts +13 -0
- package/types/dictionary/base.dictionary.d.ts +1 -1
- package/types/dictionary/dictionary.d.ts +8 -8
- package/types/document/dataLoader.d.ts +0 -3
- package/types/document/filterMeta.d.ts +2 -2
- package/types/document/loaderInfo.d.ts +2 -2
- package/types/fetch/client/fetchHandle.d.ts +24 -0
- package/types/fetch/client/httpClient.d.ts +2 -0
- package/types/fetch/client/sliceInitHandle.d.ts +29 -0
- package/types/fetch/fetchType/appliedReturn.type.d.ts +38 -1
- package/types/fetch/fetchType/sliceFetch.type.d.ts +5 -5
- package/types/fetch/requestStorage.d.ts +14 -0
- package/types/index.d.ts +17 -2
- package/types/server/akanOption.d.ts +8 -1
- package/types/server/decorators.d.ts +12 -2
- package/types/server/di/diLifecycle.d.ts +9 -1
- package/types/server/routeTreeBuilder.d.ts +1 -2
- package/types/server/routing/appWsData.d.ts +7 -3
- package/types/server/staticPath.d.ts +17 -0
- package/types/service/adapt.d.ts +1 -1
- package/types/service/predefinedAdaptor/database.adaptor.d.ts +15 -407
- package/types/service/predefinedAdaptor/sql/PendingStoreEnsures.d.ts +6 -0
- package/types/service/predefinedAdaptor/sql/QueryCompiler.d.ts +28 -0
- package/types/service/predefinedAdaptor/sql/SqlDocumentStore.d.ts +127 -0
- package/types/service/predefinedAdaptor/sql/UpdateCompiler.d.ts +17 -0
- package/types/service/predefinedAdaptor/sql/dialect/postgres.d.ts +27 -0
- package/types/service/predefinedAdaptor/sql/dialect/sqlite.d.ts +27 -0
- package/types/service/predefinedAdaptor/sql/driver/bunSqlite.d.ts +16 -0
- package/types/service/predefinedAdaptor/sql/driver/libsql.d.ts +17 -0
- package/types/service/predefinedAdaptor/sql/driver/postgres.d.ts +17 -0
- package/types/service/predefinedAdaptor/sql/types.d.ts +226 -0
- package/types/service/predefinedAdaptor/sql/values.d.ts +13 -0
- package/types/signal/CrossSiteGuard.d.ts +44 -0
- package/types/signal/SignalFailure.d.ts +28 -0
- package/types/signal/exception.d.ts +38 -54774
- package/types/signal/index.d.ts +2 -0
- package/types/signal/intercept.d.ts +3 -3
- package/types/signal/internal.d.ts +5 -1
- package/types/signal/serializer/fetch.serializer.d.ts +7 -1
- package/types/store/SliceRequest.d.ts +16 -0
- package/types/store/agentic/StToolBuilder.d.ts +10 -1
- package/types/ui/Agent/Menu.d.ts +1 -1
- package/types/ui/Field/Choice.d.ts +52 -0
- package/types/ui/Field/Collection.d.ts +51 -0
- package/types/ui/Field/DateField.d.ts +32 -0
- package/types/ui/Field/Label.d.ts +9 -0
- package/types/ui/Field/NumberField.d.ts +43 -0
- package/types/ui/Field/Relation.d.ts +80 -0
- package/types/ui/Field/Text.d.ts +101 -0
- package/types/ui/Field/index.d.ts +50 -0
- package/types/ui/Field.d.ts +1 -412
- package/types/ui/Load/Edit_Client.d.ts +3 -1
- package/types/ui/Load/Stream.d.ts +22 -0
- package/types/ui/Load/index.d.ts +2 -0
- package/types/ui/System/Root.d.ts +4 -0
- package/types/ui/index.d.ts +1 -1
- package/types/ui/recipe/badgeRecipe.d.ts +2 -2
- package/types/ui/recipe/buttonRecipe.d.ts +2 -2
- package/types/webkit/useCamera.d.ts +10 -1
- package/types/webkit/usePurchase.d.ts +4 -0
- package/ui/Agent/Menu.tsx +2 -2
- package/ui/Data/TableList.tsx +1 -1
- package/ui/DraggableList.tsx +1 -2
- package/ui/Field/Choice.tsx +172 -0
- package/ui/Field/Collection.tsx +293 -0
- package/ui/Field/DateField.tsx +138 -0
- package/ui/Field/Label.tsx +31 -0
- package/ui/Field/NumberField.tsx +169 -0
- package/ui/Field/Relation.tsx +387 -0
- package/ui/Field/Text.tsx +360 -0
- package/ui/Field/index.tsx +70 -0
- package/ui/Input.tsx +3 -3
- package/ui/Load/Edit.tsx +2 -2
- package/ui/Load/Edit_Client.tsx +11 -3
- package/ui/Load/Pagination.tsx +25 -25
- package/ui/Load/Stream.tsx +38 -0
- package/ui/Load/Units.tsx +50 -37
- package/ui/Load/View.tsx +25 -17
- package/ui/Load/index.ts +2 -0
- package/ui/Model/EditModal.tsx +7 -7
- package/ui/Signal/Message.tsx +7 -7
- package/ui/Signal/PubSub.tsx +4 -1
- package/ui/System/Client.tsx +0 -2
- package/ui/System/Gtag.tsx +1 -1
- package/ui/System/Root.tsx +4 -0
- package/ui/index.ts +1 -1
- package/ui/overlayPosition.ts +1 -2
- package/webkit/useCamera.tsx +22 -6
- package/webkit/useCodepush.tsx +2 -2
- package/webkit/useCsrValues.ts +1 -3
- package/webkit/useFetch.ts +4 -3
- package/webkit/useFrameRuntime.ts +14 -7
- package/webkit/usePurchase.tsx +4 -1
- package/fetch/serializer/fetch.serializer.ts +0 -142
- package/fetch/serializer/index.ts +0 -1
- package/types/common/httpClient.d.ts +0 -20
- package/types/fetch/serializer/fetch.serializer.d.ts +0 -14
- package/types/fetch/serializer/index.d.ts +0 -1
- package/types/fetch/serializer.d.ts +0 -1
- package/ui/Field.tsx +0 -1639
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import {
|
|
2
|
+
type DocumentUpdate,
|
|
3
|
+
type DocumentUpdateNode,
|
|
4
|
+
type DocumentUpdateOperator,
|
|
5
|
+
isDocumentUpdateNode,
|
|
6
|
+
} from "akanjs/document";
|
|
7
|
+
import { quoteIdent } from "../sqlDescriptor";
|
|
8
|
+
import { BASE_COLUMNS, type FieldMap, type SqlDialect } from "./types";
|
|
9
|
+
import { encodeSqlValue } from "./values";
|
|
10
|
+
|
|
11
|
+
export class UpdateCompiler {
|
|
12
|
+
constructor(
|
|
13
|
+
private readonly fields: FieldMap,
|
|
14
|
+
private readonly dialect: SqlDialect,
|
|
15
|
+
) {}
|
|
16
|
+
|
|
17
|
+
compile(update: DocumentUpdate): { assignments: string[]; params: unknown[]; setOnInsert: Record<string, unknown> } {
|
|
18
|
+
const baseAssignments: string[] = [];
|
|
19
|
+
const baseParams: unknown[] = [];
|
|
20
|
+
const setOnInsert: Record<string, unknown> = {};
|
|
21
|
+
const jsonOps: { op: DocumentUpdateOperator; path: string; value: unknown }[] = [];
|
|
22
|
+
for (const [path, raw] of Object.entries(update)) {
|
|
23
|
+
if (raw === undefined) continue;
|
|
24
|
+
const node: DocumentUpdateNode = isDocumentUpdateNode(raw) ? raw : { kind: "update", op: "set", value: raw };
|
|
25
|
+
this.#assertPath(path);
|
|
26
|
+
if (node.op === "setOnInsert") {
|
|
27
|
+
setOnInsert[path] = node.value;
|
|
28
|
+
continue;
|
|
29
|
+
}
|
|
30
|
+
if (BASE_COLUMNS.has(path)) {
|
|
31
|
+
if (node.op === "set") {
|
|
32
|
+
baseAssignments.push(`${quoteIdent(path)} = ?`);
|
|
33
|
+
baseParams.push(encodeSqlValue(node.value));
|
|
34
|
+
} else if (node.op === "unset") {
|
|
35
|
+
baseAssignments.push(`${quoteIdent(path)} = NULL`);
|
|
36
|
+
} else {
|
|
37
|
+
throw new Error(`Unsupported update operator '${node.op}' on base column: ${path}`);
|
|
38
|
+
}
|
|
39
|
+
continue;
|
|
40
|
+
}
|
|
41
|
+
jsonOps.push({ op: node.op, path, value: node.value });
|
|
42
|
+
}
|
|
43
|
+
const assignments = [...baseAssignments];
|
|
44
|
+
const params = [...baseParams];
|
|
45
|
+
if (jsonOps.length) {
|
|
46
|
+
let acc = this.dialect.docColumn();
|
|
47
|
+
for (const { op, path, value } of jsonOps) {
|
|
48
|
+
const frag = this.dialect.applyUpdate(acc, op, path, value);
|
|
49
|
+
acc = frag.sql;
|
|
50
|
+
params.push(...frag.params);
|
|
51
|
+
}
|
|
52
|
+
assignments.push(`${this.dialect.docColumn()} = ${acc}`);
|
|
53
|
+
}
|
|
54
|
+
return { assignments, params, setOnInsert };
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
#assertPath(path: string) {
|
|
58
|
+
const root = path.split(".")[0];
|
|
59
|
+
if (BASE_COLUMNS.has(root)) return;
|
|
60
|
+
if (!this.fields[root]) throw new Error(`Unknown document field path: ${path}`);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Per-document modification state, attached only when a document is hydrated for writing.
|
|
66
|
+
* Non-enumerable so `{ ...doc }` in `toRow` and `Object.entries` in `sanitizeJson` never see it.
|
|
67
|
+
*/
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
import type { DocumentUpdateOperator } from "akanjs/document";
|
|
2
|
+
import { quoteIdent } from "../../sqlDescriptor";
|
|
3
|
+
import type { SearchJoinProps, SqlDialect, SqlFrag } from "../types";
|
|
4
|
+
import { jsonStr } from "../values";
|
|
5
|
+
|
|
6
|
+
export class PostgresDialect implements SqlDialect {
|
|
7
|
+
readonly name = "postgres" as const;
|
|
8
|
+
timestampType() {
|
|
9
|
+
return "BIGINT";
|
|
10
|
+
}
|
|
11
|
+
docColumnType() {
|
|
12
|
+
return "jsonb";
|
|
13
|
+
}
|
|
14
|
+
docColumn() {
|
|
15
|
+
return quoteIdent("_doc");
|
|
16
|
+
}
|
|
17
|
+
docValuePlaceholder() {
|
|
18
|
+
return "?::jsonb";
|
|
19
|
+
}
|
|
20
|
+
#path(path: string) {
|
|
21
|
+
return `'{${path
|
|
22
|
+
.split(".")
|
|
23
|
+
.map((part) => part.replaceAll("'", "''"))
|
|
24
|
+
.join(",")}}'`;
|
|
25
|
+
}
|
|
26
|
+
#jsonb(path: string) {
|
|
27
|
+
return `(${this.docColumn()} #> ${this.#path(path)})`;
|
|
28
|
+
}
|
|
29
|
+
#text(path: string) {
|
|
30
|
+
return `(${this.docColumn()} #>> ${this.#path(path)})`;
|
|
31
|
+
}
|
|
32
|
+
extract(path: string) {
|
|
33
|
+
return this.#jsonb(path);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
projectExpr(path: string) {
|
|
37
|
+
return this.#jsonb(path);
|
|
38
|
+
}
|
|
39
|
+
decodeProjected(value: unknown) {
|
|
40
|
+
return value;
|
|
41
|
+
}
|
|
42
|
+
eq(path: string, value: unknown): SqlFrag {
|
|
43
|
+
return value === null
|
|
44
|
+
? { sql: `${this.#jsonb(path)} IS NULL`, params: [] }
|
|
45
|
+
: { sql: `${this.#jsonb(path)} = ?::jsonb`, params: [jsonStr(value)] };
|
|
46
|
+
}
|
|
47
|
+
ne(path: string, value: unknown): SqlFrag {
|
|
48
|
+
return value === null
|
|
49
|
+
? { sql: `${this.#jsonb(path)} IS NOT NULL`, params: [] }
|
|
50
|
+
: { sql: `${this.#jsonb(path)} <> ?::jsonb`, params: [jsonStr(value)] };
|
|
51
|
+
}
|
|
52
|
+
compare(path: string, op: "gt" | "gte" | "lt" | "lte", value: unknown): SqlFrag {
|
|
53
|
+
const operators = { gt: ">", gte: ">=", lt: "<", lte: "<=" } as const;
|
|
54
|
+
return { sql: `${this.#jsonb(path)} ${operators[op]} ?::jsonb`, params: [jsonStr(value)] };
|
|
55
|
+
}
|
|
56
|
+
between(path: string, from: unknown, to: unknown): SqlFrag {
|
|
57
|
+
return {
|
|
58
|
+
sql: `(${this.#jsonb(path)} >= ?::jsonb AND ${this.#jsonb(path)} <= ?::jsonb)`,
|
|
59
|
+
params: [jsonStr(from), jsonStr(to)],
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
inList(path: string, values: unknown[]): SqlFrag {
|
|
63
|
+
return {
|
|
64
|
+
sql: `${this.#jsonb(path)} IN (${values.map(() => "?::jsonb").join(", ")})`,
|
|
65
|
+
params: values.map(jsonStr),
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
notInList(path: string, values: unknown[]): SqlFrag {
|
|
69
|
+
return {
|
|
70
|
+
sql: `${this.#jsonb(path)} NOT IN (${values.map(() => "?::jsonb").join(", ")})`,
|
|
71
|
+
params: values.map(jsonStr),
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
exists(path: string): SqlFrag {
|
|
75
|
+
return { sql: `${this.#jsonb(path)} IS NOT NULL`, params: [] };
|
|
76
|
+
}
|
|
77
|
+
missing(path: string): SqlFrag {
|
|
78
|
+
return { sql: `${this.#jsonb(path)} IS NULL`, params: [] };
|
|
79
|
+
}
|
|
80
|
+
empty(path: string): SqlFrag {
|
|
81
|
+
return { sql: `(${this.#jsonb(path)} IS NULL OR jsonb_typeof(${this.#jsonb(path)}) = 'null')`, params: [] };
|
|
82
|
+
}
|
|
83
|
+
arrayHas(path: string, value: unknown): SqlFrag {
|
|
84
|
+
return { sql: `${this.#jsonb(path)} @> ?::jsonb`, params: [jsonStr(value)] };
|
|
85
|
+
}
|
|
86
|
+
contains(path: string, value: unknown): SqlFrag {
|
|
87
|
+
return { sql: `${this.#text(path)} LIKE ?`, params: [`%${String(value)}%`] };
|
|
88
|
+
}
|
|
89
|
+
searchJoin({ ref }: SearchJoinProps): SqlFrag {
|
|
90
|
+
|
|
91
|
+
throw new Error(
|
|
92
|
+
`Text search on "${ref}" requires the sqlite or libsql database; Postgres has no fts5 index to join.`,
|
|
93
|
+
);
|
|
94
|
+
}
|
|
95
|
+
applyUpdate(acc: string, op: DocumentUpdateOperator, path: string, value: unknown): SqlFrag {
|
|
96
|
+
const p = this.#path(path);
|
|
97
|
+
|
|
98
|
+
const jsonbAt = `(${this.docColumn()}) #> ${p}`;
|
|
99
|
+
const textAt = `(${this.docColumn()}) #>> ${p}`;
|
|
100
|
+
const arr = `COALESCE(${jsonbAt}, '[]'::jsonb)`;
|
|
101
|
+
|
|
102
|
+
switch (op) {
|
|
103
|
+
case "set":
|
|
104
|
+
return { sql: `jsonb_set(${acc}, ${p}, ?::jsonb, true)`, params: [jsonStr(value)] };
|
|
105
|
+
case "unset":
|
|
106
|
+
return { sql: `(${acc}) #- ${p}`, params: [] };
|
|
107
|
+
case "inc":
|
|
108
|
+
return {
|
|
109
|
+
sql: `jsonb_set(${acc}, ${p}, to_jsonb(COALESCE((${textAt})::numeric, 0) + ?), true)`,
|
|
110
|
+
params: [Number(value)],
|
|
111
|
+
};
|
|
112
|
+
case "mul":
|
|
113
|
+
return {
|
|
114
|
+
sql: `jsonb_set(${acc}, ${p}, to_jsonb(COALESCE((${textAt})::numeric, 0) * ?), true)`,
|
|
115
|
+
params: [Number(value)],
|
|
116
|
+
};
|
|
117
|
+
case "min":
|
|
118
|
+
return {
|
|
119
|
+
sql: `jsonb_set(${acc}, ${p}, to_jsonb(LEAST(COALESCE((${textAt})::numeric, ?), ?)), true)`,
|
|
120
|
+
params: [Number(value), Number(value)],
|
|
121
|
+
};
|
|
122
|
+
case "max":
|
|
123
|
+
return {
|
|
124
|
+
sql: `jsonb_set(${acc}, ${p}, to_jsonb(GREATEST(COALESCE((${textAt})::numeric, ?), ?)), true)`,
|
|
125
|
+
params: [Number(value), Number(value)],
|
|
126
|
+
};
|
|
127
|
+
case "push":
|
|
128
|
+
return {
|
|
129
|
+
sql: `jsonb_set(${acc}, ${p}, ${arr} || jsonb_build_array(?::jsonb), true)`,
|
|
130
|
+
params: [jsonStr(value)],
|
|
131
|
+
};
|
|
132
|
+
case "addToSet":
|
|
133
|
+
return {
|
|
134
|
+
sql: `jsonb_set(${acc}, ${p}, CASE WHEN ${arr} @> jsonb_build_array(?::jsonb) THEN ${arr} ELSE ${arr} || jsonb_build_array(?::jsonb) END, true)`,
|
|
135
|
+
params: [jsonStr(value), jsonStr(value)],
|
|
136
|
+
};
|
|
137
|
+
case "pull":
|
|
138
|
+
return {
|
|
139
|
+
sql: `jsonb_set(${acc}, ${p}, COALESCE((SELECT jsonb_agg(elem) FROM jsonb_array_elements(${arr}) elem WHERE elem <> ?::jsonb), '[]'::jsonb), true)`,
|
|
140
|
+
params: [jsonStr(value)],
|
|
141
|
+
};
|
|
142
|
+
case "setOnInsert":
|
|
143
|
+
return { sql: acc, params: [] };
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
affectedRows(result: unknown): number {
|
|
147
|
+
const row = result as { count?: number } | Array<unknown> | null;
|
|
148
|
+
if (Array.isArray(row)) return (row as { count?: number }).count ?? row.length;
|
|
149
|
+
return Number(row?.count ?? 0);
|
|
150
|
+
}
|
|
151
|
+
}
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
import type { DocumentUpdateOperator } from "akanjs/document";
|
|
2
|
+
import { DOC_TABLE, FTS_TABLE } from "../../searchIndex";
|
|
3
|
+
import { jsonPath, quoteIdent } from "../../sqlDescriptor";
|
|
4
|
+
import type { SearchJoinProps, SqlDialect, SqlFrag } from "../types";
|
|
5
|
+
import { encodeSqlValue, jsonStr } from "../values";
|
|
6
|
+
|
|
7
|
+
export class SqliteDialect implements SqlDialect {
|
|
8
|
+
readonly name = "sqlite" as const;
|
|
9
|
+
timestampType() {
|
|
10
|
+
return "INTEGER";
|
|
11
|
+
}
|
|
12
|
+
docColumnType() {
|
|
13
|
+
return "TEXT";
|
|
14
|
+
}
|
|
15
|
+
docColumn() {
|
|
16
|
+
return quoteIdent("_doc");
|
|
17
|
+
}
|
|
18
|
+
docValuePlaceholder() {
|
|
19
|
+
return "?";
|
|
20
|
+
}
|
|
21
|
+
#path(path: string) {
|
|
22
|
+
return `'${jsonPath(path).replaceAll("'", "''")}'`;
|
|
23
|
+
}
|
|
24
|
+
extract(path: string) {
|
|
25
|
+
return `json_extract(${this.docColumn()}, ${this.#path(path)})`;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
projectExpr(path: string) {
|
|
29
|
+
return `${this.docColumn()} -> ${this.#path(path)}`;
|
|
30
|
+
}
|
|
31
|
+
decodeProjected(value: unknown) {
|
|
32
|
+
return typeof value === "string" ? JSON.parse(value) : value;
|
|
33
|
+
}
|
|
34
|
+
eq(path: string, value: unknown): SqlFrag {
|
|
35
|
+
return value === null
|
|
36
|
+
? { sql: `${this.extract(path)} IS NULL`, params: [] }
|
|
37
|
+
: { sql: `${this.extract(path)} = ?`, params: [encodeSqlValue(value)] };
|
|
38
|
+
}
|
|
39
|
+
ne(path: string, value: unknown): SqlFrag {
|
|
40
|
+
return value === null
|
|
41
|
+
? { sql: `${this.extract(path)} IS NOT NULL`, params: [] }
|
|
42
|
+
: { sql: `${this.extract(path)} != ?`, params: [encodeSqlValue(value)] };
|
|
43
|
+
}
|
|
44
|
+
compare(path: string, op: "gt" | "gte" | "lt" | "lte", value: unknown): SqlFrag {
|
|
45
|
+
const operators = { gt: ">", gte: ">=", lt: "<", lte: "<=" } as const;
|
|
46
|
+
return { sql: `${this.extract(path)} ${operators[op]} ?`, params: [encodeSqlValue(value)] };
|
|
47
|
+
}
|
|
48
|
+
between(path: string, from: unknown, to: unknown): SqlFrag {
|
|
49
|
+
return {
|
|
50
|
+
sql: `(${this.extract(path)} >= ? AND ${this.extract(path)} <= ?)`,
|
|
51
|
+
params: [encodeSqlValue(from), encodeSqlValue(to)],
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
inList(path: string, values: unknown[]): SqlFrag {
|
|
55
|
+
return {
|
|
56
|
+
sql: `${this.extract(path)} IN (${values.map(() => "?").join(", ")})`,
|
|
57
|
+
params: values.map(encodeSqlValue),
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
notInList(path: string, values: unknown[]): SqlFrag {
|
|
61
|
+
return {
|
|
62
|
+
sql: `${this.extract(path)} NOT IN (${values.map(() => "?").join(", ")})`,
|
|
63
|
+
params: values.map(encodeSqlValue),
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
exists(path: string): SqlFrag {
|
|
67
|
+
return { sql: `json_type(${this.docColumn()}, ${this.#path(path)}) IS NOT NULL`, params: [] };
|
|
68
|
+
}
|
|
69
|
+
missing(path: string): SqlFrag {
|
|
70
|
+
return { sql: `json_type(${this.docColumn()}, ${this.#path(path)}) IS NULL`, params: [] };
|
|
71
|
+
}
|
|
72
|
+
empty(path: string): SqlFrag {
|
|
73
|
+
const type = `json_type(${this.docColumn()}, ${this.#path(path)})`;
|
|
74
|
+
return { sql: `(${type} IS NULL OR ${type} = 'null')`, params: [] };
|
|
75
|
+
}
|
|
76
|
+
arrayHas(path: string, value: unknown): SqlFrag {
|
|
77
|
+
return {
|
|
78
|
+
sql: `EXISTS (SELECT 1 FROM json_each(${this.extract(path)}) WHERE json_each.value = ?)`,
|
|
79
|
+
params: [encodeSqlValue(value)],
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
contains(path: string, value: unknown): SqlFrag {
|
|
83
|
+
return { sql: `${this.extract(path)} LIKE ?`, params: [`%${String(value)}%`] };
|
|
84
|
+
}
|
|
85
|
+
searchJoin({ alias, ref, match, weights }: SearchJoinProps): SqlFrag {
|
|
86
|
+
|
|
87
|
+
return {
|
|
88
|
+
sql:
|
|
89
|
+
`JOIN (SELECT d."refId" AS rid, bm25(${FTS_TABLE}, ${weights.join(", ")}) AS score ` +
|
|
90
|
+
`FROM ${FTS_TABLE} JOIN ${DOC_TABLE} d ON d."fid" = ${FTS_TABLE}."rowid" ` +
|
|
91
|
+
`WHERE ${FTS_TABLE} MATCH ? AND d."ref" = ?) ${alias} ON ${alias}."rid" = ${quoteIdent(ref)}."id"`,
|
|
92
|
+
params: [match, ref],
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
applyUpdate(acc: string, op: DocumentUpdateOperator, path: string, value: unknown): SqlFrag {
|
|
96
|
+
const p = this.#path(path);
|
|
97
|
+
|
|
98
|
+
const cur = `json_extract(${this.docColumn()}, ${p})`;
|
|
99
|
+
const arr = `COALESCE(${cur}, json('[]'))`;
|
|
100
|
+
|
|
101
|
+
switch (op) {
|
|
102
|
+
case "set":
|
|
103
|
+
return { sql: `json_set(${acc}, ${p}, json(?))`, params: [jsonStr(value)] };
|
|
104
|
+
case "unset":
|
|
105
|
+
return { sql: `json_remove(${acc}, ${p})`, params: [] };
|
|
106
|
+
case "inc":
|
|
107
|
+
return { sql: `json_set(${acc}, ${p}, COALESCE(${cur}, 0) + ?)`, params: [Number(value)] };
|
|
108
|
+
case "mul":
|
|
109
|
+
return { sql: `json_set(${acc}, ${p}, COALESCE(${cur}, 0) * ?)`, params: [Number(value)] };
|
|
110
|
+
case "min":
|
|
111
|
+
return { sql: `json_set(${acc}, ${p}, MIN(COALESCE(${cur}, ?), ?))`, params: [Number(value), Number(value)] };
|
|
112
|
+
case "max":
|
|
113
|
+
return { sql: `json_set(${acc}, ${p}, MAX(COALESCE(${cur}, ?), ?))`, params: [Number(value), Number(value)] };
|
|
114
|
+
case "push":
|
|
115
|
+
return { sql: `json_set(${acc}, ${p}, json_insert(${arr}, '$[#]', json(?)))`, params: [jsonStr(value)] };
|
|
116
|
+
case "addToSet":
|
|
117
|
+
return {
|
|
118
|
+
sql: `json_set(${acc}, ${p}, CASE WHEN EXISTS (SELECT 1 FROM json_each(${arr}) WHERE json_each.value = ?) THEN ${arr} ELSE json_insert(${arr}, '$[#]', json(?)) END)`,
|
|
119
|
+
params: [encodeSqlValue(value), jsonStr(value)],
|
|
120
|
+
};
|
|
121
|
+
case "pull":
|
|
122
|
+
return {
|
|
123
|
+
sql: `json_set(${acc}, ${p}, (SELECT json_group_array(json_each.value) FROM json_each(${arr}) WHERE json_each.value <> ?))`,
|
|
124
|
+
params: [encodeSqlValue(value)],
|
|
125
|
+
};
|
|
126
|
+
case "setOnInsert":
|
|
127
|
+
return { sql: acc, params: [] };
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
affectedRows(result: unknown): number {
|
|
131
|
+
const row = result as { changes?: number | bigint; rowsAffected?: number } | null;
|
|
132
|
+
return Number(row?.changes ?? row?.rowsAffected ?? 0);
|
|
133
|
+
}
|
|
134
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import type { Database, SQLQueryBindings, Statement } from "bun:sqlite";
|
|
2
|
+
import type { AkanSqlClient, AkanSqlStatement } from "../types";
|
|
3
|
+
|
|
4
|
+
export class BunSqliteStatement implements AkanSqlStatement {
|
|
5
|
+
constructor(private readonly statement: Statement) {}
|
|
6
|
+
async run(...params: unknown[]) {
|
|
7
|
+
return this.statement.run(...(params as SQLQueryBindings[]));
|
|
8
|
+
}
|
|
9
|
+
async get<Row = Record<string, unknown>>(...params: unknown[]): Promise<Row | null> {
|
|
10
|
+
return (this.statement.get(...(params as SQLQueryBindings[])) as Row | null) ?? null;
|
|
11
|
+
}
|
|
12
|
+
async all<Row = Record<string, unknown>>(...params: unknown[]): Promise<Row[]> {
|
|
13
|
+
return this.statement.all(...(params as SQLQueryBindings[])) as Row[];
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export class BunSqliteClient implements AkanSqlClient {
|
|
18
|
+
constructor(readonly db: Database) {}
|
|
19
|
+
async execute(sql: string, params: unknown[] | Record<string, unknown> = []) {
|
|
20
|
+
const values = Array.isArray(params) ? params : Object.values(params);
|
|
21
|
+
return this.db.query(sql).run(...(values as SQLQueryBindings[]));
|
|
22
|
+
}
|
|
23
|
+
prepare(sql: string): AkanSqlStatement {
|
|
24
|
+
return new BunSqliteStatement(this.db.query(sql));
|
|
25
|
+
}
|
|
26
|
+
async close() {
|
|
27
|
+
this.db.close();
|
|
28
|
+
}
|
|
29
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import type { Client as LibsqlClient } from "@libsql/client";
|
|
2
|
+
import type { AkanSqlClient, AkanSqlStatement } from "../types";
|
|
3
|
+
import { toLibsqlArgs } from "../values";
|
|
4
|
+
|
|
5
|
+
export class LibsqlStatement implements AkanSqlStatement {
|
|
6
|
+
constructor(
|
|
7
|
+
private readonly client: LibsqlClient,
|
|
8
|
+
private readonly sql: string,
|
|
9
|
+
) {}
|
|
10
|
+
async run(...params: unknown[]) {
|
|
11
|
+
const args = toLibsqlArgs(params);
|
|
12
|
+
return await this.client.execute({ sql: this.sql, args });
|
|
13
|
+
}
|
|
14
|
+
async get<Row = Record<string, unknown>>(...params: unknown[]): Promise<Row | null> {
|
|
15
|
+
const args = toLibsqlArgs(params);
|
|
16
|
+
const result = await this.client.execute({ sql: this.sql, args });
|
|
17
|
+
return (result.rows[0] as Row | undefined) ?? null;
|
|
18
|
+
}
|
|
19
|
+
async all<Row = Record<string, unknown>>(...params: unknown[]): Promise<Row[]> {
|
|
20
|
+
const args = toLibsqlArgs(params);
|
|
21
|
+
const result = await this.client.execute({ sql: this.sql, args });
|
|
22
|
+
return result.rows as Row[];
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export class LibsqlAkanClient implements AkanSqlClient {
|
|
27
|
+
constructor(readonly client: LibsqlClient) {}
|
|
28
|
+
async execute(sql: string, params: unknown[] | Record<string, unknown> = []) {
|
|
29
|
+
return await this.client.execute({ sql, args: toLibsqlArgs(Array.isArray(params) ? params : [params]) });
|
|
30
|
+
}
|
|
31
|
+
prepare(sql: string): AkanSqlStatement {
|
|
32
|
+
return new LibsqlStatement(this.client, sql);
|
|
33
|
+
}
|
|
34
|
+
async close() {
|
|
35
|
+
this.client.close();
|
|
36
|
+
}
|
|
37
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import type { Sql } from "postgres";
|
|
2
|
+
|
|
3
|
+
/** `unsafe` takes its own parameter union; everything reaching here was already SQL-encoded upstream. */
|
|
4
|
+
const bindings = (params: unknown[] | Record<string, unknown>) => {
|
|
5
|
+
const values = Array.isArray(params) ? params : Object.values(params);
|
|
6
|
+
return values as NonNullable<Parameters<Sql["unsafe"]>[1]>;
|
|
7
|
+
};
|
|
8
|
+
|
|
9
|
+
import type { AkanSqlClient, AkanSqlStatement } from "../types";
|
|
10
|
+
import { toPostgresSql } from "../values";
|
|
11
|
+
|
|
12
|
+
export class PostgresStatement implements AkanSqlStatement {
|
|
13
|
+
constructor(
|
|
14
|
+
private readonly client: Sql,
|
|
15
|
+
private readonly sql: string,
|
|
16
|
+
) {}
|
|
17
|
+
async run(...params: unknown[]) {
|
|
18
|
+
const { sql, params: positionalParams } = toPostgresSql(this.sql, params);
|
|
19
|
+
return await this.client.unsafe(sql, bindings(positionalParams));
|
|
20
|
+
}
|
|
21
|
+
async get<Row = Record<string, unknown>>(...params: unknown[]): Promise<Row | null> {
|
|
22
|
+
const rows = await this.all<Row>(...params);
|
|
23
|
+
return rows[0] ?? null;
|
|
24
|
+
}
|
|
25
|
+
async all<Row = Record<string, unknown>>(...params: unknown[]): Promise<Row[]> {
|
|
26
|
+
const { sql, params: positionalParams } = toPostgresSql(this.sql, params);
|
|
27
|
+
return (await this.client.unsafe(sql, bindings(positionalParams))) as Row[];
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export class PostgresAkanClient implements AkanSqlClient {
|
|
32
|
+
constructor(readonly client: Sql) {}
|
|
33
|
+
async execute(sql: string, params: unknown[] | Record<string, unknown> = []) {
|
|
34
|
+
return await this.client.unsafe(sql, bindings(params));
|
|
35
|
+
}
|
|
36
|
+
prepare(sql: string): AkanSqlStatement {
|
|
37
|
+
return new PostgresStatement(this.client, sql);
|
|
38
|
+
}
|
|
39
|
+
async close() {
|
|
40
|
+
await this.client.end();
|
|
41
|
+
}
|
|
42
|
+
}
|