Database UnityApp Controller
Overview
In this article, we will explore the implementation of a UnityApp controller in a database. This controller will be responsible for managing Unity app configurations, adding files to Unity projects, and deleting app configurations. We will also discuss the structure of the database table and the typescript type that matches it.
Database Structure
The database table for Unity app configurations is defined in the migration_1.sql
file. The table has the following structure:
CREATE TABLE unity_app_config (
id INT PRIMARY KEY,
name VARCHAR(255) NOT NULL,
user_type VARCHAR(255) NOT NULL,
app_id VARCHAR(255) NOT NULL,
config JSON NOT NULL
);
Typescript Type: UnityAppConfig
The UnityAppConfig
type is a typescript type that matches the structure of the database table. It has the following properties:
type UnityAppConfig = {
id: number;
name: string;
userType: 'INSTRUCTOR' | 'STUDENT';
appId: string;
config: any;
};
This type has a typed value for the userType
property, which can be either 'INSTRUCTOR'
or 'STUDENT'
.
UnityApp Controller
The UnityApp controller is located in the database/unityapp.controller.ts
file. It exports the following functions:
getUnityAppConfig(app_id: string)
This function is already implemented and retrieves the app launch config associated with a specific unity id.
newAppConfig(name: string): UnityAppConfig
This function creates a new Unity app config. The name
parameter must be unique per user.
async newAppConfig(name: string): Promise<UnityAppConfig> {
const existingConfig = await this.getUnityAppConfig(name);
if (existingConfig) {
throw new Error(`App config with name ${name} already exists`);
}
const newConfig: UnityAppConfig = {
id: 0,
name,
userType: 'INSTRUCTOR', // default user type
appId: '',
config: {},
};
// save new config to database
return newConfig;
}
addFileTo(app_id: string, filepath: string): bool
This function adds a new file to a given Unity project. The file is also stored in the resources/projects/app_id
directory.
async addFileTo(app_id: string, filepath: string): Promise<boolean> {
// check if file already exists
const existingFile = await this.getFile(app_id, filepath);
if (existingFile) {
return false;
}
// save file to database
// save file to resources/projects/app_id directory
return true;
}
deleteAppConfig(app_id: string): bool
This function deletes an app config from the database and returns true if successful.
async deleteAppConfig(app_id: string): Promise<boolean> {
// delete app config from database
return true;
}
Implementation
The implementation of the UnityApp controller involves creating a new typescript file in the database
directory and exporting the functions described above. The file should also import the necessary dependencies such as the database connection and the UnityAppConfig
type.
// database/unityapp.controller.ts
import { db } from '../db';
import { UnityAppConfig } from './unityapp.config';
export async function getUnityAppConfig(app_id: string): Promise<UnityAppConfig> {
// implementation
}
export async function newAppConfig(name: string): Promise<UnityAppConfig> {
// implementation
}
export async function addFileTo(app_id: string, filepath: string): Promise<boolean> {
// implementation
}
export async function deleteAppConfig(app_id: string): Promise<boolean> {
// implementation
}
Conclusion
Frequently Asked Questions
In this article, we will answer some frequently asked questions about the Database UnityApp Controller.
Q: What is the purpose of the UnityApp controller?
A: The UnityApp controller is responsible for managing Unity app configurations, adding files to Unity projects, and deleting app configurations.
Q: What is the structure of the database table for Unity app configurations?
A: The database table for Unity app configurations is defined in the migration_1.sql
file and has the following structure:
CREATE TABLE unity_app_config (
id INT PRIMARY KEY,
name VARCHAR(255) NOT NULL,
user_type VARCHAR(255) NOT NULL,
app_id VARCHAR(255) NOT NULL,
config JSON NOT NULL
);
Q: What is the UnityAppConfig
type?
A: The UnityAppConfig
type is a typescript type that matches the structure of the database table. It has the following properties:
type UnityAppConfig = {
id: number;
name: string;
userType: 'INSTRUCTOR' | 'STUDENT';
appId: string;
config: any;
};
Q: How do I create a new Unity app config?
A: You can create a new Unity app config using the newAppConfig
function. The name
parameter must be unique per user.
async newAppConfig(name: string): Promise<UnityAppConfig> {
const existingConfig = await this.getUnityAppConfig(name);
if (existingConfig) {
throw new Error(`App config with name ${name} already exists`);
}
const newConfig: UnityAppConfig = {
id: 0,
name,
userType: 'INSTRUCTOR', // default user type
appId: '',
config: {},
};
// save new config to database
return newConfig;
}
Q: How do I add a new file to a Unity project?
A: You can add a new file to a Unity project using the addFileTo
function. The file is also stored in the resources/projects/app_id
directory.
async addFileTo(app_id: string, filepath: string): Promise<boolean> {
// check if file already exists
const existingFile = await this.getFile(app_id, filepath);
if (existingFile) {
return false;
}
// save file to database
// save file to resources/projects/app_id directory
return true;
}
Q: How do I delete an app config from the database?
A: You can delete an app config from the database using the deleteAppConfig
function.
async deleteAppConfig(app_id: string): Promise<boolean> {
// delete app config from database
return true;
}
Q: What are the benefits of using the UnityApp controller?
A: The UnityApp controller provides several benefits, including:
- Simplified management of Unity app configurations
- Easy addition of files to Unity projects
- Efficient deletion of app configurations
Q: How do implement the UnityApp controller?
A: To implement the UnityApp controller, you need to create a new typescript file in the database
directory and export the functions described above. The file should also import the necessary dependencies such as the database connection and the UnityAppConfig
type.
// database/unityapp.controller.ts
import { db } from '../db';
import { UnityAppConfig } from './unityapp.config';
export async function getUnityAppConfig(app_id: string): Promise<UnityAppConfig> {
// implementation
}
export async function newAppConfig(name: string): Promise<UnityAppConfig> {
// implementation
}
export async function addFileTo(app_id: string, filepath: string): Promise<boolean> {
// implementation
}
export async function deleteAppConfig(app_id: string): Promise<boolean> {
// implementation
}
Conclusion
In this article, we have answered some frequently asked questions about the Database UnityApp Controller. We have discussed the purpose of the UnityApp controller, the structure of the database table, and the benefits of using the controller. We have also provided examples of how to implement the controller and its functions.