Welcome back to the September edition of the “Community Q&A Monthly Digest”, in which we are curating some of the Q&A that we think it might be helpful to you. Thank you for posting your questions and helping your fellow LoopBack users.

The LoopBack Slack community is a platform where LoopBack users are helping each other out. If you haven’t joined already, sign up today!

Let’s take a look at some of the questions and answers from the community.

Question: Is it possible to use auto-generate timestamp property in a model?

Answer: To set the property to the current datetime upon Model Create, you can:

@property({
type: 'date',
defaultFn: 'now',
})
timestamp?: string;

Question: Does LoopBack has any built-in cache? Or should we implement that to make response even faster?

Answer: We currently don’t but there are some example implementations for your reference:


Question: I want to perform schema migration but the order of tables is important beacause there are some relation and foreign key between them. How can I set the order of tables to be migrated?

Answer: Within migrate.ts, app.migrateSchema accepts a model array. So it can be updated as such:

await app.migrateSchema(Object.assign(<SchemaMigrationOptions>{
models: [/* Add model names here */]
}, existingSchema));

Like with any auto-migration, please do take a backup of the database before running the migration.


Question: I wanted to implement an API like the file-transfer example on the docs but with more endpoints with different storage directories. How is that possible?

Answer: You can have different endpoints backed by different methods using post /<url> decoration as you see at https://github.com/strongloop/loopback-next/blob/master/examples/file-transfer/src/controllers/file-upload.controller.ts#L28. If you want to calculate the file path per request, you can instantiate a new upload service instance instead of using the injected one.


Questions: I work with LoopBack in a k8 cluster, when i try to implement JWT authentication, all users get the same token, and the data in that token is not equal to that user. Is there any way to fix it besides saving the token in a database?

Answer: Typically JWT tokens are generated using a combination of a secret and some sort of UUID of the user. When they successfully authenticate, a token is generated and returned. When you need to verify the token, you decode it using the secret, giving you the UUID of the user. This means that you don’t actually have to save a token at all. Here’s how we are generating tokens for our users.

const userInfoForToken = {
id: userProfile.id,
name: userProfile.name,
roles: userProfile.roles,
};
// Generate a JSON Web Token
let token: string;
try {
token = await signAsync(userInfoForToken, this.jwtSecret, {
expiresIn: Number(this.jwtExpiresIn),
});
} catch (error) {
throw new HttpErrors.Unauthorized(`Error encoding token : ${error}`);
}

In this context, signAsync is a promisify’d version of jsonwebtoken.sign().


Question: I want to use the LoopBack app cli-command programmatically. Is this possible?

Answer: You can try to require('@loopback/cli/lib/cli'). See https://github.com/strongloop/loopback-next/blob/master/packages/cli/lib/cli.js. cli.js has logic to create yeoman env and register generators.


Enriching LoopBack and its Community - You are Invited!

As mentioned in our recent blog post, your contribution is important to make LoopBack a sustainable open source project.

Here is what you can do:

Let’s make LoopBack a better framework together!