0.4.8 • Published 12 months ago

git-documentdb v0.4.8

Weekly downloads
327
License
MPL-2.0
Repository
github
Last release
12 months ago

GitDocumentDB

npm version License: MPL 2.0 Coverage Status

NOTICE: This is a document for GitDocumentDB v0.3.0-alpha

Offline-first DocumentDB using Git

Use GitDocumentDB to ...

:green_book: Store JSON documents into Git repository.

:art: Manage Git repository by PouchDB-like API.

:rocket: Synchronize JSON documents with a remote Git repository.

:arrows_counterclockwise: CI/CD through GitHub.

:dromedary_camel: Travel history of database snapshots.

You do not need knowledge of Git to start. However, you make the most of GitDocumentDB if you understand Git.

API

https://github.com/sosuisen/git-documentdb/blob/main/docs/git-documentdb.gitdocumentdb.md

Usage

Getting started

Prerequisite

Node.js 10 or later

Installation

npm i git-documentdb

NOTE: GitDocumentDB uses native addon (libgit2). If you receive errors about installation, you probably miss building tools and libraries. In Ubuntu 18:

sudo apt update
sudo apt install build-essential libssl-dev libkrb5-dev libc++-dev 

In Windows 10: The list below shows typical environments.

  • Node.js 12, Python 2.7.x, and Visual Studio 2017 Community (with Desktop development with C++).
  • npm config set msvs_version 2017

If you are still encountering install problems, documents about NodeGit and Building NodeGit from source may also help you.

Import

import { GitDocumentDB } from 'git-documentdb';

const gitDDB = new GitDocumentDB({
  db_name: 'db01', // Git working directory
});

Basic CRUD

  // Open
  await gitDDB.create(); // Git creates and opens a repository (/your/path/to/the/example/git-documentdb/db01/.git)
  // Create
  await gitDDB.put({ _id: 'nara', flower: 'cherry blossoms', season: 'spring' }); // Git adds 'nara.json' under the working directory and commits it.
  // Update
  await gitDDB.put({ _id: 'nara', flower: 'double cherry blossoms', season: 'spring' }); // Git adds an updated file and commits it.
  // Read
  const doc = await gitDDB.get('nara');
  console.log(doc); // doc = { flower: 'double cherry blossoms', season: 'spring', _id: 'nara' }
  // Delete
  await gitDDB.remove('nara'); // Git removes a file and commits it.

Synchronization

  const github_repository = 'https://github.com/enter_your_accunt_name/git-documentdb-example.git'; // Please enter your GitHub account name.
  const your_github_personal_access_token = 'Enter your personal access token with checked [repo]'; // See https://docs.github.com/en/github/authenticating-to-github/creating-a-personal-access-token
  await gitDDB.sync({
    live: true,
    remote_url: github_repository,
    auth: { type: 'github', personal_access_token: your_github_personal_access_token },
  });

Prefix search

 /**
    Create documents under sub-directories

    git-documentdb
    └── db01
        ├── nara
        │   ├── nara_park.json
        │   └── tsukigase.json
        └── yoshino
            └── mt_yoshino.json

  */
  // Put documents by using filepath.
  await gitDDB.put({ _id: 'nara/nara_park', flower: 'double cherry blossoms' });
  await gitDDB.put({ _id: 'nara/tsukigase', flower: 'Japanese apricot' });
  await gitDDB.put({ _id: 'yoshino/mt_yoshino', flower: 'cherry blossoms' });

  // Read
  const flowerInYoshino = await gitDDB.get('yoshino/mt_yoshino');
  console.log(flowerInYoshino); // flowerInYoshino = { flower: 'cherry blossoms', _id: 'yoshino/mt_yoshino' }

  // Prefix search
  
  // Read all the documents whose IDs start with the prefix.
  const flowersInNara = await gitDDB.allDocs({ prefix: 'nara/', include_docs: true });
  console.dir(flowersInNara, { depth: 3 });
  /* flowersInNara = 
  {
    total_rows: 2,
    commit_sha: 'xxxxx_commit_sha_of_your_head_commit_xxxxx',
    rows: [
      {
        id: 'nara/nara_park',
        file_sha: '7448ca2f7f79d6bb585421c6c29446acb97e4a8c',
        doc: { flower: 'double cherry blossoms', _id: 'nara/nara_park' }
      },
      {
        id: 'nara/tsukigase',
        file_sha: '1241d69c4e9cd7a27f592affce94ec60d3b2207c',
        doc: { flower: 'Japanese apricot', _id: 'nara/tsukigase' }
      }
    ]
  }
  */
 
  // destroy() closes DB and removes both the Git repository and the working directory if they exist.
  await gitDDB.destroy();

Collections

  // Try it again by another way.
  await gitDDB.create();
  // Use collections to make it easier
  const nara = gitDDB.collection('nara');
  const yoshino = gitDDB.collection('yoshino');
  await nara.put({ _id: 'nara_park', flower: 'double cherry blossoms' });
  await nara.put({ _id: 'tsukigase', flower: 'Japanese apricot' });
  await yoshino.put({ _id: 'mt_yoshino', flower: 'cherry blossoms' });

  // Read
  const flowerInYoshinoCollection = await yoshino.get('mt_yoshino');
  console.log(flowerInYoshinoCollection); // flowerInYoshinoCollection = { flower: 'cherry blossoms', _id: 'mt_yoshino' }

  // Read all the documents in nara collection
  const flowersInNaraCollection = await nara.allDocs({ include_docs: true });
  console.dir(flowersInNaraCollection, { depth: 3 });
  /* flowersInNaraCollection = 
  {
    total_rows: 2,
    commit_sha: 'xxxxx_commit_sha_of_your_head_commit_xxxxx',
    rows: [
      {
        id: 'nara_park',
        file_sha: '7448ca2f7f79d6bb585421c6c29446acb97e4a8c',
        doc: { flower: 'double cherry blossoms', _id: 'nara_park' }
      },
      {
        id: 'tsukigase',
        file_sha: '1241d69c4e9cd7a27f592affce94ec60d3b2207c',
        doc: { flower: 'Japanese apricot', _id: 'tsukigase' }
      }
    ]
  }
  */
  await gitDDB.close();

