drizzle-orm icon indicating copy to clipboard operation
drizzle-orm copied to clipboard

[FEATURE]: Push feature for Dirzzle-orm

Open ansarizafar opened this issue 2 years ago • 3 comments

Describe what you want

I am using libsql and I am building a desktop and a mobile app. I can't bundle migrations folder with my app. Dirizzle-kit has a push command for altering database schema rapidly but we can't push changes with drizzle-orm at runtime.

lLike migrate, please provide a push function with drizzle-orm for schema modifications without first generating the migration scripts.

ansarizafar avatar Oct 04 '23 15:10 ansarizafar

@AndriiSherman Is it possible to add push schema feature to Drizzle-ORM?

ansarizafar avatar Oct 13 '23 12:10 ansarizafar

Would be very useful to generate memory based SQL clients for testing purposes:

import { createClient } from '@libsql/client';
import { pushSchema } from 'drizzle-orm';
import { drizzle } from 'drizzle-orm/libsql';

import { schema } from './schema';

export const createDatabaseClientMock = () => {
  const client = createClient({ url: ':memory:' });

  const db = drizzle(client);

  await pushSchema(schema, db)

  return db;
};

hugo082 avatar Dec 13 '23 15:12 hugo082

Would be very useful to generate memory based SQL clients for testing purposes:

import { createClient } from '@libsql/client';
import { pushSchema } from 'drizzle-orm';
import { drizzle } from 'drizzle-orm/libsql';

import { schema } from './schema';

export const createDatabaseClientMock = () => {
  const client = createClient({ url: ':memory:' });

  const db = drizzle(client);

  await pushSchema(schema, db)

  return db;
};

@hugo082 I'm using v0.30.10, it does not have this command. Module '"drizzle-orm"' has no exported member 'pushSchema'

eyeix avatar May 18 '24 02:05 eyeix

Is there any progress on this issue? Push feature will also be useful for migrating PGLite schema.

ansarizafar avatar Oct 06 '24 07:10 ansarizafar

Kit now exposes it's API which includes a push function. This isn't documented though.

L-Mario564 avatar Oct 16 '24 19:10 L-Mario564

@L-Mario564 Could you please share a code sample or a link about Kit push API?

ansarizafar avatar Oct 28 '24 07:10 ansarizafar

I'm running it on deno — there is some weirdness with conditional exports — I worked around it by vendoring drizzle-kit/api

/// main.ts 
import { drizzle } from "drizzle-orm/pglite";
import { integer, pgTable, varchar } from "drizzle-orm/pg-core";
import { pushSchema } from "drizzle-kit/api";

let db = drizzle();

let users = pgTable("users", {
    id: integer().primaryKey().generatedAlwaysAsIdentity(),
    name: varchar({ length: 255 }).notNull(),
    age: integer().notNull(),
    email: varchar({ length: 255 }).notNull().unique(),
});

const patch = await pushSchema({ users }, db);
await patch.apply();

await db.insert(users).values([
    { name: "Alice", age: 30, email: "[email protected]" },
    { name: "Bob", age: 40, email: "[email protected]" },
]);

console.log(await db.$count(users));

zkdiff avatar Nov 25 '24 14:11 zkdiff