Coder Social home page Coder Social logo

bs-holochain's Introduction

Holochain API bindings via Bucklescript. This library allows you to write a holochain application in pure Ocaml/Reason.

Dependencies

This project requires npm and bucklescript 4.0.1. To produce a real holochain app you also need to unify the json into one big zome file. Rollup and babel are typical choices.

Build

make

Install as a library

In your dependent project run

npm install <this-project-directory>

How to use

Define your zomes as usual

Create a dna.json and schema files as you would for any standard holochain app. See their DNA reference documentation for further details on this step.

Implement your zomes

For each zome you define a module that instantiates a builder style functor to accumulate all the entry definitions.

(** Zome example *)
open Hc

(* Instantiate a new zome builder with name equal to "myzome" *)
module Builder = Zome.Builder(struct let name = "myzome" end)

(* Define a named data type "mydata" as an abstract json record.
   Your dna.json would require a zome entry defined as well with the same name
   and shape.
*)
module MyData = struct
  let name = "mydata"
  type t = {data:string} [@@bs.deriving abstract]
end

(* Add the MyData entry type to the zome with validation functions
   that simply accepts any input (eg. returns true or Null,
   as the callback would require).

   The return moduled is an [Entry.S] signature, giving the developer
   specialized commit, update, and get functions over the MyData entry.
*)
module MyDataEntry = Builder.Entry0(MyData)(Validate.Accept_all(MyData))


(** Define input type to a zome function as a zome hash string of [MyData.t] *)
type args = MyData.t HashString.t

(* Define a public zome function (this was defined in dna.json as
   function named "dataExists")
*)
let dataExists args =
  match MyDataEntry.get args with None -> false | Some _ -> true


(* Define a public zome function to add data to the DHT
  (this was defined in dna.json as function named "addData")
*)
let addData data =
  MyDataEntry.commit (MyData.t ~data)

(* This finalizes the zome and includes the validation functions so they now serve
   as javascript call back functions.

   Since a zome requires a genesis function and possibly a receiver function, so 
   does the below functor. In this example builtin modules are used for both. The
   [Genesis.Success] module just returns true and does no initialization. Similarly,
   the send and receive functions operate on the unit value.
*)
include Builder.Build(Genesis.Success)(SendReceive.Unit)

Examples

See NewCraigsList, ported to OCaml for a more complex implementation.

Notes

This library binds to the javascript api running on a Go server via Go's Otto javascript interpreter. The holochain team is porting their stack from Go to Rust/Web Assembly.

Thus, this binding is subject to drastic change in the future. My hope is that the OCaml/Reason abstraction boundary will ease migration to the new javascript api (or web assembly directly).

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.