-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Nguyen Huu Nguyen Y
committed
Nov 17, 2024
1 parent
421a4da
commit 7377efc
Showing
16 changed files
with
177 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { pgTable, primaryKey, uuid } from 'drizzle-orm/pg-core' | ||
import { relations } from 'drizzle-orm/relations' | ||
import { sysOrganizationTable } from './sys_organizations.schema' | ||
import { sysUserTable } from './sys_users.schema' | ||
|
||
export const sysOrganizationUserTable = pgTable('sys_organization_user', { | ||
organization_id: uuid('organization_id').references(() => sysOrganizationTable.id, { onDelete: 'cascade' }).notNull(), | ||
user_id: uuid('user_id').references(() => sysUserTable.id, { onDelete: 'cascade' }).notNull(), | ||
}, table => ({ | ||
pk: primaryKey({ columns: [table.organization_id, table.user_id] }), | ||
})) | ||
|
||
export const sysOrganizationUserRelation = relations(sysOrganizationUserTable, ({ one }) => ({ | ||
organization: one(sysOrganizationTable, { | ||
fields: [sysOrganizationUserTable.organization_id], | ||
references: [sysOrganizationTable.id], | ||
}), | ||
user: one(sysUserTable, { | ||
fields: [sysOrganizationUserTable.organization_id], | ||
references: [sysUserTable.id], | ||
}), | ||
})) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,16 @@ | ||
import { pgTable, text, uuid } from 'drizzle-orm/pg-core' | ||
import { relations } from 'drizzle-orm/relations' | ||
import { permissionAction } from './enum.schema' | ||
import { sysRoleTable } from './sys_roles.schema' | ||
import { PermissionAction, PermissionScope, permissionAction, permissionScope } from './enum.schema' | ||
import { sysRolePermissionTable } from './sys_role_permission.schema' | ||
|
||
export const sysPermissionTable = pgTable('sys_permissions', { | ||
id: uuid('id').defaultRandom().primaryKey().notNull(), | ||
role_id: uuid('role_id').references(() => sysRoleTable.id, { onDelete: 'cascade', onUpdate: 'cascade' }).notNull(), | ||
action: permissionAction('action').default('read').notNull(), | ||
action: permissionAction('action').default(PermissionAction.READ).notNull(), | ||
subject: text('subject').notNull(), | ||
scope: permissionScope('scope').default(PermissionScope.ALL), | ||
scope_value: text('scope_value'), | ||
}) | ||
|
||
export const sysPermissionRelations = relations(sysPermissionTable, ({ one }) => ({ | ||
role: one(sysRoleTable, { | ||
fields: [sysPermissionTable.role_id], | ||
references: [sysRoleTable.id], | ||
}), | ||
export const sysPermissionRelations = relations(sysPermissionTable, ({ many }) => ({ | ||
roles: many(sysRolePermissionTable), | ||
})) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { pgTable, primaryKey, uuid } from 'drizzle-orm/pg-core' | ||
import { relations } from 'drizzle-orm/relations' | ||
import { sysRoleTable } from './sys_roles.schema' | ||
import { sysPermissionTable } from './sys_permissions.schema' | ||
|
||
export const sysRolePermissionTable = pgTable('sys_role_permission', { | ||
role_id: uuid('role_id').references(() => sysRoleTable.id, { onDelete: 'cascade' }).notNull(), | ||
permission_id: uuid('permission_id').references(() => sysPermissionTable.id, { onDelete: 'cascade' }).notNull(), | ||
}, table => ({ | ||
pk: primaryKey({ columns: [table.role_id, table.permission_id] }), | ||
})) | ||
|
||
export const sysRolePermissionRelations = relations(sysRolePermissionTable, ({ one }) => ({ | ||
role: one(sysRoleTable, { | ||
fields: [sysRolePermissionTable.role_id], | ||
references: [sysRoleTable.id], | ||
}), | ||
permission: one(sysPermissionTable, { | ||
fields: [sysRolePermissionTable.permission_id], | ||
references: [sysPermissionTable.id], | ||
}), | ||
})) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { pgTable, primaryKey, uuid } from 'drizzle-orm/pg-core' | ||
import { relations } from 'drizzle-orm/relations' | ||
import { sysRoleTable } from './sys_roles.schema' | ||
import { sysUserTable } from './sys_users.schema' | ||
|
||
export const sysRoleUserTable = pgTable('sys_role_user', { | ||
role_id: uuid('role_id').references(() => sysRoleTable.id, { onDelete: 'cascade' }).notNull(), | ||
user_id: uuid('user_id').references(() => sysUserTable.id, { onDelete: 'cascade' }).notNull(), | ||
}, table => ({ | ||
pk: primaryKey({ columns: [table.role_id, table.user_id] }), | ||
})) | ||
|
||
export const sysRoleUserRelation = relations(sysRoleUserTable, ({ one }) => ({ | ||
role: one(sysRoleTable, { | ||
fields: [sysRoleUserTable.role_id], | ||
references: [sysRoleTable.id], | ||
}), | ||
user: one(sysUserTable, { | ||
fields: [sysRoleUserTable.role_id], | ||
references: [sysUserTable.id], | ||
}), | ||
})) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,14 @@ | ||
import { relations } from 'drizzle-orm/relations' | ||
import { pgTable, text, uuid } from 'drizzle-orm/pg-core' | ||
import { sysUserTable } from './sys_users.schema' | ||
import { sysPermissionTable } from './sys_permissions.schema' | ||
import { sysRolePermissionTable } from './sys_role_permission.schema' | ||
import { sysRoleUserTable } from './sys_role_user.schema' | ||
|
||
export const sysRoleTable = pgTable('sys_roles', { | ||
id: uuid('id').defaultRandom().primaryKey().notNull(), | ||
name: text('name').notNull(), | ||
}) | ||
|
||
export const sysRoleRelations = relations(sysRoleTable, ({ many }) => ({ | ||
users: many(sysUserTable), | ||
permissions: many(sysPermissionTable), | ||
users: many(sysRoleUserTable), | ||
permissions: many(sysRolePermissionTable), | ||
})) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,18 +2,17 @@ import { rand, randAvatar, randCountryCode, randEmail, randFullName, randLanguag | |
import type { InferSelectModel } from 'drizzle-orm' | ||
import bcrypt from 'bcrypt' | ||
import type { sysRoleTable } from '../schemas' | ||
import { sysUserTable } from '../schemas' | ||
import { sysRoleUserTable, sysUserTable } from '../schemas' | ||
import { db } from '../../utils/db' | ||
|
||
export async function seedUsers(roles: InferSelectModel<typeof sysRoleTable>[]) { | ||
console.log('Seeding users...') | ||
|
||
const editorRole = roles.find(role => role.name === 'User') | ||
const userRole = roles.find(role => role.name === 'User') | ||
|
||
const testUser = { | ||
email: '[email protected]', | ||
password: await bcrypt.hash('123456', 10), | ||
role_id: editorRole!.id, | ||
} | ||
|
||
const users = await Promise.all(Array.from({ length: 15 }).fill(null).map( | ||
|
@@ -29,13 +28,18 @@ export async function seedUsers(roles: InferSelectModel<typeof sysRoleTable>[]) | |
postcode: '', | ||
address: '', | ||
organization: '', | ||
role_id: rand(roles).id, | ||
}), | ||
)) | ||
|
||
users[0].email = testUser.email | ||
users[0].password = testUser.password | ||
users[0].role_id = testUser.role_id | ||
|
||
return await db.insert(sysUserTable).values(users).returning() | ||
const data = await db.insert(sysUserTable).values(users).returning() | ||
|
||
await db.insert(sysRoleUserTable).values(data.map(user => ({ | ||
role_id: userRole!.id, | ||
user_id: user.id, | ||
}))) | ||
|
||
return data | ||
} |
Oops, something went wrong.