This repository has been archived on 2024-04-29. You can view files and clone it, but cannot push or open issues or pull requests.
node-fftcg/inc/fftcgdb.coffee

44 lines
989 B
CoffeeScript
Raw Normal View History

2018-12-07 09:38:46 +00:00
# libraries
bcrypt = (require 'bcrypt')
sqlite3 = (require 'sqlite3').verbose()
FFTCGDB =
open: (filename) ->
@filename = filename
@db = new sqlite3.Database @filename, (err) ->
if err
console.error err.message
console.log 'Connected to', @filename
@db.run """
CREATE TABLE IF NOT EXISTS users (
nick text NOT NULL,
pwd text NOT NULL,
socket text
);
"""
close: ->
db.close (err) ->
if err
console.error err.message
console.log 'Closed', @filename
adduser: (name, password) ->
saltRounds = 13
that = @
bcrypt.hash password, saltRounds, (err, hash) ->
2018-12-13 17:48:55 +00:00
that.db.run "INSERT INTO users (nick, pwd) VALUES ('#{name}', '#{hash}');", (err, result) ->
2018-12-07 09:38:46 +00:00
if err
console.error err.message
console.log 'Result:', result
module.exports = FFTCGDB
# Handle termination
process.on 'SIGINT', ->
console.log '[FFTCG-DB] shutting down'
FFTCGDB.close()