Merge pull request #1120 from orbitdb/docs/AC

Docs/ac
This commit is contained in:
Hayden Young 2023-11-08 08:00:18 +08:00 committed by GitHub
commit 81006341fe
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 8 additions and 48 deletions

View File

@ -8,15 +8,13 @@ Different access controllers can be assigned to the database using the `AccessCo
```js ```js
import { create } from 'ipfs-core' import { create } from 'ipfs-core'
import { createOrbitDB, getAccessController } from '@orbitdb/core' import { createOrbitDB } from '@orbitdb/core'
import * as SomeAccessController from 'some-access-controller.js'
const ipfs = create({ options }) const ipfs = create({ options })
const orbitdb = await createOrbitDB({ ipfs }) const orbitdb = await createOrbitDB({ ipfs })
// SomeAccessController must already be available in the AC list.
const SomeAccessController = getAccessController('some-access-controller')
const db = orbitdb.open('my-db', { AccessController: SomeAccessController() }) const db = orbitdb.open('my-db', { AccessController: SomeAccessController() })
``` ```
@ -37,7 +35,7 @@ To change write access, pass the IPFSAccessController with the `write` parameter
```js ```js
import { create } from 'ipfs-core' import { create } from 'ipfs-core'
import { createOrbitDB, Identities, getAccessController } from '@orbitdb/core' import { createOrbitDB, Identities, IPFSAccessController } from '@orbitdb/core'
const ipfs = create({ options }) const ipfs = create({ options })
@ -47,9 +45,6 @@ const anotherIdentity = identities.createIdentity('userB')
// OrbitDB will create an identity using the id 'UserA'. // OrbitDB will create an identity using the id 'UserA'.
const orbitdb = await createOrbitDB({ ipfs, id: 'userA' }) const orbitdb = await createOrbitDB({ ipfs, id: 'userA' })
// Retrieve the access controller from the list of preloaded ACs.
const IPFSAccessController = getAccessController('ipfs')
// Open a db with write access for userA and userB. // Open a db with write access for userA and userB.
const db = orbitdb.open('my-db', { AccessController: IPFSAccessController({ write: [orbitdb.identity.id, anotherIdentity.id]) }) const db = orbitdb.open('my-db', { AccessController: IPFSAccessController({ write: [orbitdb.identity.id, anotherIdentity.id]) })
``` ```
@ -58,14 +53,12 @@ To allow anyone to write to the database, specify the wildcard '*':
```js ```js
import { create } from 'ipfs-core' import { create } from 'ipfs-core'
import { createOrbitDB, Identities, getAccessController } from '@orbitdb/core' import { createOrbitDB, Identities, IPFSAccessController } from '@orbitdb/core'
const ipfs = create({ options }) const ipfs = create({ options })
const orbitdb = await createOrbitDB({ ipfs }) const orbitdb = await createOrbitDB({ ipfs })
const IPFSAccessController = getAccessController('ipfs')
const db = orbitdb.open('my-db', { AccessController: IPFSAccessController({ write: ['*'] }) }) const db = orbitdb.open('my-db', { AccessController: IPFSAccessController({ write: ['*'] }) })
``` ```
@ -77,7 +70,7 @@ The OrbitDB access controller provides configurable write access using grant and
```js ```js
import { create } from 'ipfs-core' import { create } from 'ipfs-core'
import { createOrbitDB, Identities, getAccessController } from '@orbitdb/core' import { createOrbitDB, Identities, OrbitDBAccessController } from '@orbitdb/core'
const ipfs = create({ options }) const ipfs = create({ options })
@ -86,9 +79,6 @@ const orbitdb = await createOrbitDB({ ipfs })
const identities = await Identities() const identities = await Identities()
const anotherIdentity = identities.createIdentity('userB') const anotherIdentity = identities.createIdentity('userB')
// Retrieve the access controller from the list of preloaded ACs.
const OrbitDBAccessController = getAccessController('orbitdb')
const db = orbitdb.open('my-db', { AccessController: OrbitDBAccessController({ write: [orbitdb.identity.id, anotherIdentity.id]) }) const db = orbitdb.open('my-db', { AccessController: OrbitDBAccessController({ write: [orbitdb.identity.id, anotherIdentity.id]) })
db.access.grant('write', anotherIdentity.id) db.access.grant('write', anotherIdentity.id)

View File

@ -117,7 +117,7 @@ npm init
Create a new file called index.js and paste in the following code: Create a new file called index.js and paste in the following code:
```js ```js
import { OrbitDB, getAccessController } from '@orbitdb/core' import { OrbitDB, IPFSAccessController } from '@orbitdb/core'
import { create } from 'ipfs-core' import { create } from 'ipfs-core'
const main = async () => { const main = async () => {
@ -139,10 +139,6 @@ const main = async () => {
// ./[randDir]/ipfs. // ./[randDir]/ipfs.
const orbitdb = await createOrbitDB({ ipfs, directory: './' + randDir + '/orbitdb' }) const orbitdb = await createOrbitDB({ ipfs, directory: './' + randDir + '/orbitdb' })
// Get the IPFS AccessController function. We will need it to ensure everyone
// can write to the database.
const AccessController = getAccessController('ipfs')
let db let db
if (process.argv[2]) { if (process.argv[2]) {
@ -154,7 +150,7 @@ const main = async () => {
// to write to the database. A more robust solution would use the // to write to the database. A more robust solution would use the
// OrbitDBAccessController to provide "fine-grain" access using grant and // OrbitDBAccessController to provide "fine-grain" access using grant and
// revoke. // revoke.
db = await orbitdb.open('my-db', { AccessController: AccessController({ write: ['*']})}) db = await orbitdb.open('my-db', { AccessController: IPFSAccessController({ write: ['*']})})
} }
// Copy this output if you want to connect a peer to another. // Copy this output if you want to connect a peer to another.

View File

@ -39,22 +39,12 @@ const useAccessController = (accessController) => {
accessControllers[accessController.type] = accessController accessControllers[accessController.type] = accessController
} }
/**
* Removes an access controller from the list.
* @param {string} type A valid access controller type.
* @static
*/
const removeAccessController = type => {
delete accessControllers[type]
}
useAccessController(IPFSAccessController) useAccessController(IPFSAccessController)
useAccessController(OrbitDBAccessController) useAccessController(OrbitDBAccessController)
export { export {
getAccessController, getAccessController,
useAccessController, useAccessController,
removeAccessController,
IPFSAccessController, IPFSAccessController,
OrbitDBAccessController OrbitDBAccessController
} }

View File

@ -2,7 +2,7 @@ import { strictEqual, deepStrictEqual, notStrictEqual } from 'assert'
import { rimraf } from 'rimraf' import { rimraf } from 'rimraf'
import * as IPFS from 'ipfs-core' import * as IPFS from 'ipfs-core'
import OrbitDB from '../src/orbitdb.js' import OrbitDB from '../src/orbitdb.js'
import { IPFSAccessController, OrbitDBAccessController, useAccessController, getAccessController, removeAccessController } from '../src/access-controllers/index.js' import { IPFSAccessController, OrbitDBAccessController, useAccessController, getAccessController } from '../src/access-controllers/index.js'
import config from './config.js' import config from './config.js'
import pathJoin from '../src/utils/path-join.js' import pathJoin from '../src/utils/path-join.js'
@ -38,9 +38,6 @@ describe('Add a custom access controller', function () {
await ipfs.stop() await ipfs.stop()
} }
// Remove the added custom database type from OrbitDB import
removeAccessController(type)
await rimraf('./orbitdb') await rimraf('./orbitdb')
await rimraf('./ipfs1') await rimraf('./ipfs1')
}) })
@ -94,18 +91,5 @@ describe('Add a custom access controller', function () {
it('returns custom access controller after adding it', async () => { it('returns custom access controller after adding it', async () => {
deepStrictEqual(getAccessController(type), CustomAccessController) deepStrictEqual(getAccessController(type), CustomAccessController)
}) })
it('can be removed from supported access controllers', async () => {
let err
removeAccessController(type)
try {
getAccessController(type)
} catch (e) {
err = e.toString()
}
deepStrictEqual(err, 'Error: AccessController type \'custom!\' is not supported')
})
}) })
}) })