Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom id value instead of number #7

Closed
ralphilius opened this issue Aug 29, 2018 · 9 comments
Closed

Custom id value instead of number #7

ralphilius opened this issue Aug 29, 2018 · 9 comments

Comments

@ralphilius
Copy link

ralphilius commented Aug 29, 2018

Is there a way that we can use our own ID value instead of auto-increment value? I'm saving data from an API which also having id from its own system.

@itmammoth
Copy link
Owner

Can I have a look at the data that you are trying to save on your spreadsheet?

@itmammoth
Copy link
Owner

Maybe idColumn property can help you.
https://github.com/itmammoth/Tamotsu#prepared-classproperties

@ralphilius
Copy link
Author

Hi @itmammoth

Please see below JSON:

{
   "listing_id": 232435,
   "title": "Some title here",
   "state": "active",
   "description": "Some very long description"
}

I've tried to use idColumn but I can't override new listing_id value into that field.

@itmammoth
Copy link
Owner

Sheet name: example

listing_id title state description
232435 Some title here active Some very long description

Run the code below,

  Tamotsu.initialize();
  var Example = Tamotsu.Table.define({
    sheetName: 'example',
    idColumn: 'listing_id',
  });
  var e1 = Example.first();
  Logger.log(e1); //=> {row_=2.0, listing_id=232435.0, description=Some very long description, state=active, title=Some title here}
  var e2 = Example.create({ title: 'Second title', state: 'active', description: 'Second description' });
  Logger.log(e2); //=> {row_=3.0, listing_id=232436.0, description=Second description, state=active, title=Second title, errors={}}

What you can get is

listing_id title state description
232435 Some title here active Some very long description
232436 Second title active Second description

It looks perfect to me.

@ralphilius
Copy link
Author

ralphilius commented Sep 7, 2018

Actually, the listing_id was incremented. As your example, the create code can possibly be

var e2 = Example.create({listing_id:12345, title: 'Second title', state: 'active', description: 'Second description' });

This is because the table is used to store data from an online database.

I think what I really need is a way to easily use some kind of createOrUpdate function based on a specific unique column. This function is based on below operations:

  1. Look up for existing value based on a column name
  2. Update the row if there's any record
  3. Or create the row if there's none

My thought when I created this issue is that I could utilize Table.find(id) to quickly lookup for duplicate so that I could perform a createOrUpdate decision. Also, some kind of string-based idColumn

@itmammoth
Copy link
Owner

Thanks @ralphilius
I understand it quite well.
I think there are 2 issues to be solved in this library.

  1. Given id value is always ignored on create
  2. There is no way to use string-based idColumn

And I will give an easy way to duplicate model instance as an enhancement.

@ralphilius
Copy link
Author

Thanks @itmammoth, you have expressed exact issues I have encountered.

@itmammoth
Copy link
Owner

I'm done with this.

@ralphilius
Copy link
Author

Great! Thanks a lot for your hard work

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants