joostschuur
joostschuur
DTDrizzle Team
Created by joostschuur on 5/15/2024 in #help
Defining types for default values in createSelectSchema
In my users table schema for a Turso DB, I've got a field of settings: text('settings', { mode: 'json' }). I'd like to define the defaults outside of the schema definition, so this doesn't potentially change the database and require a migration when I need new setting values, but I also want to type this field in my app. So I have a userSettingsSchema and want to set the default via createSelectSchema's options:
export const userSettingsSchema = z.object({
maxOpenTodoItems: z.number().int().min(1).max(24),
});
export type UserSettings = z.infer<typeof userSettingsSchema>;

const userDataSchemaOptions = {
settings: userSettingsSchema.default(defaultUserSettings),
};

export const userDataSchema = createSelectSchema(userData, userDataSchemaOptions);
export type UserData = z.infer<typeof userDataSchema>;
export const userSettingsSchema = z.object({
maxOpenTodoItems: z.number().int().min(1).max(24),
});
export type UserSettings = z.infer<typeof userSettingsSchema>;

const userDataSchemaOptions = {
settings: userSettingsSchema.default(defaultUserSettings),
};

export const userDataSchema = createSelectSchema(userData, userDataSchemaOptions);
export type UserData = z.infer<typeof userDataSchema>;
However, under UserData, settings is still null or my UserSettings type. Wouldn't my userDataSchemaOptions have narrowed that down? It looks like I can force it more explicitly like this:
export type UserData = z.infer<typeof userDataSchema> & { settings: UserSettings };
export type UserData = z.infer<typeof userDataSchema> & { settings: UserSettings };
1 replies
DTDrizzle Team
Created by joostschuur on 4/26/2024 in #help
default to empty object for sqlite text field with JSON mode
How can I default a mode: 'json' text type to an empty object in sqlite? The following in a sqliteTable definition results in a literal string of ([object Object]):
data: text('data', { mode: 'json' }).notNull().default({}),
data: text('data', { mode: 'json' }).notNull().default({}),
Empty arrays are discussed here, but not empty objects: https://orm.drizzle.team/learn/guides/empty-array-default-value#sqlite Using default(sql'{}') (with backticks around the single quotes, I can't get the to show up in Discord markdown) or default('{}') defaults it to a literal '{}', and querying that gives an error:
SyntaxError: Unexpected token ''', "'{}'" is not valid JSON
at JSON.parse (<anonymous>)
at SQLiteTextJson.mapFromDriverValue
SyntaxError: Unexpected token ''', "'{}'" is not valid JSON
at JSON.parse (<anonymous>)
at SQLiteTextJson.mapFromDriverValue
I'm using drizzle-orm": v0.30.9 and Turso/libsql.
1 replies
DTDrizzle Team
Created by joostschuur on 3/14/2024 in #help
Error: Cannot find module '@libsql/darwin-arm64' when using Drizzle/SST
I'm using SST in a monorepo with Drizzle, and my Drizzle setup is in a separate workspace package. When I try calling code that invokes the Drizzle client I get this error about missing a platform specific module:
backend:dev: | Invoked src/endpoints/domains.handler
backend:dev: | Built src/endpoints/domains.handler
backend:dev: | Error: Cannot find module '@libsql/darwin-arm64'
backend:dev: Require stack:
backend:dev: - /Users/joostschuur/Code/Playmob/survey-dashboard/apps/backend/.sst/artifacts/c8a74ec35862a7b85e0a79e5a246c48c9f0b665e9b/src/endpoints/domains.mjs
backend:dev: Error: Cannot find module '@libsql/darwin-arm64'
backend:dev: Require stack:
backend:dev: - /Users/joostschuur/Code/Playmob/survey-dashboard/apps/backend/.sst/artifacts/c8a74ec35862a7b85e0a79e5a246c48c9f0b665e9b/src/endpoints/domains.mjs
backend:dev: | Invoked src/endpoints/domains.handler
backend:dev: | Built src/endpoints/domains.handler
backend:dev: | Error: Cannot find module '@libsql/darwin-arm64'
backend:dev: Require stack:
backend:dev: - /Users/joostschuur/Code/Playmob/survey-dashboard/apps/backend/.sst/artifacts/c8a74ec35862a7b85e0a79e5a246c48c9f0b665e9b/src/endpoints/domains.mjs
backend:dev: Error: Cannot find module '@libsql/darwin-arm64'
backend:dev: Require stack:
backend:dev: - /Users/joostschuur/Code/Playmob/survey-dashboard/apps/backend/.sst/artifacts/c8a74ec35862a7b85e0a79e5a246c48c9f0b665e9b/src/endpoints/domains.mjs
This from this API handler in apps/backend:
import { ApiHandler } from 'sst/node/api';

import { db, domains } from 'database';

export const handler = ApiHandler(async () => {
const res = await db.select().from(domains);

return {
statusCode: 200,
body: JSON.stringify(res, null, 2),
};
});
import { ApiHandler } from 'sst/node/api';

import { db, domains } from 'database';

