Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

538
Visualizações
How to integrate sqlite3 in Sveltekit?

I have been using sqlite3 for most of my fullstack applications (node/express, django/drf + svelte on the front end as the consumer of the api endpoints) and have been trying to figure out how to integrate sqlite3.

Here is what I did

I am assuming you are familiar with sveltekit. For those who are new, you can go check out SvelteKit

  1. I installed better-sqlite3 module
  2. Created database.js file inside src/lib folder
  3. Added the following code:
import sqlite from 'better-sqlite3'

const DB = new sqlite('./annadb.sqlite')

const schema = `CREATE TABLE IF NOT EXISTS posts(
    id INTEGER NOT NULL PRIMARY KEY, 
    title TEXT NOT NULL 
)`;

DB.exec(schema)

export default DB
  1. I created index.json.js endpoint to get all articles from the database inside the src/routes folder withe the following code:
import DB from '$lib/database.js'

export async function get() {
const articles = await DB.prepare('SELECT * FROM posts').all()

    if (articles) {
        return {
            body: {
                articles
            }
        }
    }
   
}
  1. I consumed that endpoint inside the index.svelte (the home page) like so:
<script context="module">
    export async function load({ fetch }) {
        const res = await fetch(/index.json);
        if (res.ok) {
            return {
                props: {
                    articles: await res.json(),
                },
            };
        }
        return {
            status: res.status,
            error: new Error("Could not load"),
        };
    }
</script>
  1. The fetched articles are then used in the following way:
<script>
    export let articles;
    let latestArticles = articles.articles;
    console.log(articles);
</script>

<main>
    {#if latestArticles.length > 0}
        {#each latestArticles as article}
            <h2>{article.title}</h2>
        {/each}
    {:else}
        <p>Articles are coming soon</p>
    {/if}
</main>

That's it.

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

As far as I know, better-sqlite3 is a synchronous library.

I notice you're using await with better-sqlite3. Removing await might solve your problem.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda