我目前正在学习如何使用Knex,而且我无法按照正确的顺序运行我编写的函数。
当代码运行时,我期望发生的是删除表(如果存在),创建表,最后使用数据为表添加表。现在,播种功能在创建表之前运行,导致程序崩溃。
它应该在index.js中按以下顺序运行:main()
> dropTables()
> createTables()
> seedTables()
。
如何让这些功能以正确的顺序运行?
index.js
require("babel-core/register")
require("babel-polyfill")
import { makeUserTable, dropUserTable } from './models/users'
import { makeGameTable, dropGameTable } from './models/games'
import { seedUser } from './seeds/users'
import { seedGame } from './seeds/games'
const dbConnection = require('knex')({
client: 'mysql',
debug: false,
connection: {
host: 'localhost',
user: 'samurai',
password: 'bushido',
database: 'knex_sandbox'
}
})
function dropTables() {
dropUserTable(dbConnection)
dropGameTable(dbConnection)
}
function makeTables() {
makeUserTable(dbConnection)
makeGameTable(dbConnection)
}
function seedTables() {
// seedUser(dbConnection)
// seedGame(dbConnection)
console.log('seed tables ran')
}
const main = () => {
try {
dropTables()
makeTables()
seedTables()
// kill program
dbConnection.destroy()
console.log('===> Script done. Exiting.')
} catch (err) {
console.log('===> Something went wrong:\n', err)
}
}
export default main(dbConnection)
models/users.js
export const makeUserTable = (dbConnection) => {
dbConnection.schema
.createTableIfNotExists('users', function (table) {
table.increments('user_id').primary()
table.string('username', 100)
table.text('bio', 100)
})
.then(() => {
console.log('===> User table created.')
})
.catch(err => {
console.log('===> Something went wrong creating the user table:\n', err)
})
}
export const dropUserTable = (dbConnection) => {
dbConnection.schema
.dropTableIfExists('users')
.then(() => {
console.log('===> User table dropped (if it exists).')
})
.catch(err => {
console.log('===> Something went wrong dropping the user table:\n', err)
})
}
models/games.js
export const makeGameTable = (dbConnection) => {
dbConnection.schema
.createTableIfNotExists('games', function (table) {
table.increments('game_id')
.primary()
table.string('title', 100)
table.text('description', 100)
// table.integer('user_id')
// .unsigned()
// .references('users.user_id')
// .onDelete('cascade')
})
.then(() => {
console.log('===> Game table created.')
})
.catch(err => {
console.log('===> Something went wrong creating the game table:\n', err)
})
}
export const dropGameTable = (dbConnection) => {
dbConnection.schema
.dropTableIfExists('games')
.then(() => {
console.log('===> Game table dropped (if it exists).')
})
.catch(err => {
console.log('===> Something went wrong dropping the game table:\n', err)
})
}
seeds/users.js
export const seedUser = (dbConnection) => {
dbConnection
.insert({
username: 'dog',
bio: 'Hello, this is dog'
})
.into('users')
.then(() => {
console.log('===> User seeded')
})
.catch(err => {
console.log('===> Something went wrong seeding the user table:\n', err)
})
}
seeds/games.js
export const seedGame = (dbConnection) => {
dbConnection
.insert({
title: 'Bone',
description: 'Om nom nom',
// user_id: 1
})
.into('games')
.then(() => {
console.log('===> Game seeded')
})
.catch(err => {
console.log('===> Something went wrong seeding the game table:\n', err)
})
}
答案 0 :(得分:0)
除了在箭头函数中调用return dbConnection...
之外,您还应该返回创建的版本require("babel-core/register")
require("babel-polyfill")
import { makeUserTable, dropUserTable } from './models/users'
import { makeGameTable, dropGameTable } from './models/games'
import { seedUser } from './seeds/users'
import { seedGame } from './seeds/games'
const dbConnection = require('knex')({
client: 'mysql',
debug: false,
connection: {
host: 'localhost',
user: 'samurai',
password: 'bushido',
database: 'knex_sandbox'
}
})
async function dropTables() {
await dropUserTable(dbConnection)
await dropGameTable(dbConnection)
}
async function makeTables() {
await makeUserTable(dbConnection)
await makeGameTable(dbConnection)
}
async function seedTables() {
// seedUser(dbConnection)
// seedGame(dbConnection)
console.log('seed tables ran')
}
const main = async () => {
try {
await dropTables()
await makeTables()
await seedTables()
// kill program
await dbConnection.destroy()
console.log('===> Script done. Exiting.')
} catch (err) {
console.log('===> Something went wrong:\n', err)
}
}
,并在调用之前等待。
#header {
...
/* remove margin-top: 10px */
padding-top: 10px; /* add */
...
}
#header { /* header extra code */
position: fixed; /* fix it */
top: 0; left: 0; /* to top/left */
z-index: 1; /* above content */
width: 100%; /* at full parent width (=> .container) */
background: rgba(255,255,255,.9); /* hide 'scrolling under' content */
/* with a bit transparency to see it scroll */
}
/* new line, move content down and a nice space below header */
.container>.row:nth-child(2) {
padding-top: 6rem;
}
* {outline: 1px dashed } /* debug helper, remove when done */
/* so you can see what your element boxes look like*/