Using KiiApplication (v3 series only)

A new feature KiiApplication is introduced in Kii Cloud SDK for JavaScript v3 series.

This feature allows you to use the following functions.

Using multiple apps simultaneously

You can handle multiple apps simultaneously with the SDK. In the following example, a KiiObject is copied from a usApp app to a jpApp app.

// Assume myBucket and object xxxx is already registered.
const usApp = Kii.initializeWithSite("my-app-id1", "my-app-key1", KiiSite.US);
// If no app is injected, the app in which Kii.initializeWithSite() will be used.
// i.e. In this case, usApp will be used.
const bucketUS = Kii.bucketWithName("myBucket");
KiiObject.objectWithURI("kiicloud://buckets/myBucket/objects/xxxx").refresh().then((obj1) => {
  // create a separate app
  const jpApp = new KiiApplication("my-app-id2", "my-app-key2", KiiSite.JP);
  // Assume a myBucket and object yyyy is already registered.
  const bucketJP = jpApp.bucketWithName("myBucket");
  // Inject the jpApp while creating a object, so that this object will be created on jpApp.
  const obj2 = KiiObject.objectWithURI("kiicloud://buckets/myBucket/objects/yyyy", jpApp);
  obj1.getKeys().forEach((key) => {
    obj2.set(key, obj1.get(key));
  });
  // authenticate in jpApp to write the object
  return usApp.authenticate("myusername", "mypassword", jpApp).then(() => {
    return obj2.save();
  });
});

Kii.initializeWithSite in line 1 initializes Kii for KiiSite.US. The KiiApplication instance created by this initialization will be applied as default. Therefore, Kii.bucketWithName in line 2 and KiiObject.objectWithURI in line 3 will be applied to the KiiSite.US application specified in line 1.

You can generate a KiiApplication instance different from the default one using KiiApplication. In line 4, we use KiiApplication to create an instance for the KiiSite.JP app. Since bucketWithName and KiiObject.objectWithURI in line 6 are executed with this instance, each operation is applied to the app in KiiSite.JP.

Supporting multiple user login at the same time

The SDK can support multiple user logins at the same time. In the following example, a regular user account and an app admin account are used separately.

const userApp = Kii.initializeWithSite("my-app-id", "my-app-key", KiiSite.US);
// authenticate as a normal user. Now the userApp & the default app works as a user context.
KiiUser.authenticate("myusername", "mypassword").then((user) => {
  // get a list of topics that the user can subscribe.
  return userApp.listTopics().then((userTopics) => {
    console.log(userTopics);
  });
});

// login as an app admin. This does not affect the user / userApp above.
Kii.authenticateAsAppAdmin("your client id", "your client secret").then((adminContext) => {
  // this app has an admin context.
  const adminApp = adminContext.getApp();
  // get a full list of topics.
  return adminApp.listTopics().then((allTopics) => {
    console.log(allTopics);
  });
});

Kii.initializeWithSite in line 1 initializes Kii for KiiSite.US. The KiiApplication instance created by this initialization is applied as default. The login process of myusername by KiiUser.authenticate in line 2 and the process of listTopics in line 3 are executed in this instance. As a result, a list of topics to which the user can subscribe is returned in line 3.

Kii.authenticateAsAppAdmin in line 5 creates the context for the app admin, and getApp() in line 6 extracts the KiiApplication instance from this context. listTopics in line 7 will be executed in this instance, so it will returns the list of topics to which the app manager can subscribe, meaning the list of all topics in existence.