Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

540
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!