does Oracle NoSQL DB Cloud Service have one dedicated API to simply check if a table exists or not? - oracle-nosql

I am using Oracle NoSQL Database Cloud Service, I am using the API to Create, modify and drop Tables and Indexes. Also to do CRUD operations on my table.
But I am wondering if there is a dedicated API to simply check if a table exists or not?

Please call getTable() function. This functions retrieves static information about a table, including its provisioned througput, capacity and schema, in the form of TableResult. Dynamic information such as usage() is obtained using getTableUsage
if the table does not exist, it will return an exception.
Here an example in Node.js
try {
let resExistingTab = await client.getTable('THETABLE');
await client.forCompletion(resExistingTab);
console.log(' Table %s exists', resExistingTab.tableName);
console.log(' Table state: %s', resExistingTab.tableState.name);
let resTab = await client.getTable('UNKNOWNTABLE');
await client.forCompletion(resTab);
console.log(' Table %s exists', resTab.tableName);
console.log(' Table state: %s', resTab.tableState.name);
}
catch (e) {
console.log(e._errCode);
}
finally {
console.log("entering and leaving the finally block");
}
BTW, after reading a little bit more, You can also use Lists tables documented here
Below a snippet in node
let varListTablesResult = await client.listTables();
console.log(varListTablesResult);
Names are returned in alphabetical order to facilitate paging (seems to be case sensitive)

Related

Sharing database access with many users on Parse- ACLs or CLPs?

I am currently handling my database user groups with ACLs. This works okay as per below and as per my logic and models. I'm mainly doing this because ACLs seem easier to implement. Parse has a somewhat confusing example using both CLPs and ACLs.
I would like to know if there is advantage to using CLPs over ACLs as I'm doing below or is my solution satisfactory, I.E. would what I'm doing below encounter any issues of any kind?
I'm using Parse & Back4App.
My demo project:
/*
Info: each user will have list of all authorized users ACL (user.objectID) and these
will all be written to any object crud actions.
*/
Future<void> saveUser001GroupData() async {
final acls = ParseACL();
var box = await Hive.openBox<String>(user001GroupIDsBox);
user001GroupIDs = box.values.toList();
for (final id in user001GroupIDs) {
acls.setReadAccess(userId: id, allowed: true);
acls.setWriteAccess(userId: id, allowed: true);
}
for (final color in user001GroupData) {
color.setACL(acls);
await color.save();
}
}

How to get "COUNT(*)" in Supabase

