2019-08-16 12:00:50 +03:00
|
|
|
import {log} from "../node_modules/assemblyscript-sdk/assembly/logger";
|
|
|
|
import {query} from "../node_modules/db-connector/assembly/sqlite"
|
|
|
|
|
|
|
|
// main handler for an application
|
|
|
|
export function handler(username: string): string {
|
2019-08-16 16:49:07 +03:00
|
|
|
// Create table for messages storage
|
2019-08-16 12:00:50 +03:00
|
|
|
query(`CREATE TABLE messages(msg text, username text)`);
|
|
|
|
|
|
|
|
// Insert message 'Hello, username!' using `username` as author's username
|
|
|
|
query(`INSERT INTO messages VALUES("Hello, username!","` + username + `")`);
|
|
|
|
|
|
|
|
// Get all messages
|
|
|
|
let messages = query(`SELECT * FROM messages`);
|
|
|
|
log("messages: " + messages);
|
|
|
|
|
|
|
|
// Get all messages as JSON via SQLite's JSON extension
|
|
|
|
return query(
|
|
|
|
`SELECT json_group_array(
|
|
|
|
json_object('msg', msg, 'username', username)
|
|
|
|
) AS json_result FROM (SELECT * FROM messages)`
|
|
|
|
)
|
|
|
|
}
|