schema-dsl 2.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.eslintignore +10 -0
- package/.eslintrc.json +27 -0
- package/.github/CODE_OF_CONDUCT.md +45 -0
- package/.github/ISSUE_TEMPLATE/bug_report.md +57 -0
- package/.github/ISSUE_TEMPLATE/config.yml +11 -0
- package/.github/ISSUE_TEMPLATE/feature_request.md +45 -0
- package/.github/ISSUE_TEMPLATE/question.md +31 -0
- package/.github/PULL_REQUEST_TEMPLATE.md +70 -0
- package/.github/SECURITY.md +184 -0
- package/.github/workflows/ci.yml +35 -0
- package/CHANGELOG.md +633 -0
- package/CONTRIBUTING.md +368 -0
- package/LICENSE +21 -0
- package/README.md +1122 -0
- package/STATUS.md +273 -0
- package/docs/FEATURE-INDEX.md +521 -0
- package/docs/INDEX.md +224 -0
- package/docs/api-reference.md +1098 -0
- package/docs/best-practices.md +672 -0
- package/docs/cache-manager.md +336 -0
- package/docs/design-philosophy.md +602 -0
- package/docs/dsl-syntax.md +654 -0
- package/docs/dynamic-locale.md +552 -0
- package/docs/error-handling.md +703 -0
- package/docs/export-guide.md +459 -0
- package/docs/faq.md +576 -0
- package/docs/frontend-i18n-guide.md +290 -0
- package/docs/i18n-user-guide.md +488 -0
- package/docs/label-vs-description.md +262 -0
- package/docs/markdown-exporter.md +398 -0
- package/docs/mongodb-exporter.md +279 -0
- package/docs/multi-type-support.md +319 -0
- package/docs/mysql-exporter.md +257 -0
- package/docs/plugin-system.md +542 -0
- package/docs/postgresql-exporter.md +290 -0
- package/docs/quick-start.md +761 -0
- package/docs/schema-helper.md +340 -0
- package/docs/schema-utils.md +492 -0
- package/docs/string-extensions.md +480 -0
- package/docs/troubleshooting.md +471 -0
- package/docs/type-converter.md +319 -0
- package/docs/type-reference.md +219 -0
- package/docs/validate.md +486 -0
- package/docs/validation-guide.md +484 -0
- package/examples/array-dsl-example.js +227 -0
- package/examples/custom-extension.js +85 -0
- package/examples/dsl-match-example.js +74 -0
- package/examples/dsl-style.js +118 -0
- package/examples/dynamic-locale-configuration.js +348 -0
- package/examples/dynamic-locale-example.js +287 -0
- package/examples/export-demo.js +130 -0
- package/examples/i18n-full-demo.js +310 -0
- package/examples/i18n-memory-safety.examples.js +268 -0
- package/examples/markdown-export.js +71 -0
- package/examples/middleware-usage.js +93 -0
- package/examples/password-reset/README.md +153 -0
- package/examples/password-reset/schema.js +26 -0
- package/examples/password-reset/test.js +101 -0
- package/examples/plugin-system.examples.js +205 -0
- package/examples/simple-example.js +122 -0
- package/examples/string-extensions.js +297 -0
- package/examples/user-registration/README.md +156 -0
- package/examples/user-registration/routes.js +92 -0
- package/examples/user-registration/schema.js +150 -0
- package/examples/user-registration/server.js +74 -0
- package/index.d.ts +1999 -0
- package/index.js +270 -0
- package/index.mjs +30 -0
- package/lib/adapters/DslAdapter.js +653 -0
- package/lib/adapters/index.js +20 -0
- package/lib/config/constants.js +286 -0
- package/lib/config/patterns/creditCard.js +9 -0
- package/lib/config/patterns/idCard.js +9 -0
- package/lib/config/patterns/index.js +8 -0
- package/lib/config/patterns/licensePlate.js +4 -0
- package/lib/config/patterns/passport.js +4 -0
- package/lib/config/patterns/phone.js +9 -0
- package/lib/config/patterns/postalCode.js +5 -0
- package/lib/core/CacheManager.js +376 -0
- package/lib/core/DslBuilder.js +740 -0
- package/lib/core/ErrorCodes.js +233 -0
- package/lib/core/ErrorFormatter.js +342 -0
- package/lib/core/JSONSchemaCore.js +347 -0
- package/lib/core/Locale.js +119 -0
- package/lib/core/MessageTemplate.js +89 -0
- package/lib/core/PluginManager.js +448 -0
- package/lib/core/StringExtensions.js +209 -0
- package/lib/core/Validator.js +316 -0
- package/lib/exporters/MarkdownExporter.js +420 -0
- package/lib/exporters/MongoDBExporter.js +162 -0
- package/lib/exporters/MySQLExporter.js +212 -0
- package/lib/exporters/PostgreSQLExporter.js +289 -0
- package/lib/exporters/index.js +24 -0
- package/lib/locales/en-US.js +65 -0
- package/lib/locales/es-ES.js +66 -0
- package/lib/locales/fr-FR.js +66 -0
- package/lib/locales/index.js +8 -0
- package/lib/locales/ja-JP.js +66 -0
- package/lib/locales/zh-CN.js +93 -0
- package/lib/utils/LRUCache.js +174 -0
- package/lib/utils/SchemaHelper.js +240 -0
- package/lib/utils/SchemaUtils.js +313 -0
- package/lib/utils/TypeConverter.js +245 -0
- package/lib/utils/index.js +13 -0
- package/lib/validators/CustomKeywords.js +203 -0
- package/lib/validators/index.js +11 -0
- package/package.json +70 -0
- package/plugins/custom-format.js +101 -0
- package/plugins/custom-validator.js +200 -0
|
@@ -0,0 +1,290 @@
|
|
|
1
|
+
# PostgreSQL 导出器文档
|
|
2
|
+
|
|
3
|
+
> **模块**: `lib/exporters/PostgreSQLExporter.js`
|
|
4
|
+
|
|
5
|
+
> **用途**: 将 JSON Schema 转换为 PostgreSQL CREATE TABLE 语句
|
|
6
|
+
|
|
7
|
+
---
|
|
8
|
+
|
|
9
|
+
## 📑 目录
|
|
10
|
+
|
|
11
|
+
- [概述](#概述)
|
|
12
|
+
- [快速开始](#快速开始)
|
|
13
|
+
- [API 参考](#api-参考)
|
|
14
|
+
- [配置选项](#配置选项)
|
|
15
|
+
- [完整示例](#完整示例)
|
|
16
|
+
- [类型映射](#类型映射)
|
|
17
|
+
- [CHECK 约束](#check-约束)
|
|
18
|
+
|
|
19
|
+
---
|
|
20
|
+
|
|
21
|
+
## 概述
|
|
22
|
+
|
|
23
|
+
`PostgreSQLExporter` 将 SchemaIO 生成的 JSON Schema 转换为 PostgreSQL 的 DDL 语句,支持丰富的 PostgreSQL 特性。
|
|
24
|
+
|
|
25
|
+
### 核心功能
|
|
26
|
+
|
|
27
|
+
- ✅ 生成 `CREATE TABLE` DDL 语句
|
|
28
|
+
- ✅ 自动类型映射(支持 JSONB、UUID 等)
|
|
29
|
+
- ✅ 自动生成 CHECK 约束
|
|
30
|
+
- ✅ 支持表和列的 COMMENT
|
|
31
|
+
- ✅ 支持多种索引类型(btree、hash、gin、gist)
|
|
32
|
+
- ✅ 支持 PostgreSQL schema 命名空间
|
|
33
|
+
|
|
34
|
+
---
|
|
35
|
+
|
|
36
|
+
## 快速开始
|
|
37
|
+
|
|
38
|
+
```javascript
|
|
39
|
+
const { dsl, exporters } = require('schema-dsl');
|
|
40
|
+
|
|
41
|
+
// 1. 定义 Schema
|
|
42
|
+
const userSchema = dsl({
|
|
43
|
+
id: 'uuid!',
|
|
44
|
+
username: 'string:3-32!',
|
|
45
|
+
email: 'email!',
|
|
46
|
+
age: 'number:18-120',
|
|
47
|
+
status: 'active|inactive'
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
// 2. 创建导出器
|
|
51
|
+
const exporter = new exporters.PostgreSQLExporter();
|
|
52
|
+
|
|
53
|
+
// 3. 导出为 PostgreSQL DDL
|
|
54
|
+
const ddl = exporter.export('users', userSchema);
|
|
55
|
+
console.log(ddl);
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
**输出**:
|
|
59
|
+
|
|
60
|
+
```sql
|
|
61
|
+
CREATE TABLE public.users (
|
|
62
|
+
id UUID NOT NULL,
|
|
63
|
+
username VARCHAR(32) NOT NULL CHECK (LENGTH(username) BETWEEN 3 AND 32),
|
|
64
|
+
email VARCHAR(255) NOT NULL,
|
|
65
|
+
age DOUBLE PRECISION CHECK (age BETWEEN 18 AND 120),
|
|
66
|
+
status VARCHAR(255) CHECK (status IN ('active', 'inactive')),
|
|
67
|
+
PRIMARY KEY (id)
|
|
68
|
+
);
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
---
|
|
72
|
+
|
|
73
|
+
## API 参考
|
|
74
|
+
|
|
75
|
+
### 构造函数
|
|
76
|
+
|
|
77
|
+
```javascript
|
|
78
|
+
new PostgreSQLExporter(options)
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
**参数**:
|
|
82
|
+
|
|
83
|
+
| 参数 | 类型 | 默认值 | 说明 |
|
|
84
|
+
|------|------|--------|------|
|
|
85
|
+
| `options.schema` | string | `'public'` | PostgreSQL schema 名称 |
|
|
86
|
+
|
|
87
|
+
### 方法
|
|
88
|
+
|
|
89
|
+
#### `export(tableName, jsonSchema)`
|
|
90
|
+
|
|
91
|
+
生成 PostgreSQL CREATE TABLE 语句。
|
|
92
|
+
|
|
93
|
+
```javascript
|
|
94
|
+
const ddl = exporter.export('users', userSchema);
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
**参数**:
|
|
98
|
+
- `tableName` (string): 表名
|
|
99
|
+
- `jsonSchema` (Object): JSON Schema 对象(必须是 object 类型)
|
|
100
|
+
|
|
101
|
+
**返回值**:
|
|
102
|
+
- `string`: PostgreSQL DDL 语句(包含 COMMENT 语句)
|
|
103
|
+
|
|
104
|
+
---
|
|
105
|
+
|
|
106
|
+
#### `generateIndex(tableName, columnName, options)`
|
|
107
|
+
|
|
108
|
+
生成索引创建语句。
|
|
109
|
+
|
|
110
|
+
```javascript
|
|
111
|
+
const indexDdl = exporter.generateIndex('users', 'email', {
|
|
112
|
+
unique: true,
|
|
113
|
+
method: 'btree'
|
|
114
|
+
});
|
|
115
|
+
console.log(indexDdl);
|
|
116
|
+
// CREATE UNIQUE INDEX idx_users_email ON public.users USING btree (email);
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
**参数**:
|
|
120
|
+
- `tableName` (string): 表名
|
|
121
|
+
- `columnName` (string): 列名
|
|
122
|
+
- `options.name` (string): 索引名(可选)
|
|
123
|
+
- `options.unique` (boolean): 是否唯一索引
|
|
124
|
+
- `options.method` (string): 索引方法(btree/hash/gin/gist)
|
|
125
|
+
|
|
126
|
+
---
|
|
127
|
+
|
|
128
|
+
#### `PostgreSQLExporter.export(tableName, jsonSchema)` (静态方法)
|
|
129
|
+
|
|
130
|
+
快速导出,无需实例化。
|
|
131
|
+
|
|
132
|
+
```javascript
|
|
133
|
+
const ddl = exporters.PostgreSQLExporter.export('users', userSchema);
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
---
|
|
137
|
+
|
|
138
|
+
## 配置选项
|
|
139
|
+
|
|
140
|
+
### Schema 命名空间
|
|
141
|
+
|
|
142
|
+
```javascript
|
|
143
|
+
// 使用自定义 schema
|
|
144
|
+
const exporter = new exporters.PostgreSQLExporter({
|
|
145
|
+
schema: 'myapp'
|
|
146
|
+
});
|
|
147
|
+
|
|
148
|
+
const ddl = exporter.export('users', userSchema);
|
|
149
|
+
// CREATE TABLE myapp.users (...);
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
---
|
|
153
|
+
|
|
154
|
+
## 完整示例
|
|
155
|
+
|
|
156
|
+
### 用户表 DDL 生成
|
|
157
|
+
|
|
158
|
+
```javascript
|
|
159
|
+
const { dsl, exporters } = require('schema-dsl');
|
|
160
|
+
|
|
161
|
+
// 定义用户 Schema(带描述)
|
|
162
|
+
const userSchema = dsl({
|
|
163
|
+
id: 'uuid!',
|
|
164
|
+
username: 'string:3-32!'
|
|
165
|
+
.description('用户登录名,3-32个字符'),
|
|
166
|
+
email: 'email!'
|
|
167
|
+
.description('用户邮箱地址'),
|
|
168
|
+
password: 'string:8-64!',
|
|
169
|
+
age: 'number:18-120',
|
|
170
|
+
profile: {
|
|
171
|
+
bio: 'string:500',
|
|
172
|
+
avatar: 'url'
|
|
173
|
+
},
|
|
174
|
+
status: 'active|inactive|banned',
|
|
175
|
+
createdAt: 'datetime!'
|
|
176
|
+
});
|
|
177
|
+
|
|
178
|
+
// 添加表描述
|
|
179
|
+
userSchema.description = '用户信息表';
|
|
180
|
+
|
|
181
|
+
// 生成 DDL
|
|
182
|
+
const exporter = new exporters.PostgreSQLExporter({ schema: 'app' });
|
|
183
|
+
const ddl = exporter.export('users', userSchema);
|
|
184
|
+
|
|
185
|
+
console.log(ddl);
|
|
186
|
+
|
|
187
|
+
// 生成索引
|
|
188
|
+
console.log(exporter.generateIndex('users', 'email', { unique: true }));
|
|
189
|
+
console.log(exporter.generateIndex('users', 'username', { unique: true }));
|
|
190
|
+
console.log(exporter.generateIndex('users', 'profile', { method: 'gin' }));
|
|
191
|
+
```
|
|
192
|
+
|
|
193
|
+
**输出**:
|
|
194
|
+
|
|
195
|
+
```sql
|
|
196
|
+
CREATE TABLE app.users (
|
|
197
|
+
id UUID NOT NULL,
|
|
198
|
+
username VARCHAR(32) NOT NULL CHECK (LENGTH(username) BETWEEN 3 AND 32),
|
|
199
|
+
email VARCHAR(255) NOT NULL,
|
|
200
|
+
password VARCHAR(64) NOT NULL CHECK (LENGTH(password) BETWEEN 8 AND 64),
|
|
201
|
+
age DOUBLE PRECISION CHECK (age BETWEEN 18 AND 120),
|
|
202
|
+
profile JSONB,
|
|
203
|
+
status VARCHAR(255) CHECK (status IN ('active', 'inactive', 'banned')),
|
|
204
|
+
createdAt TIMESTAMP NOT NULL,
|
|
205
|
+
PRIMARY KEY (id)
|
|
206
|
+
);
|
|
207
|
+
|
|
208
|
+
COMMENT ON TABLE app.users IS '用户信息表';
|
|
209
|
+
|
|
210
|
+
COMMENT ON COLUMN app.users.username IS '用户登录名,3-32个字符';
|
|
211
|
+
COMMENT ON COLUMN app.users.email IS '用户邮箱地址';
|
|
212
|
+
|
|
213
|
+
CREATE UNIQUE INDEX idx_users_email ON app.users USING btree (email);
|
|
214
|
+
CREATE UNIQUE INDEX idx_users_username ON app.users USING btree (username);
|
|
215
|
+
CREATE INDEX idx_users_profile ON app.users USING gin (profile);
|
|
216
|
+
```
|
|
217
|
+
|
|
218
|
+
---
|
|
219
|
+
|
|
220
|
+
## 类型映射
|
|
221
|
+
|
|
222
|
+
| JSON Schema 类型 | 格式/约束 | PostgreSQL 类型 |
|
|
223
|
+
|------------------|-----------|-----------------|
|
|
224
|
+
| `string` | - | `VARCHAR(255)` |
|
|
225
|
+
| `string` | `maxLength: 50` | `VARCHAR(50)` |
|
|
226
|
+
| `string` | `maxLength: 500` | `TEXT` |
|
|
227
|
+
| `string` | `format: email` | `VARCHAR(255)` |
|
|
228
|
+
| `string` | `format: uuid` | `UUID` |
|
|
229
|
+
| `string` | `format: date` | `DATE` |
|
|
230
|
+
| `string` | `format: date-time` | `TIMESTAMP` |
|
|
231
|
+
| `integer` | `maximum: 32767` | `SMALLINT` |
|
|
232
|
+
| `integer` | `maximum: 2147483647` | `INTEGER` |
|
|
233
|
+
| `integer` | - | `BIGINT` |
|
|
234
|
+
| `number` | - | `DOUBLE PRECISION` |
|
|
235
|
+
| `boolean` | - | `BOOLEAN` |
|
|
236
|
+
| `object` | - | `JSONB` |
|
|
237
|
+
| `array` | - | `JSONB` |
|
|
238
|
+
|
|
239
|
+
---
|
|
240
|
+
|
|
241
|
+
## CHECK 约束
|
|
242
|
+
|
|
243
|
+
PostgreSQLExporter 会自动为以下约束生成 CHECK 语句:
|
|
244
|
+
|
|
245
|
+
### 字符串长度约束
|
|
246
|
+
|
|
247
|
+
```javascript
|
|
248
|
+
username: 'string:3-32!'
|
|
249
|
+
// CHECK (LENGTH(username) BETWEEN 3 AND 32)
|
|
250
|
+
```
|
|
251
|
+
|
|
252
|
+
### 数值范围约束
|
|
253
|
+
|
|
254
|
+
```javascript
|
|
255
|
+
age: 'number:18-120'
|
|
256
|
+
// CHECK (age BETWEEN 18 AND 120)
|
|
257
|
+
```
|
|
258
|
+
|
|
259
|
+
### 枚举约束
|
|
260
|
+
|
|
261
|
+
```javascript
|
|
262
|
+
status: 'active|inactive|banned'
|
|
263
|
+
// CHECK (status IN ('active', 'inactive', 'banned'))
|
|
264
|
+
```
|
|
265
|
+
|
|
266
|
+
---
|
|
267
|
+
|
|
268
|
+
## 索引类型
|
|
269
|
+
|
|
270
|
+
| 方法 | 用途 | 示例 |
|
|
271
|
+
|------|------|------|
|
|
272
|
+
| `btree` (默认) | 通用索引,支持排序和范围查询 | 主键、外键、排序字段 |
|
|
273
|
+
| `hash` | 等值查询 | 精确匹配查询 |
|
|
274
|
+
| `gin` | JSON/数组索引 | JSONB 字段、全文搜索 |
|
|
275
|
+
| `gist` | 几何数据、范围类型 | 地理位置、IP 范围 |
|
|
276
|
+
|
|
277
|
+
```javascript
|
|
278
|
+
// GIN 索引用于 JSONB 字段
|
|
279
|
+
exporter.generateIndex('users', 'metadata', { method: 'gin' });
|
|
280
|
+
```
|
|
281
|
+
|
|
282
|
+
---
|
|
283
|
+
|
|
284
|
+
## 相关文档
|
|
285
|
+
|
|
286
|
+
- [数据库导出指南](export-guide.md)
|
|
287
|
+
- [MongoDB 导出器](mongodb-exporter.md)
|
|
288
|
+
- [MySQL 导出器](mysql-exporter.md)
|
|
289
|
+
- [TypeConverter](type-converter.md)
|
|
290
|
+
|