Created
August 7, 2020 06:50
-
-
Save poltak/fad684fe79e1271542db219c8aa46bf3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function nameFromKeyPath (keyPath) { | |
return typeof keyPath === 'string' ? | |
keyPath : | |
keyPath ? ('[' + [].join.call(keyPath, '+') + ']') : ""; | |
} | |
function createTableSchema ( | |
name, | |
primKey, | |
indexes | |
) { | |
return { | |
name, | |
primKey, | |
indexes, | |
mappedClass: null, | |
idxByName: indexes.reduce((result, item, i) => { | |
var nameAndValue = [item.name, item] | |
if (nameAndValue) result[nameAndValue[0]] = nameAndValue[1]; | |
return result; | |
}, {}) | |
}; | |
} | |
function createIndexSpec( | |
name, | |
keyPath, | |
unique, | |
multi, | |
auto, | |
compound, | |
isPrimKey | |
) { | |
return { | |
name, | |
keyPath, | |
unique, | |
multi, | |
auto, | |
compound, | |
src: (unique && !isPrimKey ? '&' : '') + (multi ? '*' : '') + (auto ? "++" : "") + nameFromKeyPath(keyPath) | |
} | |
} | |
function buildGlobalSchema( | |
db, | |
idbdb, | |
tmpTrans | |
) { | |
const globalSchema = {}; | |
const dbStoreNames = Array.from(idbdb.objectStoreNames) | |
dbStoreNames.forEach(storeName => { | |
const store = tmpTrans.objectStore(storeName); | |
let keyPath = store.keyPath; | |
const primKey = createIndexSpec( | |
nameFromKeyPath(keyPath), | |
keyPath || "", | |
false, | |
false, | |
!!store.autoIncrement, | |
keyPath && typeof keyPath !== "string", | |
true | |
); | |
const indexes = []; | |
for (let j = 0; j < store.indexNames.length; ++j) { | |
const idbindex = store.index(store.indexNames[j]); | |
keyPath = idbindex.keyPath; | |
var index = createIndexSpec( | |
idbindex.name, | |
keyPath, | |
!!idbindex.unique, | |
!!idbindex.multiEntry, | |
false, | |
keyPath && typeof keyPath !== "string", | |
false | |
); | |
indexes.push(index); | |
} | |
globalSchema[storeName] = createTableSchema(storeName, primKey, indexes); | |
}); | |
return globalSchema; | |
} | |
const getDb = () => new Promise(resolve => { | |
indexedDB.open('memex').onsuccess = function (event) { | |
resolve(event.target.result) | |
} | |
}) | |
async function main() { | |
const db = await getDb() | |
return buildGlobalSchema(storageMan.backend.dexie, db, db.transaction(db.objectStoreNames)) | |
} | |
main().then(console.log) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment