diff --git a/Magic.IndexedDb.Cli/Magic.IndexedDb.Cli.csproj b/Magic.IndexedDb.Cli/Magic.IndexedDb.Cli.csproj
index 7be280d..abcd3de 100644
--- a/Magic.IndexedDb.Cli/Magic.IndexedDb.Cli.csproj
+++ b/Magic.IndexedDb.Cli/Magic.IndexedDb.Cli.csproj
@@ -7,7 +7,7 @@
true
magicdb
Magic.IndexedDb.Cli
- 4.1.0
+ 4.1.1
Magic Man
Code-first, language-independent migration planning, validation, and static publishing for Magic IndexedDB.
https://github.com/magiccodingman/Magic.IndexedDb
diff --git a/Magic.IndexedDb/Magic.IndexedDb.csproj b/Magic.IndexedDb/Magic.IndexedDb.csproj
index 8462594..4f2c856 100644
--- a/Magic.IndexedDb/Magic.IndexedDb.csproj
+++ b/Magic.IndexedDb/Magic.IndexedDb.csproj
@@ -15,8 +15,8 @@
4.0.0.0
README.md
LICENSE.txt
- 4.1.0.0
- 4.1.0
+ 4.1.1.0
+ 4.1.1
diff --git a/Magic.IndexedDb/wwwroot/magicLinqToIndexedDb.js b/Magic.IndexedDb/wwwroot/magicLinqToIndexedDb.js
index 9b851f6..580a920 100644
--- a/Magic.IndexedDb/wwwroot/magicLinqToIndexedDb.js
+++ b/Magic.IndexedDb/wwwroot/magicLinqToIndexedDb.js
@@ -242,18 +242,15 @@ async function runIndexedQueries(db, table, universalQueries,
return await table.toArray();
}
- let queries = [];
-
- for (let query of universalQueries) {
- const coverage = coveredIndex(table, query, queryAdditions, projection);
- queries.push({ q: runIndexedQuery(table, query, queryAdditions, Boolean(coverage)), coverage });
- }
-
- let finalResults = [];
+ const finalResults = [];
await Promise.all(
- queries.map(async ({ q, coverage }) => {
- await db.transaction('r', table, async () => {
+ universalQueries.map(query => {
+ const coverage = coveredIndex(table, query, queryAdditions, projection);
+ return db.transaction('r', table, async () => {
+ // First and Last start their IndexedDB request immediately.
+ // Create that request in the transaction that awaits it.
+ const q = runIndexedQuery(table, query, queryAdditions, Boolean(coverage));
if (coverage && !(q instanceof Promise)) {
await q.eachKey((key, cursor) => {
const pk = cursor.primaryKey;
diff --git a/tests/migrations/ProtectedWasm/App/App.razor b/tests/migrations/ProtectedWasm/App/App.razor
index f9ba7f5..94e872d 100644
--- a/tests/migrations/ProtectedWasm/App/App.razor
+++ b/tests/migrations/ProtectedWasm/App/App.razor
@@ -188,6 +188,20 @@
"The cross-store batch insert failed.");
Require((await references.ToListAsync()).Single().Target == "document",
"The protected batch failed to persist the typed value.");
+ foreach (var (id, size) in new[] { (20, 400 * 1024), (21, 400 * 1024), (22, 2800 * 1024) })
+ {
+ var secret = new string((char)('a' + id - 20), size);
+ Require(await table.InsertProtectedIfCurrentAsync(new RotationRow { Id = id, Secret = secret },
+ table, advancedFence, row => row.Generation) == MagicConditionalMutationResult.Applied,
+ "The guarded setup for the protected terminal read failed.");
+ var first = await table.FirstOrDefaultAsync(row => row.Id == id);
+ var last = await table.LastOrDefaultAsync(row => row.Id == id);
+ Require(first?.Id == id && first.Secret == secret && last?.Id == id && last.Secret == secret,
+ "Indexed terminal reads did not round-trip the protected record.");
+ }
+ Require(await table.FirstOrDefaultAsync(row => row.Id == -1) is null &&
+ await table.LastOrDefaultAsync(row => row.Id == -1) is null,
+ "An absent indexed key did not return the default value.");
await Contexts.DeactivateAsync();
}
diff --git a/tests/migrations/run.mjs b/tests/migrations/run.mjs
index b5b72f7..293559c 100644
--- a/tests/migrations/run.mjs
+++ b/tests/migrations/run.mjs
@@ -1759,10 +1759,60 @@ test('rotation checkpoints preserve compound keys across protected and public st
indexedKeys: ids.map(id => ['catalog', id]), publicRow: { id: 1, label: 'Public configuration' }, oldKeyRejected: true });
});
+test('indexed first and last start their requests inside the native read transaction', async ({ page }) => {
+ const result = await page.evaluate(async () => {
+ const { default: Dexie } = await import('/engine/dexie/dexie.js');
+ const { magicQueryAsync } = await import('/engine/magicLinqToIndexedDb.js');
+ const db = new Dexie('indexed-terminal-transaction');
+ db.version(1).stores({ records: 'id, category' });
+ await db.open();
+ try {
+ const table = db.table('records');
+ await table.bulkAdd([{ id: 1, category: 7 }, { id: 2, category: 7 }]);
+ const prototype = Object.getPrototypeOf(table.toCollection());
+ let terminalCalls = 0;
+ for (const method of ['first', 'last']) {
+ const execute = prototype[method];
+ prototype[method] = function (...args) {
+ terminalCalls++;
+ if (!Dexie.currentTransaction) {
+ // Let a request started prematurely resolve on a later task.
+ // An unrelated empty read transaction cannot stay alive for it.
+ return execute.apply(this, args).then(async row => {
+ await new Promise(resolve => setTimeout(resolve, 20));
+ return row;
+ });
+ }
+ return execute.apply(this, args);
+ };
+ }
+ const query = async (property, value, additionFunction) => {
+ const before = terminalCalls;
+ const rows = await magicQueryAsync(db, table, {
+ nodeType: 1, condition: { property, operation: 'Equal', value }
+ }, [{ additionFunction, intValue: 0 }]);
+ if (terminalCalls === before) throw new Error("The query did not use the indexed terminal path.");
+ return rows.map(row => row.id);
+ };
+ return {
+ first: await query('category', 7, 'first'),
+ last: await query('category', 7, 'last'),
+ present: await query('id', 2, 'first'),
+ missing: await query('id', 3, 'last')
+ };
+ } catch (error) {
+ throw new Error(`${error.name}: ${error.message}`);
+ } finally {
+ db.close();
+ }
+ });
+ assert.deepEqual(result, { first: [1], last: [2], present: [2], missing: [] });
+});
+
registerProtectedEngineTests({ test, schema, bundle, copy, inspect, read, loadPage, recoveryPackage,
appendRevision, jsonAsset, assets, cliFixture, getOrigin: () => origin });
registerProtectedRegistrationTests({ test, schema, bundle, jsonAsset, copy, registrationStore, inspect, appendRevision, assets });
-const expectedScenarioCount = (cliFixture ? 143 : 139) + (process.env.MAGIC_ROTATION_BENCHMARK === "1" ? 9 : 0);
+const expectedScenarioCount = (cliFixture ? 144 : 140) + (process.env.MAGIC_ROTATION_BENCHMARK === "1" ? 9 : 0);
assert.equal(tests.length, expectedScenarioCount, 'Unexpected migration scenario count.');
if (listOnly) {