Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 18 additions & 10 deletions src/commands/generate/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
DBField,
DBType,
DrizzleColumnType,
GenerateOptions,
ORMType,
PrismaColumnType,
} from "../../types.js";
Expand All @@ -26,7 +27,7 @@ import { scaffoldModel } from "./generators/model/index.js";

function provideInstructions() {
consola.info(
"Quickly generate your Model (Drizzle schema + queries / mutations), Controllers (API Routes and TRPC Routes), and Views",
"Quickly generate your Model (Drizzle schema + queries / mutations), Controllers (API Routes and TRPC Routes), and Views"
);
}

Expand Down Expand Up @@ -92,7 +93,7 @@ async function askForFields(orm: ORMType, dbType: DBType, tableName: string) {
const currentSchemas = getCurrentSchemas();

const baseFieldTypeChoices = Object.keys(
createOrmMappings()[orm][dbType].typeMappings,
createOrmMappings()[orm][dbType].typeMappings
)
.filter((field) => field !== "id")
.map((field) => {
Expand All @@ -105,7 +106,7 @@ async function askForFields(orm: ORMType, dbType: DBType, tableName: string) {
currentSchemas[0] === toCamelCase(tableName));
const fieldTypeChoices = removeReferenceOption
? baseFieldTypeChoices.filter(
(field) => field.name.toLowerCase() !== "references",
(field) => field.name.toLowerCase() !== "references"
)
: baseFieldTypeChoices;

Expand Down Expand Up @@ -203,7 +204,7 @@ export function preBuild() {
return true;
}

export async function buildSchema() {
export async function buildSchema(options?: GenerateOptions) {
const ready = preBuild();

if (!ready) return;
Expand All @@ -215,13 +216,20 @@ export async function buildSchema() {

if (orm !== null) {
provideInstructions();
const resourceType = await askForResourceType();
const tableName = await askForTable();
const fields = await askForFields(orm, driver, tableName);
const indexedField = await askForIndex(fields);
const resourceType = options.resourceTypes || (await askForResourceType());
const tableName = options.table || (await askForTable());
const fields =
options.fields || (await askForFields(orm, driver, tableName));
const indexedField =
typeof options.index === "string"
? options.index || null
: await askForIndex(fields);
let schema: Schema;
if (resourceType.includes("model") && auth !== null) {
const belongsToUser = await askIfBelongsToUser();
const belongsToUser =
typeof options.belongsToUser === "string"
? options.belongsToUser === "yes"
: await askIfBelongsToUser();
schema = {
tableName,
fields,
Expand All @@ -244,7 +252,7 @@ export async function buildSchema() {
scaffoldViewsAndComponents(schema);
} else {
consola.warn(
"You need to have an ORM installed in order to use the scaffold command.",
"You need to have an ORM installed in order to use the scaffold command."
);
addPackage();
}
Expand Down
18 changes: 18 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,24 @@ addCommonOptions(program.command("init"))
program
.command("generate")
.description("Generate a new resource")
.option("-r, --resourceTypes <types>", "Resource types", (types) => {
if (!types) return undefined;
const parts = types.split(",");
if (parts.length === 0) return undefined;
return parts;
})
.option("-t, --table <string>", "Table name")
.option("-f, --fields <fields>", "Fields in JSON format", (fields) => {
try {
if (!fields) return undefined;
return JSON.parse(fields);
} catch (error) {
console.error("Error parsing fields: ", error);
process.exit(1);
}
})
.option("-i, --index <string>", "Database index")
.option("-b, --belongsToUser <belongs-to-user>", "Belongs to user flag. yes/no", false)
.action(buildSchema);

addCommonOptions(program.command("add"))
Expand Down
16 changes: 8 additions & 8 deletions src/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,14 +110,14 @@ export type InitOptions = {
includeExample?: "yes" | "no";
};

// export type BuildOptions = {
// resources?: ("model" | "api_route" | "trpc_route" | "views_and_components")[];
// table?: string;
// belongsToUser?: "yes" | "no";
// index?: string;
// field?: DBField[];
// migrate?: "yes" | "no";
// };
export type GenerateOptions = {
resourceTypes?: ("model" | "api_route" | "trpc_route" | "views_and_components")[];
table?: string;
belongsToUser?: "yes" | "no";
index?: string;
fields?: DBField[];
migrate?: "yes" | "no";
};

export type ScaffoldSchema = {
tableName: string;
Expand Down