-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Is possible to select the data from database and export that data using loopback 4? #4624
Comments
Hi @AnanthGopal, could you clarify on "export data"? LB4's flexibility allows querying using a repository function (such as |
Do i select data from the database and need to export those data into CSV, For Ex: I create a csv file based on selected data into a particular file path. i need to download that csv file from UI |
@AnanthGopal Not sure if I understand your question correctly. But one of the basic usages of LB4 is that you can access the db if you set up the datasource properly. Take MySQL as an example, if you already have your tables defined, LB4 allows you to discover schemas from DBs (ref: https://loopback.io/doc/en/lb3/Discovering-models-from-relational-databases.html). And this can also be done by the CLI LoopBack has different connectors for databases. These connectors implement the data exchange logic to communicate with backend systems such as relational or NoSQL databases. You can check the database connectors page. |
The below URL has the download option loopback "https://stackoverflow.com/questions/29372470/downloading-file-from-strongloop-loopback/31389086#31389086", I need this same option in loopback 4 |
@AnanthGopal This can be accomplished via the controller: import {Filter, repository} from '@loopback/repository';
import {
post,
param,
get,
getFilterSchemaFor,
Response, // Import REST HTTP Response object
RestBindings,
} from '@loopback/rest';
import {File} from '../models';
import {FileRepository} from '../repositories';
import {inject} from '@loopback/core';
export class FileController {
constructor(
@repository(FileRepository)
public fileRepository: FileRepository,
// Inject REST HTTP Response object into controller
@inject(RestBindings.Http.RESPONSE)
public response: Response,
) {}
@get('/files/download', {
responses: {
'200': {
description: 'File instance file download',
content: {
// See: https://swagger.io/docs/specification/describing-responses/
'text/csv': {
schema: {
type: 'string',
format: 'binary',
},
},
},
},
},
})
async downloadById(
@param.query.object('filter', getFilterSchemaFor(File))
filter?: Filter<File>,
): Promise<string> {
this.response;
const files = await this.fileRepository.find(filter);
let csv = 'Id,Name';
files.map(x => {
csv += `\n${x.Id},${x.Value}`;
});
// Manipulate response header
this.response.set(
'Content-Disposition',
'attachment; filename="export.csv',
);
return csv;
}
} ^ The above code assumes a
|
Related: #2230 |
Hi @AnanthGopal, has the above solution helped resolve the issue? |
Thank you that code is working as expected. Below code used to download static files from the server.
|
i need to select and export data by using loopback 4. If it is possible, can you provide the sample link?
The text was updated successfully, but these errors were encountered: