GithubHelp home page GithubHelp logo

typescript-express-example's Introduction

Example how to use Express and TypeORM with TypeScript

  1. clone repository
  2. run npm i
  3. edit ormconfig.json and change your database configuration (you can also change a database type, but don't forget to install specific database drivers)
  4. run npm start
  5. open http://localhost:3000/posts and you'll empty array
  6. use curl, postman or other tools to send http requests to test your typeorm-based API

How to use CLI?

  1. install typeorm globally: npm i -g typeorm
  2. run typeorm -h to show list of available commands

typescript-express-example's People

Contributors

adetorodev avatar alexmesser avatar crutchcorn avatar hieptuanle avatar pleerock avatar shicholas avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

typescript-express-example's Issues

Is this concurrency-safe?

You have a single createConnection() happening once at the top of this example. I understand that creates a connection pool. But how does TypeORM when it should add vs reuse connections in the pool? (If you have two requests coming in at the same time, and each request handler makes some queries, how does TypeORM know which queries go to which connection?)

What is the best way to test this?

I'm wondering how to write tests for this kind of express/typorm project setup.

How to mock the Repositories?

Any ideas/recommendations?

运行时这个是什么问题?

运行的时候显示这个错误:
tsc && node src/index.js

node_modules/typeorm/driver/mongodb/typings.d.ts:4288:13 - error TS2339: Property 'asyncIterator' does not exist on type 'SymbolConstructor'.

4288 Symbol.asyncIterator: AsyncIterableIterator;
~~~~~~~~~~~~~

node_modules/typeorm/driver/mongodb/typings.d.ts:4288:31 - error TS2304: Cannot find name 'AsyncIterableIterator'.

4288 Symbol.asyncIterator: AsyncIterableIterator;
~~~~~~~~~~~~~~~~~~~~~

How to set MySQL Transaction within the request action

Hi,

Is there any approach how to set a mysql transaction inside a single request action method? So we may be able to rollback transaction whenever error occurs.

Code example:

export async function saveDepartmentAction(request: Request, response: Response) {
    // data
    let data = request.body

    // insert department data
    const result = await getEntityManager().query(`
        // Some department insert (parent data)
    `, [ some department data ])


    // add child data (department positions)
    data.positions.forEach(async position => {
         const add_position = await getEntityManager().query(`
             // Some position insert (child data)
         `, [ some department id and position id ])        
     });

    response.send(data)
}

I would like to achieved something like this https://github.com/mysqljs/mysql#transactions

Give an example on how to construct a POST request

Either using curl or Postman:

Keep getting this error:

QueryFailedError: ER_NO_DEFAULT_FOR_FIELD: Field 'title' doesn't have a default value at new QueryFailedError (D:\business\prototyping\development\src\node\typescript-express-example\node_modules\typeorm\error\QueryFailedError.js:11:28) at Query.<anonymous> (D:\business\prototyping\development\src\node\typescript-express-example\node_modules\typeorm\driver\mysql\MysqlQueryRunner.js:169:45) at Query.<anonymous> (D:\business\prototyping\development\src\node\typescript-express-example\node_modules\mysql\lib\Connection.js:525:10) at Query._callback (D:\business\prototyping\development\src\node\typescript-express-example\node_modules\mysql\lib\Connection.js:491:16) at Query.Sequence.end (D:\business\prototyping\development\src\node\typescript-express-example\node_modules\mysql\lib\protocol\sequences\Sequence.js:83:24) at Query.ErrorPacket (D:\business\prototyping\development\src\node\typescript-express-example\node_modules\mysql\lib\protocol\sequences\Query.js:90:8) at Protocol._parsePacket (D:\business\prototyping\development\src\node\typescript-express-example\node_modules\mysql\lib\protocol\Protocol.js:291:23) at Parser._parsePacket (D:\business\prototyping\development\src\node\typescript-express-example\node_modules\mysql\lib\protocol\Parser.js:433:10) at Parser.write (D:\business\prototyping\development\src\node\typescript-express-example\node_modules\mysql\lib\protocol\Parser.js:43:10) at Protocol.write (D:\business\prototyping\development\src\node\typescript-express-example\node_modules\mysql\lib\protocol\Protocol.js:38:16)

TypeScript compilerOptions -> "strict" : true

Hi There

In typescript-express-example/src/index.ts you have some beautifully elegant code:

`
// register all application routes

AppRoutes.forEach(route => {

    app[route.method](route.path, (request: Request, response: Response, next: Function) => {

        route.action(request, response)

            .then(() => next)

            .catch(err => next(err));

    });

});

`

That registers all applications routes.

If you enable

"strict": true

in the tsconfig.json I get the error:

src/index.ts:19:4 - error TS7017: Element implicitly has an 'any' type because type 'Express' has no index signature. 19 app[route.method](route.path, (request: Request, response: Response, next: Function) => { ~~~~~~~~~~~~~~~~~

Is there a way to combine modules in this elegant way and enable strict?

Kind regards

[Bug] Getting error when using Postgres and generating migration

After downloading, installing and adding pg package, i created a database and run this: typeorm migrations:generate -n seller and the result is this:
image

The only change i made was the connection args and the installation of postgres.

Any idea why this error?

How can we organize a full jest testing project with express and typeorm

Issue type:

[x ] question
[ ] bug report
[ ] feature request
[ ] documentation issue

Database system/driver:

[ ] cordova
[ ] mongodb
[ ] mssql
[x ] mysql / mariadb
[ ] oracle
[ ] postgres
[ ] cockroachdb
[ ] sqlite
[ ] sqljs
[ ] react-native
[ ] expo

TypeORM version:

[x ] latest
[ ] @next
[ ] 0.x.x (or put your version here)

I'm adding unit testing by jest for express and typeorm but it's seem many things to handle from mocking server, app and connection from typeorm.
If somebody have already build the example and share it. that would be great.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.