Drizzle <> OP SQLite
**곡μ GitHub νμ΄μ§**μ λ°λ₯΄λ©΄, OP-SQLiteλ μ΅μ λ²μ μ SQLiteλ₯Ό μλ² λνκ³ SQL 쿼리λ₯Ό μ€νν μ μλ μ μμ€ APIλ₯Ό μ 곡ν©λλ€.
npm i drizzle-orm @op-engineering/op-sqlite
npm i -D drizzle-kit
import { drizzle } from "drizzle-orm/op-sqlite";
import { open } from '@op-engineering/op-sqlite';
const opsqlite = open({
name: 'myDB',
});
const db = drizzle(opsqlite);
await db.select().from(users);Drizzle Kitλ₯Ό SQL λ§μ΄κ·Έλ μ΄μ μμ±μ μ¬μ©ν μ μμ΅λλ€. μ§ννκΈ° μ μ Drizzle Kit λ§μ΄κ·Έλ μ΄μ μ΄ μ΄λ»κ² μλνλμ§ νμΈνμΈμ. OP SQLiteλ SQL λ§μ΄κ·Έλ μ΄μ μ μ±μ λ²λ€λ‘ ν¬ν¨ν΄μΌ νλ©°, μ ν¬κ° μ΄λ₯Ό μ§μν©λλ€.
babel νλ¬κ·ΈμΈ μ€μΉ
SQL λ§μ΄κ·Έλ μ΄μ νμΌμ λ¬Έμμ΄λ‘ μ§μ λ²λ€μ ν¬ν¨νλ €λ©΄ νμν©λλ€.
npm install babel-plugin-inline-importμ€μ νμΌ μ λ°μ΄νΈ
babel.config.js, metro.config.js λ° drizzle.config.ts νμΌμ μ
λ°μ΄νΈν΄μΌ ν©λλ€
module.exports = {
presets: ['module:@react-native/babel-preset'],
plugins: [
[
'inline-import',
{
extensions: ['.sql'],
},
],
],
};const { getDefaultConfig } = require('@react-native/metro-config');
const config = getDefaultConfig(__dirname);
config.resolver.sourceExts.push('sql');
module.exports = config;Drizzle Kit μ€μ μ dialect: 'sqlite' λ° driver: 'expo'κ° μλμ§ νμΈνμΈμ
import { defineConfig } from 'drizzle-kit';
export default defineConfig({
schema: './db/schema.ts',
out: './drizzle',
dialect: 'sqlite',
driver: 'expo', // <--- λ§€μ° μ€μν©λλ€
});λ§μ΄κ·Έλ μ΄μ μμ±
SQL μ€ν€λ§ νμΌκ³Ό drizzle.config.ts νμΌμ μμ±ν ν λ§μ΄κ·Έλ μ΄μ μ μμ±ν μ μμ΅λλ€
npx drizzle-kit generateμ±μ λ§μ΄κ·Έλ μ΄μ μΆκ°
μ΄μ ./drizzle ν΄λμμ migrations.js νμΌμ Expo/React Native μ±μΌλ‘ κ°μ ΈμμΌ ν©λλ€.
μ ν리μΌμ΄μ
μμ μ 컀μ€ν
useMigrations λ§μ΄κ·Έλ μ΄μ
ν
μ μ¬μ©νκ±°λ μνλ λλ‘ useEffect ν
μμ μλμΌλ‘ λ§μ΄κ·Έλ μ΄μ
μ μ€νν μ μμ΅λλ€.
import { drizzle } from "drizzle-orm/op-sqlite";
import { open } from '@op-engineering/op-sqlite';
import { useMigrations } from 'drizzle-orm/op-sqlite/migrator';
import migrations from './drizzle/migrations';
const opsqliteDb = open({
name: 'myDB',
});
const db = drizzle(opsqliteDb);
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;
}