dataxcel
v1.0.0
Published
Sebuah library sederhana untuk menggunakan Google Sheets sebagai database untuk operasi CRUD (Create, Read, Update, Delete).
Maintainers
Readme
Google Sheet Simple DB
Sebuah library sederhana untuk menggunakan Google Sheets sebagai database untuk operasi CRUD (Create, Read, Update, Delete) di proyek Node.js.
✨ Fitur
- Mudah dikonfigurasi dengan Spreadsheet ID dan kredensial Anda.
- Mendukung operasi dasar:
getAll,findBy,create,updateBy, dandeleteBy. - Dibangun di atas
google-spreadsheetyang populer.
📦 Instalasi
npm install google-sheet-simple-db🚀 Cara Penggunaan
Pertama, pastikan Anda sudah memiliki kredensial Service Account dari Google Cloud Console dalam bentuk file JSON.
Contoh Penggunaan di Server Express.js:
const express = require('express');
const GoogleSheetDB = require('google-sheet-simple-db'); // Impor library Anda
const creds = require('./path/to/your/credentials.json'); // Impor kredensial Anda
const app = express();
app.use(express.json());
// Inisialisasi Database
const db = new GoogleSheetDB({
spreadsheetId: 'ID_SPREADSHEET_ANDA',
credentials: creds,
sheetName: 'maba2025' // Opsional, defaultnya 'maba2025'
});
// Route untuk mengambil semua data
app.get('/maba', async (req, res) => {
try {
const data = await db.getAll();
res.json({ success: true, data });
} catch (error) {
res.status(500).json({ success: false, message: error.message });
}
});
// Route untuk membuat data baru
app.post('/maba', async (req, res) => {
try {
const newData = await db.create(req.body);
res.status(201).json({ success: true, data: newData });
} catch (error) {
res.status(400).json({ success: false, message: error.message });
}
});
// Route untuk mencari berdasarkan NPM
app.get('/maba/:npm', async (req, res) => {
try {
const { npm } = req.params;
const data = await db.findBy('npm', npm);
if (!data) {
return res.status(404).json({ success: false, message: 'Data tidak ditemukan' });
}
res.json({ success: true, data });
} catch (error) {
res.status(500).json({ success: false, message: error.message });
}
});
const PORT = 3001;
app.listen(PORT, () => {
console.log(`Server berjalan di http://localhost:${PORT}`);
});