D1 Stored procedures can not use?

Can’t D1 stored procedures be used?

Today, I use the NextJS framework with Pages and D1 database to develop SAAS applications

To implement transaction control, I was planning to use stored procedures, but unfortunately, it didn’t work properly.

I refer to the following article

Below is my code
const { error } = await (process.env as any).DB.Procedures.Checkout(user_id, name)

My wrangler.toml file
procedures = "./src/db/procedures.js"

My procedures.js file

export default class Procedures {
    constructor(db, env, ctx) {
        this.db = db

    async Checkout(user_id, name) {
        const result = await this.db.transaction(async (txn) => {
            const res = await this.db.prepare("INSERT INTO user (user_id,name) VALUES (?,?)").bind(user_id,name).run();
            const {success} = await this.db.prepare("INSERT INTO recharge_log (user_id,status) VALUES (?,?)").bind(user_id, 0).run();
            if (!success) {
                await txn.rollback()
                return { error: 'has rolled back' }
                await txn.commit()
                return { error: 'has commit'}

        return result

I didn’t see any problem myself. Here is my error log:
"TypeError: Cannot read properties of undefined (reading 'Checkout')"

Can anyone tell me why?


D1 does not yet support Stored Procedures, this was a look at what the team was thinking around transactions. Keep an eye on the blog and the D1 changelog for when they/something like them is released.

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.