Get express js url rout based on id in database - sql

I have sqlite database with article table that contains articleID and articleDescription.
I'm using expressjs as server:
app.get('/articleDetail/:id',function(req,res){
res.sendFile(path.join(__dirname+'/articleDetail.html'));
var id = req.params.id;
var query = "select * from article where articleID = " + id;
});
I want the to show the content of the article based on its id in the database.
An example: when you select article 5 on the home page, it take you to url ..../articleDetail/5 and the content is shown to the user.
Summary of the 2 questions I have:
How can I connect and id in my database to the id of the url/route?
How can I show the correct article content based on its id using express?
Thanks for any help.

Install module called sqlite3 into your project. It provides exactly what you want, here you can see detailed guide on how to install it and use it for connecting and querying database - http://www.sqlitetutorial.net/sqlite-nodejs/connect/

So steps should be like this:
Get article id from request or url params e.g let id = req.params.id
Execute query and get the item from database
pass the value (article) object to view and render there
app.get('/articleDetail/:id',function(req,res){
var id = req.params.id;
var query = "select * from article where articleID = " + id;
// execute the query here
res.sendFile(path.join(__dirname+'/articleDetail.html'));
});

Related

Elastic Search Complex Logical Query (SQL to ElasticSearch Query)

My document schema is as follows.
Post
Id
Title
Body
Privacy -> Values can only be ["Me", "Anyone", "Team"]
UserId
What I want to do is retrieve all documents, but exclude documents where
Privacy = "Me"
and
UserId != "My-User-Id"
The SQL equivalent code is
SELECT * from Posts
WHERE (Privacy = "Anyone" OR Privacy = "Team")
OR (Visibility = "Me" AND UserId = 'My-User-Id')
I want to know how I can convert that SQL to the Elastic Search equivalent.
Thanks, any help would be appreciated.

SFDC - Query all contacts shared with a given user?

I'm somewhat of a SFDC novice when it comes to integration, but is there any way I can query just the contacts shared with a given user, taking into account all the ways the share can occur? Essentially just see the same contacts the user would see in within the platform?
I think this is what you are looking for. I added some inline comments to explain what each step is doing. The end result should be all the contacts that can be read by a specified user in your org.
// add a set with all the contact ids in your org
List<contact> contacts = new List<contact>([Select id from Contact]);
Set<ID> contactids = new Set<ID>();
for(Contact c : contacts)
contactids.add(c.id);
// using the user record access you can query all the recordsids and the level of access for a specified user
List<UserRecordAccess> ura = new List<UserRecordAccess>([SELECT RecordId, HasReadAccess, HasTransferAccess, MaxAccessLevel
FROM UserRecordAccess
WHERE UserId = 'theuserid'
AND RecordId in: contactids
] );
// unfortunatelly you cannot agregate your query on hasReadAccess=true so you'd need to add this step
Set<id> readaccessID = new Set<ID>();
for(UserRecordAccess ur : ura)
{
if(ur.HasReadAccess==true)
{
readaccessID.add(ur.RecordID);
}
}
// This is the list of all the Contacts that can be read by the specified user
List<Contact> readAccessContact = new List<Contact>([Select id, name from contact where id in: readaccessID]);
// show the results
system.debug( readAccessContact);

OrientDB Select on two related vertex

I have a scenario as shown below ,
I want to query the database so I get the following result,
User Resource Permissions
Edi Plan A [view]
Where
resource.name = 'Plan A' and user.name = 'Edi'
my query for above is
SELECT name,
out('hasARole').out('ofType').in('isOfType')[name = 'Plan A'].name,
set(out('hasARole').out('hasA').name) as permission
FROM user
WHERE name = 'Edi'
It should display
User Resource Permissions
Adrian Plan A [view,edit, delete]
if I change it to,
Where
resource.name = 'Plan A' and user.name = 'Adrian'
my query for above is
SELECT name,
out('hasARole').out('ofType').in('isOfType')[name = 'Plan A'].name,
set(out('hasARole').out('hasA').name) as permission
FROM user
WHERE name = 'Adrian'
Now above queries work as long as the users don't have another role on another type of resource. e.g. if Edi had Admin role on let's say a resource type of Workspace then the query gives me back all the permissions that an Admin would have , instead of just view as he only has view permission on Plan A
I have used the following graph for my answer. Note that I have corrected some incositencies with your original edges.
I see a number of possible queries for this problem. I am a bit confused why you would want to return the User and Resource in the query, as you probably already have these records due to the fact you use them to create the query. You can't 'nest' the full records in the results either (unless you JSON them). Further to this, querying on the name field, and returning only the name field seem a little nonsensical to me - but maybe you have done so to simplify the question. Regardless, the following queries will get you on your way to your desired results.
My first idea is to run a query to get all of the Roles related to a Resource. We then run a query over these results to filter for the Roles that include the User. This looks like the following;
select from (
select expand(out('isOfType').in('ofType')) from Resource where name = "Plan A"
) where in('hasARole') contains first((select from User where name = "Edi"))
This query correctly returns just the Viewer record for both Edi and Adrian.
My second idea is to run 1 query for the Roles related to a Resource (similar to above), and another for the Roles related to a User, and then find the intersect. This looks like the following, and gives the same results as the query above;
select expand(intersect) from (
select intersect($resource_roles, $user_roles)
let
$resource_roles = (select expand(out('isOfType').in('ofType')) from Resource where name = "Plan A"),
$user_roles = (select expand(out('hasARole')) from User where name = "Edi")
)
Now if you really do want the User, Resource and Permissions all in the 1 result, you can use the following, or a variant of;
select first($user).name as User, first($resource).name as Resource, intersect(resource_roles, user_roles).name as Permissions from (
select $resource.out('isOfType').in('ofType') as resource_roles, $user.out('hasARole') as user_roles
let
$resource = (select from Resource where name = "Plan A"),
$user = (select from User where name = "Edi")
)
or
select first($user).name as User, first($resource).name as Resource, intersect($resource_roles, $user_roles).name as Permissions
let
$resource = (select from Resource where name = "Plan A"),
$resource_roles = (select expand(out('isOfType').in('ofType')) from $parent.$resource),
$user = (select from User where name = "Edi"),
$user_roles = (select expand(out('hasARole')) from $parent.$user)

Retrieve all values from a column using PARSE.com in .NET Windows 8 store apps

I have a class in my PARSE database as "Categories" with column name "category". The "category" column contains some values say like "sports","fun" etc. Now i need to retrieve all the values from "category" column.So that I could show the values in a listbox in my windows 8 app.I checked the PARSE.com .NET Guide and used the follwing code
ParseQuery<ParseObject> query = ParseObject.GetQuery("category"); .Now how to get all the values of the category class and populate it in listbox?Hope I explained my question
Try this:
ParseQuery<ParseObject> query = ParseObject.GetQuery("Categories");
ParseObject categories = await query.GetAsync("categoriesID");
var categoryList = categories.Get<IList<string>>("category");
Try the following code:
ParseQuery<ParseObject> query = ParseObject.GetQuery("Categories");
IEnumerable<ParseObject> parseObjects = query.FindAsync();

Querying NHibernate for paging in a web app

Given a query for example:
from Users u where u.Country = "US"
I have a web app with a custom grid.
How do I query NHibernate to bring back a specfic page results
given page size and index???
Malcolm
Use SetFirstResult and SetMaxResults on the query or criteria:
int pagesize = 10;
int page = 2;
query = session.createQuery("...")
.SetFirstResult(pagesize * page)
.SetMaxResults(pagesize);