GithubHelp home page GithubHelp logo

eweser-db's Introduction

eweserdb

🐑🐑🐑 EweserDB, the user-owned database 🐑🐑🐑

Matrix

A User owned database

EweserDB, (pronounced 'user deebee') empowers developers to quickly create a user-owned database that is local-first yet syncs to the cloud

it's like a decentralized 🔥firebase🔥.

It syncs user data to a Matrix chat room using a yjs CRDT. Builds on the matrix-crdt library to provide NoSQL style document database.

Features:

  • User owned
  • Interoperable between apps
  • Real-time sync between devices
  • Local-first / offline-first
  • Real-time collaboration
  • No backend code needed
  • Strongly typed schemas
  • Extensible
  • Fully open source

Motivation

Frustration

I've been using this note-taking app for a while and built up a ton of data in it.

I want to try out or migrate to a new app, but I feel locked in.

Ugh! 😤 I have to start over and re-enter all my data. Even if they provide an export, it usually doesn't work or is missing data.

Current paradigm:

You ask the app: "Can I see my data?" e.g.

You ask Facebook:

"Can I see my friends list, please?"

User-owned data paradigm:

The app asks you: Can I see your data? e.g.

Facebook asks you:

"Can I see your friends list?"

This flipping of the ownership dynamic enables some important features:

  • Data changed in the user-owned database by one app will sync to other apps.
  • Users can leave an app's platform at any time and take their data with them to a new one with no loss.
  • Apps cannot survive purely on vendor lock-in or network effects and must instead compete to provide the best experience for users.
  • Third party apps can build new features and use your data in new innovative ways without relying on the first app's permission, or on them maintaining an API.
  • Users can get started or try out a new app with all their data ready, with no onboarding, and are able to enjoy all the features of the new app right away.

Get Started

npm install @eweser/db

and also our peer dependencies (currently not bundled, but could be based on feedback/user preference)

npm install matrix-crdt matrix-js-sdk

This is a simplified example. For a more use cases and working demos see the example apps like packages/example-basic/src/App.tsx

import { Database } from '@eweser/db';
import type { Note } from '@eweser/db';

const db = new Database();

const collectionKey = 'notes';
const aliasSeed = 'notes-default';
const initialRoomConnect = {
  collectionKey,
  aliasSeed,
};

// If a user has previously logged in `load` will try to start up the database connected to the collections provided in the array.
// If offline, it will open up a local-only database and the user can start interacting with the data immediately. If online, it will also connect the matrix rooms and start syncing.
db.load([initialRoomConnect]);

db.login({
  userId: 'user',
  password: 'password',
  baseUrl: 'https://matrix.org', // or your own homeserver, or the user's self-hosted homeserver. If using matrix.org, instruct users to signup for an account at https://app.element.io/.
  initialRoomConnect,
});
// ...
db.on('my-listener', ({ event }) => {
  if (event === 'started') {
    // load up the ui and start using the database
  }
});

const room = db.getRoom<Note>(collectionKey, aliasSeed); // this is a matrix room that stores a collection of note documents

// the room prepares a yjs doc that you can use to make changes and listen for changes
const doc = room.ydoc.getMap('documents');
doc.observe((event) => {
  console.log('ydoc changed', event);
});

const newNote: Note = {
  _id: 'note-id',
  text: 'hello world',
};
doc.set(newNote._id, newNote);

That's it! 🚀🚀🚀 You now have a user-owned database that syncs between devices and apps.

Try opening in another browser or device and see the changes sync. Refresh the page and see the data persist. Turn off your internet and see the data still updates.

Consider using syncedStore to make manipulating the ydoc even easier.

Features

Structure

See packages/db/examples/dbShape.ts for how the data is structured.

Like MongoDB, EweserDB has documents and collections. In SQL database terms, collections are like tables and documents like rows. Each collection can have only one document. Documents have a strict schema(typescript type). See examples of documents in the /collections folder.

Documents can be linked by reference using the document's _ref property. The ref is simply the <collection>.<roomAlias>.<documentId> e.g. flashcards.#roomName~flashcards~@username:matrix.org.doc-id. Say you wanted to store a reference to a note from a flashcard, you could add the following to the flashcard document:

{
  note_ref: db.buildRef('notes', 'default', 'note-id'),
}

when using a document ref in another document, remember to add the _ref suffix to the property name. e.g. flashcardRef becomes flashcardRef_ref.

Rooms

Each room corresponds to a Matrix chat room that will be created on the user's Matrix account inside a space called "My Database". Rooms in EweserDB are private (invite-only) and (coming soon) will be fully end to end encrypted by default.

The registry is a special collection that stores the addresses(roomAliass) to all of the user's other rooms.

ACL - Access Control, Privacy and Sharing

Building on top of Matrix allows for advanced ACL features right out of the box. All ACL happens on the room level. Users can decide which apps or other users have read or write access to which rooms simply by using Matrix's built in privacy control features and by inviting or kicking out other users in the room.

User owned

Matrix is a 'federated' system, working towards 100% user-owned and decentralized. When a user signs up to the DB they must provide a Matrix homeserver url. The user's data lives on the home server so at first glance it appears that the server owns the data and this is no different than facebook servers owning/controlling the data.

