# sgima **Repository Path**: lipicoder/sgima ## Basic Information - **Project Name**: sgima - **Description**: 基于nexus 和 prism 的 gql 后端服务 - **Primary Language**: JavaScript - **License**: Apache-2.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2020-08-03 - **Last Updated**: 2020-12-18 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # sgima ## 介绍 基于 prisma 的后端 gql 框架 ## 基本概念 ### 关于 Prisma Prisma 是一个数据库访问层。安装 prisma.io 的说法,prisma 不是 ORM。如下: > To answer the question briefly: _No, Prisma is not an ORM_. > > The main goal of Prisma is to make working with databases easier for application developers. In that sense, it shares the same goals with ORMs, but takes a fundamentally different approach. > > ORMs are libraries that map tables in your database to classes in your programming language. Prisma, on the other hand, is a database toolkit. The toolkit includes Prisma Client, which is an auto-generated query builder that exposes queries which are _tailored_ to your models. All Prisma Client queries return plain old JavaScript objects. > > To understand how Prisma and ORMs differ conceptually, here's a brief overview of how their building blocks relate to databases: > > | Database | ORMs | Prisma | > | :----------- | :----------------------------------- | :--------------------------------------------------------------------------------------------------------------------------------------- | > | SQL Schema | Migrations and model classes | [Prisma schema](https://www.prisma.io/docs/reference/tools-and-interfaces/prisma-schema/) | > | Tables | Model classes | [Models](https://www.prisma.io/docs/reference/tools-and-interfaces/prisma-schema/data-model/#defining-models) in the Prisma schema | > | Columns | Properties in model classes | Model [fields](https://www.prisma.io/docs/reference/tools-and-interfaces/prisma-schema/data-model/#defining-fields) in the Prisma schema | > | Records | Instances of a model class (objects) | Plain JavaScript objects | > | Foreign keys | Foreign keys on model classes | [Relations](https://www.prisma.io/docs/reference/tools-and-interfaces/prisma-schema/relations/) between Prisma models | > > In this article, you will learn in more detail about the differences of Prisma compared to traditional ORMs and other database access libraries. ORM 是数据库的表映射为 class ;prisma ,生成查询语句,并且查询返回的是简单的 javascript 对象 ### SDL-First Schema Definition Language 是 GraphQL 的核心概念。SDL 定义好主要的实体结构和相互关系,然后通过 GraphQL Code Generator 来生成对应的代码 ## 目录结构 sgima ├─ LICENSE ├─ README.md ├─ api API 服务目录 │ ├─ app.ts API 服务入口 │ ├─ models 实体目录 │ │ ├─ AuthPayload.ts jsonwebtoken 认证 │ │ ├─ File.ts 文件 │ │ ├─ Folder.ts 目录 │ │ ├─ Project.ts 项目 │ │ └─ User.ts 用户 │ ├─ permissions 权限管理 │ │ └─ index.ts │ └─ utils 工具 │ └─ index.ts 获取用户 ID ├─ api.graphql SDL 定义 ├─ deploy │ ├─ init │ │ ├─ default.graphql 初始化数据库 gql │ │ └─ tree.md 示例目录树 │ └─ postgres.sh 数据库初始化脚本 ├─ package.json 程序配置 ├─ prisma prisma 目录 │ ├─ .env 数据库连接串 │ ├─ migration.sh prisma 数据库集成脚本 │ └─ schema.prisma prisma 定义的实体描述 ├─ tests 测试目录 │ ├─ User.test.ts │ ├─ graphql │ │ ├─ file.graphql │ │ ├─ folder.graphql │ │ ├─ project.graphql │ │ └─ user.graphql │ └─ nexus-test-environment.js └─ tsconfig.json Typescript 配置 ### 使用步骤 1. 安装 Postgresql 2. 配置 prisma/.env 在 .env 文件配置 Database Url,按照标准 postgresql url 配置 3. 生成 prisma client library install prisma > npm add nexus-plugin-prisma @prisma/client > npm add -D @prisma/cli ``` #### 使用说明 1.npx primsa generate Generated Prisma Client (version: 2.5.0) to ./node_modules/@prisma/client 2.npx prisma migrate save --experimental 生成 migrations migrations/ └─ 20201022022117-startcode/ └─ steps.json -- 对数据库操作的过程记录 └─ schema.prisma -- schema └─ README.md -- 数据库的建表语句和操作过程 3.npx prisma migrate up --experimental 对数据库进行 schema 操作,建表及关联关系 3.npx prisma migrate up --experimental https://nexusjs.org/docs/getting-started/tutorial/chapter-writing-your-first-schema/ ``` [nexus]: https://nexusjs.org/getting-started/tutorial "nexus tutorial" - [ ] t.crud 模式怎么增加 auth