SQL usage
Define typed database models and use the module's database class for common SQL operations.
Integration notes
Database class
The module includes a custom database class for working with SQL tables and rows.
TypeScript support
Database models and operations support TypeScript type checking.
Example use (TypeScript)
Configuration
Define the table and rows
Example database model and repository from the PokerTypescript resource.
Define the SQL table and rows
import * as Aquiver from '@freamee/server';
export interface PokerDatabaseInterface {
identifier: string;
img: string;
stat_betchips: number;
stat_wonchips: number;
stat_played: number;
stat_wongames: number;
}
export class PokerBase implements PokerDatabaseInterface {
identifier: string;
img: string;
stat_betchips: number;
stat_played: number;
stat_wonchips: number;
stat_wongames: number;
constructor(data: PokerDatabaseInterface) {
this.identifier = data.identifier;
this.img = data.img;
this.stat_betchips = data.stat_betchips;
this.stat_played = data.stat_played;
this.stat_wonchips = data.stat_wonchips;
this.stat_wongames = data.stat_wongames;
}
}
export class PokerBaseRepository extends Aquiver.BaseDatabase<PokerBase, PokerDatabaseInterface> {
constructor() {
super("poker_players");
}
constructModel(row: PokerDatabaseInterface): PokerBase {
return new PokerBase(row);
}
}
Configuration
Export the repository
Expose the repository through the server database class.
Export the repository classes
import { PokerBaseRepository } from './models/model-player';
export class ServerDatabase {
static PokerRepository = new PokerBaseRepository();
}
Updating a MySQL row
Configuration
Update a row
Update the matching player's stored statistics.
Update example
ServerDatabase.PokerRepository.update({
where: {
identifier: this.identifier,
},
set: {
stat_played: this.stat_played,
},
});
Finding a MySQL row
Configuration
Find a row
Find the matching row and load its values.
Find example
ServerDatabase.PokerRepository.find({
where: {
identifier: this.identifier,
},
limit: 1,
}).then((a) => {
const data = a[0];
this.img = data.img;
this.stat_betchips = data.stat_betchips;
this.stat_played = data.stat_played;
this.stat_wonchips = data.stat_wonchips;
this.stat_wongames = data.stat_wongames;
});
Inserting data into MySQL
Configuration
Insert a row
Insert the initial player statistics and load the stored data.
Insert function example
ServerDatabase.PokerRepository.insert({
identifier: this.identifier,
stat_betchips: 0,
stat_played: 0,
stat_wonchips: 0,
stat_wongames: 0,
}).then(() => {
this.load();
});