1
- import { Pool , ResultSetHeader , RowDataPacket } from "mysql2" ;
1
+ import { FieldPacket , Pool , ResultSetHeader , RowDataPacket } from "mysql2" ;
2
2
import { Util } from "./Util" ;
3
3
import { CONFIG } from "./config" ;
4
+ import { IChannel } from "./types" ;
4
5
5
6
const mysql = require ( 'mysql2' ) ;
6
7
@@ -44,8 +45,12 @@ export class Database {
44
45
}
45
46
46
47
// contains affectedRows and lastInsertId properties.
47
- public async update ( ) {
48
- // TODO
48
+ public async insertGetId ( sql : string , parameters ?: unknown [ ] ) : Promise < number > {
49
+
50
+ const [ result , fields ] : [ ResultSetHeader , FieldPacket [ ] ] = await this . pool . promise ( )
51
+ . query < ResultSetHeader > ( sql , parameters ) ;
52
+
53
+ return result . insertId ;
49
54
}
50
55
51
56
async ping ( ) : Promise < boolean > {
@@ -61,22 +66,38 @@ export class Database {
61
66
return rows . length && rows [ 0 ] [ "cnt" ] == 0 ;
62
67
}
63
68
64
- async createChannel ( name : string ) : Promise < boolean > {
69
+ async createChannel ( name : string , ipAddress : string ) : Promise < boolean > {
65
70
66
71
if ( ! await this . checkIfChannelNameAvailable ( name ) ) {
67
72
throw "Channel with name '" + name + "' is not available" ;
68
73
}
69
74
70
- const sk = Util . generateStreamKey ( ) ;
75
+ const streamKey : string = Util . generateStreamKey ( ) ;
76
+ const authKey : string = Util . randomString ( 32 , 'ak_' ) ;
71
77
72
- const result = await this . pool . promise ( )
73
- . execute < ResultSetHeader > ( "INSERT INTO channels (created_at, name, stream_key) VALUES (UTC_TIMESTAMP(), ?, ?)" , [
74
- name , sk
75
- ] ) ;
78
+ const result = await this . insertGetId (
79
+ "INSERT INTO channels (created_at, ip_address, name, auth_key, stream_key) VALUES (UTC_TIMESTAMP(), ?, ?, ?, ? )" ,
80
+ [ ipAddress , name , authKey , streamKey ]
81
+ ) ;
76
82
77
83
return true ;
78
84
}
79
85
86
+ async findChannelById ( id : number ) : Promise < IChannel > {
87
+ const result : IChannel [ ] = await this . select < IChannel > ( "SELECT * FROM channels WHERE id = ?" , [ id ] ) ;
88
+ return result . length ? result [ 0 ] : null ;
89
+ }
90
+
91
+ async findChannelByAuthToken ( token : string ) : Promise < IChannel | null > {
92
+ const result = await this . select < IChannel > ( "SELECT * FROM channels WHERE stream_key = ? LIMIT 1" , [ token ] ) ;
93
+ return result . length ? result [ 0 ] : null ;
94
+ }
95
+
96
+ async findChannelByName ( name : string ) : Promise < IChannel > {
97
+ const result : IChannel [ ] = await this . select < IChannel > ( "SELECT * FROM channels WHERE name = ? LIMIT 1" , [ name ] ) ;
98
+ return result . length ? result [ 0 ] : null ;
99
+ }
100
+
80
101
async createNewStream ( name : string , ipAddress : string , streamInfo : string ) : Promise < void > {
81
102
await this . pool . promise ( ) . execute ( "INSERT INTO streams (name, user_ip, started_at, ffprobe_json) VALUES (?, ?, UTC_TIMESTAMP(), ?)" , [
82
103
name , ipAddress , streamInfo
0 commit comments