@mastra/cloudflare-d1 0.1.5-alpha.2
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/LICENSE.md +46 -0
- package/README.md +142 -0
- package/dist/_tsup-dts-rollup.d.cts +313 -0
- package/dist/_tsup-dts-rollup.d.ts +313 -0
- package/dist/index.cjs +944 -0
- package/dist/index.d.cts +5 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.js +938 -0
- package/package.json +46 -0
|
@@ -0,0 +1,313 @@
|
|
|
1
|
+
import type { D1Database as D1Database_2 } from '@cloudflare/workers-types';
|
|
2
|
+
import type { EvalRow } from '@mastra/core/storage';
|
|
3
|
+
import { MastraStorage } from '@mastra/core/storage';
|
|
4
|
+
import type { MessageType } from '@mastra/core/memory';
|
|
5
|
+
import type { StorageColumn } from '@mastra/core/storage';
|
|
6
|
+
import type { StorageGetMessagesArg } from '@mastra/core/storage';
|
|
7
|
+
import type { StorageThreadType } from '@mastra/core/memory';
|
|
8
|
+
import type { TABLE_NAMES } from '@mastra/core/storage';
|
|
9
|
+
import type { WorkflowRuns } from '@mastra/core/storage';
|
|
10
|
+
import type { WorkflowRunState } from '@mastra/core/workflows';
|
|
11
|
+
import type { WorkflowRunState as WorkflowRunState_2 } from '@mastra/core';
|
|
12
|
+
|
|
13
|
+
export declare const createSampleMessage: (threadId: string) => any;
|
|
14
|
+
|
|
15
|
+
export declare const createSampleThread: () => {
|
|
16
|
+
id: string;
|
|
17
|
+
resourceId: string;
|
|
18
|
+
title: string;
|
|
19
|
+
createdAt: Date;
|
|
20
|
+
updatedAt: Date;
|
|
21
|
+
metadata: {
|
|
22
|
+
key: string;
|
|
23
|
+
};
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
export declare const createSampleThreadWithParams: (threadId: string, resourceId: string, createdAt: Date, updatedAt: Date) => {
|
|
27
|
+
id: string;
|
|
28
|
+
resourceId: string;
|
|
29
|
+
title: string;
|
|
30
|
+
createdAt: Date;
|
|
31
|
+
updatedAt: Date;
|
|
32
|
+
metadata: {
|
|
33
|
+
key: string;
|
|
34
|
+
};
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
export declare const createSampleTrace: (name: string, scope?: string, attributes?: Record<string, string>) => {
|
|
38
|
+
id: string;
|
|
39
|
+
parentSpanId: string;
|
|
40
|
+
traceId: string;
|
|
41
|
+
name: string;
|
|
42
|
+
scope: string | undefined;
|
|
43
|
+
kind: string;
|
|
44
|
+
status: string;
|
|
45
|
+
events: string;
|
|
46
|
+
links: string;
|
|
47
|
+
attributes: string | undefined;
|
|
48
|
+
startTime: string;
|
|
49
|
+
endTime: string;
|
|
50
|
+
other: string;
|
|
51
|
+
createdAt: string;
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
export declare const createSampleWorkflowSnapshot: (threadId: string) => WorkflowRunState_2;
|
|
55
|
+
|
|
56
|
+
export declare function createSqlBuilder(): SqlBuilder;
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Configuration for D1 using the REST API
|
|
60
|
+
*/
|
|
61
|
+
declare interface D1Config {
|
|
62
|
+
/** Cloudflare account ID */
|
|
63
|
+
accountId: string;
|
|
64
|
+
/** Cloudflare API token with D1 access */
|
|
65
|
+
apiToken: string;
|
|
66
|
+
/** D1 database ID */
|
|
67
|
+
databaseId: string;
|
|
68
|
+
/** Optional prefix for table names */
|
|
69
|
+
tablePrefix?: string;
|
|
70
|
+
}
|
|
71
|
+
export { D1Config }
|
|
72
|
+
export { D1Config as D1Config_alias_1 }
|
|
73
|
+
|
|
74
|
+
declare class D1Store extends MastraStorage {
|
|
75
|
+
private client?;
|
|
76
|
+
private accountId?;
|
|
77
|
+
private databaseId?;
|
|
78
|
+
private binding?;
|
|
79
|
+
private tablePrefix;
|
|
80
|
+
/**
|
|
81
|
+
* Creates a new D1Store instance
|
|
82
|
+
* @param config Configuration for D1 access (either REST API or Workers Binding API)
|
|
83
|
+
*/
|
|
84
|
+
constructor(config: D1StoreConfig);
|
|
85
|
+
private getTableName;
|
|
86
|
+
private formatSqlParams;
|
|
87
|
+
private createIndexIfNotExists;
|
|
88
|
+
private executeWorkersBindingQuery;
|
|
89
|
+
private executeRestQuery;
|
|
90
|
+
/**
|
|
91
|
+
* Execute a SQL query against the D1 database
|
|
92
|
+
* @param options Query options including SQL, parameters, and whether to return only the first result
|
|
93
|
+
* @returns Query results as an array or a single object if first=true
|
|
94
|
+
*/
|
|
95
|
+
private executeQuery;
|
|
96
|
+
private getSqlType;
|
|
97
|
+
private ensureDate;
|
|
98
|
+
private serializeDate;
|
|
99
|
+
private serializeValue;
|
|
100
|
+
private deserializeValue;
|
|
101
|
+
createTable({ tableName, schema, }: {
|
|
102
|
+
tableName: TABLE_NAMES;
|
|
103
|
+
schema: Record<string, StorageColumn>;
|
|
104
|
+
}): Promise<void>;
|
|
105
|
+
clearTable({ tableName }: {
|
|
106
|
+
tableName: TABLE_NAMES;
|
|
107
|
+
}): Promise<void>;
|
|
108
|
+
private processRecord;
|
|
109
|
+
insert({ tableName, record }: {
|
|
110
|
+
tableName: TABLE_NAMES;
|
|
111
|
+
record: Record<string, any>;
|
|
112
|
+
}): Promise<void>;
|
|
113
|
+
load<R>({ tableName, keys }: {
|
|
114
|
+
tableName: TABLE_NAMES;
|
|
115
|
+
keys: Record<string, string>;
|
|
116
|
+
}): Promise<R | null>;
|
|
117
|
+
getThreadById({ threadId }: {
|
|
118
|
+
threadId: string;
|
|
119
|
+
}): Promise<StorageThreadType | null>;
|
|
120
|
+
getThreadsByResourceId({ resourceId }: {
|
|
121
|
+
resourceId: string;
|
|
122
|
+
}): Promise<StorageThreadType[]>;
|
|
123
|
+
saveThread({ thread }: {
|
|
124
|
+
thread: StorageThreadType;
|
|
125
|
+
}): Promise<StorageThreadType>;
|
|
126
|
+
updateThread({ id, title, metadata, }: {
|
|
127
|
+
id: string;
|
|
128
|
+
title: string;
|
|
129
|
+
metadata: Record<string, unknown>;
|
|
130
|
+
}): Promise<StorageThreadType>;
|
|
131
|
+
deleteThread({ threadId }: {
|
|
132
|
+
threadId: string;
|
|
133
|
+
}): Promise<void>;
|
|
134
|
+
saveMessages({ messages }: {
|
|
135
|
+
messages: MessageType[];
|
|
136
|
+
}): Promise<MessageType[]>;
|
|
137
|
+
getMessages<T = MessageType>({ threadId, selectBy }: StorageGetMessagesArg): Promise<T[]>;
|
|
138
|
+
persistWorkflowSnapshot({ workflowName, runId, snapshot, }: {
|
|
139
|
+
workflowName: string;
|
|
140
|
+
runId: string;
|
|
141
|
+
snapshot: WorkflowRunState;
|
|
142
|
+
}): Promise<void>;
|
|
143
|
+
loadWorkflowSnapshot(params: {
|
|
144
|
+
workflowName: string;
|
|
145
|
+
runId: string;
|
|
146
|
+
}): Promise<WorkflowRunState | null>;
|
|
147
|
+
/**
|
|
148
|
+
* Insert multiple records in a batch operation
|
|
149
|
+
* @param tableName The table to insert into
|
|
150
|
+
* @param records The records to insert
|
|
151
|
+
*/
|
|
152
|
+
batchInsert({ tableName, records }: {
|
|
153
|
+
tableName: TABLE_NAMES;
|
|
154
|
+
records: Record<string, any>[];
|
|
155
|
+
}): Promise<void>;
|
|
156
|
+
getTraces({ name, scope, page, perPage, attributes, }: {
|
|
157
|
+
name?: string;
|
|
158
|
+
scope?: string;
|
|
159
|
+
page: number;
|
|
160
|
+
perPage: number;
|
|
161
|
+
attributes?: Record<string, string>;
|
|
162
|
+
}): Promise<Record<string, any>[]>;
|
|
163
|
+
getEvalsByAgentName(agentName: string, type?: 'test' | 'live'): Promise<EvalRow[]>;
|
|
164
|
+
getWorkflowRuns(_args?: {
|
|
165
|
+
workflowName?: string;
|
|
166
|
+
fromDate?: Date;
|
|
167
|
+
toDate?: Date;
|
|
168
|
+
limit?: number;
|
|
169
|
+
offset?: number;
|
|
170
|
+
}): Promise<WorkflowRuns>;
|
|
171
|
+
/**
|
|
172
|
+
* Close the database connection
|
|
173
|
+
* No explicit cleanup needed for D1 in either REST or Workers Binding mode
|
|
174
|
+
*/
|
|
175
|
+
close(): Promise<void>;
|
|
176
|
+
}
|
|
177
|
+
export { D1Store }
|
|
178
|
+
export { D1Store as D1Store_alias_1 }
|
|
179
|
+
|
|
180
|
+
/**
|
|
181
|
+
* Combined configuration type supporting both REST API and Workers Binding API
|
|
182
|
+
*/
|
|
183
|
+
declare type D1StoreConfig = D1Config | D1WorkersConfig;
|
|
184
|
+
export { D1StoreConfig }
|
|
185
|
+
export { D1StoreConfig as D1StoreConfig_alias_1 }
|
|
186
|
+
|
|
187
|
+
/**
|
|
188
|
+
* Configuration for D1 using the Workers Binding API
|
|
189
|
+
*/
|
|
190
|
+
declare interface D1WorkersConfig {
|
|
191
|
+
/** D1 database binding from Workers environment */
|
|
192
|
+
binding: D1Database_2;
|
|
193
|
+
/** Optional prefix for table names */
|
|
194
|
+
tablePrefix?: string;
|
|
195
|
+
}
|
|
196
|
+
export { D1WorkersConfig }
|
|
197
|
+
export { D1WorkersConfig as D1WorkersConfig_alias_1 }
|
|
198
|
+
|
|
199
|
+
export declare const retryUntil: <T>(fn: () => Promise<T>, condition: (result: T) => boolean, timeout?: number, // REST API needs longer timeout due to higher latency
|
|
200
|
+
interval?: number) => Promise<T>;
|
|
201
|
+
|
|
202
|
+
/**
|
|
203
|
+
* SQL Builder class for constructing type-safe SQL queries
|
|
204
|
+
* This helps create maintainable and secure SQL queries with proper parameter handling
|
|
205
|
+
*/
|
|
206
|
+
export declare class SqlBuilder {
|
|
207
|
+
private sql;
|
|
208
|
+
private params;
|
|
209
|
+
private whereAdded;
|
|
210
|
+
select(columns?: string | string[]): SqlBuilder;
|
|
211
|
+
from(table: string): SqlBuilder;
|
|
212
|
+
/**
|
|
213
|
+
* Add a WHERE clause to the query
|
|
214
|
+
* @param condition The condition to add
|
|
215
|
+
* @param params Parameters to bind to the condition
|
|
216
|
+
*/
|
|
217
|
+
where(condition: string, ...params: SqlParam[]): SqlBuilder;
|
|
218
|
+
/**
|
|
219
|
+
* Add a WHERE clause if it hasn't been added yet, otherwise add an AND clause
|
|
220
|
+
* @param condition The condition to add
|
|
221
|
+
* @param params Parameters to bind to the condition
|
|
222
|
+
*/
|
|
223
|
+
whereAnd(condition: string, ...params: SqlParam[]): SqlBuilder;
|
|
224
|
+
andWhere(condition: string, ...params: SqlParam[]): SqlBuilder;
|
|
225
|
+
orWhere(condition: string, ...params: SqlParam[]): SqlBuilder;
|
|
226
|
+
orderBy(column: string, direction?: 'ASC' | 'DESC'): SqlBuilder;
|
|
227
|
+
limit(count: number): SqlBuilder;
|
|
228
|
+
offset(count: number): SqlBuilder;
|
|
229
|
+
/**
|
|
230
|
+
* Insert a row, or update specific columns on conflict (upsert).
|
|
231
|
+
* @param table Table name
|
|
232
|
+
* @param columns Columns to insert
|
|
233
|
+
* @param values Values to insert
|
|
234
|
+
* @param conflictColumns Columns to check for conflict (usually PK or UNIQUE)
|
|
235
|
+
* @param updateMap Object mapping columns to update to their new value (e.g. { name: 'excluded.name' })
|
|
236
|
+
*/
|
|
237
|
+
insert(table: string, columns: string[], values: SqlParam[], conflictColumns?: string[], updateMap?: Record<string, string>): SqlBuilder;
|
|
238
|
+
update(table: string, columns: string[], values: SqlParam[]): SqlBuilder;
|
|
239
|
+
delete(table: string): SqlBuilder;
|
|
240
|
+
/**
|
|
241
|
+
* Create a table if it doesn't exist
|
|
242
|
+
* @param table The table name
|
|
243
|
+
* @param columnDefinitions The column definitions as an array of strings
|
|
244
|
+
* @param tableConstraints Optional constraints for the table
|
|
245
|
+
* @returns The builder instance
|
|
246
|
+
*/
|
|
247
|
+
createTable(table: string, columnDefinitions: string[], tableConstraints?: string[]): SqlBuilder;
|
|
248
|
+
/**
|
|
249
|
+
* Check if an index exists in the database
|
|
250
|
+
* @param indexName The name of the index to check
|
|
251
|
+
* @param tableName The table the index is on
|
|
252
|
+
* @returns The builder instance
|
|
253
|
+
*/
|
|
254
|
+
checkIndexExists(indexName: string, tableName: string): SqlBuilder;
|
|
255
|
+
/**
|
|
256
|
+
* Create an index if it doesn't exist
|
|
257
|
+
* @param indexName The name of the index to create
|
|
258
|
+
* @param tableName The table to create the index on
|
|
259
|
+
* @param columnName The column to index
|
|
260
|
+
* @param indexType Optional index type (e.g., 'UNIQUE')
|
|
261
|
+
* @returns The builder instance
|
|
262
|
+
*/
|
|
263
|
+
createIndex(indexName: string, tableName: string, columnName: string, indexType?: string): SqlBuilder;
|
|
264
|
+
raw(sql: string, ...params: SqlParam[]): SqlBuilder;
|
|
265
|
+
/**
|
|
266
|
+
* Add a LIKE condition to the query
|
|
267
|
+
* @param column The column to check
|
|
268
|
+
* @param value The value to match (will be wrapped with % for LIKE)
|
|
269
|
+
* @param exact If true, will not add % wildcards
|
|
270
|
+
*/
|
|
271
|
+
like(column: string, value: string, exact?: boolean): SqlBuilder;
|
|
272
|
+
/**
|
|
273
|
+
* Add a JSON LIKE condition for searching in JSON fields
|
|
274
|
+
* @param column The JSON column to search in
|
|
275
|
+
* @param key The JSON key to match
|
|
276
|
+
* @param value The value to match
|
|
277
|
+
*/
|
|
278
|
+
jsonLike(column: string, key: string, value: string): SqlBuilder;
|
|
279
|
+
/**
|
|
280
|
+
* Get the built query
|
|
281
|
+
* @returns Object containing the SQL string and parameters array
|
|
282
|
+
*/
|
|
283
|
+
build(): {
|
|
284
|
+
sql: string;
|
|
285
|
+
params: SqlParam[];
|
|
286
|
+
};
|
|
287
|
+
/**
|
|
288
|
+
* Reset the builder for reuse
|
|
289
|
+
* @returns The reset builder instance
|
|
290
|
+
*/
|
|
291
|
+
reset(): SqlBuilder;
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
/**
|
|
295
|
+
* Type definition for SQL query parameters
|
|
296
|
+
*/
|
|
297
|
+
export declare type SqlParam = string | number | boolean | null | undefined;
|
|
298
|
+
|
|
299
|
+
/**
|
|
300
|
+
* Interface for SQL query options with generic type support
|
|
301
|
+
*/
|
|
302
|
+
declare interface SqlQueryOptions {
|
|
303
|
+
/** SQL query to execute */
|
|
304
|
+
sql: string;
|
|
305
|
+
/** Parameters to bind to the query */
|
|
306
|
+
params?: SqlParam[];
|
|
307
|
+
/** Whether to return only the first result */
|
|
308
|
+
first?: boolean;
|
|
309
|
+
}
|
|
310
|
+
export { SqlQueryOptions }
|
|
311
|
+
export { SqlQueryOptions as SqlQueryOptions_alias_1 }
|
|
312
|
+
|
|
313
|
+
export { }
|