Skip to main content

Database Component

Component Available
ServerYes
ClientNo

This is the default Database wrapper that we use, this is a modified version of fivem-mongodb.

Required Attributes


Server:

  • isConnected: function
  • insert: function
  • insertOne: function
  • find: function
  • findOne: function
  • update: function
  • updateOne: function
  • delete: function
  • deleteOne: function
  • count: function

Methods


Server

isConnected

  • Parameters: None
  • Return: boolean
  • Description: Return wether or not there is an active connection to the database

insert

  • Parameters: params: table, callback: function
  • Return: table
  • Description: Inserts a list of items into the specified collection.

insertOne

  • Parameters: params: table, callback: function
  • Return: table
  • Description: Inserts a single item into the specified collection.

find

  • Parameters: params: table, callback: function
  • Return: table
  • Description: Returns all documents that match the query.

findOne

  • Parameters: params: table, callback: function
  • Return: table
  • Description: Returns the first document that match the query.

update

  • Parameters: params: table, callback: function, isUpdateOne: boolean
  • Return: table
  • Description: Updates all documents that match the filter if isUpdateOne is false, or updates the first document that matches the filter if isUpdateOne true

updateOne

  • Parameters: params: table, callback: function
  • Return: table
  • Description: Updates the first document that matches the filter.

delete

  • Parameters: params: table, callback: function
  • Return: table
  • Description: Deletes all documents that match the filter.

deleteOne

  • Parameters: params: table, callback: function
  • Return: table
  • Description: Deletes the first document that match the filter.

count

  • Parameters: params: table, callback: function
  • Return: int
  • Description: Counts the number of documents that match the filter.

Client

This component has no client methods.

Events


Server

Database:Server:Ready

  • Sent Values: None
  • Description: Indicates both authentication & game databases are connected.

Client

No client events are fired from this component.

Query Table


All of the methods in this component are expecting a table that has all of the query data for MongoDB. Below are some examples of how certain operations are done.

Insert

exports['bs_base']:FetchComponent('Database'):insertOne({ 
collection = 'somecollection',
document = {
SomeKey = 'SomeData',
int = 0,
bool = true,
table = {
something = 'something'
},
}
}, function (success, result, insertedIds)
if not success then return end

-- Do stuff here
end)

Find

exports['bs_base']:FetchComponent('Database'):findOne({
collection = 'somecollection',
query = {
SomeKey = 'SomeData'
}
}, function (success, results)
if not success then return end

-- Do stuff here
end)

Update

exports['bs_base']:FetchComponent('Database'):updateOne({
collection = 'somecollection',
query = {
SomeKey = 'SomeData',
},
update = {
["$set"] = {
SomeKey = 'SomeOtherData'
}
}
})

Delete

exports['bs_base']:FetchComponent('Database'):deleteOne({
collection = 'somecollection',
query = {
SomeKey = 'SomeOtherData'
}
}, function (success, results)
if not success then return end

-- Do stuff here
end)

Count

exports['bs_base']:FetchComponent('Database'):count({
collection = 'somecollection',
query = {
SomeKey = 'SomeData'
}
}, function (success, count)
if not success then return end

-- Do stuff here
end)

Other Helpful Tips


Ordering

You may want to order your results, achieving this is very simple and just requires passing an options table in the query. 1 is to order ascending, and -1 is order descending

exports['bs_base']:FetchComponent('Database'):findOne({
collection = 'somecollection',
query = {
SomeKey = 'SomeData'
},
options = {
filter = {
SomeKey = -1
}
}
}, function (success, results)
if not success then return end

-- Do stuff here
end)

Should you need to do anything else that isn't covered in this document, refer to the MongoDB Documentation