Explorar o código

feat: Avoid returning in queries

marvin-wtt hai 1 ano
pai
achega
64a136dd11

+ 0 - 2
server/handlers/domains.handler.js

@@ -34,8 +34,6 @@ async function remove(req, res) {
     { user_id: null }
   );
 
-  redis.remove.domain(domain);
-
   if (!domain) {
     throw new CustomError("Could not delete the domain.", 500);
   }

+ 38 - 29
server/queries/domain.queries.js

@@ -6,9 +6,9 @@ async function find(match) {
     const cachedDomain = await redis.client.get(redis.key.domain(match.address));
     if (cachedDomain) return JSON.parse(cachedDomain);
   }
-  
+
   const domain = await knex("domains").where(match).first();
-  
+
   if (domain) {
     redis.client.set(
       redis.key.domain(domain.address),
@@ -17,7 +17,7 @@ async function find(match) {
       60 * 60 * 6
     );
   }
-  
+
   return domain;
 }
 
@@ -27,44 +27,53 @@ function get(match) {
 
 async function add(params) {
   params.address = params.address.toLowerCase();
-  const exists = await knex("domains").where("address", params.address).first();
-  
+
+  let { id } = await knex("domains")
+    .where("address", params.address)
+    .first();
+
   const newDomain = {
     address: params.address,
-    homepage: params.homepage || null,
-    user_id: params.user_id || null,
-    banned: !!params.banned
+    homepage: params.homepage,
+    user_id: params.user_id,
+    banned: !!params.banned,
+    banned_by_id: params.banned_by_id
   };
-  
-  let domain;
-  if (exists) {
-    const [response] = await knex("domains")
-      .where("id", exists.id)
-      .update(
-        {
-          ...newDomain,
-          updated_at: params.updated_at || new Date().toISOString()
-        },
-        "*"
-      );
-    domain = response;
+
+  if (id) {
+    await knex("domains")
+      .where("id", id)
+      .update({
+        ...newDomain,
+        updated_at: params.updated_at || new Date().toISOString()
+      });
   } else {
-    const [response] = await knex("domains").insert(newDomain, "*");
-    domain = response;
+    // Mysql and sqlite don't support returning but return the inserted id by default
+    [id] = await knex("domains")
+      .insert(newDomain)
+      .returning("id");
   }
-  
+
+  // Query domain instead of using returning as sqlite and mysql don't support it
+  const domain = await knex("domains")
+    .where("id", id);
+
   redis.remove.domain(domain);
-  
+
   return domain;
 }
 
 async function update(match, update) {
-  const domains = await knex("domains")
+  await knex("domains")
     .where(match)
-    .update({ ...update, updated_at: new Date().toISOString() }, "*");
-  
+    .update({ ...update, updated_at: new Date().toISOString() });
+
+  const domains = await knex("domains")
+    .select("*")
+    .where(match);
+
   domains.forEach(redis.remove.domain);
-  
+
   return domains;
 }
 

+ 18 - 16
server/queries/host.queries.js

@@ -21,41 +21,43 @@ async function find(match) {
   }
 
   return host;
-};
+}
 
 async function add(params) {
   params.address = params.address.toLowerCase();
 
-  const exists = await knex("hosts")
+  let { id } = await knex("hosts")
     .where("address", params.address)
     .first();
 
   const newHost = {
     address: params.address,
-    banned: !!params.banned
+    banned: !!params.banned,
+    banned_by_id: params.banned_by_id,
   };
 
-  let host;
-  if (exists) {
-    const [response] = await knex("hosts")
-      .where("id", exists.id)
-      .update(
-        {
+  if (id) {
+    await knex("hosts")
+      .where("id", id)
+      .update({
           ...newHost,
           updated_at: params.updated_at || new Date().toISOString()
-        },
-        "*"
-      );
-    host = response;
+      });
   } else {
-    const [response] = await knex("hosts").insert(newHost, "*");
-    host = response;
+    // Mysql and sqlite don't support returning but return the inserted id by default
+    [id] = await knex("hosts")
+      .insert(newHost)
+      .returning('*');
   }
 
+  // Query domain instead of using returning as sqlite and mysql don't support it
+  const host = await knex("hosts")
+    .where("id", id);
+
   redis.remove.host(host);
 
   return host;
-};
+}
 
 module.exports = {
   add,

+ 9 - 5
server/queries/link.queries.js

@@ -40,7 +40,7 @@ function normalizeMatch(match) {
   }
 
   return newMatch;
-};
+}
 
 async function total(match, params) {
   let query = knex("links");
@@ -137,7 +137,7 @@ async function remove(match) {
   
   if (!link) {
     return { isRemoved: false, error: "Could not find the link.", link: null }
-  };
+  }
 
   const deletedLink = await knex("links").where("id", link.id).delete();
   redis.remove.link(link);
@@ -167,10 +167,14 @@ async function update(match, update) {
     update.password = await bcrypt.hash(update.password, salt);
   }
   
-  const links = await knex("links")
+  await knex("links")
     .where(match)
-    .update({ ...update, updated_at: new Date().toISOString() }, "*");
-  
+    .update({ ...update, updated_at: new Date().toISOString() });
+
+  const links = await knex("links")
+    .select('*')
+    .where(match);
+
   links.forEach(redis.remove.link);
   
   return links;

+ 5 - 8
server/queries/user.queries.js

@@ -53,23 +53,20 @@ async function add(params, user) {
 
 async function update(match, update, increments = []) {
   const query = knex("users");
-  
+
   Object.entries(match).forEach(([key, value]) => {
     query.andWhere(key, ...(Array.isArray(value) ? value : [value]));
   });
 
-  let updateQuery = query;
+  let updateQuery = query.clone();
   increments.forEach(columnName => {
     updateQuery.increment(columnName);
   });
   
-  await updateQuery.update(
-    { ...update, updated_at: new Date().toISOString() },
-    "*"
-  );
+  await updateQuery.update({ ...update, updated_at: new Date().toISOString() });
+
+  const users = await query.select('*');
 
-  const users = await query.all();
-  
   users.forEach(redis.remove.user);
   
   return users;