Skip to content

Instantly share code, notes, and snippets.

@gewenyu99
Last active November 27, 2023 21:33
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gewenyu99/091f0e9e34d77586657a052a9c835f53 to your computer and use it in GitHub Desktop.
Save gewenyu99/091f0e9e34d77586657a052a9c835f53 to your computer and use it in GitHub Desktop.
Appwrite Index Example
{
"projectId": "how-do-i-index",
"projectName": "",
"collections": [
{
"$id": "movies",
"$read": [],
"$write": [],
"name": "Movies",
"enabled": true,
"permission": "collection",
"attributes": [
{
"key": "year",
"type": "integer",
"status": "available",
"required": false,
"array": false,
"min": 1800,
"max": 3000,
"default": null
},
{
"key": "title",
"type": "string",
"status": "available",
"required": false,
"array": false,
"size": 255,
"default": null
},
{
"key": "description",
"type": "string",
"status": "available",
"required": false,
"array": false,
"size": 1000,
"default": null
}
],
"indexes": [
{
"key": "title",
"type": "key",
"status": "available",
"attributes": [
"title"
],
"orders": [
"ASC"
]
},
{
"key": "description",
"type": "fulltext",
"status": "available",
"attributes": [
"description"
],
"orders": [
"ASC"
]
},
{
"key": "title-and-year",
"type": "unique",
"status": "available",
"attributes": [
"title",
"year"
],
"orders": [
"ASC",
"ASC"
]
}
]
}
]
}
const sdk = require('node-appwrite');
const client = new sdk.Client();
client
.setEndpoint('https://demo.appwrite.io/v1') // Your API Endpoint
.setProject('how-do-i-index') // Your project ID
.setKey(process.env.API_KEY) // Your secret API key
;
let database = new sdk.Database(client);
let addMovies = []
addMovies.push(database.createDocument('movies', 'unique()', {
title: "The Lion King",
year: "1994",
description: "The original cartoon Lion King from Disney."
}))
addMovies.push(database.createDocument('movies', 'unique()', {
title: "The Lion King",
year: "2019",
description: "The animated remake of Lion King"
}))
addMovies.push(database.createDocument('movies', 'unique()', {
title: "Snow White and the Seven Dwarfs",
year: "1937",
description: "Snow White and the Seven Dwarfs is a 1937 musical comedy fantasy animated film. It is America's first feature-length animated film. It is also the first one in English and the first in Technicolor. It is produced by Walt Disney Productions, premiered on December 21, 1937, and was originally released to theaters by RKO Radio Pictures on February 4, 1938. The film is an adaptation of the Brothers Grimm fairy tale, in which an evil queen attempts to have her stepdaughter, Snow White, murdered in jealousy of her beauty. But she escapes and is given shelter by seven dwarfs in their cottage in a forest."
}))
addMovies.push(database.createDocument('movies', 'unique()', {
title: "Cinderella",
year: "2015",
description: "The 2015 remake of the original animated Cinderella."
}))
addMovies.push(database.createDocument('movies', 'unique()', {
title: "Cinderella",
year: "1950",
description: "With a wicked stepmother (Wilfred Jackson) and two jealous stepsisters (Homer Brightman, Harry Reeves) who keep her enslaved and in rags, Cinderella (Clyde Geronimi) stands no chance of attending the royal ball."
}))
Promise.allSettled(addMovies).then((response) => {
console.log(response);
});
const queries = [
database.listDocuments('movies', [sdk.Query.equal('title', 'The Lion King')]),
database.listDocuments('movies', [sdk.Query.equal('title', 'Cinderella'), sdk.Query.equal('year', 1950)]),
database.listDocuments('movies', [sdk.Query.search('description', 'remake')])
]
Promise.allSettled(queries).then(
(responses)=>{
const [everyLionKing, originalCinderella, remakes] = responses;
console.log(everyLionKing.value)
console.log(originalCinderella.value)
console.log(remakes.value)
}
);
[
{
status: 'fulfilled',
value: {
title: 'The Lion King',
year: '1994',
description: 'The original cartoon Lion King from Disney.',
'$collection': 'movies',
'$id': '62a793e631cce5281683',
'$read': [],
'$write': []
}
},
{
status: 'fulfilled',
value: {
title: 'The Lion King',
year: '2019',
description: 'The animated remake of Lion King',
'$collection': 'movies',
'$id': '62a793e62d7c105ee92f',
'$read': [],
'$write': []
}
},
{
status: 'fulfilled',
value: {
title: 'Snow White and the Seven Dwarfs',
year: '1937',
description: "Snow White and the Seven Dwarfs is a 1937 musical comedy fantasy animated film. It is America's first feature-length animated film. It is also the first one in English and the first in Technicolor. It is produced by Walt Disney Productions, premiered on December 21, 1937, and was originally released to theaters by RKO Radio Pictures on February 4, 1938. The film is an adaptation of the Brothers Grimm fairy tale, in which an evil queen attempts to have her stepdaughter, Snow White, murdered in jealousy of her beauty. But she escapes and is given shelter by seven dwarfs in their cottage in a forest.",
'$collection': 'movies',
'$id': '62a793e632112774c08e',
'$read': [],
'$write': []
}
},
{
status: 'fulfilled',
value: {
title: 'Cinderella',
year: '2015',
description: 'The 2015 remake of the original animated Cinderella.',
'$collection': 'movies',
'$id': '62a793e688f4ff05f620',
'$read': [],
'$write': []
}
},
{
status: 'fulfilled',
value: {
title: 'Cinderella',
year: '1950',
description: 'With a wicked stepmother (Wilfred Jackson) and two jealous stepsisters (Homer Brightman, Harry Reeves) who keep her enslaved and in rags, Cinderella (Clyde Geronimi) stands no chance of attending the royal ball.',
'$collection': 'movies',
'$id': '62a793e6886c41162589',
'$read': [],
'$write': []
}
}
]
{
total: 2,
documents: [
{
year: 2019,
title: 'The Lion King',
description: 'The animated remake of Lion King',
'$id': '62a793e62d7c105ee92f',
'$read': [],
'$write': [],
'$collection': 'movies'
},
{
year: 1994,
title: 'The Lion King',
description: 'The original cartoon Lion King from Disney.',
'$id': '62a793e631cce5281683',
'$read': [],
'$write': [],
'$collection': 'movies'
}
]
}
{
total: 1,
documents: [
{
year: 1950,
title: 'Cinderella',
description: 'With a wicked stepmother (Wilfred Jackson) and two jealous stepsisters (Homer Brightman, Harry Reeves) who keep her enslaved and in rags, Cinderella (Clyde Geronimi) stands no chance of attending the royal ball.',
'$id': '62a793e6886c41162589',
'$read': [],
'$write': [],
'$collection': 'movies'
}
]
}
{
total: 2,
documents: [
{
year: 2019,
title: 'The Lion King',
description: 'The animated remake of Lion King',
'$id': '62a793e62d7c105ee92f',
'$read': [],
'$write': [],
'$collection': 'movies'
},
{
year: 2015,
title: 'Cinderella',
description: 'The 2015 remake of the original animated Cinderella.',
'$id': '62a793e688f4ff05f620',
'$read': [],
'$write': [],
'$collection': 'movies'
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment