refactor: Use a database type.

This commit is contained in:
Hayden Young
2023-09-07 09:29:33 +01:00
parent 86f4c9cd52
commit 7888ff06a3
5 changed files with 24 additions and 32 deletions

View File

@@ -20,27 +20,23 @@ const databaseTypes = {}
/**
* Add a new database type.
* @example
* import { addDatabaseType } from 'orbitdb'
* import { useDatabaseType } from 'orbitdb'
* const CustomDBTypeModule = async (params) => {
* const database = await Database(...params)
* ...
* }
* addDatabaseType(CustomDBTypeModule)
* @function addDatabaseType
* useDatabaseType(CustomDBTypeModule)
* @function useDatabaseType
* @param {module:Databases} database A Database-compatible module.
* @throws Database type does not contain required field \'type\'.
* @throws Database type '${store.type}' already added.
* @memberof module:Databases
*/
const addDatabaseType = (database) => {
const useDatabaseType = (database) => {
if (!database.type) {
throw new Error('Database type does not contain required field \'type\'.')
}
if (databaseTypes[database.type]) {
throw new Error(`Database type '${database.type}' already added.`)
}
databaseTypes[database.type] = database
}
@@ -56,9 +52,8 @@ const getDatabaseType = (type) => {
return databaseTypes[type]
}
addDatabaseType(Events)
addDatabaseType(Documents)
addDatabaseType(KeyValue)
addDatabaseType(KeyValueIndexed)
useDatabaseType(Events)
useDatabaseType(Documents)
useDatabaseType(KeyValue)
export { addDatabaseType, getDatabaseType, Documents, Events, KeyValue, KeyValueIndexed }
export { useDatabaseType, getDatabaseType, Documents, Events, KeyValue, KeyValueIndexed }

View File

@@ -21,7 +21,6 @@ import KeyValue from './keyvalue.js'
import LevelStorage from '../storage/level.js'
import pathJoin from '../utils/path-join.js'
const type = 'keyvalueindexed'
const valueEncoding = 'json'
/**
@@ -120,6 +119,4 @@ const KeyValueIndexed = ({ storage } = {}) => async ({ ipfs, identity, address,
}
}
KeyValueIndexed.type = type
export default KeyValueIndexed