You are making a Blog posting website for showcasing your understanding on the particular topic with the integration of WeaveDB.
This is how our schema is defined 👇.
{
"type": "object",
"properties": [
{
"title": {
"type": "string"
},
"description": {
"type": "string"
}
}
]
}
Your add query looks like this for now atleast :
await db.add({ title: titles, description: descriptions}, "Blogs")
You might be wondering why I am passing titles to the title and descriptions to the description.
Because I have set the input in such a that it gives data to the states, the states are defined as these
const [titles, setTitles] = useState("");
const [descriptions, setDescriptions] = useState("");
So that’s why I am setting them as title : titles and description : descriptions.
Ok! So basic structure might be clear now.
So you might have figured out now, that you want another property other than the title and description!
Now, the updated schema should look like this 👇
{
"type": "object",
"properties": [
{
"title": {
"type": "string"
},
"description": {
"type": "string"
},
"slug" : {
"type": "slug"
}
}
]
}
Since we have added slug in the schema, we can use it now directly in the query to make slugs automatically whenever a add query is fired to create new blog.
Let us look at the query again where we are going to add the slug.
await db.add({title:titles,description:descriptions,slug:titles}, "Blogs")
Your expression would be like why are we passing titles again in the slug, we are already going that in titles.
await db.add({title:titles,description:descriptions, slug:titles.split(" ").join("-").toLowerCase()}, "Blogs")
Let’s say we have “
Sonic Free Games”I want to convert it to “
sonic-free-games”
That’s why we are using these JavaScript built-in functions.
Note for Novice : The split function removes the whitespace in between the words and the join function will replace the whitespaces with these “-”.
For Example :
You wrote the title = How to use React useState Hook
and in description = Blah…Blah…Blah
The data registered in WeaveDB would be like this
title = How to use React useState Hook
description = Blah… Blah… Blah
slug = how-to-use-react-usestate-hook
So what happened here is the query we sent already got the altered data no need to change it through Database or no other hassles.
That’s it for this blog guys, You can reach me out at Twitter
