Skip to content

Commit 4fc020b

Browse files
committed
feat: add ast factory
1 parent effb8dd commit 4fc020b

File tree

14 files changed

+1864
-853
lines changed

14 files changed

+1864
-853
lines changed

packages/cli/src/actions/db.ts

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,28 @@ import { ZModelCodeGenerator } from '@zenstackhq/sdk';
33
import fs from 'node:fs';
44
import path from 'node:path';
55
import { execPackage } from '../utils/exec-utils';
6-
import { generateTempPrismaSchema, getSchemaFile, handleSubProcessError, loadSchemaDocumentWithServices } from './action-utils';
6+
import {
7+
generateTempPrismaSchema,
8+
getSchemaFile,
9+
handleSubProcessError,
10+
loadSchemaDocumentWithServices,
11+
} from './action-utils';
712
import { syncEnums, syncRelation, syncTable, type Relation } from './pull';
813
import { providers } from './pull/provider';
914
import { getDatasource } from './pull/utils';
15+
import { config } from '@dotenvx/dotenvx';
1016

1117
type PushOptions = {
1218
schema?: string;
1319
acceptDataLoss?: boolean;
1420
forceReset?: boolean;
1521
};
1622

17-
type PullOptions = {
23+
export type PullOptions = {
1824
schema?: string;
1925
out?: string;
26+
naming?: 'pascal' | 'camel' | 'snake' | 'kebab' | 'none';
27+
alwaysMap?: boolean;
2028
};
2129

2230
/**
@@ -63,62 +71,57 @@ async function runPush(options: PushOptions) {
6371
async function runPull(options: PullOptions) {
6472
const schemaFile = getSchemaFile(options.schema);
6573
const { model, services } = await loadSchemaDocumentWithServices(schemaFile);
66-
await import("@dotenvx/dotenvx/config")
67-
const SUPPORTED_PROVIDERS = ['sqlite', 'postgresql']
68-
const datasource = getDatasource(model)
74+
config();
75+
const SUPPORTED_PROVIDERS = ['sqlite', 'postgresql'];
76+
const datasource = getDatasource(model);
6977

7078
if (!datasource) {
71-
throw new Error('No datasource found in the schema.')
79+
throw new Error('No datasource found in the schema.');
7280
}
7381

7482
if (!SUPPORTED_PROVIDERS.includes(datasource.provider)) {
75-
throw new Error(`Unsupported datasource provider: ${datasource.provider}`)
83+
throw new Error(`Unsupported datasource provider: ${datasource.provider}`);
7684
}
7785

7886
const provider = providers[datasource.provider];
7987

8088
if (!provider) {
81-
throw new Error(
82-
`No introspection provider found for: ${datasource.provider}`
83-
)
89+
throw new Error(`No introspection provider found for: ${datasource.provider}`);
8490
}
8591

86-
const { enums, tables } = await provider.introspect(datasource.url)
92+
const { enums, tables } = await provider.introspect(datasource.url);
8793

8894
const newModel: Model = {
8995
$type: 'Model',
9096
$container: undefined,
9197
$containerProperty: undefined,
9298
$containerIndex: undefined,
93-
declarations: [...model.declarations.filter(d => ["DataSource"].includes(d.$type))],
99+
declarations: [...model.declarations.filter((d) => ['DataSource'].includes(d.$type))],
94100
imports: [],
95101
};
96102

103+
syncEnums({ dbEnums: enums, model: newModel, services, options });
97104

98-
syncEnums({ dbEnums: enums, model: newModel, services })
99-
100-
101-
102-
const resolvedRelations: Relation[] = []
105+
const resolvedRelations: Relation[] = [];
103106
for (const table of tables) {
104-
const relations = syncTable({ table, model: newModel, provider, services })
105-
resolvedRelations.push(...relations)
107+
const relations = syncTable({ table, model: newModel, provider, services, options });
108+
resolvedRelations.push(...relations);
106109
}
107110

108111
for (const relation of resolvedRelations) {
109-
syncRelation({ model: newModel, relation, services });
112+
syncRelation({ model: newModel, relation, services, options });
110113
}
111114

112115
//TODO: diff models and apply changes only
113116

114-
const generator = await new ZModelCodeGenerator();
117+
const generator = new ZModelCodeGenerator();
115118

116-
const zmodelSchema = await generator.generate(newModel)
119+
const zmodelSchema = generator.generate(newModel);
117120

118121
console.log(options.out ? `Writing to ${options.out}` : schemaFile);
119122

120123
const outPath = options.out ? path.resolve(options.out) : schemaFile;
121124
console.log(outPath);
122125

123-
fs.writeFileSync(outPath, zmodelSchema)
126+
fs.writeFileSync(outPath, zmodelSchema);
124127
}

0 commit comments

Comments
 (0)