dynode createtable with shema - javascript

I'm trying to implement dynode to talk with dynamodb form a node.js script.
There are several examples on the Internet that shows how to do that with dynode, such as:
var opts = {read: 20, write: 25, hash: {name: String}, range: {age: Number}};
dynode.createTable("ExampleTable", opts, console.log);
which actually does the trick and works for me...
The issues I'm having is that I need to implement a shema on the same time when I create the database, or at least after that, a shema means that I need to tell the dynamodb to add a first_name field with string format, an address with string format etc...
there is no way nor instructions that shows how to do that ...
It's annoying to spend so much time on a so simple issue, because it is not well documented...
here is the dynode github web site:
Thanks

DynamoDB doesn't have schemas (if that's what you mean).
You can't force the table to represent a fixed set of attributes.
You can make sure in your application code that each item you store in the table will have the attributes you require, but the DynamoDB API won't enforce it.
Look here how to build your Item objects before putting them in db

Related

How to skip,sort and limit using dynamoose?

I previously used MongoDB and mongoose, now try to implement dynamoose in my app,
how to write the same query in dynamoose for below-shown mongoose query?
return this.scan({ abcd: { $ne: null }, activeFlag: true }).skip(9 * (page - 1)).sort({ date: -1 }).limit(9)
I need the same for
$ne - not equal to
skip
sort
and also limit
Dynamoose currently doesn't have support for a skip function. This is mainly because due to my understanding DynamoDB doesn't have a skip type function on their platform.
For doing something like not equal you can use the .not() syntax in Dynamoose.
For sorting you have to use the Query.descending() or Query.ascending() syntax instead of Scan.
You can limit the number of items DynamoDB will scan or query by using Query.limit() or Scan.limit(). Please keep in mind that this limits the number of items scanned or queried on DynamoDB before any filters are applied. So if you are filtering out items in your scan or query the limit will limit the number of items that even get considered for the filter.
Finally it's important to understand that although Dynamoose was heavily inspired by Mongoose it's not meant to be a plug and play replacement. There are some major fundamental concepts that are very different between MongoDB and DynamoDB, which makes a plug and play system basically impossible.
Therefor I would highly encourage you to read through both the Dynamoose and DynamoDB documentation to understand the benefits and limitations of DynamoDB and ensure it really meets the needs for your given project.
Of course you can do everything you want to do after the results get returned from DynamoDB inside of the Dynamoose callback function by writing your own code, but depending on how many results you have, how many items you have in the table, how fast you need this to be, etc. that might not be a very good option. So although everything you are asking for is possible by writing your own code, there is a chance it won't be as good as MongoDB, and because I don't know MongoDB quite as well as DynamoDB, and because I don't know how those specific Mongoose functions work I can't really speak to that.

MongoDB: How to Sort a Query Before Updating

I'm writing a Meteor (Node.js) app which uses MongoDB on the backend. At a certain point in my code, I need to update a specific document within a collection. I need to use Mongo's update() method, but I'm having trouble passing in the proper (complex) query to narrow down to that one, specific document. The document I'm trying to update is:
db.collection.find({market: 'AAA'}).sort({creationDate:-1}).limit(1)
In words, the one document in collection that has a market of AAA and was created most recently (creationDate is a UNIX timestamp number, e.g. 1408829429914). There are multiple documents with a market of AAA, so I am using sort() and limit(1) to find the document which was created most recently.
Mongo's update() doesn't seem to accept sorting parameters as part of the query before the update process. What can I do to narrow down to this document and update it? Thanks!
You will need to fetch the document you want and then update it by _id. If your collection was called Markets, the code would look like:
var market = Markets.findOne({market: 'AAA'}, {sort: {creationDate:-1}});
Markets.update(market._id, {$set: {fancy: true}});
It's worth mentioning that even if MongoDB supported the optimization you are looking for, meteor client code can only update by _id anyway.

Meteor mongo insert unique document

I have a simple Tags Collection in Meteor. Currently in order to ensure that a user cannot create a duplicate Tag document I do this:
var existingTag = Tags.findOne({name: "userInput"})
If existingTag is undefined then I can go ahead and do the insert.
Is there a better/correct way of doing this utilizing meteor mongodb syntax? Cant seem to find any documentation on this.
Thanks.
A good solution is to create Mongo index at the unique field. That way you'll have the uniqueness validation at Mongo level, as well as performance increase for searches on that field.
Meteor currently doesn't support index creation directly, so you need to manually log in to your database and add your index from there. The command for this is:
db.tags.ensureIndex({name: 1}, {unique: true})
Here and here you can find more information.

OData:Wildcard (startswith) filtering for number (ID) fields in the url request

I've been researching a way to perform fuzzy searches for my numerical ID fields of my entity via OData and JavaScript. So far, I have not found the answer I am looking for. I can filter other edm.string columns perfectly using the "Startswith" filter option, however when i attempt to pass in any other non-string type, I get a type error response from the server back.
In applications that I control the database, I was successfully able to get around this, by creating views I need and converting the numerical type of the view to a string. However, this seems a bit overkill to go out of my way and create a view for an entire set of data just so I can allow a user to wildcard search the ID, when otherwise the OData query works perfectly well.
Has anyone found a good solution to this? Thank you!
Unfortunately I think you've already discovered one of the best solutions (creating a view). You could also create a service operation that allows you to do fuzzy search.
What are you using for your backend? This isn't supported on LINQ-to-Entities, but you might be able to create a service operation that looks something like this (to prove that it can work, you can stuff a ToList() call in there after Products, just be sure not to deploy something like that to production :)):
[ServiceBehavior(IncludeExceptionDetailInFaults = true)]
public class ScratchService : DataService<ScratchContext>
{
[WebGet]
public IQueryable<Product> FuzzySearch(string idStartsWith)
{
var context = new ScratchContext();
return context.Products.Where(p => p.ID.ToString().StartsWith(idStartsWith));
}
// ...
}
It's not an ask we've heard a lot, but I can definitely bring it up on the team and as we start the OASIS standardization process it's something we can be thinking about.

