drizzle-team / drizzle-orm

Headless TypeScript ORM with a head. Runs on Node, Bun and Deno. Lives on the Edge and yes, it's a JavaScript ORM too 😅
https://orm.drizzle.team
Apache License 2.0
24.55k stars 645 forks source link

[FEATURE]: Support react-native-quick-sqlite #708

Open peterlazar1993 opened 1 year ago

peterlazar1993 commented 1 year ago

Describe want to want

React Native has a really good SQLite library https://github.com/margelo/react-native-quick-sqlite authored by @ospfranco and now maintained by @margelo.

TypeORM supports RN but this is currently broken, it would be awesome if drizzle had an adapter for quick-sqlite!

elevyg commented 1 year ago

I'd be amazing!. I'm trying to find a solution to easily sync server and an offline react native app. I've been trying using watermelondb and quick-sqlite to implement this feature but I'd be way more easy if I could use the same ORM both in the server and backend.

Currently I'm using Prisma in the backend but it seems they are not interested on supporting React Native.

peterlazar1993 commented 1 year ago

@elevyg If you need proper sync, you might be better off using https://rxdb.info/ or https://github.com/craftzdog/pouchdb-react-native

But I think both would require your backend be based on couchdb.

elevyg commented 1 year ago

PouchDb looks like is not having support anymore and RxDb despite being open source has a premium package that I don't like.

I think there is space for something more similar to watermelon db but written in a more declarative way.

L-U-C-K-Y commented 1 year ago

This would be great 👍🏼

RaghavBhat02 commented 1 year ago

this would be awesome! Would be nice if it supported expo sqlite as well.

Zoxive commented 1 year ago

this would be awesome! Would be nice if it supported expo sqlite as well.

You can use the sqlite-proxy pretty easily.

Heres a quick and dirty example:

import { drizzle } from "drizzle-orm/sqlite-proxy";
import * as SQLite from 'expo-sqlite';
import * as schema from "./schema";

export const expoSql = SQLite.openDatabase('mydb.db');

export const db = drizzle(async (sql, params, method) => {
    try {
        const results = await expoSql.execAsync([{ sql, args: params }], false);
        const rows = (results[0] as SQLite.ResultSet).rows;
        if (method === 'get') {
            if (rows.length === 0) return { rows: null as any };
            // The typescript apis wants an array, but it seems more likely that get methods want a single item
            return { rows: rows[0] as any };
        }

        return { rows: rows };
    } catch (e) {
        error('Error in db.ts:', e);
        return { rows: [] };
    }
}, {
    schema: schema,
    logger: {
        logQuery: function (query: string, params: unknown[]): void {
            console.log('[DRIZZLE] Query', { query, params });
        }
    }
});
ospfranco commented 1 year ago

I created a new package, based on quick-sqlite. It's muc faster and I also implemented the update_hook, which I guess is needed by drizzle.

https://github.com/OP-Engineering/op-sqlite