MyGit

0.29.2

drizzle-team/drizzle-orm

版本发布时间: 2023-12-25 18:26:37

drizzle-team/drizzle-orm最新发布版本:0.34.1(2024-10-08 04:07:09)

Fixes

ESLint Drizzle Plugin, v0.2.3

npm i eslint-plugin-drizzle@0.2.3

🎉 [ESLint] Add support for functions and improve error messages #1586 - thanks @ngregrichardson

New Drivers

🎉 Expo SQLite Driver is available

For starting with Expo SQLite Driver, you need to install expo-sqlite and drizzle-orm packages.

npm install drizzle-orm expo-sqlite@next

Then, you can use it like this:

import { drizzle } from "drizzle-orm/expo-sqlite";
import { openDatabaseSync } from "expo-sqlite/next";

const expoDb = openDatabaseSync("db.db");

const db = drizzle(expoDb);

await db.select().from(...)...

// or

db.select().from(...).then(...);

// or

db.select().from(...).all();

If you want to use Drizzle Migrations, you need to update babel and metro configuration files.

  1. Install babel-plugin-inline-import package.
npm install babel-plugin-inline-import
  1. Update babel.config.js and metro.config.js files.

babel.config.js

module.exports = function(api) {
  api.cache(true);

  return {
    presets: ['babel-preset-expo'],
+   plugins: [["inline-import", { "extensions": [".sql"] }]]
  };
};

metro.config.js

const { getDefaultConfig } = require('expo/metro-config');

/** @type {import('expo/metro-config').MetroConfig} */
const config = getDefaultConfig(__dirname);

+config.resolver.sourceExts.push('sql');

module.exports = config;
  1. Create drizzle.config.ts file in your project root folder.
import type { Config } from 'drizzle-kit';

export default {
	schema: './db/schema.ts',
	out: './drizzle',
	driver: 'expo',
} satisfies Config;

After creating schema file and drizzle.config.ts file, you can generate migrations like this:

npx drizzle-kit generate:sqlite

Then you need to import migrations.js file in your App.tsx file from ./drizzle folder and use hook useMigrations or migrate function.

import { drizzle } from "drizzle-orm/expo-sqlite";
import { openDatabaseSync } from "expo-sqlite/next";
import { useMigrations } from 'drizzle-orm/expo-sqlite/migrator';
import migrations from './drizzle/migrations';

const expoDb = openDatabaseSync("db.db");

const db = drizzle(expoDb);

export default function App() {
    const { success, error } = useMigrations(db, migrations);

    if (error) {
        return (
            <View>
                <Text>Migration error: {error.message}</Text>
            </View>
        );
    }

    if (!success) {
        return (
            <View>
                <Text>Migration is in progress...</Text>
            </View>
        );
    }

    return ...your application component;
}

相关地址:原始地址 下载(tar) 下载(zip)

1、 drizzle-orm-0.29.2-dist.tgz 624.62KB

查看:2023-12-25发行的版本