Browse Source

split schema

pull/1619/head
Bernd Storath 3 months ago
parent
commit
05ccd34e67
  1. 113
      src/services/database/schema.ts
  2. 28
      src/services/database/schema/clients.ts
  3. 14
      src/services/database/schema/general.ts
  4. 20
      src/services/database/schema/hooks.ts
  5. 22
      src/services/database/schema/interface.ts
  6. 17
      src/services/database/schema/metrics.ts
  7. 15
      src/services/database/schema/oneTimeLinks.ts
  8. 23
      src/services/database/schema/userConfig.ts
  9. 19
      src/services/database/schema/users.ts
  10. 3
      src/services/database/sqlite.ts

113
src/services/database/schema.ts

@ -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';

28
src/services/database/schema/clients.ts

@ -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)`),
});

14
src/services/database/schema/general.ts

@ -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)`),
});

20
src/services/database/schema/hooks.ts

@ -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)`),
});

22
src/services/database/schema/interface.ts

@ -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)`),
});

17
src/services/database/schema/metrics.ts

@ -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)`),
});

15
src/services/database/schema/oneTimeLinks.ts

@ -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)`),
});

23
src/services/database/schema/userConfig.ts

@ -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)`),
});

19
src/services/database/schema/users.ts

@ -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)`),
});

3
src/services/database/sqlite.ts

@ -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…
Cancel
Save