mirror of https://github.com/wg-easy/wg-easy
10 changed files with 168 additions and 106 deletions
@ -1,105 +1,8 @@ |
|||
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(), |
|||
}); |
|||
export * from './schema/clients'; |
|||
export * from './schema/general'; |
|||
export * from './schema/hooks'; |
|||
export * from './schema/interface'; |
|||
export * from './schema/metrics'; |
|||
export * from './schema/oneTimeLinks'; |
|||
export * from './schema/userConfig'; |
|||
export * from './schema/users'; |
|||
|
@ -0,0 +1,28 @@ |
|||
import { sql } from 'drizzle-orm'; |
|||
import { int, sqliteTable, text } from 'drizzle-orm/sqlite-core'; |
|||
import { oneTimeLinks } from './oneTimeLinks'; |
|||
|
|||
export const clients = 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(() => oneTimeLinks.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)`), |
|||
}); |
@ -0,0 +1,14 @@ |
|||
import { sql } from 'drizzle-orm'; |
|||
import { int, sqliteTable, text } from 'drizzle-orm/sqlite-core'; |
|||
|
|||
export const general = sqliteTable('general_table', { |
|||
id: int().primaryKey({ autoIncrement: true }), |
|||
sessionTimeout: int('session_timeout').notNull(), |
|||
createdAt: text('created_at') |
|||
.notNull() |
|||
.default(sql`(CURRENT_TIMESTAMP)`), |
|||
updatedAt: text('updated_at') |
|||
.notNull() |
|||
.default(sql`(CURRENT_TIMESTAMP)`) |
|||
.$onUpdate(() => sql`(CURRENT_TIMESTAMP)`), |
|||
}); |
@ -0,0 +1,20 @@ |
|||
import { sql } from 'drizzle-orm'; |
|||
import { int, sqliteTable, text } from 'drizzle-orm/sqlite-core'; |
|||
import { wgInterface } from './interface'; |
|||
|
|||
export const hooks = sqliteTable('hooks_table', { |
|||
id: int() |
|||
.primaryKey({ autoIncrement: true }) |
|||
.references(() => wgInterface.id), |
|||
preUp: text('pre_up').notNull(), |
|||
postUp: text('post_up').notNull(), |
|||
preDown: text('pre_down').notNull(), |
|||
postDown: text('post_down').notNull(), |
|||
createdAt: text('created_at') |
|||
.notNull() |
|||
.default(sql`(CURRENT_TIMESTAMP)`), |
|||
updatedAt: text('updated_at') |
|||
.notNull() |
|||
.default(sql`(CURRENT_TIMESTAMP)`) |
|||
.$onUpdate(() => sql`(CURRENT_TIMESTAMP)`), |
|||
}); |
@ -0,0 +1,22 @@ |
|||
import { sql } from 'drizzle-orm'; |
|||
import { int, sqliteTable, text } from 'drizzle-orm/sqlite-core'; |
|||
|
|||
// maybe support multiple interfaces in the future
|
|||
export const wgInterface = 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(), |
|||
createdAt: text('created_at') |
|||
.notNull() |
|||
.default(sql`(CURRENT_TIMESTAMP)`), |
|||
updatedAt: text('updated_at') |
|||
.notNull() |
|||
.default(sql`(CURRENT_TIMESTAMP)`) |
|||
.$onUpdate(() => sql`(CURRENT_TIMESTAMP)`), |
|||
}); |
@ -0,0 +1,17 @@ |
|||
import { sql } from 'drizzle-orm'; |
|||
import { int, sqliteTable, text } from 'drizzle-orm/sqlite-core'; |
|||
import { wgInterface } from './interface'; |
|||
|
|||
export const prometheus = sqliteTable('prometheus_table', { |
|||
id: int() |
|||
.primaryKey({ autoIncrement: true }) |
|||
.references(() => wgInterface.id), |
|||
password: text().notNull(), |
|||
createdAt: text('created_at') |
|||
.notNull() |
|||
.default(sql`(CURRENT_TIMESTAMP)`), |
|||
updatedAt: text('updated_at') |
|||
.notNull() |
|||
.default(sql`(CURRENT_TIMESTAMP)`) |
|||
.$onUpdate(() => sql`(CURRENT_TIMESTAMP)`), |
|||
}); |
@ -0,0 +1,15 @@ |
|||
import { sql } from 'drizzle-orm'; |
|||
import { int, sqliteTable, text } from 'drizzle-orm/sqlite-core'; |
|||
|
|||
export const oneTimeLinks = 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)`), |
|||
}); |
@ -0,0 +1,23 @@ |
|||
import { sql } from 'drizzle-orm'; |
|||
import { int, sqliteTable, text } from 'drizzle-orm/sqlite-core'; |
|||
import { wgInterface } from './interface'; |
|||
|
|||
// default* means clients store it themselves
|
|||
export const userConfig = sqliteTable('user_config_table', { |
|||
id: int() |
|||
.primaryKey({ autoIncrement: true }) |
|||
.references(() => wgInterface.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(), |
|||
createdAt: text('created_at') |
|||
.notNull() |
|||
.default(sql`(CURRENT_TIMESTAMP)`), |
|||
updatedAt: text('updated_at') |
|||
.notNull() |
|||
.default(sql`(CURRENT_TIMESTAMP)`) |
|||
.$onUpdate(() => sql`(CURRENT_TIMESTAMP)`), |
|||
}); |
@ -0,0 +1,19 @@ |
|||
import { sql } from 'drizzle-orm'; |
|||
import { int, sqliteTable, text } from 'drizzle-orm/sqlite-core'; |
|||
|
|||
export const users = 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)`), |
|||
}); |
@ -1,7 +1,8 @@ |
|||
import { drizzle } from 'drizzle-orm/libsql'; |
|||
import { createClient } from '@libsql/client'; |
|||
import * as schema from './schema'; |
|||
|
|||
const client = createClient({ url: 'file:/etc/wireguard/wg0.db' }); |
|||
const db = drizzle({ client }); |
|||
const db = drizzle({ client, schema }); |
|||
|
|||
export default db; |
|||
|
Loading…
Reference in new issue