Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Working drizzle integration #34

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,7 @@ Thumbs.db
npm-debug.log*
yarn-debug.log*
yarn-error.log*


# db
sqlite.db
Binary file modified bun.lockb
Binary file not shown.
11 changes: 11 additions & 0 deletions drizzle.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import type { Config } from "drizzle-kit";

export default {
out: "./src/db/migrations",
schema: "./src/db/schemas/*.ts",
breakpoints: false,
driver: "better-sqlite",
dbCredentials: {
url: "./src/db/sqlite.db",
}
} satisfies Config;
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,14 @@
"docs:preview": "vitepress preview docs"
},
"dependencies": {
"drizzle-orm": "^0.28.6",
"drizzle-typebox": "^0.1.1",
"elysia": "latest"
},
"devDependencies": {
"better-sqlite3": "^8.6.0",
"bun-types": "latest",
"drizzle-kit": "^0.19.13",
"vitepress": "^1.0.0-rc.15"
},
"peerDependencies": {
Expand Down
26 changes: 26 additions & 0 deletions src/db/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@

import { Database } from 'bun:sqlite';
import { migrate } from "drizzle-orm/better-sqlite3/migrator";
import { drizzle, BunSQLiteDatabase } from 'drizzle-orm/bun-sqlite';

import { users } from './schemas/users';

const sqlite = new Database('sqlite.db');
const db: BunSQLiteDatabase = drizzle(sqlite);

migrate(db, { migrationsFolder: "./migrations" });

console.log("Migrations complete.")
const data = {
email: '[email protected]',
username: 'test',
password: 'test',
bio: 'test',
image: 'test',
}
console.log("Inserting user: ", data)
await db.insert(users).values(data)
console.log("User inserted")

const userResult = await db.select().from(users);
console.log("User result: ", userResult);
10 changes: 10 additions & 0 deletions src/db/migrations/0000_wandering_eternals.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
CREATE TABLE `users` (
`id` integer PRIMARY KEY NOT NULL,
`email` text NOT NULL,
`bio` text NOT NULL,
`image` text NOT NULL,
`password` text NOT NULL,
`username` text NOT NULL,
`created_at` text DEFAULT CURRENT_DATE,
`updated_at` text DEFAULT CURRENT_DATE
);
81 changes: 81 additions & 0 deletions src/db/migrations/meta/0000_snapshot.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
{
"version": "5",
"dialect": "sqlite",
"id": "2cdf5b8c-f514-4adc-9975-d13386512c76",
"prevId": "00000000-0000-0000-0000-000000000000",
"tables": {
"users": {
"name": "users",
"columns": {
"id": {
"name": "id",
"type": "integer",
"primaryKey": true,
"notNull": true,
"autoincrement": false
},
"email": {
"name": "email",
"type": "text",
"primaryKey": false,
"notNull": true,
"autoincrement": false
},
"bio": {
"name": "bio",
"type": "text",
"primaryKey": false,
"notNull": true,
"autoincrement": false
},
"image": {
"name": "image",
"type": "text",
"primaryKey": false,
"notNull": true,
"autoincrement": false
},
"password": {
"name": "password",
"type": "text",
"primaryKey": false,
"notNull": true,
"autoincrement": false
},
"username": {
"name": "username",
"type": "text",
"primaryKey": false,
"notNull": true,
"autoincrement": false
},
"created_at": {
"name": "created_at",
"type": "text",
"primaryKey": false,
"notNull": false,
"autoincrement": false,
"default": "CURRENT_DATE"
},
"updated_at": {
"name": "updated_at",
"type": "text",
"primaryKey": false,
"notNull": false,
"autoincrement": false,
"default": "CURRENT_DATE"
}
},
"indexes": {},
"foreignKeys": {},
"compositePrimaryKeys": {},
"uniqueConstraints": {}
}
},
"enums": {},
"_meta": {
"schemas": {},
"tables": {},
"columns": {}
}
}
13 changes: 13 additions & 0 deletions src/db/migrations/meta/_journal.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"version": "5",
"dialect": "sqlite",
"entries": [
{
"idx": 0,
"version": "5",
"when": 1695555519769,
"tag": "0000_wandering_eternals",
"breakpoints": false
}
]
}
24 changes: 24 additions & 0 deletions src/db/schemas/users.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { sql } from "drizzle-orm";
import { integer, sqliteTable, text} from "drizzle-orm/sqlite-core";
import { createInsertSchema, createSelectSchema } from 'drizzle-typebox';



export const users = sqliteTable('users', {
id: integer('id').primaryKey(),
email: text('email').notNull(),
bio: text('bio').notNull(),
image: text('image').notNull(),
password: text('password').notNull(),
username: text('username').notNull(),
created_at: text('created_at').default(sql`CURRENT_DATE`),
updated_at: text('updated_at').default(sql`CURRENT_DATE`),
});



// Schema for inserting a user - can be used to validate API requests
export const insertUserSchema = createInsertSchema(users);

// Schema for selecting a user - can be used to validate API responses
export const selectUserSchema = createSelectSchema(users);
Loading