mirror of
https://github.com/advplyr/audiobookshelf.git
synced 2025-04-02 01:16:54 +02:00
Add bulkInsertEntities to db to handle migrating large collections
This commit is contained in:
parent
399e0ea0bc
commit
7b3f9a1e0c
33
server/Db.js
33
server/Db.js
@ -316,6 +316,39 @@ class Db {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async bulkInsertEntities(entityName, entities, batchSize = 500) {
|
||||||
|
// Group entities in batches of size batchSize
|
||||||
|
var entityBatches = []
|
||||||
|
var batch = []
|
||||||
|
var index = 0
|
||||||
|
entities.forEach((ent) => {
|
||||||
|
batch.push(ent)
|
||||||
|
index++
|
||||||
|
if (index >= batchSize) {
|
||||||
|
entityBatches.push(batch)
|
||||||
|
index = 0
|
||||||
|
batch = []
|
||||||
|
}
|
||||||
|
})
|
||||||
|
if (batch.length) entityBatches.push(batch)
|
||||||
|
|
||||||
|
Logger.info(`[Db] bulkInsertEntities: ${entities.length} ${entityName} to ${entityBatches.length} batches of max size ${batchSize}`)
|
||||||
|
|
||||||
|
// Start inserting batches
|
||||||
|
var batchIndex = 1
|
||||||
|
for (const entityBatch of entityBatches) {
|
||||||
|
Logger.info(`[Db] bulkInsertEntities: Start inserting batch ${batchIndex} of ${entityBatch.length} for ${entityName}`)
|
||||||
|
var success = await this.insertEntities(entityName, entityBatch)
|
||||||
|
if (success) {
|
||||||
|
Logger.info(`[Db] bulkInsertEntities: Success inserting batch ${batchIndex} for ${entityName}`)
|
||||||
|
} else {
|
||||||
|
Logger.info(`[Db] bulkInsertEntities: Failed inserting batch ${batchIndex} for ${entityName}`)
|
||||||
|
}
|
||||||
|
batchIndex++
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
updateEntities(entityName, entities) {
|
updateEntities(entityName, entities) {
|
||||||
var entityDb = this.getEntityDb(entityName)
|
var entityDb = this.getEntityDb(entityName)
|
||||||
|
|
||||||
|
@ -242,10 +242,10 @@ async function migrateLibraryItems(db) {
|
|||||||
var libraryItems = audiobooks.map((ab) => makeLibraryItemFromOldAb(ab))
|
var libraryItems = audiobooks.map((ab) => makeLibraryItemFromOldAb(ab))
|
||||||
|
|
||||||
Logger.info(`>>> ${libraryItems.length} Library Items made`)
|
Logger.info(`>>> ${libraryItems.length} Library Items made`)
|
||||||
await db.insertEntities('libraryItem', libraryItems)
|
await db.bulkInsertEntities('libraryItem', libraryItems)
|
||||||
if (authorsToAdd.length) {
|
if (authorsToAdd.length) {
|
||||||
Logger.info(`>>> ${authorsToAdd.length} Authors made`)
|
Logger.info(`>>> ${authorsToAdd.length} Authors made`)
|
||||||
await db.insertEntities('author', authorsToAdd)
|
await db.bulkInsertEntities('author', authorsToAdd)
|
||||||
}
|
}
|
||||||
if (seriesToAdd.length) {
|
if (seriesToAdd.length) {
|
||||||
Logger.info(`>>> ${seriesToAdd.length} Series made`)
|
Logger.info(`>>> ${seriesToAdd.length} Series made`)
|
||||||
|
Loading…
Reference in New Issue
Block a user