Cara menampilkan data dari database Sqlite dengan Svelte Kit
mrfdn.com – Sveltekit kembali menjadi pilihan saya untuk belajar javascript.
Sekarang saya ingin belajar menampilkan data dari database SQLite menggunakan Svelte dan Svelte Kit.
Bagi yang belum tahu, Svelte adalah mesinnya dan Svelte Kit untuk membuat gaya.
Mengapa Anda harus khawatir tentang mengekspos data dari database?
Karena saya ingin membuat request soal pilihan ganda. Dimana tanya jawab disimpan dalam database.
Tanpa basa-basi lagi, berikut cara memuat data dari file dengan Sveldekit.
- Instal aplikasi Svelte
npm create svelte@latest my-app. - Setelah itu, buat database SQLite. Anda dapat menggunakan file CSV dan kemudian mengonversinya menjadi sqlite.db. Kemudian simpan file tersebut di file sumber Sveldekit.
- Setelah itu, buat file dan folder
/src/api/+server.js. - Di perpustakaan
+server.jsisi dengan kode ini:
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());
}
- Pengeditan berkas
/src/routes/+page.sveltemenjadi 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>
- Sekarang coba jalankan aplikasinya dengan perintah
npm run devdi dalam kotak.
Data harus terlihat 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.