How to use cloudflare agents sqlite db with drizzle?
this.ctx.storage.sql: SqlStorage { databaseSize: 40960 }
✘ [ERROR] Error initializing Agent: Migrations Failed: TypeError: this.client.transactionSync is not a function
this.ctx.storage.sql: SqlStorage { databaseSize: 40960 }
✘ [ERROR] Error initializing Agent: Migrations Failed: TypeError: this.client.transactionSync is not a function
import { Agent } from "agents";
import migrations from './db/drizzle/migrations';
import { counter } from "./db/schema";
import { migrate } from "drizzle-orm/durable-sqlite/migrator";
import { drizzle } from "drizzle-orm/durable-sqlite";
class AgentDurableObject extends Agent {
async onStart(request) {
try {
this.db = drizzle(this.ctx.storage.sql, { logger: true });
console.log("this.ctx.storage.sql: ", this.ctx.storage.sql);
await this._migrate();
console.log("Agent Started: Migrations Applied Successfully")
} catch (error) {
console.error("Error initializing Agent: Migrations Failed: ", error);
}
}
async onRequest(request) {
if (!this.sql) {
return new Response(JSON.stringify({ error: "SQL instance not initialized" }), {
status: 500,
headers: { "Content-Type": "application/json" },
});
}
const result = await this.db.select().from(counter)
return new Response(JSON.stringify({ counter: result }), {
headers: { "Content-Type": "application/json" },
});
}
async _migrate() {
await migrate(this.db, migrations);
}
}
import { Agent } from "agents";
import migrations from './db/drizzle/migrations';
import { counter } from "./db/schema";
import { migrate } from "drizzle-orm/durable-sqlite/migrator";
import { drizzle } from "drizzle-orm/durable-sqlite";
class AgentDurableObject extends Agent {
async onStart(request) {
try {
this.db = drizzle(this.ctx.storage.sql, { logger: true });
console.log("this.ctx.storage.sql: ", this.ctx.storage.sql);
await this._migrate();
console.log("Agent Started: Migrations Applied Successfully")
} catch (error) {
console.error("Error initializing Agent: Migrations Failed: ", error);
}
}
async onRequest(request) {
if (!this.sql) {
return new Response(JSON.stringify({ error: "SQL instance not initialized" }), {
status: 500,
headers: { "Content-Type": "application/json" },
});
}
const result = await this.db.select().from(counter)
return new Response(JSON.stringify({ counter: result }), {
headers: { "Content-Type": "application/json" },
});
}
async _migrate() {
await migrate(this.db, migrations);
}
}
0 Replies