mirror of
https://github.com/orbitdb/orbitdb.git
synced 2025-03-30 15:08:28 +00:00
refactor: Move database type management to db module.
This commit is contained in:
parent
85e6848f4c
commit
8b93553d0e
@ -1,4 +1,53 @@
|
|||||||
export { default as Documents } from './documents.js'
|
import Documents from './documents.js'
|
||||||
export { default as Events } from './events.js'
|
import Events from './events.js'
|
||||||
export { default as KeyValue } from './keyvalue.js'
|
import KeyValue from './keyvalue.js'
|
||||||
export { default as KeyValueIndexed } from './keyvalue-indexed.js'
|
import KeyValueIndexed from './keyvalue-indexed.js'
|
||||||
|
|
||||||
|
/**
|
||||||
|
* An array of available database types.
|
||||||
|
* @name databaseTypes
|
||||||
|
* @†ype []
|
||||||
|
* @return [] An array of database types.
|
||||||
|
* @memberof module:OrbitDB
|
||||||
|
*/
|
||||||
|
const databaseTypes = {
|
||||||
|
events: Events,
|
||||||
|
documents: Documents,
|
||||||
|
keyvalue: KeyValue,
|
||||||
|
keyvalueindexed: KeyValueIndexed
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Add a new database type.
|
||||||
|
* @example
|
||||||
|
* import { addDatabaseType } from 'orbit-db'
|
||||||
|
* const CustomDBTypeModule = async (params) => {
|
||||||
|
* const database = await Database(...params)
|
||||||
|
* ...
|
||||||
|
* }
|
||||||
|
* addDatabaseType('customDBType', CustomDBTypeModule)
|
||||||
|
* @function addDatabaseType
|
||||||
|
* @param {string} type The database type.
|
||||||
|
* @param {module:Database} store A Database-compatible module.
|
||||||
|
* @memberof module:OrbitDB
|
||||||
|
*/
|
||||||
|
const addDatabaseType = (type, store) => {
|
||||||
|
if (databaseTypes[type]) {
|
||||||
|
throw new Error(`Type already exists: ${type}`)
|
||||||
|
}
|
||||||
|
databaseTypes[type] = store
|
||||||
|
}
|
||||||
|
|
||||||
|
const getDatabaseType = (type) => {
|
||||||
|
if (!type) {
|
||||||
|
throw new Error('Type not specified')
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!databaseTypes[type]) {
|
||||||
|
throw new Error(`Unsupported database type: '${type}'`)
|
||||||
|
}
|
||||||
|
|
||||||
|
return databaseTypes[type]
|
||||||
|
}
|
||||||
|
|
||||||
|
export { addDatabaseType, getDatabaseType }
|
||||||
|
@ -13,7 +13,7 @@
|
|||||||
* const Partial = KeyValueIndexed({ storage })
|
* const Partial = KeyValueIndexed({ storage })
|
||||||
* const keyValueIndexed = await Partial({ ipfs })
|
* const keyValueIndexed = await Partial({ ipfs })
|
||||||
*/
|
*/
|
||||||
import { KeyValue } from './index.js'
|
import KeyValue from './keyvalue.js'
|
||||||
import LevelStorage from '../storage/level.js'
|
import LevelStorage from '../storage/level.js'
|
||||||
import pathJoin from '../utils/path-join.js'
|
import pathJoin from '../utils/path-join.js'
|
||||||
|
|
||||||
|
@ -1,9 +1,12 @@
|
|||||||
export {
|
export {
|
||||||
default as OrbitDB,
|
default as OrbitDB
|
||||||
databaseTypes,
|
|
||||||
addDatabaseType
|
|
||||||
} from './orbitdb.js'
|
} from './orbitdb.js'
|
||||||
|
|
||||||
|
export {
|
||||||
|
addDatabaseType,
|
||||||
|
getDatabaseType
|
||||||
|
} from './db/index.js'
|
||||||
|
|
||||||
export {
|
export {
|
||||||
default as OrbitDBAddress,
|
default as OrbitDBAddress,
|
||||||
isValidAddress,
|
isValidAddress,
|
||||||
|
@ -48,7 +48,7 @@
|
|||||||
* const orbitdb = await OrbitDB({ ipfs, identities, id: 'userA' })
|
* const orbitdb = await OrbitDB({ ipfs, identities, id: 'userA' })
|
||||||
* const mydb = await orbitdb.open('mydb')
|
* const mydb = await orbitdb.open('mydb')
|
||||||
*/
|
*/
|
||||||
import { Events, KeyValue, Documents } from './db/index.js'
|
import { getDatabaseType } from './db/index.js'
|
||||||
import KeyStore from './key-store.js'
|
import KeyStore from './key-store.js'
|
||||||
import { Identities } from './identities/index.js'
|
import { Identities } from './identities/index.js'
|
||||||
import OrbitDBAddress, { isValidAddress } from './address.js'
|
import OrbitDBAddress, { isValidAddress } from './address.js'
|
||||||
@ -58,40 +58,6 @@ import pathJoin from './utils/path-join.js'
|
|||||||
import { getAccessController } from './access-controllers/index.js'
|
import { getAccessController } from './access-controllers/index.js'
|
||||||
import IPFSAccessController from './access-controllers/ipfs.js'
|
import IPFSAccessController from './access-controllers/ipfs.js'
|
||||||
|
|
||||||
/**
|
|
||||||
* An array of available database types.
|
|
||||||
* @name databaseTypes
|
|
||||||
* @†ype []
|
|
||||||
* @return [] An array of database types.
|
|
||||||
* @memberof module:OrbitDB
|
|
||||||
*/
|
|
||||||
const databaseTypes = {
|
|
||||||
events: Events,
|
|
||||||
documents: Documents,
|
|
||||||
keyvalue: KeyValue
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Add a new database type.
|
|
||||||
* @example
|
|
||||||
* import { addDatabaseType } from 'orbit-db'
|
|
||||||
* const CustomDBTypeModule = async (params) => {
|
|
||||||
* const database = await Database(...params)
|
|
||||||
* ...
|
|
||||||
* }
|
|
||||||
* addDatabaseType('customDBType', CustomDBTypeModule)
|
|
||||||
* @function addDatabaseType
|
|
||||||
* @param {string} type The database type.
|
|
||||||
* @param {module:Database} store A Database-compatible module.
|
|
||||||
* @memberof module:OrbitDB
|
|
||||||
*/
|
|
||||||
const addDatabaseType = (type, store) => {
|
|
||||||
if (databaseTypes[type]) {
|
|
||||||
throw new Error(`Type already exists: ${type}`)
|
|
||||||
}
|
|
||||||
databaseTypes[type] = store
|
|
||||||
}
|
|
||||||
|
|
||||||
const DefaultDatabaseType = 'events'
|
const DefaultDatabaseType = 'events'
|
||||||
|
|
||||||
const DefaultAccessController = IPFSAccessController
|
const DefaultAccessController = IPFSAccessController
|
||||||
@ -181,10 +147,6 @@ const OrbitDB = async ({ ipfs, id, identities, directory } = {}) => {
|
|||||||
const open = async (address, { type, meta, sync, Database, AccessController, headsStorage, entryStorage, indexStorage, referencesCount } = {}) => {
|
const open = async (address, { type, meta, sync, Database, AccessController, headsStorage, entryStorage, indexStorage, referencesCount } = {}) => {
|
||||||
let name, manifest, accessController
|
let name, manifest, accessController
|
||||||
|
|
||||||
if (type && !databaseTypes[type]) {
|
|
||||||
throw new Error(`Unsupported database type: '${type}'`)
|
|
||||||
}
|
|
||||||
|
|
||||||
if (databases[address]) {
|
if (databases[address]) {
|
||||||
return databases[address]
|
return databases[address]
|
||||||
}
|
}
|
||||||
@ -212,7 +174,7 @@ const OrbitDB = async ({ ipfs, id, identities, directory } = {}) => {
|
|||||||
meta = manifest.meta
|
meta = manifest.meta
|
||||||
}
|
}
|
||||||
|
|
||||||
Database = Database || databaseTypes[type]()
|
Database = Database || getDatabaseType(type)()
|
||||||
|
|
||||||
if (!Database) {
|
if (!Database) {
|
||||||
throw new Error(`Unsupported database type: '${type}'`)
|
throw new Error(`Unsupported database type: '${type}'`)
|
||||||
@ -261,4 +223,4 @@ const OrbitDB = async ({ ipfs, id, identities, directory } = {}) => {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export { OrbitDB as default, OrbitDBAddress, addDatabaseType, databaseTypes }
|
export { OrbitDB as default, OrbitDBAddress }
|
||||||
|
@ -3,7 +3,7 @@ import rmrf from 'rimraf'
|
|||||||
import { copy } from 'fs-extra'
|
import { copy } from 'fs-extra'
|
||||||
import * as IPFS from 'ipfs-core'
|
import * as IPFS from 'ipfs-core'
|
||||||
import { KeyStore, Identities } from '../../src/index.js'
|
import { KeyStore, Identities } from '../../src/index.js'
|
||||||
import { Documents } from '../../src/db/index.js'
|
import Documents from '../../src/db/documents.js'
|
||||||
import config from '../config.js'
|
import config from '../config.js'
|
||||||
import testKeysPath from '../fixtures/test-keys-path.js'
|
import testKeysPath from '../fixtures/test-keys-path.js'
|
||||||
|
|
||||||
|
@ -3,7 +3,7 @@ import rmrf from 'rimraf'
|
|||||||
import { copy } from 'fs-extra'
|
import { copy } from 'fs-extra'
|
||||||
import * as IPFS from 'ipfs-core'
|
import * as IPFS from 'ipfs-core'
|
||||||
import { KeyStore, Identities } from '../../src/index.js'
|
import { KeyStore, Identities } from '../../src/index.js'
|
||||||
import { Events } from '../../src/db/index.js'
|
import Events from '../../src/db/events.js'
|
||||||
import config from '../config.js'
|
import config from '../config.js'
|
||||||
import testKeysPath from '../fixtures/test-keys-path.js'
|
import testKeysPath from '../fixtures/test-keys-path.js'
|
||||||
|
|
||||||
|
@ -5,7 +5,7 @@ import rmrf from 'rimraf'
|
|||||||
import { copy } from 'fs-extra'
|
import { copy } from 'fs-extra'
|
||||||
import * as IPFS from 'ipfs-core'
|
import * as IPFS from 'ipfs-core'
|
||||||
import { KeyStore, Identities, MemoryStorage } from '../../src/index.js'
|
import { KeyStore, Identities, MemoryStorage } from '../../src/index.js'
|
||||||
import { KeyValueIndexed } from '../../src/db/index.js'
|
import KeyValueIndexed from '../../src/db/keyvalue-indexed.js'
|
||||||
import config from '../config.js'
|
import config from '../config.js'
|
||||||
import testKeysPath from '../fixtures/test-keys-path.js'
|
import testKeysPath from '../fixtures/test-keys-path.js'
|
||||||
|
|
||||||
|
@ -3,7 +3,7 @@ import rmrf from 'rimraf'
|
|||||||
import { copy } from 'fs-extra'
|
import { copy } from 'fs-extra'
|
||||||
import * as IPFS from 'ipfs-core'
|
import * as IPFS from 'ipfs-core'
|
||||||
import { KeyStore, Identities } from '../../src/index.js'
|
import { KeyStore, Identities } from '../../src/index.js'
|
||||||
import { KeyValue } from '../../src/db/index.js'
|
import KeyValue from '../../src/db/keyvalue.js'
|
||||||
import config from '../config.js'
|
import config from '../config.js'
|
||||||
import testKeysPath from '../fixtures/test-keys-path.js'
|
import testKeysPath from '../fixtures/test-keys-path.js'
|
||||||
|
|
||||||
|
@ -3,7 +3,7 @@ import rmrf from 'rimraf'
|
|||||||
import { copy } from 'fs-extra'
|
import { copy } from 'fs-extra'
|
||||||
import * as IPFS from 'ipfs-core'
|
import * as IPFS from 'ipfs-core'
|
||||||
import { KeyStore, Identities } from '../../../src/index.js'
|
import { KeyStore, Identities } from '../../../src/index.js'
|
||||||
import { Documents } from '../../../src/db/index.js'
|
import Documents from '../../../src/db/documents.js'
|
||||||
import config from '../../config.js'
|
import config from '../../config.js'
|
||||||
import testKeysPath from '../../fixtures/test-keys-path.js'
|
import testKeysPath from '../../fixtures/test-keys-path.js'
|
||||||
import connectPeers from '../../utils/connect-nodes.js'
|
import connectPeers from '../../utils/connect-nodes.js'
|
||||||
|
@ -3,7 +3,7 @@ import rmrf from 'rimraf'
|
|||||||
import { copy } from 'fs-extra'
|
import { copy } from 'fs-extra'
|
||||||
import * as IPFS from 'ipfs-core'
|
import * as IPFS from 'ipfs-core'
|
||||||
import { KeyStore, Identities } from '../../../src/index.js'
|
import { KeyStore, Identities } from '../../../src/index.js'
|
||||||
import { Events } from '../../../src/db/index.js'
|
import Events from '../../../src/db/events.js'
|
||||||
import config from '../../config.js'
|
import config from '../../config.js'
|
||||||
import testKeysPath from '../../fixtures/test-keys-path.js'
|
import testKeysPath from '../../fixtures/test-keys-path.js'
|
||||||
import connectPeers from '../../utils/connect-nodes.js'
|
import connectPeers from '../../utils/connect-nodes.js'
|
||||||
|
@ -3,7 +3,7 @@ import rmrf from 'rimraf'
|
|||||||
import { copy } from 'fs-extra'
|
import { copy } from 'fs-extra'
|
||||||
import * as IPFS from 'ipfs-core'
|
import * as IPFS from 'ipfs-core'
|
||||||
import { KeyStore, Identities } from '../../../src/index.js'
|
import { KeyStore, Identities } from '../../../src/index.js'
|
||||||
import { KeyValueIndexed } from '../../../src/db/index.js'
|
import KeyValueIndexed from '../../../src/db/keyvalue-indexed.js'
|
||||||
import config from '../../config.js'
|
import config from '../../config.js'
|
||||||
import testKeysPath from '../../fixtures/test-keys-path.js'
|
import testKeysPath from '../../fixtures/test-keys-path.js'
|
||||||
import connectPeers from '../../utils/connect-nodes.js'
|
import connectPeers from '../../utils/connect-nodes.js'
|
||||||
|
@ -3,7 +3,7 @@ import rmrf from 'rimraf'
|
|||||||
import { copy } from 'fs-extra'
|
import { copy } from 'fs-extra'
|
||||||
import * as IPFS from 'ipfs-core'
|
import * as IPFS from 'ipfs-core'
|
||||||
import { KeyStore, Identities } from '../../../src/index.js'
|
import { KeyStore, Identities } from '../../../src/index.js'
|
||||||
import { KeyValue } from '../../../src/db/index.js'
|
import KeyValue from '../../../src/db/keyvalue.js'
|
||||||
import config from '../../config.js'
|
import config from '../../config.js'
|
||||||
import testKeysPath from '../../fixtures/test-keys-path.js'
|
import testKeysPath from '../../fixtures/test-keys-path.js'
|
||||||
import connectPeers from '../../utils/connect-nodes.js'
|
import connectPeers from '../../utils/connect-nodes.js'
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
import { strictEqual, deepStrictEqual, notStrictEqual } from 'assert'
|
import { strictEqual, deepStrictEqual, notStrictEqual } from 'assert'
|
||||||
import rmrf from 'rimraf'
|
import rmrf from 'rimraf'
|
||||||
import * as IPFS from 'ipfs-core'
|
import * as IPFS from 'ipfs-core'
|
||||||
import { OrbitDB, addDatabaseType, databaseTypes, Database } from '../src/index.js'
|
import { OrbitDB, addDatabaseType, getDatabaseType, Database } from '../src/index.js'
|
||||||
import config from './config.js'
|
import config from './config.js'
|
||||||
|
|
||||||
const type = 'custom!'
|
const type = 'custom!'
|
||||||
@ -35,24 +35,11 @@ describe('Add a custom database type', function () {
|
|||||||
await ipfs.stop()
|
await ipfs.stop()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Remove the added custom database type from OrbitDB import
|
|
||||||
delete databaseTypes[CustomStore.type]
|
|
||||||
|
|
||||||
await rmrf('./orbitdb')
|
await rmrf('./orbitdb')
|
||||||
await rmrf('./ipfs1')
|
await rmrf('./ipfs1')
|
||||||
})
|
})
|
||||||
|
|
||||||
describe('Default supported database types', function () {
|
describe('Default supported database types', function () {
|
||||||
it('returns default supported database types', async () => {
|
|
||||||
const expected = [
|
|
||||||
'events',
|
|
||||||
'documents',
|
|
||||||
'keyvalue'
|
|
||||||
]
|
|
||||||
|
|
||||||
deepStrictEqual(Object.keys(databaseTypes), expected)
|
|
||||||
})
|
|
||||||
|
|
||||||
it('throws and error if custom database type hasn\'t been added', async () => {
|
it('throws and error if custom database type hasn\'t been added', async () => {
|
||||||
let err
|
let err
|
||||||
try {
|
try {
|
||||||
@ -90,26 +77,7 @@ describe('Add a custom database type', function () {
|
|||||||
})
|
})
|
||||||
|
|
||||||
it('returns custom database type after adding it', async () => {
|
it('returns custom database type after adding it', async () => {
|
||||||
const expected = [
|
deepStrictEqual(getDatabaseType(type), CustomStore)
|
||||||
'events',
|
|
||||||
'documents',
|
|
||||||
'keyvalue',
|
|
||||||
type
|
|
||||||
]
|
|
||||||
|
|
||||||
deepStrictEqual(Object.keys(databaseTypes), expected)
|
|
||||||
})
|
|
||||||
|
|
||||||
it('can be removed from supported database types', async () => {
|
|
||||||
const expected = [
|
|
||||||
'events',
|
|
||||||
'documents',
|
|
||||||
'keyvalue'
|
|
||||||
]
|
|
||||||
|
|
||||||
delete databaseTypes[type]
|
|
||||||
|
|
||||||
deepStrictEqual(Object.keys(databaseTypes), expected)
|
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
@ -4,7 +4,7 @@ import fs from 'fs'
|
|||||||
import path from 'path'
|
import path from 'path'
|
||||||
import * as IPFS from 'ipfs-core'
|
import * as IPFS from 'ipfs-core'
|
||||||
import { OrbitDB, isValidAddress, LevelStorage } from '../src/index.js'
|
import { OrbitDB, isValidAddress, LevelStorage } from '../src/index.js'
|
||||||
import { KeyValueIndexed } from '../src/db/index.js'
|
import KeyValueIndexed from '../src/db/keyvalue-indexed.js'
|
||||||
import config from './config.js'
|
import config from './config.js'
|
||||||
import connectPeers from './utils/connect-nodes.js'
|
import connectPeers from './utils/connect-nodes.js'
|
||||||
import waitFor from './utils/wait-for.js'
|
import waitFor from './utils/wait-for.js'
|
||||||
|
Loading…
x
Reference in New Issue
Block a user