snipette-crowdfunding-site/server/index.js
2021-12-29 17:05:51 +05:30

54 lines
1.2 KiB
JavaScript

require('dotenv').config()
let DEBUG
if (process.env.DEBUG || process.env.CROWDFUNDING_SITE_DEBUG) {
DEBUG = true
} else {
DEBUG = false
}
if (DEBUG) console.log('Starting website in debug mode')
// set up knex (for database)
const knex = require('knex')({
client:'sqlite3',
connection:{
filename:'./donors.sqlite'
},
useNullAsDefault: true
})
knex.schema.hasTable('pledges').then(function(exists) {
if (!exists) {
if (DEBUG) console.debug('No table exists! Creating one now...')
return knex.schema.createTable('pledges', function(t) {
t.increments('id').primary()
t.timestamp('created_at').defaultTo(knex.fn.now()).notNullable()
t.boolean('was_robot').defaultTo(true)
t.integer('amount').notNullable()
t.string('name', 128)
t.boolean('anonymous')
t.string('email', 128)
t.string('phone', 32)
t.integer('retry_times').defaultTo(10)
t.boolean('get_newsletter').defaultTo(false)
t.text('other_message')
})
}
})
// set up bookshelf (for easy interface for database)
bookshelf = require('bookshelf')(knex)
const Pledge = bookshelf.model('Pledge', {
tableName: 'pledges',
})
module.exports = {
knex,
bookshelf,
Pledge,
}