Svelte integration RFC #276
Replies: 7 comments 5 replies
-
|
Okay, firstly, this is fantastic to see proposed. The spec and example code are clean, easy to understand, but I would stick to Svelte 5 rather than 4 unless you find it's easy enough to support both. Everything I do now is in Svelte 5 and TypeScript with the Bun runtime, so I am particularly interested. Building a community web portal that updates the UI in real-time right now via PostgreSQL's LISTEN/NOTIFY for backed stuff and Pocketbase for client-specific. Not really ideal. Site note: I am aiming to begin shipping all my SvelteKit applications as a compiled binary. Some dependencies break this ability, so I would like to also see if the proposed RFC if implemented, would work in this scenario as well. Not a deal break, but it would be nice. |
Beta Was this translation helpful? Give feedback.
-
|
OK so here's how I assume we could update this proposal to follow more of a rune-based pattern for Svelte v5. Instead of store factories, we'd provide hooks that return reactive values directly: // Data access hooks
export function useCell(tableId, rowId, cellId, store);
export function useRow(tableId, rowId, store);
export function useTable(tableId, store);
export function useValue(valueId, store);
// Two-way bindable hooks
export function useCellBindable(tableId, rowId, cellId, store);
export function useValueBindable(valueId, store);Which you could then use like: <script>
import { useCell, useCellBindable } from 'tinybase/ui-svelte';
// Read-only reactive value (no $ prefix needed!)
let species = useCell('pets', 'fido', 'species', store);
// Two-way bindable
let color = useCellBindable('pets', 'fido', 'color', store);
// Derived values with $derived
let display = $derived(`${species} (${color})`);
</script>
<div>{species}</div>
<input bind:value={color} />Am I thinking about that right? One question that I would love the community to guide me on is... would we also need to provide v4 support? Or is everyone moving this way now? |
Beta Was this translation helpful? Give feedback.
-
|
my two cents: no need to provide Svelte 4 support. is tinybase normally a client or server/SSR package? sync/async? depending on the answers to those questions it could mean that you mat want to look into the new async svelte API |
Beta Was this translation helpful? Give feedback.
-
|
Have you considered a class based implementation like what ai-sdk had done with their Svelte module. |
Beta Was this translation helpful? Give feedback.
-
|
Great feedback everyone - very helpful! Let's get this show on the road... |
Beta Was this translation helpful? Give feedback.
-
|
I was trying to use tinybase for a little project of mine earlier this year and i discovered this repository by @polvallverdu. I extended it a bit to include other hooks which I needed. I was able to work with an API which works somewhat like this <script>
import { useLocalRowIds, useRow, useRowIds } from '$lib/db/hooks.svelte';
import { store, relationships } from '$lib/db/store.svelte';
let feeds = useRowIds(store, 'rss-feeds');
</script>
<div>
{#each feeds.value as feedId (feedId)}
{@const row = useRow(store, 'rss-feeds', feedId)}
{@const entries = useLocalRowIds(relationships, 'rss-entry-to-rss-feed', feedId)}
<Tile url="/feed/{row.value.slug}">
<p>
{#if row.value.favicon}
<img src={row.value.favicon} alt="favicon for {row.value.title}" />
{/if}{row.value.title}
</p>
<p><Link href={row.value.url!}>{row.value.url}</Link></p>
<p>{row.value.feed}</p>
<p>Entries: {entries.value.length}</p>
{#if row.value.lastCheckedAt}
<Time time={row.value.lastCheckedAt} relative />
{:else}
<p>never updated...</p>
{/if}
</Tile>
{/each}
</div>and I declared the store and relationships like so import { createStore, createRelationships } from 'tinybase/with-schemas';
import * as s from './schema';
export const store = createStore().setTablesSchema(s.tables);
export const relationshipIds = {
ENTRY_TO_COLLECTION: 'entry-to-collections',
RSS_ENTRY_TO_RSS_FEED: 'rss-entry-to-rss-feed',
} as const;
export type RelationshipIds = typeof relationshipIds;
export const relationships = createRelationships(store)
.setRelationshipDefinition(
relationshipIds.ENTRY_TO_COLLECTION,
'entries',
'collections',
'inCollection'
)
.setRelationshipDefinition(
relationshipIds.RSS_ENTRY_TO_RSS_FEED,
'rss-feed-entries',
'rss-feeds',
'forRss'
);declaring the relationship Ids with that readonly object was basically just a hack to get autocomplete for the hooks cause i wasn't able to figure out how to get those id from the relationship store itself. |
Beta Was this translation helpful? Give feedback.
-
|
I started some work on this in polvallverdu/svelte-tinybase#4, just for svelte 5 |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Hello everyone!
I'm proposing a new
ui-sveltemodule for TinyBase that would provide first-class Svelte integration, similar to how the existingui-reactmodule provides React integration. This would enable Svelte developers to build reactive, local-first applications with TinyBase using idiomatic Svelte patterns.Motivation
TinyBase currently has comprehensive React support through the
ui-reactmodule, which provides:However, Svelte is growing rapidly as a framework, particularly in the local-first and lightweight application space where TinyBase likes to live. Svelte's compile-time reactivity model and minimal runtime overhead make it a natural fit for TinyBase's philosophy of being tiny, fast, and developer-friendly.
Currently, Svelte developers can use TinyBase directly, but I suspect they miss out on the ergonomic, framework-specific bindings that make development smoother and more idiomatic.
Proposed Solution
I'd like to create a
ui-sveltemodule that adapts TinyBase's reactive capabilities to Svelte's reactivity model. The module would provide three main categories of functionality:1. Reactive Stores
Instead of React hooks, we'd provide factory functions that return Svelte readable/writable stores. These would automatically subscribe to TinyBase changes and clean up listeners on unsubscription.
Example - basic data access:
Usage in Svelte:
Example - more advanced features:
2. Writable Stores for Mutations
For two-way data binding (leveraging Svelte's
bind:directive):Usage:
3. Svelte Components
Pre-built components for declarative rendering:
Component hierarchy:
CellView,RowView,TableView,TablesViewValueView,ValuesViewResultCellView,ResultRowView,ResultTableViewSliceView,IndexViewLinkedRowsView,LocalRowsView,RemoteRowViewMetricViewCheckpointView,BackwardCheckpointsView,ForwardCheckpointsView4. Context Provider
Svelte's context API for dependency injection:
Usage:
Then in child components:
5. Action Creators
For callback-based mutations:
Usage:
6. Store Creation Utilities
Memoized store creation (similar to
useCreateStorein React):Advantages
Hopefully these things would give you some advantages over direct TinyBase usage in Svelte:
$reactive references andbind:directivesui-reactComparison: ui-react vs ui-svelte
useCell,useRow)cell,row).sveltecomponentsbind:directiveImplementation goals
TypeScript Support
I would hope we can provide full type definitions with:
Testing
Gotta keep the 100% test coverage record up! So...
Documentation
ui-reactOpen questions for you all
tinybaseCell) to avoid conflicts?ui-react-dom)?ui-svelte(core) andui-svelte-components(likeui-react-dom)?Community feedback requested!
This is a community RFC - I'm interested in feedback before committing to implementation. I'd love to hear from the community, especially:
Some specific questions:
Conclusion
A
ui-sveltemodule would bring TinyBase's powerful reactive data management to the Svelte ecosystem with idiomatic, ergonomic bindings. It would reduce boilerplate, improve developer experience, and leverage Svelte's compile-time reactivity for optimal performance.I believe this would be a valuable addition to TinyBase and would help grow adoption in the Svelte community, which aligns well with TinyBase's tiny and, er, svelte philosophies!
Looking forward to your thoughts and feedback.
Beta Was this translation helpful? Give feedback.
All reactions