diff --git a/src/databases/documents.js b/src/databases/documents.js index c45457a..61b3625 100644 --- a/src/databases/documents.js +++ b/src/databases/documents.js @@ -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 diff --git a/src/databases/events.js b/src/databases/events.js index cd1bcc5..1ddfe87 100644 --- a/src/databases/events.js +++ b/src/databases/events.js @@ -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 diff --git a/src/databases/index.js b/src/databases/index.js index 7415cb6..acb521e 100644 --- a/src/databases/index.js +++ b/src/databases/index.js @@ -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 } diff --git a/src/databases/keyvalue-indexed.js b/src/databases/keyvalue-indexed.js index 28c3078..a66009a 100644 --- a/src/databases/keyvalue-indexed.js +++ b/src/databases/keyvalue-indexed.js @@ -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 diff --git a/src/databases/keyvalue.js b/src/databases/keyvalue.js index ae85bbd..e6ab4c0 100644 --- a/src/databases/keyvalue.js +++ b/src/databases/keyvalue.js @@ -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 diff --git a/test/orbitdb-custom-database-types.test.js b/test/orbitdb-custom-database-types.test.js index 76cb686..2773ad2 100644 --- a/test/orbitdb-custom-database-types.test.js +++ b/test/orbitdb-custom-database-types.test.js @@ -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 () => {