Updated Writing a database interface (markdown)

master
X 6 months ago
parent 443f08aed0
commit 52fef591db

@ -42,12 +42,12 @@ export class YAMLDatabase implements Database {
return Promise.resolve();
}
public get(table: DatabaseTable, key: string): Promise<DatabaseValue | undefined> {
return Promise.resolve(undefined);
public get(table: DatabaseTable, key: string): Promise<DatabaseValue> {
throw new Error("Key does not exist");
}
public getAll(table: DatabaseTable): Promise<{ [index: string]: DatabaseValue; }> {
return Promise.resolve({});
public getAll(table: DatabaseTable): Promise<DatabaseValue[]> {
return Promise.resolve([]);
}
}
```
@ -205,21 +205,21 @@ exists, it should do nothing and reject.
#### get()
```typescript
function get(table: 'assfiles', key: string): Promise<AssFile | undefined>
function get(table: 'assusers', key: string): Promise<AssUser | undefined>
function get(table: 'asstokens', key: string): Promise<UploadToken | undefined>
function get(table: DatabaseTable, key: string): Promise<DatabaseValue | undefined>
function get(table: 'assfiles', key: string): Promise<AssFile>
function get(table: 'assusers', key: string): Promise<AssUser
function get(table: 'asstokens', key: string): Promise<UploadToken>
function get(table: DatabaseTable, key: string): Promise<DatabaseValue>
```
`get` gets a value from the database. It should resolve to undefined if the key does not exist and should resolve to the value if it does. If something goes wrong it should reject.
`get` gets a value from the database. It should throw an error if the key does not exist and should resolve to the value if it does. If something goes wrong it should reject.
#### getAll()
```typescript
function getAll(table: 'assfiles', key: string): Promise<{ [index: string]: AssFile }>
function getAll(table: 'assusers', key: string): Promise<{ [index: string]: AssUser }>
function getAll(table: 'asstokens', key: string): Promise<{ [index: string]: UploadToken }>
function getAll(table: DatabaseTable, key: string): Promise<{ [index: string]: DatabaseValue }>
function getAll(table: 'assfiles', key: string): Promise<AssFile[]>
function getAll(table: 'assusers', key: string): Promise<AssUser[]>
function getAll(table: 'asstokens', key: string): Promise<UploadToken[]>
function getAll(table: DatabaseTable, key: string): Promise<DatabaseValue[]>
```
`getAll` should resolve to an object containing a map of keys to values. It should reject if something goes wrong.
`getAll` should resolve an array of values. It should reject if something goes wrong.
### Database configuration
When a database is selected, `DBManager` calls `db.open()` to connect
@ -326,6 +326,9 @@ return new Promise(async (resolve, reject) => {
// Load the database
let db = this._readDB();
// Check for the key
if (db[DB_TABLES[table]][key] == undefined) throw new Error("Key does not exist");
// Get the thing
resolve(db[DB_TABLES[table]][key]);
} catch (err) {
@ -349,7 +352,7 @@ return new Promise(async (resolve, reject) => {
let db = this._readDB();
// Get the thing
resolve(db[DB_TABLES[table]]);
resolve(Object.values(db[DB_TABLES[table]]));
} catch (err) {
log.error('Failed to get data');
reject(err);

Loading…
Cancel
Save