@evidence-dev/sdk

中文文档
4.0.2发布于 2026/02/07MITTypeScriptESM + CJS近 30 天下载 9.4万
evidence-logo
原始使用文档
文档目录6 节
  1. How it works
  2. Installation
  3. Usage
  4. Using <code/>
  5. Using runQuery
  6. Enabling SSR
evidence-logo

Evidence SDK

The fastest way to build highly interactive data apps

GitHub Repo stars NPM NPM Version (with dist tag) Join Slack

This documentation has not been updated for the latest versions of the SDK

How it works

The Evidence SDK contains all the tools needed to build interactive data apps, all the way from the database to the browser.

  • Datasource Connectors - Plugins to connect to a variety of datasoures, including Databases, APIs, Flat files, and more
  • Run queries in the browser - Universal SQL is the fastest way to use DuckDB WASM with your databases

Installation

  1. (If needed), create your SvelteKit project (e.g. npm create svelte)
  2. Install the SDK npm i @evidence-dev/sdk@preview
  3. Add the SDK to your project using npx evidence-sdk add
  4. Use npx evidence-sdk plugins install if you did not install a plugin during add
    5. If the data source you want to use is not listed, and there is an Evidence plugin for it, you can install the package and add it to evidence.config.yaml manually.
  5. Create a new connection with npx evidence-sdk connections edit
    1. Once the connection is created, there will be a new sources/[connection] directory, for most connectors, you will need to create some source queries.
  6. Populate the data with npx evidence-sdk sources

Usage

See the wiki

There are 2 methods for executing queries using the Evidence SDK; using <code/> elements, and calling runQuery directly. <code/> elements automatically make their results available to child components (or pages, if used in a layout), while runQuery keeps the query scoped to the current page.

Using <code/>

Writing Queries

To add a query to your page, use a <code/> element with an evidence-query-name attribute (lang=sql is optional but recommended).

Wrapping this in a <pre> tag will prevent formatters from changing your SQL.

Example:

<pre><code evidence-query-name="myFirstQuery" lang="sql">
SELECT * FROM my_first_table
</code></pre>

When using <code/>, you can interpolate variables from the page using template string syntax, Svelte syntax is not supported.

Working Example:

<pre><code evidence-query-name="myFirstQuery" lang="sql">
SELECT * FROM my_first_table WHERE user_id = '${selectedUserId}'
</code></pre>

Broken Example:

<pre><code evidence-query-name="myFirstQuery" lang="sql">
SELECT * FROM my_first_table WHERE user_id = '{selectedUserId}'
</code></pre>

Accessing Queries

To load queries that are placed on the page, create a reference to the queries store from $evidence/queries

<script>
    import { getQueries } from '$evidence/queries';
    const queries = getQueries();
    /** @type {import("@evidence-dev/query-store").QueryStore} */
    let { myFirstQuery } = $queries;
    $: ({ myFirstQuery } = $queries);
</script>

$queries is a set of QueryStore based on what is provided on the page.

Full Example

<script>
    import { getQueries } from '$evidence/queries';
    const queries = getQueries();
    /** @type {import("@evidence-dev/query-store").QueryStore} */
    let { myFirstQuery } = $queries;
    $: ({ myFirstQuery } = $queries);
</script>

<pre><code evidence-query-name="myFirstQuery" lang="sql">
SELECT * FROM my_first_table
</code></pre>

