
No Comments Yet
Be the first to share your thoughts and start the conversation.
Be the first to share your thoughts and start the conversation.
How did you manage to remove the blur property and reach here?
Upgrading gives you access to quizzes so you can test your knowledge, track progress, and improve your skills.
By logging in, you'll unlock full access to this and other free tutorials on JSM Pro.
Why? Logging in lets us personalize your learning experience, track your progress, and keep you in the loop with new workshops, coding tips, and platform updates.
You'll also be the first to know about upcoming launches, events, and exclusive discounts.
No spam—just helpful content to level up your skills.
If that sounds fair, go ahead and log in to continue →
Enter your name and email to get instant access
In this lesson, we explore the concept of database seeding, which allows developers to visualize and interact with data in a newly set-up database. By generating dummy data, we can test various relationships and functionalities without needing to create a complex admin panel.
data.ts
file containing dummy data for various product categories (e.g., burgers, pizzas, sandwiches).seed.ts
file is utilized to seed the database, creating TypeScript interfaces for type safety and defining functions for database operations.00:00:02 In this lesson, we'll talk about database seeding.
00:00:06 Whenever you're setting up a new database architecture, it's always useful to be able to visualize the data that you're creating.
00:00:14 Because think about it, if you don't yet have a form through which you can actually create the data, how will you be able to see it and interact with it
00:00:21 on the screen?
00:00:22 For that reason, we want to be able to see the database immediately and automatically so we can immediately test out all of these different relationships
00:00:32 and functionalities.
00:00:33 The alternative to that is building an entire admin panel through which you can then add menu items and other customizations.
00:00:41 But that would go beyond this Dorial, so instead we'll do it with seeding the database.
00:00:46 To get started with it, head over into your application, where we have our lib, and right below AppWrite, create a new file and call it data.ts.
00:00:58 Now, this file will contain tons of different dummy data pieces that we can then fuse into our application.
00:01:05 You can get it within the video kit link down below.
00:01:08 As you can see, this file just contains a couple of different arrays within a single object called dummy data that has different categories of products
00:01:17 with their descriptions, such as burgers, pizzas, sandwiches, and more.
00:01:21 It also have different customizations that we can add to those products.
00:01:26 It has also different menu items.
00:01:29 Now, of course, I didn't go ahead and create all of these by myself.
00:01:32 I just asked ChatGBT to generate a couple of these menu items for me.
00:01:36 And there you have it.
00:01:37 That's the dummy data.
00:01:39 But now we have to put that dummy data to use within a new file within the lib folder, which I will call seed.ts.
00:01:48 And I'll also provide this seed file right within the video kit down below.
00:01:53 Now what the seed file does is it takes that dummy data.
00:01:57 creates a couple of TypeScript interfaces for it, so we can be type safe, and then create a couple of functions that we can call,
00:02:04 such as clearing the database, clearing the storage, uploading those images to storage, and then finally seeding this database.
00:02:13 Make sure that these are called exactly as you call them within the AppAid config.
00:02:17 So for example, I call this customizations collection with an S at the end and menu customizations collection right here.
00:02:23 So just make sure that these are actually matching with what you have here.
00:02:27 Menu, customizations, collection.
00:02:30 There we have it.
00:02:31 So first things first, we clear out the entire database, then we create the categories, then we create the customizations,
00:02:38 and then we create the menu, and finally, give a console log that we have successfully seeded the database.
00:02:45 This file will also interact with the AppRite's storage to upload these images online.
00:02:51 So to make that work, let's head over into lib apprite.ts and let's create an instance of storage.
00:03:00 Here, where we have account, databases, and avatars, let's also add export const storage is equal to new storage coming from AppRite to which we pass our
00:03:13 AppRite client.
00:03:14 And make sure to import storage from React Native AppRite.
00:03:19 Perfect.
00:03:20 Now if you head back over to the seed function, let's see if everything is good.
00:03:24 Yep, the storage is now correct, so we should have no more issues, besides just making sure that we have properly culled all of these collections,
00:03:33 such as the customizations collection, as well as the menu customizations collection.
00:03:39 Perfect.
00:03:40 We're good.
00:03:41 So now, how do we actually call a script or this specific seed file?
00:03:46 Well, we have to call it within our application.
00:03:49 So, for example, let's head over into App, Tabs, and then we can do it within the search because we have nothing on that screen.
00:03:57 Wrap it within a safe area view.
00:04:01 And notice how immediately the text, the search actually comes down, because it's within the safe area view, not within a regular view.
00:04:08 Then, you have a text of search, and just below it, we can have a button with a title of Seed, that when pressed, we'll call a callback function that'll
00:04:21 simply call the seed function from lib seed.
00:04:24 And if something goes wrong, we'll catch the error and then simply console log that error by saying something like failed to seed the database.
00:04:34 And then we can also console log the error as the second parameter to this console log function.
00:04:40 So now you should be able to see the seed button right here on your phone.
00:04:44 So just click it.
00:04:45 and open up the terminal before you do just so we can track what's happening.
00:04:49 Let's make sure that we don't have any issues and it looks like we are good.
00:04:55 and click Seed.
00:04:58 It says, failed to see the database, the current user is not authorized to perform the requested action.
00:05:05 Okay, good that we put the error handling in place.
00:05:08 What action are we actually talking about?
00:05:11 It could be any of the actions here like creating documents and so on.
00:05:15 So if I head back over to Databases, User Collection, settings and then permissions, I give all roles full permissions.
00:05:26 We have to make sure that we haven't forgot to add those permissions for any of our collections.
00:05:31 But it seems like users have full permissions in all of these different collections.
00:05:38 Oh, besides categories.
00:05:39 Looks like I forgot to do it for the categories.
00:05:42 If you had it in there and if you didn't miss it, then everything should have worked for the first time.
00:05:47 But I'll add it now, and I'll just try to re-see the database one more time.
00:05:52 Let me just restart the server just to have a clean UI right here in the terminal, and I'll reload the application.
00:06:00 It's bundled and I will click Seed.
00:06:04 Now, don't re-click it a couple of times and actually give it some time to do its job.
00:06:09 It's going to actually take maybe 10, 20, 30, 50 seconds.
00:06:14 It can even take up to a couple of minutes.
00:06:16 But after some time, you'll get a Seeding Complete log or you'll get a message that something went wrong.
00:06:23 But for me, I got Seeding Complete.
00:06:26 So we can actually verify it by heading back over to our database and now open up any of these collections, such as the user collection,
00:06:34 where you'll be able to see that we only have one current user.
00:06:39 But for every other collection, you should have many things within it.
00:06:43 such as for categories, you have all of these different categories right within your database.
00:06:49 For the menu, you have all of these different menu items and even image URLs that are stored directly within AppRite Storage with ratings,
00:06:58 calories, proteins, price, and more.
00:07:01 Some are related to some categories and the others are not.
00:07:05 So, for example, we can see that we have this, what is this, grilled veggie sandwich and it is linked to this category, which is the one ending in 1-2-A,
00:07:15 and that is a sandwich.
00:07:17 You can also check out all of the different customizations that we have access to.
00:07:21 So with that in mind, we have now successfully seeded our database and we can remove this seed button from the search because now we know that the data
00:07:30 is there and we can actually start showing it on our search screen.
00:07:35 So let's do that next.