How to integrate a working typeorm project into an Angular 7 project

811 views
Skip to first unread message

Di Me

unread,
Dec 13, 2018, 10:54:13 AM12/13/18
to Angular and AngularJS discussion
I am currently working on an Angular 7 project using https://www.highcharts.com and its "Stockchart" for displaying data in that chart. I have gotten that "frontend" chart to work by reading in dummy data at the moment.

Apart from the above project, I also established a typeorm project version 0.2.8 (my backend) following the steps outlined in the typeorm documentation here https://www.npmjs.com/package/typeorm and am able to successfully access a MSSQL database in my local network that holds statistical data to be read into the Angular 7 project Stockchart mentioned before.

My aim is now to connect the two projects in order to be able to read in data from the backend and into the frontend project.

So far, I have tried the following approach recommended to me by others...

1. Run npm generate library somelibraryname in my Angular 7 project
2. "Porting" the syntax from the working typeorm project into the newly created library in my Angular 7 project
3. Trying to test the functionality of the MSSQL connection however results in CLI errors due to the "start" script being a duplicate and as such, unable to run both projects with "start" at the same time

As a result, I have ran into a dead-end and am now contemplating, whether I have used the correct approach in order to achieve the above goal which is getting access to the data from within the Angular 7 project.

The matter of this issue seems to be ambiguous as some people are obviously having similar problems without being able to solve them https://github.com/typeorm/typeorm/issues/2884 while others, such as some more professional companies have gone as far as to warn about incompatibility between Angular 7 and typeorm due to CLI issues https://hackernoon.com/from-typeorm-to-loopback-a-retrospective-188ea18527a2 and as a result, have opted for alternative solutions.

I have talked to the person who is responsible for the deployment/lead development of typeorm on https://www.npmjs.com on slack but have been told, that he is unable to provide unpaid support which resulted in the present dead-end.

Furthermore, I found this link https://github.com/typeorm/typeorm/issues/1290#issuecomment-420143054 indicating, that typeorm could potentially work with Angular 6 but which doesn't disclose sufficient details. I am still awaiting some response from that individual after having sent a request for elaboration on the post and possible solution as I am not only having the aforementioned doubts but am using Angular 7, not Angular 6 which could spell even more problems down the road if there is no confirmation of compatibility between the two.

I am therefore turning to this forum with my issue in hopes to find some viable input or to be pointed into the right direction.

For what it's worth, I am currently considering an alternative approach (though coming from a backend-developer background and being fairly new to Angular, I am not sure if this is possible at all and this is so far only an idea)...

- Using a library such as https://www.npmjs.com/package/ng-packagr to package/bundle my entire typeorm project in order to being able to import the resulting .tgz as a library into node_modules in the Angular 7 project and thus, getting access to the data this way (not sure if this is possible though)

What I am essentially looking for is either a solution or to be pointed into the right direction in terms of how to solve the problem...


Thanks in advance for any input, big or small.

Sander Elias

unread,
Dec 13, 2018, 12:04:17 PM12/13/18
to Angular and AngularJS discussion
Hi Di,

I just checked out typeOrm. Looks nice but the way it is built will conflict with the angular configuration that is provided with the CLI. While it might be possible to hack a way around this, it will bite every time you want/need to update one or the other.
So the option to buy paid for support by the maintainer might be the only maintainable way out.
If that's not an option you should consider moving to something else.

I never liked any of the ORM's they usually just get into my way. What I do myself lately is run swagger on my server, and generate interfaces, and validation function from there. Also, a default function that provides an 'empty' record is there. The entire system is free of classes, and clutter.

Regards
Sander

 

Tito

unread,
Dec 13, 2018, 12:08:59 PM12/13/18
to Angular and AngularJS discussion
If you are not able to resolve this issue I recommend the following working solution using another module. App I am developing consumes data from an api service that uses mssql npm package . It works wonderfully with Ms sql server.

Then from within my angular 7 app DataService.ts, I make a call to the endpoint http://ip.address:8081/api/virtual machines to fetch data to display on client side.

Tito

unread,
Dec 13, 2018, 12:46:12 PM12/13/18
to Angular and AngularJS discussion
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';


@Injectable({
providedIn: 'root'
})
export class DataService {
constructor(private http: HttpClient) { }
getVirtualMachines() {
}

getUsers() {
}
}

Tito

