mirror of
https://github.com/orbitdb/orbitdb.git
synced 2025-05-29 18:26:36 +00:00
refactor: Match database registration to AC/IDP registration processes.
This commit is contained in:
parent
94a8988115
commit
c4f085b267
@ -14,6 +14,8 @@
|
|||||||
*/
|
*/
|
||||||
import Database from '../database.js'
|
import Database from '../database.js'
|
||||||
|
|
||||||
|
const type = 'documents'
|
||||||
|
|
||||||
const DefaultOptions = { indexBy: '_id' }
|
const DefaultOptions = { indexBy: '_id' }
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -145,7 +147,7 @@ const Documents = ({ indexBy } = DefaultOptions) => async ({ ipfs, identity, add
|
|||||||
|
|
||||||
return {
|
return {
|
||||||
...database,
|
...database,
|
||||||
type: 'documents',
|
type,
|
||||||
put,
|
put,
|
||||||
del,
|
del,
|
||||||
get,
|
get,
|
||||||
@ -156,4 +158,6 @@ const Documents = ({ indexBy } = DefaultOptions) => async ({ ipfs, identity, add
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Documents.type = type
|
||||||
|
|
||||||
export default Documents
|
export default Documents
|
||||||
|
@ -8,6 +8,8 @@
|
|||||||
*/
|
*/
|
||||||
import Database from '../database.js'
|
import Database from '../database.js'
|
||||||
|
|
||||||
|
const type = 'events'
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Defines an Events database.
|
* Defines an Events database.
|
||||||
* @return {module:Databases.Databases-Events} A Events function.
|
* @return {module:Databases.Databases-Events} A Events function.
|
||||||
@ -86,7 +88,7 @@ const Events = () => async ({ ipfs, identity, address, name, access, directory,
|
|||||||
|
|
||||||
return {
|
return {
|
||||||
...database,
|
...database,
|
||||||
type: 'events',
|
type,
|
||||||
add,
|
add,
|
||||||
get,
|
get,
|
||||||
iterator,
|
iterator,
|
||||||
@ -94,4 +96,6 @@ const Events = () => async ({ ipfs, identity, address, name, access, directory,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Events.type = type
|
||||||
|
|
||||||
export default Events
|
export default Events
|
||||||
|
@ -15,12 +15,7 @@ import KeyValueIndexed from './keyvalue-indexed.js'
|
|||||||
* @return [] An array of database types.
|
* @return [] An array of database types.
|
||||||
* @memberof module:Databases
|
* @memberof module:Databases
|
||||||
*/
|
*/
|
||||||
const databaseTypes = {
|
const databaseTypes = {}
|
||||||
events: Events,
|
|
||||||
documents: Documents,
|
|
||||||
keyvalue: KeyValue,
|
|
||||||
keyvalueindexed: KeyValueIndexed
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Add a new database type.
|
* Add a new database type.
|
||||||
@ -30,17 +25,23 @@ const databaseTypes = {
|
|||||||
* const database = await Database(...params)
|
* const database = await Database(...params)
|
||||||
* ...
|
* ...
|
||||||
* }
|
* }
|
||||||
* addDatabaseType('customDBType', CustomDBTypeModule)
|
* addDatabaseType(CustomDBTypeModule)
|
||||||
* @function addDatabaseType
|
* @function addDatabaseType
|
||||||
* @param {string} type The database type.
|
* @param {module:Databases} database A Database-compatible module.
|
||||||
* @param {module:Databases} store A Database-compatible module.
|
* @throws Database type does not contain required field \'type\'.
|
||||||
|
* @throws Database type '${store.type}' already added.
|
||||||
* @memberof module:Databases
|
* @memberof module:Databases
|
||||||
*/
|
*/
|
||||||
const addDatabaseType = (type, store) => {
|
const addDatabaseType = (database) => {
|
||||||
if (databaseTypes[type]) {
|
if (!database.type) {
|
||||||
throw new Error(`Type already exists: ${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) => {
|
const getDatabaseType = (type) => {
|
||||||
@ -55,4 +56,9 @@ const getDatabaseType = (type) => {
|
|||||||
return databaseTypes[type]
|
return databaseTypes[type]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
addDatabaseType(Events)
|
||||||
|
addDatabaseType(Documents)
|
||||||
|
addDatabaseType(KeyValue)
|
||||||
|
addDatabaseType(KeyValueIndexed)
|
||||||
|
|
||||||
export { addDatabaseType, getDatabaseType, Documents, Events, KeyValue, KeyValueIndexed }
|
export { addDatabaseType, getDatabaseType, Documents, Events, KeyValue, KeyValueIndexed }
|
||||||
|
@ -21,6 +21,7 @@ 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'
|
||||||
|
|
||||||
|
const type = 'keyvalueindexed'
|
||||||
const valueEncoding = 'json'
|
const valueEncoding = 'json'
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -119,4 +120,6 @@ const KeyValueIndexed = ({ storage } = {}) => async ({ ipfs, identity, address,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
KeyValueIndexed.type = type
|
||||||
|
|
||||||
export default KeyValueIndexed
|
export default KeyValueIndexed
|
||||||
|
@ -8,6 +8,8 @@
|
|||||||
*/
|
*/
|
||||||
import Database from '../database.js'
|
import Database from '../database.js'
|
||||||
|
|
||||||
|
const type = 'keyvalue'
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Defines an KeyValue database.
|
* Defines an KeyValue database.
|
||||||
* @return {module:Databases.Databases-KeyValue} A KeyValue function.
|
* @return {module:Databases.Databases-KeyValue} A KeyValue function.
|
||||||
@ -107,7 +109,7 @@ const KeyValue = () => async ({ ipfs, identity, address, name, access, directory
|
|||||||
|
|
||||||
return {
|
return {
|
||||||
...database,
|
...database,
|
||||||
type: 'keyvalue',
|
type,
|
||||||
put,
|
put,
|
||||||
set: put, // Alias for put()
|
set: put, // Alias for put()
|
||||||
del,
|
del,
|
||||||
@ -117,4 +119,6 @@ const KeyValue = () => async ({ ipfs, identity, address, name, access, directory
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
KeyValue.type = type
|
||||||
|
|
||||||
export default KeyValue
|
export default KeyValue
|
||||||
|
@ -16,6 +16,8 @@ const CustomStore = () => async ({ ipfs, identity, address, name, access, direct
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
CustomStore.type = type
|
||||||
|
|
||||||
describe('Add a custom database type', function () {
|
describe('Add a custom database type', function () {
|
||||||
this.timeout(5000)
|
this.timeout(5000)
|
||||||
|
|
||||||
@ -55,7 +57,7 @@ describe('Add a custom database type', function () {
|
|||||||
|
|
||||||
describe('Custom database type', function () {
|
describe('Custom database type', function () {
|
||||||
before(() => {
|
before(() => {
|
||||||
addDatabaseType(type, CustomStore)
|
addDatabaseType(CustomStore)
|
||||||
})
|
})
|
||||||
|
|
||||||
it('create a database with the custom database type', async () => {
|
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 () => {
|
it('throws and error if custom database type already exists', async () => {
|
||||||
let err
|
let err
|
||||||
try {
|
try {
|
||||||
addDatabaseType(type, CustomStore)
|
addDatabaseType(CustomStore)
|
||||||
throw new Error('This should not run.')
|
throw new Error('This should not run.')
|
||||||
} catch (e) {
|
} 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 () => {
|
it('returns custom database type after adding it', async () => {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user