@@ -260,30 +260,67 @@ import { open } from 'react-native-nitro-sqlite';
260260try {
261261 const db = open ({ name: ' myDb.sqlite' });
262262
263- // Platform-specific extension paths
264- const extensionPath = Platform .OS === ' ios'
265- ? ' path/to/extension.dylib'
266- : ' path/to/extension.so' ;
267-
268263 // Load the extension
269- const success = db .loadExtension (extensionPath );
264+ db .loadExtension (' /path/to/extension ' );
270265
271- if (success ) {
272- // Use the extension's functionality
273- const result = db .execute (' SELECT extension_function(?) as result' , [someValue ]);
274- console .log (' Result:' , result .rows ._array [0 ].result );
275- }
266+ // Now you can use the extension's functionality
267+ const result = db .execute (' SELECT extension_function(?) as result' , [someValue ]);
276268} catch (e ) {
277269 console .error (' Failed to load extension:' , e .message );
278270}
279271```
280272
281- Notes about extensions:
282- - Extensions must be compiled specifically for each platform (iOS, Android)
283- - The extension path should be accessible from your app's bundle
284- - Extensions are useful for adding mathematical functions, encryption, full-text search, and more
285- - For iOS, extensions need to be included in the app bundle
286- - For Android, extensions should be in the app's native libs directory
273+ ### Setting up extensions in your project
274+
275+ To use SQLite extensions with your app, you need to:
276+
277+ #### Android
278+ 1 . Place extension files (` .so ` files) in your app's assets folder
279+ 2 . Add these files to your Android project
280+ 3 . At runtime, copy the file from assets to a readable location before loading
281+
282+ #### iOS
283+ 1 . Add extension files (` .dylib ` files) to your Xcode project
284+ 2 . Make sure they're included in "Build Phases → Copy Bundle Resources"
285+ 3 . At runtime, the files will be available in your app's bundle
286+
287+ ### Implementation example
288+
289+ ``` typescript
290+ async loadExtension (): Promise < void > {
291+ try {
292+ const appDir = getAppDirectory ();
293+
294+ if (Platform .OS === ' android' ) {
295+ const libFileName = ` libextension.so ` ;
296+ const destinationPath = ` ${appDir }/${libFileName } ` ;
297+
298+ // Copy from assets to app directory then load
299+ try {
300+ await RNFS .copyFileAssets (libFileName , destinationPath );
301+ this .db .loadExtension (` ${appDir }/libextension ` );
302+ } catch (error ) {
303+ console .error (' Failed to copy or load extension:' , error );
304+ }
305+ } else if (Platform .OS === ' ios' ) {
306+ const sourcePath = ` ${RNFS .MainBundlePath }/extension.dylib ` ;
307+ const destPath = ` ${appDir }/libextension.dylib ` ;
308+
309+ // Copy and load
310+ try {
311+ await RNFS .copyFile (sourcePath , destPath );
312+ this .db .loadExtension (` ${appDir }/libextension ` );
313+ } catch (error ) {
314+ console .error (' Failed to copy or load extension:' , error );
315+ }
316+ }
317+ } catch (error) {
318+ console.error(' Failed to load SQLite extension:' , error);
319+ }
320+ }
321+ ```
322+
323+ You'll need the ` react-native-fs ` package for this implementation.
287324
288325References: [ Attach] ( https://www.sqlite.org/lang_attach.html ) - [ Detach] ( https://www.sqlite.org/lang_detach.html )
289326
0 commit comments