通过代码使用 CRUD 和查询之间有什么区别吗?
Is there any difference between using CRUD and Queries through code?
使用像这样通过 nodeJS 代码查询是否有任何显着差异:
const {Client} = require('pg');
const client = new Client ({
user:"postgres",
password:"123456",
host:"localhost",
port:5432,
database:"nodeapp"
})
client.connect()
.then(()=>console.log("Connected succsefully"))
.then(()=>client.query(`INSERT INTO public.users(id, name, phone) VALUES (3, 'sdasd', '012312132132323123');`)) //post vs query ?
.then(()=>client.query("SELECT * FROM users"))
.then((results)=>console.table(results.rows))
.catch((e)=>console.log(e))
.finally(()=>client.end())
或者这样的快递包裹CRUD:
app.post('/user',(req,res)=>{
const {body} = req,
{id,name,phone} = body,
q = `INSERT INTO public.users(
id, name, phone)
VALUES (${id}, '${name}', '${phone}');`
// debugger;
db.query(q,(err,dbRes)=>{
if(err)
res.status(400).send(err)
else
res.send({
id,name,phone
})
})
})
在这两个例子中我得到了相同的结果,我应该使用 CRUD 和 express 吗?这重要吗?
CRUD = 创建、读取、更新、删除
这些是您的数据库中常见的操作。您的第二个示例是一种远程执行这些操作的方法。
第二个示例使用 Express 公开端点 /user
,您可以访问该端点来完成查询。您提供数据的方式是使用 HTTP 请求的主体。
第一个示例不是动态的,它是一个硬编码查询,您只能从一个地方调用,只能做一件事。
您无法从任何地方(除了您的应用程序)访问第一个示例,因此如果您在其他地方需要它,则您不能。
此外,如果您没有在函数中获取查询,您以后也无法访问它。
- https://www.smashingmagazine.com/2018/01/understanding-using-rest-api/
- https://medium.com/@TebbaVonMathenstien/what-is-an-api-and-why-should-i-use-one-863c3365726b
- https://www.perforce.com/blog/qac/challenge-code-reuse-and-how-reuse-code-effectively
- https://restfulapi.net/http-methods/
- https://www.codecademy.com/articles/what-is-crud(什么是 CRUD?- 有用)
TL;DR:第二个选项类似于使用 import/export
,而不是导出方法,而是导出(公开)端点,然后 import
使用 HTTP 请求的端点: http://localhost:3000/v1/users/123
与 GET 方法相同:
function getUser(userId) {
// REALLY THIS IS YOUR MYSQL QUERY
return {
userId,
username: 'user'
}
}
const user = getUser(123);
console.log(user);
第一种方法是使用 Promises,第二种方法是使用回调。两种方法都可以。虽然,我在你的第二种方法中看到有一个 "db" 对象被重用(这是一个很好的做法)。不要一直重连,所有操作使用同一个连接
使用像这样通过 nodeJS 代码查询是否有任何显着差异:
const {Client} = require('pg');
const client = new Client ({
user:"postgres",
password:"123456",
host:"localhost",
port:5432,
database:"nodeapp"
})
client.connect()
.then(()=>console.log("Connected succsefully"))
.then(()=>client.query(`INSERT INTO public.users(id, name, phone) VALUES (3, 'sdasd', '012312132132323123');`)) //post vs query ?
.then(()=>client.query("SELECT * FROM users"))
.then((results)=>console.table(results.rows))
.catch((e)=>console.log(e))
.finally(()=>client.end())
或者这样的快递包裹CRUD:
app.post('/user',(req,res)=>{
const {body} = req,
{id,name,phone} = body,
q = `INSERT INTO public.users(
id, name, phone)
VALUES (${id}, '${name}', '${phone}');`
// debugger;
db.query(q,(err,dbRes)=>{
if(err)
res.status(400).send(err)
else
res.send({
id,name,phone
})
})
})
在这两个例子中我得到了相同的结果,我应该使用 CRUD 和 express 吗?这重要吗?
CRUD = 创建、读取、更新、删除
这些是您的数据库中常见的操作。您的第二个示例是一种远程执行这些操作的方法。
第二个示例使用 Express 公开端点 /user
,您可以访问该端点来完成查询。您提供数据的方式是使用 HTTP 请求的主体。
第一个示例不是动态的,它是一个硬编码查询,您只能从一个地方调用,只能做一件事。
您无法从任何地方(除了您的应用程序)访问第一个示例,因此如果您在其他地方需要它,则您不能。
此外,如果您没有在函数中获取查询,您以后也无法访问它。
- https://www.smashingmagazine.com/2018/01/understanding-using-rest-api/
- https://medium.com/@TebbaVonMathenstien/what-is-an-api-and-why-should-i-use-one-863c3365726b
- https://www.perforce.com/blog/qac/challenge-code-reuse-and-how-reuse-code-effectively
- https://restfulapi.net/http-methods/
- https://www.codecademy.com/articles/what-is-crud(什么是 CRUD?- 有用)
TL;DR:第二个选项类似于使用 import/export
,而不是导出方法,而是导出(公开)端点,然后 import
使用 HTTP 请求的端点: http://localhost:3000/v1/users/123
与 GET 方法相同:
function getUser(userId) {
// REALLY THIS IS YOUR MYSQL QUERY
return {
userId,
username: 'user'
}
}
const user = getUser(123);
console.log(user);
第一种方法是使用 Promises,第二种方法是使用回调。两种方法都可以。虽然,我在你的第二种方法中看到有一个 "db" 对象被重用(这是一个很好的做法)。不要一直重连,所有操作使用同一个连接