CLI can add, list, delete colors

This commit is contained in:
Ryan Pandya 2024-11-22 12:38:03 -08:00
parent de9dca5274
commit 47e8371fb3
5 changed files with 51 additions and 17 deletions

View File

@ -21,14 +21,14 @@ colorsCmd
try { try {
const colors = (await api.colors.list.query()).colors; const colors = (await api.colors.list.query()).colors;
colors.sort((a, b) => b.numCategories - a.numCategories); // colors.sort((a, b) => b.numCategories - a.numCategories);
if (getGlobalOptions().json) { if (getGlobalOptions().json) {
printObject(colors); printObject(colors);
} else { } else {
const data: string[][] = [["Id", "Name", "Hexcode", "Num categories"]]; const data: string[][] = [["Id", "Name", "Hexcode", "user"]];
colors.forEach((color) => { colors.forEach((color) => {
data.push([color.id, color.name, color.hexcode, color.numCategories.toString()]); data.push([color.id, color.name, color.hexcode, color.userId]);
}); });
console.log( console.log(
data.length <= 1 ? data.length <= 1 ?
@ -61,6 +61,21 @@ colorsCmd
.catch(printError(`Failed to create the color "${name}"`)); .catch(printError(`Failed to create the color "${name}"`));
}); });
colorsCmd
.command("delete")
.description("delete a color")
.argument("<name>", "the name of the color")
.action(async (name: string) => {
const api = getAPIClient();
await api.colors.delete
.mutate({
colorName: name,
})
.then(printSuccess(`Successfully deleted the color "${name}"`))
.catch(printError(`Failed to delete the color "${name}"`));
});
// colorsCmd // colorsCmd
// .command("delete") // .command("delete")
// .description("delete a color") // .description("delete a color")

View File

@ -139,7 +139,11 @@ export const colors = sqliteTable(
.references(() => users.id, { onDelete: "cascade" }), .references(() => users.id, { onDelete: "cascade" }),
}, },
(c) => ({ (c) => ({
uniq: unique().on(c.userId, c.name) // Name should be unique per user
uniq: unique().on(c.userId, c.name),
// Hexcode should start with a pound sign, or have one prepended
// TODO. No internet, so no chatGPT cheating nor regular google looking up,
// and fuck if the in built documentation makes any sense
}), }),
); );
export const categories = sqliteTable( export const categories = sqliteTable(

View File

@ -18,6 +18,7 @@
"drizzle-orm": "^0.33.0", "drizzle-orm": "^0.33.0",
"superjson": "^2.2.1", "superjson": "^2.2.1",
"tiny-invariant": "^1.3.3", "tiny-invariant": "^1.3.3",
"title-case": "^4.3.2",
"zod": "^3.23.8" "zod": "^3.23.8"
}, },
"devDependencies": { "devDependencies": {

View File

@ -9,26 +9,23 @@ import {
} from "@lifetracker/shared/types/colors"; } from "@lifetracker/shared/types/colors";
import type { Context } from "../index"; import type { Context } from "../index";
import { authedProcedure, router } from "../index"; import { authedProcedure, router } from "../index";
import { titleCase } from "title-case";
function conditionFromInput(input: { colorName: string }, userId: string) {
function conditionFromInput(input: { colorId: string }, userId: string) { return and(eq(titleCase(colors.name), titleCase(input.colorName)), eq(colors.userId, userId));
return and(eq(colors.id, input.colorId), eq(colors.userId, userId));
} }
async function createColor( async function createColor(
input: ZColor, input: ZColor,
ctx: Context, ctx: Context,
) { ) {
console.log("[TRPC Router] Begin Create Color");
return ctx.db.transaction(async (trx) => { return ctx.db.transaction(async (trx) => {
try { try {
const result = await trx const result = await trx
.insert(colors) .insert(colors)
.values({ .values({
name: input.name, name: titleCase(input.name),
hexcode: input.hexcode, hexcode: input.hexcode,
inverse: calcInverseColor(input.hexcode), inverse: calcInverseColor(input.hexcode),
userId: ctx.user!.id, userId: ctx.user!.id,
@ -65,7 +62,7 @@ export const colorsAppRouter = router({
id: z.string(), id: z.string(),
name: z.string(), name: z.string(),
hexcode: z.string(), hexcode: z.string(),
numCategories: z.number(), userId: z.string(),
}), }),
), ),
})) }))
@ -75,13 +72,13 @@ export const colorsAppRouter = router({
id: colors.id, id: colors.id,
name: colors.name, name: colors.name,
hexcode: colors.hexcode, hexcode: colors.hexcode,
userId: colors.userId,
}) })
.from(colors); .from(colors);
return { return {
colors: dbColors.map(({ ...color }) => ({ colors: dbColors.map(({ ...color }) => ({
...color, ...color,
numCategories: 0,
})), })),
}; };
}), }),
@ -97,4 +94,18 @@ export const colorsAppRouter = router({
.mutation(async ({ input, ctx }) => { .mutation(async ({ input, ctx }) => {
return createColor(input, ctx); return createColor(input, ctx);
}), }),
}); delete: authedProcedure
.input(
z.object({
colorName: z.string(),
}),
)
.mutation(async ({ input, ctx }) => {
const res = await ctx.db
.delete(colors)
.where(conditionFromInput(input, ctx.user.id));
if (res.changes == 0) {
throw new TRPCError({ code: "NOT_FOUND" });
}
}),
});

9
pnpm-lock.yaml generated
View File

