Using FireStore as an external Database

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!

18 Likes

I might work on expanding this module, if I do I will update this tutorial.

1 Like

Exactly, they could have the ban command calling the set function and creating a document with the player ID and a value like: banned true
And then have on a player added function the get part that checks if the player is banned or not in the firestore

How would I sort every value stored in the database from largest to smallest when making the get request? Or in any other possible way.

Edit: I can sort it when getting the results, but I want to know if there’s another way.

1 Like

I know this is possible using the runquery endpoint, however I dont have it added yet.
That’s a nice suggestion, I will try to update the module soon.

The api documentation if you are interested
https://firebase.google.com/docs/firestore/reference/rest/v1beta1/projects.databases.documents/runQuery

Edit:One possible thing would be to have a single document holding the values and then getting this document and reading it to order the values inside it.

1 Like

Quite interesting, I’ve never seen a tutorial using cloud firestore, mainly firebase which is what I use. Nice.

1 Like

Careful. I looked at your module and all your ProxyService/Heroku tokens and Firestore tokens are there. I really really insist you remove those so someone doesn’t do something malicious with them.

3 Likes

How long have you been using this code for yourself? Have you ever met any problems like reaching the allocated data cap?

I am using it for small projects, for the moment I haven’t hit any limits, tho I have a weird thing happening with subscriptions, I am using a REST API but subscriptions seems to be created for some reason

Thank you, it seem I uploaded my test version and not the blank module lo

I changed the module and got rid of the Proxy service by using the HTTP overide header, also added a method to update some fields only.

ik im extremely late to this but how does one insert a mapValue (table), ive tried alot of things to insert a table (im not on about the [“fields”] i mean a table within it like this:

[“fields”] = {
[“A”] = {
[“a”] = 1,
[“b”] = 1,
[“c”] = 1,
},
[“e”] = 1
}

the thing above being a example

Good use case, though this is not a good reason to. You could just make a pastebin and paste the banned user’s UserId with commas seperating each one. Then in each one of your games do a GET request each time a player joins or however many seconds you need to retrieve it.

You could transform the table into a JSON like string using HttpService:JSONEncode() and then store it.

Not the best idea to uss pastebin as a database

ah i see do you mind providing a example or documentation please?