Knex.js:创建table并插入数据
Knex.js: Create table and insert data
考虑到我有一个这样的 Knex.js 脚本:
exports.up = function(knex, Promise) {
return knex.schema.createTable('persons', function(table) {
table.increments('id').primary();
table.string('name').notNullable();
});
};
当前创建 table.
如何向该脚本添加后续插入语句?
我想要做的是添加这样一行(或类似的):
knex.insert({id: 1, name: 'Test'}).into('persons')
我不确定我是否理解这种基于承诺的方法是如何工作的。我应该用插入语句编写另一个脚本吗?或者我可以以某种方式将它们附加到我现有的脚本中吗?
不幸的是,我在 Knex.js 文档中找不到任何 完整的 创建 + 插入示例。
then方法returns一个Promise,创建[=后可以用它实现插入18=]。例如:
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")
]);
};
then 方法 returns 一个 Promise,您可以在创建 table 后使用它来实现插入。例如:
exports.up = (knex) => {
return knex.schema
.createTable("payment_paypal_status", (table) => {
table.increments()
table.string("name")
table.string("description")
})
.then(() =>
knex("payment_paypal_status").insert([
{name: "A", description: "A"},
{name: "B", description: "BB"},
{name: "C", description: "CCC"},
{name: "D", description: "DDDD"},
])
)
}
exports.down = (knex) => {
return knex.schema.dropTableIfExists("payment_paypal_status")
}
Since .createTableIfNotExists actually just generates plain "CREATE TABLE IF NOT EXIST..." query it will not work correctly if there are any alter table queries generated for columns afterwards. To not break old migrations this function is left untouched for now, but it should not be used when writing new code and it is removed from documentation.
我用了Fareed Alnamrouti example/code and follow the suggestion to discard Promise.All
by Jonny Rathbone
使用现代 Javascript 的 await/async
关键字,您可以这样做:
exports.up = async function(knex) {
await knex.schema.createTable('persons', function(table) {
table.increments('id').primary();
table.string('name').notNullable();
});
// You could replace "return" by "await" here if you wish.
return knex.insert({id: 1, name: 'Test'}).into('persons');
};
基本相同,只是用await/async
代替了then
。
考虑到我有一个这样的 Knex.js 脚本:
exports.up = function(knex, Promise) {
return knex.schema.createTable('persons', function(table) {
table.increments('id').primary();
table.string('name').notNullable();
});
};
当前创建 table.
如何向该脚本添加后续插入语句?
我想要做的是添加这样一行(或类似的):
knex.insert({id: 1, name: 'Test'}).into('persons')
我不确定我是否理解这种基于承诺的方法是如何工作的。我应该用插入语句编写另一个脚本吗?或者我可以以某种方式将它们附加到我现有的脚本中吗?
不幸的是,我在 Knex.js 文档中找不到任何 完整的 创建 + 插入示例。
then方法returns一个Promise,创建[=后可以用它实现插入18=]。例如:
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")
]);
};
then 方法 returns 一个 Promise,您可以在创建 table 后使用它来实现插入。例如:
exports.up = (knex) => {
return knex.schema
.createTable("payment_paypal_status", (table) => {
table.increments()
table.string("name")
table.string("description")
})
.then(() =>
knex("payment_paypal_status").insert([
{name: "A", description: "A"},
{name: "B", description: "BB"},
{name: "C", description: "CCC"},
{name: "D", description: "DDDD"},
])
)
}
exports.down = (knex) => {
return knex.schema.dropTableIfExists("payment_paypal_status")
}
Since .createTableIfNotExists actually just generates plain "CREATE TABLE IF NOT EXIST..." query it will not work correctly if there are any alter table queries generated for columns afterwards. To not break old migrations this function is left untouched for now, but it should not be used when writing new code and it is removed from documentation.
我用了Fareed Alnamrouti example/code and follow the suggestion to discard Promise.All
by Jonny Rathbone
使用现代 Javascript 的 await/async
关键字,您可以这样做:
exports.up = async function(knex) {
await knex.schema.createTable('persons', function(table) {
table.increments('id').primary();
table.string('name').notNullable();
});
// You could replace "return" by "await" here if you wish.
return knex.insert({id: 1, name: 'Test'}).into('persons');
};
基本相同,只是用await/async
代替了then
。