# @mrapi/dal usage documention
DAL is an upper application framework based on prisma 2. We are committed to quickly generating standardized CURD services, while also being able to switch flexibly under different tenants and different scenarios
# core
Multi-tenant support
Multiple prisma instance support
Database support
- PostgreSQL
- MySQL
- SQLite
Data cache
Multi-protocol call
- prismaClient
- graphql
- openAPI
- gRPC
Service access (log, monitoring, frequency control, etc.)
# install
npm install @mrapi/dal --save
# use
Take the most common usage as an example, HTTP Server.
# First, Configure the basic configuration file
// config/mrapi.config.js
exports.default = {
managementUrl: 'file:config/db/management.db',
}
2
3
4
Note: For other configuration items, please refer to DAL configuration items
# Second, Prepare for initialization
Configure DALOptions (@mrapi/cli may be used in the configuration process)
import { DALOptions } from '@mrapi/dal/lib/types'
const options: DALOptions = [
{
name: 'one',
defaultTenant: {
name: 'dev',
url: 'file:../config/db/dev.db', // 默认租户为此 db
},
},
{
name: 'two',
defaultTenant: {
url: 'file:../config/db/prod.db', // 默认租户为此 db
},
},
{
name: 'three',
defaultTenant: {
name: 'dev', // 默认租户为 management 表中标识为 dev 的 db
},
openAPI: {
enable: false, // 不启用 openAPI
},
},
]
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
Note: Each configuration must meet the requirements, please refer to the following [configuration](#Basic configuration) instructions for details
# Start DAL service
import DAL from '@mrapi/dal'
const options = ...
const app = new DAL(options)
app.start() // Default ip, port start service
2
3
4
5
6
# Fourth, Access service
All right! Start enjoying the convenience brought by DAL
🚀 Server ready at: http://0.0.0.0:1358
⭐️ [one] Running a GraphQL API route at: /graphql/one
⭐️ [one] Running a openAPI route at: /api/one
⭐️ [one] Running a openAPI Swagger document at: /api/one/swagger
2
3
4
5
Note: If you access the schema without defaultTenant, you need to set the requested tenantIdentity to be able to access the request normally!
# Basic configuration
Reference DAL basic configuration
# DALOptions
import { OptionsData } from 'express-graphql'
interface openAPIOptions {
dependencies?: {
[name: string]: Function | Promise<Function>
}
oasDir: string
validateApiDoc?: boolean
}
export interface DALOption {
name: string
nexusDir?: string
prismaClientDir?: string
defaultTenant?: {
name?: string
url?: string
}
graphql?: {
enable?: boolean
options?: OptionsData
}
openAPI?:
| {
enable?: true
options: openAPIOptions
}
| {
enable: false
options?: openAPIOptions
}
}
export type DALOptions = DALOption[]
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# name
Schema unique identifier
- Required
- Type:
string
- Reference: schema file name
# defaultTenant
Default tenant configuration information
- Type:
object
{
name?: string // 默认租户标识,对应于 management 表中的 db
url?: string // 默认租户的 db 连接地址
}
2
3
4
# prismaClientDir
prisma client
directory
- Type:
string
- Default:
mrapiConfig.outputDir
Note: \ The prisma client generated by mrapi/cli can directly use the default value
# nexusDir
nexus type
directory
- Type:
string
- Default:
path.join(mrapiConfig.outputDir, 'nexus-types')
**Note: \ The nexus type generated by mrapi/cli can directly use the default value **
# graphql
graphql
service configuration information
- Type:
object
{
enable?: boolean // 是否启用 graphql, 默认启用
options?: OptionsData // 参考 import { OptionsData } from 'express-graphql'
}
2
3
4
# openAPI
openAPI
service configuration information
- Type:
object
| {
enable?: true // 是否启用 openAPI, 默认启用
options: openAPIOptions
}
| {
enable: false
options?: openAPIOptions
}
openAPIOptions?: {
dependencies?: { // oas dependencies 方法扩展
[name: string]: Function | Promise<Function>
}
oasDir: string // oas 目录
validateApiDoc?: boolean // 是否校验 oas 文档
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# API
API methods provided externally after instantiation
# getPrisma = async (name: string, tenantName?: string) => prismaClient
- Async method
Through the schema
identifier and tenant
identifier, return the corresponding prisma client
. When tenantName
is empty, it will try to find the default value in the configuration to fill in
# hasSchema(name: string): boolean
whether the schema identifier exists
# getSchema(name: string): GraphQLSchema
Get @nexus/schema
# addSchema(name: string, option: DALSchemaOptions = {}): boolean
Add the DALSchemaOptions
configuration whose schema identifier is name, and the return value can be used to determine whether the addition is successful.
# removeSchema(name: string): boolean
Remove the related configuration whose schema identifier is name. The return value can be used to determine whether the removal is successful.
# async start(serverOptions?: ServerOption)
- Async method
Start service
export interface ServerOptions {
host?: string
port?: number
tenantIdentity?: string // 默认取用 mrapi.config.js 中的 tenantIdentity
}
2
3
4
5
# async stop()
- Async method
Out of service
# Instance object
Properties of instantiated objects
# server
DAL server instance object
app.start().then(() => {
const thisApp = app.server.app // 实际为 Express 实例
// 可以通过 thisApp 调用 Express 的能力
thisApp.use(cors()) // 注意:DAL 中已内置 cors 和 body-parser 插件,这里只是示例
})
2
3
4
5
6
Note: Only exists after instantiation after app.start().