Cara menampilkan data dari database Sqlite dengan Svelte Kit

mrfdn.com – Sveltekit sekali lagi menjadi pilihan saya untuk mempelajari JavaScript.

Kali ini saya ingin mempelajari cara menampilkan data dari database SQLite menggunakan Svelte dan Svelte Kit.

Bagi yang belum tahu, Svelte adalah mesinnya sedangkan Svelte Kit untuk tampilannya.

Mengapa Anda harus peduli menampilkan data dari database?

Karena saya ingin membuat aplikasi soal pilihan ganda. Dimana tanya jawab disimpan dalam database.

Tanpa basa-basi lagi, berikut cara memuat data dari database dengan Sveldekit.

  1. Siapkan aplikasi Svelte npm create svelte@latest my-app.
  2. Selanjutnya, buat database SQLite. Anda dapat menggunakan file CSV lalu mengubahnya menjadi sqlite.db Kemudian simpan file database di folder root Sveldekit.
  3. Setelah itu buat folder dan file /src/api/+server.js.
  4. dalam berkas +server.js Isi dengan kode berikut:
import { error, json } from '@sveltejs/kit';

import Database from "better-sqlite3";
import fs from "node:fs";

let db = new Database(fs.readFileSync("mydatabase.db"));

export function GET() {
  const items = db.prepare("select * from mytable");
  return json(items.all());
}
  1. Edit berkasnya /src/routes/+page.svelte Jadilah seperti ini
<script>
// untuk bisa load data, pastikan menggunakan onMount
import { onMount } from 'svelte';
let items = [];

onMount (async () => {
        const response = await fetch('./api');
        items = await response.json();
})
</script>

<main>
<ol>
  {#each items as item}
    <li>
       {item.id}
       {item.nama}
    </li>
  {/each}
</ol>
</main>
  1. Sekarang coba jalankan aplikasinya dengan perintah npm run dev di terminal

Data seharusnya sudah muncul di layar.

Semoga bermanfaat.

PakarPBN

A Private Blog Network (PBN) is a collection of websites that are controlled by a single individual or organization and used primarily to build backlinks to a “money site” in order to influence its ranking in search engines such as Google. The core idea behind a PBN is based on the importance of backlinks in Google’s ranking algorithm. Since Google views backlinks as signals of authority and trust, some website owners attempt to artificially create these signals through a controlled network of sites.

In a typical PBN setup, the owner acquires expired or aged domains that already have existing authority, backlinks, and history. These domains are rebuilt with new content and hosted separately, often using different IP addresses, hosting providers, themes, and ownership details to make them appear unrelated. Within the content published on these sites, links are strategically placed that point to the main website the owner wants to rank higher. By doing this, the owner attempts to pass link equity (also known as “link juice”) from the PBN sites to the target website.

The purpose of a PBN is to give the impression that the target website is naturally earning links from multiple independent sources. If done effectively, this can temporarily improve keyword rankings, increase organic visibility, and drive more traffic from search results.

Jasa Backlink

Download Anime Batch

Tinggalkan Komentar

Alamat email Anda tidak akan dipublikasikan. Ruas yang wajib ditandai *