From b4426fc2f0c1a0e36f7166e82e93a053cac3772d Mon Sep 17 00:00:00 2001 From: David ALLIX Date: Tue, 15 Oct 2024 20:31:24 +0200 Subject: [PATCH] Fix --- README.md | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/README.md b/README.md index 1795576..610e723 100644 --- a/README.md +++ b/README.md @@ -157,6 +157,35 @@ const allUsersPaginated = await schemQl.all({ ORDER BY ${'@users.id'} ${s.sqlCond(data.query.dir === 'prev', 'DESC', 'ASC')} LIMIT ${':limit'} `) + +// Automatically stringify JSON params 'metadata' (by schemQl if enabled) +// and get parsed JSON metadata, as well (if Zod preprocess set rightly) +const firstSession = await schemQl.firstOrThrow({ + params: { + id: uuidv4(), + user_id: 'uuid-1', + metadata: { + account: 'credentials', + }, + expiresAtAdd: 10000, + }, + paramsSchema: z.object({ + ...zSessionDb.pick({ id: true, user_id: true, metadata: true }).shape, + expiresAtAdd: z.number().int(), + }), + resultSchema: zSessionDb, +})((s) => s.sql` + INSERT INTO + ${{ sessions: ['id', 'user_id', 'metadata', 'expires_at'] }} + VALUES + ( + ${':id'} + , ${':user_id'} + , json(${':metadata'}) + , strftime('%s', 'now') + ${':expiresAtAdd'} + ) + RETURNING * +`) ```