mirror of
https://github.com/FranP-code/create-better-t-stack.git
synced 2025-10-12 23:52:15 +00:00
add mongoose orm to the stack builder (#191)
This commit is contained in:
committed by
GitHub
parent
946f3eb421
commit
437cf9a45a
@@ -58,6 +58,31 @@ export const auth = betterAuth({
|
||||
});
|
||||
{{/if}}
|
||||
|
||||
{{#if (eq orm "mongoose")}}
|
||||
import { betterAuth } from "better-auth";
|
||||
import { mongodbAdapter } from "better-auth/adapters/mongodb";
|
||||
{{#if (includes frontend "native")}}
|
||||
import { expo } from "@better-auth/expo";
|
||||
{{/if}}
|
||||
import { client } from "../db";
|
||||
|
||||
export const auth = betterAuth({
|
||||
database: mongodbAdapter(client.db()),
|
||||
trustedOrigins: [
|
||||
process.env.CORS_ORIGIN || "",{{#if (includes frontend "native")}}
|
||||
"my-better-t-app://",{{/if}}
|
||||
],
|
||||
emailAndPassword: {
|
||||
enabled: true,
|
||||
}
|
||||
|
||||
{{~#if (includes frontend "native")}}
|
||||
,
|
||||
plugins: [expo()]
|
||||
{{/if~}}
|
||||
});
|
||||
{{/if}}
|
||||
|
||||
{{#if (eq orm "none")}}
|
||||
import { betterAuth } from "better-auth";
|
||||
{{#if (includes frontend "native")}}
|
||||
|
||||
@@ -0,0 +1,68 @@
|
||||
import mongoose from 'mongoose';
|
||||
|
||||
const { Schema, model } = mongoose;
|
||||
|
||||
const userSchema = new Schema(
|
||||
{
|
||||
_id: { type: String },
|
||||
name: { type: String, required: true },
|
||||
email: { type: String, required: true, unique: true },
|
||||
emailVerified: { type: Boolean, required: true },
|
||||
image: { type: String },
|
||||
createdAt: { type: Date, required: true },
|
||||
updatedAt: { type: Date, required: true },
|
||||
},
|
||||
{ collection: 'user' }
|
||||
);
|
||||
|
||||
const sessionSchema = new Schema(
|
||||
{
|
||||
_id: { type: String },
|
||||
expiresAt: { type: Date, required: true },
|
||||
token: { type: String, required: true, unique: true },
|
||||
createdAt: { type: Date, required: true },
|
||||
updatedAt: { type: Date, required: true },
|
||||
ipAddress: { type: String },
|
||||
userAgent: { type: String },
|
||||
userId: { type: String, ref: 'User', required: true },
|
||||
},
|
||||
{ collection: 'session' }
|
||||
);
|
||||
|
||||
const accountSchema = new Schema(
|
||||
{
|
||||
_id: { type: String },
|
||||
accountId: { type: String, required: true },
|
||||
providerId: { type: String, required: true },
|
||||
userId: { type: String, ref: 'User', required: true },
|
||||
accessToken: { type: String },
|
||||
refreshToken: { type: String },
|
||||
idToken: { type: String },
|
||||
accessTokenExpiresAt: { type: Date },
|
||||
refreshTokenExpiresAt: { type: Date },
|
||||
scope: { type: String },
|
||||
password: { type: String },
|
||||
createdAt: { type: Date, required: true },
|
||||
updatedAt: { type: Date, required: true },
|
||||
},
|
||||
{ collection: 'account' }
|
||||
);
|
||||
|
||||
const verificationSchema = new Schema(
|
||||
{
|
||||
_id: { type: String },
|
||||
identifier: { type: String, required: true },
|
||||
value: { type: String, required: true },
|
||||
expiresAt: { type: Date, required: true },
|
||||
createdAt: { type: Date },
|
||||
updatedAt: { type: Date },
|
||||
},
|
||||
{ collection: 'verification' }
|
||||
);
|
||||
|
||||
const User = model('User', userSchema);
|
||||
const Session = model('Session', sessionSchema);
|
||||
const Account = model('Account', accountSchema);
|
||||
const Verification = model('Verification', verificationSchema);
|
||||
|
||||
export { User, Session, Account, Verification };
|
||||
6
apps/cli/templates/db/mongoose/mongodb/src/db/index.ts
Normal file
6
apps/cli/templates/db/mongoose/mongodb/src/db/index.ts
Normal file
@@ -0,0 +1,6 @@
|
||||
import mongoose from 'mongoose';
|
||||
|
||||
await mongoose.connect(process.env.DATABASE_URL || "");
|
||||
const client = mongoose.connection.getClient();
|
||||
|
||||
export { client };
|
||||
@@ -0,0 +1,66 @@
|
||||
{{#if (eq api "orpc")}}
|
||||
import { z } from "zod";
|
||||
import { publicProcedure } from "../lib/orpc";
|
||||
import { Todo } from "../db/models/todo.model";
|
||||
|
||||
export const todoRouter = {
|
||||
getAll: publicProcedure.handler(async () => {
|
||||
return await Todo.find().lean();
|
||||
}),
|
||||
|
||||
create: publicProcedure
|
||||
.input(z.object({ text: z.string().min(1) }))
|
||||
.handler(async ({ input }) => {
|
||||
const newTodo = await Todo.create({ text: input.text });
|
||||
return newTodo.toObject();
|
||||
}),
|
||||
|
||||
toggle: publicProcedure
|
||||
.input(z.object({ id: z.string(), completed: z.boolean() }))
|
||||
.handler(async ({ input }) => {
|
||||
await Todo.updateOne({ id: input.id }, { completed: input.completed });
|
||||
return { success: true };
|
||||
}),
|
||||
|
||||
delete: publicProcedure
|
||||
.input(z.object({ id: z.string() }))
|
||||
.handler(async ({ input }) => {
|
||||
await Todo.deleteOne({ id: input.id });
|
||||
return { success: true };
|
||||
}),
|
||||
};
|
||||
|
||||
{{/if}}
|
||||
|
||||
{{#if (eq api "trpc")}}
|
||||
import { z } from "zod";
|
||||
import { router, publicProcedure } from "../lib/trpc";
|
||||
import { Todo } from "../db/models/todo.model";
|
||||
|
||||
export const todoRouter = router({
|
||||
getAll: publicProcedure.query(async () => {
|
||||
return await Todo.find().lean();
|
||||
}),
|
||||
|
||||
create: publicProcedure
|
||||
.input(z.object({ text: z.string().min(1) }))
|
||||
.mutation(async ({ input }) => {
|
||||
const newTodo = await Todo.create({ text: input.text });
|
||||
return newTodo.toObject();
|
||||
}),
|
||||
|
||||
toggle: publicProcedure
|
||||
.input(z.object({ id: z.string(), completed: z.boolean() }))
|
||||
.mutation(async ({ input }) => {
|
||||
await Todo.updateOne({ id: input.id }, { completed: input.completed });
|
||||
return { success: true };
|
||||
}),
|
||||
|
||||
delete: publicProcedure
|
||||
.input(z.object({ id: z.string() }))
|
||||
.mutation(async ({ input }) => {
|
||||
await Todo.deleteOne({ id: input.id });
|
||||
return { success: true };
|
||||
}),
|
||||
});
|
||||
{{/if}}
|
||||
@@ -0,0 +1,24 @@
|
||||
import mongoose from 'mongoose';
|
||||
|
||||
const { Schema, model } = mongoose;
|
||||
|
||||
const todoSchema = new Schema({
|
||||
id: {
|
||||
type: mongoose.Schema.Types.ObjectId,
|
||||
auto: true,
|
||||
},
|
||||
text: {
|
||||
type: String,
|
||||
required: true,
|
||||
},
|
||||
completed: {
|
||||
type: Boolean,
|
||||
default: false,
|
||||
},
|
||||
}, {
|
||||
collection: 'todo'
|
||||
});
|
||||
|
||||
const Todo = model('Todo', todoSchema);
|
||||
|
||||
export { Todo };
|
||||
Reference in New Issue
Block a user