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.
Merge pull request #1 from Mike-FreeAI/add-mobile-support-1
Add mobile support electric-sql#2
- Loading branch information
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) | ||
}) |