mirror of
https://github.com/HeyPuter/puter.git
synced 2026-08-23 14:36:44 +00:00
dev(data-access): add owner object to app select
This commit is contained in:
@@ -3,6 +3,8 @@ import { DB_READ } from '../../services/database/consts.js';
|
||||
import { Context } from '../../util/context.js';
|
||||
import AppRepository from './AppRepository.js';
|
||||
import { as_bool } from './lib/coercion.js';
|
||||
import { user_to_client } from './lib/filter.js';
|
||||
import { extract_from_prefix } from './lib/sqlutil.js';
|
||||
|
||||
/**
|
||||
* AppService contains an instance using the repository pattern
|
||||
@@ -43,7 +45,13 @@ export default class AppService extends BaseService {
|
||||
|
||||
const userCanEditOnly = Array.prototype.includes.call(predicate, 'user-can-edit');
|
||||
|
||||
const stmt = `SELECT * FROM apps ${userCanEditOnly ? 'WHERE owner_user_id=?' : ''} LIMIT 5000`;
|
||||
const stmt = 'SELECT *, ' +
|
||||
'owner_user.username AS owner_user_username, ' +
|
||||
'owner_user.uuid AS owner_user_uuid ' +
|
||||
'FROM apps ' +
|
||||
'LEFT JOIN user owner_user ON owner_user_id = owner_user.id ' +
|
||||
`${userCanEditOnly ? 'WHERE owner_user_id=?' : ''} ` +
|
||||
'LIMIT 5000';
|
||||
const values = userCanEditOnly ? [Context.get('user').id] : [];
|
||||
const rows = await db.read(stmt, values);
|
||||
|
||||
@@ -75,6 +83,11 @@ export default class AppService extends BaseService {
|
||||
// app.filetype_associations = row.filetype_associations;
|
||||
// app.owner = row.owner;
|
||||
|
||||
{
|
||||
const owner_user = extract_from_prefix(row, 'owner_user_');
|
||||
app.owner_user = user_to_client(owner_user);
|
||||
}
|
||||
|
||||
// REFINED BY OTHER DATA
|
||||
// app.icon;
|
||||
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
// These utility functions describe how to produce an object safe
|
||||
// for transfer that came from a "raw" object.
|
||||
|
||||
export const user_to_client = raw_user => {
|
||||
return {
|
||||
username: raw_user.username,
|
||||
// This `uuid` is not an internal-only ID.
|
||||
uuid: raw_user.uuid,
|
||||
};
|
||||
};
|
||||
@@ -0,0 +1,21 @@
|
||||
/**
|
||||
* When columns are selected from a joined table and prefixed:
|
||||
*
|
||||
* SELECT joined_table.* AS joined_table_
|
||||
*
|
||||
* This function is able to extract the object from the result:
|
||||
*
|
||||
* extract_from_prefix(row, 'joined_table_') // columns of joined_table
|
||||
*
|
||||
* @param {*} row
|
||||
* @param {*} prefix
|
||||
*/
|
||||
export const extract_from_prefix = (row, prefix) => {
|
||||
const result = {};
|
||||
for ( const [key, value] of Object.entries(row) ) {
|
||||
if ( key.startsWith(prefix) ) {
|
||||
result[key.replace(prefix, '')] = value;
|
||||
}
|
||||
}
|
||||
return result;
|
||||
};
|
||||
Reference in New Issue
Block a user