ServiceStack batabase first OrmLite.Poco.cs generated, now what? - servicestack

there are extremely few docs about ServiceStack around. Let alone database first approach. So now I have OrmLite.Poco.cs with all the models mapped from SQL Server. What is the next step? Add routes to the models on OrmLite.Poco.cs or copy out each class into individual .cs file then add routes to it? Thanks.

Great you now have your Table POCO's which you can start querying with, e.g:
var results = Db.Select<Poco>();
Custom Routes only go on Request DTO's you should not re-use your OrmLite models to also define the contract for your Services.
For an example, checkout this Simple OrmLite Database sample showing how to use the Customer Table POCO in OrmLite to create a Simple Customer REST Service.

Related

Are Entities between two sub-apps in NestJs sharable?

I am very new to NestJS and I am trying to create a project with multiple subapps. The query that I have is, if one entity is present in one sub-app, can we use that same entity in another sub-app of the same project?
This is my project strucutre:
apps
subApp1
entities
- entityone.ts
controller, modules and services
subApp2
entities
- entityTWo.ts
controller, modules and services
I want to use the entity one in subApp2 service. Is it possible? If so, how we can acheive this? Is there any better way to approach this situation where I need to use Entity of one sub app into another.
I tried exporting the Entity from one sub-app and import the Module in another sub app. But I am getting this error:
Nest cannot export a provider/module that is not a part of the currently processed module (StoreModule). Please verify whether the exported Store is available in this particular context.
I am not sure, if it was the correct way for this situation, but this solution came into my mind and i tried it.
you don't need to specify entities for every module.
keep them all together apart, so you can use them wherever you want.

Best Practice Advice - Loopback API

I want to make a webservice and it looks like Loopback is good starting point.
To explain my question, I will describe situation
I have 2 MySQL Tables:
Users
Companies
Every User has it's Company. It's like master User for it's company.
I wish to create Products table for each company next way:
company1_Products,
company2_Products,
company3_Products
Each company have internal Users, like:
company1_Users,
company2_Users,
company3_Users
Internal users are logging in from corresponding subdomain, like
company1.myservice.com
company2.myservice.com
For the API, I want datasource to get Products from the corresponding table. So the question is, how to change datasource dynamically?
And how to handle Users? Storing in one table is not good, because internal company users could be in different companies...
Maybe there's better way to do such models?
Disclaimer: I am co-author and one of current maintainers of LoopBack.
how to change datasource dynamically?
The following StackOverflow answer describes a solution how to attach a single model (e.g. Product) to multiple datasources: https://stackoverflow.com/a/28327323/69868 This solution would work if you were creating one MySQL database per company instead of using company's name as the prefix of Product table name.
To achieve what you described, you can use model subclassing. For each company, define a new company-specific Product model inheriting from the shared Product model and changing the table name.
// common/models/company1-product.json
{
"name": "Company1_Product",
"base": "Product",
"mysql": {
"tableName": "company1_Products"
}
// etc.
}
You can even create these models on the fly using app.registry.createModel() and app.model() APIs, and then run dataSource.autoupdate to create SQL tables for the new model(s).
And how to handle Users? Storing in one table is not good, because internal company users could be in different companies...
I suppose you can use the same approach as you do for Products and as you described in your question.
Maybe there's better way to do such models?
The problem you are facing is calling multi-tenancy. I am afraid we haven't figured out an easy to use solution yet. There are many possible ways how to implement multi-tenancy.
For example, you can create one LoopBack application for each Company (tenant) and then create a top-level LoopBack or Express application to route incoming requests to appropriate tenant-specific LB app instance. See the following repository for a proof-of-concept implementation: https://github.com/strongloop/loopback-multitenant-poc

Entity CodeFirst EF6, EntityData string id