export const handler = ApiHandler(async () => {
const res = await db.select().from(domains);

return {
statusCode: 200,
body: JSON.stringify(res, null, 2),
};
});
With db being exported from packages/database here:
import { createClient } from '@libsql/client';
import { drizzle } from 'drizzle-orm/libsql';

import * as schema from '@/schema';

const authToken = process.env.DATABASE_AUTH_TOKEN;
const url = process.env.DATABASE_URL;

const client = createClient({
url,
authToken,
});

export const db = drizzle(client, { schema });
import { createClient } from '@libsql/client';
import { drizzle } from 'drizzle-orm/libsql';

import * as schema from '@/schema';

const authToken = process.env.DATABASE_AUTH_TOKEN;
const url = process.env.DATABASE_URL;

const client = createClient({
url,
authToken,
});

export const db = drizzle(client, { schema });
packages/database has @libsql/client as a dependency, nothing platform specific. If I explicitly add @libsql/darwin-arm64 as a dependency to apps/backend, then that works for local dev. When I deploy it, it complains about @libsql/linux-x64-gnu of course, but this should just work without a hack like that, right? I have a Next.js app running on Vercel (for now) also importing the database package and that works just fine.
3 replies
DTDrizzle Team
Created by joostschuur on 2/13/2024 in #help
Using Drizzle SQLite in a Next.js server component gives error that suggests client side execution
I attempted to do a basic implementation in a Next.js 14.1 app using Drizzle (v0.29.3) and @libsql/client (v0.4.3) based on https://orm.drizzle.team/docs/get-started-sqlite#turso and get the following error on my local machine when I start next dev.
⨯ ./node_modules/.pnpm/@[email protected]/node_modules/@libsql/darwin-arm64/README.md
Module parse failed: Unexpected character ' ' (1:1)
You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders
> # `@libsql/darwin-arm64`
|
| Prebuilt binary package for `libsql` on `darwin-arm64`.

Import trace for requested module:
./node_modules/.pnpm/@[email protected]/node_modules/@libsql/darwin-arm64/README.md
./node_modules/.pnpm/[email protected]/node_modules/@libsql/ ./node_modules/.pnpm/node_modules/@libsql/ ./node_modules/@libsql/ ./node_modules/.pnpm/node_modules/@libsql/ ./node_modules/.pnpm/[email protected]/node_modules/@libsql/ ./node_modules/.pnpm/node_modules/@libsql/ ./node_modules/@libsql/ ./node_modules/.pnpm/node_modules/@libsql/ sync ^\.\/.*$
./node_modules/.pnpm/[email protected]/node_modules/libsql/index.js
./node_modules/.pnpm/@[email protected]/node_modules/@libsql/client/lib-esm/sqlite3.js
./node_modules/.pnpm/@[email protected]/node_modules/@libsql/client/lib-esm/node.js
⨯ ./node_modules/.pnpm/@[email protected]/node_modules/@libsql/darwin-arm64/README.md
Module parse failed: Unexpected character ' ' (1:1)
You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders
> # `@libsql/darwin-arm64`
|
| Prebuilt binary package for `libsql` on `darwin-arm64`.

Import trace for requested module:
./node_modules/.pnpm/@[email protected]/node_modules/@libsql/darwin-arm64/README.md
./node_modules/.pnpm/[email protected]/node_modules/@libsql/ ./node_modules/.pnpm/node_modules/@libsql/ ./node_modules/@libsql/ ./node_modules/.pnpm/node_modules/@libsql/ ./node_modules/.pnpm/[email protected]/node_modules/@libsql/ ./node_modules/.pnpm/node_modules/@libsql/ ./node_modules/@libsql/ ./node_modules/.pnpm/node_modules/@libsql/ sync ^\.\/.*$
./node_modules/.pnpm/[email protected]/node_modules/libsql/index.js
./node_modules/.pnpm/@[email protected]/node_modules/@libsql/client/lib-esm/sqlite3.js
./node_modules/.pnpm/@[email protected]/node_modules/@libsql/client/lib-esm/node.js
26 replies
DTDrizzle Team
Created by joostschuur on 9/9/2023 in #help
Updating with a subquery
Can I do this in Drizzle without the raw SQL?
db.execute(sql`
UPDATE feeds AS f
SET "lastPublishedAt" = (
SELECT MAX(fi."publishedAt")
FROM "feedItems" AS fi
WHERE fi."feedId" = f.id
);`);
db.execute(sql`
UPDATE feeds AS f
SET "lastPublishedAt" = (
SELECT MAX(fi."publishedAt")
FROM "feedItems" AS fi
WHERE fi."feedId" = f.id
);`);
34 replies
DTDrizzle Team
Created by joostschuur on 5/6/2023 in #help
Using Vercel Postgres and developing locally
To confirm, if I want to use Vercel Postgres in production and a local Postgres DB for development, is the best way to just conditionally import drizzle (and other platform specific stuff like sql) from drizzle-orm/vercel-postgres or drizzle-orm/node-postgres based on NODE_ENV? I see the sample Next.js project has been updated for Vercel Postgres: https://github.com/vercel/examples/blob/main/storage/postgres-drizzle/lib/drizzle.ts
65 replies