| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394 |
- /**
- * 列出 Parse Server 所有表及其字段
- *
- * 用法: cd backend && npx tsx scripts/list-schemas.ts
- */
- import 'dotenv/config';
- import Parse from '../src/db/parse-client.js';
- interface FieldDef {
- type: string;
- targetClass?: string;
- }
- async function main(): Promise<void> {
- console.log('=== Parse Server 数据库 Schema 一览 ===\n');
- console.log(`Server: ${Parse.serverURL}`);
- console.log(`App ID: ${Parse.applicationId}\n`);
- // 测试连接
- try {
- await new Parse.Query('_User').limit(1).find({ useMasterKey: true });
- console.log('✅ Parse 连接正常\n');
- } catch (err: any) {
- console.error('❌ Parse 连接失败:', err.message);
- process.exit(1);
- }
- // 获取所有 schema
- const schemas = await Parse.Schema.all({ useMasterKey: true });
- console.log(`共 ${schemas.length} 个表:\n`);
- console.log('-'.repeat(90));
- for (const schema of schemas) {
- const className = schema.className;
- const fields = schema.fields as Record<string, FieldDef>;
- // 跳过内部系统表
- if (className.startsWith('_') && className !== '_User') {
- continue;
- }
- console.log(`\n📦 ${className}`);
- console.log(` 字段数: ${Object.keys(fields).length}`);
- const entries = Object.entries(fields)
- .filter(([name]) => !name.startsWith('_'))
- .sort(([a], [b]) => a.localeCompare(b));
- const stringFields: string[] = [];
- const numberFields: string[] = [];
- const pointerFields: string[] = [];
- const arrayFields: string[] = [];
- const dateFields: string[] = [];
- const booleanFields: string[] = [];
- const otherFields: string[] = [];
- for (const [name, def] of entries) {
- const type = def.type;
- if (type === 'String') {
- stringFields.push(name);
- } else if (type === 'Number') {
- numberFields.push(name);
- } else if (type === 'Pointer') {
- const target = def.targetClass ? ` → ${def.targetClass}` : '';
- pointerFields.push(`${name}${target}`);
- } else if (type === 'Array') {
- arrayFields.push(name);
- } else if (type === 'Date') {
- dateFields.push(name);
- } else if (type === 'Boolean') {
- booleanFields.push(name);
- } else {
- otherFields.push(`${name}(${type})`);
- }
- }
- if (stringFields.length) console.log(` String: ${stringFields.join(', ')}`);
- if (numberFields.length) console.log(` Number: ${numberFields.join(', ')}`);
- if (pointerFields.length) console.log(` Pointer: ${pointerFields.join(', ')}`);
- if (arrayFields.length) console.log(` Array: ${arrayFields.join(', ')}`);
- if (dateFields.length) console.log(` Date: ${dateFields.join(', ')}`);
- if (booleanFields.length) console.log(` Boolean: ${booleanFields.join(', ')}`);
- if (otherFields.length) console.log(` Other: ${otherFields.join(', ')}`);
- }
- console.log('\n' + '-'.repeat(90));
- console.log('\n完成!');
- }
- main().catch((err) => {
- console.error('失败:', err.message);
- process.exit(1);
- });
|