-
Notifications
You must be signed in to change notification settings - Fork 0
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
0 parents
commit c0eadfc
Showing
14 changed files
with
1,562 additions
and
0 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 @@ | ||
DATABASE_URL="" |
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,11 @@ | ||
# Dependencies | ||
node_modules | ||
|
||
# Build output directory | ||
build | ||
|
||
# Distribution directory | ||
dist | ||
|
||
# Environment variables | ||
.env |
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,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2024 Dashxboard | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
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,47 @@ | ||
# Dashxboard — @dashxboard/db | ||
|
||
From ideas to actions. The unofficial platform designed for the **Stronghold (SHx)** community. | ||
|
||
**[Website](https://dashxboard.com)** • **[Discord](https://discord.gg/eJhzDbKbdj)** • **[GitHub](https://github.com/dashxboard)** | ||
|
||
## Overview | ||
|
||
**@dashxboard/db** handles database connections and migrations for the **Dashboard** platform. | ||
|
||
## Installation | ||
|
||
Include this package as a dependency in the `package.json` files for the `@dashxboard/web` and `@dashxboard/bot` repositories: | ||
|
||
```bash | ||
"dependencies": { | ||
"@dashxboard/db": "git+https://${GITHUB_TOKEN}:[email protected]/dashxboard/dashxboard-db.git#main" | ||
} | ||
``` | ||
|
||
## Usage | ||
|
||
Import the database as a standalone package: | ||
|
||
```typescript | ||
import { db } from "@dashxboard/db"; | ||
``` | ||
|
||
## Scripts | ||
|
||
- `npm run build` — Compiles the package. | ||
- `npm run migrate` — Executes database migrations. | ||
- `npm run migrate: down` — Rolls back the last migration. | ||
- `npm run migrate:list` — Displays a list of all migrations. | ||
|
||
## Contributing | ||
|
||
Dashxboard is open-source, and contributions are welcome! You can help by: | ||
|
||
- Submitting pull requests (_for minor changes_). | ||
- Reporting bugs or suggesting features (_for major changes_). | ||
- Enhancing the documentation. | ||
- Engaging with the community on Discord. | ||
|
||
## License | ||
|
||
This project is licensed under the [MIT license](https://choosealicense.com/licenses/mit/). |
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,21 @@ | ||
import { Kysely, sql, PostgresDialect, Transaction } from "kysely"; | ||
import pg from "pg"; | ||
import { DB } from "./schema.js"; | ||
|
||
if (!process.env.DATABASE_URL) { | ||
throw new Error("DATABASE_URL has not been set!"); | ||
} | ||
|
||
const { Pool } = pg; | ||
|
||
export const db = new Kysely<DB>({ | ||
dialect: new PostgresDialect({ | ||
pool: new Pool({ | ||
connectionString: process.env.DATABASE_URL, | ||
}), | ||
}), | ||
}); | ||
|
||
export { sql } from "kysely"; | ||
export type TransactionDB = Transaction<DB>; | ||
export type KyselyDB = Kysely<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,3 @@ | ||
import dotenv from "dotenv"; | ||
|
||
dotenv.config({ path: "./.env", debug: 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 @@ | ||
export * from "./database.js"; |
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,51 @@ | ||
import "./env.js"; | ||
import path from "path"; | ||
import url from "url"; | ||
import fs from "fs/promises"; | ||
import { Migrator, FileMigrationProvider } from "kysely"; | ||
import { db } from "./database.js"; | ||
|
||
const option = process.env.MIGRATE_OPTION ?? "latest"; | ||
const dirname = url.fileURLToPath(new URL(".", import.meta.url)); | ||
|
||
const migrator = new Migrator({ | ||
db, | ||
provider: new FileMigrationProvider({ | ||
fs, | ||
path, | ||
migrationFolder: path.join(dirname, "./migrations"), | ||
}), | ||
}); | ||
|
||
if (option === "list") { | ||
const migrations = await migrator.getMigrations(); | ||
for (const migration of migrations) { | ||
console.log(`${migration.name} - Executed at: ${migration.executedAt}`); | ||
} | ||
await db.destroy(); | ||
process.exit(0); | ||
} | ||
|
||
const { error, results } = await (option === "latest" | ||
? migrator.migrateToLatest() | ||
: migrator.migrateDown()); | ||
|
||
results?.forEach((it) => { | ||
if (it.status === "Success") { | ||
console.log( | ||
`[${it.direction}] migration "${it.migrationName}" was executed successfully!` | ||
); | ||
} else if (it.status === "Error") { | ||
console.error( | ||
`[${it.direction}] failed to execute migration "${it.migrationName}"!` | ||
); | ||
} | ||
}); | ||
|
||
if (error) { | ||
console.error("Failed to migrate:"); | ||
console.error(error); | ||
process.exit(1); | ||
} | ||
|
||
await db.destroy(); |
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,150 @@ | ||
import { Kysely, sql } from "kysely"; | ||
import { SnowflakeDataType, uuidColumnBuilder } from "../utils.js"; | ||
|
||
export async function up(db: Kysely<any>): Promise<void> { | ||
// Users | ||
await db.schema | ||
.createTable("users") | ||
.addColumn("id", "uuid", uuidColumnBuilder) | ||
.addColumn("snowflake", SnowflakeDataType, (col) => col.notNull().unique()) | ||
.addColumn("username", "text", (col) => col.notNull()) | ||
.addColumn("discriminator", "varchar(4)", (col) => col.notNull()) | ||
.addColumn("avatar", "text", (col) => col.notNull()) | ||
.addColumn("public", "boolean", (col) => col.notNull().defaultTo(false)) | ||
.addColumn("moderator", "boolean", (col) => col.notNull().defaultTo(false)) | ||
.addColumn("reputation", "integer", (col) => col.notNull().defaultTo(0)) | ||
.execute(); | ||
|
||
await db.schema | ||
.createIndex("users_snowflake_index") | ||
.on("users") | ||
.column("snowflake") | ||
.execute(); | ||
|
||
await db.schema | ||
.createIndex("users_reputation_index") | ||
.on("users") | ||
.column("reputation") | ||
.execute(); | ||
|
||
// Channels | ||
await db.schema | ||
.createTable("channels") | ||
.addColumn("id", "uuid", uuidColumnBuilder) | ||
.addColumn("snowflake", SnowflakeDataType, (col) => col.notNull().unique()) | ||
.addColumn("name", "text", (col) => col.notNull()) | ||
.addColumn("type", "int2", (col) => col.notNull()) | ||
.addColumn("topic", "text", (col) => col.notNull()) | ||
.execute(); | ||
|
||
// Posts (Proposals) | ||
await db.schema | ||
.createTable("posts") | ||
.addColumn("id", "uuid", uuidColumnBuilder) | ||
.addColumn("snowflake", SnowflakeDataType, (col) => col.notNull().unique()) | ||
.addColumn("title", "text", (col) => col.notNull()) | ||
.addColumn("locked", "boolean", (col) => col.notNull()) | ||
.addColumn("created", "timestamptz", (col) => col.notNull()) | ||
.addColumn("edited", "timestamptz") | ||
.addColumn("active", "timestamptz", (col) => | ||
col.notNull().defaultTo(sql`now()`) | ||
) | ||
.addColumn("indexed", "boolean", (col) => col.notNull().defaultTo(true)) | ||
.addColumn("user", SnowflakeDataType) | ||
.addColumn("channel", SnowflakeDataType) | ||
.addColumn("status", SnowflakeDataType) | ||
.addColumn("category", SnowflakeDataType) | ||
.execute(); | ||
|
||
await db.schema | ||
.createIndex("posts_snowflake_index") | ||
.on("posts") | ||
.column("snowflake") | ||
.execute(); | ||
|
||
await db.schema | ||
.createIndex("posts_indexed_index") | ||
.on("posts") | ||
.column("indexed") | ||
.execute(); | ||
|
||
await db.schema | ||
.createIndex("posts_user_index") | ||
.on("posts") | ||
.column("user") | ||
.execute(); | ||
|
||
await db.schema | ||
.createIndex("posts_channel_index") | ||
.on("posts") | ||
.column("channel") | ||
.execute(); | ||
|
||
// Messages | ||
await db.schema | ||
.createTable("messages") | ||
.addColumn("id", "uuid", uuidColumnBuilder) | ||
.addColumn("snowflake", SnowflakeDataType, (col) => col.notNull().unique()) | ||
.addColumn("content", "text", (col) => col.notNull()) | ||
.addColumn("created", "timestamptz", (col) => col.notNull()) | ||
.addColumn("edited", "timestamptz") | ||
.addColumn("user", SnowflakeDataType, (col) => col.notNull()) | ||
.addColumn("post", SnowflakeDataType, (col) => col.notNull()) | ||
.addColumn("reply", SnowflakeDataType) | ||
.execute(); | ||
|
||
await db.schema | ||
.createIndex("messages_snowflake_index") | ||
.on("messages") | ||
.column("snowflake") | ||
.execute(); | ||
|
||
await db.schema | ||
.createIndex("messages_user_index") | ||
.on("messages") | ||
.column("user") | ||
.execute(); | ||
|
||
await db.schema | ||
.createIndex("messages_post_index") | ||
.on("messages") | ||
.column("post") | ||
.execute(); | ||
|
||
await db.schema | ||
.createIndex("messages_reply_index") | ||
.on("messages") | ||
.column("reply") | ||
.execute(); | ||
|
||
// Attachments | ||
await db.schema | ||
.createTable("attachments") | ||
.addColumn("id", "uuid", uuidColumnBuilder) | ||
.addColumn("snowflake", SnowflakeDataType, (col) => col.notNull().unique()) | ||
.addColumn("url", "text", (col) => col.notNull()) | ||
.addColumn("name", "text", (col) => col.notNull()) | ||
.addColumn("content", "text") | ||
.addColumn("message", SnowflakeDataType, (col) => col.notNull()) | ||
.execute(); | ||
|
||
await db.schema | ||
.createIndex("attachments_snowflake_index") | ||
.on("attachments") | ||
.column("snowflake") | ||
.execute(); | ||
|
||
await db.schema | ||
.createIndex("attachments_message_index") | ||
.on("attachments") | ||
.column("message") | ||
.execute(); | ||
} | ||
|
||
export async function down(db: Kysely<any>): Promise<void> { | ||
await db.schema.dropTable("attachments").execute(); | ||
await db.schema.dropTable("messages").execute(); | ||
await db.schema.dropTable("posts").execute(); | ||
await db.schema.dropTable("channels").execute(); | ||
await db.schema.dropTable("users").execute(); | ||
} |
Oops, something went wrong.