Schema Access Methods

The Knack object provides comprehensive methods to access your application's schema and structure programmatically.

⏲️

COMING SOON

What You'll Learn

You'll discover how to retrieve information about your app's pages, tables, fields, views, and users using promise-based methods. This enables dynamic applications that can adapt based on your app's current structure and user context.

👍

Every JavaScript customization must begin with the Knack.ready() method. This ensures the Knack object is fully initialized and the React application has finished mounting before your custom code executes.


Knack.ready().then(async () => {
  // All your custom code goes here
  console.log('Knack is ready for customization!');
});

Page Methods

Access information about the pages in your application:

Knack.ready().then(async () => {
  // Get all pages in your application
  const pages = await Knack.getPages();
  console.log('All pages:', pages);

  // Get a specific page by its key
  const page = await Knack.getPage('scene_123');
  console.log('Specific page:', page);
});

Table and Object Methods

Retrieve information about your data structure:

Knack.ready().then(async () => {
  // Get all tables (objects) in your application
  const tables = await Knack.getTables();
  console.log('All tables:', tables);

  // Get a specific table by its key
  const table = await Knack.getTable('object_1');
  console.log('Specific table:', table);

  // Get all fields for a specific table
  const fields = await Knack.getFields('object_1');
  console.log('Table fields:', fields);

  // Get a specific field
  const field = await Knack.getField('field_1', 'object_1');
  console.log('Specific field:', field);
});
👍

Tip: Field and object keys typically look like field_1, object_1. Use the actual keys from your app

View Methods

Access information about views on specific pages:

Knack.ready().then(async () => {
  // Get all views for a specific page
  const views = await Knack.getViews('scene_123');
  console.log('Page views:', views);

  // Get a specific view
  const view = await Knack.getView('view_456', 'scene_123');
  console.log('Specific view:', view);
});

User and Authentication Methods

Get information about the current user and their permissions:

Knack.ready().then(async () => {
  // Get current user information
  const user = await Knack.getUser();

  if (user) {
    console.log('Current user:', user);
    console.log('User email:', user.email);
    console.log('User name:', user.name);
  } else {
    console.log('No user is currently logged in');
  }
});

interface SchemaUser {
  id: string;
  email: string;
  name: string;
  status: string;
  profileKeys: KnackObjectProfileKey[];
  profileObjects: string[];
}


});
  // Check if the current user has a specific profile/role key
  try {
    const hasProfile = await Knack.hasProfileKey?.('profile_1'); // e.g., 'profile_1'
    if (typeof hasProfile === 'boolean') {
      console.log('Has profile_1?', hasProfile);
    }
  } catch (e) {
    // Swallow if not supported
  }
});

Application Details

Retrieve metadata about your application:

Knack.ready().then(async () => {
  // Get application metadata
  const appDetails = await Knack.getApplicationDetails();
  console.log('Application details:', appDetails);

SchemaApplicationDetails {
 id: string;
 name: string;
 slug: string;
 description: string;
 settings: {
   users: {
     registration_type: string;
     invitation_required: boolean;
     self_registration: boolean;
   };
   region: string;
   timezone: string;
   language: string;
 };
}

Best Practices

Error Handling

Wrap API calls in try-catch blocks:

Knack.ready().then(async () => {
  try {
    const user = await Knack.getUser();
    const roles = user?.roles || [];
    console.log('User loaded:', user?.email, roles);
  } catch (error) {
    console.error('Failed to load user data:', error);
  }
});

Caching Results

The Knack object automatically caches results, but you can store frequently accessed data:

Knack.ready().then(async () => {
  let cachedTables;

  async function getTablesCached() {
    if (!cachedTables) {
      cachedTables = await Knack.getTables();
    }
    return cachedTables;
  }

  // Usage
  const tables = await getTablesCached();
  console.log('Cached tables:', tables);
});