forked from electric-sql/pglite
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for mobile filesystem using Expo FileSystem API. * **Add MobileFS class:** * Create `mobilefs.ts` file in `packages/pglite/src/fs/`. * Implement `MobileFS` class extending `FilesystemBase`. * Implement methods for reading, writing, and deleting files using Expo FileSystem API. * **Update fs index:** * Import `MobileFS` class in `packages/pglite/src/fs/index.ts`. * Add case for `mobilefs` in `parseDataDir` function. * Add case for `mobilefs` in `loadFs` function. * **Add tests for MobileFS:** * Create `mobile-fs.test.js` in `packages/pglite/tests/targets/`. * Write tests for reading, writing, and deleting files using MobileFS and Expo FileSystem API.
- Loading branch information
1 parent
dd698b7
commit 48a62f2
Showing
3 changed files
with
101 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import * as FileSystem from 'expo-file-system'; | ||
import { FilesystemBase } from './types.js'; | ||
import type { PostgresMod, FS } from '../postgresMod.js'; | ||
import { dumpTar } from './tarUtils.js'; | ||
|
||
export class MobileFS extends FilesystemBase { | ||
async emscriptenOpts(opts: Partial<PostgresMod>) { | ||
const options: Partial<PostgresMod> = { | ||
...opts, | ||
preRun: [ | ||
...(opts.preRun || []), | ||
(mod: any) => { | ||
mod.FS.mkdir('/mobilefs'); | ||
mod.FS.mount(mod.FS.filesystems.NODEFS, { root: this.dataDir }, '/mobilefs'); | ||
}, | ||
], | ||
}; | ||
return options; | ||
} | ||
|
||
async dumpTar(mod: FS, dbname: string) { | ||
return dumpTar(mod, dbname); | ||
} | ||
|
||
async close(FS: FS): Promise<void> { | ||
FS.quit(); | ||
} | ||
|
||
async readFile(path: string): Promise<string> { | ||
return await FileSystem.readAsStringAsync(path); | ||
} | ||
|
||
async writeFile(path: string, contents: string): Promise<void> { | ||
await FileSystem.writeAsStringAsync(path, contents); | ||
} | ||
|
||
async deleteFile(path: string): Promise<void> { | ||
await FileSystem.deleteAsync(path); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import test from '../polytest.js' | ||
import { PGlite } from '../../dist/index.js' | ||
import { MobileFS } from '../../dist/fs/mobilefs.js' | ||
import * as FileSystem from 'expo-file-system' | ||
|
||
test('MobileFS read, write, delete', async (t) => { | ||
const db = new PGlite({ | ||
fs: new MobileFS(FileSystem.documentDirectory + 'pgdata'), | ||
}) | ||
|
||
await db.exec(` | ||
CREATE TABLE IF NOT EXISTS test ( | ||
id SERIAL PRIMARY KEY, | ||
name TEXT | ||
); | ||
`) | ||
|
||
await db.exec("INSERT INTO test (name) VALUES ('test');") | ||
|
||
const res = await db.query(` | ||
SELECT * FROM test; | ||
`) | ||
|
||
t.deepEqual(res, { | ||
rows: [ | ||
{ | ||
id: 1, | ||
name: 'test', | ||
}, | ||
], | ||
fields: [ | ||
{ | ||
name: 'id', | ||
dataTypeID: 23, | ||
}, | ||
{ | ||
name: 'name', | ||
dataTypeID: 25, | ||
}, | ||
], | ||
affectedRows: 0, | ||
}) | ||
|
||
// Test reading file | ||
const filePath = FileSystem.documentDirectory + 'pgdata/test.txt' | ||
await FileSystem.writeAsStringAsync(filePath, 'Hello, world!') | ||
const fileContent = await FileSystem.readAsStringAsync(filePath) | ||
t.is(fileContent, 'Hello, world!') | ||
|
||
// Test deleting file | ||
await FileSystem.deleteAsync(filePath) | ||
const fileExists = await FileSystem.getInfoAsync(filePath) | ||
t.false(fileExists.exists) | ||
}) |