js-bao 0.3.0-alpha.0 → 0.3.0-alpha.1
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/dist/cloudflare-do.cjs +59 -3
- package/dist/cloudflare-do.d.cts +18 -1
- package/dist/cloudflare-do.d.ts +18 -1
- package/dist/cloudflare-do.js +59 -3
- package/dist/cloudflare.d.cts +16 -1
- package/dist/cloudflare.d.ts +16 -1
- package/dist/codegen.cjs +1 -1
- package/package.json +1 -1
package/dist/cloudflare-do.cjs
CHANGED
|
@@ -2026,6 +2026,8 @@ function createDatabaseDO(config = {}) {
|
|
|
2026
2026
|
return this._handleHealth();
|
|
2027
2027
|
case "/describe":
|
|
2028
2028
|
return this._handleDescribe(request);
|
|
2029
|
+
case "/models":
|
|
2030
|
+
return this._handleModelList();
|
|
2029
2031
|
default:
|
|
2030
2032
|
return this._errorResponse("Not found", 404);
|
|
2031
2033
|
}
|
|
@@ -2047,6 +2049,7 @@ function createDatabaseDO(config = {}) {
|
|
|
2047
2049
|
modelName,
|
|
2048
2050
|
docId,
|
|
2049
2051
|
request,
|
|
2052
|
+
engine: this._engine,
|
|
2050
2053
|
filter,
|
|
2051
2054
|
options
|
|
2052
2055
|
};
|
|
@@ -2065,7 +2068,18 @@ function createDatabaseDO(config = {}) {
|
|
|
2065
2068
|
});
|
|
2066
2069
|
const { sql, params, sortFields } = translator.translateFind(filter, options);
|
|
2067
2070
|
const rawResults = this._engine.execSqlSync(sql, params);
|
|
2068
|
-
|
|
2071
|
+
let data = rawResults.map((row) => this._parseRow(row));
|
|
2072
|
+
if (hooks?.afterQuery) {
|
|
2073
|
+
const ctx = {
|
|
2074
|
+
modelName,
|
|
2075
|
+
docId,
|
|
2076
|
+
request,
|
|
2077
|
+
engine: this._engine,
|
|
2078
|
+
results: data
|
|
2079
|
+
};
|
|
2080
|
+
const result = await hooks.afterQuery(ctx);
|
|
2081
|
+
data = result.results;
|
|
2082
|
+
}
|
|
2069
2083
|
const limit = options?.limit;
|
|
2070
2084
|
const hasMore = CursorManager.hasMoreResults(limit, data.length);
|
|
2071
2085
|
const isFirstPage = !options?.uniqueStartKey;
|
|
@@ -2108,6 +2122,7 @@ function createDatabaseDO(config = {}) {
|
|
|
2108
2122
|
modelName,
|
|
2109
2123
|
docId,
|
|
2110
2124
|
request,
|
|
2125
|
+
engine: this._engine,
|
|
2111
2126
|
id,
|
|
2112
2127
|
data,
|
|
2113
2128
|
stringSets,
|
|
@@ -2160,6 +2175,7 @@ function createDatabaseDO(config = {}) {
|
|
|
2160
2175
|
modelName,
|
|
2161
2176
|
docId,
|
|
2162
2177
|
request,
|
|
2178
|
+
engine: this._engine,
|
|
2163
2179
|
id,
|
|
2164
2180
|
data,
|
|
2165
2181
|
stringSets,
|
|
@@ -2205,11 +2221,21 @@ function createDatabaseDO(config = {}) {
|
|
|
2205
2221
|
);
|
|
2206
2222
|
}
|
|
2207
2223
|
if (hooks?.beforeDelete) {
|
|
2224
|
+
let record = null;
|
|
2225
|
+
const rows = this._engine.execSqlSync(
|
|
2226
|
+
"SELECT id, type, data_json FROM records WHERE id = ? AND type = ?",
|
|
2227
|
+
[id, modelName]
|
|
2228
|
+
);
|
|
2229
|
+
if (rows.length > 0) {
|
|
2230
|
+
record = this._parseRow(rows[0]);
|
|
2231
|
+
}
|
|
2208
2232
|
const ctx = {
|
|
2209
2233
|
modelName,
|
|
2210
2234
|
docId,
|
|
2211
2235
|
request,
|
|
2212
|
-
|
|
2236
|
+
engine: this._engine,
|
|
2237
|
+
id,
|
|
2238
|
+
record
|
|
2213
2239
|
};
|
|
2214
2240
|
const result = await hooks.beforeDelete(ctx);
|
|
2215
2241
|
if (!result.allow) {
|
|
@@ -2249,6 +2275,7 @@ function createDatabaseDO(config = {}) {
|
|
|
2249
2275
|
modelName: op.modelName,
|
|
2250
2276
|
docId,
|
|
2251
2277
|
request,
|
|
2278
|
+
engine: this._engine,
|
|
2252
2279
|
id: op.id,
|
|
2253
2280
|
data: op.data,
|
|
2254
2281
|
stringSets: op.stringSets,
|
|
@@ -2261,11 +2288,21 @@ function createDatabaseDO(config = {}) {
|
|
|
2261
2288
|
}
|
|
2262
2289
|
} else if (op.op === "delete") {
|
|
2263
2290
|
if (hooks?.beforeDelete) {
|
|
2291
|
+
let record = null;
|
|
2292
|
+
const rows = this._engine.execSqlSync(
|
|
2293
|
+
"SELECT id, type, data_json FROM records WHERE id = ? AND type = ?",
|
|
2294
|
+
[op.id, op.modelName]
|
|
2295
|
+
);
|
|
2296
|
+
if (rows.length > 0) {
|
|
2297
|
+
record = this._parseRow(rows[0]);
|
|
2298
|
+
}
|
|
2264
2299
|
const ctx = {
|
|
2265
2300
|
modelName: op.modelName,
|
|
2266
2301
|
docId,
|
|
2267
2302
|
request,
|
|
2268
|
-
|
|
2303
|
+
engine: this._engine,
|
|
2304
|
+
id: op.id,
|
|
2305
|
+
record
|
|
2269
2306
|
};
|
|
2270
2307
|
const result = await hooks.beforeDelete(ctx);
|
|
2271
2308
|
if (!result.allow) {
|
|
@@ -2490,6 +2527,7 @@ function createDatabaseDO(config = {}) {
|
|
|
2490
2527
|
modelName,
|
|
2491
2528
|
docId,
|
|
2492
2529
|
request,
|
|
2530
|
+
engine: this._engine,
|
|
2493
2531
|
filter
|
|
2494
2532
|
};
|
|
2495
2533
|
const result = await hooks.beforeQuery(ctx);
|
|
@@ -2602,6 +2640,7 @@ function createDatabaseDO(config = {}) {
|
|
|
2602
2640
|
modelName,
|
|
2603
2641
|
docId,
|
|
2604
2642
|
request,
|
|
2643
|
+
engine: this._engine,
|
|
2605
2644
|
filter
|
|
2606
2645
|
};
|
|
2607
2646
|
const result = await hooks.beforeQuery(ctx);
|
|
@@ -3087,6 +3126,23 @@ function createDatabaseDO(config = {}) {
|
|
|
3087
3126
|
const response = { modelName, fields };
|
|
3088
3127
|
return Response.json(response);
|
|
3089
3128
|
}
|
|
3129
|
+
/** @internal — List all known model names from records and _model_fields */
|
|
3130
|
+
_handleModelList() {
|
|
3131
|
+
const fromRecords = this._engine.execSqlSync(
|
|
3132
|
+
"SELECT DISTINCT type FROM records ORDER BY type"
|
|
3133
|
+
);
|
|
3134
|
+
const fromFields = this._engine.execSqlSync(
|
|
3135
|
+
"SELECT DISTINCT model_name FROM _model_fields ORDER BY model_name"
|
|
3136
|
+
);
|
|
3137
|
+
const fromIndexes = this._engine.execSqlSync(
|
|
3138
|
+
"SELECT DISTINCT model_name FROM _indexes ORDER BY model_name"
|
|
3139
|
+
);
|
|
3140
|
+
const modelSet = /* @__PURE__ */ new Set();
|
|
3141
|
+
for (const row of fromRecords) modelSet.add(row.type);
|
|
3142
|
+
for (const row of fromFields) modelSet.add(row.model_name);
|
|
3143
|
+
for (const row of fromIndexes) modelSet.add(row.model_name);
|
|
3144
|
+
return Response.json({ models: Array.from(modelSet).sort() });
|
|
3145
|
+
}
|
|
3090
3146
|
/** @internal */
|
|
3091
3147
|
_errorResponse(message, status) {
|
|
3092
3148
|
const body = { error: message };
|
package/dist/cloudflare-do.d.cts
CHANGED
|
@@ -880,6 +880,8 @@ interface HookContext {
|
|
|
880
880
|
docId: string;
|
|
881
881
|
/** The raw request (for reading headers, auth tokens, etc.) */
|
|
882
882
|
request: Request;
|
|
883
|
+
/** The DO's SQLite engine (for direct queries, e.g. lookup functions) */
|
|
884
|
+
engine: any;
|
|
883
885
|
}
|
|
884
886
|
/** Context for beforeSave hooks */
|
|
885
887
|
interface BeforeSaveContext extends HookContext {
|
|
@@ -896,6 +898,8 @@ interface BeforeSaveContext extends HookContext {
|
|
|
896
898
|
interface BeforeDeleteContext extends HookContext {
|
|
897
899
|
/** Record ID being deleted */
|
|
898
900
|
id: string;
|
|
901
|
+
/** The existing record being deleted (parsed from data_json) */
|
|
902
|
+
record: Record<string, any> | null;
|
|
899
903
|
}
|
|
900
904
|
/** Context for beforeQuery hooks */
|
|
901
905
|
interface BeforeQueryContext extends HookContext {
|
|
@@ -904,6 +908,11 @@ interface BeforeQueryContext extends HookContext {
|
|
|
904
908
|
/** Query options (sort, limit, etc.) */
|
|
905
909
|
options?: QueryOptions;
|
|
906
910
|
}
|
|
911
|
+
/** Context for afterQuery hooks */
|
|
912
|
+
interface AfterQueryContext extends HookContext {
|
|
913
|
+
/** The query results to filter */
|
|
914
|
+
results: Record<string, any>[];
|
|
915
|
+
}
|
|
907
916
|
/** Result from beforeSave/beforeDelete hooks */
|
|
908
917
|
interface HookResult {
|
|
909
918
|
allow: boolean;
|
|
@@ -915,11 +924,17 @@ interface BeforeQueryResult extends HookResult {
|
|
|
915
924
|
/** Additional filter merged into the query via $and */
|
|
916
925
|
injectFilter?: DocumentFilter;
|
|
917
926
|
}
|
|
927
|
+
/** Result from afterQuery hooks — returns filtered results */
|
|
928
|
+
interface AfterQueryResult {
|
|
929
|
+
/** The filtered results to return */
|
|
930
|
+
results: Record<string, any>[];
|
|
931
|
+
}
|
|
918
932
|
/** Hook definitions for createDatabaseDO */
|
|
919
933
|
interface DatabaseDOHooks {
|
|
920
934
|
beforeSave?: (ctx: BeforeSaveContext) => Promise<HookResult>;
|
|
921
935
|
beforeDelete?: (ctx: BeforeDeleteContext) => Promise<HookResult>;
|
|
922
936
|
beforeQuery?: (ctx: BeforeQueryContext) => Promise<BeforeQueryResult>;
|
|
937
|
+
afterQuery?: (ctx: AfterQueryContext) => Promise<AfterQueryResult>;
|
|
923
938
|
}
|
|
924
939
|
|
|
925
940
|
/**
|
|
@@ -1231,6 +1246,8 @@ declare function createDatabaseDO(config?: DatabaseDOConfig): {
|
|
|
1231
1246
|
_handleHealth(): Response;
|
|
1232
1247
|
/** @internal — Return tracked field names and types for a model */
|
|
1233
1248
|
_handleDescribe(request: Request): Response;
|
|
1249
|
+
/** @internal — List all known model names from records and _model_fields */
|
|
1250
|
+
_handleModelList(): Response;
|
|
1234
1251
|
/** @internal */
|
|
1235
1252
|
_errorResponse(message: string, status: number): Response;
|
|
1236
1253
|
/** @internal */
|
|
@@ -1300,4 +1317,4 @@ interface Env {
|
|
|
1300
1317
|
*/
|
|
1301
1318
|
declare function handleRequest(request: Request, env: Env): Promise<Response>;
|
|
1302
1319
|
|
|
1303
|
-
export { type AggregateRequest, type AggregateResponse, type AggregationOperation, type AggregationOptions, type AggregationResult, type BatchOperation, type BatchOperationResult, type BatchRequest, type BatchResponse, type BeforeDeleteContext, type BeforeQueryContext, type BeforeQueryResult, type BeforeSaveContext, type CountRequest, type CountResponse, type DatabaseDOConfig, type DatabaseDOHooks, type DeleteRequest, type DeleteResponse, type DocumentDOConfig, type DropIndexRequest, type DropIndexResponse, type DropUniqueConstraintRequest, type DropUniqueConstraintResponse, DurableObjectEngine, type DurableObjectEngineOptions, type DurableObjectId, type DurableObjectNamespace, type DurableObjectState, type DurableObjectStorage, type DurableObjectStub, type Env, type ErrorResponse, type GroupByField, type HookContext, type HookResult, type IncrementRequest, type IncrementResponse, type IndexEntry, type IndexListResponse, JsonQueryTranslator, type JsonQueryTranslatorOptions, JsonSchemaDDL, type JsonSchemaOptions, type PatchRequest, type PatchResponse, type QueryRequest, type QueryResponse, type RegisterIndexRequest, type RegisterIndexResponse, type RegisterUniqueConstraintRequest, type RegisterUniqueConstraintResponse, type SaveRequest, type SaveResponse, type SqlStorage, type SqlStorageCursor, type StringSetMembership, type StringSetUpdateRequest, type StringSetUpdateResponse, type SyncIndexesRequest, type SyncIndexesResponse, type UniqueConstraintEntry, type UniqueConstraintListResponse, createDatabaseDO, createDocumentDO, handleRequest };
|
|
1320
|
+
export { type AfterQueryContext, type AfterQueryResult, type AggregateRequest, type AggregateResponse, type AggregationOperation, type AggregationOptions, type AggregationResult, type BatchOperation, type BatchOperationResult, type BatchRequest, type BatchResponse, type BeforeDeleteContext, type BeforeQueryContext, type BeforeQueryResult, type BeforeSaveContext, type CountRequest, type CountResponse, type DatabaseDOConfig, type DatabaseDOHooks, type DeleteRequest, type DeleteResponse, type DocumentDOConfig, type DropIndexRequest, type DropIndexResponse, type DropUniqueConstraintRequest, type DropUniqueConstraintResponse, DurableObjectEngine, type DurableObjectEngineOptions, type DurableObjectId, type DurableObjectNamespace, type DurableObjectState, type DurableObjectStorage, type DurableObjectStub, type Env, type ErrorResponse, type GroupByField, type HookContext, type HookResult, type IncrementRequest, type IncrementResponse, type IndexEntry, type IndexListResponse, JsonQueryTranslator, type JsonQueryTranslatorOptions, JsonSchemaDDL, type JsonSchemaOptions, type PatchRequest, type PatchResponse, type QueryRequest, type QueryResponse, type RegisterIndexRequest, type RegisterIndexResponse, type RegisterUniqueConstraintRequest, type RegisterUniqueConstraintResponse, type SaveRequest, type SaveResponse, type SqlStorage, type SqlStorageCursor, type StringSetMembership, type StringSetUpdateRequest, type StringSetUpdateResponse, type SyncIndexesRequest, type SyncIndexesResponse, type UniqueConstraintEntry, type UniqueConstraintListResponse, createDatabaseDO, createDocumentDO, handleRequest };
|
package/dist/cloudflare-do.d.ts
CHANGED
|
@@ -880,6 +880,8 @@ interface HookContext {
|
|
|
880
880
|
docId: string;
|
|
881
881
|
/** The raw request (for reading headers, auth tokens, etc.) */
|
|
882
882
|
request: Request;
|
|
883
|
+
/** The DO's SQLite engine (for direct queries, e.g. lookup functions) */
|
|
884
|
+
engine: any;
|
|
883
885
|
}
|
|
884
886
|
/** Context for beforeSave hooks */
|
|
885
887
|
interface BeforeSaveContext extends HookContext {
|
|
@@ -896,6 +898,8 @@ interface BeforeSaveContext extends HookContext {
|
|
|
896
898
|
interface BeforeDeleteContext extends HookContext {
|
|
897
899
|
/** Record ID being deleted */
|
|
898
900
|
id: string;
|
|
901
|
+
/** The existing record being deleted (parsed from data_json) */
|
|
902
|
+
record: Record<string, any> | null;
|
|
899
903
|
}
|
|
900
904
|
/** Context for beforeQuery hooks */
|
|
901
905
|
interface BeforeQueryContext extends HookContext {
|
|
@@ -904,6 +908,11 @@ interface BeforeQueryContext extends HookContext {
|
|
|
904
908
|
/** Query options (sort, limit, etc.) */
|
|
905
909
|
options?: QueryOptions;
|
|
906
910
|
}
|
|
911
|
+
/** Context for afterQuery hooks */
|
|
912
|
+
interface AfterQueryContext extends HookContext {
|
|
913
|
+
/** The query results to filter */
|
|
914
|
+
results: Record<string, any>[];
|
|
915
|
+
}
|
|
907
916
|
/** Result from beforeSave/beforeDelete hooks */
|
|
908
917
|
interface HookResult {
|
|
909
918
|
allow: boolean;
|
|
@@ -915,11 +924,17 @@ interface BeforeQueryResult extends HookResult {
|
|
|
915
924
|
/** Additional filter merged into the query via $and */
|
|
916
925
|
injectFilter?: DocumentFilter;
|
|
917
926
|
}
|
|
927
|
+
/** Result from afterQuery hooks — returns filtered results */
|
|
928
|
+
interface AfterQueryResult {
|
|
929
|
+
/** The filtered results to return */
|
|
930
|
+
results: Record<string, any>[];
|
|
931
|
+
}
|
|
918
932
|
/** Hook definitions for createDatabaseDO */
|
|
919
933
|
interface DatabaseDOHooks {
|
|
920
934
|
beforeSave?: (ctx: BeforeSaveContext) => Promise<HookResult>;
|
|
921
935
|
beforeDelete?: (ctx: BeforeDeleteContext) => Promise<HookResult>;
|
|
922
936
|
beforeQuery?: (ctx: BeforeQueryContext) => Promise<BeforeQueryResult>;
|
|
937
|
+
afterQuery?: (ctx: AfterQueryContext) => Promise<AfterQueryResult>;
|
|
923
938
|
}
|
|
924
939
|
|
|
925
940
|
/**
|
|
@@ -1231,6 +1246,8 @@ declare function createDatabaseDO(config?: DatabaseDOConfig): {
|
|
|
1231
1246
|
_handleHealth(): Response;
|
|
1232
1247
|
/** @internal — Return tracked field names and types for a model */
|
|
1233
1248
|
_handleDescribe(request: Request): Response;
|
|
1249
|
+
/** @internal — List all known model names from records and _model_fields */
|
|
1250
|
+
_handleModelList(): Response;
|
|
1234
1251
|
/** @internal */
|
|
1235
1252
|
_errorResponse(message: string, status: number): Response;
|
|
1236
1253
|
/** @internal */
|
|
@@ -1300,4 +1317,4 @@ interface Env {
|
|
|
1300
1317
|
*/
|
|
1301
1318
|
declare function handleRequest(request: Request, env: Env): Promise<Response>;
|
|
1302
1319
|
|
|
1303
|
-
export { type AggregateRequest, type AggregateResponse, type AggregationOperation, type AggregationOptions, type AggregationResult, type BatchOperation, type BatchOperationResult, type BatchRequest, type BatchResponse, type BeforeDeleteContext, type BeforeQueryContext, type BeforeQueryResult, type BeforeSaveContext, type CountRequest, type CountResponse, type DatabaseDOConfig, type DatabaseDOHooks, type DeleteRequest, type DeleteResponse, type DocumentDOConfig, type DropIndexRequest, type DropIndexResponse, type DropUniqueConstraintRequest, type DropUniqueConstraintResponse, DurableObjectEngine, type DurableObjectEngineOptions, type DurableObjectId, type DurableObjectNamespace, type DurableObjectState, type DurableObjectStorage, type DurableObjectStub, type Env, type ErrorResponse, type GroupByField, type HookContext, type HookResult, type IncrementRequest, type IncrementResponse, type IndexEntry, type IndexListResponse, JsonQueryTranslator, type JsonQueryTranslatorOptions, JsonSchemaDDL, type JsonSchemaOptions, type PatchRequest, type PatchResponse, type QueryRequest, type QueryResponse, type RegisterIndexRequest, type RegisterIndexResponse, type RegisterUniqueConstraintRequest, type RegisterUniqueConstraintResponse, type SaveRequest, type SaveResponse, type SqlStorage, type SqlStorageCursor, type StringSetMembership, type StringSetUpdateRequest, type StringSetUpdateResponse, type SyncIndexesRequest, type SyncIndexesResponse, type UniqueConstraintEntry, type UniqueConstraintListResponse, createDatabaseDO, createDocumentDO, handleRequest };
|
|
1320
|
+
export { type AfterQueryContext, type AfterQueryResult, type AggregateRequest, type AggregateResponse, type AggregationOperation, type AggregationOptions, type AggregationResult, type BatchOperation, type BatchOperationResult, type BatchRequest, type BatchResponse, type BeforeDeleteContext, type BeforeQueryContext, type BeforeQueryResult, type BeforeSaveContext, type CountRequest, type CountResponse, type DatabaseDOConfig, type DatabaseDOHooks, type DeleteRequest, type DeleteResponse, type DocumentDOConfig, type DropIndexRequest, type DropIndexResponse, type DropUniqueConstraintRequest, type DropUniqueConstraintResponse, DurableObjectEngine, type DurableObjectEngineOptions, type DurableObjectId, type DurableObjectNamespace, type DurableObjectState, type DurableObjectStorage, type DurableObjectStub, type Env, type ErrorResponse, type GroupByField, type HookContext, type HookResult, type IncrementRequest, type IncrementResponse, type IndexEntry, type IndexListResponse, JsonQueryTranslator, type JsonQueryTranslatorOptions, JsonSchemaDDL, type JsonSchemaOptions, type PatchRequest, type PatchResponse, type QueryRequest, type QueryResponse, type RegisterIndexRequest, type RegisterIndexResponse, type RegisterUniqueConstraintRequest, type RegisterUniqueConstraintResponse, type SaveRequest, type SaveResponse, type SqlStorage, type SqlStorageCursor, type StringSetMembership, type StringSetUpdateRequest, type StringSetUpdateResponse, type SyncIndexesRequest, type SyncIndexesResponse, type UniqueConstraintEntry, type UniqueConstraintListResponse, createDatabaseDO, createDocumentDO, handleRequest };
|
package/dist/cloudflare-do.js
CHANGED
|
@@ -1995,6 +1995,8 @@ function createDatabaseDO(config = {}) {
|
|
|
1995
1995
|
return this._handleHealth();
|
|
1996
1996
|
case "/describe":
|
|
1997
1997
|
return this._handleDescribe(request);
|
|
1998
|
+
case "/models":
|
|
1999
|
+
return this._handleModelList();
|
|
1998
2000
|
default:
|
|
1999
2001
|
return this._errorResponse("Not found", 404);
|
|
2000
2002
|
}
|
|
@@ -2016,6 +2018,7 @@ function createDatabaseDO(config = {}) {
|
|
|
2016
2018
|
modelName,
|
|
2017
2019
|
docId,
|
|
2018
2020
|
request,
|
|
2021
|
+
engine: this._engine,
|
|
2019
2022
|
filter,
|
|
2020
2023
|
options
|
|
2021
2024
|
};
|
|
@@ -2034,7 +2037,18 @@ function createDatabaseDO(config = {}) {
|
|
|
2034
2037
|
});
|
|
2035
2038
|
const { sql, params, sortFields } = translator.translateFind(filter, options);
|
|
2036
2039
|
const rawResults = this._engine.execSqlSync(sql, params);
|
|
2037
|
-
|
|
2040
|
+
let data = rawResults.map((row) => this._parseRow(row));
|
|
2041
|
+
if (hooks?.afterQuery) {
|
|
2042
|
+
const ctx = {
|
|
2043
|
+
modelName,
|
|
2044
|
+
docId,
|
|
2045
|
+
request,
|
|
2046
|
+
engine: this._engine,
|
|
2047
|
+
results: data
|
|
2048
|
+
};
|
|
2049
|
+
const result = await hooks.afterQuery(ctx);
|
|
2050
|
+
data = result.results;
|
|
2051
|
+
}
|
|
2038
2052
|
const limit = options?.limit;
|
|
2039
2053
|
const hasMore = CursorManager.hasMoreResults(limit, data.length);
|
|
2040
2054
|
const isFirstPage = !options?.uniqueStartKey;
|
|
@@ -2077,6 +2091,7 @@ function createDatabaseDO(config = {}) {
|
|
|
2077
2091
|
modelName,
|
|
2078
2092
|
docId,
|
|
2079
2093
|
request,
|
|
2094
|
+
engine: this._engine,
|
|
2080
2095
|
id,
|
|
2081
2096
|
data,
|
|
2082
2097
|
stringSets,
|
|
@@ -2129,6 +2144,7 @@ function createDatabaseDO(config = {}) {
|
|
|
2129
2144
|
modelName,
|
|
2130
2145
|
docId,
|
|
2131
2146
|
request,
|
|
2147
|
+
engine: this._engine,
|
|
2132
2148
|
id,
|
|
2133
2149
|
data,
|
|
2134
2150
|
stringSets,
|
|
@@ -2174,11 +2190,21 @@ function createDatabaseDO(config = {}) {
|
|
|
2174
2190
|
);
|
|
2175
2191
|
}
|
|
2176
2192
|
if (hooks?.beforeDelete) {
|
|
2193
|
+
let record = null;
|
|
2194
|
+
const rows = this._engine.execSqlSync(
|
|
2195
|
+
"SELECT id, type, data_json FROM records WHERE id = ? AND type = ?",
|
|
2196
|
+
[id, modelName]
|
|
2197
|
+
);
|
|
2198
|
+
if (rows.length > 0) {
|
|
2199
|
+
record = this._parseRow(rows[0]);
|
|
2200
|
+
}
|
|
2177
2201
|
const ctx = {
|
|
2178
2202
|
modelName,
|
|
2179
2203
|
docId,
|
|
2180
2204
|
request,
|
|
2181
|
-
|
|
2205
|
+
engine: this._engine,
|
|
2206
|
+
id,
|
|
2207
|
+
record
|
|
2182
2208
|
};
|
|
2183
2209
|
const result = await hooks.beforeDelete(ctx);
|
|
2184
2210
|
if (!result.allow) {
|
|
@@ -2218,6 +2244,7 @@ function createDatabaseDO(config = {}) {
|
|
|
2218
2244
|
modelName: op.modelName,
|
|
2219
2245
|
docId,
|
|
2220
2246
|
request,
|
|
2247
|
+
engine: this._engine,
|
|
2221
2248
|
id: op.id,
|
|
2222
2249
|
data: op.data,
|
|
2223
2250
|
stringSets: op.stringSets,
|
|
@@ -2230,11 +2257,21 @@ function createDatabaseDO(config = {}) {
|
|
|
2230
2257
|
}
|
|
2231
2258
|
} else if (op.op === "delete") {
|
|
2232
2259
|
if (hooks?.beforeDelete) {
|
|
2260
|
+
let record = null;
|
|
2261
|
+
const rows = this._engine.execSqlSync(
|
|
2262
|
+
"SELECT id, type, data_json FROM records WHERE id = ? AND type = ?",
|
|
2263
|
+
[op.id, op.modelName]
|
|
2264
|
+
);
|
|
2265
|
+
if (rows.length > 0) {
|
|
2266
|
+
record = this._parseRow(rows[0]);
|
|
2267
|
+
}
|
|
2233
2268
|
const ctx = {
|
|
2234
2269
|
modelName: op.modelName,
|
|
2235
2270
|
docId,
|
|
2236
2271
|
request,
|
|
2237
|
-
|
|
2272
|
+
engine: this._engine,
|
|
2273
|
+
id: op.id,
|
|
2274
|
+
record
|
|
2238
2275
|
};
|
|
2239
2276
|
const result = await hooks.beforeDelete(ctx);
|
|
2240
2277
|
if (!result.allow) {
|
|
@@ -2459,6 +2496,7 @@ function createDatabaseDO(config = {}) {
|
|
|
2459
2496
|
modelName,
|
|
2460
2497
|
docId,
|
|
2461
2498
|
request,
|
|
2499
|
+
engine: this._engine,
|
|
2462
2500
|
filter
|
|
2463
2501
|
};
|
|
2464
2502
|
const result = await hooks.beforeQuery(ctx);
|
|
@@ -2571,6 +2609,7 @@ function createDatabaseDO(config = {}) {
|
|
|
2571
2609
|
modelName,
|
|
2572
2610
|
docId,
|
|
2573
2611
|
request,
|
|
2612
|
+
engine: this._engine,
|
|
2574
2613
|
filter
|
|
2575
2614
|
};
|
|
2576
2615
|
const result = await hooks.beforeQuery(ctx);
|
|
@@ -3056,6 +3095,23 @@ function createDatabaseDO(config = {}) {
|
|
|
3056
3095
|
const response = { modelName, fields };
|
|
3057
3096
|
return Response.json(response);
|
|
3058
3097
|
}
|
|
3098
|
+
/** @internal — List all known model names from records and _model_fields */
|
|
3099
|
+
_handleModelList() {
|
|
3100
|
+
const fromRecords = this._engine.execSqlSync(
|
|
3101
|
+
"SELECT DISTINCT type FROM records ORDER BY type"
|
|
3102
|
+
);
|
|
3103
|
+
const fromFields = this._engine.execSqlSync(
|
|
3104
|
+
"SELECT DISTINCT model_name FROM _model_fields ORDER BY model_name"
|
|
3105
|
+
);
|
|
3106
|
+
const fromIndexes = this._engine.execSqlSync(
|
|
3107
|
+
"SELECT DISTINCT model_name FROM _indexes ORDER BY model_name"
|
|
3108
|
+
);
|
|
3109
|
+
const modelSet = /* @__PURE__ */ new Set();
|
|
3110
|
+
for (const row of fromRecords) modelSet.add(row.type);
|
|
3111
|
+
for (const row of fromFields) modelSet.add(row.model_name);
|
|
3112
|
+
for (const row of fromIndexes) modelSet.add(row.model_name);
|
|
3113
|
+
return Response.json({ models: Array.from(modelSet).sort() });
|
|
3114
|
+
}
|
|
3059
3115
|
/** @internal */
|
|
3060
3116
|
_errorResponse(message, status) {
|
|
3061
3117
|
const body = { error: message };
|
package/dist/cloudflare.d.cts
CHANGED
|
@@ -608,6 +608,8 @@ interface HookContext {
|
|
|
608
608
|
docId: string;
|
|
609
609
|
/** The raw request (for reading headers, auth tokens, etc.) */
|
|
610
610
|
request: Request;
|
|
611
|
+
/** The DO's SQLite engine (for direct queries, e.g. lookup functions) */
|
|
612
|
+
engine: any;
|
|
611
613
|
}
|
|
612
614
|
/** Context for beforeSave hooks */
|
|
613
615
|
interface BeforeSaveContext extends HookContext {
|
|
@@ -624,6 +626,8 @@ interface BeforeSaveContext extends HookContext {
|
|
|
624
626
|
interface BeforeDeleteContext extends HookContext {
|
|
625
627
|
/** Record ID being deleted */
|
|
626
628
|
id: string;
|
|
629
|
+
/** The existing record being deleted (parsed from data_json) */
|
|
630
|
+
record: Record<string, any> | null;
|
|
627
631
|
}
|
|
628
632
|
/** Context for beforeQuery hooks */
|
|
629
633
|
interface BeforeQueryContext extends HookContext {
|
|
@@ -632,6 +636,11 @@ interface BeforeQueryContext extends HookContext {
|
|
|
632
636
|
/** Query options (sort, limit, etc.) */
|
|
633
637
|
options?: QueryOptions;
|
|
634
638
|
}
|
|
639
|
+
/** Context for afterQuery hooks */
|
|
640
|
+
interface AfterQueryContext extends HookContext {
|
|
641
|
+
/** The query results to filter */
|
|
642
|
+
results: Record<string, any>[];
|
|
643
|
+
}
|
|
635
644
|
/** Result from beforeSave/beforeDelete hooks */
|
|
636
645
|
interface HookResult {
|
|
637
646
|
allow: boolean;
|
|
@@ -643,11 +652,17 @@ interface BeforeQueryResult extends HookResult {
|
|
|
643
652
|
/** Additional filter merged into the query via $and */
|
|
644
653
|
injectFilter?: DocumentFilter;
|
|
645
654
|
}
|
|
655
|
+
/** Result from afterQuery hooks — returns filtered results */
|
|
656
|
+
interface AfterQueryResult {
|
|
657
|
+
/** The filtered results to return */
|
|
658
|
+
results: Record<string, any>[];
|
|
659
|
+
}
|
|
646
660
|
/** Hook definitions for createDatabaseDO */
|
|
647
661
|
interface DatabaseDOHooks {
|
|
648
662
|
beforeSave?: (ctx: BeforeSaveContext) => Promise<HookResult>;
|
|
649
663
|
beforeDelete?: (ctx: BeforeDeleteContext) => Promise<HookResult>;
|
|
650
664
|
beforeQuery?: (ctx: BeforeQueryContext) => Promise<BeforeQueryResult>;
|
|
665
|
+
afterQuery?: (ctx: AfterQueryContext) => Promise<AfterQueryResult>;
|
|
651
666
|
}
|
|
652
667
|
|
|
653
668
|
/**
|
|
@@ -1346,4 +1361,4 @@ interface DoDb {
|
|
|
1346
1361
|
*/
|
|
1347
1362
|
declare function connectDoDb(options: ConnectDoDbOptions): DoDb;
|
|
1348
1363
|
|
|
1349
|
-
export { type AggregateRequest, type AggregateResponse, type AggregationOperation, type AggregationOptions, type AggregationResult, type BatchOperation, type BatchOperationResult, type BatchRequest, type BatchResponse, type BeforeDeleteContext, type BeforeQueryContext, type BeforeQueryResult, type BeforeSaveContext, type ConnectDoDbOptions, type CountRequest, type CountResponse, DOClientEngine, type DOClientEngineConfig, type DatabaseDOHooks, type DeleteRequest, type DeleteResponse, type DoDb, type DocumentFilter, type DropIndexRequest, type DropIndexResponse, type DropUniqueConstraintRequest, type DropUniqueConstraintResponse, type ErrorResponse, type GroupByField, type HookContext, type HookResult, type IncrementRequest, type IncrementResponse, type IndexEntry, type IndexListResponse, type InitJsBaoDOOptions, type InitJsBaoDOResult, type ModelAccessor, type PaginatedResult, type PatchOptions, type PatchRequest, type PatchResponse, type ProjectionSpec, type QueryOptions, type QueryRequest, type QueryResponse, type RegisterIndexRequest, type RegisterIndexResponse, type RegisterUniqueConstraintRequest, type RegisterUniqueConstraintResponse, type SaveOptions, type SaveRequest, type SaveResponse, type SortSpec, type StringSetMembership, type StringSetUpdateRequest, type StringSetUpdateResponse, type SyncIndexesRequest, type SyncIndexesResponse, type UniqueConstraintEntry, type UniqueConstraintListResponse, type WriteCondition, connectDoDb, getActiveDOEngine, initJsBaoDO, resetJsBaoDO };
|
|
1364
|
+
export { type AfterQueryContext, type AfterQueryResult, type AggregateRequest, type AggregateResponse, type AggregationOperation, type AggregationOptions, type AggregationResult, type BatchOperation, type BatchOperationResult, type BatchRequest, type BatchResponse, type BeforeDeleteContext, type BeforeQueryContext, type BeforeQueryResult, type BeforeSaveContext, type ConnectDoDbOptions, type CountRequest, type CountResponse, DOClientEngine, type DOClientEngineConfig, type DatabaseDOHooks, type DeleteRequest, type DeleteResponse, type DoDb, type DocumentFilter, type DropIndexRequest, type DropIndexResponse, type DropUniqueConstraintRequest, type DropUniqueConstraintResponse, type ErrorResponse, type GroupByField, type HookContext, type HookResult, type IncrementRequest, type IncrementResponse, type IndexEntry, type IndexListResponse, type InitJsBaoDOOptions, type InitJsBaoDOResult, type ModelAccessor, type PaginatedResult, type PatchOptions, type PatchRequest, type PatchResponse, type ProjectionSpec, type QueryOptions, type QueryRequest, type QueryResponse, type RegisterIndexRequest, type RegisterIndexResponse, type RegisterUniqueConstraintRequest, type RegisterUniqueConstraintResponse, type SaveOptions, type SaveRequest, type SaveResponse, type SortSpec, type StringSetMembership, type StringSetUpdateRequest, type StringSetUpdateResponse, type SyncIndexesRequest, type SyncIndexesResponse, type UniqueConstraintEntry, type UniqueConstraintListResponse, type WriteCondition, connectDoDb, getActiveDOEngine, initJsBaoDO, resetJsBaoDO };
|
package/dist/cloudflare.d.ts
CHANGED
|
@@ -608,6 +608,8 @@ interface HookContext {
|
|
|
608
608
|
docId: string;
|
|
609
609
|
/** The raw request (for reading headers, auth tokens, etc.) */
|
|
610
610
|
request: Request;
|
|
611
|
+
/** The DO's SQLite engine (for direct queries, e.g. lookup functions) */
|
|
612
|
+
engine: any;
|
|
611
613
|
}
|
|
612
614
|
/** Context for beforeSave hooks */
|
|
613
615
|
interface BeforeSaveContext extends HookContext {
|
|
@@ -624,6 +626,8 @@ interface BeforeSaveContext extends HookContext {
|
|
|
624
626
|
interface BeforeDeleteContext extends HookContext {
|
|
625
627
|
/** Record ID being deleted */
|
|
626
628
|
id: string;
|
|
629
|
+
/** The existing record being deleted (parsed from data_json) */
|
|
630
|
+
record: Record<string, any> | null;
|
|
627
631
|
}
|
|
628
632
|
/** Context for beforeQuery hooks */
|
|
629
633
|
interface BeforeQueryContext extends HookContext {
|
|
@@ -632,6 +636,11 @@ interface BeforeQueryContext extends HookContext {
|
|
|
632
636
|
/** Query options (sort, limit, etc.) */
|
|
633
637
|
options?: QueryOptions;
|
|
634
638
|
}
|
|
639
|
+
/** Context for afterQuery hooks */
|
|
640
|
+
interface AfterQueryContext extends HookContext {
|
|
641
|
+
/** The query results to filter */
|
|
642
|
+
results: Record<string, any>[];
|
|
643
|
+
}
|
|
635
644
|
/** Result from beforeSave/beforeDelete hooks */
|
|
636
645
|
interface HookResult {
|
|
637
646
|
allow: boolean;
|
|
@@ -643,11 +652,17 @@ interface BeforeQueryResult extends HookResult {
|
|
|
643
652
|
/** Additional filter merged into the query via $and */
|
|
644
653
|
injectFilter?: DocumentFilter;
|
|
645
654
|
}
|
|
655
|
+
/** Result from afterQuery hooks — returns filtered results */
|
|
656
|
+
interface AfterQueryResult {
|
|
657
|
+
/** The filtered results to return */
|
|
658
|
+
results: Record<string, any>[];
|
|
659
|
+
}
|
|
646
660
|
/** Hook definitions for createDatabaseDO */
|
|
647
661
|
interface DatabaseDOHooks {
|
|
648
662
|
beforeSave?: (ctx: BeforeSaveContext) => Promise<HookResult>;
|
|
649
663
|
beforeDelete?: (ctx: BeforeDeleteContext) => Promise<HookResult>;
|
|
650
664
|
beforeQuery?: (ctx: BeforeQueryContext) => Promise<BeforeQueryResult>;
|
|
665
|
+
afterQuery?: (ctx: AfterQueryContext) => Promise<AfterQueryResult>;
|
|
651
666
|
}
|
|
652
667
|
|
|
653
668
|
/**
|
|
@@ -1346,4 +1361,4 @@ interface DoDb {
|
|
|
1346
1361
|
*/
|
|
1347
1362
|
declare function connectDoDb(options: ConnectDoDbOptions): DoDb;
|
|
1348
1363
|
|
|
1349
|
-
export { type AggregateRequest, type AggregateResponse, type AggregationOperation, type AggregationOptions, type AggregationResult, type BatchOperation, type BatchOperationResult, type BatchRequest, type BatchResponse, type BeforeDeleteContext, type BeforeQueryContext, type BeforeQueryResult, type BeforeSaveContext, type ConnectDoDbOptions, type CountRequest, type CountResponse, DOClientEngine, type DOClientEngineConfig, type DatabaseDOHooks, type DeleteRequest, type DeleteResponse, type DoDb, type DocumentFilter, type DropIndexRequest, type DropIndexResponse, type DropUniqueConstraintRequest, type DropUniqueConstraintResponse, type ErrorResponse, type GroupByField, type HookContext, type HookResult, type IncrementRequest, type IncrementResponse, type IndexEntry, type IndexListResponse, type InitJsBaoDOOptions, type InitJsBaoDOResult, type ModelAccessor, type PaginatedResult, type PatchOptions, type PatchRequest, type PatchResponse, type ProjectionSpec, type QueryOptions, type QueryRequest, type QueryResponse, type RegisterIndexRequest, type RegisterIndexResponse, type RegisterUniqueConstraintRequest, type RegisterUniqueConstraintResponse, type SaveOptions, type SaveRequest, type SaveResponse, type SortSpec, type StringSetMembership, type StringSetUpdateRequest, type StringSetUpdateResponse, type SyncIndexesRequest, type SyncIndexesResponse, type UniqueConstraintEntry, type UniqueConstraintListResponse, type WriteCondition, connectDoDb, getActiveDOEngine, initJsBaoDO, resetJsBaoDO };
|
|
1364
|
+
export { type AfterQueryContext, type AfterQueryResult, type AggregateRequest, type AggregateResponse, type AggregationOperation, type AggregationOptions, type AggregationResult, type BatchOperation, type BatchOperationResult, type BatchRequest, type BatchResponse, type BeforeDeleteContext, type BeforeQueryContext, type BeforeQueryResult, type BeforeSaveContext, type ConnectDoDbOptions, type CountRequest, type CountResponse, DOClientEngine, type DOClientEngineConfig, type DatabaseDOHooks, type DeleteRequest, type DeleteResponse, type DoDb, type DocumentFilter, type DropIndexRequest, type DropIndexResponse, type DropUniqueConstraintRequest, type DropUniqueConstraintResponse, type ErrorResponse, type GroupByField, type HookContext, type HookResult, type IncrementRequest, type IncrementResponse, type IndexEntry, type IndexListResponse, type InitJsBaoDOOptions, type InitJsBaoDOResult, type ModelAccessor, type PaginatedResult, type PatchOptions, type PatchRequest, type PatchResponse, type ProjectionSpec, type QueryOptions, type QueryRequest, type QueryResponse, type RegisterIndexRequest, type RegisterIndexResponse, type RegisterUniqueConstraintRequest, type RegisterUniqueConstraintResponse, type SaveOptions, type SaveRequest, type SaveResponse, type SortSpec, type StringSetMembership, type StringSetUpdateRequest, type StringSetUpdateResponse, type SyncIndexesRequest, type SyncIndexesResponse, type UniqueConstraintEntry, type UniqueConstraintListResponse, type WriteCondition, connectDoDb, getActiveDOEngine, initJsBaoDO, resetJsBaoDO };
|
package/dist/codegen.cjs
CHANGED
|
@@ -1189,7 +1189,7 @@ var SchemaExtractor = class {
|
|
|
1189
1189
|
// package.json
|
|
1190
1190
|
var package_default = {
|
|
1191
1191
|
name: "js-bao",
|
|
1192
|
-
version: "0.3.0-alpha.
|
|
1192
|
+
version: "0.3.0-alpha.1",
|
|
1193
1193
|
description: "A library providing data modeling capabilities which support live updates and queries.",
|
|
1194
1194
|
types: "dist/index.d.ts",
|
|
1195
1195
|
type: "module",
|