Have started to use Entity Code First with Azure Mobile Services and have met some issue:
For using TableController with Entity objects I need to derive each of them from EntityData class, that provide string Id field!
I used to use int, long and even guid instead string for id, so...
What is benefits of this?
What about insert operation and what is the best practice for this?
What about performance for string id?
#carlosfigueira has a good writeup about the change to using string id here: http://blogs.msdn.com/b/carlosfigueira/archive/2013/11/23/new-tables-in-azure-mobile-services-string-id-system-properties-and-optimistic-concurrency.aspx
EntityData is a class which Dtos should implement because it is required for offline feature, at least was I got as answer.
To create a Table controller you need to use it, but you can ignore if you want... Suppose you have a class called "CarDto" the dto will have a property "carDto.Id" that is from EntityData, but you can create the "carDto.CarId" as your key! The only thing you need to do is create a model based in that you want and match it with your database and then use AutoMapper to match the car's model with car's dto :)
See Azure Mobile Services - Samples and articles to help developers to use Azure Mobile Services, which has a sample about to match dtos to model.
Or you can apply this approach Build a service using an existing SQL database with the Mobile Services .NET backend, but I do not like because in my opinion is confuse.... you choose what fits your needs :)

Sail.js - how to structure JSON based live data output with existing static data in the model

In my Angular app, I want to display a table which contains the following
a) URL
b) Social share counts divided by different social networks
Using Sails.js, I already have the api created for the URL when the results show up, I can display the URL now I'm confused how to get the appropriate social counts showing right besides
Here's the API I'm using: https://docs.sharedcount.com/
by itself, I can see the JSON it produces
But here are my questions:
Should I create a new api (model/controller) for social count data or include it in my model where I have the 'url' action defined?
If I create a new api or include the social_counts as an action in the current, what would my JSON query look like? to retrieve the URL's, I'm using default API blueprint that Sails provides, so:
http://www.example.com/url/find?where={"title":{"contains":"mark"}}
Struggling a bit in terms of the thought process, would be great to get input on this
It depends on your app. is your app will store that data or just consume it? If it need to store, of course you need the API. In purpose for modification or aggregating the data for example.
No, you can't do that. That shortcut method only works if you have the data in your database and let the Sails Waterline ORM and Blueprint API served it.
Perhaps, if you only need to consume the data from that Sharedcount API, you didn't need to use Sails as a backend, in this context. Just use Angular as a client of that API. Except if you need to modify the data first and store it in your own database, so Sails will helps with it's Waterline ORM and Blueprint API.

Sequelize REST API generator

I am developing a Nodejs application and my database is Postgres and I am using Sequelize as my ORM because of its excellent support for migrations.
I am on the lookout for a good REST API generator based on the schema I have defined. There are two main hurdles I am facing and they are that the generators don't do a good job of creating association API routes and lack of ACL support.
On the associations front, my schema has multiple levels of association i.e. for example..
Student.hasMany(Courses);
Courses.hasMany(Subjects);
So ideally the generated REST API should be something like
/student/:student_id/course/:course_id/subject/:subjectId
I found a few projects that are doing this, but are incomplete.
https://github.com/sequelize/sequelize-restful - is good but does not have ACL support
https://www.npmjs.org/package/restizr - is in alpha stage and does not generate API routes for associations.
Is there any module that supports this?
What you were doing here is writing a webservice without a domain model. https://en.wikipedia.org/wiki/Anemic_domain_model Ofc. you have every right to do it, but I wonder if you really understood what a webservice means. https://stackoverflow.com/a/1530607/607033 It is not a database with CRUD HTTP interface normally, though nowadays it is popular doing something this way and call it REST. A response to a REST HTTP request is a viewmodel https://softwareengineering.stackexchange.com/a/425001/65755 and it contains not just data, but a lot of metadata and hyperlinks. A REST API is a special type of webservice with many constraints. https://www.ics.uci.edu/~fielding/pubs/dissertation/fielding_dissertation.pdf http://www.markus-lanthaler.com/research/hydra-a-vocabulary-for-hypermedia-driven-web-apis.pdf Your ORM is used 2 layers deeper in the data layer and it has nothing to do with the presentation layer where your REST API should be. I really wonder why people are making applications, which are doing nothing except serving data directly from the database and use the most inconvenient technology to do it. I guess there are databases nowadays with ACL and REST API support, so all you need is just using them. https://learn.microsoft.com/en-us/rest/api/sql/ Or there was something for PgSQL and Nodejs too around the time you asked this. https://github.com/QBisConsult/psql-api

Resources