Skip to content

Commit

Permalink
normalize db link
Browse files Browse the repository at this point in the history
  • Loading branch information
rafapaezbas committed Jan 19, 2025
1 parent 7abe148 commit 605ddea
Showing 1 changed file with 14 additions and 9 deletions.
23 changes: 14 additions & 9 deletions subsystems/sidecar/lib/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const HyperDB = require('hyperdb')
const DBLock = require('db-lock')
const dbSpec = require('../../../spec/db')
const { PLATFORM_HYPERDB } = require('../../../constants')
const path = require('bare-path')

module.exports = class Model {
constructor () {
Expand All @@ -20,7 +21,7 @@ module.exports = class Model {
}

async getBundle (link) {
const bundle = await this.db.get('@pear/bundle', { link })
const bundle = await this.db.get('@pear/bundle', { link: normalizeLink(link) })
return bundle
}

Expand All @@ -30,15 +31,15 @@ module.exports = class Model {

async addBundle (link, appStorage) {
const tx = await this.lock.enter()
await tx.insert('@pear/bundle', { link, appStorage })
await tx.insert('@pear/bundle', { link: normalizeLink(link), appStorage })
await this.lock.exit()
return { link, appStorage }
}

async updateEncryptionKey (link, encryptionKey) {
let result
const tx = await this.lock.enter()
const bundle = await tx.get('@pear/bundle', { link })
const bundle = await tx.get('@pear/bundle', { link: normalizeLink(link) })
if (!bundle) {
result = null
} else {
Expand All @@ -53,7 +54,7 @@ module.exports = class Model {
async updateAppStorage (link, newAppStorage, oldStorage) {
let result
const tx = await this.lock.enter()
const bundle = await tx.get('@pear/bundle', { link })
const bundle = await tx.get('@pear/bundle', { link: normalizeLink(link) })
if (!bundle) {
result = null
} else {
Expand All @@ -77,13 +78,13 @@ module.exports = class Model {
}

async getTags (link) {
return (await this.db.get('@pear/bundle', { link }))?.tags || []
return (await this.db.get('@pear/bundle', { link: normalizeLink(link) }))?.tags || []
}

async updateTags (link, tags) {
let result
const tx = await this.lock.enter()
const bundle = await tx.get('@pear/bundle', { link })
const bundle = await tx.get('@pear/bundle', { link: normalizeLink(link) })
if (!bundle) {
result = null
} else {
Expand All @@ -96,13 +97,13 @@ module.exports = class Model {
}

async getAppStorage (link) {
return (await this.db.get('@pear/bundle', { link }))?.appStorage
return (await this.db.get('@pear/bundle', { link: normalizeLink(link) }))?.appStorage
}

async shiftAppStorage (srcLink, dstLink, newSrcAppStorage = null) {
const tx = await this.lock.enter()
const srcBundle = await tx.get('@pear/bundle', { link: srcLink })
const dstBundle = await tx.get('@pear/bundle', { link: dstLink })
const srcBundle = await tx.get('@pear/bundle', { link: normalizeLink(srcLink) })
const dstBundle = await tx.get('@pear/bundle', { link: normalizeLink(dstLink) })

if (!srcBundle || !dstBundle) {
await this.lock.exit()
Expand All @@ -125,3 +126,7 @@ module.exports = class Model {
await this.db.close()
}
}

const normalizeLink = (link) => {
return link.endsWith(path.sep) ? link.slice(0, -1) : link
}

0 comments on commit 605ddea

Please sign in to comment.