Skip to content

Data source schema & providers#6633

Merged
artf merged 36 commits intodevfrom
data-source-schema
Oct 28, 2025
Merged

Data source schema & providers#6633
artf merged 36 commits intodevfrom
data-source-schema

Conversation

@artf
Copy link
Member

@artf artf commented Oct 27, 2025

Schema support with relations

Added schema support to DataSource with field type definitions and relation capabilities:

const dsm = editor.DataSources;

dsm.add({
  id: 'categories',
  records: [
    { id: 'cat1', uid: 'cat1-uid', name: 'Category 1' },
    { id: 'cat2', uid: 'cat2-uid', name: 'Category 2' },
    { id: 'cat3', uid: 'cat3-uid', name: 'Category 3' },
  ],
});

dsm.add({
 id: 'users',
 records: [
    { id: 'user1', username: 'user_one' },
    { id: 'user2', username: 'user_two' },
    { id: 'user3', username: 'user_three' },
  ],
});

const blogsDS  = dsm.add({
  id: 'blogs',
  records: [
    { id: 'blog1', title: 'First Blog', author: 'user1', categories: ['cat1-uid'] },
    { id: 'blog2', title: 'Second Blog', author: 'user2' },
    { id: 'blog3', title: 'Third Blog', categories: ['cat1-uid', 'cat3-uid'] },
  ],
  schema: {
    author: { type: DataFieldPrimitiveType.relation, target: 'users' },
    categories: { 
      type: DataFieldPrimitiveType.relation, 
      target: 'categories',  // Target data source
      targetField: 'uid',      // Field to match (defaults to 'id')
      isMany: true,          // One-to-many relation
    },
  },
});

// Get resolved records with populated relations
const blogs = blogsDS.getResolvedRecords();
// Result:
[
  { 
    id: 'blog1',
    title: 'First Blog',
    author: { id: 'user1', name: 'user_one' },
    categories: [
      { id: 'cat1', uid: 'cat1-uid', name: 'Category 1' }    
    ]
  },
 // ...
]

Provider Support

Added provider support for external data

dsm.add({
  id: 'blogs',
  name: 'My blogs',
  provider: { get:'https://api.example.com/data' },
});

// The provider API is expected to receive this JSON (schema is optional)
{
  records: [
    { id: 'blog1', title: 'Blog post 1', ... }
  ],
  schema: {
    author: {
      type: 'relation',
      target: 'users'
    }
  }
}

When the provider is set on the data source, the records won't be exported in the project JSON but will be loaded on project JSON load. This is an ideal case for managing a lot of records without storing them directly in the project.

@artf artf marked this pull request as ready for review October 28, 2025 08:58
@artf artf merged commit 83bb01b into dev Oct 28, 2025
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant