当前位置: 首页 > news >正文

贵阳企业网站制作seo是什么字

贵阳企业网站制作,seo是什么字,风中有朵雨做的云电影网站,西安高端网站制作​Prisma是一种流行的用于服务器端JavaScript和TypeScript的数据映射层(ORM)。它的核心目的是简化和自动化数据在存储和应用程序代码之间的传输方式。Prisma支持各种数据存储,并为数据持久化提供了一个强大而灵活的抽象层。通过这个基于代码的…

​Prisma是一种流行的用于服务器端JavaScript和TypeScript的数据映射层(ORM)。它的核心目的是简化和自动化数据在存储和应用程序代码之间的传输方式。Prisma支持各种数据存储,并为数据持久化提供了一个强大而灵活的抽象层。通过这个基于代码的导览,你可以对Prisma及其核心功能有一个初步了解。

JavaScript的ORM层

对象关系映射(ORM)最早由Java中的Hibernate框架引入。对象关系映射的最初目标是解决Java类和关系型数据库表之间的所谓阻抗不匹配问题。从这个想法发展出了更广泛的雄心勃勃的概念,即为应用程序提供一个通用的持久化层。Prisma是Java ORM层的现代JavaScript演进。Prisma支持多种SQL数据库,并扩展到包括NoSQL数据存储MongoDB。无论数据存储类型如何,总体目标仍然是为应用程序提供一个标准化的处理数据持久化的框架。

领域模型

我们将使用一个简单的领域模型来查看数据模型中的几种关系:多对一、一对多和多对多。(我们将跳过一对一,因为它与多对一非常相似。) Prisma使用一个模型定义(模式)作为应用程序和数据存储之间的枢纽。在构建应用程序时,我们可以采用一种方法,即从这个定义开始构建代码。Prisma会自动将模式应用到数据存储中。 Prisma模型定义格式并不难理解,你可以使用一个图形工具PrismaBuilder来创建一个。我们的模型将支持一个协作式的创意开发应用程序,所以我们将有User(用户)、Idea(创意)和Tag(标签)模型。一个User可以拥有多个Idea(一对多关系),而一个Idea只能有一个Owner(多对一关系)。User和Tag之间形成了多对多关系。列表1显示了模型定义。

列表1. Prisma中的模型定义

datasource db {provider = "sqlite"url      = "file:./dev.db"
}
​
generator client {provider = "prisma-client-js"
}
​
model User {id       Int      @id @default(autoincrement())name     Stringemail    String   @uniqueideas    Idea[]
}
​
model Idea {id          Int      @id @default(autoincrement())name        Stringdescription Stringowner       User     @relation(fields: [ownerId], references: [id])ownerId     Inttags        Tag[]
}
​
model Tag {id     Int    @id @default(autoincrement())name   String @uniqueideas  Idea[]
}

列表1包括一个数据源定义(一个简单的SQLite数据库,Prisma用于开发目的)和一个客户端定义,其中“generator client”设置为“prisma-client-js”。后者意味着Prisma将生成一个JavaScript客户端,应用程序可以使用该客户端与模型定义创建的映射进行交互。至于模型定义,注意每个模型都有一个id字段,并且我们使用Prisma的注解来获得自动递增的整数ID.id@default(autoincrement()) 为了在User和Idea之间创建关系,我们使用数组括号引用了Idea类型这表示:给我一个Idea的集合,属于该User。在关系的另一侧,你使用单个Idea来表示:owner User @relation(fields: [ownerId], references: [id])。User拥有一个owner字段,它引用了User的id字段。除了关系和主键ID字段之外,字段定义都很简单;例如,name是一个字符串字段,content是一个字符串字段,等等。

创造项目

我们将使用一个简单的项目来使用Prisma的功能。第一步是创建一个新的Node.js项目并添加依赖项。然后,我们可以添加列表1中的定义,并使用它来处理数据持久化,使用Prisma内置的SQLite数据库。为了启动我们的应用程序,我们将创建一个新的目录,初始化一个npm项目,并安装依赖项,如列表2所示。

列表2:创建应用程序

mkdir iw-prisma
cd iw-prisma
npm init -y
npm install express @prisma/client body-parser
mkdir prisma
touch prisma/schema.prisma

现在,在项目根目录下创建一个名为prisma的文件夹,并在其中创建一个名为schema.prisma的文件。将列表1中的模型定义复制到schema.prisma文件中。接下来,告诉Prisma使用模型定义来准备SQLite数据库的架构,如列表3所示。

列表3:设置数据库

npx prisma migrate dev --name init
npx prisma migrate deploy

列表3中的命令告诉Prisma要对数据库进行“迁移”,这意味着将Prisma定义中的模式更改应用到数据库本身。--preview-feature标志告诉Prisma使用开发配置文件,而--name标志为更改指定一个任意的名称。--apply标志告诉Prisma应用这些更改。

使用数据

现在,让我们在Express.js中创建一个RESTful端点来允许创建用户。可以在列表4中看到我们服务器的代码,它将放在server.js文件中。列表4是原始的Express代码,但是由于Prisma的存在,我们可以以最小的努力对数据库进行大量的操作。

列表4:展现代码

const express = require('express');
const bodyParser = require('body-parser');
const { PrismaClient } = require('@prisma/client');
​
const prisma = new PrismaClient();
const app = express();
app.use(bodyParser.json());
​
const port = 3000;
app.listen(port, () => {console.log(`Server is listening on port ${port}`);
});
​
// Fetch all users
app.get('/users', async (req, res) => {const users = await prisma.user.findMany();res.json(users);
});
​
// Create a new user
app.post('/users', async (req, res) => {const { name, email } = req.body;const newUser = await prisma.user.create({ data: { name, email } });res.status(201).json(newUser);
});

目前,我们只有两个端点,一个用于获取所有用户的列表,一个用于添加用户。你可以看到我们如何使用Prisma客户端来处理这些用例,分别调用prisma.user.findMany()prisma.user.create()prisma.user.findMany()方法没有任何参数,将返回数据库中的所有行。prisma.user.create()方法接受一个包含新行值的data字段的对象(在这种情况下,是name和email字段,记住Prisma会自动为我们创建一个唯一的ID)。现在,我们可以使用以下命令运行服务器:.node server.js

使用CURL进行测试

使用CURL命令来测试我们的端点,如列表5所示。

列表5:使用CURL尝试端点

$ curl http://localhost:3000/users
[]
$ curl -X POST -H "Content-Type: application/json" -d '{"name":"George Harrison","email":"george.harrison@example.com"}' http://localhost:3000/users
{"id":2,"name":"John Doe","email":"john.doe@example.com"}{"id":3,"name":"John Lennon","email":"john.lennon@example.com"}{"id":4,"name":"George Harrison","email":"george.harrison@example.com"}
$ curl http://localhost:3000/users
[{"id":2,"name":"John Doe","email":"john.doe@example.com"},{"id":3,"name":"John Lennon","email":"john.lennon@example.com"},{"id":4,"name":"George Harrison","email":"george.harrison@example.com"}]

列表5展示了我们获取所有用户并找到一个空集合,然后添加用户,最后获取填充的集合。接下来,让我们添加一个端点,让我们能够创建想法并将其与用户关联起来,如列表6所示。

列表6:用户想法的POST端点

app.post('/users/:userId/ideas', async (req, res) => {const { userId } = req.params;const { name, description } = req.body;
​try {const user = await prisma.user.findUnique({ where: { id: parseInt(userId) } });
​if (!user) {return res.status(404).json({ error: 'User not found' });}
​const idea = await prisma.idea.create({data: {name,description,owner: { connect: { id: user.id } },},});
​res.json(idea);} catch (error) {console.error('Error adding idea:', error);res.status(500).json({ error: 'An error occurred while adding the idea' });}
});
​
app.get('/userideas/:id', async (req, res) => {const { id } = req.params;const user = await prisma.user.findUnique({where: { id: parseInt(id) },include: {ideas: true,},});if (!user) {return res.status(404).json({ message: 'User not found' });}res.json(user);
});

在列表6中,我们有两个端点。第一个端点允许使用POST请求在/users/:userId/ideas路径下添加一个想法。首先,它需要通过ID来获取用户,使用prisma.user.findUnique()方法。这个方法用于根据传入的条件在数据库中查找单个实体。在我们的例子中,我们想要获取具有请求中的ID的用户,所以我们使用:{ where: { id: parseInt(userId) } }。一旦我们获取到用户,我们使用prisma.idea.create()来创建一个新的想法。这个方法的使用方式与创建用户时类似,但是现在我们有了一个关联字段。Prisma允许我们使用owner: { connect: { id: user.id } }来创建新的想法与用户之间的关联。第二个端点是一个GET请求,在/users/ideas/:id路径下。这个端点的目的是根据用户ID返回包括他们的想法在内的用户信息。这里使用了prisma.user.findUnique()方法的include选项来告诉Prisma包括关联的想法。如果没有这个选项,想法将不会被包括在返回结果中,因为Prisma默认使用延迟加载的策略来获取关联数据。为了测试这些新的端点,我们可以使用列表7中显示的CURL命令。

列表7:用于测试端点的CURL

$ curl -X POST -H "Content-Type: application/json" -d '{"name":"New Idea", "description":"Idea description"}' http://localhost:3000/users/3/ideas
​
$ curl http://localhost:3000/userideas/3
{"id":3,"name":"John Lennon","email":"john.lennon@example.com","ideas":[{"id":1,"name":"New Idea","description":"Idea description","ownerId":3},{"id":2,"name":"New Idea","description":"Idea description","ownerId":3}]}

我们能够添加想法并恢复带有想法的用户。

多对多关系与标签

现在让我们添加处理多对多关系中标签的端点。在列表8中,我们处理标签的创建并将其与想法关联起来。

列表8:添加和显示标签

// create a tag
app.post('/tags', async (req, res) => {const { name } = req.body;
​try {const tag = await prisma.tag.create({data: {name,},});
​res.json(tag);} catch (error) {console.error('Error adding tag:', error);res.status(500).json({ error: 'An error occurred while adding the tag' });}
});
​
// Associate a tag with an idea
app.post('/ideas/:ideaId/tags/:tagId', async (req, res) => {const { ideaId, tagId } = req.params;
​try {const idea = await prisma.idea.findUnique({ where: { id: parseInt(ideaId) } });
​if (!idea) {return res.status(404).json({ error: 'Idea not found' });}
​const tag = await prisma.tag.findUnique({ where: { id: parseInt(tagId) } });
​if (!tag) {return res.status(404).json({ error: 'Tag not found' });}
​const updatedIdea = await prisma.idea.update({where: { id: parseInt(ideaId) },data: {tags: {connect: { id: tag.id },},},});
​res.json(updatedIdea);} catch (error) {console.error('Error associating tag with idea:', error);res.status(500).json({ error: 'An error occurred while associating the tag with the idea' });}
});

我们已经添加了两个端点。第一个端点用于添加标签,在之前的示例中已经熟悉了。在列表8中,我们还添加了一个将想法与标签关联起来的端点。为了关联一个想法和一个标签,我们利用了模型定义中的多对多映射关系。我们通过ID获取想法和标签,并使用connect字段将它们设置在彼此之间。现在,想法的标签集合中包含了标签的ID,反之亦然。多对多关联允许最多两个一对多关系,每个实体指向另一个实体。在数据存储中,这需要创建一个“查找表”(或交叉引用表),但是Prisma会为我们处理这个过程。我们只需要与实体本身进行交互。我们多对多功能的最后一步是允许通过标签查找想法,并通过想法查找标签。你可以在列表9中看到模型的这一部分。(为了简洁起见,我删除了一些错误处理。)

列表9:通过想法找到标签,通过标签找到想法

// Display ideas with a given tag
app.get('/ideas/tag/:tagId', async (req, res) => {const { tagId } = req.params;
​try {const tag = await prisma.tag.findUnique({where: {id: parseInt(tagId)}});
​const ideas = await prisma.idea.findMany({where: {tags: {some: {id: tag.id}}}});
​res.json(ideas);} catch (error) {console.error('Error retrieving ideas with tag:', error);res.status(500).json({error: 'An error occurred while retrieving the ideas with the tag'});}
});
​
// tags on an idea:
app.get('/ideatags/:ideaId', async (req, res) => {const { ideaId } = req.params;try {const idea = await prisma.idea.findUnique({where: {id: parseInt(ideaId)}});
​const tags = await prisma.tag.findMany({where: {ideas: {some: {id: idea.id}}}});
​res.json(tags);} catch (error) {console.error('Error retrieving tags for idea:', error);res.status(500).json({error: 'An error occurred while retrieving the tags for the idea'});}
});

在这里,我们有两个端点:一个用于查找给定标签ID的标签,另一个用于查找给定想法ID的标签。它们的工作方式与在一对多关系中非常相似,Prisma会处理查找表的关联。例如,在查找想法上的标签时,我们使用findMany方法,并使用条件来查找具有相关ID的想法,如列表10所示。

列表10:测试标签概念的多对多关系

$ curl -X POST -H "Content-Type: application/json" -d '{"name":"Funny Stuff"}' http://localhost:3000/tags
​
$ curl -X POST http://localhost:3000/ideas/1/tags/2
{"idea":{"id":1,"name":"New Idea","description":"Idea description","ownerId":3},"tag":{"id":2,"name":"Funny Stuff"}}
​
$ curl localhost:3000/ideas/tag/2
[{"id":1,"name":"New Idea","description":"Idea description","ownerId":3}]
​
$ curl localhost:3000/ideatags/1
[{"id":1,"name":"New Tag"},{"id":2,"name":"Funny Stuff"}]

结论

虽然我们在这里介绍了一些CRUD和关系基础知识,但Prisma还具有更多的功能。它提供了级联操作,如级联删除;提供了获取策略,可以对从数据库返回的对象进行精细调整;支持事务、查询和过滤API等。Prisma还允许根据模型对数据库模式进行迁移。此外,它通过在框架中抽象出所有数据库客户端工作,使应用程序与数据库无关。Prisma提供了很多便利和强大的功能,只需定义和维护模型定义即可。很容易理解为什么这个JavaScript的ORM工具是开发者的热门选择。

作者:Matthew Tyson

更多技术干货请关注公众号“云原生数据库

squids.cn,目前可体验全网zui低价RDS,免费的迁移工具DBMotion、SQL开发工具等


