More retooling of the DB
This commit is contained in:
@@ -1,13 +1,14 @@
|
|||||||
import Database from 'better-sqlite3'
|
import Database from 'better-sqlite3'
|
||||||
import Services from '$lib/services.json'
|
|
||||||
import { generateUUID } from '$lib/utils'
|
import { generateUUID } from '$lib/utils'
|
||||||
|
|
||||||
const db = new Database('./src/lib/server/users.db', { verbose: console.info })
|
const db = new Database('./src/lib/server/users.db', { verbose: console.info })
|
||||||
db.pragma('foreign_keys = ON')
|
db.pragma('foreign_keys = ON')
|
||||||
const initUsersTable = 'CREATE TABLE IF NOT EXISTS Users(id VARCHAR(36) PRIMARY KEY, username VARCHAR(30) UNIQUE NOT NULL, password VARCHAR(72) NOT NULL)'
|
const initUsersTable = 'CREATE TABLE IF NOT EXISTS Users(id VARCHAR(36) PRIMARY KEY, username VARCHAR(30) UNIQUE NOT NULL, password VARCHAR(72) NOT NULL)'
|
||||||
|
const initServicesTable = 'CREATE TABLE IF NOT EXISTS Services(id VARCHAR(36) PRIMARY KEY, type VARCHAR(64) NOT NULL, userId TEXT NOT NULL, url TEXT NOT NULL)'
|
||||||
const initConnectionsTable =
|
const initConnectionsTable =
|
||||||
'CREATE TABLE IF NOT EXISTS Connections(id VARCHAR(36) PRIMARY KEY, userId VARCHAR(36) NOT NULL, serviceType VARCHAR(64) NOT NULL, serviceUser TEXT NOT NULL, serviceUrl TEXT NOT NULL, accessToken TEXT NOT NULL, refreshToken TEXT, expiry INTEGER, FOREIGN KEY(userId) REFERENCES Users(id))'
|
'CREATE TABLE IF NOT EXISTS Connections(id VARCHAR(36) PRIMARY KEY, userId VARCHAR(36) NOT NULL, serviceId VARCHAR(36), accessToken TEXT NOT NULL, refreshToken TEXT, expiry INTEGER, FOREIGN KEY(userId) REFERENCES Users(id), FOREIGN KEY(serviceId) REFERENCES Services(id))'
|
||||||
db.exec(initUsersTable)
|
db.exec(initUsersTable)
|
||||||
|
db.exec(initServicesTable)
|
||||||
db.exec(initConnectionsTable)
|
db.exec(initConnectionsTable)
|
||||||
|
|
||||||
export interface User {
|
export interface User {
|
||||||
@@ -16,38 +17,76 @@ export interface User {
|
|||||||
password: string
|
password: string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export type serviceType = 'jellyfin' | 'youtube-music'
|
||||||
|
|
||||||
export interface Service {
|
export interface Service {
|
||||||
|
id: string
|
||||||
|
type: serviceType
|
||||||
|
userId: string
|
||||||
|
url: URL
|
||||||
|
}
|
||||||
|
|
||||||
|
interface DBServiceRow {
|
||||||
|
id: string
|
||||||
|
type: string
|
||||||
|
userId: string
|
||||||
url: string
|
url: string
|
||||||
serviceType: 'jellyfin' | 'youtube-music'
|
|
||||||
displayName: string
|
|
||||||
icon: string
|
|
||||||
primaryColor: string
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface Connection {
|
export interface Connection {
|
||||||
id: string
|
id: string
|
||||||
user: User
|
user: User
|
||||||
service: Service
|
service: Service
|
||||||
serviceUser: string
|
|
||||||
accessToken: string
|
accessToken: string
|
||||||
refreshToken: string | null
|
refreshToken: string | null
|
||||||
expiry: number | null
|
expiry: number | null
|
||||||
}
|
}
|
||||||
|
|
||||||
|
interface DBConnectionRow {
|
||||||
|
id: string
|
||||||
|
userId: string
|
||||||
|
serviceId: string
|
||||||
|
accessToken: string
|
||||||
|
refreshToken: string
|
||||||
|
expiry: number
|
||||||
|
}
|
||||||
|
|
||||||
export class Users {
|
export class Users {
|
||||||
|
static getUser = (id: string): User => {
|
||||||
|
return db.prepare('SELECT * FROM Users WHERE id = ?').get(id) as User
|
||||||
|
}
|
||||||
|
|
||||||
static addUser = (username: string, hashedPassword: string): User => {
|
static addUser = (username: string, hashedPassword: string): User => {
|
||||||
const userId = generateUUID()
|
const userId = generateUUID()
|
||||||
db.prepare('INSERT INTO Users(id, username, password) VALUES(?, ?, ?)').run(userId, username, hashedPassword)
|
db.prepare('INSERT INTO Users(id, username, password) VALUES(?, ?, ?)').run(userId, username, hashedPassword)
|
||||||
return this.getUser(userId)
|
return this.getUser(userId)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
static getUser = (id: string): User => {
|
export class Services {
|
||||||
return db.prepare('SELECT * FROM Users WHERE id = ?').get(id) as User
|
static getService = (id: string): Service => {
|
||||||
|
const { type, userId, url } = db.prepare('SELECT * FROM Users WHERE id = ?').get(id) as DBServiceRow
|
||||||
|
const service: Service = { id, type: type as serviceType, userId, url: new URL(url) }
|
||||||
|
return service
|
||||||
|
}
|
||||||
|
|
||||||
|
static addService = (type: serviceType, userId: string, url: URL): Service => {
|
||||||
|
const serviceId = generateUUID()
|
||||||
|
db.prepare('INSERT INTO Services(id, type, userId, url) VALUES(?, ?, ?, ?)').run(serviceId, type, userId, url.origin)
|
||||||
|
return this.getService(serviceId)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export class Connections {
|
export class Connections {
|
||||||
// static getConnection = (id: string): Connection => {
|
static getConnection = (id: string): Connection => {
|
||||||
// const connectionRow = db.prepare('SELECT * FROM Connections WHERE id = ?').get(id)
|
const { userId, serviceId, accessToken, refreshToken, expiry } = db.prepare('SELECT * FROM Connections WHERE id = ?').get(id) as DBConnectionRow
|
||||||
// }
|
const connection: Connection = { id, user: Users.getUser(userId), service: Services.getService(serviceId), accessToken, refreshToken, expiry }
|
||||||
|
return connection
|
||||||
|
}
|
||||||
|
|
||||||
|
static addConnection = (userId: string, serviceId: string, accessToken: string, refreshToken: string | null, expiry: number | null): Connection => {
|
||||||
|
const connectionId = generateUUID()
|
||||||
|
db.prepare('INSERT INTO Connections(id, userId, serviceId, accessToken, refreshToken, expiry) VALUES(?, ?, ?, ?, ?, ?)').run(connectionId, userId, serviceId, accessToken, refreshToken, expiry)
|
||||||
|
return this.getConnection(connectionId)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user