refactor: better shortcut hook
This commit is contained in:
@@ -6,6 +6,7 @@ import {
|
||||
timestamp,
|
||||
pgEnum,
|
||||
uniqueIndex,
|
||||
numeric,
|
||||
} from "drizzle-orm/pg-core";
|
||||
|
||||
export const users = pgTable(
|
||||
@@ -65,3 +66,25 @@ export const plaidAccessTokens = pgTable("plaidAccessToken", {
|
||||
token: text("token").notNull(),
|
||||
createdAt: timestamp("created_at").notNull().defaultNow(),
|
||||
});
|
||||
|
||||
export const budget = pgTable("budget", {
|
||||
id: text("id").primaryKey(),
|
||||
orgId: text("org_id").notNull(),
|
||||
label: text("label").notNull(),
|
||||
createdBy: text("created_by").notNull(),
|
||||
createdAt: timestamp("created_at").notNull().defaultNow(),
|
||||
updatedAt: timestamp("updated_at").notNull().defaultNow(),
|
||||
});
|
||||
|
||||
export const category = pgTable("category", {
|
||||
id: text("id").primaryKey(),
|
||||
budgetId: text("budget_id").notNull(),
|
||||
amount: decimal("amount").notNull(),
|
||||
every: text("every", { enum: ["year", "month", "week"] }).notNull(),
|
||||
order: numeric("order").notNull(),
|
||||
label: text("label").notNull(),
|
||||
color: text("color").notNull(),
|
||||
createdBy: text("created_by").notNull(),
|
||||
createdAt: timestamp("created_at").notNull().defaultNow(),
|
||||
updatedAt: timestamp("updated_at").notNull().defaultNow(),
|
||||
});
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import type { Transaction } from "@rocicorp/zero";
|
||||
import type { AuthData } from "./auth";
|
||||
import { authDataSchema, type AuthData } from "./auth";
|
||||
import { type Schema } from "./zero-schema.gen";
|
||||
import { isLoggedIn } from "./zql";
|
||||
|
||||
@@ -39,6 +39,17 @@ export function createMutators(authData: AuthData | null) {
|
||||
}
|
||||
},
|
||||
},
|
||||
budget: {
|
||||
async create(tx: Tx, { id }: { id: string }) {
|
||||
isLoggedIn(authData);
|
||||
await tx.mutate.budget.insert({
|
||||
id,
|
||||
orgId: authData.user.id,
|
||||
label: "New Budget",
|
||||
createdBy: authData.user.id,
|
||||
});
|
||||
},
|
||||
},
|
||||
} as const;
|
||||
}
|
||||
|
||||
|
||||
@@ -60,4 +60,20 @@ export const queries = {
|
||||
.orderBy("createdAt", "desc");
|
||||
},
|
||||
),
|
||||
getBudgets: syncedQueryWithContext(
|
||||
"getBudgets",
|
||||
z.tuple([]),
|
||||
(authData: AuthData | null) => {
|
||||
isLoggedIn(authData);
|
||||
return builder.budget.limit(10);
|
||||
},
|
||||
),
|
||||
getBudgetCategories: syncedQueryWithContext(
|
||||
"getBudgetCategories",
|
||||
z.tuple([]),
|
||||
(authData: AuthData | null) => {
|
||||
isLoggedIn(authData);
|
||||
return builder.category.orderBy("order", "desc");
|
||||
},
|
||||
),
|
||||
};
|
||||
|
||||
@@ -112,6 +112,170 @@ export const schema = {
|
||||
},
|
||||
primaryKey: ["id"],
|
||||
},
|
||||
budget: {
|
||||
name: "budget",
|
||||
columns: {
|
||||
id: {
|
||||
type: "string",
|
||||
optional: false,
|
||||
customType: null as unknown as ZeroCustomType<
|
||||
ZeroSchema,
|
||||
"budget",
|
||||
"id"
|
||||
>,
|
||||
},
|
||||
orgId: {
|
||||
type: "string",
|
||||
optional: false,
|
||||
customType: null as unknown as ZeroCustomType<
|
||||
ZeroSchema,
|
||||
"budget",
|
||||
"orgId"
|
||||
>,
|
||||
serverName: "org_id",
|
||||
},
|
||||
label: {
|
||||
type: "string",
|
||||
optional: false,
|
||||
customType: null as unknown as ZeroCustomType<
|
||||
ZeroSchema,
|
||||
"budget",
|
||||
"label"
|
||||
>,
|
||||
},
|
||||
createdBy: {
|
||||
type: "string",
|
||||
optional: false,
|
||||
customType: null as unknown as ZeroCustomType<
|
||||
ZeroSchema,
|
||||
"budget",
|
||||
"createdBy"
|
||||
>,
|
||||
serverName: "created_by",
|
||||
},
|
||||
createdAt: {
|
||||
type: "number",
|
||||
optional: true,
|
||||
customType: null as unknown as ZeroCustomType<
|
||||
ZeroSchema,
|
||||
"budget",
|
||||
"createdAt"
|
||||
>,
|
||||
serverName: "created_at",
|
||||
},
|
||||
updatedAt: {
|
||||
type: "number",
|
||||
optional: true,
|
||||
customType: null as unknown as ZeroCustomType<
|
||||
ZeroSchema,
|
||||
"budget",
|
||||
"updatedAt"
|
||||
>,
|
||||
serverName: "updated_at",
|
||||
},
|
||||
},
|
||||
primaryKey: ["id"],
|
||||
},
|
||||
category: {
|
||||
name: "category",
|
||||
columns: {
|
||||
id: {
|
||||
type: "string",
|
||||
optional: false,
|
||||
customType: null as unknown as ZeroCustomType<
|
||||
ZeroSchema,
|
||||
"category",
|
||||
"id"
|
||||
>,
|
||||
},
|
||||
budgetId: {
|
||||
type: "string",
|
||||
optional: false,
|
||||
customType: null as unknown as ZeroCustomType<
|
||||
ZeroSchema,
|
||||
"category",
|
||||
"budgetId"
|
||||
>,
|
||||
serverName: "budget_id",
|
||||
},
|
||||
amount: {
|
||||
type: "number",
|
||||
optional: false,
|
||||
customType: null as unknown as ZeroCustomType<
|
||||
ZeroSchema,
|
||||
"category",
|
||||
"amount"
|
||||
>,
|
||||
},
|
||||
every: {
|
||||
type: "string",
|
||||
optional: false,
|
||||
customType: null as unknown as ZeroCustomType<
|
||||
ZeroSchema,
|
||||
"category",
|
||||
"every"
|
||||
>,
|
||||
},
|
||||
order: {
|
||||
type: "number",
|
||||
optional: false,
|
||||
customType: null as unknown as ZeroCustomType<
|
||||
ZeroSchema,
|
||||
"category",
|
||||
"order"
|
||||
>,
|
||||
},
|
||||
label: {
|
||||
type: "string",
|
||||
optional: false,
|
||||
customType: null as unknown as ZeroCustomType<
|
||||
ZeroSchema,
|
||||
"category",
|
||||
"label"
|
||||
>,
|
||||
},
|
||||
color: {
|
||||
type: "string",
|
||||
optional: false,
|
||||
customType: null as unknown as ZeroCustomType<
|
||||
ZeroSchema,
|
||||
"category",
|
||||
"color"
|
||||
>,
|
||||
},
|
||||
createdBy: {
|
||||
type: "string",
|
||||
optional: false,
|
||||
customType: null as unknown as ZeroCustomType<
|
||||
ZeroSchema,
|
||||
"category",
|
||||
"createdBy"
|
||||
>,
|
||||
serverName: "created_by",
|
||||
},
|
||||
createdAt: {
|
||||
type: "number",
|
||||
optional: true,
|
||||
customType: null as unknown as ZeroCustomType<
|
||||
ZeroSchema,
|
||||
"category",
|
||||
"createdAt"
|
||||
>,
|
||||
serverName: "created_at",
|
||||
},
|
||||
updatedAt: {
|
||||
type: "number",
|
||||
optional: true,
|
||||
customType: null as unknown as ZeroCustomType<
|
||||
ZeroSchema,
|
||||
"category",
|
||||
"updatedAt"
|
||||
>,
|
||||
serverName: "updated_at",
|
||||
},
|
||||
},
|
||||
primaryKey: ["id"],
|
||||
},
|
||||
plaidAccessTokens: {
|
||||
name: "plaidAccessTokens",
|
||||
columns: {
|
||||
@@ -433,6 +597,16 @@ export type Schema = typeof schema;
|
||||
* This type is auto-generated from your Drizzle schema definition.
|
||||
*/
|
||||
export type Balance = Row<Schema["tables"]["balance"]>;
|
||||
/**
|
||||
* Represents a row from the "budget" table.
|
||||
* This type is auto-generated from your Drizzle schema definition.
|
||||
*/
|
||||
export type Budget = Row<Schema["tables"]["budget"]>;
|
||||
/**
|
||||
* Represents a row from the "category" table.
|
||||
* This type is auto-generated from your Drizzle schema definition.
|
||||
*/
|
||||
export type Category = Row<Schema["tables"]["category"]>;
|
||||
/**
|
||||
* Represents a row from the "plaidAccessTokens" table.
|
||||
* This type is auto-generated from your Drizzle schema definition.
|
||||
|
||||
Reference in New Issue
Block a user