unread,
Dec 13, 2018, 12:48:51 PM12/13/18
to Angular and AngularJS discussion
module.exports = function(app) {
const sql = require('mssql'),
js2xmlparser = require("js2xmlparser");
var express = require( "express" )
, router = express.Router()

var api = '/api/';
var four0four = require('../../utils/404')();

router.get(api + 'virtualmachines', getVirtualMachines);


.....
function getVirtualMachines(req, res, next) {
for (var key in req.body) {
if(req.body[key] === '' ) {
req.body[key] = null;
}
}
var vmname = req.body.vmname;
var ipaddress = req.body.ipaddress;
var vmowner = req.body.vmowner;
const pool2 = new sql.ConnectionPool(config, err => {
if (err) console.log(err)
// ... error checks
pool2.on('error', err => {
console.log('ConnectionPool', err);
})
// Stored Procedure
pool2.request() // or: new sql.Request(pool2)
.input('vmname', sql.VarChar(50), vmname)
.input('ipaddress', sql.VarChar(50), ipaddress)
.input('vmowner', sql.VarChar(50), vmowner)
.execute('dbo.virtualmachines_sp', (err, result) => {
// ... error checks
console.log('ConnectionPool', err);
//console.dir(result)
res.send(result.recordsets[0])
})
})
} 

Di Me

unread,
Dec 14, 2018, 6:45:27 AM12/14/18
to Angular and AngularJS discussion
Hi Sander Elias,

and thanks for your swift response and the info.

That sounds kind of worrisome to be honest. I started out using https://www.npmjs.com/package/node-mssql at the beginning of my project which was no good due to some other issue (I was told that the server specs were too weak to be able to run the SQL query for each user request - which is part of that library setup - in the far future as the statistical data would increase exponentially in the years to come and hundreds of users potentially would access the system simultaneously) and had to find an alternative which turned out to be typeorm.

Considering your input and this new information on the limits within the CLI I'd say, that it looks like as if I have to repeat the process all over again which is frustrating at best. But I guess it's part of the learning process and there's nothing really I can do about it now. However, I'd like to 'damage-control' this as much as possible in order to minimize waste of time and efforts. So if you don't mind, could you post here about your way of doing things in a more detailed and elaborate way? For instance is there a good tutorial on this? Do you have your own way of doing things? If so, how would I proceed? What are the steps?

You were mentioning something about swagger which I am unfamiliar with. Are we talking about this particular library here https://www.npmjs.com/package/swagger as I found multiple libraries containing that name. I had a quick look at the documentation and am wondering about a couple of things. Does this library work with the Angular CLI? Would/could I run it from within the Angular 7 project as in does it have to be included into it? Is there compatibility? Need for packaging/bundling and importing it afterwards into the same in order to make things work?

To answer your question about paid support. Unfortunately, I am doing an unpaid internship in the public sector at the moment and there is no budget here either. That means that it's essentially me and the internet which makes this all the more frustrating.

As a note on the side..., the person writing this blog https://hackernoon.com/from-typeorm-to-loopback-a-retrospective-188ea18527a2 that I linked to in my initial post mentioned LoopBack as an alternative library to work with Angular (I am pretty sure his Angular version was prior to Angular 7 but nevertheless). Do you or has anybody else insight into or experience with this particular solution?

Looking forward to your response.

Thanks again for the heads up.

Di Me

unread,
Dec 14, 2018, 7:07:41 AM12/14/18
to Angular and AngularJS discussion
Hi Tito,

also to you, thanks for the very nice input and swift reply.

Actually, I am getting more and more doubts that I will be able to make things work with typeorm as I haven't found a workaround to the CLI issue yet.

I am a bit in doubt as to what you meant by the following...

"App I am developing consumes data from an api service that uses mssql npm package"

...and was hoping, you could provide me with a step-by-step guide, explaining the specifics of your solution. What app did you think of here? Are we talking a library from https://www.npmjs.com or is this a custom solution that you tailored to your needs? Another challenge I am dealing with is that the server I am supposed to use with my solution is - unfortunately - made up of poor physical specifications which hampers my efforts furthermore. Thus placing an API on it could or most likely would spell further trouble down the road. So if you would be so kind as to give me more details on your suggestion, software architecture you are using etc., that would be great.

Thanks in advance.

Sander Elias

unread,
Dec 14, 2018, 7:16:05 AM12/14/18
to Angular and AngularJS discussion
Hi Di,

Here is a link that will get you started with swagger. https://swagger.io/tools/open-source/getting-started/
What swagger does, traverses your DB, and API and make a machine-readable representation.
My solution is very custom to our use-case, and way of working. and frankly, I lack the time to create a good explainer on this. 
But there are tools available that can read swagger and do similar things. If I'm not mistaken, Manfred Styer has created a schematic that will create services for your rest endpoints. This is something that's readily available.

I have only little personal experience with ORM's and that experience is _old_ (8+ years ago). So I'm the wrong person on giving you advice on ORM systems. 
Looking at it from the outside Loopback seems to be a vailable option.

Regards
Sander

Di Me

unread,
Dec 14, 2018, 8:07:21 AM12/14/18
to Angular and AngularJS discussion
Hi again Sander Elias,

thanks. I'll have a look into that documentation.

Got to get familiarized with the details and see how things work in the first place.

As I understand this part of your answer...

"What swagger does, traverses your DB, and API and make a machine-readable representation."

...there is a need of an existing API. However, there is none at the moment. Right now, the MSSQL database is accessed directly in our local network as a mapped, shared network-drive. I have previously been told by the creator of typeorm, that an API would be needed but the issue is with the hardware specifics (physical machine) and that I'm restricted by keeping the load at a minimum by running as much code as possible on the client side.

I will also have a look at LoopBack and see whether or not this could be the way to go.

Thanks for now for all the info and directions provided. ;-)

