Merge pull request #35 from orbitdb/fix/manifest-meta-data

Fix passing in database manifest meta data
This commit is contained in:
Haad
2023-03-09 10:15:10 +02:00
committed by GitHub
8 changed files with 50 additions and 17 deletions

View File

@@ -6,7 +6,7 @@ import KeyStore from './key-store.js'
import { Identities } from './identities/index.js'
import IPFSAccessController from './access-controllers/ipfs.js'
import OrbitDBAddress, { isValidAddress } from './address.js'
import createDBManifest from './manifest.js'
import DBManifest from './manifest.js'
import { createId, isDefined } from './utils/index.js'
// import Logger from 'logplease'
import path from 'path'
@@ -50,7 +50,7 @@ const OrbitDB = async ({ ipfs, id, identity, keystore, directory } = {}) => {
let databases = {}
const open = async (address, { type, Store } = {}) => {
const open = async (address, { type, meta, Store } = {}) => {
let name, manifest, accessController
if (databases[address]) {
@@ -67,15 +67,17 @@ const OrbitDB = async ({ ipfs, id, identity, keystore, directory } = {}) => {
accessController = await IPFSAccessController({ ipfs, identities, identity, address: acAddress, storage: manifestStorage })
name = manifest.name
type = type || manifest.type
meta = manifest.meta
} else {
// If the address given was not valid, eg. just the name of the database
type = type || 'events'
accessController = await IPFSAccessController({ ipfs, identities, identity, storage: manifestStorage })
const m = await createDBManifest(manifestStorage, address, type, accessController.address, {})
const m = await DBManifest(manifestStorage, address, type, accessController.address, { meta })
manifest = m.manifest
address = OrbitDBAddress(m.hash)
accessController = m.accessController
name = manifest.name
meta = manifest.meta
}
const DatabaseModel = Store || databaseTypes[type]
@@ -84,7 +86,7 @@ const OrbitDB = async ({ ipfs, id, identity, keystore, directory } = {}) => {
throw new Error(`Unspported database type: '${type}'`)
}
const db = await DatabaseModel({ OpLog, Database, ipfs, identity, address: address.toString(), name, accessController, directory })
const db = await DatabaseModel({ OpLog, Database, ipfs, identity, address: address.toString(), name, accessController, directory, meta })
db.events.on('close', onDatabaseClosed(address.toString()))

View File

@@ -7,10 +7,11 @@ import { ComposedStorage, LRUStorage, IPFSBlockStorage, LevelStorage } from './s
const defaultPointerCount = 0
const defaultCacheSize = 1000
const Database = async ({ OpLog, ipfs, identity, address, name, accessController, directory, storage, headsStorage, pointerCount }) => {
const Database = async ({ OpLog, ipfs, identity, address, name, accessController, directory, storage, meta, headsStorage, pointerCount }) => {
const { Log, Entry } = OpLog
directory = Path.join(directory || './orbitdb', `./${address}/`)
meta = meta || {}
pointerCount = pointerCount || defaultPointerCount
const entryStorage = await ComposedStorage(
@@ -77,6 +78,7 @@ const Database = async ({ OpLog, ipfs, identity, address, name, accessController
address,
name,
identity,
meta,
close,
drop,
addOperation,

View File

@@ -1,5 +1,5 @@
const DocumentStore = async ({ OpLog, Database, ipfs, identity, address, name, accessController, directory, storage, indexBy = '_id' }) => {
const database = await Database({ OpLog, ipfs, identity, address, name, accessController, directory, storage })
const DocumentStore = async ({ OpLog, Database, ipfs, identity, address, name, accessController, directory, storage, meta, indexBy = '_id' }) => {
const database = await Database({ OpLog, ipfs, identity, address, name, accessController, directory, storage, meta })
const { addOperation, log } = database

View File

@@ -1,5 +1,5 @@
const Events = async ({ OpLog, Database, ipfs, identity, address, name, accessController, directory, storage }) => {
const database = await Database({ OpLog, ipfs, identity, address, name, accessController, directory, storage })
const Events = async ({ OpLog, Database, ipfs, identity, address, name, accessController, directory, storage, meta }) => {
const database = await Database({ OpLog, ipfs, identity, address, name, accessController, directory, storage, meta })
const { addOperation, log } = database

View File

@@ -5,8 +5,8 @@ import path from 'path'
const valueEncoding = 'json'
const KeyValuePersisted = async ({ OpLog, Database, ipfs, identity, address, name, accessController, directory, storage }) => {
const keyValueStore = await KeyValue({ OpLog, Database, ipfs, identity, address, name, accessController, directory, storage })
const KeyValuePersisted = async ({ OpLog, Database, ipfs, identity, address, name, accessController, directory, storage, meta }) => {
const keyValueStore = await KeyValue({ OpLog, Database, ipfs, identity, address, name, accessController, directory, storage, meta })
const { events, log } = keyValueStore
const queue = new PQueue({ concurrency: 1 })

View File

@@ -1,5 +1,5 @@
const KeyValue = async ({ OpLog, Database, ipfs, identity, address, name, accessController, directory, storage }) => {
const database = await Database({ OpLog, ipfs, identity, address, name, accessController, directory, storage })
const KeyValue = async ({ OpLog, Database, ipfs, identity, address, name, accessController, directory, storage, meta }) => {
const database = await Database({ OpLog, ipfs, identity, address, name, accessController, directory, storage, meta })
const { addOperation, log } = database

View File

@@ -1,5 +1,4 @@
import path from 'path'
// import * as io from 'orbit-db-io'
import * as Block from 'multiformats/block'
import * as dagCbor from '@ipld/dag-cbor'
@@ -11,15 +10,15 @@ const hasher = sha256
const hashStringEncoding = base58btc
// Creates a DB manifest file and saves it in IPFS
export default async (storage, name, type, accessControllerAddress, options) => {
export default async (storage, name, type, accessControllerAddress, { meta } = {}) => {
const manifest = Object.assign(
{
name,
type,
accessController: (path.posix || path).join('/ipfs', accessControllerAddress)
},
// meta field is only added to manifest if options.meta is defined
options.meta !== undefined ? { meta: options.meta } : {}
// meta field is only added to manifest if meta parameter is defined
meta !== undefined ? { meta } : {}
)
const { cid, bytes } = await Block.encode({ value: manifest, codec, hasher })

View File

@@ -133,6 +133,11 @@ describe('Open databases', function () {
strictEqual(typeof db.all, 'function')
})
it('has a meta object', async () => {
notStrictEqual(db.meta, undefined)
strictEqual(typeof db.meta, 'object')
})
it('creates a directory for the database oplog', async () => {
const expectedPath = path.join(orbitdb1.directory, `./${db.address}`, '/log/_heads')
const directoryExists = fs.existsSync(expectedPath)
@@ -140,6 +145,31 @@ describe('Open databases', function () {
})
})
describe('creating a database with meta info in the manifest', () => {
let db
const expected = { hello: 'world' }
before(async () => {
orbitdb1 = await OrbitDB({ ipfs: ipfs1, id: 'user1', directory: './orbitdb1' })
db = await orbitdb1.open('helloworld', { meta: expected })
})
after(async () => {
if (db) {
await db.drop()
await db.close()
}
if (orbitdb1) {
await orbitdb1.stop()
}
await rmrf('./orbitdb1')
})
it('contains the given meta info', async () => {
deepStrictEqual(db.meta, expected)
})
})
describe('opening a database', () => {
let db