Skip to content

Commit

Permalink
Replace Locale service with Database service
Browse files Browse the repository at this point in the history
  • Loading branch information
adityaoberai committed Sep 22, 2023
1 parent 568e39e commit 9c87ce4
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 15 deletions.
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
132 changes: 118 additions & 14 deletions src/routes/docs/quick-starts/node/+page.markdoc
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Head to the [Appwrite Console](https://cloud.appwrite.io/console).

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 `locale.read` scope in the **Other** category (all other scopes are optional).
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]()

Expand Down Expand Up @@ -54,20 +54,79 @@ client
```

{% /section %}
{% section #step-5 step=5 title="Use Locale Service" %}
{% section #step-5 step=5 title="Initialize Database Service" %}

Once the Appwrite Client is set up, initialize the Locale service using the Client and use it to get a list of all countries by adding the following code to `app.js`.
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 locale = new sdk.Locale(client);
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" %}

locale.listCountries()
.then(response => console.log(response))
.catch(error => console.log(error));
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-6 step=6 title="Review your project" %}
{% 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.

Expand All @@ -81,15 +140,60 @@ client
.setProject("<YOUR_PROJECT_ID>")
.setKey("<YOUR_API_KEY>");

const locale = new sdk.Locale(client);

locale.listCountries()
.then(response => console.log(response))
.catch(error => console.log(error));
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);
await databases.createDocument(todoDatabase.$id, todoCollection.$id, sdk.ID.unique(), testTodo2);
await databases.createDocument(todoDatabase.$id, todoCollection.$id, sdk.ID.unique(), testTodo3);
}

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-7 step=7 title="Checkout what you've built" %}
{% 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.

Expand Down

0 comments on commit 9c87ce4

Please sign in to comment.