Skip to content

Commit

Permalink
chore: update
Browse files Browse the repository at this point in the history
  • Loading branch information
sobird committed Nov 19, 2024
1 parent f736fc7 commit ae8dfaf
Show file tree
Hide file tree
Showing 26 changed files with 427 additions and 84 deletions.
8 changes: 5 additions & 3 deletions actions/role.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@

import { revalidatePath } from 'next/cache';
import { redirect } from 'next/navigation';
import { RoleFormZod, RoleFormAttributes } from '@/zod/role';
import { RoleModel } from '@/models';
import { getServerAuthToken } from '@/lib/auth';

import { defineAbilityFor } from '@/lib/ability';
import { getServerAuthToken } from '@/services/auth/auth';
import { RoleModel } from '@/models';
import { RoleFormZod, RoleFormAttributes } from '@/zod/role';

import { ActionStatus } from '.';

type RoleFormServerActionState = ServerActionState<RoleFormAttributes>;
Expand Down
9 changes: 5 additions & 4 deletions actions/user.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
'use server';

import { revalidatePath } from 'next/cache';
import { redirect } from 'next/navigation';
import { WhereOptions, Op } from 'sequelize';

import { revalidatePath } from 'next/cache';
import { UserModel } from '@/models';
import { generate, verify } from '@/lib/otp';
import CaptchaEmailBody from '@/components/email-template/captcha';
import { transporter } from '@/lib/mailer';
import { generate, verify } from '@/lib/otp';
import reactToHtml from '@/lib/reactToHtml';
import CaptchaEmailBody from '@/components/email-template/captcha';
import { UserModel } from '@/models';
import {
signUpZod, SignUpAttributes, createUserZod, UserAttributes, updateUserZod,
} from '@/zod/user';

import { ActionStatus } from '.';

type UserServerActionState = ServerActionState<SignUpAttributes>;
Expand Down
1 change: 1 addition & 0 deletions app.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
type Optional<T, K extends keyof T> = Omit<T, K> & Partial<Pick<T, K>>;
/**
* 数据列表分页查询参数
*/
Expand Down
7 changes: 4 additions & 3 deletions app/(authentication)/signin/form.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
'use client';

import React, { useState } from 'react';
import { signIn } from 'next-auth/react';
import { useRouter } from 'next/navigation';
import {
Form, Input, Button, ConfigProvider, message,
} from 'antd';
import { useRouter } from 'next/navigation';
import { signIn } from 'next-auth/react';
import React, { useState } from 'react';

import { isEmail } from '@/utils/validator';

const SigninForm: React.FC = () => {
Expand Down
1 change: 1 addition & 0 deletions app/(authentication)/signin/page.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Metadata } from 'next';

import SigninForm from './form';
import styles from './page.module.scss';

Expand Down
3 changes: 2 additions & 1 deletion app/api/auth/[...nextauth]/route.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import NextAuth from 'next-auth';
import { authOptions } from '@/lib/auth';

import { authOptions } from '@/services/auth/auth';

const handler = NextAuth(authOptions);

Expand Down
12 changes: 7 additions & 5 deletions app/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ import {
RegisterButton,
} from '@/components/buttons';
import { defineAbilityFor } from '@/lib/ability';
import { getServerAuthToken } from '@/lib/auth';
import {
UserModel, SessionModel, AccountModel, sequelize,
} from '@/models';
import { getServerAuthToken } from '@/services/auth/auth';

const HomePage: IAppPage<{ id: string }> = async () => {
const HomePage = async () => {
const token = await getServerAuthToken();
console.log('getServerAuthToken', token);
// await sequelize.sync({ force: true });
Expand All @@ -41,10 +41,12 @@ const HomePage: IAppPage<{ id: string }> = async () => {
},
]);

console.log('ability', ability.can('read', '/test1'));
console.log('ability', ability);

const users = await UserModel.findAll({ raw: true });
console.log('users', users);
console.log('ability', ability.can('read', '/test'));

