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

Indexeddb faster alldocs updated 2023 09 13 #5

Open
wants to merge 3 commits into
base: indexeddb-faster-alldocs
Choose a base branch
from
Open
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
42 changes: 37 additions & 5 deletions packages/node_modules/pouchdb-adapter-indexeddb/src/setup.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
'use strict';

import { uuid } from 'pouchdb-utils';
import { isLocalId } from 'pouchdb-adapter-utils';

import { META_STORE, DOC_STORE, rawIndexFields, naturalIndexName, correctIndexFields } from './util';

//
// Core PouchDB schema version. Increment this if we, as a library, want to make
// schema changes in indexeddb. See upgradePouchDbSchema()
//
var POUCHDB_IDB_VERSION = 1;
var POUCHDB_IDB_VERSION = 2;

//
// Functions that manage a combinate indexeddb version, by combining the current
Expand Down Expand Up @@ -91,17 +92,21 @@ function maintainNativeIndexes(openReq, reject) {
};
}

function upgradePouchDbSchema(db, pouchdbVersion) {
function upgradePouchDbSchema(db, tx, pouchdbVersion) {
if (pouchdbVersion < 1) {
var docStore = db.createObjectStore(DOC_STORE, {keyPath : 'id'});
docStore.createIndex('seq', 'seq', {unique: true});
docStore.createIndex('_isDeleted_id', [ 'deleted', 'id' ], {unique: true});

db.createObjectStore(META_STORE, {keyPath: 'id'});
}

if (pouchdbVersion < 2) {
const docStore = tx.objectStore(DOC_STORE);
docStore.createIndex('_isDeleted_id', [ 'deleted', 'id' ], {unique: true});
}

// Declare more PouchDB schema changes here
// if (pouchdbVersion < 2) { .. }
// if (pouchdbVersion < 3) { .. }
}

function openDatabase(openDatabases, api, opts, resolve, reject) {
Expand All @@ -123,11 +128,38 @@ function openDatabase(openDatabases, api, opts, resolve, reject) {
throw new Error('Database was deleted while open');
}

const tx = e.target.transaction;
var db = e.target.result;

var pouchdbVersion = getPouchDbVersion(e.oldVersion);
upgradePouchDbSchema(db, pouchdbVersion);
upgradePouchDbSchema(db, tx, pouchdbVersion);
maintainNativeIndexes(openReq, reject);

if (pouchdbVersion < 2) {
const docStore = openReq.transaction.objectStore(DOC_STORE);
const metaStore = openReq.transaction.objectStore(META_STORE);

const allDocsReq = docStore.openCursor();
allDocsReq.onsuccess = event => {
const cursor = event.target.result;
if (!cursor) {
return;
}

const doc = cursor.value;

if (!isLocalId(doc.id)) {
return cursor.continue();
}

// Move _local/ docs to the META_STORE
metaStore.put(doc).onsuccess = () => {
cursor.delete(doc).onsuccess = () => {
cursor.continue();
};
};
};
}
};

openReq.onblocked = function (e) {
Expand Down
Loading