Knex.js: Create table and insert data
Given that I have a Knex.js script like this:
exports.up = function(knex, Promise) {
return knex.schema.createTable('persons', function(table) {
table.increments('id').primary();
table.string('name').notNullable();
});
};
which currently creates a table.
How do I add subsequent insert statements to this script?
What I want to do is add a line like this (or similar):
knex.insert({id: 1, name: 'Test'}).into('persons')
I'm not sure I understand how this promise-based approach works. Am I supposed to write another script with insert statements? Or can I somehow append them to my existing script?
Unfortunately, I don't find any complete example of create + insert in Knex.js documentation.
node.js knex.js
add a comment |
Given that I have a Knex.js script like this:
exports.up = function(knex, Promise) {
return knex.schema.createTable('persons', function(table) {
table.increments('id').primary();
table.string('name').notNullable();
});
};
which currently creates a table.
How do I add subsequent insert statements to this script?
What I want to do is add a line like this (or similar):
knex.insert({id: 1, name: 'Test'}).into('persons')
I'm not sure I understand how this promise-based approach works. Am I supposed to write another script with insert statements? Or can I somehow append them to my existing script?
Unfortunately, I don't find any complete example of create + insert in Knex.js documentation.
node.js knex.js
add a comment |
Given that I have a Knex.js script like this:
exports.up = function(knex, Promise) {
return knex.schema.createTable('persons', function(table) {
table.increments('id').primary();
table.string('name').notNullable();
});
};
which currently creates a table.
How do I add subsequent insert statements to this script?
What I want to do is add a line like this (or similar):
knex.insert({id: 1, name: 'Test'}).into('persons')
I'm not sure I understand how this promise-based approach works. Am I supposed to write another script with insert statements? Or can I somehow append them to my existing script?
Unfortunately, I don't find any complete example of create + insert in Knex.js documentation.
node.js knex.js
Given that I have a Knex.js script like this:
exports.up = function(knex, Promise) {
return knex.schema.createTable('persons', function(table) {
table.increments('id').primary();
table.string('name').notNullable();
});
};
which currently creates a table.
How do I add subsequent insert statements to this script?
What I want to do is add a line like this (or similar):
knex.insert({id: 1, name: 'Test'}).into('persons')
I'm not sure I understand how this promise-based approach works. Am I supposed to write another script with insert statements? Or can I somehow append them to my existing script?
Unfortunately, I don't find any complete example of create + insert in Knex.js documentation.
node.js knex.js
node.js knex.js
asked Jan 29 '16 at 16:38
SputNick
5914820
5914820
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
why not using Promise then method where you can do for example:
exports.up = function (knex, Promise) {
return Promise.all([
knex.schema.createTableIfNotExists("payment_paypal_status", function (table) {
table.increments(); // integer id
// name
table.string('name');
//description
table.string('description');
}).then(function () {
return knex("payment_paypal_status").insert([
{name: "A", description: "A"},
{name: "B", description: "BB"},
{name: "C", description: "CCC"},
{name: "D", description: "DDDD"}
]);
}
),
]);
};
exports.down = function (knex, Promise) {
return Promise.all([
knex.schema.dropTableIfExists("payment_paypal_status")
]);
};
5
Thank you, this makes much more sense than the answer I came up with. Lack of experience with Promises got in my way here.
– SputNick
Feb 12 '16 at 13:22
Hello. New to knex and Bookshelf. Just curious to know why use up and down for method names, I mean how are they used later in the code
– j10
Jul 18 '17 at 11:05
1
These methods used to upgrade or downgrade database scheme (structure) usually you don't use these functions from your code you use and create these files from the terminal command line, Knex library try to make it more safe for you to change your database structure specially when you want to reflect your database structure changes from local or development database to your production database every time you upgrade your database the up function will run and the version of the current database will save inside a special database table called migrate and the opposite when you downgrade
– Fareed Alnamrouti
Jul 18 '17 at 11:24
add a comment |
OK, now I understand.
edit: No, I didn't. See fareed namrouti's answer for the real solution.
Knex seems to distinguish between "schema migration scripts" and "regular SQL queries", the latter including INSERT statements.
- schema migration scripts are organized in dedicated migration script files and loaded with e.g.
knex.migrate.latest()
- regular statements are called from any Node script
Hence, I must place INSERT statements in a regular Node script, not within the migration script's exports.up function.
Still, I cannot see this being clearly pointed out in the documentation.
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%2f35089571%2fknex-js-create-table-and-insert-data%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
why not using Promise then method where you can do for example:
exports.up = function (knex, Promise) {
return Promise.all([
knex.schema.createTableIfNotExists("payment_paypal_status", function (table) {
table.increments(); // integer id
// name
table.string('name');
//description
table.string('description');
}).then(function () {
return knex("payment_paypal_status").insert([
{name: "A", description: "A"},
{name: "B", description: "BB"},
{name: "C", description: "CCC"},
{name: "D", description: "DDDD"}
]);
}
),
]);
};
exports.down = function (knex, Promise) {
return Promise.all([
knex.schema.dropTableIfExists("payment_paypal_status")
]);
};
5
Thank you, this makes much more sense than the answer I came up with. Lack of experience with Promises got in my way here.
– SputNick
Feb 12 '16 at 13:22
Hello. New to knex and Bookshelf. Just curious to know why use up and down for method names, I mean how are they used later in the code
– j10
Jul 18 '17 at 11:05
1
These methods used to upgrade or downgrade database scheme (structure) usually you don't use these functions from your code you use and create these files from the terminal command line, Knex library try to make it more safe for you to change your database structure specially when you want to reflect your database structure changes from local or development database to your production database every time you upgrade your database the up function will run and the version of the current database will save inside a special database table called migrate and the opposite when you downgrade
– Fareed Alnamrouti
Jul 18 '17 at 11:24
add a comment |
why not using Promise then method where you can do for example:
exports.up = function (knex, Promise) {
return Promise.all([
knex.schema.createTableIfNotExists("payment_paypal_status", function (table) {
table.increments(); // integer id
// name
table.string('name');
//description
table.string('description');
}).then(function () {
return knex("payment_paypal_status").insert([
{name: "A", description: "A"},
{name: "B", description: "BB"},
{name: "C", description: "CCC"},
{name: "D", description: "DDDD"}
]);
}
),
]);
};
exports.down = function (knex, Promise) {
return Promise.all([
knex.schema.dropTableIfExists("payment_paypal_status")
]);
};
5
Thank you, this makes much more sense than the answer I came up with. Lack of experience with Promises got in my way here.
– SputNick
Feb 12 '16 at 13:22
Hello. New to knex and Bookshelf. Just curious to know why use up and down for method names, I mean how are they used later in the code
– j10
Jul 18 '17 at 11:05
1
These methods used to upgrade or downgrade database scheme (structure) usually you don't use these functions from your code you use and create these files from the terminal command line, Knex library try to make it more safe for you to change your database structure specially when you want to reflect your database structure changes from local or development database to your production database every time you upgrade your database the up function will run and the version of the current database will save inside a special database table called migrate and the opposite when you downgrade
– Fareed Alnamrouti
Jul 18 '17 at 11:24
add a comment |
why not using Promise then method where you can do for example:
exports.up = function (knex, Promise) {
return Promise.all([
knex.schema.createTableIfNotExists("payment_paypal_status", function (table) {
table.increments(); // integer id
// name
table.string('name');
//description
table.string('description');
}).then(function () {
return knex("payment_paypal_status").insert([
{name: "A", description: "A"},
{name: "B", description: "BB"},
{name: "C", description: "CCC"},
{name: "D", description: "DDDD"}
]);
}
),
]);
};
exports.down = function (knex, Promise) {
return Promise.all([
knex.schema.dropTableIfExists("payment_paypal_status")
]);
};
why not using Promise then method where you can do for example:
exports.up = function (knex, Promise) {
return Promise.all([
knex.schema.createTableIfNotExists("payment_paypal_status", function (table) {
table.increments(); // integer id
// name
table.string('name');
//description
table.string('description');
}).then(function () {
return knex("payment_paypal_status").insert([
{name: "A", description: "A"},
{name: "B", description: "BB"},
{name: "C", description: "CCC"},
{name: "D", description: "DDDD"}
]);
}
),
]);
};
exports.down = function (knex, Promise) {
return Promise.all([
knex.schema.dropTableIfExists("payment_paypal_status")
]);
};
answered Feb 12 '16 at 6:44
Fareed Alnamrouti
19.7k26255
19.7k26255
5
Thank you, this makes much more sense than the answer I came up with. Lack of experience with Promises got in my way here.
– SputNick
Feb 12 '16 at 13:22
Hello. New to knex and Bookshelf. Just curious to know why use up and down for method names, I mean how are they used later in the code
– j10
Jul 18 '17 at 11:05
1
These methods used to upgrade or downgrade database scheme (structure) usually you don't use these functions from your code you use and create these files from the terminal command line, Knex library try to make it more safe for you to change your database structure specially when you want to reflect your database structure changes from local or development database to your production database every time you upgrade your database the up function will run and the version of the current database will save inside a special database table called migrate and the opposite when you downgrade
– Fareed Alnamrouti
Jul 18 '17 at 11:24
add a comment |
5
Thank you, this makes much more sense than the answer I came up with. Lack of experience with Promises got in my way here.
– SputNick
Feb 12 '16 at 13:22
Hello. New to knex and Bookshelf. Just curious to know why use up and down for method names, I mean how are they used later in the code
– j10
Jul 18 '17 at 11:05
1
These methods used to upgrade or downgrade database scheme (structure) usually you don't use these functions from your code you use and create these files from the terminal command line, Knex library try to make it more safe for you to change your database structure specially when you want to reflect your database structure changes from local or development database to your production database every time you upgrade your database the up function will run and the version of the current database will save inside a special database table called migrate and the opposite when you downgrade
– Fareed Alnamrouti
Jul 18 '17 at 11:24
5
5
Thank you, this makes much more sense than the answer I came up with. Lack of experience with Promises got in my way here.
– SputNick
Feb 12 '16 at 13:22
Thank you, this makes much more sense than the answer I came up with. Lack of experience with Promises got in my way here.
– SputNick
Feb 12 '16 at 13:22
Hello. New to knex and Bookshelf. Just curious to know why use up and down for method names, I mean how are they used later in the code
– j10
Jul 18 '17 at 11:05
Hello. New to knex and Bookshelf. Just curious to know why use up and down for method names, I mean how are they used later in the code
– j10
Jul 18 '17 at 11:05
1
1
These methods used to upgrade or downgrade database scheme (structure) usually you don't use these functions from your code you use and create these files from the terminal command line, Knex library try to make it more safe for you to change your database structure specially when you want to reflect your database structure changes from local or development database to your production database every time you upgrade your database the up function will run and the version of the current database will save inside a special database table called migrate and the opposite when you downgrade
– Fareed Alnamrouti
Jul 18 '17 at 11:24
These methods used to upgrade or downgrade database scheme (structure) usually you don't use these functions from your code you use and create these files from the terminal command line, Knex library try to make it more safe for you to change your database structure specially when you want to reflect your database structure changes from local or development database to your production database every time you upgrade your database the up function will run and the version of the current database will save inside a special database table called migrate and the opposite when you downgrade
– Fareed Alnamrouti
Jul 18 '17 at 11:24
add a comment |
OK, now I understand.
edit: No, I didn't. See fareed namrouti's answer for the real solution.
Knex seems to distinguish between "schema migration scripts" and "regular SQL queries", the latter including INSERT statements.
- schema migration scripts are organized in dedicated migration script files and loaded with e.g.
knex.migrate.latest()
- regular statements are called from any Node script
Hence, I must place INSERT statements in a regular Node script, not within the migration script's exports.up function.
Still, I cannot see this being clearly pointed out in the documentation.
add a comment |
OK, now I understand.
edit: No, I didn't. See fareed namrouti's answer for the real solution.
Knex seems to distinguish between "schema migration scripts" and "regular SQL queries", the latter including INSERT statements.
- schema migration scripts are organized in dedicated migration script files and loaded with e.g.
knex.migrate.latest()
- regular statements are called from any Node script
Hence, I must place INSERT statements in a regular Node script, not within the migration script's exports.up function.
Still, I cannot see this being clearly pointed out in the documentation.
add a comment |
OK, now I understand.
edit: No, I didn't. See fareed namrouti's answer for the real solution.
Knex seems to distinguish between "schema migration scripts" and "regular SQL queries", the latter including INSERT statements.
- schema migration scripts are organized in dedicated migration script files and loaded with e.g.
knex.migrate.latest()
- regular statements are called from any Node script
Hence, I must place INSERT statements in a regular Node script, not within the migration script's exports.up function.
Still, I cannot see this being clearly pointed out in the documentation.
OK, now I understand.
edit: No, I didn't. See fareed namrouti's answer for the real solution.
Knex seems to distinguish between "schema migration scripts" and "regular SQL queries", the latter including INSERT statements.
- schema migration scripts are organized in dedicated migration script files and loaded with e.g.
knex.migrate.latest()
- regular statements are called from any Node script
Hence, I must place INSERT statements in a regular Node script, not within the migration script's exports.up function.
Still, I cannot see this being clearly pointed out in the documentation.
edited May 23 '17 at 12:02
Community♦
11
11
answered Jan 31 '16 at 10:51
SputNick
5914820
5914820
add a comment |
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%2f35089571%2fknex-js-create-table-and-insert-data%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