crea algunas funciones

pull/16/head
Borja Robert 2 years ago
parent 7e3684522f
commit a6cb842957

@ -1,7 +1,7 @@
/** @typedef {typeof import('better-sqlite3')} better-sqlite3 */ /** @typedef {typeof import('better-sqlite3')} better-sqlite3 */
import Database from 'better-sqlite3'; import Database from 'better-sqlite3';
export const db = new Database('./carteles_dev.sqlite', { fileMustExist: true, verbose: console.log }) export const db = new Database('./carteles_dev.sqlite', { verbose: console.log })
db.pragma("journal_mode = WAL"); db.pragma("journal_mode = WAL");
db.pragma("synchronous = normal"); db.pragma("synchronous = normal");
db.pragma("temp_store = memory"); db.pragma("temp_store = memory");

@ -0,0 +1,50 @@
import { db } from '$lib/db/db';
/** What all actions on DB return
* @typedef {Object} DBActionResult
* @property {string} [error]
* @property {string} [success]
*/
/** Adds user to the db
* @param {string} email
* @param {string} passwordHash
* @param {boolean} isAdmin
* @returns {DBActionResult} return
*/
export const addUserToDB = (email, passwordHash, isAdmin) => {
if (!email || !passwordHash || !isAdmin) {
return {
error: "Either email, password or isAdmin are missing"
};
}
const addUser = db.prepare(`INSERT INTO Users(email,password,isAdmin) VALUES(?,?,?);`)
const result = addUser.run(email, passwordHash, isAdmin);
if (result.changes === 1) {
return {
success: `User '${email}' added to DB`
}
}
return { error: `Could not add user '${email}' to DB`, };
}
/** Removes user from db
* @param {string} email
* @returns {DBActionResult} return
*/
export const delUserFromDB = (email) => {
if (!email) {
return {
error: "Email not provided"
}
}
const delUser = db.prepare(`DELETE FROM Users WHERE email=${email};`);
const result = delUser.run();
if (result.changes === 1) {
return {
success: `User '${email}' removed successfully`
}
}
return { error: `Could not remove user '${email}' from DB` }
}

@ -2,7 +2,7 @@
/** @type {number} */ export let templateIndex; /** @type {number} */ export let templateIndex;
/** @type {string} */ export let templateImage; /** @type {string} */ export let templateImage;
import { organizedBy, colabs, canvas, multiplier } from '$lib/stores/store'; import { organizedBy, colabs, canvas } from '$lib/stores/store';
import Heading from '$lib/components/Heading.svelte'; import Heading from '$lib/components/Heading.svelte';
import Title from '$lib/components/Title.svelte'; import Title from '$lib/components/Title.svelte';

Loading…
Cancel
Save