update full stack demo to 1.3.4

This commit is contained in:
Rolands 2023-07-22 17:46:00 +03:00
parent 0ce834e054
commit e1ec99224f
4 changed files with 39 additions and 40 deletions

View file

@ -9,7 +9,7 @@
"version": "1.0.0",
"dependencies": {
"sequelize": "^6.28.0",
"socio": "^1.0.1",
"socio": "^1.3.1",
"sqlite3": "^5.1.4",
"svelte-french-toast": "^1.0.3",
"ws": "^8.11.0"
@ -1424,9 +1424,9 @@
}
},
"node_modules/make-dir/node_modules/semver": {
"version": "6.3.0",
"resolved": "https://registry.npmjs.org/semver/-/semver-6.3.0.tgz",
"integrity": "sha512-b39TBaTSfV6yBrapU89p5fKekE2m/NwnDocOVruQFS1/veMgdzuPcnOM34M6CwxW8jH/lxEa5rBoDeUwu5HHTw==",
"version": "6.3.1",
"resolved": "https://registry.npmjs.org/semver/-/semver-6.3.1.tgz",
"integrity": "sha512-BR7VvDCVHO+q2xBEWskxS6DJE1qRnb7DxzUrogb71CWoSficBxYsiAGd+Kl0mmq/MprG9yArRkyrQxTO6XjMzA==",
"bin": {
"semver": "bin/semver.js"
}
@ -2289,9 +2289,9 @@
}
},
"node_modules/socio": {
"version": "1.0.5",
"resolved": "https://registry.npmjs.org/socio/-/socio-1.0.5.tgz",
"integrity": "sha512-3zB05Bjwhe5/o8cqHnylydWoD5YZB/tPwe77mfSF1D+nJQZbylew64M7I9PzgIMV/mrio87EHp43U26eDxNG4w==",
"version": "1.3.4",
"resolved": "https://registry.npmjs.org/socio/-/socio-1.3.4.tgz",
"integrity": "sha512-RyuoYC5nBLA/6ZwiuyKHuB7dj4WDXvxMdwblyYM2zgLRN8Z576duxzAr5SUNElOzmCJdIC84yQNVgGbBpAd/FA==",
"dependencies": {
"base64-js": "^1.5.1",
"recursive-diff": "^1.0.9",

View file

@ -23,7 +23,7 @@
"type": "module",
"dependencies": {
"sequelize": "^6.28.0",
"socio": "^1.0.1",
"socio": "^1.3.1",
"sqlite3": "^5.1.4",
"svelte-french-toast": "^1.0.3",
"ws": "^8.11.0"

View file

@ -2,51 +2,51 @@
import { log, info, done, soft_error } from 'socio/dist/logging';
import { SocioServer } from 'socio/dist/core';
import { SocioSecurity } from 'socio/dist/secure';
import {perMessageDeflate} from 'socio/dist/utils'; //for auto compressing WS messages. Carefully read documentation before using this! Possible memory leaks!
import { perMessageDeflate } from 'socio/dist/utils'; //for auto compressing WS messages. Carefully read documentation before using this! Possible memory leaks!
import { SaveFilesToDiskPath } from 'socio/dist/fs-utils';
//DB stuff
import { Sequelize } from 'sequelize';
//types
import type { QueryFunction, QueryFuncParams } from 'socio/dist/core';
import type { PropValue, id, PropAssigner, SocioFiles } from 'socio/dist/types';
import type { SocioSession } from 'socio/dist/core-session';
try{
try {
info('Starting SocioServer...');
//constants
const Query = await InitDB_GetQueryFunc();
const db_interface = await SetUpDBInterface();
//load in the secure_private_key with dotenv or smth. Dont hardcode like this
const socsec = new SocioSecurity({ secure_private_key: 'skk#$U#Y$7643GJHKGDHJH#$K#$HLI#H$KBKDBDFKU34534', logging: { verbose: false } });
const socserv = new SocioServer({ port: 3000, perMessageDeflate }, { DB_query_function: Query, logging: { verbose: true, hard_crash:false }, socio_security: socsec });
const socserv = new SocioServer({ port: 3000 }, { db: db_interface, logging: { verbose: true, hard_crash: false }, socio_security: socsec });
const validate_color_prop: PropAssigner = (curr_val: PropValue, new_val: PropValue) => {
if (typeof new_val != 'string' || new_val.length != 7) return false;
if (!new_val.match(/^#[0-9a-f]{6}/mi)) return false;
return socserv.SetPropVal('color', new_val);
}
socserv.RegisterProp('color', '#ffffff', validate_color_prop);
socserv.RegisterProp('color', '#ffffff', {
// assigner is optional and has a default to just accept whatever new value comes in.
assigner: (curr_val: PropValue, new_val: PropValue) => {
if (typeof new_val != 'string' || new_val.length != 7) return false;
if (!new_val.match(/^#[0-9a-f]{6}/mi)) return false;
return socserv.SetPropVal('color', new_val);
}
});
socserv.RegisterProp('num', 0);
socserv.RegisterLifecycleHookHandler('file_upload', (client: SocioSession, files: SocioFiles) => {
return SaveFilesToDiskPath(['.', 'upload_files'], files).result;
});
}
catch (e:any) {soft_error(e)}
catch (e: any) { soft_error(e) }
async function InitDB_GetQueryFunc() {
async function SetUpDBInterface() {
const sequelize = new Sequelize('sqlite::memory:');
await sequelize.query('CREATE TABLE Users(userid INTEGER PRIMARY KEY AUTOINCREMENT, name varchar(50), num INTEGER NOT NULL DEFAULT 0);', { logging: false });
await sequelize.query('INSERT INTO Users (name, num) VALUES("Jane", 42);', { logging: false });
await sequelize.query('INSERT INTO Users (name, num) VALUES("John", 69);', { logging: false });
const QueryWrap = async (client: SocioSession, id: id, sql: string, params: object | null = null) => {
//@ts-expect-error
return (await sequelize.query(sql, { logging: false, raw: true, replacements: params }))[0] as Promise<object>;
}
return QueryWrap as QueryFunction;
return {
Query: async (client: SocioSession, id: id, sql: string, params: any) => {
return (await sequelize.query(sql, { logging: false, raw: true, replacements: params }))?.at(0) as Promise<object>;
}
};
}

View file

@ -1,9 +1,8 @@
<script lang="ts">
//imports
import { SocioClient } from "socio/dist/core-client";
import type {id} from 'socio/dist/types'
import { onMount, onDestroy } from "svelte";
import {socio} from 'socio/dist/utils'
import {socio} from 'socio/dist/utils';
import { slide } from "svelte/transition";
import toast from 'svelte-french-toast'; //https://github.com/kbrgl/svelte-french-toast
@ -32,7 +31,7 @@
//variables
let ready = false;
let user_count = 0, users: { userid: number; name: string; num: number }[] = [];
let user_count = 0, Users: { userid: number; name: string; num: number }[] = [];
let insert_fields = { name: "Bob", num: 42 };
let color_prop = "#ffffff", num = 0;
let progress = writable(0);
@ -41,13 +40,13 @@
ready = await sc.ready();
toast.success('Socio Client connected!', {icon:'🥳', style:'padding:2px;',position: "bottom-center", duration:1500});
const id = sc.Subscribe({sql: socio`SELECT COUNT(*) AS RES FROM users WHERE name = :name;`, params: { name: "John" }}, (res:any) => {
const id = sc.Subscribe({sql: socio`SELECT COUNT(*) AS RES FROM Users WHERE name = :name;`, params: { name: "John" }}, (res:any) => {
user_count = res[0].RES as number; //res is whatever object your particular DB interface lib returns from a raw query
}
);
sc.Subscribe({ sql: socio`SELECT * FROM users;` },(res:any) => {
users = res as { userid: number; name: string; num: number }[]; //res is whatever object your particular DB interface lib returns from a raw query
sc.Subscribe({ sql: socio`SELECT * FROM Users;` },(res:any) => {
Users = res as { userid: number; name: string; num: number }[]; //res is whatever object your particular DB interface lib returns from a raw query
}
);
@ -91,8 +90,8 @@
<h6 class="darker_text bold">subscribed sql query:</h6>
<h4>
SELECT COUNT(*) FROM users WHERE name = :name <span class="h5 darker_text bold">(John)</span>; =
{#if user_count}
SELECT COUNT(*) FROM Users WHERE name = :name <span class="h5 darker_text bold">(John)</span>; =
{#if typeof user_count == 'number'}
<span class="bold">{user_count}</span>
{:else}
<Bloom><Spinner style="--h:24px;--t:6px;" /></Bloom>
@ -112,18 +111,18 @@
style="width:100%;"
on:click={async () =>
await sc.Query(
socio`INSERT INTO users (name, num) VALUES(:name, :num);`,
socio`INSERT INTO Users (name, num) VALUES(:name, :num);`,
insert_fields
)}
>
INSERT INTO users (name, num) VALUES("<span class="acc1 norm">{insert_fields.name}</span>",
INSERT INTO Users (name, num) VALUES("<span class="acc1 norm">{insert_fields.name}</span>",
<span class="acc1 norm">{insert_fields.num || 0}</span>);
</Button>
</Bloom>
</section>
<section class="vert users">
{#each users as u (u.userid)}
<section class="vert Users">
{#each Users as u (u.userid)}
<div class="user" transition:slide>
<h4>{u.userid}</h4>
<h4 class="acc1">|</h4>
@ -207,7 +206,7 @@
outline: none;
}
.users {
.Users {
max-width: 600px;
width: 600px;
gap: $pad_small;