# StrongVault SDK
This SDK was created to utilize the StrongVault network to store and search documents.

 ***Limitations***: The current implementation is designed to work in a Node.js environment and will not work in the browser.


# File Search
Current supported file formats that can be added to the search index are: png, jpg, pdf, txt, json, xlsx and docx.

# Docs

## Install
 npm install @strongvault/sdk

## Generating a mnemonic phrase
A mnemonic phrase needs to be generated and saved in order to access encrypted data. If the phrase is lost, the data cannot
be recovered. This phrase can be generated by using the SDK or anything that follows the [bip39 spec](https://github.com/bitcoin/bips/blob/master/bip-0039.mediawiki)

	  const { generateMnemonicPhrase } = require('@strongvault/sdk');
	  console.log('mnemonic: ', generateMnemonicPhrase());

## Initialize

Initialization will ensure the account is created for the mnemonic given. If one doesn't exist, it will be created.

	const sv = require('@strongvault/sdk');

	await sv.initialize({
        mnemonic: 'your mnemonic phrase',
        stellarHorizonUrl: 'https://horizon1.staging.electra.strongvault.app',
        stellarUrl: 'https://node1.staging.electra.strongvault.app',
        stellarNetworkPassphrase: 'Strongvault Staging Network Electra May 2019',
        
        // Search Options (optional)
        searchFilePath: 'directory where the search related files will be written to',
        searchIndexFilename: 'unique filename where the search index will be written to',
        searchKeyFilename: 'unique filename where the search key will be written to',
        customIndexFields: [ 'custom_index_field', 'custom_index_field' ],
        dynamicAddIndexFields: boolean, // Add json keys dynamically as indexed fields to the search index when parsing the json document
	});

Default Search Option Values
- searchFilePath: use current directory if blank
- searchIndexFilename: use 'search_index_<account_id>' if blank
- searchKeyFilename: use '.search_keys_<account_id>' if blank
- customIndexFields: empty array
- dynamicAddIndexFields: false

## Sync
The SDK is stateful, works offline and will need to be synced after actions that affect files. The sync sends the proper changes to the network records the transactions on the blockchain.
	await sv.sync()

## Import a File
	 await sv.importFile(filename, filePath)
 When a document is uploaded, it is automatically added to the search index.

## List Current Files
	sv.listDocs
	//[{id: 'dsfoid8sfnfkdsj389d8dn', name: 'my_document', ...}, ...]

## Retrieve a File
	 await sv.retrieveDocument(document, destinationPath);

## Delete files
	 await sv.deleteDocuments(docs);

## Search
There are 4 predefined fields that are used in the search index. When documents such as PDF, Word and Excel files are uploaded, the text is extracted from the file and added to the index under the field 'ssText'.

 - ssUuid: an unique identifier
 - ssFileUuid: the uuid created when a file is uploaded
 - ssFileName: the file name
 - ssText: text extracted from documents

###### Initialize

	  sv.searchApi.init(keys, options);

###### General Search

	  const params = {query: '<word to query>'};
	  sv.runSearch(params);

###### Field Search

	  const params = {query: '<word to query>', field: '<indexed field>'};
	  sv.runFieldSearch(params);

###### Add Documents

	  await sv.searchApi.addDocs(docs);

###### Get Document

	  await sv.searchApi.getDoc(docId);

###### Delete Documents

	  await sv.searchApi.removeDocs(docIds);

###### Update Documents

	  await sv.searchApi.updateDocs(docs);
