-
Notifications
You must be signed in to change notification settings - Fork 205
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Server SDK Quickstarts #26
Merged
Merged
Changes from 2 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
568e39e
Add Node.js quickstart
adityaoberai 9c87ce4
Replace Locale service with Database service
adityaoberai 4512e48
Add Dart quickstart
adityaoberai 30ee6ed
Improve code example readability
adityaoberai 9845307
Fixed some formatting
6cd2afe
Merge branch 'main' into add-node-quickstart
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,7 +10,8 @@ | |
'qwik', | ||
'react', | ||
'sveltekit', | ||
'vuejs' | ||
'vuejs', | ||
'node' | ||
]; | ||
</script> | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,200 @@ | ||
--- | ||
layout: article | ||
title: Start with Node.js | ||
description: This is the description used for SEO. | ||
--- | ||
Learn to setup your first Node.js project powered by Appwrite. | ||
{% section #step-1 step=1 title="Create project" %} | ||
Head to the [Appwrite Console](https://cloud.appwrite.io/console). | ||
|
||
![Create project screen](/images/docs/databases/quick-start/create-project.png) | ||
|
||
If this is your first time using Appwrite, create an account and create your first project. | ||
|
||
Then, under **Integrate with your server**, add an **API Key**. Make sure to add the `databases.write`, `collections.write`, `attributes.write`, `documents.read`, and `documents.write` scopes in the **Database** category (all other scopes are optional). | ||
|
||
![Add API Key]() | ||
|
||
{% /section %} | ||
{% section #step-2 step=2 title="Create Node.js project" %} | ||
Create a Node.js CLI application. | ||
|
||
```sh | ||
mkdir my-app | ||
cd my-app | ||
npm init | ||
``` | ||
|
||
{% /section %} | ||
{% section #step-3 step=3 title="Install Appwrite" %} | ||
|
||
Install the Node.js Appwrite SDK. | ||
|
||
```sh | ||
npm install node-appwrite | ||
``` | ||
{% /section %} | ||
{% section #step-4 step=4 title="Import Appwrite" %} | ||
|
||
Find your project ID in the **Settings** page. Also, click on the **View API Keys** button to find the API key that was created earlier. | ||
|
||
![Settings page in Appwrite Console.](/images/docs/databases/quick-start/project-id.png) | ||
|
||
Create a new file `app.js` and initialize the Appwrite Client. Replace `<YOUR_PROJECT_ID>` with your project ID and `<YOUR_API_KEY>` with your API key. | ||
|
||
```js | ||
const sdk = require("node-appwrite"); | ||
|
||
const client = new sdk.Client(); | ||
|
||
client | ||
.setEndpoint("https://cloud.appwrite.io/v1") | ||
.setProject("<YOUR_PROJECT_ID>") | ||
.setKey("<YOUR_API_KEY>"); | ||
``` | ||
|
||
{% /section %} | ||
{% section #step-5 step=5 title="Initialize Database Service" %} | ||
|
||
Once the Appwrite Client is set up, initialize the Database service using the Client and create a function to prepare a Todos database and collection (with the necessary attributes) by adding the following code to `app.js`. | ||
|
||
```js | ||
const databases = new sdk.Databases(client); | ||
|
||
var todoDatabase; | ||
var todoCollection; | ||
|
||
async function prepareDatabase() { | ||
todoDatabase = await databases.create(sdk.ID.unique(), 'TodosDB'); | ||
todoCollection = await databases.createCollection(todoDatabase.$id, sdk.ID.unique(), 'Todos'); | ||
await databases.createStringAttribute(todoDatabase.$id, todoCollection.$id, 'title', 255, true); | ||
await databases.createStringAttribute(todoDatabase.$id, todoCollection.$id, 'description', 255, false, 'This is a test description'); | ||
await databases.createBooleanAttribute(todoDatabase.$id, todoCollection.$id, 'isComplete', true); | ||
} | ||
``` | ||
|
||
{% /section %} | ||
{% section #step-6 step=6 title="Seed Todos Database" %} | ||
|
||
Once the Todos database and collection is ready, create a function to seed it with sample data by adding the following code to `app.js`. | ||
|
||
```js | ||
async function seedDatabase() { | ||
var testTodo1 = { | ||
title: 'Buy apples', | ||
description: 'At least 2KGs', | ||
isComplete: true | ||
}; | ||
|
||
var testTodo2 = { | ||
title: 'Wash the apples', | ||
isComplete: true | ||
}; | ||
|
||
var testTodo3 = { | ||
title: 'Cut the apples', | ||
description: 'Don\'t forget to pack them in a box', | ||
isComplete: false | ||
}; | ||
|
||
await databases.createDocument(todoDatabase.$id, todoCollection.$id, sdk.ID.unique(), testTodo1); | ||
await databases.createDocument(todoDatabase.$id, todoCollection.$id, sdk.ID.unique(), testTodo2); | ||
await databases.createDocument(todoDatabase.$id, todoCollection.$id, sdk.ID.unique(), testTodo3); | ||
} | ||
``` | ||
|
||
{% /section %} | ||
{% section #step-7 step=7 title="Get List Of Todos" %} | ||
|
||
After the database is seeded, create a function to get the list of all the seeded data and a function to trigger all the created functions in the steps above by adding the following code to `app.js`. | ||
|
||
```js | ||
async function getTodos() { | ||
var todos = await databases.listDocuments(todoDatabase.$id, todoCollection.$id); | ||
|
||
todos.documents.forEach(todo => { | ||
console.log(`Title: ${todo.title}\nDescription: ${todo.description}\nIs Todo Complete: ${todo.isComplete}\n\n`); | ||
}); | ||
} | ||
|
||
async function runAllTasks() { | ||
await prepareDatabase(); | ||
await seedDatabase(); | ||
await getTodos(); | ||
} | ||
runAllTasks(); | ||
``` | ||
|
||
{% /section %} | ||
{% section #step-8 step=8 title="Review your project" %} | ||
|
||
Review the entire program in `app.js` once before running it. This is a good time to catch any errors that may have been made in any past step. | ||
|
||
```js | ||
const sdk = require("node-appwrite"); | ||
|
||
const client = new sdk.Client(); | ||
|
||
client | ||
.setEndpoint("https://cloud.appwrite.io/v1") | ||
.setProject("<YOUR_PROJECT_ID>") | ||
.setKey("<YOUR_API_KEY>"); | ||
|
||
const databases = new sdk.Databases(client); | ||
|
||
var todoDatabase; | ||
var todoCollection; | ||
|
||
async function prepareDatabase() { | ||
todoDatabase = await databases.create(sdk.ID.unique(), 'TodosDB'); | ||
todoCollection = await databases.createCollection(todoDatabase.$id, sdk.ID.unique(), 'Todos'); | ||
await databases.createStringAttribute(todoDatabase.$id, todoCollection.$id, 'title', 255, true); | ||
await databases.createStringAttribute(todoDatabase.$id, todoCollection.$id, 'description', 255, false, 'This is a test description'); | ||
await databases.createBooleanAttribute(todoDatabase.$id, todoCollection.$id, 'isComplete', true); | ||
} | ||
|
||
async function seedDatabase() { | ||
var testTodo1 = { | ||
title: 'Buy apples', | ||
description: 'At least 2KGs', | ||
isComplete: true | ||
}; | ||
|
||
var testTodo2 = { | ||
title: 'Wash the apples', | ||
isComplete: true | ||
}; | ||
|
||
var testTodo3 = { | ||
title: 'Cut the apples', | ||
description: 'Don\'t forget to pack them in a box', | ||
isComplete: false | ||
}; | ||
|
||
await databases.createDocument(todoDatabase.$id, todoCollection.$id, sdk.ID.unique(), testTodo1); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here, line breaks |
||
await databases.createDocument(todoDatabase.$id, todoCollection.$id, sdk.ID.unique(), testTodo2); | ||
await databases.createDocument(todoDatabase.$id, todoCollection.$id, sdk.ID.unique(), testTodo3); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here! |
||
} | ||
|
||
async function getTodos() { | ||
var todos = await databases.listDocuments(todoDatabase.$id, todoCollection.$id); | ||
|
||
todos.documents.forEach(todo => { | ||
console.log(`Title: ${todo.title}\nDescription: ${todo.description}\nIs Todo Complete: ${todo.isComplete}\n\n`); | ||
}); | ||
} | ||
|
||
async function runAllTasks() { | ||
await prepareDatabase(); | ||
await seedDatabase(); | ||
await getTodos(); | ||
} | ||
runAllTasks(); | ||
``` | ||
|
||
{% /section %} | ||
{% section #step-9 step=9 title="Check out what you've built" %} | ||
|
||
Run your project with `node app.js` and view the response in your console. | ||
|
||
{% /section %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's break lines here so the lines aren't so long
Like