@mastra/cloudflare-d1 0.0.0-switch-to-core-20250424015131 → 0.0.0-vnext-inngest-20250506123700
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/_tsup-dts-rollup.d.cts +22 -4
- package/dist/_tsup-dts-rollup.d.ts +22 -4
- package/dist/index.cjs +120 -6
- package/dist/index.js +120 -6
- package/package.json +5 -5
|
@@ -2,15 +2,19 @@ import type { D1Database as D1Database_2 } from '@cloudflare/workers-types';
|
|
|
2
2
|
import type { EvalRow } from '@mastra/core/storage';
|
|
3
3
|
import { MastraStorage } from '@mastra/core/storage';
|
|
4
4
|
import type { MessageType } from '@mastra/core/memory';
|
|
5
|
+
import type { MessageType as MessageType_2 } from '@mastra/core';
|
|
5
6
|
import type { StorageColumn } from '@mastra/core/storage';
|
|
6
7
|
import type { StorageGetMessagesArg } from '@mastra/core/storage';
|
|
7
8
|
import type { StorageThreadType } from '@mastra/core/memory';
|
|
8
9
|
import type { TABLE_NAMES } from '@mastra/core/storage';
|
|
10
|
+
import type { WorkflowRun } from '@mastra/core/storage';
|
|
9
11
|
import type { WorkflowRuns } from '@mastra/core/storage';
|
|
10
12
|
import type { WorkflowRunState } from '@mastra/core/workflows';
|
|
11
13
|
import type { WorkflowRunState as WorkflowRunState_2 } from '@mastra/core';
|
|
12
14
|
|
|
13
|
-
export declare const
|
|
15
|
+
export declare const checkWorkflowSnapshot: (snapshot: WorkflowRunState_2 | string, stepId: string, status: string) => void;
|
|
16
|
+
|
|
17
|
+
export declare const createSampleMessage: (threadId: string) => MessageType_2;
|
|
14
18
|
|
|
15
19
|
export declare const createSampleThread: () => {
|
|
16
20
|
id: string;
|
|
@@ -51,7 +55,11 @@ export declare const createSampleTrace: (name: string, scope?: string, attribute
|
|
|
51
55
|
createdAt: string;
|
|
52
56
|
};
|
|
53
57
|
|
|
54
|
-
export declare const createSampleWorkflowSnapshot: (threadId: string) =>
|
|
58
|
+
export declare const createSampleWorkflowSnapshot: (threadId: string, status: string, createdAt?: Date) => {
|
|
59
|
+
snapshot: WorkflowRunState_2;
|
|
60
|
+
runId: string;
|
|
61
|
+
stepId: string;
|
|
62
|
+
};
|
|
55
63
|
|
|
56
64
|
export declare function createSqlBuilder(): SqlBuilder;
|
|
57
65
|
|
|
@@ -153,21 +161,30 @@ declare class D1Store extends MastraStorage {
|
|
|
153
161
|
tableName: TABLE_NAMES;
|
|
154
162
|
records: Record<string, any>[];
|
|
155
163
|
}): Promise<void>;
|
|
156
|
-
getTraces({ name, scope, page, perPage, attributes, }: {
|
|
164
|
+
getTraces({ name, scope, page, perPage, attributes, fromDate, toDate, }: {
|
|
157
165
|
name?: string;
|
|
158
166
|
scope?: string;
|
|
159
167
|
page: number;
|
|
160
168
|
perPage: number;
|
|
161
169
|
attributes?: Record<string, string>;
|
|
170
|
+
fromDate?: Date;
|
|
171
|
+
toDate?: Date;
|
|
162
172
|
}): Promise<Record<string, any>[]>;
|
|
163
173
|
getEvalsByAgentName(agentName: string, type?: 'test' | 'live'): Promise<EvalRow[]>;
|
|
164
|
-
|
|
174
|
+
private parseWorkflowRun;
|
|
175
|
+
private hasColumn;
|
|
176
|
+
getWorkflowRuns({ workflowName, fromDate, toDate, limit, offset, resourceId, }?: {
|
|
165
177
|
workflowName?: string;
|
|
166
178
|
fromDate?: Date;
|
|
167
179
|
toDate?: Date;
|
|
168
180
|
limit?: number;
|
|
169
181
|
offset?: number;
|
|
182
|
+
resourceId?: string;
|
|
170
183
|
}): Promise<WorkflowRuns>;
|
|
184
|
+
getWorkflowRunById({ runId, workflowName, }: {
|
|
185
|
+
runId: string;
|
|
186
|
+
workflowName?: string;
|
|
187
|
+
}): Promise<WorkflowRun | null>;
|
|
171
188
|
/**
|
|
172
189
|
* Close the database connection
|
|
173
190
|
* No explicit cleanup needed for D1 in either REST or Workers Binding mode
|
|
@@ -226,6 +243,7 @@ export declare class SqlBuilder {
|
|
|
226
243
|
orderBy(column: string, direction?: 'ASC' | 'DESC'): SqlBuilder;
|
|
227
244
|
limit(count: number): SqlBuilder;
|
|
228
245
|
offset(count: number): SqlBuilder;
|
|
246
|
+
count(): SqlBuilder;
|
|
229
247
|
/**
|
|
230
248
|
* Insert a row, or update specific columns on conflict (upsert).
|
|
231
249
|
* @param table Table name
|
|
@@ -2,15 +2,19 @@ import type { D1Database as D1Database_2 } from '@cloudflare/workers-types';
|
|
|
2
2
|
import type { EvalRow } from '@mastra/core/storage';
|
|
3
3
|
import { MastraStorage } from '@mastra/core/storage';
|
|
4
4
|
import type { MessageType } from '@mastra/core/memory';
|
|
5
|
+
import type { MessageType as MessageType_2 } from '@mastra/core';
|
|
5
6
|
import type { StorageColumn } from '@mastra/core/storage';
|
|
6
7
|
import type { StorageGetMessagesArg } from '@mastra/core/storage';
|
|
7
8
|
import type { StorageThreadType } from '@mastra/core/memory';
|
|
8
9
|
import type { TABLE_NAMES } from '@mastra/core/storage';
|
|
10
|
+
import type { WorkflowRun } from '@mastra/core/storage';
|
|
9
11
|
import type { WorkflowRuns } from '@mastra/core/storage';
|
|
10
12
|
import type { WorkflowRunState } from '@mastra/core/workflows';
|
|
11
13
|
import type { WorkflowRunState as WorkflowRunState_2 } from '@mastra/core';
|
|
12
14
|
|
|
13
|
-
export declare const
|
|
15
|
+
export declare const checkWorkflowSnapshot: (snapshot: WorkflowRunState_2 | string, stepId: string, status: string) => void;
|
|
16
|
+
|
|
17
|
+
export declare const createSampleMessage: (threadId: string) => MessageType_2;
|
|
14
18
|
|
|
15
19
|
export declare const createSampleThread: () => {
|
|
16
20
|
id: string;
|
|
@@ -51,7 +55,11 @@ export declare const createSampleTrace: (name: string, scope?: string, attribute
|
|
|
51
55
|
createdAt: string;
|
|
52
56
|
};
|
|
53
57
|
|
|
54
|
-
export declare const createSampleWorkflowSnapshot: (threadId: string) =>
|
|
58
|
+
export declare const createSampleWorkflowSnapshot: (threadId: string, status: string, createdAt?: Date) => {
|
|
59
|
+
snapshot: WorkflowRunState_2;
|
|
60
|
+
runId: string;
|
|
61
|
+
stepId: string;
|
|
62
|
+
};
|
|
55
63
|
|
|
56
64
|
export declare function createSqlBuilder(): SqlBuilder;
|
|
57
65
|
|
|
@@ -153,21 +161,30 @@ declare class D1Store extends MastraStorage {
|
|
|
153
161
|
tableName: TABLE_NAMES;
|
|
154
162
|
records: Record<string, any>[];
|
|
155
163
|
}): Promise<void>;
|
|
156
|
-
getTraces({ name, scope, page, perPage, attributes, }: {
|
|
164
|
+
getTraces({ name, scope, page, perPage, attributes, fromDate, toDate, }: {
|
|
157
165
|
name?: string;
|
|
158
166
|
scope?: string;
|
|
159
167
|
page: number;
|
|
160
168
|
perPage: number;
|
|
161
169
|
attributes?: Record<string, string>;
|
|
170
|
+
fromDate?: Date;
|
|
171
|
+
toDate?: Date;
|
|
162
172
|
}): Promise<Record<string, any>[]>;
|
|
163
173
|
getEvalsByAgentName(agentName: string, type?: 'test' | 'live'): Promise<EvalRow[]>;
|
|
164
|
-
|
|
174
|
+
private parseWorkflowRun;
|
|
175
|
+
private hasColumn;
|
|
176
|
+
getWorkflowRuns({ workflowName, fromDate, toDate, limit, offset, resourceId, }?: {
|
|
165
177
|
workflowName?: string;
|
|
166
178
|
fromDate?: Date;
|
|
167
179
|
toDate?: Date;
|
|
168
180
|
limit?: number;
|
|
169
181
|
offset?: number;
|
|
182
|
+
resourceId?: string;
|
|
170
183
|
}): Promise<WorkflowRuns>;
|
|
184
|
+
getWorkflowRunById({ runId, workflowName, }: {
|
|
185
|
+
runId: string;
|
|
186
|
+
workflowName?: string;
|
|
187
|
+
}): Promise<WorkflowRun | null>;
|
|
171
188
|
/**
|
|
172
189
|
* Close the database connection
|
|
173
190
|
* No explicit cleanup needed for D1 in either REST or Workers Binding mode
|
|
@@ -226,6 +243,7 @@ export declare class SqlBuilder {
|
|
|
226
243
|
orderBy(column: string, direction?: 'ASC' | 'DESC'): SqlBuilder;
|
|
227
244
|
limit(count: number): SqlBuilder;
|
|
228
245
|
offset(count: number): SqlBuilder;
|
|
246
|
+
count(): SqlBuilder;
|
|
229
247
|
/**
|
|
230
248
|
* Insert a row, or update specific columns on conflict (upsert).
|
|
231
249
|
* @param table Table name
|
package/dist/index.cjs
CHANGED
|
@@ -74,6 +74,10 @@ var SqlBuilder = class {
|
|
|
74
74
|
this.params.push(count);
|
|
75
75
|
return this;
|
|
76
76
|
}
|
|
77
|
+
count() {
|
|
78
|
+
this.sql += "SELECT COUNT(*) AS count";
|
|
79
|
+
return this;
|
|
80
|
+
}
|
|
77
81
|
/**
|
|
78
82
|
* Insert a row, or update specific columns on conflict (upsert).
|
|
79
83
|
* @param table Table name
|
|
@@ -496,7 +500,8 @@ var D1Store = class extends storage.MastraStorage {
|
|
|
496
500
|
try {
|
|
497
501
|
await this.executeQuery({ sql, params });
|
|
498
502
|
} catch (error) {
|
|
499
|
-
|
|
503
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
504
|
+
this.logger.error(`Error inserting into ${fullTableName}:`, { message });
|
|
500
505
|
throw new Error(`Failed to insert into ${fullTableName}: ${error}`);
|
|
501
506
|
}
|
|
502
507
|
}
|
|
@@ -594,7 +599,8 @@ var D1Store = class extends storage.MastraStorage {
|
|
|
594
599
|
await this.executeQuery({ sql, params });
|
|
595
600
|
return thread;
|
|
596
601
|
} catch (error) {
|
|
597
|
-
|
|
602
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
603
|
+
this.logger.error(`Error saving thread to ${fullTableName}:`, { message });
|
|
598
604
|
throw error;
|
|
599
605
|
}
|
|
600
606
|
}
|
|
@@ -628,7 +634,8 @@ var D1Store = class extends storage.MastraStorage {
|
|
|
628
634
|
updatedAt: /* @__PURE__ */ new Date()
|
|
629
635
|
};
|
|
630
636
|
} catch (error) {
|
|
631
|
-
|
|
637
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
638
|
+
this.logger.error("Error updating thread:", { message });
|
|
632
639
|
throw error;
|
|
633
640
|
}
|
|
634
641
|
}
|
|
@@ -862,7 +869,9 @@ var D1Store = class extends storage.MastraStorage {
|
|
|
862
869
|
scope,
|
|
863
870
|
page,
|
|
864
871
|
perPage,
|
|
865
|
-
attributes
|
|
872
|
+
attributes,
|
|
873
|
+
fromDate,
|
|
874
|
+
toDate
|
|
866
875
|
}) {
|
|
867
876
|
const fullTableName = this.getTableName(storage.TABLE_TRACES);
|
|
868
877
|
try {
|
|
@@ -878,6 +887,12 @@ var D1Store = class extends storage.MastraStorage {
|
|
|
878
887
|
query.jsonLike("attributes", key, value);
|
|
879
888
|
}
|
|
880
889
|
}
|
|
890
|
+
if (fromDate) {
|
|
891
|
+
query.andWhere("createdAt >= ?", fromDate instanceof Date ? fromDate.toISOString() : fromDate);
|
|
892
|
+
}
|
|
893
|
+
if (toDate) {
|
|
894
|
+
query.andWhere("createdAt <= ?", toDate instanceof Date ? toDate.toISOString() : toDate);
|
|
895
|
+
}
|
|
881
896
|
query.orderBy("startTime", "DESC").limit(perPage).offset((page - 1) * perPage);
|
|
882
897
|
const { sql, params } = query.build();
|
|
883
898
|
const results = await this.executeQuery({ sql, params });
|
|
@@ -929,8 +944,107 @@ var D1Store = class extends storage.MastraStorage {
|
|
|
929
944
|
return [];
|
|
930
945
|
}
|
|
931
946
|
}
|
|
932
|
-
|
|
933
|
-
|
|
947
|
+
parseWorkflowRun(row) {
|
|
948
|
+
let parsedSnapshot = row.snapshot;
|
|
949
|
+
if (typeof parsedSnapshot === "string") {
|
|
950
|
+
try {
|
|
951
|
+
parsedSnapshot = JSON.parse(row.snapshot);
|
|
952
|
+
} catch (e) {
|
|
953
|
+
console.warn(`Failed to parse snapshot for workflow ${row.workflow_name}: ${e}`);
|
|
954
|
+
}
|
|
955
|
+
}
|
|
956
|
+
return {
|
|
957
|
+
workflowName: row.workflow_name,
|
|
958
|
+
runId: row.run_id,
|
|
959
|
+
snapshot: parsedSnapshot,
|
|
960
|
+
createdAt: this.ensureDate(row.createdAt),
|
|
961
|
+
updatedAt: this.ensureDate(row.updatedAt),
|
|
962
|
+
resourceId: row.resourceId
|
|
963
|
+
};
|
|
964
|
+
}
|
|
965
|
+
async hasColumn(table, column) {
|
|
966
|
+
const sql = `PRAGMA table_info(${table});`;
|
|
967
|
+
const result = await this.executeQuery({ sql, params: [] });
|
|
968
|
+
if (!result || !Array.isArray(result)) return false;
|
|
969
|
+
return result.some((col) => col.name === column || col.name === column.toLowerCase());
|
|
970
|
+
}
|
|
971
|
+
async getWorkflowRuns({
|
|
972
|
+
workflowName,
|
|
973
|
+
fromDate,
|
|
974
|
+
toDate,
|
|
975
|
+
limit,
|
|
976
|
+
offset,
|
|
977
|
+
resourceId
|
|
978
|
+
} = {}) {
|
|
979
|
+
const fullTableName = this.getTableName(storage.TABLE_WORKFLOW_SNAPSHOT);
|
|
980
|
+
try {
|
|
981
|
+
const builder = createSqlBuilder().select().from(fullTableName);
|
|
982
|
+
const countBuilder = createSqlBuilder().count().from(fullTableName);
|
|
983
|
+
if (workflowName) builder.whereAnd("workflow_name = ?", workflowName);
|
|
984
|
+
if (resourceId) {
|
|
985
|
+
const hasResourceId = await this.hasColumn(fullTableName, "resourceId");
|
|
986
|
+
if (hasResourceId) {
|
|
987
|
+
builder.whereAnd("resourceId = ?", resourceId);
|
|
988
|
+
countBuilder.whereAnd("resourceId = ?", resourceId);
|
|
989
|
+
} else {
|
|
990
|
+
console.warn(`[${fullTableName}] resourceId column not found. Skipping resourceId filter.`);
|
|
991
|
+
}
|
|
992
|
+
}
|
|
993
|
+
if (fromDate) {
|
|
994
|
+
builder.whereAnd("createdAt >= ?", fromDate instanceof Date ? fromDate.toISOString() : fromDate);
|
|
995
|
+
countBuilder.whereAnd("createdAt >= ?", fromDate instanceof Date ? fromDate.toISOString() : fromDate);
|
|
996
|
+
}
|
|
997
|
+
if (toDate) {
|
|
998
|
+
builder.whereAnd("createdAt <= ?", toDate instanceof Date ? toDate.toISOString() : toDate);
|
|
999
|
+
countBuilder.whereAnd("createdAt <= ?", toDate instanceof Date ? toDate.toISOString() : toDate);
|
|
1000
|
+
}
|
|
1001
|
+
builder.orderBy("createdAt", "DESC");
|
|
1002
|
+
if (typeof limit === "number") builder.limit(limit);
|
|
1003
|
+
if (typeof offset === "number") builder.offset(offset);
|
|
1004
|
+
const { sql, params } = builder.build();
|
|
1005
|
+
let total = 0;
|
|
1006
|
+
if (limit !== void 0 && offset !== void 0) {
|
|
1007
|
+
const { sql: countSql, params: countParams } = countBuilder.build();
|
|
1008
|
+
const countResult = await this.executeQuery({ sql: countSql, params: countParams, first: true });
|
|
1009
|
+
total = Number(countResult?.count ?? 0);
|
|
1010
|
+
}
|
|
1011
|
+
const results = await this.executeQuery({ sql, params });
|
|
1012
|
+
const runs = (isArrayOfRecords(results) ? results : []).map((row) => this.parseWorkflowRun(row));
|
|
1013
|
+
return { runs, total: total || runs.length };
|
|
1014
|
+
} catch (error) {
|
|
1015
|
+
this.logger.error("Error getting workflow runs:", {
|
|
1016
|
+
message: error instanceof Error ? error.message : String(error)
|
|
1017
|
+
});
|
|
1018
|
+
throw error;
|
|
1019
|
+
}
|
|
1020
|
+
}
|
|
1021
|
+
async getWorkflowRunById({
|
|
1022
|
+
runId,
|
|
1023
|
+
workflowName
|
|
1024
|
+
}) {
|
|
1025
|
+
const fullTableName = this.getTableName(storage.TABLE_WORKFLOW_SNAPSHOT);
|
|
1026
|
+
try {
|
|
1027
|
+
const conditions = [];
|
|
1028
|
+
const params = [];
|
|
1029
|
+
if (runId) {
|
|
1030
|
+
conditions.push("run_id = ?");
|
|
1031
|
+
params.push(runId);
|
|
1032
|
+
}
|
|
1033
|
+
if (workflowName) {
|
|
1034
|
+
conditions.push("workflow_name = ?");
|
|
1035
|
+
params.push(workflowName);
|
|
1036
|
+
}
|
|
1037
|
+
const whereClause = conditions.length > 0 ? "WHERE " + conditions.join(" AND ") : "";
|
|
1038
|
+
const sql = `SELECT * FROM ${fullTableName} ${whereClause} ORDER BY createdAt DESC LIMIT 1`;
|
|
1039
|
+
const result = await this.executeQuery({ sql, params, first: true });
|
|
1040
|
+
if (!result) return null;
|
|
1041
|
+
return this.parseWorkflowRun(result);
|
|
1042
|
+
} catch (error) {
|
|
1043
|
+
this.logger.error("Error getting workflow run by ID:", {
|
|
1044
|
+
message: error instanceof Error ? error.message : String(error)
|
|
1045
|
+
});
|
|
1046
|
+
throw error;
|
|
1047
|
+
}
|
|
934
1048
|
}
|
|
935
1049
|
/**
|
|
936
1050
|
* Close the database connection
|
package/dist/index.js
CHANGED
|
@@ -68,6 +68,10 @@ var SqlBuilder = class {
|
|
|
68
68
|
this.params.push(count);
|
|
69
69
|
return this;
|
|
70
70
|
}
|
|
71
|
+
count() {
|
|
72
|
+
this.sql += "SELECT COUNT(*) AS count";
|
|
73
|
+
return this;
|
|
74
|
+
}
|
|
71
75
|
/**
|
|
72
76
|
* Insert a row, or update specific columns on conflict (upsert).
|
|
73
77
|
* @param table Table name
|
|
@@ -490,7 +494,8 @@ var D1Store = class extends MastraStorage {
|
|
|
490
494
|
try {
|
|
491
495
|
await this.executeQuery({ sql, params });
|
|
492
496
|
} catch (error) {
|
|
493
|
-
|
|
497
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
498
|
+
this.logger.error(`Error inserting into ${fullTableName}:`, { message });
|
|
494
499
|
throw new Error(`Failed to insert into ${fullTableName}: ${error}`);
|
|
495
500
|
}
|
|
496
501
|
}
|
|
@@ -588,7 +593,8 @@ var D1Store = class extends MastraStorage {
|
|
|
588
593
|
await this.executeQuery({ sql, params });
|
|
589
594
|
return thread;
|
|
590
595
|
} catch (error) {
|
|
591
|
-
|
|
596
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
597
|
+
this.logger.error(`Error saving thread to ${fullTableName}:`, { message });
|
|
592
598
|
throw error;
|
|
593
599
|
}
|
|
594
600
|
}
|
|
@@ -622,7 +628,8 @@ var D1Store = class extends MastraStorage {
|
|
|
622
628
|
updatedAt: /* @__PURE__ */ new Date()
|
|
623
629
|
};
|
|
624
630
|
} catch (error) {
|
|
625
|
-
|
|
631
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
632
|
+
this.logger.error("Error updating thread:", { message });
|
|
626
633
|
throw error;
|
|
627
634
|
}
|
|
628
635
|
}
|
|
@@ -856,7 +863,9 @@ var D1Store = class extends MastraStorage {
|
|
|
856
863
|
scope,
|
|
857
864
|
page,
|
|
858
865
|
perPage,
|
|
859
|
-
attributes
|
|
866
|
+
attributes,
|
|
867
|
+
fromDate,
|
|
868
|
+
toDate
|
|
860
869
|
}) {
|
|
861
870
|
const fullTableName = this.getTableName(TABLE_TRACES);
|
|
862
871
|
try {
|
|
@@ -872,6 +881,12 @@ var D1Store = class extends MastraStorage {
|
|
|
872
881
|
query.jsonLike("attributes", key, value);
|
|
873
882
|
}
|
|
874
883
|
}
|
|
884
|
+
if (fromDate) {
|
|
885
|
+
query.andWhere("createdAt >= ?", fromDate instanceof Date ? fromDate.toISOString() : fromDate);
|
|
886
|
+
}
|
|
887
|
+
if (toDate) {
|
|
888
|
+
query.andWhere("createdAt <= ?", toDate instanceof Date ? toDate.toISOString() : toDate);
|
|
889
|
+
}
|
|
875
890
|
query.orderBy("startTime", "DESC").limit(perPage).offset((page - 1) * perPage);
|
|
876
891
|
const { sql, params } = query.build();
|
|
877
892
|
const results = await this.executeQuery({ sql, params });
|
|
@@ -923,8 +938,107 @@ var D1Store = class extends MastraStorage {
|
|
|
923
938
|
return [];
|
|
924
939
|
}
|
|
925
940
|
}
|
|
926
|
-
|
|
927
|
-
|
|
941
|
+
parseWorkflowRun(row) {
|
|
942
|
+
let parsedSnapshot = row.snapshot;
|
|
943
|
+
if (typeof parsedSnapshot === "string") {
|
|
944
|
+
try {
|
|
945
|
+
parsedSnapshot = JSON.parse(row.snapshot);
|
|
946
|
+
} catch (e) {
|
|
947
|
+
console.warn(`Failed to parse snapshot for workflow ${row.workflow_name}: ${e}`);
|
|
948
|
+
}
|
|
949
|
+
}
|
|
950
|
+
return {
|
|
951
|
+
workflowName: row.workflow_name,
|
|
952
|
+
runId: row.run_id,
|
|
953
|
+
snapshot: parsedSnapshot,
|
|
954
|
+
createdAt: this.ensureDate(row.createdAt),
|
|
955
|
+
updatedAt: this.ensureDate(row.updatedAt),
|
|
956
|
+
resourceId: row.resourceId
|
|
957
|
+
};
|
|
958
|
+
}
|
|
959
|
+
async hasColumn(table, column) {
|
|
960
|
+
const sql = `PRAGMA table_info(${table});`;
|
|
961
|
+
const result = await this.executeQuery({ sql, params: [] });
|
|
962
|
+
if (!result || !Array.isArray(result)) return false;
|
|
963
|
+
return result.some((col) => col.name === column || col.name === column.toLowerCase());
|
|
964
|
+
}
|
|
965
|
+
async getWorkflowRuns({
|
|
966
|
+
workflowName,
|
|
967
|
+
fromDate,
|
|
968
|
+
toDate,
|
|
969
|
+
limit,
|
|
970
|
+
offset,
|
|
971
|
+
resourceId
|
|
972
|
+
} = {}) {
|
|
973
|
+
const fullTableName = this.getTableName(TABLE_WORKFLOW_SNAPSHOT);
|
|
974
|
+
try {
|
|
975
|
+
const builder = createSqlBuilder().select().from(fullTableName);
|
|
976
|
+
const countBuilder = createSqlBuilder().count().from(fullTableName);
|
|
977
|
+
if (workflowName) builder.whereAnd("workflow_name = ?", workflowName);
|
|
978
|
+
if (resourceId) {
|
|
979
|
+
const hasResourceId = await this.hasColumn(fullTableName, "resourceId");
|
|
980
|
+
if (hasResourceId) {
|
|
981
|
+
builder.whereAnd("resourceId = ?", resourceId);
|
|
982
|
+
countBuilder.whereAnd("resourceId = ?", resourceId);
|
|
983
|
+
} else {
|
|
984
|
+
console.warn(`[${fullTableName}] resourceId column not found. Skipping resourceId filter.`);
|
|
985
|
+
}
|
|
986
|
+
}
|
|
987
|
+
if (fromDate) {
|
|
988
|
+
builder.whereAnd("createdAt >= ?", fromDate instanceof Date ? fromDate.toISOString() : fromDate);
|
|
989
|
+
countBuilder.whereAnd("createdAt >= ?", fromDate instanceof Date ? fromDate.toISOString() : fromDate);
|
|
990
|
+
}
|
|
991
|
+
if (toDate) {
|
|
992
|
+
builder.whereAnd("createdAt <= ?", toDate instanceof Date ? toDate.toISOString() : toDate);
|
|
993
|
+
countBuilder.whereAnd("createdAt <= ?", toDate instanceof Date ? toDate.toISOString() : toDate);
|
|
994
|
+
}
|
|
995
|
+
builder.orderBy("createdAt", "DESC");
|
|
996
|
+
if (typeof limit === "number") builder.limit(limit);
|
|
997
|
+
if (typeof offset === "number") builder.offset(offset);
|
|
998
|
+
const { sql, params } = builder.build();
|
|
999
|
+
let total = 0;
|
|
1000
|
+
if (limit !== void 0 && offset !== void 0) {
|
|
1001
|
+
const { sql: countSql, params: countParams } = countBuilder.build();
|
|
1002
|
+
const countResult = await this.executeQuery({ sql: countSql, params: countParams, first: true });
|
|
1003
|
+
total = Number(countResult?.count ?? 0);
|
|
1004
|
+
}
|
|
1005
|
+
const results = await this.executeQuery({ sql, params });
|
|
1006
|
+
const runs = (isArrayOfRecords(results) ? results : []).map((row) => this.parseWorkflowRun(row));
|
|
1007
|
+
return { runs, total: total || runs.length };
|
|
1008
|
+
} catch (error) {
|
|
1009
|
+
this.logger.error("Error getting workflow runs:", {
|
|
1010
|
+
message: error instanceof Error ? error.message : String(error)
|
|
1011
|
+
});
|
|
1012
|
+
throw error;
|
|
1013
|
+
}
|
|
1014
|
+
}
|
|
1015
|
+
async getWorkflowRunById({
|
|
1016
|
+
runId,
|
|
1017
|
+
workflowName
|
|
1018
|
+
}) {
|
|
1019
|
+
const fullTableName = this.getTableName(TABLE_WORKFLOW_SNAPSHOT);
|
|
1020
|
+
try {
|
|
1021
|
+
const conditions = [];
|
|
1022
|
+
const params = [];
|
|
1023
|
+
if (runId) {
|
|
1024
|
+
conditions.push("run_id = ?");
|
|
1025
|
+
params.push(runId);
|
|
1026
|
+
}
|
|
1027
|
+
if (workflowName) {
|
|
1028
|
+
conditions.push("workflow_name = ?");
|
|
1029
|
+
params.push(workflowName);
|
|
1030
|
+
}
|
|
1031
|
+
const whereClause = conditions.length > 0 ? "WHERE " + conditions.join(" AND ") : "";
|
|
1032
|
+
const sql = `SELECT * FROM ${fullTableName} ${whereClause} ORDER BY createdAt DESC LIMIT 1`;
|
|
1033
|
+
const result = await this.executeQuery({ sql, params, first: true });
|
|
1034
|
+
if (!result) return null;
|
|
1035
|
+
return this.parseWorkflowRun(result);
|
|
1036
|
+
} catch (error) {
|
|
1037
|
+
this.logger.error("Error getting workflow run by ID:", {
|
|
1038
|
+
message: error instanceof Error ? error.message : String(error)
|
|
1039
|
+
});
|
|
1040
|
+
throw error;
|
|
1041
|
+
}
|
|
928
1042
|
}
|
|
929
1043
|
/**
|
|
930
1044
|
* Close the database connection
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mastra/cloudflare-d1",
|
|
3
|
-
"version": "0.0.0-
|
|
3
|
+
"version": "0.0.0-vnext-inngest-20250506123700",
|
|
4
4
|
"description": "D1 provider for Mastra - includes db storage capabilities",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"files": [
|
|
@@ -23,19 +23,19 @@
|
|
|
23
23
|
},
|
|
24
24
|
"dependencies": {
|
|
25
25
|
"cloudflare": "^4.1.0",
|
|
26
|
-
"@mastra/core": "0.0.0-
|
|
26
|
+
"@mastra/core": "0.0.0-vnext-inngest-20250506123700"
|
|
27
27
|
},
|
|
28
28
|
"devDependencies": {
|
|
29
29
|
"@cloudflare/workers-types": "^4.20250417.0",
|
|
30
|
-
"@microsoft/api-extractor": "^7.52.
|
|
30
|
+
"@microsoft/api-extractor": "^7.52.5",
|
|
31
31
|
"@types/node": "^20.17.27",
|
|
32
32
|
"dotenv": "^16.4.7",
|
|
33
33
|
"eslint": "^9.23.0",
|
|
34
34
|
"miniflare": "^4.20250410.1",
|
|
35
35
|
"tsup": "^8.4.0",
|
|
36
36
|
"typescript": "^5.8.2",
|
|
37
|
-
"vitest": "^3.
|
|
38
|
-
"@internal/lint": "0.0.
|
|
37
|
+
"vitest": "^3.1.2",
|
|
38
|
+
"@internal/lint": "0.0.0-vnext-inngest-20250506123700"
|
|
39
39
|
},
|
|
40
40
|
"scripts": {
|
|
41
41
|
"build": "tsup src/index.ts --format esm,cjs --experimental-dts --clean --treeshake=smallest --splitting",
|