Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

mfdb

Package Overview
Dependencies
Maintainers
1
Versions
19
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

mfdb

MFdb is a portable P2P database backed by the IPFS Mutable File System.

  • 0.0.10-alpha
  • npm
  • Socket score

Version published
Weekly downloads
2
increased by100%
Maintainers
1
Weekly downloads
 
Created
Source

MFdb

MFdb is a portable P2P database backed by the IPFS Mutable File System.

Create large searchable datasets and easily share them with others. Works in Node and the browser.

Create a database locally and store it in IPFS. Then share the hash with others. They load it and can instantly search it after downloading. Since each commit is written to MFS you can also load a database at each intermediate state if you have the CID. Broadcast these changes and mirrored copies can stay up to date in real time. All merging happens at the Mutable File System level.

  • A database contain multiple tables.
  • Each table has a schema.
  • Index multiple fields.
  • Create unique and non-unique indexes. A b-tree is created for each index.
  • Broadcast updates to peers via IPNS. (coming soon)
  • Backup/load full database from hash.
  • Lightweight. Less than 300kb total.

Query data by primary and non-primary indexes. Each table has a schema and when records are inserted we build btree indexes. B-trees are loaded into memory when fields are searched.

The entire database is stored in an IPFS Mutable File System folder and can be exchanged with a single hash. Broadcast the hash via IPNS and any peers can sync up to the latest version. The sync speed depends on many different factors but should be faster the more peers you have.

Install

npm install ipfs mfdb

Usage

First initialize MFdb with IPFS.

import assert from 'assert'
import { MFdb } from "mfdb"

const ipfs = await IPFS.create()

const db = new MFdb(ipfs)

Create a database

await db.createDatabase("my-database")

Use the database

db.useDatabase("my-database")

Create a table with a schema. In this example we're saving baseball players. We'll add 4 different indexed fields. Indexes can be unique.

await db.createTable({
    database: "my-database",
    name: "players",
    schema: {
        name: { unique: false },
        currentTeam: { unique: false },
        battingHand: { unique: false },
        throwingHand: { unique: false }
    }
})

Add a record and retreive it by the primary key


//Start transaction
db.startTransaction()

//Save it
await db.put("players", 101, {
    id: 101,
    name: "Andrew McCutchen",
    currentTeam: "PIT",
    battingHand: "R",
    throwingHand: "R"
})

//Commit changes
await db.commit()


//Retrieve it
let player = await store.get("players", 101)

Now we're going to add a few more players


//Start transaction
db.startTransaction()

await store.put(102, {
  id: 102,
  name: "Pedro Alvarez",
  currentTeam: "BAL",
  battingHand: "R",
  throwingHand: "R"
})

await store.put(103, {
    id: 103,
    name: "Jordy Mercer",
    currentTeam: "PIT",
    battingHand: "L",
    throwingHand: "R"
})

await store.put(104, {
    id: 104,
    name: "Doug Drabek",
    currentTeam: "BAL",
    battingHand: "L",
    throwingHand: "R"
})

//Commit changes
await db.commit()

Now retreive the values by the secondary indexes.

  //Get players who play for PIT
  let teamPIT = await store.getByIndex("players", "currentTeam", "PIT", "desc", 0, 100)

  //Get players who who bat right handed. 
  let battingR = await store.getByIndex("players", "battingHand", "R", "desc", 0, 100)

Get the CID of the database.

let cid = await db.getDatabaseCid('players')

Load database from CID.

await db.updateFromCid("test", "QmSRtaj7Vu8Q4YhcU32xaiHvmKR4KD5h7WR27azzeECCki")

Examples can be found in the tests.

API

MFdb exposes the following functions:

startTransaction()

  • Discard any uncommitted changes and begin a new transaction. All puts and deletes must have an active transaction.

async commit()

  • Flushes uncommitted changes to disk.

async createDatabase(name: string)

  • Creates a database with the passed in name.

async createTable(definition: TableDefinition)

  • Takes in a TableDefinition and creates a table.
interface TableDefinition {
    database: string, //database name
    name: string //table name
    schema: Schema //Schema
}

async databaseExists(name: string)

  • Checks whether a database exists or not.

async dropDatabase(name: string)

  • Drops a database by name.

useDatabase(name: string)

  • Switch the active database to the passed in one. All table related functions will use this value.

async put(table: string, key: any, value: any)

  • Stores a key/value pair in a particular table. Table must exist.

async get(table: string, key: any)

  • Gets a value by key. Takes in the table name.

async getCID(table: string, key: any)

  • Gets the CID of the value stored in a specific table with key.

async getByIndex(table: string, indexName, value, sortDirection, offset = 0, limit = 1)

  • Get rows in a table that are indexed by a particular field. Limit is 1 by default.

async delete(table:string, key: any)

  • Delete a row in a table by key.

async count(table: string)

  • Count the records in a table.

async getDatabaseCid(name: string)

  • Get the IPFS CID for the mutable file system folder where the database is stored.

FAQs

Package last updated on 17 Jan 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc