-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlearn_db-analyzer.md.Cw3-H0Lq.js
11 lines (11 loc) · 6.96 KB
/
learn_db-analyzer.md.Cw3-H0Lq.js
1
2
3
4
5
6
7
8
9
10
11
import{_ as a,c as e,o as s,a9 as t}from"./chunks/framework.DgZLXPSQ.js";const m=JSON.parse('{"title":"How DB Analyzer works?","description":"","frontmatter":{},"headers":[],"relativePath":"learn/db-analyzer.md","filePath":"learn/db-analyzer.md"}'),i={name:"learn/db-analyzer.md"},n=t(`<h1 id="how-db-analyzer-works" tabindex="-1">How DB Analyzer works? <a class="header-anchor" href="#how-db-analyzer-works" aria-label="Permalink to "How DB Analyzer works?""></a></h1><p class="description"> Axe API is not just a passive framework that expects all the definitions from the developer. It is a very creative framework that helps developers to reduce mistakes in the development period. In this section, we are going to talk about the DB analyzer. </p><ul class="intro"><li>You will learn</li><li>What is DB Analyzer?</li><li>What DB Analyzer works?</li><li>What is the metadata?</li></ul><h2 id="analyzing-the-schema" tabindex="-1">Analyzing the schema <a class="header-anchor" href="#analyzing-the-schema" aria-label="Permalink to "Analyzing the schema""></a></h2><p>Axe API fetches the database schema structure in the <strong><em>initialization process</em></strong>, and keeps it in the memory to understand what kind of tables and data structures you have. It is named <strong>DB Analyzer</strong>.</p><p>It is designed to help developers to build the API with minimum errors.</p><p><a href="https://github.com/knex/knex-schema-inspector" target="_blank" rel="noreferrer">knex-schema-inspector</a> is used under the hood. Since <a href="https://knexjs.org/guide/" target="_blank" rel="noreferrer">knex</a> supports almost every modern relational database, it works perfectly for all of them.</p><h2 id="errors" tabindex="-1">Errors <a class="header-anchor" href="#errors" aria-label="Permalink to "Errors""></a></h2><p>Since the database schema is kept in memory, <strong><em>DB Analyzer</em></strong> checks your model definitions while you are developing the API.</p><p>DB Analyzers throws an error if you define something in your model, which is not found on the database schema.</p><p>Let's assume that you have a model like this;</p><div class="vp-code-group vp-adaptive-theme"><div class="tabs"><input type="radio" name="group-wsPlC" id="tab-jRn_Uz-" checked><label for="tab-jRn_Uz-">User.ts</label></div><div class="blocks"><div class="language-ts vp-adaptive-theme active"><button title="Copy Code" class="copy"></button><span class="lang">ts</span><pre class="shiki shiki-themes github-light github-dark has-focused-lines vp-code" tabindex="0"><code><span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">import</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> { Model } </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">from</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> "axe-api"</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">;</span></span>
<span class="line"></span>
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">class</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> User</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> extends</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> Model</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> {</span></span>
<span class="line"><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> fillable</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">() {</span></span>
<span class="line has-focus"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> return</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> [</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">"name"</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">, </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">"surnamex"</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">]; </span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> }</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">}</span></span>
<span class="line"></span>
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">export</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> default</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> User;</span></span></code></pre></div></div></div><p>You would get the following error in your terminal if the <code>users</code> table doesn't have a column like <code>surnamex</code>.</p><div class="language-bash vp-adaptive-theme"><button title="Copy Code" class="copy"></button><span class="lang">bash</span><pre class="shiki shiki-themes github-light github-dark vp-code" tabindex="0"><code><span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">[UNDEFINED_COLUMN]</span></span>
<span class="line"></span>
<span class="line"><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">User</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> model</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> doesn't have the following columns on the database; "users.surnamex"</span></span></code></pre></div><p>DB Analyzer checks every table and column definition.</p><p>Also, in the HTTP request handling process, it checks the column names if they are actually defined. That way, you won't get an undefined column error from the database server.</p><h2 id="documentation" tabindex="-1">Documentation <a class="header-anchor" href="#documentation" aria-label="Permalink to "Documentation""></a></h2><p>Thanks to DB Analyzer, Axe API provides the correct database schema structure to display <strong><em>auto-created documentation</em></strong>.</p><p>You can use the following endpoint to get all <strong><em>API metadata</em></strong>, including the database schema.</p><div class="language-bash vp-adaptive-theme"><button title="Copy Code" class="copy"></button><span class="lang">bash</span><pre class="shiki shiki-themes github-light github-dark vp-code" tabindex="0"><code><span class="line"><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">GET</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> /metadata</span></span></code></pre></div><p>Also, you can use the following link to get metadata of the example Axe API project (Bookstore API):</p><p><a href="https://bookstore.axe-api.com/metadata" target="_blank" rel="noreferrer">bookstore.axe-api.com/metadata</a></p><h2 id="next-step" tabindex="-1">Next step <a class="header-anchor" href="#next-step" aria-label="Permalink to "Next step""></a></h2><p>In this section, we covered what is the DB Analyzer.</p><p>In the next chapter, we are going to talk about more advanced topics such as configuration, security, transactions, etc.</p>`,25),l=[n];function o(r,h,p,d,c,k){return s(),e("div",null,l)}const g=a(i,[["render",o]]);export{m as __pageData,g as default};