From 8b93553d0ee0554e7afb63a24f12867e54767ebd Mon Sep 17 00:00:00 2001 From: Hayden Young Date: Sun, 18 Jun 2023 02:43:03 +0100 Subject: [PATCH] refactor: Move database type management to db module. --- src/db/index.js | 57 ++++++++++++++++++-- src/db/keyvalue-indexed.js | 2 +- src/index.js | 9 ++-- src/orbitdb.js | 44 ++------------- test/db/document-store.test.js | 2 +- test/db/event-store.test.js | 2 +- test/db/keyvalue-indexed.js | 2 +- test/db/keyvalue.test.js | 2 +- test/db/replication/document-store.test.js | 2 +- test/db/replication/event-store.test.js | 2 +- test/db/replication/keyvalue-indexed.test.js | 2 +- test/db/replication/keyvalue.test.js | 2 +- test/orbitdb-custom-database-types.test.js | 36 +------------ test/orbitdb-open.test.js | 2 +- 14 files changed, 74 insertions(+), 92 deletions(-) diff --git a/src/db/index.js b/src/db/index.js index 632ad1a..5ddcb9e 100644 --- a/src/db/index.js +++ b/src/db/index.js @@ -1,4 +1,53 @@ -export { default as Documents } from './documents.js' -export { default as Events } from './events.js' -export { default as KeyValue } from './keyvalue.js' -export { default as KeyValueIndexed } from './keyvalue-indexed.js' +import Documents from './documents.js' +import Events from './events.js' +import KeyValue from './keyvalue.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 } diff --git a/src/db/keyvalue-indexed.js b/src/db/keyvalue-indexed.js index ded37df..d80041b 100644 --- a/src/db/keyvalue-indexed.js +++ b/src/db/keyvalue-indexed.js @@ -13,7 +13,7 @@ * const Partial = KeyValueIndexed({ storage }) * const keyValueIndexed = await Partial({ ipfs }) */ -import { KeyValue } from './index.js' +import KeyValue from './keyvalue.js' import LevelStorage from '../storage/level.js' import pathJoin from '../utils/path-join.js' diff --git a/src/index.js b/src/index.js index 8de08df..62de267 100644 --- a/src/index.js +++ b/src/index.js @@ -1,9 +1,12 @@ export { - default as OrbitDB, - databaseTypes, - addDatabaseType + default as OrbitDB } from './orbitdb.js' +export { + addDatabaseType, + getDatabaseType +} from './db/index.js' + export { default as OrbitDBAddress, isValidAddress, diff --git a/src/orbitdb.js b/src/orbitdb.js index 4c12cf1..3f6a5ed 100644 --- a/src/orbitdb.js +++ b/src/orbitdb.js @@ -48,7 +48,7 @@ * const orbitdb = await OrbitDB({ ipfs, identities, id: 'userA' }) * 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 { Identities } from './identities/index.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 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 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 } = {}) => { let name, manifest, accessController - if (type && !databaseTypes[type]) { - throw new Error(`Unsupported database type: '${type}'`) - } - if (databases[address]) { return databases[address] } @@ -212,7 +174,7 @@ const OrbitDB = async ({ ipfs, id, identities, directory } = {}) => { meta = manifest.meta } - Database = Database || databaseTypes[type]() + Database = Database || getDatabaseType(type)() if (!Database) { 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 } diff --git a/test/db/document-store.test.js b/test/db/document-store.test.js index 0614c93..725e7f1 100644 --- a/test/db/document-store.test.js +++ b/test/db/document-store.test.js @@ -3,7 +3,7 @@ import rmrf from 'rimraf' import { copy } from 'fs-extra' import * as IPFS from 'ipfs-core' 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 testKeysPath from '../fixtures/test-keys-path.js' diff --git a/test/db/event-store.test.js b/test/db/event-store.test.js index 08b6608..e6430a5 100644 --- a/test/db/event-store.test.js +++ b/test/db/event-store.test.js @@ -3,7 +3,7 @@ import rmrf from 'rimraf' import { copy } from 'fs-extra' import * as IPFS from 'ipfs-core' 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 testKeysPath from '../fixtures/test-keys-path.js' diff --git a/test/db/keyvalue-indexed.js b/test/db/keyvalue-indexed.js index 25299bc..b87c5f3 100644 --- a/test/db/keyvalue-indexed.js +++ b/test/db/keyvalue-indexed.js @@ -5,7 +5,7 @@ import rmrf from 'rimraf' import { copy } from 'fs-extra' import * as IPFS from 'ipfs-core' 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 testKeysPath from '../fixtures/test-keys-path.js' diff --git a/test/db/keyvalue.test.js b/test/db/keyvalue.test.js index 6839d4f..00672ff 100644 --- a/test/db/keyvalue.test.js +++ b/test/db/keyvalue.test.js @@ -3,7 +3,7 @@ import rmrf from 'rimraf' import { copy } from 'fs-extra' import * as IPFS from 'ipfs-core' 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 testKeysPath from '../fixtures/test-keys-path.js' diff --git a/test/db/replication/document-store.test.js b/test/db/replication/document-store.test.js index 125cf1d..9f7e261 100644 --- a/test/db/replication/document-store.test.js +++ b/test/db/replication/document-store.test.js @@ -3,7 +3,7 @@ import rmrf from 'rimraf' import { copy } from 'fs-extra' import * as IPFS from 'ipfs-core' 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 testKeysPath from '../../fixtures/test-keys-path.js' import connectPeers from '../../utils/connect-nodes.js' diff --git a/test/db/replication/event-store.test.js b/test/db/replication/event-store.test.js index 101b2d6..b212900 100644 --- a/test/db/replication/event-store.test.js +++ b/test/db/replication/event-store.test.js @@ -3,7 +3,7 @@ import rmrf from 'rimraf' import { copy } from 'fs-extra' import * as IPFS from 'ipfs-core' 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 testKeysPath from '../../fixtures/test-keys-path.js' import connectPeers from '../../utils/connect-nodes.js' diff --git a/test/db/replication/keyvalue-indexed.test.js b/test/db/replication/keyvalue-indexed.test.js index 8237f4a..d826c08 100644 --- a/test/db/replication/keyvalue-indexed.test.js +++ b/test/db/replication/keyvalue-indexed.test.js @@ -3,7 +3,7 @@ import rmrf from 'rimraf' import { copy } from 'fs-extra' import * as IPFS from 'ipfs-core' 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 testKeysPath from '../../fixtures/test-keys-path.js' import connectPeers from '../../utils/connect-nodes.js' diff --git a/test/db/replication/keyvalue.test.js b/test/db/replication/keyvalue.test.js index 8dcf7d6..9b205d7 100644 --- a/test/db/replication/keyvalue.test.js +++ b/test/db/replication/keyvalue.test.js @@ -3,7 +3,7 @@ import rmrf from 'rimraf' import { copy } from 'fs-extra' import * as IPFS from 'ipfs-core' 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 testKeysPath from '../../fixtures/test-keys-path.js' import connectPeers from '../../utils/connect-nodes.js' diff --git a/test/orbitdb-custom-database-types.test.js b/test/orbitdb-custom-database-types.test.js index d16c95b..2753a3d 100644 --- a/test/orbitdb-custom-database-types.test.js +++ b/test/orbitdb-custom-database-types.test.js @@ -1,7 +1,7 @@ import { strictEqual, deepStrictEqual, notStrictEqual } from 'assert' import rmrf from 'rimraf' 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' const type = 'custom!' @@ -35,24 +35,11 @@ describe('Add a custom database type', function () { await ipfs.stop() } - // Remove the added custom database type from OrbitDB import - delete databaseTypes[CustomStore.type] - await rmrf('./orbitdb') await rmrf('./ipfs1') }) 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 () => { let err try { @@ -90,26 +77,7 @@ describe('Add a custom database type', function () { }) it('returns custom database type after adding it', async () => { - const expected = [ - '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) + deepStrictEqual(getDatabaseType(type), CustomStore) }) }) }) diff --git a/test/orbitdb-open.test.js b/test/orbitdb-open.test.js index fbc5dd8..703f94a 100644 --- a/test/orbitdb-open.test.js +++ b/test/orbitdb-open.test.js @@ -4,7 +4,7 @@ import fs from 'fs' import path from 'path' import * as IPFS from 'ipfs-core' 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 connectPeers from './utils/connect-nodes.js' import waitFor from './utils/wait-for.js'