Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

537
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda