Skip to content

数据库

NuxtPro项目中均使用 DrizzleOrm 作为主要的orm框架,能够支持像mysql、pgsql等常见的数据库类型。

这里以mysql作为案例,讲述NuxtPro中的配置流程:

数据库配置

  • 1、项目中mysql的配置

NuxtPro项目中,无论是用户端还是后台端中,都仅需在环境变量env中配置1行参数即可实现与mysql的无缝对接,简单快捷。

bash
DATABASE_URL=mysql://用户名:密码@mysql地址:端口/数据库 # mysql数据库连接地址
  • 2、orm框架针对mysql的配置

NuxtPro项目中,找到config文件夹,里面包含着针对mysql数据库的映射对应关系。后续如需切换其他数据库,只需将方言dialect 修改成对应的数据库即可。

bash
import type { Config } from 'drizzle-kit';

export default {
    dialect: 'mysql', 
    schema: './server/database/schema.ts', 
    out: './server/migrations', 
    dbCredentials: {
      url: process.env.DATABASE_URL!
    },
  } satisfies Config;

随后在server->database文件夹下,调整database.ts参数为对应mysql的驱动连接即可。其他数据库连接方式同样。

bash
import { drizzle } from 'drizzle-orm/mysql2';
import mysql from 'mysql2/promise';
import * as schema from './schema';

const connection = mysql.createPool(process.env.DATABASE_URL!);
export const db = drizzle(connection, { schema, mode: 'default' });
  • 3、navicat中mysql建库建表

为了更简单高效的实现对mysql的建库建表操作,这里不采取自带的migrations迁移方式。而是采用更为直接的navicat中执行sql脚本的形式。

  • a).新建数据库

image

  • b).执行建表语句

您可以直接将server->migrations文件夹下的db.sql文件中的内容复制粘贴到navicat的sql执行栏。也可通过导入的形式直接导入db.sql到刚刚新建的nuxtpro数据库中

image

  • c).CRUD操作基础案例

以下为drizzle-orm操作基础案例,具体操作详情可参考官方文档 DrizzleOrm

bash
import { category } from "@/db/schema";
import { db } from "@/db";
import { and, desc, eq } from "drizzle-orm";

export enum CategoryStatus {
 Created = "created",
 Deleted = "deleted",
 Online = "online",
 Offline = "offline",
}

export async function insertCategory(
 data: typeof category.$inferInsert
): Promise<typeof category.$inferSelect | undefined> {
 const [category] = await db().insert(category).values(data).returning();

 return category;
}

export async function updatecategory(
 uuid: string,
 data: Partial<typeof category.$inferInsert>
): Promise<typeof category.$inferSelect | undefined> {
 const [category] = await db()
   .update(category)
   .set(data)
   .where(eq(category.uuid, uuid))
   .returning();

 return category;
}

export async function findCategoryByUuid(
 uuid: string
): Promise<typeof category.$inferSelect | undefined> {
 const [category] = await db()
   .select()
   .from(category)
   .where(eq(category.uuid, uuid))
   .limit(1);

 return category;
}

export async function findCategoryBySlug(
 slug: string,
 locale: string
): Promise<typeof category.$inferSelect | undefined> {
 const [category] = await db()
   .select()
   .from(category)
   .where(and(eq(category.slug, slug), eq(category.locale, locale)))
   .limit(1);

 return category;
}

export async function getAllCategory(
 page: number = 1,
 limit: number = 50
): Promise<(typeof category.$inferSelect)[] | undefined> {
 const offset = (page - 1) * limit;

 const data = await db()
   .select()
   .from(category)
   .orderBy(desc(category.created_at))
   .limit(limit)
   .offset(offset);

 return data;
}

export async function getCategoryByLocale(
 locale: string,
 page: number = 1,
 limit: number = 50
): Promise<(typeof category.$inferSelect)[] | undefined> {
 const offset = (page - 1) * limit;

 const data = await db()
   .select()
   .from(category)
   .where(and(eq(category.locale, locale), eq(category.status, CategoryStatus.Online)))
   .orderBy(desc(category.created_at))
   .limit(limit)
   .offset(offset);

 return data;
}

export async function getCategoryTotal(): Promise<number> {
 const total = await db().$count(category);

 return total;
}