Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@ const DEFAULT_DB_IDLE_TIMEOUT_MILLIS = 60000;
const DEFAULT_DB_CONNECTION_TIMEOUT_MILLIS = 2000;

const config = {
<% if (project.serviceDependency && project.baseServiceStoreName ) { -%>
name: <%= project.baseServiceStoreName %>,
<% }else{ -%>
name: '<%= project.datasourceName %>',
<% } -%>
connector: '<%= project.datasourceConnectorName %>',
host: process.env.DB_HOST,
port: process.env.DB_PORT,
Expand Down Expand Up @@ -45,7 +49,12 @@ juggler.DataSource
static readonly defaultConfig = config;

constructor(

<% if (project.serviceDependency && project.baseServiceStoreName ) { -%>
@inject(`datasources.config.${<%= project.baseServiceStoreName %>}`, {optional: true})
<% }else{ -%>
@inject('datasources.config.<%= project.datasourceName %>', {optional: true})
<% } -%>
dsConfig: object = config,
) {
if (!!+(process.env.ENABLE_DB_CONNECTION_POOLING ?? 0)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,13 @@ import {AuthCacheSourceName} from '@sourceloop/core';
<% } -%>

const config = {
<% if (project.serviceDependency && project.baseServiceCacheName ) { -%>
name: <%= project.baseServiceCacheName %>,
<% } else if (project.facade) { -%>
name: AuthCacheSourceName,
<% }else{ -%>
name: process.env.REDIS_NAME,
<% } -%>
connector: 'kv-redis',
host: process.env.REDIS_HOST,
port: process.env.REDIS_PORT,
Expand Down Expand Up @@ -49,11 +55,19 @@ export class RedisDataSource
static dataSourceName = <%= project.baseServiceCacheName %>;
<% } else if (project.facade) { -%>
static dataSourceName = AuthCacheSourceName;
<% } -%>
<% }else{ -%>
static dataSourceName = process.env.REDIS_NAME;
<% } -%>
static readonly defaultConfig = config;

constructor(
<% if (project.serviceDependency && project.baseServiceCacheName ) { -%>
@inject(`datasources.config.${<%= project.baseServiceCacheName %>}`, {optional: true})
<% } else if (project.facade) { -%>
@inject(`datasources.config.${AuthCacheSourceName}`, {optional: true})
<% }else{ -%>
@inject(`datasources.config.${process.env.REDIS_NAME}`, {optional: true})
<% } -%>
dsConfig: AnyObject = config,
) {
if (
Expand Down
7 changes: 3 additions & 4 deletions packages/cli/src/generators/microservice/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,10 +203,9 @@ export default class MicroserviceGenerator extends AppGenerator<MicroserviceOpti
const redisDsPresent = baseServiceDSList.filter(
ds => ds.type === 'cache',
);

this.projectInfo.baseServiceCacheName = redisDsPresent.length
? 'redis'
: undefined;
this.projectInfo.baseServiceCacheName =
redisDsPresent[0]?.name ||
(redisDsPresent.length ? 'redis' : undefined);
}
this.destinationRoot(join(type, this.options.name ?? DEFAULT_NAME));
this.projectInfo.dependencies = appendDependencies(
Expand Down
12 changes: 8 additions & 4 deletions packages/cli/src/generators/microservice/templates/.nycrc
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
{
"extends": "@istanbuljs/nyc-config-typescript",
"all": true,
"reporter": ["html", "text-summary"]
}
"include": ["dist"],
"exclude": ["dist/__tests__/", "dist/index.js"],
"extension": [".js", ".ts"],
"reporter": ["text", "html", "json"],
"exclude-after-remap": false,
"check-coverage": true,
"lines": 80
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,34 @@ import {
<% if (project.facade) { -%>
import {RateLimitSecurityBindings} from 'loopback4-ratelimiter';
<% } -%>
<%
const importMap = {};

if (project.baseServiceDSList) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is this logic in template? this should be in the generator

for (const ds of project.baseServiceDSList) {
if (ds.name && ds.fileName) {
importMap[ds.fileName] = importMap[ds.fileName] || new Set();
importMap[ds.fileName].add(ds.name);
}
}
}

if (project.serviceDependency && project.baseServiceCacheName) {
const redis = project.baseServiceCacheName;
importMap[redis] = importMap[redis] || new Set();
importMap[redis].add(project.baseServiceCacheName);
}

for (const [fileName, namesSet] of Object.entries(importMap)) {
const names = Array.from(namesSet);
-%>
import {<%= names.join(', ') %>} from '@sourceloop/<%= project.serviceDependency %>';
<% } %>
<% if (project.facade) { -%>
import {AuthCacheSourceName} from '@sourceloop/core';
<% } -%>



export async function setupApplication(): Promise<AppWithClient> {
const restConfig = givenHttpServerConfig({
Expand All @@ -23,21 +51,41 @@ export async function setupApplication(): Promise<AppWithClient> {
});

<% if(project.baseServiceDSList) { -%>
<% for(let i=0; i< project.baseServiceDSList.length; i++) {-%>
app.bind('datasources.config.db').to({
name: 'db',
connector: 'memory',
});
<% for(let i=0; i< project.baseServiceDSList.length; i++) {
const ds = project.baseServiceDSList[i]; -%>
<% if (ds.name) { -%>
app.bind(`datasources.config.${<%= ds.name %>}`).to({
name: '<%= ds.name %>',
connector: 'memory',
});
<% }else{ -%>
app.bind('datasources.config.db').to({
name: 'db',
connector: 'memory',
});
<% } -%>
<% } -%>
<% } -%>




<% if (project.facade || project.baseServiceCacheName) { -%>
<% if (project.serviceDependency && project.baseServiceCacheName ) { -%>
app.bind(`datasources.config.${<%= project.baseServiceCacheName %>}`).to({
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but we are binding redis anyways on line 84? do we still need it?

name: <%= project.baseServiceCacheName %>,
connector: 'kv-memory',
});
<% } else if (project.facade) { -%>
app.bind(`datasources.config.${AuthCacheSourceName}`).to({
name: AuthCacheSourceName,
connector: 'kv-memory',
});
<% }else{ -%>
app.bind(`datasources.config.${process.env.REDIS_NAME}`).to({
name: process.env.REDIS_NAME,
connector: 'kv-memory',
});
<% } -%>
<% } -%>

<% if (project.facade) { -%>
app.bind(RateLimitSecurityBindings.RATELIMIT_SECURITY_ACTION).to(async () => {
Expand Down
Loading