Quickstarters
CRUD Samples
How to Create a CRUD Application with Svelte?
33 min
overview in this walkthrough, you'll learn how to build a complete crud (create, read, update, delete) app using svelte we’ll utilize back4app as our backend platform, which offers a robust low code backend to manage your app’s data this guide explains how to set up a back4app project, design a versatile data schema, and implement crud operations within a svelte application initially, you will create a back4app project titled basic crud app svelte this project forms the basis of your backend setup, where you'll define your data schema either manually or with the help of back4app’s integrated ai agent next, you'll explore the back4app admin app—a user friendly interface for data management—allowing you to easily perform crud operations finally, you will connect your svelte application to back4app, making use of either the parse javascript sdk or direct rest/graphql api calls, while ensuring secure data handling by the end of this tutorial, you’ll have a fully functional svelte application that performs basic crud tasks along with secure user authentication what you will learn how to set up a svelte based crud application with a flexible backend methods to organize your backend and connect it to your svelte app how to leverage the back4app admin app for seamless data operations strategies for deploying your svelte application, including docker containerization prerequisites before you begin, ensure you have a back4app account with an active project need help? visit back4app getting started https //www back4app com/docs/get started/new parse app for guidance a svelte development setup you can use a code editor like vscode and ensure node js is installed basic familiarity with svelte, javascript, and rest apis review the svelte documentation https //svelte dev/docs if needed step 1 – setting up your project launching a new back4app project log into your back4app account hit the “new app” button on your dashboard name your project basic crud app svelte and follow the on screen instructions to complete the setup create new project after creating your project, you’ll see it on your dashboard, which serves as the foundation for configuring your backend step 2 – crafting the data schema structuring your data for this crud application, you need to define a few collections in your back4app project below are examples of the key collections and fields necessary to support your svelte crud operations 1\ items collection this collection stores information for each item field type details id objectid auto generated unique identifier title string name or title of the item description string brief description of the item createdat date timestamp when the item was created updatedat date timestamp for the last update to the item 2\ users collection this collection manages user credentials and authentication details field type details id objectid auto generated unique identifier username string unique username for each user email string user’s email address passwordhash string encrypted password for authentication createdat date account creation timestamp updatedat date timestamp for the last account update you can create these collections and define fields directly from your back4app dashboard create new class you can add fields by choosing the appropriate type, naming the field, setting default values, and marking it as required create column using the back4app ai agent for quick schema generation the integrated ai agent in back4app can automatically set up your data schema based on a simple description this tool streamlines the setup process and ensures that your schema is optimized for crud operations how to utilize the ai agent open the ai agent in your project settings on the back4app dashboard, find the ai agent detail your schema requirements provide a clear prompt that outlines the collections and fields you need review and confirm check the proposed schema and confirm to apply it example prompt create the following collections in my back4app project 1\) collection items \ fields \ id objectid (auto generated) \ title string \ description string \ createdat date (auto generated) \ updatedat date (auto updated) 2\) collection users \ fields \ id objectid (auto generated) \ username string (unique) \ email string (unique) \ passwordhash string \ createdat date (auto generated) \ updatedat date (auto updated) this method saves time and ensures your backend schema is perfectly aligned with your app’s needs step 3 – managing data with the admin app overview of the admin app the back4app admin app offers a no code environment for managing your backend data with its drag and drop interface, you can easily perform operations like adding, editing, and deleting records activating the admin app go to the “more” menu in your back4app dashboard select “admin app” and then click on “enable admin app ” set up your admin account by following the prompts to establish your credentials this process will also set up roles (like b4aadminuser ) and other system configurations enable admin app once enabled, log in to the admin app to manage your project’s data effectively admin app dashboard how to use the admin app insert new records use the “add record” feature in a collection (like items) to introduce new data examine or edit records click on a record to view its details or modify its fields delete records remove outdated or unnecessary data entries this intuitive interface makes managing your backend data straightforward and efficient step 4 – connecting your svelte app to back4app now that your backend is set up, it’s time to link your svelte application to back4app option a using the parse javascript sdk install the sdk use npm or yarn to install the parse javascript sdk npm install parse initialize parse in your svelte app in your main javascript file (e g , src/main js ), initialize parse // src/main js import parse from 'parse'; parse initialize("your application id", "your javascript key"); parse serverurl = 'https //parseapi back4app com'; // your svelte app initialization code follows implement crud functions create a module (for example, src/services/items js ) to handle crud operations // src/services/items js import parse from 'parse'; export async function fetchitems() { const query = new parse query("items"); try { const results = await query find(); return results; } catch (error) { console error("error fetching items ", error); return \[]; } } export async function additem(title, description) { const item = parse object extend("items"); const item = new item(); item set("title", title); item set("description", description); try { await item save(); console log("item successfully created!"); } catch (error) { console error("error creating item ", error); } } export async function updateitem(objectid, newtitle, newdescription) { const query = new parse query("items"); try { const item = await query get(objectid); item set("title", newtitle); item set("description", newdescription); await item save(); console log("item updated successfully!"); } catch (error) { console error("error updating item ", error); } } export async function removeitem(objectid) { const query = new parse query("items"); try { const item = await query get(objectid); await item destroy(); console log("item deleted successfully!"); } catch (error) { console error("error deleting item ", error); } } option b using rest/graphql apis if the parse sdk is not ideal for your use case, you can directly invoke back4app’s rest or graphql endpoints for instance, to retrieve items via rest export async function getitemsrest() { try { const response = await fetch('https //parseapi back4app com/classes/items', { headers { 'x parse application id' 'your application id', 'x parse rest api key' 'your rest api key' } }); const data = await response json(); return data results; } catch (error) { console error("rest error fetching items ", error); return \[]; } } integrate these api calls within your svelte components as needed step 5 – securing your backend using access control lists (acls) enhance the security of your data by setting up acls on your objects for example, to create an item visible only to its owner import parse from 'parse'; export async function createprivateitem(title, description, owner) { const item = parse object extend("items"); const item = new item(); item set("title", title); item set("description", description); const acl = new parse acl(); acl setreadaccess(owner, true); acl setwriteaccess(owner, true); acl setpublicreadaccess(false); acl setpublicwriteaccess(false); item setacl(acl); try { await item save(); console log("private item successfully created!"); } catch (error) { console error("error saving private item ", error); } } setting class level permissions (clps) configure clps directly in your back4app dashboard to restrict access so that only authenticated users can interact with certain collections step 6 – implementing user authentication in svelte setting up user accounts back4app leverages parse’s built in user collection for managing authentication in your svelte app, implement registration and login functions similar to the examples below import parse from 'parse'; export async function registeruser(username, password, email) { const user = new parse user(); user set("username", username); user set("password", password); user set("email", email); try { await user signup(); console log("user registered successfully!"); } catch (error) { console error("registration error ", error); } } export async function loginuser(username, password) { try { const user = await parse user login(username, password); console log("user logged in ", user get("username")); } catch (error) { console error("login failed ", error); } } this approach can be extended to manage sessions, password resets, and additional authentication features step 7 – deploying your svelte application back4app simplifies the deployment process you can deploy your svelte app using several methods, including docker containerization 7 1 build your svelte application compile your app run your svelte build command, typically npm run build review the build ensure that the generated files (usually in the public or build folder) include all necessary assets 7 2 project structure example a typical svelte project structure might look like basic crud app svelte/ \| public/ \| | build/ \| | global css \| | index html \| src/ \| | components/ \| | | itemlist svelte \| | | itemform svelte \| | services/ \| | | items js \| | | auth js \| | app svelte \| | main js \| package json \| readme md 7 3 dockerizing your svelte app if you prefer containerized deployment, create a dockerfile in your project’s root \# use a node js image to build the app from node 16 alpine as build \# set the working directory workdir /app \# copy package files and install dependencies copy package json package lock json / run npm install \# copy the rest of the project files and build the app copy run npm run build \# use a lightweight server to serve the built files from nginx\ alpine copy from=build /app/public /usr/share/nginx/html expose 80 cmd \["nginx", " g", "daemon off;"] 7 4 deploying via back4app web deployment link your github repository host your svelte project on github set up deployment in back4app in the back4app dashboard, choose the web deployment option, connect your repository (e g , basic crud app svelte ), and select your target branch define build settings specify your build command (like npm run build ) and point to the build output directory deploy your application click deploy and monitor the logs until your app is live step 8 – wrap up and future enhancements congratulations! you have built a svelte based crud application that connects to back4app you set up a project named basic crud app svelte , configured collections for items and users, and managed your data through the back4app admin app you then integrated your svelte frontend using the parse javascript sdk (or rest/graphql) and added robust security measures what’s next? expand functionality add features like advanced search, detailed views, or real time updates enhance backend logic experiment with cloud functions, third party api integrations, or role based access control deepen your learning explore further tutorials and the back4app documentation https //www back4app com/docs to optimize your app happy coding and enjoy building your svelte crud application!