-
Notifications
You must be signed in to change notification settings - Fork 130
Add templates for Data table and columns #148
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 5 commits
31e09ca
0377171
d25b15b
bfb2fdb
8984e67
30aaacc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,5 @@ | ||
| node_modules | ||
| dist | ||
| .DS_Store | ||
| bun.lockb | ||
| tester/ | ||
| bun.lockb |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -540,7 +540,7 @@ export const addScriptsToPackageJson = ( | |
| "db:migrate": `tsx ${libPath}/db/migrate.ts`, | ||
| "db:drop": "drizzle-kit drop", | ||
| "db:pull": `drizzle-kit introspect:${driver}`, | ||
| ...(driver !== "pg" ? { "db:push": `drizzle-kit push:${driver}` } : {}), | ||
| "db:push": `drizzle-kit push:${driver}`, | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. remove the guardrails around db:push for pg, now that it's supported. |
||
| "db:studio": "drizzle-kit studio", | ||
| "db:check": `drizzle-kit check:${driver}`, | ||
| }; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -36,6 +36,7 @@ import { formatTableName, toCamelCase } from "../utils.js"; | |
| import { existsSync, readFileSync } from "fs"; | ||
| import { consola } from "consola"; | ||
| import { addToShadcnComponentList } from "../../add/utils.js"; | ||
| import eta from "../../../eta.js"; | ||
|
|
||
| export const scaffoldViewsAndComponentsWithServerActions = async ( | ||
| schema: ExtendedSchema | ||
|
|
@@ -80,6 +81,15 @@ export const scaffoldViewsAndComponentsWithServerActions = async ( | |
| createListComponent(schema) | ||
| ); | ||
|
|
||
| // create columns | ||
| createFile( | ||
| formatFilePath( | ||
| `app/(app)/${tableNameKebabCase}/columns.tsx`, | ||
| { prefix: "rootPath", removeExtension: false } | ||
| ), | ||
| eta.render('DataTable/columns.eta', { fields: schema.fields, tableNameKebabCase }) | ||
| ); | ||
|
|
||
| // create components/tableName/TableNameForm.tsx | ||
| createFile( | ||
| formatFilePath( | ||
|
|
@@ -251,7 +261,8 @@ const generateView = (schema: Schema) => { | |
| const relationsFormatted = formatRelations(relations); | ||
|
|
||
| return `import { Suspense } from "react"; | ||
|
|
||
| import { DataTable } from "${formatFilePath(`components/ui/DataTable`, {prefix: "alias", removeExtension: false})}"; | ||
| import { columns } from "./columns"; | ||
| import Loading from "${formatFilePath("app/loading", { | ||
| prefix: "alias", | ||
| removeExtension: false, | ||
|
|
@@ -313,6 +324,7 @@ const ${tableNameCapitalised} = async () => { | |
| .join(" ") | ||
| : "" | ||
| } /> | ||
| <DataTable data={${tableNameCamelCase}} columns={columns} /> | ||
| </Suspense> | ||
| ); | ||
| }; | ||
|
|
@@ -469,19 +481,6 @@ export default function ${tableNameSingularCapitalised}List({ | |
| + | ||
| </Button> | ||
| </div> | ||
| {optimistic${tableNamePluralCapitalised}.length === 0 ? ( | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. remove this list since it's taken care of by the table |
||
| <EmptyState openModal={openModal} /> | ||
| ) : ( | ||
| <ul> | ||
| {optimistic${tableNamePluralCapitalised}.map((${tableNameSingular}) => ( | ||
| <${tableNameSingularCapitalised} | ||
| ${tableNameSingular}={${tableNameSingular}} | ||
| key={${entityName}.id} | ||
| openModal={openModal} | ||
| /> | ||
| ))} | ||
| </ul> | ||
| )} | ||
| </div> | ||
| ); | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| import { Eta } from "eta" | ||
| import path from 'path'; | ||
| import { fileURLToPath } from 'url'; | ||
| import fs from 'fs'; | ||
|
|
||
| const __filename = fileURLToPath(import.meta.url); | ||
| const __dirname = path.dirname(__filename); | ||
| export const eta = new Eta({ views: path.join(__dirname, "templates") }); | ||
| export default eta; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,6 +20,7 @@ addCommonOptions(program.command("init")) | |
|
|
||
| program | ||
| .command("generate") | ||
| .alias("g") | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. alias 😎 |
||
| .description("Generate a new resource") | ||
| .action(buildSchema); | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| "use client" | ||
| import { MoreHorizontal } from "lucide-react" | ||
| import { | ||
| ColumnDef, | ||
| } from "@tanstack/react-table" | ||
| import { | ||
| DropdownMenu, | ||
| DropdownMenuContent, | ||
| DropdownMenuItem, | ||
| DropdownMenuLabel, | ||
| DropdownMenuTrigger, | ||
| } from "@/components/ui/dropdown-menu" | ||
| import { Button } from "@/components/ui/button" | ||
| import Link from 'next/link' | ||
|
|
||
| export const columns: ColumnDef<TData, TValue>[] = [ | ||
| <% it.fields.map(function (field) { %> | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. templating in action |
||
| { | ||
| accessorKey: "<%_ = field.name _%>", | ||
| header: "<%_ = field.name _%>", | ||
| enableHiding: false, | ||
| cell: ({ row }) => { | ||
| const resource = row.original | ||
| return ( | ||
| <Link href={`/<%= it.tableNameKebabCase %>/${resource.id}`}> | ||
| {resource.<%_ = field.name _%>} | ||
| </Link> | ||
| ) | ||
| }, | ||
| }, | ||
| <% }) %> | ||
| { | ||
| id: "actions", | ||
| enableHiding: false, | ||
| cell: ({ row }) => { | ||
| const resource = row.original | ||
| return ( | ||
| <DropdownMenu> | ||
| <DropdownMenuTrigger asChild> | ||
| <Button variant="ghost" className="square-8 p-0"> | ||
| <span className="sr-only">Open menu</span> | ||
| <MoreHorizontal className="square-4" /> | ||
| </Button> | ||
| </DropdownMenuTrigger> | ||
| <DropdownMenuContent align="end"> | ||
| <DropdownMenuLabel>Actions</DropdownMenuLabel> | ||
| <DropdownMenuItem>Edit</DropdownMenuItem> | ||
| <DropdownMenuItem>Delete</DropdownMenuItem> | ||
| </DropdownMenuContent> | ||
| </DropdownMenu> | ||
| ) | ||
| }, | ||
| }, | ||
| ] | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not sure if there is a better way to do this, but I figured this is at least a quick and dirty way to pull templates into the
distfolder.Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I personally used a npm package called
typescript-cpfor it and then just using the watch flag in dev and without it when building it.Edit: I'm also using npm-run-all to run the dev scripts in parallel. I dont like concurrently so I used npm-run-all.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👌