view src/lib/Config.luan @ 13:af2b95854072

add server records
author Franklin Schmidt <fschmidt@gmail.com>
date Mon, 30 Oct 2023 21:32:42 -0600
parents 43814e9f5802
children 80105b716a62
line wrap: on
line source

local Luan = require "luan:Luan.luan"
local error = Luan.error
local parse = Luan.parse or error()
local Db = require "site:/lib/Db.luan"


local Config = {}

local function get()
	local doc = Db.get_document("type:config")
	return doc and parse(doc.config)
end

function Config.get()
	return get() or error "config not set"
end

function Config.get_for_config()
	local config = get() or {}
	config.discord = config.discord or {}
	config.discord.client_id = config.discord.client_id or "replace"
	config.discord.client_secret = config.discord.client_secret or "replace"
	return config
end

function Config.set(text)
	parse(text)  -- validate
	Db.run_in_transaction( function()
		local doc = Db.get_document("type:config") or {type="config"}
		doc.config = text
		Db.save(doc)
	end )
end

return Config