Skip to content

Commit

Permalink
Merge pull request #401 from aparzi/fix-connection-mode-open-cursor
Browse files Browse the repository at this point in the history
Fix connection mode open cursor
  • Loading branch information
aparzi authored Oct 2, 2024
2 parents df37e4b + b3eb98c commit 73dd358
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 13 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ngx-indexed-db",
"version": "19.0.0",
"version": "19.0.1",
"files": [
"dist/ngx-indexed-db/**/*"
],
Expand Down
2 changes: 1 addition & 1 deletion projects/ngx-indexed-db/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "ngx-indexed-db",
"author": "Charles Assunção",
"description": "Angular wrapper to IndexedDB database.",
"version": "19.0.0",
"version": "19.0.1",
"license": "ISC",
"private": false,
"keywords": [
Expand Down
2 changes: 1 addition & 1 deletion projects/ngx-indexed-db/src/lib/ngx-indexed-db.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -533,7 +533,7 @@ export class NgxIndexedDBService {
openDatabase(this.indexedDB, this.dbConfig.name, this.dbConfig.version)
.then((db) => {
validateBeforeTransaction(db, storeName, (e) => obs.error(e));
const transaction = createTransaction(db, optionsGenerator(DBMode.readonly, storeName, obs.error));
const transaction = createTransaction(db, optionsGenerator(DBMode.readwrite, storeName, obs.error));
const objectStore = transaction.objectStore(storeName);
const request = keyRange === undefined ? objectStore.openCursor() : objectStore.openCursor(keyRange, direction);

Expand Down
1 change: 1 addition & 0 deletions projects/playground/src/app/app.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
<button (click)="getByKey()">getByKey</button>
<button (click)="bulkDelete()">bulkDelete</button>
<button (click)="versionDatabase()">versionDatabase</button>
<button (click)="testUpdateCursor()">Test update cursor</button>
<hr />
<div>
<input type="text" [(ngModel)]="storeName" name="store" #store />
Expand Down
38 changes: 28 additions & 10 deletions projects/playground/src/app/app.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ import { NgxIndexedDBService } from './../../../ngx-indexed-db/src/lib/ngx-index
import { forkJoin, of, throwError } from 'rxjs';
import { catchError, switchMap, tap, throttle } from 'rxjs/operators';
import { Component } from '@angular/core';

@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.scss'],
styleUrls: ['./app.component.scss']
})
export class AppComponent {
title = 'playground';
Expand All @@ -20,8 +21,8 @@ export class AppComponent {
add(): void {
//prepare random person data with or without email for count by index
let randomPerson = {
name: `charles number ${Math.random() * 10}`,
}
name: `charles number ${Math.random() * 10}`
};
if (Math.random().toFixed(0) === '1') {
randomPerson['email'] = `email number ${Math.random() * 10}`;
}
Expand Down Expand Up @@ -54,7 +55,7 @@ export class AppComponent {
addToTest(): void {
this.dbService
.add('test', {
name: `charles number`,
name: `charles number`
})
.pipe(
catchError((x) => {
Expand All @@ -79,7 +80,7 @@ export class AppComponent {
bulkPut(): void {
const people = [];
for (let i = 0; i < 100_000; ++i) {
people.push({name: `charles number ${Math.random() * 10}`, email: `email number ${Math.random() * 10}`});
people.push({ name: `charles number ${Math.random() * 10}`, email: `email number ${Math.random() * 10}` });
}
this.dbService.bulkPut('people', people).subscribe((result) => {
console.log('result: ', result);
Expand Down Expand Up @@ -135,8 +136,8 @@ export class AppComponent {
storeConfig: { keyPath: 'id', autoIncrement: true },
storeSchema: [
{ name: 'name', keypath: 'name', options: { unique: false } },
{ name: 'email', keypath: 'email', options: { unique: false } },
],
{ name: 'email', keypath: 'email', options: { unique: false } }
]
};

this.dbService.createObjectStore(storeSchema);
Expand All @@ -159,17 +160,34 @@ export class AppComponent {
forkJoin([
this.dbService.add('people', {
name: `desmond`,
email: `email number ${Math.random() * 10}`,
email: `email number ${Math.random() * 10}`
}),
this.dbService.add('people', {
name: `desmond`,
email: `email number ${Math.random() * 10}`,
}),
email: `email number ${Math.random() * 10}`
})
])
.pipe(switchMap(() => this.dbService.getAllByIndex('people', 'name', IDBKeyRange.only('desmond'))))
.subscribe((result) => console.log(result));
}

testUpdateCursor() {
this.dbService.openCursor('people', undefined, 'prev').subscribe((evt) => {
const cursor = (evt.target as IDBOpenDBRequest).result as unknown as IDBCursorWithValue;

if (cursor) {
const item = cursor.value;

item.name = `${item.name} ${Math.random() * 10}`;

cursor.update(item);
cursor.continue();
} else {
console.log('Not found');
}
});
}

public async versionDatabase(): Promise<void> {
this.dbService.getDatabaseVersion().pipe(
tap(response => console.log('Versione database => ', response)),
Expand Down

0 comments on commit 73dd358

Please sign in to comment.