53 lines
1.2 KiB
JavaScript
53 lines
1.2 KiB
JavaScript
|
const path = require('path')
|
||
|
const envPath = path.resolve(process.cwd(), '.env.local')
|
||
|
|
||
|
console.log({ envPath })
|
||
|
|
||
|
require('dotenv').config({ path: envPath })
|
||
|
|
||
|
const mysql = require('serverless-mysql')
|
||
|
|
||
|
const db = mysql({
|
||
|
config: {
|
||
|
host: process.env.MYSQL_HOST,
|
||
|
database: process.env.MYSQL_DATABASE,
|
||
|
user: process.env.MYSQL_USERNAME,
|
||
|
password: process.env.MYSQL_PASSWORD,
|
||
|
},
|
||
|
})
|
||
|
|
||
|
async function query(q) {
|
||
|
try {
|
||
|
const results = await db.query(q)
|
||
|
await db.end()
|
||
|
return results
|
||
|
} catch (e) {
|
||
|
throw Error(e.message)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// Create "entries" table if doesn't exist
|
||
|
async function migrate() {
|
||
|
try {
|
||
|
await query(`
|
||
|
CREATE TABLE IF NOT EXISTS entries (
|
||
|
id INT AUTO_INCREMENT PRIMARY KEY,
|
||
|
title TEXT NOT NULL,
|
||
|
content TEXT NOT NULL,
|
||
|
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||
|
updated_at
|
||
|
TIMESTAMP
|
||
|
NOT NULL
|
||
|
DEFAULT CURRENT_TIMESTAMP
|
||
|
ON UPDATE CURRENT_TIMESTAMP
|
||
|
)
|
||
|
`)
|
||
|
console.log('migration ran successfully')
|
||
|
} catch (e) {
|
||
|
console.error('could not run migration, double check your credentials.')
|
||
|
process.exit(1)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
migrate().then(() => process.exit())
|