Wishing you a nice weekend.

Tito

unread,
Dec 14, 2018, 11:37:52 AM12/14/18
to Angular and AngularJS discussion
I created a github repo for you here
0
yosiasz/express.mssql.api

Please take a look and let me
Know any question you will have.
This api is super lighweight.

Tito

unread,
Dec 14, 2018, 11:41:25 AM12/14/18
to Angular and AngularJS discussion
https://github.com/yosiasz/express.mssql.api

So the app I built is designed for
In house use for functional managers
To do yearly budgeting.

Sander Elias

unread,
Dec 15, 2018, 2:08:05 AM12/15/18
to Angular and AngularJS discussion
Hi Di, Tito,

I did some searching. There are some good articles to create a RESTfull server with node and MySQL 
Also, I found this: https://github.com/o1lab/xmysql, From the sound of your use case that would be enough to get you started.

Regards
Sander

Di Me

unread,
Dec 19, 2018, 4:11:09 AM12/19/18
to Angular and AngularJS discussion
Hello again!

You guys are awesome. Thanks so much for your input to both of you. Also, apologies for the delay in answer but I was a bit sick lately.

Tito, this is great. Thanks for the effort of uploading the project to github. I'm looking into this as we speak. Right now, I'm focusing on - maybe - getting typeorm packaged with ng-packagr by creating a separate project and adding the syntax to a library there. This is a bit of trial and error but if it works, I'd bypass the issue at hand and be out of the woods.

Other than that, I'm looking into Loopback and Swagger as mentioned earlier. If this all fails, I'll proceed with the options you guys just gave me.

Once again..., it's awesome to have your help guys. Highly appreciated...!

Di Me

unread,
Dec 19, 2018, 4:31:51 AM12/19/18
to Angular and AngularJS discussion
Hi Sander Elias,

as mentioned in my answer to Tito, you guys are great. Thanks for the heads up on the github repo in terms of creating a RESTfull server with Node and MySQL

There's just one caveat. I'm using MSSQL (maybe you misread that?) and am not sure if your solution is also applicable with that database. Could you confirm/refute this possibility?

I also had a look at our second link, Building REST API in the blink of an eye! (MySQL + NodeJS). This looks interesting but I assume that this is in continuation of your first link I commented on above as being a sort of "manual" so to speak on creating the API. Is this correctly understood?

Looking forward to your response. Thanks also to you for your time and kind help. :-)

Sander Elias

unread,
Dec 20, 2018, 11:54:36 PM12/20/18
to Angular and AngularJS discussion
Hi Di,

I did misread indeed. But those tools are available for mssql too. you might have to google around a bit to find it.

Regards
Sander
Message has been deleted

Di Me

unread,
Jan 8, 2019, 5:01:44 AM1/8/19
to Angular and AngularJS discussion
Hello again Sander Elias,

back on the project after the holidays.

No, problem and good to hear that your suggestion also works with MSSQL. I'll take a look at it later on. In the meantime, I found a good article in regards to the issue which I am trying to implement right now. If it works out, I'll turn back here with the details. Alternatively, I'll follow up on your idea.

Thanks again for your time and help and top of the day to you. :-)
Reply all
Reply to author
Forward
0 new messages