sveltekit-libsql Svelte Themes

Sveltekit Libsql

SvelteKit + libSQL (Local File) Example

This project demonstrates a basic Todo list application built with SvelteKit, using the libsql-client directly to interact with a local SQLite database file (todos.db).

It serves as a minimal example of performing common database operations:

  • Create: Adding new todos to the database (INSERT).
  • Read: Fetching and displaying the list of todos (SELECT).
  • Update: Toggling the completion status of a todo (UPDATE).
  • Delete: Removing a todo from the list (DELETE).

Setup and Running

  1. Clone or Download: Get the project files onto your local machine.
  2. Navigate: Open your terminal and change into the project directory:
    cd path/to/your-project-directory
    
  3. Install Dependencies:
    npm install
    
  4. Run Development Server:
    npm run dev
    
  5. Open: Visit http://localhost:5173 (or the specified port) in your browser.

The application will automatically create the todos.db SQLite database file in the project's root directory and set up the necessary todos table the first time the server starts, thanks to the initialization logic.

Key Files

  • src/lib/server/db/index.ts:
    • Configures and exports the libsql-client (db).
    • Important: Contains the hardcoded url (e.g., "file:./todos.db") for the database connection.
    • Includes the initializeDb function that runs on server start to execute the CREATE TABLE IF NOT EXISTS statement.
  • src/routes/+page.server.ts:
    • Handles all server-side data loading and form actions.
    • The load function fetches todos (SELECT) using db.execute.
    • The actions object contains functions (addTodo, toggleTodo, deleteTodo) that perform INSERT, UPDATE, and DELETE operations using db.execute based on form submissions.
  • src/routes/+page.svelte:
    • Provides the user interface for displaying todos.
    • Contains HTML forms that submit data to the actions defined in +page.server.ts to trigger database operations.

Relevant Documentation

Top categories

Loading Svelte Themes