|
| 1 | +# Example: PGlite + Vite + React + TypeScript |
| 2 | + |
| 3 | +This is an example of a simple project using [PGlite](https://pglite.dev) and it's [React integration](https://pglite.dev/docs/framework-hooks/react). It uses [Vite](https://vite.dev), [React](https://react.dev/) and [TypeScript](https://www.typescriptlang.org/). |
| 4 | + |
| 5 | +This example demonstrates the usage of some of PGlite's React API: [PGliteProvider](https://pglite.dev/docs/framework-hooks/react#pgliteprovider), [usePGlite](https://pglite.dev/docs/framework-hooks/react#usepglite), [useLiveQuery](https://pglite.dev/docs/framework-hooks/react#uselivequery). |
| 6 | + |
| 7 | +On page load, a database is created with a single table. On pressing the button, a new row is inserted into the database. The `useLiveQuery` will watch for any changes and display the most recently inserted 5 rows. |
| 8 | + |
| 9 | +# Getting started with this example |
| 10 | + |
| 11 | +## Prerequisites |
| 12 | +You need [node](https://nodejs.org/en/download) (at least version 18), [pnpm](https://pnpm.io/installation) and [git](https://git-scm.com/downloads) installed. |
| 13 | + |
| 14 | +Check node version |
| 15 | +``` |
| 16 | +$ node --version |
| 17 | +``` |
| 18 | +Sample output: `v20.9.0` |
| 19 | + |
| 20 | +Check pnpm version |
| 21 | +``` |
| 22 | +$ pnpm --version |
| 23 | +``` |
| 24 | +Sample output: `9.15.3` |
| 25 | + |
| 26 | +Check git version |
| 27 | +``` |
| 28 | +$ git --version |
| 29 | +``` |
| 30 | +Sample output: `git version 2.34.1` |
| 31 | + |
| 32 | +## Install and run the example locally |
| 33 | + |
| 34 | +This example depends on `PGlite` packages released on npmjs.com, so you don't need to build the entire `PGlite` project in order to run the example. |
| 35 | + |
| 36 | +1. Get the code from GitHub. The example is in `PGlite`'s main repository |
| 37 | +``` |
| 38 | +$ git clone https://github.com/electric-sql/pglite |
| 39 | +``` |
| 40 | +2. Navigate to this example's directory |
| 41 | +``` |
| 42 | +$ cd ./pglite/examples/react |
| 43 | +``` |
| 44 | +3. Install dependencies |
| 45 | + |
| 46 | +``` |
| 47 | +$ pnpm i --ignore-workspace |
| 48 | +``` |
| 49 | + |
| 50 | +This example is part of the `pglite` pnpm workspace, but for our needs, we do not need to install all dependencies in the workspace. Thus passing the `--ignore-workspace` flag. |
| 51 | + |
| 52 | +4. Start a development server locally |
| 53 | +``` |
| 54 | +$ pnpm dev |
| 55 | +``` |
| 56 | +Sample output: |
| 57 | +``` |
| 58 | + VITE v6.1.0 ready in 126 ms |
| 59 | +
|
| 60 | + ➜ Local: http://localhost:5173/ |
| 61 | + ➜ Network: use --host to expose |
| 62 | + ➜ press h + enter to show help |
| 63 | +``` |
| 64 | + |
| 65 | +5. Open your browser and point to the above indicated address (http://localhost:5173/ but your address might be different) |
| 66 | + |
| 67 | +# Getting started with PGlite + Vite + React + Typescript |
| 68 | + |
| 69 | +If you'd like to start from scratch, here is how you can reproduce this example, so you understand better how it was made: |
| 70 | + |
| 71 | +1. Install [node](https://nodejs.org/en/download), [pnpm](https://pnpm.io/installation) and [git](https://git-scm.com/downloads) |
| 72 | +2. Run `pnpm create vite`. Follow the steps, input the name of your project (eg pglite-vite-react-project), selecte `React` for framework, `TypeScript` as variant. |
| 73 | + |
| 74 | +Sample output: |
| 75 | + |
| 76 | +``` |
| 77 | +$ pnpm create vite |
| 78 | +.../19522a4053e-20b11 | +1 + |
| 79 | +.../19522a4053e-20b11 | Progress: resolved 1, reused 1, downloaded 0, added 1, done |
| 80 | +✔ Project name: … pglite-vite-react-project |
| 81 | +✔ Select a framework: › React |
| 82 | +✔ Select a variant: › TypeScript |
| 83 | +
|
| 84 | +Scaffolding project in /tmp/pglite-vite-react-project... |
| 85 | +
|
| 86 | +Done. Now run: |
| 87 | +
|
| 88 | + cd pglite-vite-react-project |
| 89 | + pnpm install |
| 90 | + pnpm run dev |
| 91 | +``` |
| 92 | + |
| 93 | +3. Run the steps above: |
| 94 | + |
| 95 | +``` |
| 96 | + $ cd pglite-vite-react-project |
| 97 | + $ pnpm install |
| 98 | + $ pnpm run dev |
| 99 | +
|
| 100 | +``` |
| 101 | + |
| 102 | +4. The last command will start an http server on your local machine. Point your browser to the indicated address: |
| 103 | + |
| 104 | +``` |
| 105 | +$ pnpm run dev |
| 106 | + VITE v6.1.0 ready in 126 ms |
| 107 | +
|
| 108 | + ➜ Local: http://localhost:5173/ |
| 109 | + ➜ Network: use --host to expose |
| 110 | + ➜ press h + enter to show help |
| 111 | +``` |
| 112 | + |
| 113 | +4. Install [@electric-sql/pglite](https://www.npmjs.com/package/@electric-sql/pglite) and [@electric-sql/pglite-react](https://www.npmjs.com/package/@electric-sql/pglite-react) npm packages: |
| 114 | + |
| 115 | +``` |
| 116 | +$ pnpm install @electric-sql/pglite @electric-sql/pglite-react |
| 117 | +
|
| 118 | +Packages: +2 |
| 119 | +++ |
| 120 | +Downloading @electric-sql/pglite@0.2.17: 9.99 MB/9.99 MB, done |
| 121 | +Progress: resolved 224, reused 180, downloaded 2, added 2, done |
| 122 | +
|
| 123 | +dependencies: |
| 124 | ++ @electric-sql/pglite 0.2.17 |
| 125 | ++ @electric-sql/pglite-react 0.2.17 |
| 126 | +
|
| 127 | +``` |
| 128 | + |
| 129 | +5. You are ready start using PGlite's React API. |
| 130 | +Make sure to exclude pglite from dependency optimization using the [optimizeDeps option inside vite.config.js](https://pglite.dev/docs/bundler-support#vite). |
| 131 | + |
| 132 | +6. Have a look at this project's `App.tsx`, `MyPGliteComponent.tsx` and `MyPGliteItemsComponent.tsx` to see how you can create a database and use PGlite React's integration and start modifying your project. Switch to the browser to see your changes. |
| 133 | + |
| 134 | + |
0 commit comments