Skip to content

Commit ff717ec

Browse files
Copilotmathiasrw
andauthored
Add grammar stub for transactions when using IndexedDB to close #1969 (#2399)
Co-authored-by: copilot-swe-agent[bot] <[email protected]> Co-authored-by: mathiasrw <[email protected]>
1 parent a6e030f commit ff717ec

File tree

2 files changed

+136
-0
lines changed

2 files changed

+136
-0
lines changed

src/91indexeddb.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -453,3 +453,32 @@ IDB.updateTable = function (databaseid, tableid, assignfn, wherefn, params, cb)
453453
};
454454
};
455455
};
456+
457+
/**
458+
* Commit transaction for IndexedDB
459+
* Note: IndexedDB operations are auto-committed per operation
460+
* This method provides API compatibility
461+
*/
462+
IDB.commit = function (databaseid, cb) {
463+
// IndexedDB auto-commits each operation's transaction
464+
// No additional action needed
465+
return cb ? cb(1) : 1;
466+
};
467+
468+
/**
469+
* Begin transaction - alias to commit for IndexedDB
470+
* Similar to LOCALSTORAGE pattern
471+
*/
472+
IDB.begin = IDB.commit;
473+
474+
/**
475+
* Rollback transaction for IndexedDB
476+
* Note: IndexedDB operations are auto-committed per operation
477+
* Manual rollback not supported - operations cannot be undone
478+
*/
479+
IDB.rollback = function (databaseid, cb) {
480+
// IndexedDB auto-commits each operation
481+
// Cannot rollback already-committed operations
482+
// This provides API compatibility only
483+
return cb ? cb(1) : 1;
484+
};

test/test2361.js

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
if (typeof exports === 'object') {
2+
var assert = require('assert');
3+
var alasql = require('..');
4+
}
5+
6+
// only run in browser
7+
if (typeof exports != 'object') {
8+
describe('Test 2361 - IndexedDB transactions support', function () {
9+
const SCHEMA_NAME = 'test2361db';
10+
11+
before(function () {
12+
// Clean up any existing database
13+
return alasql.promise('DROP INDEXEDDB DATABASE IF EXISTS ' + SCHEMA_NAME);
14+
});
15+
16+
after(function () {
17+
// Clean up after tests
18+
return alasql.promise('DROP INDEXEDDB DATABASE IF EXISTS ' + SCHEMA_NAME);
19+
});
20+
21+
it('1. BEGIN TRANSACTION should work with IndexedDB', function (done) {
22+
const queriesAttach = [
23+
'CREATE INDEXEDDB DATABASE ' + SCHEMA_NAME,
24+
'ATTACH INDEXEDDB DATABASE ' + SCHEMA_NAME,
25+
'USE ' + SCHEMA_NAME,
26+
];
27+
28+
alasql
29+
.promise(queriesAttach)
30+
.then(() => alasql.promise('CREATE TABLE test_table'))
31+
.then(() => alasql.promise('BEGIN TRANSACTION'))
32+
.then(res => {
33+
assert.equal(res, 1);
34+
done();
35+
})
36+
.catch(error => {
37+
done(error);
38+
});
39+
});
40+
41+
it('2. COMMIT TRANSACTION should work with IndexedDB', function (done) {
42+
alasql
43+
.promise('USE ' + SCHEMA_NAME)
44+
.then(() => alasql.promise('BEGIN TRANSACTION'))
45+
.then(() => alasql.promise('COMMIT TRANSACTION'))
46+
.then(res => {
47+
assert.equal(res, 1);
48+
done();
49+
})
50+
.catch(error => {
51+
done(error);
52+
});
53+
});
54+
55+
it('3. ROLLBACK TRANSACTION should work with IndexedDB', function (done) {
56+
alasql
57+
.promise('USE ' + SCHEMA_NAME)
58+
.then(() => alasql.promise('BEGIN TRANSACTION'))
59+
.then(() => alasql.promise('ROLLBACK TRANSACTION'))
60+
.then(res => {
61+
assert.equal(res, 1);
62+
done();
63+
})
64+
.catch(error => {
65+
done(error);
66+
});
67+
});
68+
69+
it('4. Full transaction workflow with INSERT', function (done) {
70+
const query = "INSERT INTO test_table VALUES ('test1'), ('test2')";
71+
72+
alasql
73+
.promise('USE ' + SCHEMA_NAME)
74+
.then(() => alasql.promise('BEGIN TRANSACTION'))
75+
.then(() => alasql.promise(query))
76+
.then(() => alasql.promise('COMMIT TRANSACTION'))
77+
.then(res => {
78+
assert.equal(res, 1);
79+
done();
80+
})
81+
.catch(error => {
82+
done(error);
83+
});
84+
});
85+
86+
it('5. Transaction workflow as described in issue', function (done) {
87+
const queriesAttach = ['ATTACH INDEXEDDB DATABASE ' + SCHEMA_NAME, 'USE ' + SCHEMA_NAME];
88+
89+
const query = 'SELECT * FROM test_table';
90+
91+
alasql
92+
.promise(queriesAttach)
93+
.then(() => alasql.promise('BEGIN TRANSACTION'))
94+
.then(() => alasql.promise(query))
95+
.then(() => alasql.promise('COMMIT TRANSACTION'))
96+
.then(res => {
97+
// Should succeed without throwing the error:
98+
// "l.engines[l.databases[l.useid].engineid].begin is not a function"
99+
assert.equal(res, 1);
100+
done();
101+
})
102+
.catch(error => {
103+
done(error);
104+
});
105+
});
106+
});
107+
}

0 commit comments

Comments
 (0)