mirror of
https://github.com/orbitdb/orbitdb.git
synced 2025-03-30 15:08:28 +00:00
refactor: Use a database type.
This commit is contained in:
parent
86f4c9cd52
commit
7888ff06a3
@ -205,7 +205,7 @@ db2.events.on('update', async (entry) => {
|
||||
|
||||
To learn more, check out [OrbitDB's sychronization protocol](https://orbitdb.org/api/module-Sync.html) and the [OrbitDB replication documentation](./REPLICATION.md).
|
||||
|
||||
## Building a custom database
|
||||
## Custom databases
|
||||
|
||||
OrbitDB can be extended to use custom data models and database types. To implement a custom database, ensure the Database object is extended and that the OrbitDB database interface is implement. The database will also require a unique type.
|
||||
|
||||
@ -260,3 +260,15 @@ export default CustomDB
|
||||
```
|
||||
|
||||
[Documents](../src/db/documents.js), [Events](../src/db/events.js) and [KeyValue](../src/db/keyvalue.js) provide good examples of how a database is implemented in OrbitDB and how to add the logic for returning records from the database (the state of the database).
|
||||
|
||||
To use a custom database, add it to the list of supported database types:
|
||||
|
||||
```js
|
||||
import { createOrbitDB, useDatabaseType } from '@orbitdb/core'
|
||||
import { CustomDB } from './custom-db.js'
|
||||
|
||||
useDatabaseType(CustomDB)
|
||||
const orbitdb = await createOrbitDB()
|
||||
await orbitdb.open('my-custom-db', { type: 'customdb' })
|
||||
```
|
||||
|
||||
|
@ -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 }
|
||||
|
@ -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
|
||||
|
@ -7,7 +7,7 @@ export {
|
||||
Events,
|
||||
KeyValue,
|
||||
KeyValueIndexed,
|
||||
addDatabaseType
|
||||
useDatabaseType
|
||||
} from './databases/index.js'
|
||||
|
||||
export {
|
||||
|
@ -2,7 +2,7 @@ import { strictEqual, deepStrictEqual, notStrictEqual } from 'assert'
|
||||
import rmrf from 'rimraf'
|
||||
import * as IPFS from 'ipfs-core'
|
||||
import { getDatabaseType } from '../src/databases/index.js'
|
||||
import { createOrbitDB, addDatabaseType, Database } from '../src/index.js'
|
||||
import { createOrbitDB, useDatabaseType, Database } from '../src/index.js'
|
||||
import config from './config.js'
|
||||
|
||||
const type = 'custom!'
|
||||
@ -57,7 +57,7 @@ describe('Add a custom database type', function () {
|
||||
|
||||
describe('Custom database type', function () {
|
||||
before(() => {
|
||||
addDatabaseType(CustomStore)
|
||||
useDatabaseType(CustomStore)
|
||||
})
|
||||
|
||||
it('create a database with the custom database type', async () => {
|
||||
@ -67,18 +67,6 @@ describe('Add a custom database type', function () {
|
||||
strictEqual(db.name, name)
|
||||
})
|
||||
|
||||
it('throws and error if custom database type already exists', async () => {
|
||||
let err
|
||||
try {
|
||||
addDatabaseType(CustomStore)
|
||||
throw new Error('This should not run.')
|
||||
} catch (e) {
|
||||
err = e.toString()
|
||||
}
|
||||
|
||||
strictEqual(err, 'Error: Database type \'custom!\' already added.')
|
||||
})
|
||||
|
||||
it('returns custom database type after adding it', async () => {
|
||||
deepStrictEqual(getDatabaseType(type), CustomStore)
|
||||
})
|
||||
|
Loading…
x
Reference in New Issue
Block a user