Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for post insert triggers for indexdb #1735

Merged
merged 2 commits into from
Jul 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/91indexeddb.js
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,8 @@ IDB.dropTable = async function (databaseid, tableid, ifexists, cb) {
IDB.intoTable = function (databaseid, tableid, value, columns, cb) {
const ixdbid = alasql.databases[databaseid].ixdbid;
const request = indexedDB.open(ixdbid);
var db = alasql.databases[databaseid];
var table = db.tables[tableid];

request.onupgradeneeded = (evt) => {
evt.target.transaction.abort();
Expand All @@ -302,6 +304,16 @@ IDB.intoTable = function (databaseid, tableid, value, columns, cb) {
}
tx.oncomplete = function () {
ixdb.close();
for (var tr in table.afterinsert) {
if (table.afterinsert[tr]) {
var trigger = table.afterinsert[tr];
if (trigger.funcid) {
alasql.fn[trigger.funcid](value);
} else if (trigger.statement) {
trigger.statement.execute(databaseid);
}
}
}
if (cb) cb(ilen);
};
};
Expand Down
55 changes: 55 additions & 0 deletions test/test1409.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
if (typeof exports === 'object') {
var assert = require('assert');
var alasql = require('..');
}

// only run in browser
if (typeof exports != 'object') {
describe('Test 1409 - post insert triggers should run on indexdb', function () {
before(
() => alasql.promise('DROP IndexedDB DATABASE IF EXISTS test_db;') // delete indexeddb
);

it('post insert trigger after adding some data', function (done) {
var count = 0;
alasql.fn.onInsert = function (r) {
count++;
console.log('this never happens!');
};

return alasql
.promise(
'CREATE INDEXEDDB DATABASE IF NOT EXISTS test_db;' +
'ATTACH INDEXEDDB DATABASE test_db; ' +
'USE test_db;'
)
.then(function () {
return alasql.promise('DROP TABLE IF EXISTS asset7');
})
.then(function () {
return alasql.promise(
'CREATE TABLE asset7([id] varchar(36) NOT NULL, [name] varchar(45) NOT NULL, PRIMARY KEY ([id]) );'
);
})
.then(function () {
var data = [
{id: 'abc1', name: 'test1', amount: 7},
{id: 'abc2', name: 'test2', amount: 8},
{id: 'abc3', name: 'test3', amount: 9},
];
return alasql.promise('INSERT INTO asset7 SELECT * FROM ?', [data]);
})
.then(function () {
return alasql.promise('CREATE TRIGGER mytrigger after INSERT ON asset7 onInsert');
})
.then(function () {
var data2 = [{id: 'abc4', name: 'test17', amount: 17}];
return alasql.promise(`INSERT INTO asset7 SELECT * FROM ?`, [data2]);
})
.then(function () {
assert.equal(count, 1);
done();
});
});
});
}
Loading