|
| 1 | +# Pre-populated FS |
| 2 | + |
| 3 | +A pre-populated FS that you can use instead of letting initdb run (which is the default). This can lead to faster startup times because initdb doesn't need to run. |
| 4 | + |
| 5 | +This package contains an archive as a static asset that you can access through the `dataDir()` function. |
| 6 | + |
| 7 | +:::info |
| 8 | +The prepopulated FS is created during build on our CI and therefore guaranteed to work only for the corresponding version of PGlite from which it was created. If you encounter issues, make sure this package is up to date with your PGlite version. |
| 9 | +::: |
| 10 | + |
| 11 | +## Installation |
| 12 | + |
| 13 | +```bash |
| 14 | +npm install @electric-sql/pglite-prepopulatedfs |
| 15 | +# or |
| 16 | +yarn add @electric-sql/pglite-prepopulatedfs |
| 17 | +# or |
| 18 | +pnpm add @electric-sql/pglite-prepopulatedfs |
| 19 | +``` |
| 20 | + |
| 21 | +## Usage |
| 22 | + |
| 23 | +```typescript |
| 24 | +import { PGlite } from '@electric-sql/pglite' |
| 25 | +import { dataDir } from '@electric-sql/pglite-prepopulatedfs' |
| 26 | + |
| 27 | +// Create a PGlite instance with the prepopulated FS |
| 28 | +const pg = await PGlite.create({ |
| 29 | + loadDataDir: await dataDir(), |
| 30 | +}) |
| 31 | +``` |
| 32 | + |
| 33 | +As an example, this is useful when you have multiple test, each with its own PGlite instance. Consider the following usage with vitest: |
| 34 | + |
| 35 | +```typescript |
| 36 | +import { describe, it, expect, beforeEach } from 'vitest' |
| 37 | +import { PGlite } from '@electric-sql/pglite' |
| 38 | +import { dataDir } from '@electric-sql/pglite-prepopulatedfs' |
| 39 | + |
| 40 | +describe('query and exec with different data sizes', () => { |
| 41 | + let pg: PGlite |
| 42 | + |
| 43 | + beforeEach(async () => { |
| 44 | + pg = await PGlite.create({ |
| 45 | + loadDataDir: await dataDir(), |
| 46 | + }) |
| 47 | + |
| 48 | + await pg.exec(` |
| 49 | + // setup default data |
| 50 | + `) |
| 51 | + }) |
| 52 | + |
| 53 | + describe('test no. 1', () => { |
| 54 | + ... |
| 55 | + }) |
| 56 | + |
| 57 | + describe('test no. 2', () => { |
| 58 | + ... |
| 59 | + }) |
| 60 | + |
| 61 | + // many more tests here |
| 62 | +}) |
| 63 | +``` |
| 64 | + |
| 65 | +Although more bandwidth is needed to download the `@electric-sql/pglite-prepopulatedfs` package, the tests will run faster as PGlite doesn't need to run `initdb` for each one of them. |
| 66 | + |
| 67 | +The same applies if your application needs to instantiate PGlite over and over again with a clean slate. You will initialy use more bandwidth but will save on speed in the long-run. |
| 68 | + |
| 69 | +## Benchmarking |
| 70 | + |
| 71 | +A simple benchmarking is done as part of our automated testing in `packages/pglite-prepopulatedfs/tests/prepopulatedfs.test.ts`. |
| 72 | + |
| 73 | +Here is a sample output on an Apple M1: |
| 74 | + |
| 75 | +``` |
| 76 | +initdb duration: prepopulated avg (trimmed) 263.38 ms vs. classic initdb 886.29 ms. |
| 77 | +Speedup: 3.37x |
| 78 | +``` |
0 commit comments