Introduction

The Vircadia inventory app has been created to work without the need of a database server. Instead it loads a database file into memory and uses a javascript database application called pouchDB. pouchDB pulls the database from the Bazaar/inventoryDB folder. This script is used to help pouchDB create the database file. In order for the database to be up to date, you must run this script each time the repo is changed. You will download the DB file and upload it to the inventoryDB folder in the repository.

Permissions

You must ensure that this script has read access to all the Bazaar folders and files on the internet. The script does not work against local copies. If your distribution is on an Amazon S3 bucket you can make the entire bucket readonly to the public.

1) Open your bucket in the AWS S3 interface.
2) Click on the Permissions tab at the top.
3) Click on the Bucket Policy button.
4) Enter the following changing #bucket# with the name of your bucket.
{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "PublicReadGetObject",
            "Effect": "Allow",
            "Principal": "*",
            "Action": "s3:GetObject",
            "Resource": "arn:aws:s3:::#bucket#/*"
        }
    ]
}


5) Don't forget to click the Save button.

How it works

The script searches recursively for an index.json. The index.json tells the script what folders exist in each subfolder provided. When the app does not find an index.json in a folder it looks for a resource.json file. The resource.json file tells the app what kind of resource is in this folder and provides details for accessing it. Categories and subcategories are created from the index.json files. These categories are populated with resources based upon what is provided in the resource.json files. All this information is written to an in memory database. It's this database that we will export and upload to your file repository.

The database holds two types of records, folder and resource. The fields are as below:

var folders = {
  "_id": "bazaar",
  "r_type": "folder";
  "name": "Bazaar",
  "parent": "repo",
  "type": "index",
  "path": "https://"
};

var resource = {
  "_id": "kim",
  "r_type": "resource";
  "name": "Kim",
  "parent": "Avatars",
  "type": "avatar",
  "path": "https://",
  "tags": ["Female","Avatar"]
};

Understanding Errors

Watch the results of this script in the browser console. If no index.json or resource.json is present you will see GET errors in the console. This is to be expected. What you do need to look for in your console are JSON errors. If you get a JSON parse error you will need to run your json file in a json validator or lint to determine why... Failure to provide properly formed json will result in inaccurate or incomplete data in the inventory app. If you have to correct your JSON, just re-run the download script afterwards and update your repository with the new GZ file. Uncaught (in promise) errors occur when you are trying to load a record that already exists. You will need to flush your cache before you reset this page. This will wipe the database and you will be loading it freshly.

Download the Database

Click on the button provided and the script will recurse the repository you provide it. Note that this only works if you have followed the Vircadia pattern of placing an index.json to define the folders and the package.json to define the assets. Only valid JSON will do. It will generate a database and export the database in a gzip file. You will need to upload this file into the inventoryDB folder.

Enter the path to your repository and press the Download button... It may take a few minutes before the download begins. Do not click on the button more than once unless you want to download the export several times.

Do not close this window before the export occurs!


example: https://yourbucket.s3-us-west-1.amazonaws.com/repo/Bazaar

   

Place the gzip database file in the Bazaar/inventoryDB folder. You can use the link below to test the database once you have uploaded it to your repository.

Make sure you update the $repo variable in the demo.html file before you test it... otherwise it may fail or you will not be looking at your repo but mine... which may go away.

Demo Page


If you have any questions about this repository or scripts feel free to ask in the Vircadia Discord channel.

References

PouchDB
pouchdb-quick-search
pouchdb-find
pako