Docs
Skip to content

SvelteKit

Add database_

Add databases and queries to store user data in you SvelteKit project.

1 min read

Raw

Create table

In Appwrite, data is stored as a table of rows. Create a table inside a database in the Appwrite Console to store our ideas.

Create project screen
Create project screen

Create the following columns within the table:

FieldTypeSizeRequired
userIdVarchar36Yes
titleVarchar128Yes
descriptionText-No

For this tutorial, we'll also set the permissions to allow any users to read and write to the table. In a real-world scenario, you would probably want to restrict access to a certain group of users.

Managing ideas

Now that you have a table to hold ideas, we can read and write to it from our app.

Let's create some methods to manage ideas in our app.

Create a new file src/lib/ideas.js and add the following code:

JavaScript
import { ID, Query } from 'appwrite';
import { tablesDB } from '$lib/appwrite';
const IDEAS_DATABASE_ID = '<YOUR_DATABASE_ID>'; // Replace with your database ID
const IDEAS_TABLE_ID = '<YOUR_TABLE_ID>'; // Replace with your table ID
export async function getIdeas() {
return await tablesDB.listRows({
databaseId: IDEAS_DATABASE_ID,
tableId: IDEAS_TABLE_ID,
// Use a query to show the latest ideas first
queries: [Query.orderDesc('$createdAt')]
});
}
export async function addIdea(userId, title, description) {
await tablesDB.createRow({
databaseId: IDEAS_DATABASE_ID,
tableId: IDEAS_TABLE_ID,
rowId: ID.unique(),
data: {
userId,
title,
description
}
});
}
export async function deleteIdea(id) {
await tablesDB.deleteRow({
databaseId: IDEAS_DATABASE_ID,
tableId: IDEAS_TABLE_ID,
rowId: id
});
}

Remember to use a store to hold data returned from Appwrite Databases, so your components can be updated when the data changes.

Was this page helpful?

Share what worked or what we should fix. Once approved, our agents automatically apply suggested updates to the docs.