cosette/server/controller.js

36 lines
1 KiB
JavaScript
Raw Normal View History

2022-08-11 09:57:51 +02:00
import Database from 'better-sqlite3'
import { v4 } from 'uuid'
const db = new Database('./cosette.db')
function load () {
db.pragma('journal_mode = WAL')
db.exec('CREATE TABLE IF NOT EXISTS cosette (uuid TEXT PRIMARY KEY, name TEXT NOT NULL, description TEXT, tags TEXT, images TEXT)')
}
// load()
export function add (cosetta) {
const q = db.prepare('INSERT INTO cosette (uuid, name, description, tags, images) VALUES(:uuid, :name, :description, :tags, :imgs)')
cosetta.uuid = v4()
cosetta.tags = JSON.stringify(cosetta.tags)
cosetta.imgs = JSON.stringify(cosetta.imgs)
q.run(cosetta)
return cosetta
}
export function get (uuid) {
const q = db.prepare('SELECT * from cosette WHERE uuid = ?')
console.error('sono dentro cosetta e cerco ', uuid)
q.get(uuid)
}
export function getAll (limit=0, offset=0, tags=[], search='') {
const q = db.prepare('SELECT * FROM cosette')
const ret = q.all()
return ret.map(r => {
r.tags = JSON.parse(r.tags)
r.imgs = JSON.parse(r.imgs)
return r
})
}