How to insert an object into postgreSQL db using knex.raw() method?
I am looking for a way to insert an object into postgreSQL
db using knex.raw() method.
Here is my try, it works, but I don't know if it is a best way.
const newLink = {
url: 'http://github.com',
name: 'GitHub',
description: 'GitHub is awesome',
rel: '',
last_update: new Date()
};
const columns = Object.keys(newLink);
const values = Object.values(newLink);
console.log('columns: ', columns);
console.log('values: ', values);
const sql = `
INSERT INTO link (${columns.map(col => col).join(',')})
VALUES (${values.map(() => '?').join(',')})
`;
console.log('sql: ', sql);
try {
const insertObjResp = await knex.raw(sql, values);
console.log('insertObjResp: ', insertObjResp);
} catch (error) {
console.error(error);
}
knex
give me debug information like this:
{ method: 'raw',
sql: 'n INSERT INTO link (url,name,description,rel,last_update)n VALUES (?,?,?,?,?)n ',
bindings:
[ 'http://github.com',
'GitHub',
'GitHub is awesome',
'',
2018-11-20T04:47:21.085Z ],
options: {},
__knexQueryUid: '304fb5e5-9864-4ec6-9a63-1051e8e3c9ad' }
Is there an knex.js
official way to do this work? thanks.
postgresql knex.js
add a comment |
I am looking for a way to insert an object into postgreSQL
db using knex.raw() method.
Here is my try, it works, but I don't know if it is a best way.
const newLink = {
url: 'http://github.com',
name: 'GitHub',
description: 'GitHub is awesome',
rel: '',
last_update: new Date()
};
const columns = Object.keys(newLink);
const values = Object.values(newLink);
console.log('columns: ', columns);
console.log('values: ', values);
const sql = `
INSERT INTO link (${columns.map(col => col).join(',')})
VALUES (${values.map(() => '?').join(',')})
`;
console.log('sql: ', sql);
try {
const insertObjResp = await knex.raw(sql, values);
console.log('insertObjResp: ', insertObjResp);
} catch (error) {
console.error(error);
}
knex
give me debug information like this:
{ method: 'raw',
sql: 'n INSERT INTO link (url,name,description,rel,last_update)n VALUES (?,?,?,?,?)n ',
bindings:
[ 'http://github.com',
'GitHub',
'GitHub is awesome',
'',
2018-11-20T04:47:21.085Z ],
options: {},
__knexQueryUid: '304fb5e5-9864-4ec6-9a63-1051e8e3c9ad' }
Is there an knex.js
official way to do this work? thanks.
postgresql knex.js
add a comment |
I am looking for a way to insert an object into postgreSQL
db using knex.raw() method.
Here is my try, it works, but I don't know if it is a best way.
const newLink = {
url: 'http://github.com',
name: 'GitHub',
description: 'GitHub is awesome',
rel: '',
last_update: new Date()
};
const columns = Object.keys(newLink);
const values = Object.values(newLink);
console.log('columns: ', columns);
console.log('values: ', values);
const sql = `
INSERT INTO link (${columns.map(col => col).join(',')})
VALUES (${values.map(() => '?').join(',')})
`;
console.log('sql: ', sql);
try {
const insertObjResp = await knex.raw(sql, values);
console.log('insertObjResp: ', insertObjResp);
} catch (error) {
console.error(error);
}
knex
give me debug information like this:
{ method: 'raw',
sql: 'n INSERT INTO link (url,name,description,rel,last_update)n VALUES (?,?,?,?,?)n ',
bindings:
[ 'http://github.com',
'GitHub',
'GitHub is awesome',
'',
2018-11-20T04:47:21.085Z ],
options: {},
__knexQueryUid: '304fb5e5-9864-4ec6-9a63-1051e8e3c9ad' }
Is there an knex.js
official way to do this work? thanks.
postgresql knex.js
I am looking for a way to insert an object into postgreSQL
db using knex.raw() method.
Here is my try, it works, but I don't know if it is a best way.
const newLink = {
url: 'http://github.com',
name: 'GitHub',
description: 'GitHub is awesome',
rel: '',
last_update: new Date()
};
const columns = Object.keys(newLink);
const values = Object.values(newLink);
console.log('columns: ', columns);
console.log('values: ', values);
const sql = `
INSERT INTO link (${columns.map(col => col).join(',')})
VALUES (${values.map(() => '?').join(',')})
`;
console.log('sql: ', sql);
try {
const insertObjResp = await knex.raw(sql, values);
console.log('insertObjResp: ', insertObjResp);
} catch (error) {
console.error(error);
}
knex
give me debug information like this:
{ method: 'raw',
sql: 'n INSERT INTO link (url,name,description,rel,last_update)n VALUES (?,?,?,?,?)n ',
bindings:
[ 'http://github.com',
'GitHub',
'GitHub is awesome',
'',
2018-11-20T04:47:21.085Z ],
options: {},
__knexQueryUid: '304fb5e5-9864-4ec6-9a63-1051e8e3c9ad' }
Is there an knex.js
official way to do this work? thanks.
postgresql knex.js
postgresql knex.js
edited Dec 6 at 4:46
asked Nov 20 at 4:53
slideshowp2
2,41712462
2,41712462
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
Is there an knex.js official way to do this work? thanks.
Official way is to use knex.insert()
.
Your code seems correct and there is no better way. Arrays of values and knex.raw
is a bit inconvenient to use. Of course you should wrap that monster inside a helper function at least.
Thanks anyway. I prefer use rawSQL
rather than knex.js api. So I am looking for a rawSQL
way to insert an object data directly.
– slideshowp2
Nov 21 at 2:01
In that case it makes not much sense to use knex and re-implement all query building routines by yourself that knex has already implemented. It will be more powerful to use the db driver directly without knex in your case.
– Mikael Lepistö
Nov 22 at 5:05
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53386436%2fhow-to-insert-an-object-into-postgresql-db-using-knex-raw-method%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Is there an knex.js official way to do this work? thanks.
Official way is to use knex.insert()
.
Your code seems correct and there is no better way. Arrays of values and knex.raw
is a bit inconvenient to use. Of course you should wrap that monster inside a helper function at least.
Thanks anyway. I prefer use rawSQL
rather than knex.js api. So I am looking for a rawSQL
way to insert an object data directly.
– slideshowp2
Nov 21 at 2:01
In that case it makes not much sense to use knex and re-implement all query building routines by yourself that knex has already implemented. It will be more powerful to use the db driver directly without knex in your case.
– Mikael Lepistö
Nov 22 at 5:05
add a comment |
Is there an knex.js official way to do this work? thanks.
Official way is to use knex.insert()
.
Your code seems correct and there is no better way. Arrays of values and knex.raw
is a bit inconvenient to use. Of course you should wrap that monster inside a helper function at least.
Thanks anyway. I prefer use rawSQL
rather than knex.js api. So I am looking for a rawSQL
way to insert an object data directly.
– slideshowp2
Nov 21 at 2:01
In that case it makes not much sense to use knex and re-implement all query building routines by yourself that knex has already implemented. It will be more powerful to use the db driver directly without knex in your case.
– Mikael Lepistö
Nov 22 at 5:05
add a comment |
Is there an knex.js official way to do this work? thanks.
Official way is to use knex.insert()
.
Your code seems correct and there is no better way. Arrays of values and knex.raw
is a bit inconvenient to use. Of course you should wrap that monster inside a helper function at least.
Is there an knex.js official way to do this work? thanks.
Official way is to use knex.insert()
.
Your code seems correct and there is no better way. Arrays of values and knex.raw
is a bit inconvenient to use. Of course you should wrap that monster inside a helper function at least.
answered Nov 20 at 9:53
Mikael Lepistö
6,76212728
6,76212728
Thanks anyway. I prefer use rawSQL
rather than knex.js api. So I am looking for a rawSQL
way to insert an object data directly.
– slideshowp2
Nov 21 at 2:01
In that case it makes not much sense to use knex and re-implement all query building routines by yourself that knex has already implemented. It will be more powerful to use the db driver directly without knex in your case.
– Mikael Lepistö
Nov 22 at 5:05
add a comment |
Thanks anyway. I prefer use rawSQL
rather than knex.js api. So I am looking for a rawSQL
way to insert an object data directly.
– slideshowp2
Nov 21 at 2:01
In that case it makes not much sense to use knex and re-implement all query building routines by yourself that knex has already implemented. It will be more powerful to use the db driver directly without knex in your case.
– Mikael Lepistö
Nov 22 at 5:05
Thanks anyway. I prefer use raw
SQL
rather than knex.js api. So I am looking for a raw SQL
way to insert an object data directly.– slideshowp2
Nov 21 at 2:01
Thanks anyway. I prefer use raw
SQL
rather than knex.js api. So I am looking for a raw SQL
way to insert an object data directly.– slideshowp2
Nov 21 at 2:01
In that case it makes not much sense to use knex and re-implement all query building routines by yourself that knex has already implemented. It will be more powerful to use the db driver directly without knex in your case.
– Mikael Lepistö
Nov 22 at 5:05
In that case it makes not much sense to use knex and re-implement all query building routines by yourself that knex has already implemented. It will be more powerful to use the db driver directly without knex in your case.
– Mikael Lepistö
Nov 22 at 5:05
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53386436%2fhow-to-insert-an-object-into-postgresql-db-using-knex-raw-method%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown