Creating Nest.js dependency tree for standalone executable script (without hosting server)

Can a service be run in an executable file? I want to streamline the process of "bootstrapping" a module and calling a service method directly

const userService = new UserService()
userService.find(1).then(console.log)

However, all dependencies would need to be connected, and the database initialized.

The usage of typedi enables you to utilize getContainer which automatically manages these tasks for you.

Answer №1

import { NestFactory } from '@nestjs/core'
import { AppModule } from 'app/AppModule'
import { ContactCore } from 'domain/contact/ContactCore'
import { ContactModule } from 'domain/contact/ContactModule'

(async () => {
  const app = await NestFactory.create(AppModule, {
    cors: {
      origin: ['http://localhost:3000', 'http://localhost:6006'],
      credentials: true
    }
  })
  const contactCore = app.select(ContactModule).get(ContactCore)

  const result = await contactCore.read({ uuid: "550718e8-4137-46c0-a90c-2abc6c4d96e2" })

  console.log(result)

})()

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

RxJs: The art of updating a BehaviorSubject by concatenating the latest value with a new one

Within my Angular application, I am currently implementing a state management system using BehaviourSubject This is what I have in my store file: myStoreData = new BehaviourSubject([]) In my actions file, I have the following: export const SAVE_DATA_IN_C ...

Error: Angular does not recognize session storage reference

While my project is up and running, I have encountered an error in the terminal. let obj = { doc_id: sessionStorage.getItem('doc_id'), batch_no: sessionStorage.getItem('batch_no') } I attempted to make adjustments by a ...

Guide for configuring a server to exclusively render Vue components

I have been utilizing the vue-no-ssr library (available at https://github.com/egoist/vue-no-ssr), but it only renders components on the client side. I require my component to render on the server side. Is there a method to create a vue component that exclu ...

Can you explain the significance of using an exclamation mark after defining a variable in TypeScript?

As I delve into TypeScript in an effort to enhance my programming skills, I have encountered the use of exclamation marks when defining variables. An example of this can be seen in the following code snippet: protected _db!: CheckpointDB ...

Error message is not shown by React Material UI OutlinedInput

Using React and material UI to show an outlined input. I can successfully display an error by setting the error prop to true, but I encountered a problem when trying to include a message using the helperText prop: <OutlinedInput margin="dense&quo ...

Modeling with Nested Objects in TypeScript and Angular

ERROR: Unable to access 'applicants' property of undefined object When my component initializes, I am attempting to execute the following code: application: application; ngOnInit(): void { this.application.applicants[0].type = "1"; <-- T ...

Angular Material Password Confirmation

Currently, I am working on implementing user authentication using Angular Material. Specifically, I am focusing on displaying the appropriate error message when the confirmed password does not match the initially entered password. Below is the snippet of ...

Transforming a Typescript class instance into a JavaScript object

Here is the code snippet I am working with: class A{ test: string constructor(test: string){ this.test = test } } const a = new A("hi") console.log(a) This is what the output looks like: A { test: 'hi' } However, when at ...

The Azure DevOps build task halts execution of an SQL script after a series of sp_rename calls

I am currently working on creating an Azure Devops build task that will run a series of SQL scripts against a SQL Server 2017 database. I have followed a helpful tutorial to develop this task, which can be found here: https://learn.microsoft.com/en-us/azur ...

Handling events in React using TypeScript

Currently diving into the world of React with Typescript and encountered a challenge involving event handling using the onClick property. I have a react component displaying a list of items from an array, and I aim to log the clicked item in the console. I ...

What are the reasons behind the issues encountered when enabling "Optimization" feature in Angular that affect the proper rendering of PrimeNg elements?

Angular Version : 9.x Primeng Version : 9.x We've encountered an issue with PrimeNg elements not rendering correctly in our dev/prod environments, although they work fine in the local environment. After some investigation, we found that the culprit ...

What is the process for upgrading TypeScript to the latest version?

Is there a way to upgrade TypeScript version for ASP.net MV5 project in Visual Studio 2015? I attempted searching through Nuget but couldn't locate it. There seems to be an issue with the razor intellisense (index.d.ts file) and I'm hoping that ...

How can I display a new module in Angular without navigating to it?

After following the tutorial at https://angular.io/guide/lazy-loading-ngmodules#create-a-feature-module-with-routing I set out to create the following: My goal is to have a dedicated module for all customer-related components accessible through the /cust ...

How can I retrieve the text input from a physical barcode scanner in a React Native screen?

Currently, I am developing a mobile application with React Native version 0.68.2. I am seeking a solution to detect barcode scanning events from a physical scanner hardware that is integrated into the mobile device as a handheld scanner. The key requirem ...

Issue with OpenAI's Rate Limit 429 Restriction

I've been experimenting with this repository in order to implement semantic search for YouTube videos using OpenAI + Pinecone. However, I keep encountering a 429 error at the following step - "Run the command npx tsx src/bin/process-yt-playlist.ts to ...

Changing a photo into base64 format using Angular 2

How can I convert an image to base64 in Angular 2? The image is uploaded from the local filesystem. Currently, I am using fileLoadedEvent.target.result to achieve this. However, when I try to send this base64 string through REST services to Java, it fails ...

What is the reason behind the TypeScript HttpClient attempting to interpret a plain text string as JSON data?

When utilizing Angular TypeScript, I have implemented the following approach to send a POST request. This request requires a string parameter and returns a string as the response. sendPostRequest(postData: string): Observable<string> { let header: ...

What is the best way to display my images within Material UI Cards using React with Typescript?

I'm currently faced with a challenge of rendering images within Material UI's cards. I am successfully passing props to each card from a constants.tsx file where the heading, description, and bodyHeader are all properly read, but for some reason, ...

React component is being invoked prior to the completion of the fetch operation

In my React code, I am facing an issue with the fetch function not completing in time to pass data to the Chart component. As a result, the chart is rendered without any graph displayed. export const OverviewChart = () => { type dateValue = { x: n ...

React-bootstrap-table Axios delete operation causing [object%20Object] to be displayed in the browser console

I am having trouble executing a delete operation using axios on a react-bootstrap-table, and encountering this error in the console DELETE http://localhost:9000/api/terminals/[object%20Object] Uncaught (in promise) Error: Request failed with status cod ...