Skip to content

Commit 9b1348e

Browse files
committed
fixes
1 parent 0cea5bd commit 9b1348e

2 files changed

Lines changed: 154 additions & 26 deletions

File tree

README.md

Lines changed: 153 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,51 @@
11
# 🍋 MongoDB Wrapper for FiveM
22

3-
> ⚠️ **This resource is currently in testing phase.** Expect frequent updates and improvements. Feedback is welcome!
3+
> ⚠️ **This resource is in active development.** Bugs and frequent updates are expected. Feedback and contributions are welcome!
44
5-
A modern and lightweight **MongoDB wrapper** built for **FiveM** using Lua. It provides a simple and efficient way to interact with MongoDB databases in your server scripts, supporting all the essential operations like `find`, `insert`, `update`, and `delete`.
5+
A modern, lightweight **MongoDB wrapper** built for **FiveM** using Lua. It provides a simple and efficient way to interact with MongoDB databases inside your server scriptssupporting all essential operations such as `find`, `insert`, `update`, `delete`, and more.
66

77
---
88

99
## 🚀 Features
1010

11-
-Simple and readable Lua MongoDB API
12-
- ✅ Asynchronous operations with callback support
13-
-Support for `ObjectId` conversion
14-
- ✅ ESX/QBCore-friendly
15-
- ✅ Easily extendable for custom use cases
16-
- ✅ Built with scalability in mind
11+
-Clean and readable Lua-based API
12+
- ✅ Asynchronous operations with callback support
13+
-Automatic `ObjectId` handling
14+
-Compatible with ESX / QBCore
15+
- ✅ Easily extendable for custom use cases
16+
- ✅ Built with scalability in mind
1717

1818
---
1919

2020
## 📦 Supported Operations
2121

22-
- `Mongo:Find()`
23-
- `Mongo:FindOne()`
24-
- `Mongo:InsertOne()`
25-
- `Mongo:UpdateOne()`
26-
- `Mongo:DeleteOne()`
27-
- `Mongo:FindMany()`
28-
- `Mongo:FindOneAndDelete()`
22+
- `Mongo:Find()`
23+
- `Mongo:FindOne()`
24+
- `Mongo:InsertOne()`
25+
- `Mongo:InsertMany()`
26+
- `Mongo:UpdateOne()`
27+
- `Mongo:UpdateMany()`
28+
- `Mongo:DeleteOne()`
29+
- `Mongo:DeleteMany()`
30+
- `Mongo:FindMany()`
31+
- `Mongo:FindOneAndDelete()`
32+
- `Mongo:Count()`
33+
- `Mongo:Aggregate() ` *[TODO]*
34+
- `Mongo:FindSpec()` *(custom utility)*
2935

3036
---
3137

32-
## 📚 Example Usage
38+
## 📚 Example Usages
39+
40+
3341

3442
```lua
3543
local Mongo
3644
RegisterCommand('getusers', function(source, args, rawCommand)
37-
Mongo:Find({
45+
Mongo:FindMany({
3846
collection = "users",
3947
query = {}
40-
}, function(users)
48+
}, function(err, users)
4149
if not users or #users == 0 then
4250
print("^1No users found.^7")
4351
return
@@ -84,6 +92,133 @@ Mongo:FindOne({
8492
end)
8593
end
8694
end)
95+
96+
97+
Mongo:InsertMany({
98+
collection = "users",
99+
documents = {
100+
{ username = "user1", email = "user1@example.com" },
101+
{ username = "user2", email = "user2@example.com" }
102+
}
103+
}, function(err, result)
104+
if err then
105+
print("InsertMany Error:", err)
106+
else
107+
print("Inserted documents:", json.encode(result.insertedIds))
108+
end
109+
end)
110+
111+
112+
Mongo:UpdateOne({
113+
collection = "users",
114+
query = { username = "jane_doe" },
115+
update = {
116+
["$set"] = { last_login = os.date("!%Y-%m-%dT%H:%M:%SZ") } or last_login = os.date("!%Y-%m-%dT%H:%M:%SZ")
117+
}
118+
}, function(err, result)
119+
if err then
120+
print("UpdateOne Error:", err)
121+
else
122+
print("Modified count:", result.modifiedCount)
123+
end
124+
end)
125+
126+
127+
128+
Mongo:UpdateMany({
129+
collection = "users",
130+
query = { active = false },
131+
update = {
132+
["$set"] = { active = true }
133+
}
134+
}, function(err, result)
135+
if err then
136+
print("UpdateMany Error:", err)
137+
else
138+
print("Documents updated:", result.modifiedCount)
139+
end
140+
end)
141+
142+
143+
144+
145+
Mongo:DeleteOne({
146+
collection = "users",
147+
query = { username = "user_to_delete" }
148+
}, function(err, result)
149+
if err then
150+
print("DeleteOne Error:", err)
151+
else
152+
print("Deleted count:", result.deletedCount)
153+
end
154+
end)
155+
156+
157+
158+
159+
Mongo:DeleteMany({
160+
collection = "users",
161+
query = { inactive = true }
162+
}, function(err, result)
163+
if err then
164+
print("DeleteMany Error:", err)
165+
else
166+
print("Deleted documents:", result.deletedCount)
167+
end
168+
end)
169+
170+
171+
Mongo:Count({
172+
collection = "users",
173+
query = { active = true }
174+
}, function(err, count)
175+
if err then
176+
print("CountDocuments Error:", err)
177+
else
178+
print("Active users count:", count)
179+
end
180+
end)
181+
182+
183+
Mongo:FindSpec({
184+
collection = "users",
185+
query = { age = { ["$gt"] = 18 } },
186+
limit = 50
187+
}, function(err, data)
188+
if err then
189+
print("Error fetching users:", err)
190+
return
191+
end
192+
193+
for _, user in ipairs(data) do
194+
print(("User: %s | Identifier: %s"):format(user.username, user.identifier))
195+
end
196+
end)
197+
198+
199+
200+
-- TODO!
201+
Mongo:Aggregate({
202+
collection = "users",
203+
pipeline = {
204+
{ ["$match"] = { active = true } },
205+
{ ["$group"] = {
206+
_id = "$country",
207+
count = { ["$sum"] = 1 }
208+
}}
209+
}
210+
}, function(err, results)
211+
if err then
212+
print("Aggregate Error:", err)
213+
else
214+
for _, row in ipairs(results) do
215+
print(("Country: %s | Users: %d"):format(row._id, row.count))
216+
end
217+
end
218+
end)
219+
220+
221+
87222
```
88223

89224

src/query/queries.ts

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -154,14 +154,7 @@ const formatDoc = (doc: WithId<Document> | null): Record<string, any> | null =>
154154
return { _id: _id.toString(), ...rest };
155155
};
156156

157-
/* const handleResult = <T>(result: T | null, error?: string | null): Result<T> => {
158-
if (error) {
159-
console.error("Error:", error);
160-
return { success: false, error };
161-
}
162-
if (result) return { success: true, data: result };
163-
return { success: false, error: "No results found" };
164-
}; */
157+
165158

166159
async function withErrorHandling<T>(
167160
operation: () => Promise<T>,

0 commit comments

Comments
 (0)