I want to retrieve row count in Supabase.
I am guessing it would be something like this:
const { data, error } = await supabase
.from('cities')
.select('name', 'COUNT(*)')
Is this possible with Supabase?
For future visitors, we are working on this functionality with the PostgREST maintainer:
https://github.com/supabase/postgrest-js/issues/94
(I'm a maintainer)
This is now released:
const { data, count } = supabase
.from('countries')
.select('*', { count: 'exact', head: true })
If you want to return the results simultaneously, remove head:
const { data, count } = supabase
.from('countries')
.select('*', { count: 'exact' })
As a workaround, you could write a standard Postgres stored procedure or function that returns the count then call that via the SB client.
https://supabase.io/docs/client/rpc
It is currently not supported yet, but there is a WIP issue on Github that would bring this feature to Supabase.
The code below has not been implemented in Supabase yet, but it might look something like this:
const { data, error, count } = await supabase
.from('table')
.select('*')
.gt('id', 10)
.count()
Edit 7.19.2021
As kiwicopple has answered, this feature has been added to Supabase with a slightly different form from what I have above. View the accepted answer for more.

request.object.id not returning in afterSave() in Cloud Code

Parse.Cloud.afterSave(function(request) {
var type = request.object.get("type");
switch (type) {
case 'inspiration':
var query = new Parse.Query("Inspiration");
break;
case 'event':
var query = new Parse.Query("Event");
break;
case 'idea':
var query = new Parse.Query("Idea");
break;
case 'comment':
break;
default:
return;
}
if (query) {
query.equalTo("shares", request.object.id);
query.first({
success: function(result) {
result.increment("sharesCount");
result.save();
},
error: function(error) {
throw "Could not save share count: " + error.message;
}
});
}
});
For some reason request.object.id is not returning the object id from the newly created record. I've tested this code out throughly and have isolated it down to the request.object.id variable. I've even successfully ran it with using a pre-existing object ID and it worked fine. Am I using the wrong variable for the object ID?
Thanks in advanced for any help!
Had this exact problem a few weeks ago.
It turned out to be a bug in Parse's newest Javascript SDK. Please have a look at your CloudCode folder - it should contain a global.json file where you can specify the JavaScript SDK version. By default, it states "latest", change it to "1.4.2" and upload your CloudCode folder again.
In case the global.json file is missing in your cloud code folder, please have a look at this thread, where I described how to create it manually.
Thanks for the reply. I found out another work around for this for version 1.6.5. I should probably also mention that my use case for this code is to increment a count column (comments count) when a new relation has been added to a particular record (post).
Instead of implementing an afterSave method on my relation class (comment), I instead implemented a beforeSave method on my class (Post) and used request.object.dirtyKeys() to get my modified columns. From there I check to see if my dirty key was comments and if it is I increment my count column. It works pretty well actually.

Windows Azure Mobile Services multiple inserts, how to get the scope identity

in the insert script of one of the tables using Windows Azure Mobile Services, I want to get the id of the just inserted and use it in to insert child related data in another table (one to many style)
any idea??
Thanks in advance
Note that you also get the id of items that you insert within the server script:
var theNewRow = {
youProperty1: 'inserted',
yourProperty2: 'in server script'
};
yourTable.insert(theNewRow, {
success: function()
{
console.log('the row id' + theNewRow.id);
}
});
function insert(item, user, request) {
request.execute({
success: function() {
var id = item.id;
console.log("The new item's id is: ", id);
request.respond();
}
});
}
Carlos covers working with 1:n relationships in this post: http://blogs.msdn.com/b/carlosfigueira/archive/2012/09/11/supporting-complex-types-in-azure-mobile-services-clients-implementing-1-n-table-relationships.aspx
As an aside, you can find a a collection of Mobile Services related posts here: aka.ms/CommonWAMS

Can I switch use of 'entities.SingleOrDefault' ==> 'entities.Find' without hazards?

In my WCF service's business logic, most of the places when I need to locate an entity, I use this syntax:
public void UpdateUser(Guid userId, String notes)
{
using (ProjEntities entities = new ProjEntities())
{
User currUser = entities.SingleOrDefault(us => us.Id == userId);
if (currUser == null)
throw new Exception("User with ID " + userId + " was not found");
}
}
I have recentely discovered that the DbContext has the Find method, and I understand I can now do this:
public void UpdateUser(Guid userId, String notes)
{
using (ProjEntities entities = new ProjEntities())
{
User currUser = entities.Find(userId);
if (currUser == null)
throw new Exception("User with ID " + userId + " was not found");
}
}
Note : the 'userId' property is the primary key for the table.
I read that when using Find method entity framework checks first to see if the entity is already in the local memory, and if so - brings it from there. Otherwise - a trip is made to the database (vs. SingleOrDefault which always makes a trip to the database).
I was wondering if I now will convert all my uses of SingleOrDefault to Find is there any potential of danger?
Is there a chance I could get some old data that has not been updated if I use Find and it fetches the data from memory instead of the database?
What happens if I have the user in memory, and someone changed the user in the database - won't it be a problem if I always use now this 'memory' replica instead of always fetching the latest updated one from the database?
Is there a chance I could get some old data that has not been updated
if I use Find and it fetches the data from memory instead of the
database?
I think you have sort of answered your own question here. Yes, there is a chance that using Find you could end up having an entity returned that is out of sync with your database because your context has a local copy.
There isn't much more anyone can tell you without knowing more about your specific application; do you keep a context alive for a long time or do you open it, do your updates and close it? obviously, the longer you keep your context around the more susceptible you are to retrieving an up to date entity.
I can think of two strategies for dealing with this. The first is outlined above; open your context, do what you need and then dispose of it:
using (var ctx = new MyContext())
{
var entity = ctx.EntitySet.Find(123);
// Do something with your entity here...
ctx.SaveChanges();
}
Secondly, you could retrieve the DbEntityEntry for your entity and use the GetDatabaseValues method to update it with the values from the database. Something like this:
var entity = ctx.EntitySet.Find(123);
// This could be a cached version so ensure it is up to date.
var entry = ctx.Entry(entity);
entry.OriginalValues.SetValues(entry.GetDatabaseValues());

Resources