How to clone a record and create a new records in app maker? - google-app-maker

I have a table. I want to clone a record. I added a button and in the OnClick event I have tried
var rowDataSource = widget.datasource;
// Instead of using the row datasource for create, explicitly use the data source of your list.
var listDatasource = app.datasources.Change;
var createDataSource = listDatasource.modes.create;
createDataSource.item.Systems.SystemName = rowDataSource.item.Systems.SystemName;
createDataSource.item.changeType = rowDataSource.item.changeType;
widget.datasource.createItem();
widget.datasource.saveChanges();
But this gave me error
can't associate Data with Draft Records since I am using relational Database and my databases are in manual save mode.
How to clone all field from one record and create a new record??
Here Author name comes from the relational database and Timestamp is from the tables database.

Related

Best way for displaying total Pages for a datasource in Appmaker

I have a Google drive table data source which stores list of open positions. Now in the data source I've set "Query per size" field to 10 so that I can get 10 records per page. I've added a Pager as well to show pagination.
My query is I want to display like "Page 1 of X" to my end users and this X will vary based on certain search filters. What will the best way to achieve this in Appmaker?
I've tried counting total records in a data source as per below code but every time updating that with the search criteria and recounting it is not a proper solution.
//Server side
var newQuery = app.models.Company.newQuery();
var records = newQuery.run();
var totalCount =0;
for(var i=0;i<records.length;i++)
{
totalCount=totalCount+1;
}
return totalCount;
In case you don't have any filters in your table your server code can be as simple as
// Server script
function getPagesCount(pageSize) {
var recordsCount = app.models.MyModel.newQuery().run().length;
var pagesCount = Math.ceil(recordsCount / pageSize);
return pagesCount;
}
As an alternative you can consider creating Calculated Model with a single field PagesCount.
In case you have some filters associated with the table then you'll need to run the query for the pages number with exact same filters.
Most likely the entire setup will not work effectively with Drive Tables since there is no way to query records number without querying records themselves. With Cloud SQL data backend one can create Calculated SQL Model with lightweight native SQL query (here :PageSize is query parameter which should be equal to the query.limit of the actual datasource):
SELECT
Ceil(COUNT(1) / :PageSize) AS RecordsNumber
FROM
TableName
WHERE
...
I've achieved this using Calculated Model as suggested by Pavel.
Steps :
Create a calculated data source with one field count.
In that data source add one parameter searchQuery. This will contain users filter going forward. Currently I have only one search query in which user can search many things. So I've added one parameter only.
In this data source add following server script.
Code:
// Server script
function getTotalRecords(query) {
var receivedQuery = query.parameters.searchQuery;
// console.log('Received query:' + query.parameters.searchQuery);
var records = app.models.Company.newQuery();
records.parameters.SearchText = query.parameters.searchQuery;
if(receivedQuery !== null) {
records.where = '(Name contains? :SearchText or InternalId contains? ' +
':SearchText or LocationList contains? :SearchText )';
}
var recordsCount = records.run().length;
var calculatedModelRecords = [];
var draftRecord = app.models.RecordCount.newRecord();
draftRecord.count = ''+recordsCount;
calculatedModelRecords.push(draftRecord);
return calculatedModelRecords;
}
.
On the Appmaker page bind a label with this data source.
On search query/your filter applied event add following code which Reload this data source and assign value to Parameter.
// Client script
function updateRecordCount(newValue) {
var ds = app.datasources.RecordCount;
ds.query.parameters.searchQuery = newValue;
ds.unload();
ds.load();
}

Delete items from dynamodb based on date

I am new to dynamodb, I am able to delete an item based on key.
Below is the code snippet:
HashMap<String, AttributeValue> key = new HashMap<String, AttributeValue>();
key.put("hash", new AttributeValue().withS("hashEncodedStringValue"));
DeleteItemRequest deleteItemRequest = new DeleteItemRequest().withTableName("HashTable").withKey(key);
I need to delete items less than 7 days old from my table. And my table has a field called 'created_at' in this format "2017-10-25 14:54:52.278"
If you want dynamodb to delete it automatically after 7 days you can create TTL field and update the timestamp, dynamodb will automatically delete it.
Reference:
http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/howitworks-ttl.html
CRUD Operations with Java:
http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/JavaDocumentAPICRUDExample.html
Hope it helps.

Google App maker record key value

In App maker we enabled the manual save mode. On button click a new form will open and we will create an empty record, when user fills the fields and clicks the save button saveChanges function will save all the values.
In documentation and sample projects I can see after a record creation _key value is updated in the data source and we can use that key value to query record from its child model.
But in our case key value is not returned. But after save changes function when we open that record key value is coming, what could be the issue.
You don't have record key on the client, until you save it, since record key is generated by the server. This applies both to Auto and Manual save modes.
Here is code snippet from App Maker documentation:
var myCreateDatasource = app.datasources.MyDatasource.modes.create;
var draft = myCreateDatasource.item;
draft.Name = "Name";
draft.Age = 21;
// Create the new item
myCreateDatasource.createItem(function(newRecord) {
// Callback is called after the record is saved, so it now has a key.
var key = newRecord._key;
// Do something with the key here.
}

Updating multiple records in DynamoDB

How can i update multiple records in DynamoDB in single query?
I have a csv file as an input based on the csv file I have to update multiple records(only one attribute) in the DB.
Is there any API available for this?
or This can be done using batch processing(Spring-batch)?
DynamoDB doesn't have batchUpdate API directly. It does have batch get item and batch write item API.
However, you can use batchWriteItem API to update the item.
1) Use the below BatchWriteItemSpec class to construct the request
BatchWriteItemSpec
2) Use the below TableWriteItems class to construct the item that need to be updated
TableWriteItems
3) Use the below addItemToPut method (or withItemsToPut) to add the new attribute
addItemToPut
The batchWriteItem API creates a new item if the item (i.e. partition key) is not available. If the partition key is available, it will update the existing item. Your use case falls under this category.
Code Sample:-
files - is the the table name
fileName - is the partition key
transcriptionnew - Adding the new attribute (or can update the existing attribute value as well)
DynamoDB dynamoDB = new DynamoDB(dynamoDBClient);
Item itemUpdate1 = new Item();
itemUpdate1.withKeyComponent("fileName", "file1")
.withString("transcriptionnew", "new value");
Item itemUpdate2 = new Item();
itemUpdate2.withKeyComponent("fileName", "file2")
.withString("transcriptionnew", "new value");
TableWriteItems tableWriteItems = new TableWriteItems("files").withItemsToPut(itemUpdate1, itemUpdate2);
BatchWriteItemSpec batchWriteItemSpec = new BatchWriteItemSpec().withTableWriteItems(tableWriteItems);
BatchWriteItemOutcome batchWriteItemOutCome = dynamoDB.batchWriteItem(batchWriteItemSpec);
if (batchWriteItemOutCome.getUnprocessedItems().isEmpty()) {
//All items are processed
return true;
}

How to remove store from ydn.db?

I am using ydn.db from local storage. Which function can be used to remove the store using YDB.DB library ? The stores i have added is as follows
var schema = {
stores: [{
name:'consignments',
keyPath:"timeStamp"
}];
var db = new ydn.db.Storage('localhost', schema);
I wish to check whether the store exist in localstorage and if it exists delete the store and if not exist add the store.
If store exist or not can be known only after opening the database. If a schema does not a store name, but exit in the connected database, the existing store will be removed.
To delete the database
var db = new ydn.db.Storage('localhost', schema);
To delete above the database,
ydn.db.deleteDatabase(db.getName(), db.getType());
If type of database is not known
ydn.db.deleteDatabase('localhost');
I use the deleteDatabase function like this:
indexedDB.deleteDatabase(storage_name);
see the doc here: https://dev.yathit.com/api/ydn/db/index.html

Resources