How to initialize data structure for your new Skygear app


#1

How can we initialize data structure for a new project?

When Skygear saves your records at the cloudDB, it automatically handles the DB schema. Skygear updates the schema behind the scene on the fly upon record save requests.

Quick way to save a record with JavaScript

You can save a record via JS SDK:
For example, we want to set up a Note record with title, content, and rate fields.

<!--Skygear CDN-->
<script src="https://code.skygear.io/js/polyfill/latest/polyfill.min.js"></script>
<script src="https://code.skygear.io/js/skygear/latest/skygear.min.js"></script>

<script>
var msg = document.getElementById('msg');


skygear.config({
  'endPoint': 'https://testapi20180109.skygeario.com/', // trailing slash is required
  'apiKey': '4bb9cb48bab7439c97f6d033204841fc', 
}).then(() => {
  console.log('skygear container is now ready for making API calls.');
  
  skygear.auth.signupAnonymously().then(()=>{

    var Note = skygear.Record.extend("Note");
    note = new Note({    // Save a template record here
    'title': 'Title', // Define custom fields
    'content': 'Hello World',
    'rate': 1
  });
  skygear.publicDB.save(note).then((result)=>{
    console.log('ok');
    msg.innerText = msg.innerText+' Saved note as record (ID: '+result._id+')\n';
    
  });
  })

}, (error) => {
  console.error(error);
});

</script>
<span id="msg"><span>

Live example: https://codepen.io/david90/pen/BJYyYb

Database data importer

Furthermore, to intialize a batch of data, you can also import data from CSV files with this helper script.

Example:

CSV

title,content,rate
Title 1, Good morning!, 1
Title 2, Good night, 3

Then run the following script to import existing notes to as Note record:

skygear-data-importer -e 'https://demoapp.skygeario.com' -k 'APIKEY_XXXXXXXXXXX' -t 'Note' 'note_records.csv'

Freezing the database schema

When you launch your app publicly, you may want to freeze the database schema to prevent unexpected changes to your cloudDB. You can update the settings at Skygear Portal > Database > Settings > Database Schema Freezing

If you have further questions. Please ask below and let us know :wink: