refactor: Match database registration to AC/IDP registration processes.

This commit is contained in:
Hayden Young 2023-09-05 22:52:51 +01:00
parent 94a8988115
commit c4f085b267
6 changed files with 44 additions and 21 deletions

View File

@ -14,6 +14,8 @@
*/
import Database from '../database.js'
const type = 'documents'
const DefaultOptions = { indexBy: '_id' }
/**
@ -145,7 +147,7 @@ const Documents = ({ indexBy } = DefaultOptions) => async ({ ipfs, identity, add
return {
...database,
type: 'documents',
type,
put,
del,
get,
@ -156,4 +158,6 @@ const Documents = ({ indexBy } = DefaultOptions) => async ({ ipfs, identity, add
}
}
Documents.type = type
export default Documents

View File

@ -8,6 +8,8 @@
*/
import Database from '../database.js'
const type = 'events'
/**
* Defines an Events database.
* @return {module:Databases.Databases-Events} A Events function.
@ -86,7 +88,7 @@ const Events = () => async ({ ipfs, identity, address, name, access, directory,
return {
...database,
type: 'events',
type,
add,
get,
iterator,
@ -94,4 +96,6 @@ const Events = () => async ({ ipfs, identity, address, name, access, directory,
}
}
Events.type = type
export default Events

View File

@ -15,12 +15,7 @@ import KeyValueIndexed from './keyvalue-indexed.js'
* @return [] An array of database types.
* @memberof module:Databases
*/
const databaseTypes = {
events: Events,
documents: Documents,
keyvalue: KeyValue,
keyvalueindexed: KeyValueIndexed
}
const databaseTypes = {}
/**
* Add a new database type.
@ -30,17 +25,23 @@ const databaseTypes = {
* const database = await Database(...params)
* ...
* }
* addDatabaseType('customDBType', CustomDBTypeModule)
* addDatabaseType(CustomDBTypeModule)
* @function addDatabaseType
* @param {string} type The database type.
* @param {module:Databases} store A Database-compatible module.
* @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 = (type, store) => {
if (databaseTypes[type]) {
throw new Error(`Type already exists: ${type}`)
const addDatabaseType = (database) => {
if (!database.type) {
throw new Error('Database type does not contain required field \'type\'.')
}
databaseTypes[type] = store
if (databaseTypes[database.type]) {
throw new Error(`Database type '${database.type}' already added.`)
}
databaseTypes[database.type] = database
}
const getDatabaseType = (type) => {
@ -55,4 +56,9 @@ const getDatabaseType = (type) => {
return databaseTypes[type]
}
addDatabaseType(Events)
addDatabaseType(Documents)
addDatabaseType(KeyValue)
addDatabaseType(KeyValueIndexed)
export { addDatabaseType, getDatabaseType, Documents, Events, KeyValue, KeyValueIndexed }

View File

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

View File

@ -8,6 +8,8 @@
*/
import Database from '../database.js'
const type = 'keyvalue'
/**
* Defines an KeyValue database.
* @return {module:Databases.Databases-KeyValue} A KeyValue function.
@ -107,7 +109,7 @@ const KeyValue = () => async ({ ipfs, identity, address, name, access, directory
return {
...database,
type: 'keyvalue',
type,
put,
set: put, // Alias for put()
del,
@ -117,4 +119,6 @@ const KeyValue = () => async ({ ipfs, identity, address, name, access, directory
}
}
KeyValue.type = type
export default KeyValue

View File

@ -16,6 +16,8 @@ const CustomStore = () => async ({ ipfs, identity, address, name, access, direct
}
}
CustomStore.type = type
describe('Add a custom database type', function () {
this.timeout(5000)
@ -55,7 +57,7 @@ describe('Add a custom database type', function () {
describe('Custom database type', function () {
before(() => {
addDatabaseType(type, CustomStore)
addDatabaseType(CustomStore)
})
it('create a database with the custom database type', async () => {
@ -68,13 +70,13 @@ describe('Add a custom database type', function () {
it('throws and error if custom database type already exists', async () => {
let err
try {
addDatabaseType(type, CustomStore)
addDatabaseType(CustomStore)
throw new Error('This should not run.')
} catch (e) {
err = e
err = e.toString()
}
notStrictEqual(err, undefined)
strictEqual(err.message.indexOf('already exists') !== -1, true)
strictEqual(err, 'Error: Database type \'custom!\' already added.')
})
it('returns custom database type after adding it', async () => {