refactor: move into monorepo

This commit is contained in:
Max Koon
2025-11-08 13:37:55 -05:00
parent 63670ff3b0
commit 058f2bb94f
50 changed files with 1550 additions and 1523 deletions

View File

@@ -0,0 +1,25 @@
import { z } from "zod";
export const sessionSchema = z.object({
id: z.string(),
userId: z.string(),
expiresAt: z.date(),
});
export const userSchema = z.object({
id: z.string(),
email: z.string(),
emailVerified: z.boolean(),
name: z.string(),
createdAt: z.date(),
updatedAt: z.date(),
});
export const authDataSchema = z.object({
session: sessionSchema,
user: userSchema,
});
export type Session = z.infer<typeof sessionSchema>;
export type User = z.infer<typeof userSchema>;
export type AuthData = z.infer<typeof authDataSchema>;

View File

@@ -0,0 +1,3 @@
export const HOST = process.env.EXPO_PUBLIC_TAILSCALE_MACHINE || "localhost";
export const BASE_URL = `http://${HOST}`;

View File

@@ -0,0 +1,10 @@
import { drizzle } from "drizzle-orm/node-postgres";
import { Pool } from "pg";
import * as schema from "./schema";
export const getDb = ({ connectionString }: { connectionString: string }) => {
const pool = new Pool({ connectionString, max: 5 });
return drizzle(pool, {
schema,
});
};

View File

@@ -0,0 +1,6 @@
import * as drizzleSchema from "./schema";
export * from "./schema";
export * from "./client";
export { drizzleSchema };

View File

@@ -0,0 +1,2 @@
export * from "./public";
export * from "./private";

View File

@@ -0,0 +1,95 @@
import { relations } from "drizzle-orm";
import {
index,
pgTable,
text,
timestamp,
uniqueIndex,
} from "drizzle-orm/pg-core";
import { users } from "./public";
export const accounts = pgTable(
"account",
{
id: text("id").primaryKey(),
userId: text("user_id")
.notNull()
.references(() => users.id, { onDelete: "cascade" }),
accountId: text("account_id").notNull(),
providerId: text("provider_id").notNull(),
accessToken: text("access_token"),
refreshToken: text("refresh_token"),
accessTokenExpiresAt: timestamp("access_token_expires_at"),
refreshTokenExpiresAt: timestamp("refresh_token_expires_at"),
scope: text("scope"),
idToken: text("id_token"),
password: text("password"),
createdAt: timestamp("created_at").notNull().defaultNow(),
updatedAt: timestamp("updated_at").notNull().defaultNow(),
},
(table) => [
uniqueIndex("account_provider_account_unique").on(
table.providerId,
table.accountId,
),
index("account_user_id_idx").on(table.userId),
],
);
export const accountRelations = relations(accounts, ({ one }) => ({
user: one(users, {
fields: [accounts.userId],
references: [users.id],
}),
}));
export const sessions = pgTable(
"session",
{
id: text("id").primaryKey(),
userId: text("user_id")
.notNull()
.references(() => users.id, { onDelete: "cascade" }),
token: text("token").notNull(),
expiresAt: timestamp("expires_at").notNull(),
ipAddress: text("ip_address"),
userAgent: text("user_agent"),
createdAt: timestamp("created_at").notNull().defaultNow(),
updatedAt: timestamp("updated_at").notNull().defaultNow(),
},
(table) => [
uniqueIndex("session_token_unique").on(table.token),
index("session_user_id_idx").on(table.userId),
],
);
export const sessionRelations = relations(sessions, ({ one }) => ({
user: one(users, {
fields: [sessions.userId],
references: [users.id],
}),
}));
export const verifications = pgTable(
"verification",
{
id: text("id").primaryKey(),
identifier: text("identifier").notNull(),
value: text("value").notNull(),
expiresAt: timestamp("expires_at").notNull(),
createdAt: timestamp("created_at").notNull().defaultNow(),
updatedAt: timestamp("updated_at").notNull().defaultNow(),
},
(table) => [index("verification_identifier_idx").on(table.identifier)],
);
export const auditLogs = pgTable("audit_log", {
id: text("id").primaryKey(),
createdAt: timestamp("created_at").notNull().defaultNow(),
updatedAt: timestamp("updated_at").notNull().defaultNow(),
userId: text("user_id")
.notNull()
.references(() => users.id, { onDelete: "cascade" }),
action: text("action").notNull(),
});

View File

@@ -0,0 +1,57 @@
import { pgTable, text, boolean, timestamp, uniqueIndex, decimal } from "drizzle-orm/pg-core";
export const users = pgTable(
"user",
{
id: text("id").primaryKey(),
name: text("name"),
email: text("email").notNull(),
emailVerified: boolean("email_verified").notNull().default(false),
image: text("image"),
createdAt: timestamp("created_at").notNull().defaultNow(),
updatedAt: timestamp("updated_at").notNull().defaultNow(),
},
(table) => [uniqueIndex("user_email_unique").on(table.email)],
);
export const transaction = pgTable("transaction", {
id: text("id").primaryKey(),
user_id: text("user_id").notNull(),
plaid_id: text("plaid_id").notNull().unique(),
account_id: text("account_id").notNull(),
name: text("name").notNull(),
amount: decimal("amount").notNull(),
datetime: timestamp("datetime"),
authorized_datetime: timestamp("authorized_datetime"),
json: text("json"),
createdAt: timestamp("created_at").notNull().defaultNow(),
updatedAt: timestamp("updated_at").notNull().defaultNow(),
});
export const plaidLink = pgTable("plaidLink", {
id: text("id").primaryKey(),
user_id: text("user_id").notNull(),
link: text("link").notNull(),
token: text("token").notNull(),
createdAt: timestamp("created_at").notNull().defaultNow(),
});
export const balance = pgTable("balance", {
id: text("id").primaryKey(),
user_id: text("userId").notNull(),
plaid_id: text("plaidId").notNull().unique(),
avaliable: decimal("avaliable").notNull(),
current: decimal("current").notNull(),
name: text("name").notNull(),
createdAt: timestamp("created_at").notNull().defaultNow(),
updatedAt: timestamp("updated_at").notNull().defaultNow(),
});
export const plaidAccessTokens = pgTable("plaidAccessToken", {
id: text("id").primaryKey(),
name: text("name").notNull(),
logoUrl: text("logoUrl").notNull(),
userId: text("user_id").notNull(),
token: text("token").notNull(),
createdAt: timestamp("created_at").notNull().defaultNow(),
});

View File

@@ -0,0 +1,5 @@
export * from "./queries";
export * from "./mutators";
export * from "./zero-schema.gen";
export * from "./zql";
export * from "./const";

View File

@@ -0,0 +1,18 @@
import type { Transaction } from "@rocicorp/zero";
import type { AuthData } from "./auth";
import type { Schema } from ".";
type Tx = Transaction<Schema>;
export function createMutators(authData: AuthData | null) {
return {
link: {
async create() {},
async get(tx: Tx, { link_token }: { link_token: string }) {},
async updateTransactions() {},
async updateBalences() {},
}
} as const;
}
export type Mutators = ReturnType<typeof createMutators>;

View File

@@ -0,0 +1,40 @@
import { syncedQueryWithContext } from "@rocicorp/zero";
import { z } from "zod";
import { builder } from ".";
import { type AuthData } from "./auth";
import { isLoggedIn } from "./zql";
export const queries = {
me: syncedQueryWithContext('me', z.tuple([]), (authData: AuthData | null) => {
isLoggedIn(authData);
return builder.users
.where('id', '=', authData.user.id)
.one();
}),
allTransactions: syncedQueryWithContext('allTransactions', z.tuple([]), (authData: AuthData | null) => {
isLoggedIn(authData);
return builder.transaction
.where('user_id', '=', authData.user.id)
.orderBy('datetime', 'desc')
.limit(50)
}),
getPlaidLink: syncedQueryWithContext('getPlaidLink', z.tuple([]), (authData: AuthData | null) => {
isLoggedIn(authData);
return builder.plaidLink
.where('user_id', '=', authData.user.id)
.orderBy('createdAt', 'desc')
.one();
}),
getBalances: syncedQueryWithContext('getBalances', z.tuple([]), (authData: AuthData | null) => {
isLoggedIn(authData);
return builder.balance
.where('user_id', '=', authData.user.id)
.orderBy('name', 'asc');
}),
getItems: syncedQueryWithContext('getItems', z.tuple([]), (authData: AuthData | null) => {
isLoggedIn(authData);
return builder.plaidAccessTokens
.where('userId', '=', authData.user.id)
.orderBy('createdAt', 'desc');
})
};

View File

@@ -0,0 +1,6 @@
import { definePermissions } from "@rocicorp/zero";
import { schema as schemaGen } from "./zero-schema.gen";
export const schema = schemaGen;
export const permissions = definePermissions(schema, () => ({}));

View File

@@ -0,0 +1,442 @@
/* eslint-disable */
// @ts-nocheck
// noinspection JSUnusedGlobalSymbols
// This file was automatically generated by drizzle-zero.
// You should NOT make any changes in this file as it will be overwritten.
// Additionally, you should also exclude this file from your linter and/or formatter to prevent it from being checked or modified.
import type { Row } from "@rocicorp/zero";
import { createBuilder } from "@rocicorp/zero";
import type { DrizzleToZeroSchema, ZeroCustomType } from "drizzle-zero";
import type * as drizzleSchema from "./db/schema/public";
type ZeroSchema = DrizzleToZeroSchema<typeof drizzleSchema>;
/**
* The Zero schema object.
* This type is auto-generated from your Drizzle schema definition.
*/
export const schema = {
tables: {
balance: {
name: "balance",
columns: {
id: {
type: "string",
optional: false,
customType: null as unknown as ZeroCustomType<
ZeroSchema,
"balance",
"id"
>,
},
user_id: {
type: "string",
optional: false,
customType: null as unknown as ZeroCustomType<
ZeroSchema,
"balance",
"user_id"
>,
serverName: "userId",
},
plaid_id: {
type: "string",
optional: false,
customType: null as unknown as ZeroCustomType<
ZeroSchema,
"balance",
"plaid_id"
>,
serverName: "plaidId",
},
avaliable: {
type: "number",
optional: false,
customType: null as unknown as ZeroCustomType<
ZeroSchema,
"balance",
"avaliable"
>,
},
current: {
type: "number",
optional: false,
customType: null as unknown as ZeroCustomType<
ZeroSchema,
"balance",
"current"
>,
},
name: {
type: "string",
optional: false,
customType: null as unknown as ZeroCustomType<
ZeroSchema,
"balance",
"name"
>,
},
createdAt: {
type: "number",
optional: true,
customType: null as unknown as ZeroCustomType<
ZeroSchema,
"balance",
"createdAt"
>,
serverName: "created_at",
},
updatedAt: {
type: "number",
optional: true,
customType: null as unknown as ZeroCustomType<
ZeroSchema,
"balance",
"updatedAt"
>,
serverName: "updated_at",
},
},
primaryKey: ["id"],
},
plaidAccessTokens: {
name: "plaidAccessTokens",
columns: {
id: {
type: "string",
optional: false,
customType: null as unknown as ZeroCustomType<
ZeroSchema,
"plaidAccessTokens",
"id"
>,
},
name: {
type: "string",
optional: false,
customType: null as unknown as ZeroCustomType<
ZeroSchema,
"plaidAccessTokens",
"name"
>,
},
logoUrl: {
type: "string",
optional: false,
customType: null as unknown as ZeroCustomType<
ZeroSchema,
"plaidAccessTokens",
"logoUrl"
>,
},
userId: {
type: "string",
optional: false,
customType: null as unknown as ZeroCustomType<
ZeroSchema,
"plaidAccessTokens",
"userId"
>,
serverName: "user_id",
},
token: {
type: "string",
optional: false,
customType: null as unknown as ZeroCustomType<
ZeroSchema,
"plaidAccessTokens",
"token"
>,
},
createdAt: {
type: "number",
optional: true,
customType: null as unknown as ZeroCustomType<
ZeroSchema,
"plaidAccessTokens",
"createdAt"
>,
serverName: "created_at",
},
},
primaryKey: ["id"],
serverName: "plaidAccessToken",
},
plaidLink: {
name: "plaidLink",
columns: {
id: {
type: "string",
optional: false,
customType: null as unknown as ZeroCustomType<
ZeroSchema,
"plaidLink",
"id"
>,
},
user_id: {
type: "string",
optional: false,
customType: null as unknown as ZeroCustomType<
ZeroSchema,
"plaidLink",
"user_id"
>,
},
link: {
type: "string",
optional: false,
customType: null as unknown as ZeroCustomType<
ZeroSchema,
"plaidLink",
"link"
>,
},
token: {
type: "string",
optional: false,
customType: null as unknown as ZeroCustomType<
ZeroSchema,
"plaidLink",
"token"
>,
},
createdAt: {
type: "number",
optional: true,
customType: null as unknown as ZeroCustomType<
ZeroSchema,
"plaidLink",
"createdAt"
>,
serverName: "created_at",
},
},
primaryKey: ["id"],
},
transaction: {
name: "transaction",
columns: {
id: {
type: "string",
optional: false,
customType: null as unknown as ZeroCustomType<
ZeroSchema,
"transaction",
"id"
>,
},
user_id: {
type: "string",
optional: false,
customType: null as unknown as ZeroCustomType<
ZeroSchema,
"transaction",
"user_id"
>,
},
plaid_id: {
type: "string",
optional: false,
customType: null as unknown as ZeroCustomType<
ZeroSchema,
"transaction",
"plaid_id"
>,
},
account_id: {
type: "string",
optional: false,
customType: null as unknown as ZeroCustomType<
ZeroSchema,
"transaction",
"account_id"
>,
},
name: {
type: "string",
optional: false,
customType: null as unknown as ZeroCustomType<
ZeroSchema,
"transaction",
"name"
>,
},
amount: {
type: "number",
optional: false,
customType: null as unknown as ZeroCustomType<
ZeroSchema,
"transaction",
"amount"
>,
},
datetime: {
type: "number",
optional: true,
customType: null as unknown as ZeroCustomType<
ZeroSchema,
"transaction",
"datetime"
>,
},
authorized_datetime: {
type: "number",
optional: true,
customType: null as unknown as ZeroCustomType<
ZeroSchema,
"transaction",
"authorized_datetime"
>,
},
json: {
type: "string",
optional: true,
customType: null as unknown as ZeroCustomType<
ZeroSchema,
"transaction",
"json"
>,
},
createdAt: {
type: "number",
optional: true,
customType: null as unknown as ZeroCustomType<
ZeroSchema,
"transaction",
"createdAt"
>,
serverName: "created_at",
},
updatedAt: {
type: "number",
optional: true,
customType: null as unknown as ZeroCustomType<
ZeroSchema,
"transaction",
"updatedAt"
>,
serverName: "updated_at",
},
},
primaryKey: ["id"],
},
users: {
name: "users",
columns: {
id: {
type: "string",
optional: false,
customType: null as unknown as ZeroCustomType<
ZeroSchema,
"users",
"id"
>,
},
name: {
type: "string",
optional: true,
customType: null as unknown as ZeroCustomType<
ZeroSchema,
"users",
"name"
>,
},
email: {
type: "string",
optional: false,
customType: null as unknown as ZeroCustomType<
ZeroSchema,
"users",
"email"
>,
},
emailVerified: {
type: "boolean",
optional: true,
customType: null as unknown as ZeroCustomType<
ZeroSchema,
"users",
"emailVerified"
>,
serverName: "email_verified",
},
image: {
type: "string",
optional: true,
customType: null as unknown as ZeroCustomType<
ZeroSchema,
"users",
"image"
>,
},
createdAt: {
type: "number",
optional: true,
customType: null as unknown as ZeroCustomType<
ZeroSchema,
"users",
"createdAt"
>,
serverName: "created_at",
},
updatedAt: {
type: "number",
optional: true,
customType: null as unknown as ZeroCustomType<
ZeroSchema,
"users",
"updatedAt"
>,
serverName: "updated_at",
},
},
primaryKey: ["id"],
serverName: "user",
},
},
relationships: {},
enableLegacyQueries: false,
enableLegacyMutators: false,
} as const;
/**
* Represents the Zero schema type.
* This type is auto-generated from your Drizzle schema definition.
*/
export type Schema = typeof schema;
/**
* Represents a row from the "balance" table.
* This type is auto-generated from your Drizzle schema definition.
*/
export type Balance = Row<Schema["tables"]["balance"]>;
/**
* Represents a row from the "plaidAccessTokens" table.
* This type is auto-generated from your Drizzle schema definition.
*/
export type PlaidAccessToken = Row<Schema["tables"]["plaidAccessTokens"]>;
/**
* Represents a row from the "plaidLink" table.
* This type is auto-generated from your Drizzle schema definition.
*/
export type PlaidLink = Row<Schema["tables"]["plaidLink"]>;
/**
* Represents a row from the "transaction" table.
* This type is auto-generated from your Drizzle schema definition.
*/
export type Transaction = Row<Schema["tables"]["transaction"]>;
/**
* Represents a row from the "users" table.
* This type is auto-generated from your Drizzle schema definition.
*/
export type User = Row<Schema["tables"]["users"]>;
/**
* Represents the Zero schema query builder.
* This type is auto-generated from your Drizzle schema definition.
*/
export const builder = createBuilder(schema);

View File

@@ -0,0 +1,10 @@
import type { AuthData } from "./auth";
export function isLoggedIn(
authData: AuthData | null,
): asserts authData is AuthData {
console.log("AUTHDATA", authData);
if (!authData?.user.id) {
throw new Error("User is not logged in");
}
}