Remove Log and Database dependency injection

This commit is contained in:
haad 2023-04-06 08:45:23 +03:00
parent a94d2a7918
commit d681bb720a
9 changed files with 37 additions and 38 deletions

View File

@ -1,7 +1,5 @@
import Database from './database.js'
import { Events, KeyValue, Documents } from './db/index.js'
import { Log, Entry } from './oplog/index.js'
import { ComposedStorage, IPFSBlockStorage, LevelStorage, LRUStorage } from './storage/index.js'
import { ComposedStorage, IPFSBlockStorage, LRUStorage } from './storage/index.js'
import KeyStore from './key-store.js'
import { Identities } from './identities/index.js'
import OrbitDBAddress, { isValidAddress } from './address.js'
@ -48,8 +46,6 @@ const addDatabaseType = (type, store) => {
// const defaultTimeout = 30000 // 30 seconds
const OpLog = { Log, Entry, IPFSBlockStorage, LevelStorage }
const OrbitDB = async ({ ipfs, id, identity, keystore, directory } = {}) => {
if (ipfs == null) {
throw new Error('IPFS instance is a required argument. See https://github.com/orbitdb/orbit-db/blob/master/API.md#createinstance')
@ -113,7 +109,7 @@ const OrbitDB = async ({ ipfs, id, identity, keystore, directory } = {}) => {
throw new Error(`Unsupported database type: '${type}'`)
}
const db = await DatabaseModel({ OpLog, Database, ipfs, identity, address: address.toString(), name, access: accessController, directory, meta, syncAutomatically: sync != null ? sync : true })
const db = await DatabaseModel({ ipfs, identity, address: address.toString(), name, access: accessController, directory, meta, syncAutomatically: sync != null ? sync : true })
db.events.on('close', onDatabaseClosed(address.toString()))

View File

@ -1,15 +1,14 @@
import { EventEmitter } from 'events'
import PQueue from 'p-queue'
import Sync from './sync.js'
import { Log, Entry } from './oplog/index.js'
import { ComposedStorage, LRUStorage, IPFSBlockStorage, LevelStorage } from './storage/index.js'
import pathJoin from './utils/path-join.js'
const defaultReferencesCount = 16
const defaultCacheSize = 1000
const Database = async ({ OpLog, ipfs, identity, address, name, access, directory, meta, headsStorage, entryStorage, indexStorage, referencesCount, syncAutomatically }) => {
const { Log, Entry } = OpLog
const Database = async ({ ipfs, identity, address, name, access, directory, meta, headsStorage, entryStorage, indexStorage, referencesCount, syncAutomatically }) => {
directory = pathJoin(directory || './orbitdb', `./${address}/`)
meta = meta || {}
referencesCount = referencesCount || defaultReferencesCount

View File

@ -1,5 +1,7 @@
const Documents = async ({ OpLog, Database, ipfs, identity, address, name, access, directory, storage, meta, syncAutomatically, indexBy = '_id' }) => {
const database = await Database({ OpLog, ipfs, identity, address, name, access, directory, storage, meta, syncAutomatically })
import Database from '../database.js'
const Documents = async ({ ipfs, identity, address, name, access, directory, storage, meta, syncAutomatically, indexBy = '_id' }) => {
const database = await Database({ ipfs, identity, address, name, access, directory, storage, meta, syncAutomatically })
const { addOperation, log } = database

View File

@ -1,5 +1,7 @@
const Events = async ({ OpLog, Database, ipfs, identity, address, name, access, directory, storage, meta, syncAutomatically }) => {
const database = await Database({ OpLog, ipfs, identity, address, name, access, directory, storage, meta, syncAutomatically })
import Database from '../database.js'
const Events = async ({ ipfs, identity, address, name, access, directory, storage, meta, syncAutomatically }) => {
const database = await Database({ ipfs, identity, address, name, access, directory, storage, meta, syncAutomatically })
const { addOperation, log } = database

View File

@ -1,12 +1,12 @@
import LevelStorage from '../storage/level.js'
import { KeyValue } from './index.js'
import LevelStorage from '../storage/level.js'
import pathJoin from '../utils/path-join.js'
import PQueue from 'p-queue'
const valueEncoding = 'json'
const KeyValueIndexed = async ({ OpLog, Database, ipfs, identity, address, name, access, directory, storage, meta }) => {
const keyValueStore = await KeyValue({ OpLog, Database, ipfs, identity, address, name, access, directory, storage, meta })
const KeyValueIndexed = async ({ ipfs, identity, address, name, access, directory, storage, meta }) => {
const keyValueStore = await KeyValue({ ipfs, identity, address, name, access, directory, storage, meta })
const { events, log } = keyValueStore
const queue = new PQueue({ concurrency: 1 })

View File

@ -1,5 +1,7 @@
const KeyValue = async ({ OpLog, Database, ipfs, identity, address, name, access, directory, storage, meta, syncAutomatically }) => {
const database = await Database({ OpLog, ipfs, identity, address, name, access, directory, storage, meta, syncAutomatically })
import Database from '../database.js'
const KeyValue = async ({ ipfs, identity, address, name, access, directory, storage, meta, syncAutomatically }) => {
const database = await Database({ ipfs, identity, address, name, access, directory, storage, meta, syncAutomatically })
const { addOperation, log } = database

View File

@ -2,7 +2,7 @@ import { strictEqual, deepStrictEqual } from 'assert'
import rmrf from 'rimraf'
import { copy } from 'fs-extra'
import * as IPFS from 'ipfs-core'
import { Log, Entry, Database, KeyStore, Identities } from '../src/index.js'
import { Database, KeyStore, Identities } from '../src/index.js'
import config from './config.js'
import testKeysPath from './fixtures/test-keys-path.js'
import connectPeers from './utils/connect-nodes.js'
@ -11,7 +11,6 @@ import ComposedStorage from '../src/storage/composed.js'
import IPFSBlockStorage from '../src/storage/ipfs-block.js'
import MemoryStorage from '../src/storage/memory.js'
const OpLog = { Log, Entry }
const keysPath = './testkeys'
describe('Database - Replication', function () {
@ -78,8 +77,8 @@ describe('Database - Replication', function () {
describe('Replicate across peers', () => {
beforeEach(async () => {
db1 = await Database({ OpLog, ipfs: ipfs1, identity: testIdentity1, address: databaseId, accessController, directory: './orbitdb1' })
db2 = await Database({ OpLog, ipfs: ipfs2, identity: testIdentity2, address: databaseId, accessController, directory: './orbitdb2' })
db1 = await Database({ ipfs: ipfs1, identity: testIdentity1, address: databaseId, accessController, directory: './orbitdb1' })
db2 = await Database({ ipfs: ipfs2, identity: testIdentity2, address: databaseId, accessController, directory: './orbitdb2' })
})
it('replicates databases across two peers', async () => {
@ -176,7 +175,7 @@ describe('Database - Replication', function () {
await db1.addOperation({ op: 'PUT', key: 1, value: 'record 1 on db 1' })
db2 = await Database({ OpLog, ipfs: ipfs2, identity: testIdentity2, address: databaseId, accessController, directory: './orbitdb2' })
db2 = await Database({ ipfs: ipfs2, identity: testIdentity2, address: databaseId, accessController, directory: './orbitdb2' })
db2.events.on('join', onConnected)
@ -200,8 +199,8 @@ describe('Database - Replication', function () {
it('uses given ComposedStorage with MemoryStorage/IPFSBlockStorage for entryStorage', async () => {
const storage1 = await ComposedStorage(await MemoryStorage(), await IPFSBlockStorage({ ipfs: ipfs1, pin: true }))
const storage2 = await ComposedStorage(await MemoryStorage(), await IPFSBlockStorage({ ipfs: ipfs2, pin: true }))
db1 = await Database({ OpLog, ipfs: ipfs1, identity: testIdentity1, address: databaseId, accessController, directory: './orbitdb1', entryStorage: storage1 })
db2 = await Database({ OpLog, ipfs: ipfs2, identity: testIdentity2, address: databaseId, accessController, directory: './orbitdb2', entryStorage: storage2 })
db1 = await Database({ ipfs: ipfs1, identity: testIdentity1, address: databaseId, accessController, directory: './orbitdb1', entryStorage: storage1 })
db2 = await Database({ ipfs: ipfs2, identity: testIdentity2, address: databaseId, accessController, directory: './orbitdb2', entryStorage: storage2 })
let connected1 = false
let connected2 = false
@ -241,8 +240,8 @@ describe('Database - Replication', function () {
describe('Events', () => {
beforeEach(async () => {
db1 = await Database({ OpLog, ipfs: ipfs1, identity: testIdentity1, address: databaseId, accessController, directory: './orbitdb1' })
db2 = await Database({ OpLog, ipfs: ipfs2, identity: testIdentity2, address: databaseId, accessController, directory: './orbitdb2' })
db1 = await Database({ ipfs: ipfs1, identity: testIdentity1, address: databaseId, accessController, directory: './orbitdb1' })
db2 = await Database({ ipfs: ipfs2, identity: testIdentity2, address: databaseId, accessController, directory: './orbitdb2' })
})
it('emits \'update\' once when one operation is added', async () => {

View File

@ -4,13 +4,12 @@ import { existsSync } from 'fs'
import { copy } from 'fs-extra'
import * as IPFS from 'ipfs-core'
import Path from 'path'
import { Log, Entry, Database, KeyStore, Identities } from '../src/index.js'
import { Database, Entry, KeyStore, Identities } from '../src/index.js'
import LevelStorage from '../src/storage/level.js'
import MemoryStorage from '../src/storage/memory.js'
import config from './config.js'
import testKeysPath from './fixtures/test-keys-path.js'
const OpLog = { Log, Entry }
const keysPath = './testkeys'
describe('Database', function () {
@ -58,7 +57,7 @@ describe('Database', function () {
})
it('adds an operation', async () => {
db = await Database({ OpLog, ipfs, identity: testIdentity, address: databaseId, accessController, directory: './orbitdb' })
db = await Database({ ipfs, identity: testIdentity, address: databaseId, accessController, directory: './orbitdb' })
const expected = 'zdpuAwhx6xVpnMPUA7Q4JrvZsyoti5wZ18iDeFwBjPAwsRNof'
const op = { op: 'PUT', key: 1, value: 'record 1 on db 1' }
const actual = await db.addOperation(op)
@ -70,7 +69,7 @@ describe('Database', function () {
describe('Options', () => {
it('uses default directory for headsStorage', async () => {
db = await Database({ OpLog, ipfs, identity: testIdentity, address: databaseId, accessController })
db = await Database({ ipfs, identity: testIdentity, address: databaseId, accessController })
const op = { op: 'PUT', key: 1, value: 'record 1 on db 1' }
const hash = await db.addOperation(op)
@ -90,7 +89,7 @@ describe('Database', function () {
})
it('uses given directory for headsStorage', async () => {
db = await Database({ OpLog, ipfs, identity: testIdentity, address: databaseId, accessController, directory: './custom-directory' })
db = await Database({ ipfs, identity: testIdentity, address: databaseId, accessController, directory: './custom-directory' })
const op = { op: 'PUT', key: 1, value: 'record 1 on db 1' }
const hash = await db.addOperation(op)
@ -112,7 +111,7 @@ describe('Database', function () {
it('uses given MemoryStorage for headsStorage', async () => {
const headsStorage = await MemoryStorage()
db = await Database({ OpLog, ipfs, identity: testIdentity, address: databaseId, accessController, directory: './orbitdb', headsStorage })
db = await Database({ ipfs, identity: testIdentity, address: databaseId, accessController, directory: './orbitdb', headsStorage })
const op = { op: 'PUT', key: 1, value: 'record 1 on db 1' }
const hash = await db.addOperation(op)
@ -123,7 +122,7 @@ describe('Database', function () {
it('uses given MemoryStorage for entryStorage', async () => {
const entryStorage = await MemoryStorage()
db = await Database({ OpLog, ipfs, identity: testIdentity, address: databaseId, accessController, directory: './orbitdb', entryStorage })
db = await Database({ ipfs, identity: testIdentity, address: databaseId, accessController, directory: './orbitdb', entryStorage })
const op = { op: 'PUT', key: 1, value: 'record 1 on db 1' }
const hash = await db.addOperation(op)
@ -135,7 +134,7 @@ describe('Database', function () {
describe('Events', () => {
beforeEach(async () => {
db = await Database({ OpLog, ipfs, identity: testIdentity, address: databaseId, accessController, directory: './orbitdb' })
db = await Database({ ipfs, identity: testIdentity, address: databaseId, accessController, directory: './orbitdb' })
})
it('emits \'close\' when the database is closed', async () => {

View File

@ -1,13 +1,13 @@
import { strictEqual, deepStrictEqual, notStrictEqual } from 'assert'
import rmrf from 'rimraf'
import * as IPFS from 'ipfs-core'
import { OrbitDB, addDatabaseType, databaseTypes } from '../src/index.js'
import { OrbitDB, addDatabaseType, databaseTypes, Database } from '../src/index.js'
import config from './config.js'
const type = 'custom!'
const CustomStore = async ({ OpLog, Database, ipfs, identity, address, name, accessController, directory, storage, meta }) => {
const database = await Database({ OpLog, ipfs, identity, address, name, accessController, directory, storage, meta })
const CustomStore = async ({ ipfs, identity, address, name, accessController, directory, storage, meta }) => {
const database = await Database({ ipfs, identity, address, name, accessController, directory, storage, meta })
return {
...database,