@ -568,6 +568,9 @@ importers:
tiny-invariant: tiny-invariant:
specifier: ^1.3.3 specifier: ^1.3.3
version: 1.3.3 version: 1.3.3
title-case:
specifier: ^4.3.2
version: 4.3.2
zod: zod:
specifier: ^3.23.8 specifier: ^3.23.8
version: 3.23.8 version: 3.23.8
@ -19731,7 +19734,7 @@ snapshots:
eslint: 8.57.0 eslint: 8.57.0
eslint-import-resolver-node: 0.3.9 eslint-import-resolver-node: 0.3.9
eslint-import-resolver-typescript: 3.6.1(@typescript-eslint/parser@7.1.0(eslint@8.57.0)(typescript@5.3.3))(eslint-import-resolver-node@0.3.9)(eslint-plugin-import@2.29.0(eslint@8.57.0))(eslint@8.57.0) eslint-import-resolver-typescript: 3.6.1(@typescript-eslint/parser@7.1.0(eslint@8.57.0)(typescript@5.3.3))(eslint-import-resolver-node@0.3.9)(eslint-plugin-import@2.29.0(eslint@8.57.0))(eslint@8.57.0)
eslint-plugin-import: 2.29.0(@typescript-eslint/parser@7.1.0(eslint@8.57.0)(typescript@5.3.3))(eslint-import-resolver-typescript@3.6.1(@typescript-eslint/parser@7.1.0(eslint@8.57.0)(typescript@5.3.3))(eslint-import-resolver-node@0.3.9)(eslint-plugin-import@2.29.0(eslint@8.57.0))(eslint@8.57.0))(eslint@8.57.0) eslint-plugin-import: 2.29.0(@typescript-eslint/parser@7.1.0(eslint@8.57.0)(typescript@5.3.3))(eslint-import-resolver-typescript@3.6.1)(eslint@8.57.0)
eslint-plugin-jsx-a11y: 6.8.0(eslint@8.57.0) eslint-plugin-jsx-a11y: 6.8.0(eslint@8.57.0)
eslint-plugin-react: 7.33.2(eslint@8.57.0) eslint-plugin-react: 7.33.2(eslint@8.57.0)
eslint-plugin-react-hooks: 4.6.0(eslint@8.57.0) eslint-plugin-react-hooks: 4.6.0(eslint@8.57.0)
@ -19785,7 +19788,7 @@ snapshots:
enhanced-resolve: 5.15.0 enhanced-resolve: 5.15.0
eslint: 8.57.0 eslint: 8.57.0
eslint-module-utils: 2.8.0(@typescript-eslint/parser@7.1.0(eslint@8.57.0)(typescript@5.3.3))(eslint-import-resolver-node@0.3.9)(eslint-import-resolver-typescript@3.6.1(@typescript-eslint/parser@7.1.0(eslint@8.57.0)(typescript@5.3.3))(eslint-import-resolver-node@0.3.9)(eslint-plugin-import@2.29.0(eslint@8.57.0))(eslint@8.57.0))(eslint@8.57.0) eslint-module-utils: 2.8.0(@typescript-eslint/parser@7.1.0(eslint@8.57.0)(typescript@5.3.3))(eslint-import-resolver-node@0.3.9)(eslint-import-resolver-typescript@3.6.1(@typescript-eslint/parser@7.1.0(eslint@8.57.0)(typescript@5.3.3))(eslint-import-resolver-node@0.3.9)(eslint-plugin-import@2.29.0(eslint@8.57.0))(eslint@8.57.0))(eslint@8.57.0)
eslint-plugin-import: 2.29.0(@typescript-eslint/parser@7.1.0(eslint@8.57.0)(typescript@5.3.3))(eslint-import-resolver-typescript@3.6.1(@typescript-eslint/parser@7.1.0(eslint@8.57.0)(typescript@5.3.3))(eslint-import-resolver-node@0.3.9)(eslint-plugin-import@2.29.0(eslint@8.57.0))(eslint@8.57.0))(eslint@8.57.0) eslint-plugin-import: 2.29.0(@typescript-eslint/parser@7.1.0(eslint@8.57.0)(typescript@5.3.3))(eslint-import-resolver-typescript@3.6.1)(eslint@8.57.0)
fast-glob: 3.3.1 fast-glob: 3.3.1
get-tsconfig: 4.7.2 get-tsconfig: 4.7.2
is-core-module: 2.13.1 is-core-module: 2.13.1
@ -19824,7 +19827,7 @@ snapshots:
eslint: 8.57.0 eslint: 8.57.0
ignore: 5.3.1 ignore: 5.3.1
eslint-plugin-import@2.29.0(@typescript-eslint/parser@7.1.0(eslint@8.57.0)(typescript@5.3.3))(eslint-import-resolver-typescript@3.6.1(@typescript-eslint/parser@7.1.0(eslint@8.57.0)(typescript@5.3.3))(eslint-import-resolver-node@0.3.9)(eslint-plugin-import@2.29.0(eslint@8.57.0))(eslint@8.57.0))(eslint@8.57.0): eslint-plugin-import@2.29.0(@typescript-eslint/parser@7.1.0(eslint@8.57.0)(typescript@5.3.3))(eslint-import-resolver-typescript@3.6.1)(eslint@8.57.0):
dependencies: dependencies:
array-includes: 3.1.7 array-includes: 3.1.7
array.prototype.findlastindex: 1.2.3 array.prototype.findlastindex: 1.2.3