Skip to content

Commit

Permalink
Add groups and permissions for authorization (#6)
Browse files Browse the repository at this point in the history
* 🧑‍💻 Add scripts for linting and building

* ✨ Define createdAt and updatedAt aliases

* 🗃️ Add auth group and permission tables

* 🚚 Move query directory into db directory

* 📌 Pin Bun to v1.1.12 in CI environment

* 👷 Add build stage to CI pipeline
  • Loading branch information
injoonH authored Jun 8, 2024
1 parent d7b271d commit d58fb54
Show file tree
Hide file tree
Showing 17 changed files with 684 additions and 57 deletions.
5 changes: 4 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,13 @@ jobs:
- name: Install bun
uses: oven-sh/setup-bun@v1
with:
bun-version: 1.1.x
bun-version: 1.1.12

- name: Install dependencies
run: bun install

- name: Build app
run: bun run build

- name: Run tests
run: bun test
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
node_modules
build

.env
.volume
5 changes: 3 additions & 2 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
dist

node_modules
build

.volume
3 changes: 3 additions & 0 deletions eslint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,7 @@ export default tseslint.config(
],
},
},
{
ignores: ['node_modules/', 'build/', '.volume/'],
},
)
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
{
"type": "module",
"scripts": {
"build": "bun build src/index.ts --outdir build --target bun",
"db:generate": "drizzle-kit generate",
"db:migrate": "drizzle-kit migrate",
"db:studio": "drizzle-kit studio",
"dev": "bun run --hot src/index.ts",
"lint": "bun prettier -c -w -u . && bun eslint --fix .",
"prepare": "husky"
},
"lint-staged": {
"**/*": "prettier --write --ignore-unknown",
"**/*": "prettier -c -w -u",
"*.{js,jsx,ts,tsx}": "eslint --fix",
"package.json": "sort-package-json"
},
Expand Down
2 changes: 1 addition & 1 deletion src/common/cookie.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { env } from '@/env'

const cookieSchema = z.object({
user: createSelectSchema(userSchema, {
signUpDate: z.coerce.date(),
createdAt: z.coerce.date(),
}).omit({
password: true,
}),
Expand Down
11 changes: 11 additions & 0 deletions src/db/field.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { int, timestamp } from 'drizzle-orm/mysql-core'

export const id = int('id').primaryKey().autoincrement()

// No need to worry about the Y2K38 problem if you are using 64-bit versions of
// platforms and MySQL 8.0.28 or later.
export const createdAt = timestamp('created_at').notNull().defaultNow()
export const updatedAt = timestamp('updated_at')
.notNull()
.defaultNow()
.onUpdateNow()
3 changes: 3 additions & 0 deletions src/db/migration/0001_organic_cassandra_nova.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
ALTER TABLE `auth_user` RENAME COLUMN `signup_date` TO `created_at`;--> statement-breakpoint
ALTER TABLE `auth_user` MODIFY COLUMN `created_at` timestamp NOT NULL DEFAULT (now());--> statement-breakpoint
ALTER TABLE `auth_user` ADD `updated_at` timestamp DEFAULT (now()) NOT NULL ON UPDATE CURRENT_TIMESTAMP;
39 changes: 39 additions & 0 deletions src/db/migration/0002_yellow_dexter_bennett.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
CREATE TABLE `auth_group` (
`id` int AUTO_INCREMENT NOT NULL,
`name` varchar(128) NOT NULL,
CONSTRAINT `auth_group_id` PRIMARY KEY(`id`),
CONSTRAINT `auth_group_name_unique` UNIQUE(`name`)
);
--> statement-breakpoint
CREATE TABLE `auth_group_permission` (
`group_id` int NOT NULL,
`permission_id` int NOT NULL,
CONSTRAINT `auth_group_permission_group_id_permission_id_pk` PRIMARY KEY(`group_id`,`permission_id`)
);
--> statement-breakpoint
CREATE TABLE `auth_permission` (
`id` int AUTO_INCREMENT NOT NULL,
`name` varchar(128) NOT NULL,
`code` varchar(128) NOT NULL,
CONSTRAINT `auth_permission_id` PRIMARY KEY(`id`),
CONSTRAINT `auth_permission_code_unique` UNIQUE(`code`)
);
--> statement-breakpoint
CREATE TABLE `auth_user_group` (
`user_id` int NOT NULL,
`group_id` int NOT NULL,
CONSTRAINT `auth_user_group_user_id_group_id_pk` PRIMARY KEY(`user_id`,`group_id`)
);
--> statement-breakpoint
CREATE TABLE `auth_user_permission` (
`user_id` int NOT NULL,
`permission_id` int NOT NULL,
CONSTRAINT `auth_user_permission_user_id_permission_id_pk` PRIMARY KEY(`user_id`,`permission_id`)
);
--> statement-breakpoint
ALTER TABLE `auth_group_permission` ADD CONSTRAINT `auth_group_permission_group_id_auth_group_id_fk` FOREIGN KEY (`group_id`) REFERENCES `auth_group`(`id`) ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE `auth_group_permission` ADD CONSTRAINT `auth_group_permission_permission_id_auth_permission_id_fk` FOREIGN KEY (`permission_id`) REFERENCES `auth_permission`(`id`) ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE `auth_user_group` ADD CONSTRAINT `auth_user_group_user_id_auth_user_id_fk` FOREIGN KEY (`user_id`) REFERENCES `auth_user`(`id`) ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE `auth_user_group` ADD CONSTRAINT `auth_user_group_group_id_auth_group_id_fk` FOREIGN KEY (`group_id`) REFERENCES `auth_group`(`id`) ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE `auth_user_permission` ADD CONSTRAINT `auth_user_permission_user_id_auth_user_id_fk` FOREIGN KEY (`user_id`) REFERENCES `auth_user`(`id`) ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE `auth_user_permission` ADD CONSTRAINT `auth_user_permission_permission_id_auth_permission_id_fk` FOREIGN KEY (`permission_id`) REFERENCES `auth_permission`(`id`) ON DELETE no action ON UPDATE no action;
83 changes: 83 additions & 0 deletions src/db/migration/meta/0001_snapshot.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
{
"version": "5",
"dialect": "mysql",
"id": "ab101c32-dd97-48ad-97d6-c3c797b2914b",
"prevId": "a824d5e5-dff7-4abc-8eaf-6a4f119cafa3",
"tables": {
"auth_user": {
"name": "auth_user",
"columns": {
"id": {
"name": "id",
"type": "int",
"primaryKey": false,
"notNull": true,
"autoincrement": true
},
"created_at": {
"name": "created_at",
"type": "timestamp",
"primaryKey": false,
"notNull": true,
"autoincrement": false,
"default": "(now())"
},
"updated_at": {
"name": "updated_at",
"type": "timestamp",
"primaryKey": false,
"notNull": true,
"autoincrement": false,
"onUpdate": true,
"default": "(now())"
},
"password": {
"name": "password",
"type": "varchar(128)",
"primaryKey": false,
"notNull": true,
"autoincrement": false
},
"nickname": {
"name": "nickname",
"type": "varchar(32)",
"primaryKey": false,
"notNull": true,
"autoincrement": false
},
"email": {
"name": "email",
"type": "varchar(255)",
"primaryKey": false,
"notNull": true,
"autoincrement": false
}
},
"indexes": {},
"foreignKeys": {},
"compositePrimaryKeys": {
"auth_user_id": {
"name": "auth_user_id",
"columns": ["id"]
}
},
"uniqueConstraints": {
"auth_user_nickname_unique": {
"name": "auth_user_nickname_unique",
"columns": ["nickname"]
},
"auth_user_email_unique": {
"name": "auth_user_email_unique",
"columns": ["email"]
}
}
}
},
"_meta": {
"schemas": {},
"tables": {},
"columns": {
"\"auth_user\".\"signup_date\"": "\"auth_user\".\"created_at\""
}
}
}
Loading

0 comments on commit d58fb54

Please sign in to comment.