MongoDB wrapper for use within FiveM
A typescript implementation of a MongoDB wrapper for use within FiveM. Completely written in typescript, exporting functions to easily access and manipulate a Mongo database. Adapted from @alcoholiclobster's fivem-mongodb, but rewritten in Typescript to be modular, and type safe.
- Clone the library, update NPM, and build:
git clone https://github.com/SkinnyPeteTheGiraffe/n-mongodb.git mongodb cd mongodb yarn install yarn run build
- Create a
database.cfg
and place it within your server root directory.- Add the following lines to the
database.cfg
file (do not include <>):set mongodb_host <REPLACE_WITH_MONGO_HOST> set mongodb_collection <REPLACE_WITH_MONGO_COLLECTION_NAME>
- Add the following lines to the
- At the top of your
server.cfg
include (beforeensure
):exec "database.cfg"
- Include the library in your
server.cfg
:ensure mongodb
exports.mongodb:findOne({ collection = "users", query = { _id = id } }, function (success, result)
if not success then
print("Error message: "..tostring(result))
return
end
print("User name is "..tostring(result[1].name))
end)
exports.mongodb.findOne({ collection: "users", query: { _id: id } }, (success, result) => {
if (!success) {
console.log(`Error message: ${result}`)
return
}
console.log(`User name is ${result[1].name}`)
})