I am a Dev
I am a Dev
Explore posts from servers
TtRPC
Created by I am a Dev on 10/14/2023 in #❓-help
Hello, is there any way to create a base router with common procedures? like an interface
I'm using electron-trpc, so basically the router is like a store, so I can use all the node logic in the main process, but subscribe to the state in the renderer. And because I have more than 30 store like this, want to create common hooks and interfaces
8 replies
TtRPC
Created by I am a Dev on 10/14/2023 in #❓-help
Hello, is there any way to create a base router with common procedures? like an interface
But i want to only pass the router as a argument, because i want to return more properties that only the state
8 replies
TtRPC
Created by I am a Dev on 10/14/2023 in #❓-help
Hello, is there any way to create a base router with common procedures? like an interface
I have something like this:
import { useEffect, useState } from 'react';
import type { Unsubscribable } from '@trpc/server/observable';
import type {} from '@trpc/client/src/';

interface UseRouterStateOptions<TState> {
stateSubscribe: (
input: undefined,
opts: { onData: (state: TState) => void },
) => Unsubscribable;
}

export function useRouterState<TState>(
opts: UseRouterStateOptions<TState>,
): TState | null {
const [state, setState] = useState<TState | null>(null);

useEffect(() => {
const stateSubscription = opts.stateSubscribe(undefined, {
onData: (data) => {
setState(data);
},
});

return () => {
stateSubscription.unsubscribe();
};
}, []);

return state;
}
import { useEffect, useState } from 'react';
import type { Unsubscribable } from '@trpc/server/observable';
import type {} from '@trpc/client/src/';

interface UseRouterStateOptions<TState> {
stateSubscribe: (
input: undefined,
opts: { onData: (state: TState) => void },
) => Unsubscribable;
}

export function useRouterState<TState>(
opts: UseRouterStateOptions<TState>,
): TState | null {
const [state, setState] = useState<TState | null>(null);

useEffect(() => {
const stateSubscription = opts.stateSubscribe(undefined, {
onData: (data) => {
setState(data);
},
});

return () => {
stateSubscription.unsubscribe();
};
}, []);

return state;
}
And in the widget I should use this:
const state = useRouterState({
stateSubscribe:
tRPCClient.router1.router2.state.subscribe,
});
const state = useRouterState({
stateSubscribe:
tRPCClient.router1.router2.state.subscribe,
});
8 replies
TTCTheo's Typesafe Cult
Created by I am a Dev on 10/5/2023 in #questions
with t3 what do you use to stream open ai chat gpt responses?
Custom express endpoint? Web socket with custom Chanel per request? Thanks
4 replies
DTDrizzle Team
Created by I am a Dev on 10/3/2023 in #help
Drizzle support SELECT FOR UPDATE of postgres to avoid logical race conditions ?
I will try tomorrow 🙂
23 replies
DTDrizzle Team
Created by I am a Dev on 10/3/2023 in #help
Drizzle support SELECT FOR UPDATE of postgres to avoid logical race conditions ?
Yes, I tested with 20k items at works fine
23 replies
DTDrizzle Team
Created by I am a Dev on 10/3/2023 in #help
Drizzle support SELECT FOR UPDATE of postgres to avoid logical race conditions ?
Yes, also you can add select pg_sleep(5) before commit, and call it from multiple clients and it do correct the row lock
23 replies
DTDrizzle Team
Created by I am a Dev on 10/3/2023 in #help
Drizzle support SELECT FOR UPDATE of postgres to avoid logical race conditions ?
It's something like that
await prisma.$transaction(
async (tx) => {
const counters = await tx.$queryRaw<
Counter[]
>`SELECT * FROM "Counter" WHERE id = ${input.id} FOR UPDATE;`;

if (counters.length === 0) {
throw new Error('Counter not found');
}

const counter = counters[0];
await tx.counter.update({
data: {
count: counter.count + 1,
},
where: {
id: counter.id,
},
});
}
);
await prisma.$transaction(
async (tx) => {
const counters = await tx.$queryRaw<
Counter[]
>`SELECT * FROM "Counter" WHERE id = ${input.id} FOR UPDATE;`;

if (counters.length === 0) {
throw new Error('Counter not found');
}

const counter = counters[0];
await tx.counter.update({
data: {
count: counter.count + 1,
},
where: {
id: counter.id,
},
});
}
);
23 replies
DTDrizzle Team
Created by I am a Dev on 10/3/2023 in #help
Drizzle support SELECT FOR UPDATE of postgres to avoid logical race conditions ?
In prisma I use raw sql since it not support select ... for update , but I can't figure out how to do it with drizzle methods
23 replies
DTDrizzle Team
Created by I am a Dev on 10/3/2023 in #help
Drizzle support SELECT FOR UPDATE of postgres to avoid logical race conditions ?
I make this work with Prisma and node-postgres, I try to migrate it to drizzle, but I can't 😦
23 replies
DTDrizzle Team
Created by I am a Dev on 10/3/2023 in #help
Drizzle support SELECT FOR UPDATE of postgres to avoid logical race conditions ?
I tried with this, but this is called 50 time but only increment the counter 1 time, not like the expected behavior
const incrementCounter = async (id: number): Promise<void> => {
console.log(`Incrementing counter ${id}`);
await db.transaction(async (tx) => {
const counter = await tx
.select()
.from(counters)
.where(eq(counters.id, id))
.for('update');
if (counter.length > 0) {
const newCount = counter[0].count + 1;

await tx
.update(counters)
.set({
count: newCount,
})
.where(eq(counters.id, id));
}
});
};

await Promise.allSettled(
Array.from({ length: 50 }, (_, i) => i).map(() => incrementCounter(1)),
);
console.log('Incremented counter 50 times');
const incrementCounter = async (id: number): Promise<void> => {
console.log(`Incrementing counter ${id}`);
await db.transaction(async (tx) => {
const counter = await tx
.select()
.from(counters)
.where(eq(counters.id, id))
.for('update');
if (counter.length > 0) {
const newCount = counter[0].count + 1;

await tx
.update(counters)
.set({
count: newCount,
})
.where(eq(counters.id, id));
}
});
};

await Promise.allSettled(
Array.from({ length: 50 }, (_, i) => i).map(() => incrementCounter(1)),
);
console.log('Incremented counter 50 times');
23 replies
DTDrizzle Team
Created by I am a Dev on 10/3/2023 in #help
Drizzle support SELECT FOR UPDATE of postgres to avoid logical race conditions ?
For example how to do this statement?
BEGIN;
SELECT * FROM "counters" WHERE id = 1 FOR UPDATE;
UPDATE "counters" SET count = count + 1 WHERE id = 1;
COMMIT;
BEGIN;
SELECT * FROM "counters" WHERE id = 1 FOR UPDATE;
UPDATE "counters" SET count = count + 1 WHERE id = 1;
COMMIT;
23 replies
TtRPC
Created by I am a Dev on 9/30/2023 in #❓-help
How to return a stream? like open ai api
Thanks, I will check it. Meantime, how can I make each subscription return a different value for each request? for example, each chat completion request must be different for each user, and only each user can listen the response. Thanks
5 replies
PD🧩 Plasmo Developers
Created by I am a Dev on 9/19/2023 in #🔰newbie
There is a difference in the developer experience building the options page vs normal web with vite?
I mean hot reload and hmr
3 replies