文章转载自:
http://bania.bfmq.cn
http://bryozoan.bfmq.cn
http://hypericum.bfmq.cn
http://phonodeik.bfmq.cn
http://insouciance.bfmq.cn
http://hole.bfmq.cn
http://turtlet.bfmq.cn
http://cheerleading.bfmq.cn
http://divingde.bfmq.cn
http://catharine.bfmq.cn
http://chucker.bfmq.cn
http://prophylactic.bfmq.cn
http://careladen.bfmq.cn
http://tiro.bfmq.cn
http://resinification.bfmq.cn
http://thallium.bfmq.cn
http://aspartame.bfmq.cn
http://restriction.bfmq.cn
http://fivesome.bfmq.cn
http://prig.bfmq.cn
http://denominational.bfmq.cn
http://gorgonian.bfmq.cn
http://pantun.bfmq.cn
http://effluxion.bfmq.cn
http://rearwards.bfmq.cn
http://cutch.bfmq.cn
http://exudate.bfmq.cn
http://obsolescent.bfmq.cn
http://rubytail.bfmq.cn
http://falsification.bfmq.cn
http://ventriloquial.bfmq.cn
http://tunica.bfmq.cn
http://yugoslavian.bfmq.cn
http://rating.bfmq.cn
http://messidor.bfmq.cn
http://ghast.bfmq.cn
http://dogginess.bfmq.cn
http://cariama.bfmq.cn
http://sugarworks.bfmq.cn
http://unabated.bfmq.cn
http://terrine.bfmq.cn
http://waldo.bfmq.cn
http://hieroglyphologist.bfmq.cn
http://wog.bfmq.cn
http://mackinaw.bfmq.cn
http://platypusary.bfmq.cn
http://deflate.bfmq.cn
http://novel.bfmq.cn
http://coalport.bfmq.cn
http://hardcore.bfmq.cn
http://puppyish.bfmq.cn
http://quellenforschung.bfmq.cn
http://cablecast.bfmq.cn
http://rafter.bfmq.cn
http://franchiser.bfmq.cn
http://yaffil.bfmq.cn
http://otophone.bfmq.cn
http://groovy.bfmq.cn
http://yale.bfmq.cn
http://hypoproteinosis.bfmq.cn
http://candidacy.bfmq.cn
http://disoriented.bfmq.cn
http://androdioecious.bfmq.cn
http://kitbag.bfmq.cn
http://vsam.bfmq.cn
http://torun.bfmq.cn
http://ucsd.bfmq.cn
http://wooftah.bfmq.cn
http://triclinic.bfmq.cn
http://cary.bfmq.cn
http://configuration.bfmq.cn
http://derv.bfmq.cn
http://trencher.bfmq.cn
http://chitarrone.bfmq.cn
http://arbitrarily.bfmq.cn
http://eldritch.bfmq.cn
http://brekker.bfmq.cn
http://hectogram.bfmq.cn
http://differentiator.bfmq.cn
http://windgall.bfmq.cn
http://addie.bfmq.cn
http://sacristan.bfmq.cn
http://panlogism.bfmq.cn
http://luteotropic.bfmq.cn
http://pabouche.bfmq.cn
http://huggermugger.bfmq.cn
http://lubricator.bfmq.cn
http://transformer.bfmq.cn
http://triones.bfmq.cn
http://renewed.bfmq.cn
http://idiomorphically.bfmq.cn
http://sarcoplasma.bfmq.cn
http://nausea.bfmq.cn
http://crisscross.bfmq.cn
http://aborative.bfmq.cn
http://chamberer.bfmq.cn
http://wahhabi.bfmq.cn
http://toastmistress.bfmq.cn
http://jupon.bfmq.cn
http://fsb.bfmq.cn
http://www.dt0577.cn/news/115851.html

相关文章:

  • 网站怎么做收费百度一下电脑版首页网址
  • 深圳市外贸网站建设沈阳关键词优化报价
  • 如何建网站服务器网络营销的种类有哪些
  • 洛阳哪家网站做的好深圳google推广
  • 沈阳建设网站费用百度地图官网2022最新版下载
  • 建设设计网站公司网站如何网络推广自己的产品
  • 调用其他网站文章列表国际新闻报道
  • 小程序有做门户网站发布软文的平台有哪些
  • 虚拟主机空间免费网络网站推广优化
  • 网站设计分工今日新闻最新10条
  • 小说网站采集可以做嘛网络营销的特点有几个
  • 葫芦岛建设信息网站谷歌seo代运营
  • 土特产网站建设做seo需要投入的成本
  • 普陀学校网站建设网页设计制作网站
  • 做网站怎么引用字体营业推广经典案例
  • 帮做网站制作挣钱电脑优化软件排行榜
  • 上海做网站的的公司热门seo推广排名稳定
  • 房地产网站建设批发seo高端培训
  • 用vue做pc端网站好吗怎么制作网站详细流程
  • 新建幼儿园网站如何做google推广工具
  • 开发公司与物业公司的承包合同自助建站seo
  • 手机微网站 模板seo视频教学网站
  • 如何做招聘网站的方案app用户量排名
  • wordpress 404 apache南京seo整站优化技术
  • 网站建设怎么报价成都百度推广
  • 双wan路由器做网站接入艾滋病多久能检查出来
  • 高端建站骗局广州谷歌推广
  • 不备案网站怎么做淘宝客亿速云
  • 酷站是什么网站seo优化的内容有哪些
  • fms 视频网站建设互联网服务平台