Skip to content

Instantly share code, notes, and snippets.

View fritzy's full-sized avatar
💽
Please Insert Disk III

Nathan Fritz fritzy

💽
Please Insert Disk III
  • Kennewick, WA
  • 18:32 (UTC -07:00)
  • X @fritzy
View GitHub Profile
function promiser(input) {
if (input === 1) {
return Promise.resolve(input);
}
if (input === 2) {
return Promise.reject(new Error('Rejected'));
}
throw new Error('Broken');
}
@fritzy
fritzy / 1unique_short_id.sql
Last active September 15, 2017 17:43
Fight the German Tank Problem in Postgresql with short, unique, url-safe ids.
CREATE EXTENSION IF NOT EXISTS "pgcrypto";
-- Create a trigger function that takes no arguments.
-- Trigger functions automatically have OLD, NEW records
-- and TG_TABLE_NAME as well as others.
CREATE OR REPLACE FUNCTION unique_short_id()
RETURNS TRIGGER AS $$
-- Declare the variables we'll be using.
DECLARE

Pros:

  • Shallow Learning Curve
  • Easy to get and app running

Cons:

  • It's not composable.
  • DDP is not used anywhere else, REST is, and you'd have to do that in addition to your app.
  • Only MongoDB support right now, which we consider a marketing driven product with a troubled history rather than a technical driven tool (although Postgres support is in the works)
  • Making React views in Meteor client-side is not the point -- the front-end is tightly coupled to the backed via DDP, having the server direct the client state and causing many edge cases.
  • The biggest use case for Meteor is a niche edge case -- simultaneously editing the same field, and can be solved via standards and small packages.
@fritzy
fritzy / basic_proxy_example.js
Last active December 30, 2015 22:53
Basic Proxy Example
const someObject = {};
const someProxy = new Proxy(someObject, {
get: function (target, property, reciever) {
if (property.substr(0, 6) === 'happy_') {
return Reflect.get(target, property.substr(6), reciever);
}
return 'sad';
},
set: function (target, property, value, reciever) {
module.exports = function (server, options, next) {
console.log('loading plugin');
server.ext({
type:'onPostAuth',
method: function (request, reply) {
var req = request.raw.req;
var res2 = reply.response
var headers = {};
@fritzy
fritzy / json_pagination.sql
Last active November 27, 2020 11:22
Getting JSON paginated results of a table SELECT
SELECT json_build_object(
'total', (SELECT n_live_tup FROM pg_stat_user_tables WHERE relname='sometable'),
'count', count(sometable_rows.*),
'offset', 0,
'results', json_agg(row_to_json(sometable_rows))
)
FROM (SELECT * FROM sometable
ORDER BY "time"
LIMIT 10 OFFSET 0)
sometable_rows;
@fritzy
fritzy / node_postgres_careful.js
Last active August 29, 2015 14:21
Potentially dangerous use of SET ROLE in Postgres with Node.js
//POTENTIALLY DANGEROUS
pg.query('SET ROLE fritzy', function (err) {
//this doesn't happen until the event loop calls it after SET ROLE
//you don't know if the role has been changed since then on this connection
//your next query could be ANY role
pg.query('INSERT INTO some_table (...) VALUES (...)', function (err, result) {
// ...
});
});
CREATE OR REPLACE FUNCTION generate_update_query(value JSON, tbl TEXT) RETURNS TEXT as $$
DECLARE
key TEXT;
sets TEXT[];
BEGIN
FOR key IN
SELECT json_object_keys(value)
LOOP
IF (key != 'id') THEN
SELECT array_append(sets, format('%I=%L', key, value->>key)) INTO sets;
@fritzy
fritzy / 1_triggers.sql
Last active April 7, 2024 20:07
Get table change notifications from Postgres as JSON
CREATE OR REPLACE FUNCTION table_update_notify() RETURNS trigger AS $$
DECLARE
id bigint;
BEGIN
IF TG_OP = 'INSERT' OR TG_OP = 'UPDATE' THEN
id = NEW.id;
ELSE
id = OLD.id;
END IF;
PERFORM pg_notify('table_update', json_build_object('table', TG_TABLE_NAME, 'id', id, 'type', TG_OP)::text);
@fritzy
fritzy / gist:1b051bffc608cd25bd1c
Created March 30, 2015 23:36
generalized json joining
fritzy=# select * from authors2;
id | name
----+--------------
1 | Nathan Fritz
(1 row)
fritzy=# select * from books2;
id | title | author_id
----+-----------------------+-----------
1 | Happy Fun Times | 1