Create or update database from array items

This forum contains topics that were moved from Get Satisfaction
Mark7294305
Posts: 0
Joined: Fri Dec 26, 2014 2:10 am

Create or update database from array items

Post by Mark7294305 »

Hello Support
I have an App where I wish to import clothing items into a database. When the clothing stock has arrived to the clothing store, the user will scan the barcode on the delivery receipt and details of the delivery is displayed on the app. For example

Item Qty Stock No
Levi Jeans 30 4528
Nike 15 4521
Adidas 14 3526

My app is working so far where I created an array for the items and imported them into the database. The problem that I am having is all items imported are being duplicated where the item already exist in the database. So for example if there is already an entry for Nike and the current quantity is 3, when I import the new stock you will see 2 entries for Nike. Both displaying the same stock No, one with 3 items and the other for 15 whereby we need only need one entry displaying 18 items. This happens if I use the create service. When I use the update service to import the new if the first item on the import is a new item and not already existing in the database, then none of the items to be imported are imported (updated or created) on the database.
So what I need help on is if an array item exists on the database the quantity is updated on the existing item on the database. If the array item is new then a new entry is created on the database.

Can you help?

Mark7294305
Posts: 0
Joined: Fri Dec 26, 2014 2:10 am

Create or update database from array items

Post by Mark7294305 »

Item, Qty, Stock No
Levi Jeans, 30, 4528
Nike, 15, 4521
Adidas, 14, 3526

Serhii Kulibaba
Posts: 147
Joined: Tue Aug 27, 2013 1:47 pm

Create or update database from array items

Post by Serhii Kulibaba »

Hello Mark,

Please use create, update and read services for every action

Mark7294305
Posts: 0
Joined: Fri Dec 26, 2014 2:10 am

Create or update database from array items

Post by Mark7294305 »

Hi Sergiy
Thanks for your reply. Can you give some example instructions?

Mark7294305
Posts: 0
Joined: Fri Dec 26, 2014 2:10 am

Create or update database from array items

Post by Mark7294305 »

So I have added screen shots of my current project. The database has current stock. The App is showing all receiving items. The Addidas item need to create a new entry in the database and the other items need to update their entry in the database. I have configured an Array to contain the new stock but I don't know how to configure the app to choose each item on the receiving new stock, query the database and detect if the item is to be updated or created then execute the update or create the item Image Image

Alena Prykhodko
Posts: 0
Joined: Tue Apr 09, 2013 7:36 am

Create or update database from array items

Post by Alena Prykhodko »

Hello Mark,

[quote:]query the database and detect if the item is to be updated or created then execute the update or create the item [/quote]

Yes.

Mark7294305
Posts: 0
Joined: Fri Dec 26, 2014 2:10 am

Create or update database from array items

Post by Mark7294305 »

Hi Alena
Sorry what I am asking is could you give me the steps on how to query the database and detect if the item is to be updated or created then execute the update or create the item with the items that are stored in the array

Mark7294305
Posts: 0
Joined: Fri Dec 26, 2014 2:10 am

Create or update database from array items

Post by Mark7294305 »

I searched through the doc but it doesn't help me.

Just to re explain what I need.

This how the app is currently configured. The app connects to a rest service which stores a invoice copy of all of our deliveries. The invoice containing all products is mapping to a storage model array which is then mapped to a list on the app page

Let’s say one product is received in the invoice. The user presses a button to upload the product to database. The first process is that a query service runs to the database to check if the product exist on the database. If it does exist then the update service runs and the qty is updated on the database item. If the product does not exist on the database then the create service runs adding the new item to the database.
The problem I am facing is if I receive multiple (let’s say 10 for example) different products in a single delivery invoice. If all (10) products already exist on the database the update service runs and the products qty is updated on all (10) products. If all (10) products do not exist on the database the create service runs and all (10) products are added to the database. However if (Let’s say 6) products already exist on the database and (Let’s say 4) products are new, If the first item on the list does not exist on the database it will treat the (9) other product as new and run the create service causing duplicates to the existing products on the database. If the first item on the list already exist on the database it will treat the (9) other product as new and run the update service but will stop when it tries to update a product which does not exist on the database.

So in summary this is what is currently happening

When the user presses the upload button

Product 1 runs the query service, detects the first product as to be updated, treats all other products as “to be updated” runs the update service on all products and stops when processing a product which does not exist in the database

Or

When the user presses the upload button

Product 1 runs the query service, detects the product as new, treats all other products as “new products”, runs the create service on all products, creates duplicate entries of products which already exist in the database

what I’m looking is when the user presses the upload button,

Product 1 runs the query service, then the create/update service (which ever applies at the time)
product 2 runs the query service, then the create/update service (which ever applies at the time)
product 3 runs the query service, then the create/update service (which ever applies at the time)

Yurii Orishchuk
Posts: 0
Joined: Fri Feb 14, 2014 8:20 am

Create or update database from array items

Post by Yurii Orishchuk »

Hi Mark,

The common solution to add add items only if there is no such items in your db. And update item instead:

  1. Get items with "list/query" service and see count of these items.
    1.1. You should specify "where" request parameter to see whether you have in database such items(that satisfy your business logic condition).

  2. If you received more then 0 items(1 or more) - it means you have this item in DB.
    Thus you need to invoke "update" service.(_id for update service you can find in response of "list/query" service).

  3. If you received 0 items. That means you don't have such items in your db. And you can use "create" service to add new item.

    Regards.

Post Reply