How to do SQL-like queries in client side browser?

I've been looking for a way to do complex queries like SQL can perform but totally client side. I know that I can get the exact results that I want from doing SQL queries off of the server and I could even AJAX it so that it looks smooth. However for scaleability, performance, and bandwidth reasons I'd prefer to do this all client side.
Some requirements:
Wide browser compatibility. Anything that can run jQuery is fine. I'd actually prefer that it be a jQuery plugin.
Can sort on more than one column. For instance, order by state alphabetically and list all cities alphabetically within each state.
Can filter results. For instance, the equivalent of "where state = 'CA' or 'NY' or 'TX'".
Must work completely client side so the user only needs to download a large set of data once and can cut the data however they want without constantly fetching data from the server and would in fact be able to do all queries offline after the initial pull.
I've looked around on stackoverflow and found jslinq but it was last updated in 2009 and has no documentation. I also can't tell if it can do more complex queries like ordering on two different columns or doing "and" or "or" filtering.
I would think that something like this would have been done already. I know HTML5 got started down this path but then hit a roadblock. I just need basic queries, no joins or anything. Does anyone know of something that can do this? Thanks.
Edit: I think I should include a use case to help clarify what I'm looking for.
For example, I have a list of the 5000 largest cities in the US. Each record include Cityname, State, and Population. I would like to be able to download the entire dataset once and populate a JS array with it then, on the client side only, be able to run queries like the following and create a table from the resulting records.
Ten largest cities in California
All cities that start with "S" with populations of 1,000,000 or more.
Largest three cities in California, New York, Florida, Texas, and Illinois and order them alphabetically by state then by population. i.e. California, Los Angeles, 3,792,621; California, San Diego, 1,307,402; California, San Jose, 945,942...etc.
All of these queries would be trivial to do via SQL but I don't want to keep going back and forth to the server and I also want to allow offline use.
Take a look at http://linqjs.codeplex.com/
It easily meets all your requirements.
Try Alasql.js. This is a javascript client-side SQL database.
You can do complex queries with joins and grouping, even optimization of joins and where parts. It does not use WebSQL.
Your requirements support:
Wide browser compatibility - all modern versions of browsers, including mobiles.
Can sort on more than one column.- Alasql does it with ORDER BY clause.
Can filter results. - with WHERE clause.
Must work completely client side so the user only needs to download a large set of data once and can cut the data however they want without constantly fetching data from the server and would in fact be able to do all queries offline after the initial pull. - you can use pure JavaScript (Array.push(), etc.) operations to modify data (do not forget to set table.dirty flag).
Here is a simple example ( play with it in jsFiddle ):
// Fill table with data
var person = [
{ name: 'bill' , sex:'M', income:50000 },
{ name: 'sara' , sex:'F', income:100000 },
{ name: 'larry' , sex:'M', income:90000 },
{ name: 'olga' , sex:'F', income:85000 },
];
// Do the query
var res = alasql("SELECT * FROM ? person WHERE sex='F' AND income > 60000", [person]);
As long as the data can fit in memory as an array of objects, you can just use sort and filter. For example, say you want to filter products. You want to find all products either below $5 or above $100 and you want to sort by price (ascending), and if there are two products with the same price, sort by manufacturer (descending). You could do that like this:
var results = products.filter(function(product) {
// price is in cents
return product.price < 500 || product.price > 10000;
});
results.sort(function(a, b) {
var order = a.price - b.price;
if(order == 0) {
order = b.manufacturer.localeCompare(a.manufacturer);
}
return order;
});
For cross-browser compatibility, just shim filter.
How about Yahoo's YQL? I've only briefly looked at it, but it looks interesting.
Backbone is a pretty good js library which (their words) "gives structure to web applications by providing models with key-value binding and custom events, collections with a rich API of enumerable functions, views with declarative event handling, and connects it all to your existing API over a RESTful JSON interface."
I am not sure if this is what you are looking for but you can use it to mock up your model and bind event listeners to it. This seems to be a good tutorial to go through some of the base uses for it.
You can use CanJS. Its a relativelly new library that performs better then Backbone and other and its based on the infamous JavaScript MVC library. In reallity, its the MVC part of the JS MVC with a bit of spices.
You can take a look at this tut by net.tutsplus.com http://net.tutsplus.com/tutorials/javascript-ajax/diving-into-canjs-part-3/
It's pretty powerfull and fast. Has features like live binding that make's your life easy.
Coils is a Clojurescript framework which compiles to Javascropt and has client side SQL queries like this:
(go
(log (sql "SELECT * FROM test_table where name = ?" ["shopping"] )))
: It is full SQL that is passed to a server side relational database:
https://github.com/zubairq/coils

Categories