Skip to content

Instantly share code, notes, and snippets.

View glommer's full-sized avatar

Glauber Costa glommer

View GitHub Profile
import { User } from "../models/User";
import { Action, RequestContext } from "@chiselstrike/api";
export default {
read: (user: User, ctx: RequestContext) => {
if (ctx.token && user.userId && user.userId == ctx.token['userId']) {
return Action.Allow;
} else {
return Action.Skip;
}
import { User } from "../models/User";
import { Action, RequestContext } from "@chiselstrike/api";
export default {
create: (user: User, ctx: RequestContext) => {
if (ctx.token && user.userId && user.userId == ctx.token['userId']) {
return Action.Allow;
} else {
return Action.Deny;
}
@glommer
glommer / jwt.json
Last active November 11, 2022 23:13
{
"userId": "xxxx",
"otherFields": "yyy"
}
// in models/User.ts
import {ChiselEntity} from "@chiselstrike/api";
export class User extends ChiselEntity {
// the external user_id (coming from JWT auth, not public data)
userId?: string;
// the user's username
username: string;
// the user's email (not public data!)
email?: string;
import { TopOfBook } from "../models/TopOfBook";
export default TopOfBook.crud();
import { ChiselEvent } from "@chiselstrike/api";
import { TopOfBook } from "../models/TopOfBook";
function toJSON(buffer: ArrayBuffer) {
return JSON.parse(String.fromCharCode.apply(null, new Uint8Array(buffer)));
}
export default async function (event: ChiselEvent) {
const bookUpdate = toJSON(event.value);
const symbol = bookUpdate.symbol;
import { ChiselEntity } from "@chiselstrike/api"
export class TopOfBook extends ChiselEntity {
symbol: string;
bidPrice?: number;
bidVolume?: number;
askPrice?: number;
askVolume?: number;
}
CHISEL_SERVER="https://chiselstrike-examples-glommer.chiselstrike.io"
CHISEL_VERSION="main"
import type { Post } from "../../chiselstrike/Post";
export type { Post } from "../../chiselstrike/Post";
function chiselUrl(name: string): string {
// Use environment variables to figure out where we are. Feel free to add this
// to your .env, but to keep things simple we'll add a default
const chiselServer = process.env.CHISEL_SERVER ?? "http://localhost:8080";
// Which version? Versions are isolated backends that can be used in the same
// chisel instance. You can do test databases, API versions, or what your heart
// desires. By (local) default this is "dev", but when deployed it is customary
$ curl http://localhost:8080/dev/posts
{
"next_page": "http://localhost:8080/dev/posts?cursor=eyJheGVzIjpbeyJrZXkiOnsiZmllbGROYW1lIjoiaWQiLCJhc2NlbmRpbmciOnRydWV9LCJ2YWx1ZSI6ImMyNzMwODA3LTVjZWYtNDQ0MC1hNzk2LWVjNjIxNjcxMjZkYyJ9XSwiZm9yd2FyZCI6dHJ1ZSwiaW5jbHVzaXZlIjpmYWxzZX0%3D",
"prev_page": "http://localhost:8080/dev/posts?cursor=eyJheGVzIjpbeyJrZXkiOnsiZmllbGROYW1lIjoiaWQiLCJhc2NlbmRpbmciOnRydWV9LCJ2YWx1ZSI6IjE5YTQzOTU3LWVmN2YtNGViZC04MDk2LTJkN2UzNzQ5NGUyYSJ9XSwiZm9yd2FyZCI6ZmFsc2UsImluY2x1c2l2ZSI6ZmFsc2V9",
"results": [
{
"id": "19a43957-ef7f-4ebd-8096-2d7e37494e2a",
"title": "my first post",
"markdown": "Here iss our first post"
},