I have seen some subjects about firestore but never a good module, so I have decided to create one, as I am not an expert the code might not be very good and the module does not have every api methods.
This module is supposed to make it easier for you to use Firestore.
Why using Firestore
You could use Firestore if you want to have global bans for every player, storing global values ect…
Features
This make easier to use the firestore API by simplifying the following api methods:
Get
Batch Get
Delete
Patch
Queries
Creating the Firestore
First create a firebase project by going here, selecting a project (or creating one) and then doing the 4 steps (they do not matter)
Then go in the firestore tab by cliking the fourth icon and click the button “Create Database”,
For the security rules use that code:
rules_version = ‘2’;
service cloud.firestore {
match /databases/{database}/documents {
match /{document=**} {
allow read, write: if true;
}
}
}
That will allow any request(you can modify this later) , select a region for your database and click “Enable”.
When done, you will see your database, add a collection by clicking “Start Collection”, fill the collection name, this will be your datatstore name and then fill the document.
The document is how your data will be stored you can create maps, arrays, ect…
For this tutorial my Datastore name will be “users”.
Setup the module in your game
First insert this module in the game
Open the parent module named “FireStore” and edit the values in the settings:
- FireStoreProjectId: The ID of the google project that you are using for your firestore you can find it here (mine is dc-ct-1602191218229 for example)
That’s it your done.
Usage
Here is an example:
FireStoreService = require(script.Parent.Firestore)
data1 = FireStoreService.Get("users","TestDocument")--Gets the data from the document named "TestDocument"
--"test-1" is the Datastore, "Test" the key
--Returns an object with the data
print(data1)
data2 = FireStoreService.BatchGet("users",{"TestDocument","Test2"}) --Gets the data from the documents named "TestDocument" and "Test2"
--"test-1" is the Datatstore and the second argument is an array of keys
--Returns an array of objects with the data
print(data2)
data3 = FireStoreService.Set("users","Test3",data1) --Creates a new document named "Test3" that will be a copy of the "TestDocument" as we are using the data from the GET
--"test-&" is the Datatsore, "Test3" the key and data1 the data that we got from the key "Test" at the first function
--Returns an object with the oebject created
print(data3)
data4 = FireStoreService.Delete("users","Test2") --Delete the document named "Test2"
--"test-1" is the Datastore, "Test2" is the key of the entry to delete
--Returns an empty object if the entry was deleted correctly
print(data4)
data5 = FireStoreService.query("test-1","Gold",1,">=","ASCENDING","Gold")
--"test-1" is the Datastore
--"Gold" is the field that have the value you filter
--1 is the value you are using to filter
--">=" means its filters all entries that have the field gold > or = to 1 (can be </<=/>/>=/=/~=)
--The order in what to return the query results (can be ASCENDING/DESCENDING)
--The field on what the ordering will be based
--The query will return every documents that have the field gold > or = to 1 ordered in ascending order based on the value of the gold field
--Returns an array of documents
print(data5)
local datatoupdate= {
["fields"] = {
["Golds"] = {
["integerValue"] = "30",
},
["Banned"] = {
["booleanValue"] = false,
}
}
}
Firebase.UpdateFields("users","Test1",datatoupdate)
--Updates the value of specific fields, here Gold and banned fields.
Information about the structure of Firestore
The structure is: A fields object then an object with a key that is the field name and then a key with the type of the value and the value.
If you have doubts on your structure you can build it on the firestore console and then make a get request to get the structure of your data.
You will be then able to re-use this data to make other requests.
Note: I am quite new to posting so if you think something is wrong with this post or I should change something let me know!