const users = await UserModel.findAll({ include: [UserModel.associations.Roles] });
console.log('users', users[0].createdAt);

return (
<main
Expand Down
3 changes: 2 additions & 1 deletion app/profile/page.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { redirect } from 'next/navigation';
import { getServerAuthSession } from '@/lib/auth';

import { getServerAuthSession } from '@/services/auth/auth';

export default async function Profile() {
const session = await getServerAuthSession();
Expand Down
1 change: 1 addition & 0 deletions lib/ability.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import {
createMongoAbility, ForcedSubject, AbilityBuilder, MongoAbility, CreateAbility, InferSubjects,
} from '@casl/ability';

import Models, { UserAttributes } from '@/models';

type Subjects = InferSubjects<keyof typeof Models | 'all'>;
Expand Down
5 changes: 3 additions & 2 deletions lib/antd.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@

'use client';

import React from 'react';
import { useServerInsertedHTML } from 'next/navigation';
import { createCache, extractStyle, StyleProvider } from '@ant-design/cssinjs';
import { useServerInsertedHTML } from 'next/navigation';
import React from 'react';

import type Entity from '@ant-design/cssinjs/es/Cache';

const StyledComponentsRegistry = ({ children }: React.PropsWithChildren) => {
Expand Down
3 changes: 2 additions & 1 deletion lib/mailer.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { createTransport, type SendMailOptions } from 'nodemailer';
import reactToHtml from '@/lib/reactToHtml';

import Authentication from '@/components/email-template/authentication';
import CaptchaEmailBody from '@/components/email-template/captcha';
import reactToHtml from '@/lib/reactToHtml';

// 创建发送邮件的对象
export const transporter = createTransport({
Expand Down
38 changes: 31 additions & 7 deletions lib/sequelize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,19 @@
* sobird<i@sobird.me> at 2021/11/16 20:33:20 created.
*/

import debug from 'debug';
import { AbilityTuple, MongoAbility } from '@casl/ability';
import log4js from 'log4js';
import {
Sequelize, Model, CreationOptional, ModelStatic, InferAttributes,
Sequelize, Model, CreationOptional, ModelStatic, InferAttributes, ModelOptions, DataTypes,
} from 'sequelize';
import sqlite3 from 'sqlite3';
import { AbilityTuple, MongoAbility } from '@casl/ability';

import { accessibleBy } from '@/casl/toSequelizeQuery';
import type { Models } from '@/models';

const logger = log4js.getLogger('sequelize');
logger.level = log4js.levels.DEBUG;

/** 数据库链接实例 */
export const sequelize = new Sequelize({
// The name of the database
Expand Down Expand Up @@ -114,10 +118,9 @@ export const sequelize = new Sequelize({
// isolationLevel: Transaction.ISOLATION_LEVELS.REPEATABLE_READ
logging: (sql, queryObject: any) => {
const { type, bind } = queryObject;
const log = debug(`app:sql:${type}`);
log(sql);
logger.debug(type, sql);
if (['INSERT', 'UPDATE', 'BULKUPDATE'].includes(type)) {
log(bind);
logger.debug(bind);
}
},
});
Expand All @@ -127,16 +130,20 @@ export const sequelize = new Sequelize({
// console.log('attributes', attributes);
// });

type Initattributes<MS extends ModelStatic<Model>, M extends InstanceType<MS>> = Omit<Parameters<typeof Model.init<MS, M>>[0], 'updatedAt' | 'createdAt'>;

/**
* 模型基类
*
* sobird<i@sobird.me> at 2023/12/05 21:08:43 created.
*/
export class BaseModel<T extends {} = any, P extends {} = T> extends Model<T, P> {
declare id?: CreationOptional<any>;
// declare id: CreationOptional<string>;

declare createdAt: CreationOptional<Date>;

declare updatedAt: CreationOptional<Date>;

/**
* Helper method for defining associations.
* This method is not a part of Sequelize lifecycle.
Expand Down Expand Up @@ -189,4 +196,21 @@ export class BaseModel<T extends {} = any, P extends {} = T> extends Model<T, P>
pn, ps, count: 0, rows: [],
};
}

public static define<MS extends ModelStatic<BaseModel>, M extends InstanceType<MS>>(
this: MS,
attributes: Initattributes<MS, M>,
options: ModelOptions<M>,
): MS {
return super.init({
...attributes,

createdAt: DataTypes.DATE,
updatedAt: DataTypes.DATE,
}, {
sequelize,
deletedAt: true,
...options,
}) as unknown as MS;
}
}
6 changes: 3 additions & 3 deletions models/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ import {
DataTypes,
type InferAttributes, InferCreationAttributes, CreationOptional,
} from 'sequelize';
import { sequelize, BaseModel } from '@/lib/sequelize';

import { BaseModel } from '@/lib/sequelize';

/** These are all the attributes in the Account model */
export type AccountAttributes = InferAttributes<Account>;
Expand Down Expand Up @@ -65,7 +66,7 @@ class Account extends BaseModel<AccountAttributes, AccountCreationAttributes> {
}
}

Account.init(
Account.define(
{
type: {
type: DataTypes.STRING,
Expand Down Expand Up @@ -116,7 +117,6 @@ Account.init(
},
},
{
sequelize,
modelName: 'Account',
},
);
Expand Down
6 changes: 3 additions & 3 deletions models/dictionaryItem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import {
DataTypes,
type InferAttributes, InferCreationAttributes,
} from 'sequelize';
import { sequelize, BaseModel } from '@/lib/sequelize';

import { BaseModel } from '@/lib/sequelize';

/** These are all the attributes in the DictionaryItem model */
export type DictionaryItemAttributes = InferAttributes<DictionaryItem>;
Expand All @@ -28,7 +29,7 @@ class DictionaryItem extends BaseModel<DictionaryItemAttributes, DictionaryItemC
typeId: number;
}

DictionaryItem.init(
DictionaryItem.define(
{
label: {
type: DataTypes.STRING(120),
Expand All @@ -55,7 +56,6 @@ DictionaryItem.init(
},
},
{
sequelize,
modelName: 'DictionaryItem',
},
);
Expand Down
12 changes: 10 additions & 2 deletions models/role.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import {
DataTypes,
type InferAttributes, InferCreationAttributes, CreationOptional,
Association,
BelongsToManyGetAssociationsMixin,
BelongsToManySetAssociationsMixin,
BelongsToManyAddAssociationMixin,
Expand All @@ -18,17 +19,19 @@ import {
BelongsToManyCreateAssociationMixin,
BelongsToManyCountAssociationsMixin,
} from 'sequelize';

import { sequelize, BaseModel } from '@/lib/sequelize';
import type User from './user';

import type Permission from './permission';
import type User from './user';

/** These are all the attributes in the Role model */
export type RoleAttributes = InferAttributes<Role>;
/** Some attributes are optional in `Role.build` and `Role.create` calls */
export type RoleCreationAttributes = InferCreationAttributes<Role>;

class Role extends BaseModel<RoleAttributes, RoleCreationAttributes> {
declare id?: CreationOptional<number>;
declare id: CreationOptional<number>;

declare parentId?: CreationOptional<number>;

Expand Down Expand Up @@ -78,6 +81,11 @@ class Role extends BaseModel<RoleAttributes, RoleCreationAttributes> {

declare countPermissions: BelongsToManyCountAssociationsMixin;

declare static associations: {
Users: Association<Role, User>;
Permissions: Association<Role, Permission>;
};

static associate({
User, UserRole, Permission, RolePermission,
}) {
Expand Down
1 change: 1 addition & 0 deletions models/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import {
DataTypes, type InferAttributes, InferCreationAttributes,
} from 'sequelize';

import { sequelize, BaseModel } from '@/lib/sequelize';

/** These are all the attributes in the Session model */
Expand Down
Loading

0 comments on commit ae8dfaf

Please sign in to comment.