The first big difference is that users can sign up using a self-hosted homeserver or a homeserver of their choice that they trust more.

Another key distinction is that with end to end encryption enabled (coming soon), the homeserver cannot read any of the data.

Thirdly, because of Matrix's federated model, users could have a second homeserver that connects to each of their rooms, and their data would be stored on both, decreasing centralized control.

In the future, Matrix has plans to roll out full P2P functionality, which would allow each device to act as its own homeserver.

EweserDB also plans to increase user ownership by making backing up and restoring the user's data easy and automatic. Backups could be through a traditional provider like dropbox or web3 options like IPFS (through pinata) or Ethereum (through swarm).

Extending the Database and Collections

Say you'd like to add a new collection or document type, like TodoItem, or BlogPost etc. Either you'd like it to be only accessible by your app, or to other apps that use eweser-db as well.

To make the collection usable by other apps, submit a pull request to add the collection types. Follow the examples in the db/src/collections folder.

To only use a document in your app that you don't want made available to other apps, fork the project and do the same thing, or extend locally and use some //@ts-ignores for the type errors you will encounter.

App development strategy, user consent

As it is designed now, when the user signs in, the app will have read/write access to the full user-owned database for the duration of the session. Users should be made aware of this fact when logging in to an app. People are already comfortable with an app managing its own data, but it is another level of trust required in the app to also let it manage data used by other apps. Users need to know the level of trust they are putting in each app when they sign ing.

Because user-owned data flips the current data storage paradigm on its head, app developers might be wondering how to share public data between users. For example, a user might mark a certain collection of notes as public and apps could aggregate them and let other users search and discover those.

One solution would be that for each room marked as 'public' by the user, we could invite an observer account into the room.

Another option would be to simply use a traditional database for public collections, but then that would break interoperability between apps.

This is an area that needs further consideration. Community input is appreciated.

Limitations

  • This project is still in Alpha. It is not ready for production use. Database schema and API are subject to change.
  • Matrix events size limit
  • Matrix-crdt e2ee support
  • connecting to rooms can be slow depending on the homeserver, especially the getRoomIdForAlias call when the homeserver has many rooms it needs to search through. Because the registry stores the id, the second time a room is connected to it should be faster.
  • Developers should minimize the number of rooms the user has connected to at any given time and use room.matrixProvider.dispose() to disconnect from rooms when they are not needed. Otherwise you might run into an error saying there are too many event listeners.

Contribute and develop

  • Make an app with EweserDB and provide feedback. We can make an 'awesome-eweserdb' list of interoperable apps that use it.
  • Submit a pull request to add (for example):
    • a new collection type to the db package.
    • a new e2e test to the e2e package.
    • a new example app to the example package.
    • a new feature to the db package.
    • something from the to do list below.

Set up local dev

npm install && lerna bootstrap npm run dev

The example apps are in in packages/example-basic and packages/example-editor etc. you Example apps will be served at http://localhost:8081/, http://localhost:8082/ etc.

Run unit tests by first starting the docker server (make sure you have docker running) with npm run start-test-server and then npm run test

Run e2e tests headless once with npm run test:e2e, or with npm run dev-e2e to open the Cypress GUI.

To Do

Priority:

  • set up cross collection reference links and helpers. -async getLinkedRef() connect the linked ref’s room if needed and retrieve the linked document
  • Example: connect data from 2 apps with refs. e.g. in a note, click ‘turn into flashcard’ and it creates a flashcard in the flashcard app and links to it in the note.

--- at this point ready to make real apps ---

  • Files: set up file hosting provider services like Pinata, Dropbox, etc. and give users the option to connect their accounts to the app. Could also try the ‘matrix files’ library.
  • Public data: set up ‘aggregator’ listeners when a user makes a collection as public. These will be MatrixReader’s that live on a node server and listen for changes to the collection. How to aggregate and serve to public listeners?
  • Backups - user can add storage account (dropbox, pinata, etc) that store snapshots of the database in the file hosting provider.
  • End 2 End Encryption — multiple devices?
  • Sharing,: user can invite another to a room and collaborate on the documents within. Can also just be read only
  • Versioning strategy for schema/api changes. How to maintain backwards compatibility with older versions of the database.
  • Per-App Access control. Instead of signing in the matrix client as the user, we could instead sign in with a Matrix account provided by the app owner, and then have the use invite that account into each room, specifying read-only or write permissions.

Nice to haves:

  • Add WebRTC to addTempDocToRoom()
  • create an async cron job that runs after the db loads, and once a day, to delete items with the _deleted flag whose ttl has expired.
  • set up the ‘awareness’ listeners for shared editing.
  • example of next.js, server-side rendering workarounds
  • give some instructions on self-hosting.
  • set up a matrix synapse server so that users can sign up with me instead of matrix.org.
  • make 2 servers and federate them. Figure out a federation-as-backup strategy for users, for example inviting another of our accounts as a listener to each of your rooms.
  • “Joins” or aggregation searches across collections. e.g. select all documents in the notes collection that have a ref to a document in the flashcards collection.
  • Stress testing. warnings about room or document size limits
  • offline mode: offline first - allow interacting with app before ever signed up or logged in. This might just need to be an example, not db feature. Might need to add an 'initialValues' option to create and connect room.

eweser-db's People

Contributors

jacobcoro avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.