xdenser / node-firebird-libfbclient

Firebird SQL binding
MIT License
82 stars 34 forks source link

insert blob in startNewTransaction return 'invalid BLOB ID' #134

Open sqllyw opened 1 year ago

sqllyw commented 1 year ago

Hi,

Following this link, I came out following working code:

 append_item_photo(itemno: number, caption: string, filename: string) {

        return new Promise((resolve, reject) => {

            try {

                let sql = `insert into picture(PICTNO, CAPTION, ITEMNO, JPEG) VALUES(GEN_ID(PICTNO_GEN, 1 ), '${caption}', ${itemno}, ? )`
                var stmt = this.con.prepareSync(sql);

                var blob = this.con.newBlobSync();
                var strm = new fb.Stream(blob);
                var fstrm = fs.createReadStream(filename);

                fstrm.resume();
                fstrm.pipe(strm);

                let cn = this.con

                strm.on("close", function () {
                    stmt.execSync(blob)
                    cn.commitSync();
                    resolve(true)
                    //    cb();
                });
                strm.on('error', function (err) {
                    console.log('error in write blob stream ', err);
                })
            } catch (error) {
                resolve(false)
            }
        })
    }

Now, I'd like to do this in a specific transaction, following is the code, but got 'invalid BLOB ID':

append_item_photo(itemno: number, caption: string, filename: string) {

        return new Promise((resolve, reject) => {

            this.con.startNewTransaction(async (err, tx) => {

                try {

                    let sql = `insert into picture(PICTNO, CAPTION, ITEMNO, JPEG) VALUES(GEN_ID(PICTNO_GEN, 1 ), '${caption}', ${itemno}, ? )`
                    var stmt = tx.prepareSync(sql);

                    var blob = this.con.newBlobSync();  // this uses default transaction?
                    var strm = new fb.Stream(blob);
                    var fstrm = fs.createReadStream(filename);

                    fstrm.resume();
                    fstrm.pipe(strm);

                    strm.on("close", function () {
                        stmt.execInTransSync(tx, blob)
                        tx.commitSync();
                        resolve(true)
                    });
                    strm.on('error', function (err) {
                        console.log('error in write blob stream ', err);
                    })

                } catch (error) {
                    resolve(false)
                }
            });

        })

    }

looks like :

var blob = this.con.newBlobSync();

it should have been

    var blob = tx.newBlobSync();

but Transaction does not have newBlobSync()

any idea how to do it? Thanks