-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added registrations sqlite and updated examples
- Loading branch information
Showing
17 changed files
with
833 additions
and
66 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import type { Config } from "drizzle-kit"; | ||
import "dotenv/config"; | ||
|
||
export default { | ||
schema: "./playground/database/schema/", | ||
out: "./playground/database/migrations/", | ||
dialect: "sqlite", | ||
verbose: true, | ||
dbCredentials: { | ||
url: "./playground/database/sqlite.db", | ||
}, | ||
} satisfies Config; |
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,13 @@ | ||
import { drizzle, BetterSQLite3Database } from "drizzle-orm/better-sqlite3"; | ||
import Database from "better-sqlite3"; | ||
import registrations from "./schema/registrations"; | ||
|
||
const sqlite = new Database("./playground/database/sqlite.db"); | ||
|
||
const config = { | ||
schema: { registrations }, | ||
fileMustExist: true, | ||
}; | ||
const db: BetterSQLite3Database = drizzle(sqlite, config); | ||
|
||
export default db; |
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,8 @@ | ||
CREATE TABLE `registrations` ( | ||
`id` integer PRIMARY KEY AUTOINCREMENT NOT NULL, | ||
`name` text(255), | ||
`email` text(255), | ||
`created_at` text DEFAULT (CURRENT_TIMESTAMP) | ||
); | ||
--> statement-breakpoint | ||
CREATE UNIQUE INDEX `registrations_email_unique` ON `registrations` (`email`); |
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,63 @@ | ||
{ | ||
"version": "6", | ||
"dialect": "sqlite", | ||
"id": "3e843d89-52e3-4405-a94d-8b135dbf2d3f", | ||
"prevId": "00000000-0000-0000-0000-000000000000", | ||
"tables": { | ||
"registrations": { | ||
"name": "registrations", | ||
"columns": { | ||
"id": { | ||
"name": "id", | ||
"type": "integer", | ||
"primaryKey": true, | ||
"notNull": true, | ||
"autoincrement": true | ||
}, | ||
"name": { | ||
"name": "name", | ||
"type": "text(255)", | ||
"primaryKey": false, | ||
"notNull": false, | ||
"autoincrement": false | ||
}, | ||
"email": { | ||
"name": "email", | ||
"type": "text(255)", | ||
"primaryKey": false, | ||
"notNull": false, | ||
"autoincrement": false | ||
}, | ||
"created_at": { | ||
"name": "created_at", | ||
"type": "text", | ||
"primaryKey": false, | ||
"notNull": false, | ||
"autoincrement": false, | ||
"default": "(CURRENT_TIMESTAMP)" | ||
} | ||
}, | ||
"indexes": { | ||
"registrations_email_unique": { | ||
"name": "registrations_email_unique", | ||
"columns": [ | ||
"email" | ||
], | ||
"isUnique": true | ||
} | ||
}, | ||
"foreignKeys": {}, | ||
"compositePrimaryKeys": {}, | ||
"uniqueConstraints": {} | ||
} | ||
}, | ||
"enums": {}, | ||
"_meta": { | ||
"schemas": {}, | ||
"tables": {}, | ||
"columns": {} | ||
}, | ||
"internal": { | ||
"indexes": {} | ||
} | ||
} |
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,13 @@ | ||
{ | ||
"version": "7", | ||
"dialect": "sqlite", | ||
"entries": [ | ||
{ | ||
"idx": 0, | ||
"version": "6", | ||
"when": 1717878275568, | ||
"tag": "0000_deep_naoko", | ||
"breakpoints": true | ||
} | ||
] | ||
} |
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,30 @@ | ||
import { migrate } from "drizzle-orm/mysql2/migrator"; | ||
import mysql from "mysql2/promise"; | ||
import { drizzle } from "drizzle-orm/mysql2"; | ||
|
||
// get the database configuration from dotenv | ||
import "dotenv/config"; | ||
|
||
async function run() { | ||
const connection = await mysql.createConnection({ | ||
host: process.env.DB_HOST, | ||
port: process.env.DB_PORT ?? 3306, | ||
database: process.env.DB_DATABASE, | ||
user: process.env.DB_USERNAME, | ||
password: process.env.DB_PASSWORD, | ||
}); | ||
const db = drizzle(connection); | ||
|
||
// This will run migrations on the database, skipping the ones already applied | ||
await migrate(db, { migrationsFolder: "./database/migrations" }); | ||
// Don't forget to close the connection, otherwise the script will hang | ||
await connection.end(); | ||
} | ||
|
||
run() | ||
.catch((e) => { | ||
console.error("Migrations failed"); | ||
console.error(e); | ||
process.exit(1); | ||
}) | ||
.then(() => console.log("Migrations ran successfully")); |
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,9 @@ | ||
import { sql } from "drizzle-orm"; | ||
import { sqliteTable, integer, text } from "drizzle-orm/sqlite-core"; | ||
|
||
export default sqliteTable("registrations", { | ||
id: integer("id", { mode: "number" }).primaryKey({ autoIncrement: true }), | ||
name: text("name", { length: 255 }), | ||
email: text("email", { length: 255 }).unique(), | ||
createdAt: text("created_at").default(sql`(CURRENT_TIMESTAMP)`), | ||
}); |
Binary file not shown.
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 was deleted.
Oops, something went wrong.
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,8 @@ | ||
import db from "~/database"; | ||
|
||
export default defineEventHandler(async (event) => { | ||
// get the list of registrations from the database | ||
const results = await db.query.registrations.findMany(); | ||
|
||
return results; | ||
}); |
Oops, something went wrong.