Im trying to hide api key in my angular project environment using #angular-builders/custom-webpack which will fetch value from system environment variable through process.env during build .
But after sucessful build , i'm still able to see the actual value in main.js file.
//custom.webapack.config
const webpack = require('webpack');
module.exports = {
plugins: [
new webpack.DefinePlugin({
"nvar": {
ENVIRONMENT: JSON.stringify(process.env.ENVIRONMENT),
SomeAPIKey: JSON.stringify(process.env.DDSK),
SomeOtherAPIKey: JSON.stringify(process.env.SomeOtherAPIKey)
}
})
]
};
//typings.d.ts
declare var nvar: Env;
interface Env {
ENVIRONMENT: string;
SomeAPIKey: string;
SomeOtherAPIKey: string;
}
environment.ts
export const environment = {
production: true,
apiURL: 'some api url',
appUrl:''some url,
googleClientId:'client key',
__dds:nvar.SomeAPIKey
}
//angular.json
"builder":"#angular-builders/custom-webpack:browser",
"options": {
"customWebpackConfig": {
"path":"custom-webpack.config.js"
},
Output
Related
The TS compiler complains about the following code snippet:
import('HelloWorldApp/HelloWorldButton')
.then(HelloWorldButtonModule => {
const HelloWorldButton = HelloWorldButtonModule.default;
const helloWorldButton = new HelloWorldButton();
helloWorldButton.render();
});
this: Cannot find module 'HelloWorldApp/HelloWorldButton' or its corresponding type declarations.
Tried adding declare module 'HelloWorldApp/HelloWorldButton' to a declaration.d.ts file specified in tsconfig.json but to no avail.
How do I fix it?
//types.d.ts inside src folder
declare module 'host/NewComponent'
module type declaration problem solved by types.d.ts
Please take look properly on config file for host project .
I was facing same issues then I used -
optimization:{
splitChunks: false,
}
you will find optimization on vue.config.js file of host project
take a look into the config files -
//*vue.config.js* for host project
const { defineConfig } = require('#vue/cli-service')
const ModuleFederationPlugin =
require("webpack").container.ModuleFederationPlugin;
module.exports = defineConfig({
publicPath:"http://localhost:8000/",
configureWebpack: {
plugins: [
new ModuleFederationPlugin({
name: "host",
filename: "remoteEntry.js",
exposes: {
"./NewComponent": "./src/components/NewComponent.vue",
},
}),
],
optimization:{
splitChunks: false,
}
},
devServer: {
port: 8000,
},
});
Though version doesn't matter here but I want to inform you that I'm using vue 3 .
//vue.config.js for consumer project
const { defineConfig } = require('#vue/cli-service')
const ModuleFederationPlugin =
require("webpack").container.ModuleFederationPlugin;
module.exports = defineConfig({
publicPath:"http://localhost:8001/",
configureWebpack: {
plugins: [
new ModuleFederationPlugin({
name: "consumer",
remotes: {
host: "host#http://localhost:8000/remoteEntry.js",
},
}),
],
},
devServer: {
port: 8001,
},
});
And Finally I used the component from host in consumer project.
//HelloWorld.vue component in consumer project
<template>
<div>
<otherComp></otherComp>
</div>
</template>
<script lang="ts">
import { defineAsyncComponent, defineComponent } from 'vue';
const otherComp = defineAsyncComponent(() => import("host/NewComponent"));
export default defineComponent({
name: 'HelloWorld',
components:{
otherComp,
},
props: {
msg: String,
}
});
</script>
****Everything is works fine for me .Hoping it will also solve your and others problem to build micro front end . Happy Coding ****
I used to have:
import { getManager } from "typeorm";
return getManager()
.count(entity, {
...where,
})
.then((count) => count < 1);
to use the current connection in a validation decorator and access to database.
But now with the version 0.3.0 of typeorm, getManager() is deprecated and I get the following error:
`ConnectionNotFoundError: Connection default was not found`
How should I use the new DataSource API to get the current connection using Nest.js in external scripts like validation decorators?
I have dealt with the following error with "typeorm": "0.3.6", before, so hopes that my experience will be relevant for you.
Instead of using getManager I am using entityManager but result is the same.
Here is the example for your case:
test.module.ts
#Module({
imports: [
TypeOrmModule.forRoot(postgresConfig), // <- connection configs
TypeOrmModule.forFeature([
TestEntity,
]),
],
controllers: [],
providers: [TestService],
})
export class TestModule {}
test.service.ts
#Injectable()
export class TestService implements OnApplicationBootstrap {
constructor(
#InjectEntityManager()
private readonly entityManager: EntityManager,
)
async onApplicationBootstrap(): Promise<void> {
const count = await this.entityManager.getRepository(TestEntity).count('query');
console.log(count);
}
}
You should also have created a typeorm entities for that.
If we are talking about migrations you could use an ormconfig.json, but also there was a problem with using typeorm combined with test frameworks like Jest and if you case is relevant with it, just mention it, and I'll find an example in on of my projects.
It should be something relevant with creating manual connections without decorator like:
export const getDatabaseConnectionOptions = (config): PostgresConnectionOptions => {
return {
type: 'postgres',
logging: config.db.logging,
connectTimeoutMS: config.db.master.connectionTimeoutMillis,
uuidExtension: 'uuid-ossp',
extra: {
idleTimeoutMillis: config.db.master.idleTimeoutMillis,
max: config.db.master.max,
keepalives_idle: config.db.master.keepalives_idle,
},
entities: process.env.TS_NODE
? [path.join(process.cwd(), 'src/**/*.entity.ts')]
: [path.join(process.cwd(), 'dist/**/*.entity.js')],
migrations: process.env.TS_NODE ? ['src/typeorm_migrations/*.ts'] : ['dist/typeorm_migrations/*.js'],
migrationsTableName: 'typeorm_migrations',
namingStrategy: new DatabaseNamingStrategy(),
replication: {
master: {
host: config.db.master.host,
port: config.db.master.port,
username: config.db.master.user,
password: config.db.master.password,
database: config.db.master.database,
},
slaves: [
{
host: config.db.slave.host,
port: config.db.slave.port,
username: config.db.slave.user,
password: config.db.slave.password,
database: config.db.slave.database,
},
],
},
cli: {
migrationsDir: 'src/typeorm_migrations',
},
};
};
export const createDatabaseConnection = async (config): Promise<Connection> => {
// Setup TypeDI
useContainer(Container);
// Setup typeorm-transactional-cls-hooked
initializeTransactionalContext();
patchTypeORMRepositoryWithBaseRepository();
return await createConnection(config);
};
const connection = await createDatabaseConnection(getDatabaseConnectionOptions(config));
const { app } = createAppApi();
and then access to managers or entities via connection props.
I got this test from another site. They are injecting a mock route. I think I need to mock router itself or pass a real one into the test so the page can run. There is a way to do this in vue 2, but I haven't found an example for vue 3.
import { mount } from "#vue/test-utils";
import Nav from "./Nav.vue";
test("it displays a menu item", () => {
const mockRoute = {
params: {
id: 1,
},
};
const mockRouter = {
push: jest.fn(),
};
const wrapper = mount(Nav, {
props: {
isAuthenticated: true,
},
global: {
mocks: {
$route: mockRoute,
$router: mockRouter,
},
},
});
expect(wrapper.find("#navLabel_0").text()).toEqual("Appointments");
});
The component I'm testing has tags.
The test fails with:
Failed to resolve component: router-link
You have to pass the router-link as a stub: stubs: ['router-link'] when you mount the component:
const wrapper = mount(Nav, {
props: {
isAuthenticated: true,
},
global: {
mocks: {
$route: mockRoute,
$router: mockRouter,
},
},
stubs: ['router-link'] });
I am working on my first TypeScript project, which is based off of this template:
https://github.com/ljlm0402/typescript-express-starter
I am working on adding migrations to our app, up to this point we have been relying on the automigration functionality and dropping / recreating DB/Tables as entities are updated. We would like to add a "default user" to our system via the migrations... but because of the way the app is running the up/down functions in the migration for some reason CAN create/drop tables but CAN NOT access any of these tables to insert data into them (??!!).
I am worried it may be a .env / ormconfig.js issue, and also the layer of how TS is compiled to JS makes me worry that for some reason my app is not reading the right config file.
How to reproduce this:
ts-node ./node_modules/typeorm/cli.js --config ./ormconfig.js "migration:run"
ormconfig.js:
const env = process.env.NODE_ENV || 'development';
module.exports = {
type: 'postgres',
host: process.env.POSTGRESQL_HOST,
port: process.env.POSTGRESQL_PORT,
username: process.env.POSTGRESQL_USERNAME,
password: process.env.POSTGRESQL_PASSWORD,
database: process.env.POSTGRESQL_DATABASE,
synchronize: true,
logging: false,
entities: [env === 'production' ? 'build/entity/*{.ts,.js}' : 'src/entity/*{.ts,.js}'],
migrations: [env === 'production' ? 'build/migration/*{.ts,.js}' : 'src/migration/*{.ts,.js}'],
subscribers: [env === 'production' ? 'build/subscriber/*{.ts,.js}' : 'src/subscriber/*{.ts,.js}'],
cli: {
entitiesDir: 'src/entity',
migrationsDir: 'src/migration',
subscribersDir: 'src/subscriber',
},
};
Migration (trimmed for brevity)
import { MigrationInterface, QueryRunner, TableColumn } from 'typeorm';
import userService from '../services/user.service';
export class SeedDefaultUser1610084649009 implements MigrationInterface {
public userService = new userService();
public async up(queryRunner: QueryRunner): Promise<void> {
const dbs = await queryRunner.getDatabases(); // returns empty array []
const schemas = await queryRunner.getSchemas(); // returns empty array []
console.log(dbs, schemas);
await queryRunner.createTable(
new Table({
name: 'user_entity',
columns: [
{
name: 'id',
type: 'int',
isPrimary: true,
},
{
name: 'email',
type: 'varchar',
},
],
}),
true,
); // Despite no db connections, this works! tables exist!
const user: User = await this.userService.create(
{
email: 'admin#email.com',
},
); // This fails with:
//QueryFailedError: relation "user_entity" does not exist
}
public async down(queryRunner: QueryRunner): Promise<void> {}
}
To insert fake data using TypeOrm, you need to create seeds instead of migrations.
Migrations are used to create tables, seeds are used to fill them with data.
You can follow this medium article to create a seed, this npm package or faker.js
[Contextualizing]
I have a simple NodeJS project using Mongodb, Mongoose (with Typegoose) and Nodemon.
In my package.json I have commands for developer and production environments.
...
"start-dev": "nodemon src/index.ts",
"start-prd": "node dist/index",
...
For the start-prd command, I am using webpack to generate the binaries.
This is my webpack.config.
const webpack = require("webpack");
const path = require("path");
const nodeExternals = require("webpack-node-externals");
module.exports = {
mode: "dsv",
entry: ["webpack/hot/poll?100", "./src/index.ts"],
watch: true,
target: "node",
externals: [
nodeExternals({
allowlist: ["webpack/hot/poll?100"]
})
],
module: {
rules: [
{
test: /.tsx?$/,
use: "ts-loader",
exclude: /node_modules/
}
]
},
resolve: {
extensions: [".tsx", ".ts", ".js"]
},
plugins: [new webpack.HotModuleReplacementPlugin()],
output: {
path: path.join(__dirname, "dist"),
filename: "index.js"
}
};
I have a Delivery model class mapped with Typegoose ...
import { prop } from '#typegoose/typegoose';
import { Typegoose } from 'typegoose';
export class Delivery extends Typegoose {
#prop()
name: string;
#prop()
description?: string;
#prop()
services: string[];
#prop()
rating?: number;
};
... and I define the repository like this.
import mongoose from 'mongoose';
import { Delivery } from '../entities/delivery';
export class DeliveryRepository {
async list(): Promise<Delivery[]> {
await mongoose.connect('mongodb://usr:passw#localhost:21017/my_db',
{
auth: {
user:"usr",
password:"passw"
},
useNewUrlParser: true,
useUnifiedTopology: true
});
const Model = new Delivery().getModelForClass(Delivery, {
existingMongoose: mongoose,
schemaOptions: {collection: 'deliveries'}
});
return Model.find({});
}
}
Calling the API with the get method.
...
app.route('/api/deliveries').get((req, res) => {
var repo = new DeliveryRepository();
repo.list().then((result) => {
res.status(200).send(result);
}).catch(r => console.log(r));
})
...
[Scenario]
If run prd script.
1. npm run webpack
2. npm run start-prd
It works perfectly! I call the api get method and list my objects.
When run dev script.
1. npm run start-dev
Nodemon starts normally, but when calling the api get method error occurs ...
TypeError: Cannot read property '_id' of undefined
at Schema.add (D:\pessoal\workspaces\omeurestaurante-api\node_modules\mongoose\lib\schema.js:462:11)
at _buildSchema (D:\pessoal\workspaces\omeurestaurante-api\node_modules\typegoose\src\typegoose.ts:134:9)
at Delivery.buildSchema (D:\pessoal\workspaces\omeurestaurante-api\node_modules\typegoose\src\typegoose.ts:109:13)
at Delivery.setModelForClass (D:\pessoal\workspaces\omeurestaurante-api\node_modules\typegoose\src\typegoose.ts:78:22)
at Delivery.getModelForClass (D:\pessoal\workspaces\omeurestaurante-api\node_modules\typegoose\src\typegoose.ts:52:12)
at SessionDb.getModel (D:\pessoal\workspaces\omeurestaurante-api\src\db\session-db.ts:29:24)
at DeliveryRepository.<anonymous> (D:\pessoal\workspaces\omeurestaurante-api\src\repositories\delivery.repository.ts:16:30)
at Generator.next (<anonymous>)
at D:\pessoal\workspaces\omeurestaurante-api\src\repositories\delivery.repository.js:8:71
at new Promise (<anonymous>)
at __awaiter (D:\pessoal\workspaces\omeurestaurante-api\src\repositories\delivery.repository.js:4:12)
at DeliveryRepository.list (D:\pessoal\workspaces\omeurestaurante-api\src\repositories\delivery.repository.js:22:16)
at app.route.get (D:\pessoal\workspaces\omeurestaurante-api\src\index.ts:40:8)
at Layer.handle [as handle_request] (D:\pessoal\workspaces\omeurestaurante-api\node_modules\express\lib\router\layer.js:95:5)
at next (D:\pessoal\workspaces\omeurestaurante-api\node_modules\express\lib\router\route.js:137:13)
at Route.dispatch (D:\pessoal\workspaces\omeurestaurante-api\node_modules\express\lib\router\route.js:112:3)