Skip to content
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 6 commits into from
Sep 25, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/routes/docs/quick-starts/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
'qwik',
'react',
'sveltekit',
'vuejs'
'vuejs',
'node'
];
</script>

Expand Down
200 changes: 200 additions & 0 deletions src/routes/docs/quick-starts/node/+page.markdoc
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');
Copy link
Contributor

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

await databases.createStringAttribute(
    todoDatabase.$id, 
    todoCollection.$id, 
    'title', 
    255, 
    true
);    

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);
Copy link
Contributor

Choose a reason for hiding this comment

The 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);
Copy link
Contributor

Choose a reason for hiding this comment

The 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 %}
2 changes: 1 addition & 1 deletion src/routes/docs/quick-starts/sveltekit/+page.markdoc
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ 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 accout and create your first project.
If this is your first time using Appwrite, create an account and create your first project.

Then, under **Add a platform**, add a **Web app**. The **Hostname** should be localhost.

Expand Down