{#if !$myFirstQuery.loaded}
    <!-- 
        The query has not yet loaded.
        $myFirstQuery.loading is also available, 
        but will be false if the store has not yet started to load.

        QueryStore should begin loading automatically when attempting to access data,
        but can be induced with .fetch()
     -->
    Loading...
{:else if $myFirstQuery.error}
    <!-- An error has been encountered -->
    Error: {$myFirstQuery.error.message}
{:else}
    <!-- Results have been loaded -->
    {#each $myFirstQuery as row (row.id)}
        Row ID: {row.id}
    {:else}
        No resuls available
    {/each}
{/if}

Using runQuery

Full Example

<script>
    import { runQuery } from '$evidence/queries';

    const myFirstQuery = runQuery('myFirstQuery', 'SELECT 1');
    myFirstQuery.fetch(); // This is not done for you when using runQuery

    // Queries can by dynamic
    let x = 0;
    // declare here to ensure it is defined
    let dynamicQuery = runQuery('dynamicQuery', `SELECT ${x}`);
    // react to updates in the query string
    $: dynamicQuery = runQuery('dynamicQuery', `SELECT ${x}`);
    $: dynamicQuery.fetch();
</script>

<!-- Usage is the exact same as above -->
{#if !$myFirstQuery.loaded}
    <!-- 
        The query has not yet loaded.
        $myFirstQuery.loading is also available, 
        but will be false if the store has not yet started to load.

        QueryStore should begin loading automatically when attempting to access data,
        but can be induced with .fetch()
     -->
    Loading...
{:else if $myFirstQuery.error}
    <!-- An error has been encountered -->
    Error: {$myFirstQuery.error.message}
{:else}
    <!-- Results have been loaded -->
    {#each $myFirstQuery as row (row.id)}
        Row ID: {row.id}
    {:else}
        No resuls available
    {/each}
{/if}

Enabling SSR

If you are using the <code/> method, all you need to do is make sure the server hook is installed.

If you are using the runQuery method, you will need to use the <QuerySSR/> component.

Configuring SSR Hook

  1. Ensure that you have a ./src/hooks.server.[js|ts] file

  2. Create or update the handle function to match:

    • To create:
    import { ssrHook } from '$evidence/ssrHook.svelte.js';
    
    /** @type {import('@sveltejs/kit').Handle} */
    export async function handle({ event, resolve }) {
     /** @type {{ name: string, queryString: string}[]} */
     const presentQueries = [];
     const response = await resolve(event, {
         transformPageChunk: ssrHook(presentQueries)
     });
     return response;
    }
    
    • If you already use resolve and transformPageChunk, you can chain the functions like so:
    import { ssrHook } from '$evidence/ssrHook.svelte.js';
    
    /** @type {import('@sveltejs/kit').Handle} */
    export async function handle({ event, resolve }) {
        /** @type {{ name: string, queryString: string}[]} */
        const presentQueries = []
        const evidenceChunkTransform = ssrHook(presentQueries)
    
        const response = await resolve(event, {
            transformPageChunk: ({html, done}) => {
                // ... Do something to html
                html = await evidenceChunkTransform({ html, done })
                // ... Do other things to html
                return html
        });
        return response;
    }
    

Using <QuerySSR/>

<QuerySSR/> registers your queries with the Evidence Preprocessor, which enables correct rehydration of your queries.

<script>
    import { runQuery } from '$evidence/queries';
    import QuerySSR from '$evidence/QuerySSR.svelte';

    let manualQuery = runQuery('x', `SELECT * FROM users`);
    $: manualQuery = runQuery('x', `SELECT * FROM users`);
</script>

<QuerySSR queries={[manualQuery]} />

版本与发布记录

@evidence-dev/sdk 共发布 85 个版本,首次发布于 2024/01/19;过去 12 个月发布了 2 个版本。

版本发布日期距今
4.0.2latest2026/02/079 个月前
4.0.02025/11/1511 个月前
3.0.102025/11/051 年前
3.0.92025/02/141.7 年前
3.0.82025/02/071.8 年前
3.0.72025/02/041.8 年前

其他发布通道:preview → 0.0.1,next → 0.0.0-52469075,features-b → 1.2.3-features-b.0,dropdown-preview → 1.3.9-dropdown-preview.0。

关于 @evidence-dev/sdk 的常见问题

如何安装 @evidence-dev/sdk?

在项目目录运行 npm install @evidence-dev/sdk,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:nanoid(3.3.8)、perfect-debounce(^1.0.0)、@evidence-dev/universal-sql(3.0.1)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。它还提供命令行工具 evidence-sdk,可以用 npx evidence-sdk 直接运行。

@evidence-dev/sdk 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@evidence-dev/sdk 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@evidence-dev/sdk 可以商用吗?

@evidence-dev/sdk 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@evidence-dev/sdk 的下载量有多少?

根据 npm 官方下载统计,@evidence-dev/sdk 在 2026/10/14 – 2026/11/12 这 30 天内被下载 93,639 次,日均约 3,121 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@evidence-dev/sdk 还在维护吗?

最新版本 4.0.2 发布于 2026/02/07(9 个月前);至今共发布 85 个版本,其中过去 12 个月发布了 2 个;首次发布于 2024/01/19。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。