How to port from MySQL to CouchDB?


#1

Would someone care to teach us newbs on to port from MySQL to CouchDB so me personally can get my server back up and functioning… Also im sure there is a few others that would also like to know so we dont have to wait for all the Devs to do it. Thats is if they ever did…


#2

I would just wait for fivem devs to add mysql support.


#3

Thats what i planned to do but who knows when thats gonna happen…


#4

One of them said this week. Don’t know if that will hold true though.


#5

There’s plenty of tutorials online on how to use CouchDB. These are just the first three I’ve found.

https://www.tutorialspoint.com/couchdb/

http://docs.couchdb.org/en/2.0.0/intro/tour.html

Learn how to use the database then you will be able to port the MySQL data over yourself :slight_smile:


#6

Also note that once your data is in couchdb, you need to update all your scripts aswell where you have mysql connections and make sure they are coucbdb(JSON) compatible.


#7

It’s just a database connection. If you don’t want to change your database infrastructure it might be easier to support sql yourself rather than porting everything to couch. Fivem development and essentialmode development are two different things.


#8

I see so many people saying pick one or the other lol, I just made something that converts the information from couchdb database to mysql and use mysql-async’s essentialmode api. At least this way I dont have to keep porting scripts to either couch or mysql…

We gotta work with what we are given, and sometimes it is something bad like having to port a bunch of scripts. either way usually there are ways around it. stop complaining and find the easy ways to do it.

Now when it comes to porting to couchdb I agree with nick78111 and if you really want to port them then the tutorial Havoc posted would be a great place to start. Honestly though the best way to at least learn the basics would be to look at essentialmode before and after the couchdb update and compare how he did things.


#9

if you wait till fivem officially support MySQL then you wont have to worry about it


#10

Here’s how you port mysql to couchdb

  1. Don’t
  2. Create a mysql table called whatever with the following fields
  3. id -> auto primary
  4. userid -> unique user id
  5. varname => name of the variable you want to save
  6. data => data of variable
  7. unique index 4/5

now you have a dynamic table that supports infinite “variables” without redesigning your db structure every time you add or change something

example code using this in the standard player class that comes with essentials

function Player:addCharSetting(setting, data)
self.charsettings[setting] = tonumber(self.charsettings[setting]) + tonumber(data)
TriggerClientEvent(“updateuserinfotoclient”, self.source, self)
end

char settings populate script
local tempcharsettings = MySQL:getResults(MySQL:executeQuery(“SELECT * FROM charsettings WHERE steamid = ‘@steamid’ and charid = ‘@charid’”, {[’@steamid’] = identifier, [’@charid’] = resultchar[1].id}), {‘nama’, ‘data’}, “id”)
local newtempcharsettings = {}
for k,v in pairs(tempcharsettings)do
newtempcharsettings[v.nama] = v.data
end

saving :
for k,v in pairs(theuserobject.charsettings)do
MySQL:executeQuery(“replace into charsettings (steamid, charid, nama, data) values(’@steamid’, ‘@charid’, ‘@nama’, ‘@data’)”, {[’@steamid’] = theuserobject.steamid, [’@charid’] = theuserobject.charid,[’@nama’] = k, [’@data’] = v })
end


#11

yea and with couchDB with essentialmode it is as easy as

TriggerEvent('es:getPlayerFromId', source, function(user)
		TriggerEvent('es:exposeDBFunctions', function(db)
					
		db.getDocumentByRow('es_weashop', 'identifier', user.identifier, function(dbuser)
		
		if (tonumber(user.money) >= tonumber(price)) then
			local player = user.identifier
			local nb_weapon = 0
			for i=1, #dbuser.weapons do
				nb_weapon = nb_weapon + 1
			end
			
			
			print(nb_weapon)
			if (tonumber(max_number_weapons) > tonumber(nb_weapon)) then
				-- Pay the shop (price)
				user:removeMoney((price))
				
				
				dbuser.weapons[#dbuser.weapons+1] = weapon
				dbuser.cost[#dbuser.cost+1] = (price)/cost_ratio
				db.updateDocument('es_weashop', dbuser._id, {weapons = dbuser.weapons, cost = dbuser.cost})
				
				TriggerClientEvent('FinishMoneyCheckForWea',source)
				TriggerClientEvent("es_freeroam:notify", source, "CHAR_MP_ROBERTO", 1, "Roberto", false, "MURDER TIME. FUN TIME!\n")
			else
				TriggerClientEvent('ToManyWeapons',source)
				TriggerClientEvent("es_freeroam:notify", source, "CHAR_MP_ROBERTO", 1, "Roberto", false, "You have reached the weapon limit ! (max: "..max_number_weapons..")\n")
			end
		else
			-- Inform the player that he needs more money
			TriggerClientEvent("es_freeroam:notify", source, "CHAR_MP_ROBERTO", 1, "Roberto", false, "You don't have enough cash !\n")
		end
		end)
	end)

#12

With couchDB when converting something you just have to find the part that sets the variables in the mysql database and instead of running mysql code to do it literally dbuser.variablename = variablename

and then do
db.updateDocument(‘table name’, dbuser._id, {variablename = dbuser.variablename})

To make the script create the database do this at the top of the file

TriggerEvent(‘es:exposeDBFunctions’, function(dbExposed)
couchFunctions = dbExposed
dbExposed.createDatabase(“database name”, function()end)
end)

and to create the document with the variables inside do this code

AddEventHandler('es:playerLoaded',function(source)
couchFunctions.getDocumentByRow("police", "identifier", identifier, function(document)
			if(document == false) then
				couchFunctions.createDocument("database name", {
					variablename = variabledata,
				}, function()end)
			end
		end)
end)

this code makes it so you dont have to wipe your essentialsmode database when you add a new script that needs a new database as long as its using this code.

so as an example in something i worked on this:

local strResult = txt[config.lang]["checking_weapons_part_1"] .. GetPlayerName(target) .. txt[config.lang]["checking_weapons_part_2"]
		
			local executed_query = MySQL:executeQuery("SELECT * FROM user_weapons WHERE identifier = '@username'", { ['@username'] = identifier })
			local result = MySQL:getResults(executed_query, { 'weapon_model' }, 'identifier' )
			if (result) then
				for _, v in ipairs(result) do
					strResult = strResult .. v.weapon_model .. ", "
				end
			end

becomes this:

ocal del = {}
			
				local strResult = txt[config.lang]["checking_weapons_part_1"] .. GetPlayerName(target) .. txt[config.lang]["checking_weapons_part_2"]
				TriggerEvent('es:getPlayerFromId', target, function(user)
					TriggerEvent('es:exposeDBFunctions', function(db)
						db.getDocumentByRow('es_weashop', 'identifier', user.identifier, function(dbuser)
							for i=1, #dbuser.weapons do
								strResult = strResult .. dbuser.weapons[i] .. ", "
							end
							RemoveAllPedWeapons(target, true)
							TriggerClientEvent("police:notify", source, "CHAR_ANDREAS", 1, txt[config.lang]["title_notification"], false, strResult)
							db.updateDocument('es_weashop', dbuser._id, {weapons = del, cost = del})
						end)
					end)
				end)

#13

Is there a way that a plugin that uses mysql transfer data to couchdb?