Browse Source

start drizzle migration

pull/1619/head
Bernd Storath 3 months ago
parent
commit
b456f9dad5
  1. 2
      src/.gitignore
  2. 10
      src/drizzle.config.ts
  3. 3
      src/package.json
  4. 839
      src/pnpm-lock.yaml
  5. 105
      src/services/database/schema.ts
  6. 7
      src/services/database/sqlite.ts

2
src/.gitignore

@ -22,3 +22,5 @@ logs
.env
.env.*
!.env.example
wg0.db

10
src/drizzle.config.ts

@ -0,0 +1,10 @@
import { defineConfig } from 'drizzle-kit';
export default defineConfig({
out: './migrations',
schema: './services/database/schema.ts',
dialect: 'sqlite',
dbCredentials: {
url: 'file:./wg0.db',
},
});

3
src/package.json

@ -18,6 +18,7 @@
},
"dependencies": {
"@eschricht/nuxt-color-mode": "^1.1.5",
"@libsql/client": "^0.14.0",
"@nuxtjs/i18n": "^9.1.1",
"@nuxtjs/tailwindcss": "^6.12.2",
"@pinia/nuxt": "^0.9.0",
@ -28,6 +29,7 @@
"cidr-tools": "^11.0.2",
"crc-32": "^1.2.2",
"debug": "^4.4.0",
"drizzle-orm": "^0.38.3",
"ip-bigint": "^8.2.0",
"is-cidr": "^5.1.0",
"is-ip": "^5.0.1",
@ -49,6 +51,7 @@
"@types/debug": "^4.1.12",
"@types/qrcode": "^1.5.5",
"@types/semver": "^7.5.8",
"drizzle-kit": "^0.30.1",
"eslint": "^9.17.0",
"eslint-config-prettier": "^9.1.0",
"prettier": "^3.4.2",

839
src/pnpm-lock.yaml

File diff suppressed because it is too large

105
src/services/database/schema.ts

@ -0,0 +1,105 @@
import { sql } from 'drizzle-orm';
import { int, sqliteTable, text } from 'drizzle-orm/sqlite-core';
export const usersTable = sqliteTable('users_table', {
id: int().primaryKey({ autoIncrement: true }),
username: text().notNull(),
password: text().notNull(),
email: text(),
name: text().notNull(),
role: int().notNull(),
enabled: int().notNull().default(1),
createdAt: text('created_at')
.notNull()
.default(sql`(CURRENT_TIMESTAMP)`),
updatedAt: text('updated_at')
.notNull()
.default(sql`(CURRENT_TIMESTAMP)`)
.$onUpdate(() => sql`(CURRENT_TIMESTAMP)`),
});
export const clientsTable = sqliteTable('clients_table', {
id: int().primaryKey({ autoIncrement: true }),
name: text().notNull(),
ipv4Address: text('ipv4_address').notNull().unique(),
ipv6Address: text('ipv6_address').notNull().unique(),
privateKey: text('private_key').notNull(),
publicKey: text('public_key').notNull(),
preSharedKey: text('pre_shared_key').notNull(),
expiresAt: text('expires_at'),
allowedIps: text('allowed_ips', { mode: 'json' }).notNull(),
serverAllowedIps: text('server_allowed_ips', { mode: 'json' }).notNull(),
oneTimeLink: int('one_time_link').references(() => oneTimeLinksTable.id),
persistentKeepalive: int('persistent_keepalive').notNull(),
mtu: int().notNull(),
dns: text({ mode: 'json' }).notNull(),
enabled: int({ mode: 'boolean' }).notNull(),
createdAt: text('created_at')
.notNull()
.default(sql`(CURRENT_TIMESTAMP)`),
updatedAt: text('updated_at')
.notNull()
.default(sql`(CURRENT_TIMESTAMP)`)
.$onUpdate(() => sql`(CURRENT_TIMESTAMP)`),
});
export const oneTimeLinksTable = sqliteTable('one_time_links_table', {
id: int().primaryKey({ autoIncrement: true }),
oneTimeLink: text('one_time_link').notNull(),
expiresAt: text('expires_at').notNull(),
createdAt: text('created_at')
.notNull()
.default(sql`(CURRENT_TIMESTAMP)`),
updatedAt: text('updated_at')
.notNull()
.default(sql`(CURRENT_TIMESTAMP)`)
.$onUpdate(() => sql`(CURRENT_TIMESTAMP)`),
});
// maybe support multiple interfaces in the future
export const interfaceTable = sqliteTable('interface_table', {
id: int().primaryKey({ autoIncrement: true }),
device: text().notNull(),
port: int().notNull(),
privateKey: text('private_key').notNull(),
publicKey: text('public_key').notNull(),
ipv4Cidr: text('ipv4_cidr').notNull(),
ipv6Cidr: text('ipv6_cidr').notNull(),
mtu: int().notNull(),
enabled: int({ mode: 'boolean' }).notNull(),
});
// default* means clients store it themselves
export const userConfigTable = sqliteTable('user_config_table', {
id: int()
.primaryKey({ autoIncrement: true })
.references(() => interfaceTable.id),
defaultMtu: int('default_mtu').notNull(),
defaultPersistentKeepalive: int('default_persistent_keepalive').notNull(),
defaultDns: text('default_dns', { mode: 'json' }).notNull(),
defaultAllowedIps: text('default_allowed_ips', { mode: 'json' }).notNull(),
host: text().notNull(),
port: int().notNull(),
});
export const hooksTable = sqliteTable('hooks_table', {
id: int()
.primaryKey({ autoIncrement: true })
.references(() => interfaceTable.id),
preUp: text('pre_up').notNull(),
postUp: text('post_up').notNull(),
preDown: text('pre_down').notNull(),
postDown: text('post_down').notNull(),
});
export const prometheusTable = sqliteTable('prometheus_table', {
id: int()
.primaryKey({ autoIncrement: true })
.references(() => interfaceTable.id),
password: text().notNull(),
});
export const generalTable = sqliteTable('general_table', {
id: int().primaryKey({ autoIncrement: true }),
sessionTimeout: int('session_timeout').notNull(),
});

7
src/services/database/sqlite.ts

@ -0,0 +1,7 @@
import { drizzle } from 'drizzle-orm/libsql';
import { createClient } from '@libsql/client';
const client = createClient({ url: 'file:/etc/wireguard/wg0.db' });
const db = drizzle({ client });
export default db;
Loading…
Cancel
Save