Examples:

See examples directory.

$ cd examples
$ npm i
$ npm start

Continuous Deployment (CD) using GitDocumentDB

https://github.com/sosuisen/sosuisen-my-inventory-gatsby

App using GitDocumentDB

https://github.com/sosuisen/inventory-manager

Roadmap

  • v0.1 Basic CRUD
  • v0.2 Collections
    • v0.2.8 Prefix search
  • v0.3 Synchronization with GitHub :feet:(Here now)
    • v0.3.1 OAuth
    • v0.3.2 Replication
    • v0.3.3 SSH key pair
  • v0.4 Grep
  • v0.5 Revisions
  • v0.6 Transaction
  • v0.7 Undo/Redo (Move between snapshots)
  • v0.8 Binary attachments

First official release

  • v1.0 Push server

Future releases

  • Indexed search
  • Migration
  • Plugins
  • Incremental indexing
0.4.8

1 year ago

0.4.9-alpha

12 months ago

0.4.9-alpha.2

12 months ago

0.4.8-alpha.3

1 year ago

0.4.8-alpha.1

2 years ago

0.4.7

2 years ago

0.4.7-beta.1

2 years ago

0.4.7-alpha.18

2 years ago

0.4.7-alpha.15

2 years ago

0.4.7-alpha.16

2 years ago

0.4.7-alpha.13

2 years ago

0.4.7-alpha.14

2 years ago

0.4.7-alpha.17

2 years ago

0.4.7-alpha.11

2 years ago

0.4.7-alpha.12

2 years ago

0.4.7-alpha.10

2 years ago

0.4.7-alpha.5

2 years ago

0.4.7-alpha.6

2 years ago

0.4.7-alpha.7

2 years ago

0.4.7-alpha.8

2 years ago

0.4.7-alpha.9

2 years ago

0.4.7-alpha.2

3 years ago

0.4.7-alpha.3

3 years ago

0.4.7-alpha.4

3 years ago

0.4.7-alpha.0

3 years ago

0.4.7-alpha.1

3 years ago

0.4.6-beta.0

3 years ago

0.4.6

3 years ago

0.4.5

3 years ago

0.4.5-beta.0

3 years ago

0.4.5-beta.1

3 years ago

0.4.5-beta.2

3 years ago

0.4.4

3 years ago

0.4.3-beta.0

3 years ago

0.4.3

3 years ago

0.4.2

3 years ago

0.4.1-beta.3

3 years ago

0.4.1

3 years ago

0.4.1-beta.2

3 years ago

0.4.1-beta.1

3 years ago

0.4.1-beta.0

3 years ago

0.4.0-beta.0

3 years ago

0.4.0

3 years ago

0.4.0-alpha.9

3 years ago

0.4.0-alpha.8

3 years ago

0.4.0-alpha.7

3 years ago

0.4.0-alpha.6

3 years ago

0.4.0-alpha.5

3 years ago

0.4.0-alpha.4

3 years ago

0.4.0-alpha.3

3 years ago

0.4.0-alpha.2

3 years ago

0.4.0-alpha.1

3 years ago

0.4.0-alpha.0

3 years ago

0.3.4-beta.1

3 years ago

0.3.3-beta.1

3 years ago

0.3.4-beta.0

3 years ago

0.3.3-beta.0

3 years ago

0.3.4

3 years ago

0.3.3

3 years ago

0.3.0-alpha.6

3 years ago

0.3.0

3 years ago

0.3.0-beta.0

3 years ago

0.3.0-alpha.2

3 years ago

0.3.0-alpha.3

3 years ago

0.3.0-alpha.4

3 years ago

0.3.0-alpha.5

3 years ago

0.3.2

3 years ago

0.3.1

3 years ago

0.3.0-alpha.1

3 years ago

0.3.0-alpha.0

3 years ago

0.2.7

3 years ago

0.2.9

3 years ago

0.2.8

3 years ago

0.2.6

3 years ago

0.2.5

3 years ago

0.2.3

3 years ago

0.2.4

3 years ago

0.2.0-alpha.7

3 years ago

0.2.1

3 years ago

0.2.0

3 years ago

0.2.2

3 years ago

0.2.0-alpha.6

3 years ago

0.2.0-alpha.5

3 years ago

0.2.0-alpha.4

3 years ago

0.2.0-alpha.3

3 years ago

0.2.0-alpha.2

3 years ago

0.2.0-alpha

3 years ago

0.1.16

3 years ago

0.1.15

3 years ago

0.1.14

3 years ago

0.1.13

3 years ago

0.1.12

3 years ago

0.1.11

3 years ago

0.1.10

3 years ago

0.1.8

3 years ago

0.1.7

3 years ago

0.1.9

3 years ago

0.1.6

3 years ago

0.1.5

3 years ago

0.1.4

3 years ago

0.1.3

3 years ago

0.1.2

3 years ago

0.1.1

3 years ago

0.1.0

3 years ago