Skip to content

Commit

Permalink
fix(setup): auto setup of database #6
Browse files Browse the repository at this point in the history
  • Loading branch information
adrienZ committed Sep 3, 2024
1 parent 1af84a7 commit 80d5481
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 17 deletions.
16 changes: 0 additions & 16 deletions playground/server/plugins/database.setup.ts

This file was deleted.

4 changes: 3 additions & 1 deletion src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export default defineNuxtModule<ModuleOptions>({

nitroConfig.scheduledTasks = {
...nitroConfig.scheduledTasks,
// @daily https://crontab.guru/#00_00_*_*_*
"* * * * *": ["slip:db:expired-sessions"],
};
});
Expand All @@ -60,7 +61,8 @@ export default defineNuxtModule<ModuleOptions>({
nitro.plugins = nitro.plugins || [];

// add your custom plugin
nitro.plugins.push(resolver.resolve("runtime/server/plugins/sessions.plugin"));
// nitro.plugins.push(resolver.resolve("runtime/server/plugins/sessions.plugin"));
// nitro.plugins.push(resolver.resolve("runtime/server/plugins/auto-setup.plugin"));
});
},
});
22 changes: 22 additions & 0 deletions src/runtime/server/plugins/auto-setup.plugin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { useSlipAuth } from "../utils/useSlipAuth";
import {
defineNitroPlugin,

Check failure on line 3 in src/runtime/server/plugins/auto-setup.plugin.ts

View workflow job for this annotation

GitHub Actions / install-lint-and-test

'"#imports"' has no exported member named 'defineNitroPlugin'. Did you mean 'defineNuxtPlugin'?
useRuntimeConfig,
// @ts-expect-error experimental feature
useDatabase,
} from "#imports";

export default defineNitroPlugin(async (nitro) => {

Check failure on line 9 in src/runtime/server/plugins/auto-setup.plugin.ts

View workflow job for this annotation

GitHub Actions / install-lint-and-test

Parameter 'nitro' implicitly has an 'any' type.
const config = useRuntimeConfig();
const db = useDatabase();

if (config.slipAuth.database.dialect === "sqlite") {
await db.sql`CREATE TABLE IF NOT EXISTS slip_auth_users ("id" TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP PRIMARY KEY, "email" TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP UNIQUE, "created_at" TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, "updated_at" TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP)`;
await db.sql`CREATE TABLE IF NOT EXISTS slip_auth_sessions ("id" TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP PRIMARY KEY, "expires_at" INTEGER NOT NULL, "ip" TEXT, "ua" TEXT, "user_id" TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP, "created_at" TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, "updated_at" TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, FOREIGN KEY (user_id) REFERENCES slip_auth_users(id))`;
await db.sql`CREATE TABLE IF NOT EXISTS slip_auth_oauth_accounts ("provider_id" TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP, "provider_user_id" TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP, "user_id" TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP, "created_at" TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, "updated_at" TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,PRIMARY KEY (provider_id, provider_user_id), FOREIGN KEY (user_id) REFERENCES slip_auth_users(id))`;
}

nitro.hooks.hookOnce("request", () => {
useSlipAuth().checkDbAndTables("sqlite");
});
});

0 comments on commit 80d5481

Please sign in to comment.