-
Notifications
You must be signed in to change notification settings - Fork 5
/
app.js
112 lines (89 loc) · 3.98 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
const sdk = require("node-appwrite");
const client = new sdk.Client();
client
.setEndpoint("https://cloud.appwrite.io/v1") // Ensure this is the correct endpoint
.setProject("your_project_id") // Replace with your Project ID
.setKey("your_api_key"); // Replace with your API Key
const databases = new sdk.Databases(client);
var todoDatabase;
var todoCollection;
async function prepareDatabase() {
try {
// Check if database already exists
const existingDatabases = await databases.list();
todoDatabase = existingDatabases.databases.find(db => db.name === 'TodosDB');
if (!todoDatabase) {
// Create new database if not existing
todoDatabase = await databases.create(sdk.ID.unique(), 'TodosDB');
console.log('Database created: TodosDB');
} else {
console.log('Database already exists: TodosDB');
}
// Check if collection already exists
const existingCollections = await databases.listCollections(todoDatabase.$id);
todoCollection = existingCollections.collections.find(col => col.name === 'Todos');
if (!todoCollection) {
// Create new collection if not existing
todoCollection = await databases.createCollection(todoDatabase.$id, sdk.ID.unique(), 'Todos');
console.log('Collection created: Todos');
} else {
console.log('Collection already exists: Todos');
}
// Check and create attributes only if not already created
const attributes = await databases.listAttributes(todoDatabase.$id, todoCollection.$id);
if (Array.isArray(attributes) && !attributes.some(attr => attr.key === 'title')) {
await databases.createStringAttribute(todoDatabase.$id, todoCollection.$id, 'title', 255, true);
console.log('Attribute created: title');
}
if (Array.isArray(attributes) && !attributes.some(attr => attr.key === 'description')) {
await databases.createStringAttribute(todoDatabase.$id, todoCollection.$id, 'description', 255, false, 'This is a test description');
console.log('Attribute created: description');
}
if (Array.isArray(attributes) && !attributes.some(attr => attr.key === 'isComplete')) {
await databases.createBooleanAttribute(todoDatabase.$id, todoCollection.$id, 'isComplete', true);
console.log('Attribute created: isComplete');
}
} catch (error) {
console.error("Error preparing database or attributes:", error);
}
}
async function seedDatabase() {
try {
const testTodo1 = {
title: 'Buy apples',
description: 'At least 2KGs',
isComplete: true
};
const testTodo2 = {
title: 'Wash the apples',
isComplete: true
};
const 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);
console.log('Test todos seeded successfully.');
} catch (error) {
console.error("Error seeding database with todos:", error);
}
}
async function getTodos() {
try {
const 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`);
});
} catch (error) {
console.error("Error fetching todos:", error);
}
}
async function runAllTasks() {
await prepareDatabase();
await seedDatabase();
await getTodos();
}
runAllTasks();