Name

Description

SK.DB.Setup(
   session_id,
   structure,
   callback_ok,
   callback_failure
)

Constructor

This library is used only in administrator view. Applications use it mainly when they want to set up some database structure upon installation. However it can be used in any other administrator view moment. 

The way the library works is the developer supplies a "structure" defining the database you want to be created, the list of columns you'd like it to have and the tokens that you would use to work with that database. 

This information is passed in the "structure" which has the following format: 
{
   "database": { ...database meta information... },
   "columns": [ 
      { ...column1 meta information... },
      { ...column2 meta information... },
      ...
   ],
   "tokens": [
      { ...token1 meta information... },
      { ...token2 meta information... },
      ...
   ]
}


The meta information of the database, column, token is the same you pass to SK.DB.addDatabase, SK.DB.addColumn, SK.DB.addToken. Note that you don't have to pass the "db_id" in the addColumn and the "databases" in addToken. They will be automatically set after a database is set up. 

If the database with the name you provide already exists it won't be re-created. If the columns you want with the same name and type already exist, they won't be added. The same for tokens — if the tokens with the same names exist, they won't be added.

The callback_ok is invoked at the end of the operations and it receives again a "structure" hash with the same format as in the constructor, but this way it has the ID of the database, the IDs of the columns, the IDs of the tokens and the authentication token strings themselves as well as the database put in the "databases" property of the token meta information.

create( )

This method initiates the setup of the database.



Examples
Setting up a test database with a token to manage it

 

<script type="text/javascript">
   var MySetupDatabase = new Class({
      print_callback:
function () {}
      session_id: '',
      
      initialize: function( print_callback ) {
         this.session_id     = SK.Singletons.env.get( 'session_id' );
         this.print_callback = print_callback;
         
         new SK.DB.Setup(
            this.session_id,
            {
               // The database
               database: {
                  name: 'Test Database Setup',
                  description: 'Testing if the database setup works'
               },
               // The columns
               columns: [
                  {
                     'name': 'ID',
                     'type': SK.DB.Column.ID
                  },
                  {
                     'name': 'Name',
                     'type': SK.DB.Column.TEXT
                  },
                  {
                     'name': 'Qty',
                     'type': SK.DB.Column.NUMBER
                  }
               ],
               // The token
               tokens: [
                  {
                     name: 'Test Database Setup Permission',
                     extra: {
                        db_permissions: {
                           permissions: {
                              get: 1
                           }
                        }
                     }
                  }
               ]
            },
            this.ready.bind( this ),
            this.failure.bind( this )
         ).create( );
      },
      
      ready: function( structure ) {
         this.print_callback( JSON.encode( structure ) );
      },
      
      failure: function( message ) {
         alert( message );
      }
   });
   
   new MySetupDatabase( function( message ) {
      _$( 'mylog' ).grab( new Element( 'div', { html: message } ) );
   });
</script>
<div id="mylog" style="width: 500px;"></div>