Record Templates file descriptors
Tags: javascript, database, oip, ddx
Published: 2020-12-04 21:16:29

Record templates are the records in the DDX ecosystem that defines the schema of a particular table.

Following the Open Index Protocol (OIP), when we publish a record template, the protocol builds a file descriptor.

We can use the OIP public API to get a file descriptor of an existing record template.

## Organization template

The recommended DDX template for organizations, institutions, groups and etc resides on the FLO blockchain here:

[`05e5eea349e2d2788f835b3cd46843e08e5acf6664f764297fe806f7e9198b57`](http://network.flo.cash/tx/05e5eea349e2d2788f835b3cd46843e08e5acf6664f764297fe806f7e9198b57)

And we can use the OIP API to get the info already assembled and decoded here:

https://api.oip.io/oip/o5/template/get/05E5EEA3

> If you want to know more about the OIP API check [this documentation](https://github.com/oipwg/oip/blob/master/api.md)

One of the fields is the `file_descriptor_set`.

For the `organization` template the `file_descriptor_set` is:
```
CncKB3AucHJvdG8SEm9pcFByb3RvLnRlbXBsYXRlcyJQCgFQEhAKCGxvY2F0aW9uGAEgASgJEiQKFnBhcmVudE9yZ2FuaXphdGlvbkxpc3QYAiADKAsyBFR4aWQaEwoEVHhpZBILCgNyYXcYASABKAxiBnByb3RvMw
```

How do we get the info about the template from here?

The hard answer is a combination of protobuf and base64 decoding, but the easy answer is to use the package:

[`oip-protobufjs`](https://www.npmjs.com/package/oip-protobufjs)

## Using `oip-protobufjs` to read a file descriptor

Let's assume we already have `node` and `npm` installed.

First, we create a project

```bash
mkdir oip-read-template
cd oip-read-template
npm init
```

Now let's install the packages:

```bash
npm install @babel/runtime oip-protobufjs
```

Let's make a file called `app.js`
```bash
touch app.js
```

The contents of app.js should be:
```javascript
const { decodeDescriptor } = require('oip-protobufjs/lib/builders')
const descriptor = "CncKB3AucHJvdG8SEm9pcFByb3RvLnRlbXBsYXRlcyJQCgFQEhAKCGxvY2F0aW9uGAEgASgJEiQKFnBhcmVudE9yZ2FuaXphdGlvbkxpc3QYAiADKAsyBFR4aWQaEwoEVHhpZBILCgNyYXcYASABKAxiBnByb3RvMw"
const info = decodeDescriptor(descriptor)
console.log(info)
```

That's it, 4 lines. Save it and run.

```bash
node app.js
```

If everything went ok, it should have shown a bunch of info regarding the organization template, for example its fields, the data type that is supposed to be in each field and etc.


## Final considerations

We can use the OIP API and the `oip-protobufjs` to get information on the fly about how to read and write records of any template in the DDX system.

We could run an instance of the FLO blockchain and the OIP daemon locally and we would have full interrupted access to the data in the DDX system, as long as we have an Internet connection, completely independent from anyone.

Also, the template `05E5EEA3` for _organizations_ is a recommended template. Nobody is obligated to use it and anyone can build another one if they wish or use this one. Another option is to extend this template which will be the subject of another post where we cover the concept of **Hierarchical Templates**.


Stay tuned for the next posts!
☀️ + ⚜️ = 🚀🚀🚀