-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexport-guide.ts
More file actions
108 lines (88 loc) · 4.68 KB
/
Copy pathexport-guide.ts
File metadata and controls
108 lines (88 loc) · 4.68 KB
1
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import { s, validate, MongoDBExporter, MySQLExporter, PostgreSQLExporter, MarkdownExporter } from '../../dist/pure.js'
// ============================================================
// Export guide — generate DDL and docs from schemas
//
// Exporters:
// MySQLExporter — CREATE TABLE DDL for MySQL
// PostgreSQLExporter — CREATE TABLE DDL for PostgreSQL
// MongoDBExporter — db.createCollection() validator command
// MarkdownExporter — human-readable API documentation
// ============================================================
// ============================================================
// 1. Define a reusable domain schema
// ============================================================
const userSchema = s({
id: s('uuid!').description('Primary key (UUID v4)'),
username: s('string:3-32!').description('Unique login handle'),
email: s('email!').description('Login email address'),
role: s('admin|user|guest').description('Access role'),
status: s('active|suspended|deleted').description('Account status'),
createdAt: s('datetime!').description('ISO-8601 creation timestamp'),
age: s('integer:18-120').description('User age in years'),
})
// ============================================================
// 2. MySQL DDL
// ============================================================
const mysqlExporter = new MySQLExporter()
const mysqlDdl = mysqlExporter.export('users', userSchema)
console.log('export-guide.mysql.hasCreate =', mysqlDdl.includes('CREATE TABLE')) // true
console.log('export-guide.mysql.hasId =', mysqlDdl.toLowerCase().includes('id')) // true
console.log('export-guide.mysql.hasEmail =', mysqlDdl.toLowerCase().includes('email')) // true
console.log('export-guide.mysql.length =', mysqlDdl.length > 100) // true
// ============================================================
// 3. PostgreSQL DDL
// ============================================================
const pgExporter = new PostgreSQLExporter({ schema: 'app' })
const pgDdl = pgExporter.export('users', userSchema)
console.log('export-guide.pg.hasCreate =', pgDdl.includes('CREATE TABLE')) // true
console.log('export-guide.pg.hasEmail =', pgDdl.toLowerCase().includes('email')) // true
// Multiple tables in one pass
const productSchema = s({
id: 'uuid!',
name: s('string:2-200!').description('Product display name'),
price: s('number:0.01-!').description('Unit price'),
stock: s('integer:0-!').description('Current inventory'),
active: 'boolean',
})
const pgTables = ['users', 'products'].map((tbl, i) =>
pgExporter.export(tbl, [userSchema, productSchema][i]!),
)
console.log('export-guide.pg.tables.count =', pgTables.length) // 2
console.log('export-guide.pg.tables.allHaveCreate =',
pgTables.every(t => t.includes('CREATE TABLE'))) // true
// ============================================================
// 4. MongoDB validator command
// ============================================================
const mongoExporter = new MongoDBExporter({ strict: true })
const mongoCmd = mongoExporter.generateCommand('users', userSchema)
console.log('export-guide.mongo.hasCreate =', mongoCmd.includes('db.createCollection')) // true
console.log('export-guide.mongo.length =', mongoCmd.length > 100) // true
// ============================================================
// 5. Markdown API docs
// ============================================================
const mdDoc = MarkdownExporter.export(userSchema as any, {
title: 'User API Reference',
locale: 'en-US',
})
console.log('export-guide.markdown.hasTitle =', mdDoc.includes('User API Reference')) // true
console.log('export-guide.markdown.hasEmail =', mdDoc.toLowerCase().includes('email')) // true
console.log('export-guide.markdown.isString =', typeof mdDoc) // 'string'
// Chinese docs (column headers will be in Chinese due to zh-CN locale)
const zhDoc = MarkdownExporter.export(userSchema as any, {
title: 'User API Docs',
locale: 'zh-CN',
})
console.log('export-guide.markdown.zh.title =', zhDoc.includes('User API Docs')) // true
// ============================================================
// 6. Still validates correctly — exporters are non-destructive
// ============================================================
const testResult = validate(userSchema, {
id: '123e4567-e89b-12d3-a456-426614174000',
username: 'alice_99',
email: 'alice@example.com',
role: 'user',
status: 'active',
createdAt: '2024-01-15T12:00:00Z',
age: 28,
})
console.log('export-guide.validate.stillOk =', testResult.valid) // true