Choosing a database shapes how your app stores and finds data. It changes how you build and organize your app.
When deciding how to save user info for a website
When planning how to handle lots of data quickly
When needing to support complex searches or relationships
When building an app that must work offline or sync data
When choosing tools that fit your team's skills and project needs
Syntax
Deno
No specific code syntax applies here since this is a concept about architecture and database choice.
Different databases use different ways to store data, like tables or documents.
Your app's code and structure often depend on the database style you pick.
Examples
This example shows connecting to a SQL database. SQL databases use tables and structured queries.
Deno
// Using a SQL database (PostgreSQL) with Deno
import { Client } from"https://deno.land/x/postgres/mod.ts";
const client = new Client({ user: "user", database: "db" });
await client.connect();
const result = await client.queryArray("SELECT * FROM users");
console.log(result.rows);
await client.end();
This example shows connecting to a NoSQL database. NoSQL stores data as flexible documents.
Deno
// Using a NoSQL database (MongoDB) with Deno
import { MongoClient } from"https://deno.land/x/mongo@v0.31.1/mod.ts";
const client = new MongoClient();
await client.connect("mongodb://localhost:27017");
const db = client.database("test");
const users = db.collection("users");
const allUsers = await users.find().toArray();
console.log(allUsers);
Sample Program
This Deno program uses a SQL database to create a table, add a message, and read messages. It shows how the database structure affects the code you write.
Deno
import { Client } from"https://deno.land/x/postgres/mod.ts";
async function run() {
const client = new Client({ user: "user", database: "testdb" });
await client.connect();
// Create a simple table
await client.queryArray(`
CREATE TABLE IF NOT EXISTS messages (
id SERIAL PRIMARY KEY,
text VARCHAR(100)
);
`);
// Insert a message
await client.queryArray("INSERT INTO messages (text) VALUES ($1)", ["Hello from Deno"]);
// Fetch messages
const result = await client.queryArray("SELECT * FROM messages");
console.log("Messages:", result.rows);
await client.end();
}
run();
OutputSuccess
Important Notes
SQL databases require defining tables and columns before storing data.
NoSQL databases let you store data more flexibly but may need different code patterns.
Choosing the right database helps your app run faster and be easier to build.
Summary
Database choice affects how you design your app's data and code.
SQL and NoSQL databases need different ways to store and get data.
Pick a database that fits your app's needs and your team's skills.
Practice
(1/5)
1. Why does the choice of database affect the architecture of a Deno application?
easy
A. Because Deno only supports one type of database
B. Because all databases use the same query language
C. Because different databases require different data models and query methods
D. Because database choice does not affect application design
Solution
Step 1: Understand database types
SQL databases use structured tables, while NoSQL databases use flexible document or key-value models.
Step 2: Connect database type to app design
The app must use different data models and queries depending on the database type chosen.
Final Answer:
Because different databases require different data models and query methods -> Option C
Quick Check:
Database type affects data model = D [OK]
Hint: Database type changes data structure and queries [OK]
Common Mistakes:
Thinking all databases use the same query language
2. Which of the following is the correct way to import a SQL client library in a Deno project?
easy
A. require('postgres');
B. import { Client } from "https://deno.land/x/postgres@v0.17.0/mod.ts";
C. import Client from 'postgres';
D. const Client = import('postgres');
Solution
Step 1: Recall Deno import syntax
Deno uses ES module syntax with URL imports, not CommonJS require.
Step 2: Identify correct import statement
import { Client } from "https://deno.land/x/postgres@v0.17.0/mod.ts"; uses correct ES module import with a URL and named import.
Final Answer:
import { Client } from "https://deno.land/x/postgres@v0.17.0/mod.ts"; -> Option B
Quick Check:
Deno uses ES module imports = A [OK]
Hint: Deno imports use URLs and ES module syntax [OK]
Common Mistakes:
Using CommonJS require syntax
Using bare module names without URLs
Using default import when named import is required
3. Given this Deno code snippet using a NoSQL database client, what will be the output?
const db = new NoSQLClient();
await db.insert({ id: 1, name: "Alice" });
const user = await db.find({ id: 1 });
console.log(user.name);
medium
A. Alice
B. undefined
C. Error: insert method not found
D. null
Solution
Step 1: Understand NoSQL insert and find
The insert method adds a document; find retrieves it by query.
Step 2: Trace code execution
Insert adds user with id 1 and name Alice; find returns that user; console logs 'Alice'.
Final Answer:
Alice -> Option A
Quick Check:
Inserted user found and logged = Alice [OK]
Hint: Insert then find returns inserted data [OK]
Common Mistakes:
Assuming find returns undefined if no await
Confusing insert with update method
Expecting error due to missing await
4. This Deno code tries to query a SQL database but throws an error. What is the likely cause?
const client = new Client();
await client.connect();
const result = await client.query('SELECT * FROM users WHERE id = $1', [1]);
console.log(result.rows[0].name);
medium
A. The console.log statement is incorrect
B. The query syntax is invalid for SQL
C. The query method does not support parameters
D. The Client constructor needs connection options
Solution
Step 1: Check Client initialization
SQL clients in Deno require connection details (host, user, password) in constructor.
Step 2: Analyze error cause
Without connection options, client.connect() fails, causing query error.
Final Answer:
The Client constructor needs connection options -> Option D
Quick Check:
Missing connection info causes error = A [OK]
Hint: SQL clients need connection info on creation [OK]
Common Mistakes:
Assuming query syntax is wrong
Thinking parameters are unsupported
Blaming console.log for error
5. You want to build a Deno app that handles complex transactions and relationships between data. Which database choice best fits this need and why?
hard
A. SQL database, because it supports ACID transactions and relational data
B. In-memory database, because it is fastest for all data types
C. NoSQL database, because it handles flexible schemas well
D. Flat file storage, because it is simplest to implement
Solution
Step 1: Identify app requirements
Complex transactions and relationships require strong data integrity and relations.
Step 2: Match database features
SQL databases support ACID transactions and relational models, ideal for this need.
Final Answer:
SQL database, because it supports ACID transactions and relational data -> Option A
Quick Check:
Complex relations need SQL ACID support = C [OK]
Hint: Use SQL for complex transactions and relations [OK]
Common Mistakes:
Choosing NoSQL for complex relational data
Assuming in-memory is best for persistence
Using flat files for transactional integrity
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.