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

Enable string functions and arrays as default column values #82

Merged
merged 2 commits into from
May 10, 2017

Conversation

arthurfranca
Copy link
Contributor

No description provided.

@dolezel
Copy link
Contributor

dolezel commented May 5, 2017

Sorry, but I do not quite understand purpose of this PR.
Are you trying to something like:

pgm.createTable('t', {
  a1: { type: 'integer[]', default: pgm.func("'{1, 2, 3}'") },
  a2: { type: 'integer[]', default: pgm.func('ARRAY[1, 2, 3]') },
});

?

@arthurfranca
Copy link
Contributor Author

The purpose of the 2 commits is to enable these two defaults (without using pgm.func):

pgm.createTable('table', {
  id: { type: 'uuid', default: 'gen_random_uuid()' },
  table_2_ids: { type: uuid[]', default: [] }
})

Notice that the array isn't a string, but an array literal
It's following postgresql documentation and converting [1,2] to '{1,2}'

@dolezel
Copy link
Contributor

dolezel commented May 9, 2017

But that is the purpose of pgm.func. Escaping and unescaping string is worse option. It will introduce new bugs when it will unescape strings that should not be unescaped.
If you want some string that should not be escaped, just use pgm.func.

@dolezel dolezel closed this May 9, 2017
@arthurfranca
Copy link
Contributor Author

arthurfranca commented May 9, 2017

@dolezel what about the array literal as default? I don't see a problem as there's special treatment to booleans and nulls. I mean, you can say default: null and default: true instead of 'NULL' and 'true' which are expected by postgres.

It seems better using [1, 2, 3] instead of pgm.func('ARRAY[1, 2, 3]') as with null instead of 'NULL'.

Also, the added tests were discarded =0.

@dolezel
Copy link
Contributor

dolezel commented May 10, 2017

Yes, that is a good idea, so please change your code to
if (Array.isArray(val)) return `ARRAY[${val.map(escapeValue).join(',')}]`;
and keep relevant test.
Thanks!

@dolezel dolezel reopened this May 10, 2017
@arthurfranca
Copy link
Contributor Author

@dolezel done. Used your code with a small addition to allow multidimensional arrays, so that the added test pass.

Copy link
Contributor

@dolezel dolezel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@dolezel dolezel merged commit e61f661 into salsita:master May 10, 2017
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

Successfully merging this pull request may close these issues.

2 participants