Skip to content

Commit

Permalink
adds datamodel export example (#2)
Browse files Browse the repository at this point in the history
Co-authored-by: Moti Granovsky <[email protected]>
  • Loading branch information
motigra and Moti Granovsky authored Mar 30, 2020
1 parent 59ee85c commit 0393bab
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
# Settings file (contains API token)
shared.js

# Exported models folder
/backup/

# Dependency directories
node_modules

Expand Down
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,26 @@ node change-connection.js
1. Updates the table's `id` property according to new CSV file name
1. Builds the datamodel

### Demo #3: Exporting Datamodel Schemas

**Available on: Sisense Linux L8.0.3.150 or later**

This demo script downloads all available Datamodel schemas, without data, as `.smodel` files, which can be imported to Sisense via UI or API, into a folder called `backup`.

**To Run:**

```
node export-all.js
```

**What it does**

1. Creates a folder called `backup` if one doesn't already exist
1. Gets the `OID` and `title` of all available Datamodel entities
1. For each Datamodel found:
1. Exports the Datamodel
1. Stores the response JSON as `.smodel`

## Extending this demo

This demo uses Node.js with a minimal set of dependencies, listed below.
Expand Down Expand Up @@ -118,6 +138,7 @@ Additionally, this project lists `eslint` and several plugins for it as a DevDep
├── upload.js --> Shared library for uploading CSV/XLSX files to Sisense
├── demo.js --> Demo #1 - Create datamodel from scratch
├── change-connection.js --> Demo #2 - Change connection of a dataset
├── export-all.js --> Demo #3 - Export all Datamodel schemas
└── assets/
├── demo.csv --> Sample data
├── demo2.csv --> Sample data
Expand Down
82 changes: 82 additions & 0 deletions export-all.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/**
* Sisense Datamodels API Demo - #3 Export All Schemas
* Written by Moti Granovsky, Sisense DevX, March 2020
*
* Exports all existing Datamodel schemas as .smodel files
*
* **Available on: Sisense Linux L8.0.3.150 or later**
*/

const fs = require('fs');
const { token, baseUrl } = require('./shared.js');
const client = new (require('./client.js'))(token, baseUrl);

const TARGET_FOLDER = './backup';

/**
* Main script flow
*/
async function main() {

/**
* Step 1: Create a folder to export to
*/

console.log(`01. Create folder ${TARGET_FOLDER} if it didn't exist: starting`);

if (!fs.existsSync(TARGET_FOLDER)) {
fs.mkdirSync(TARGET_FOLDER);
}

console.log(`01. Create folder ${TARGET_FOLDER} if it didn't exist: done`);

/**
* Step 2: Get a list of all datamodel OIDs and titles
*/

console.log('02. Getting all datamodels: starting');

const datamodels = await client.get('datamodels/schema', { fields: 'oid, title' });

console.log(`02. Getting all datamodels: done | found ${datamodels.length} models`);

/**
* Step 3: Iterate over all models and export each one
*/

console.log('03. Exporting all datamodels: starting');

for (const item of datamodels) {
await exportAndSave(item);
}

console.log('03. Exporting all datamodels: done');

return true;
}

/**
* Exports a datamodel using the API and saves it to the target folder
* @param {object} datamodel Object containing a datamodel `oid` and `title`
*/
async function exportAndSave(datamodel) {

console.log(`--> Exporting datamodel "${datamodel.title}"`);

const schema = await client.get(`datamodel-exports/schema`, { datamodelId: datamodel.oid, type: 'schema-latest' });

console.log(` > Saving datamodel "${datamodel.title}" as "${TARGET_FOLDER}/${datamodel.title}.smodel"`);

fs.writeFileSync(`${TARGET_FOLDER}/${datamodel.title}.smodel`, JSON.stringify(schema), { encoding: 'utf8' });

console.log(` > Datamodel "${datamodel.title}" downloaded`);

return true;
}

process.on('unhandledRejection', (err) => {
console.error(err);
process.exit(1);
});

main().then(console.log).catch(console.error);

0 comments on commit 0393bab

Please sign in to comment.