Skip to content

Commit 6367cf4

Browse files
authored
Merge pull request #423 from cosentino/ionic-react-typeorm-example
Ionic react typeorm example
2 parents a7f14ad + 7d33917 commit 6367cf4

File tree

2 files changed

+121
-2
lines changed

2 files changed

+121
-2
lines changed

README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
<a href="https://www.npmjs.com/package/@capacitor-community/sqlite"><img src="https://img.shields.io/npm/dw/@capacitor-community/sqlite?style=flat-square" /></a>
1717
<a href="https://www.npmjs.com/package/@capacitor-community/sqlite"><img src="https://img.shields.io/npm/v/@capacitor-community/sqlite?style=flat-square" /></a>
1818
<!-- ALL-CONTRIBUTORS-BADGE:START - Do not remove or modify this section -->
19-
<a href="#contributors-"><img src="https://img.shields.io/badge/all%20contributors-35-orange?style=flat-square" /></a>
19+
<a href="#contributors-"><img src="https://img.shields.io/badge/all%20contributors-36-orange?style=flat-square" /></a>
2020
<!-- ALL-CONTRIBUTORS-BADGE:END -->
2121
</p>
2222

@@ -279,6 +279,10 @@ npm install --save [email protected]
279279

280280
- [react-vite-sqlite-app](https://github.com/jepiqueau/react-vite-sqlite-app)
281281

282+
### Ionic/React Capacitor SQLite + TypeORM Example App
283+
284+
- [capacitor-sqlite-react-typeorm-app](https://github.com/cosentino/capacitor-sqlite-react-typeorm-app)
285+
282286
### Ionic/Vue
283287

284288
- [Ionic/Vue Usage Documentation](https://github.com/capacitor-community/sqlite/blob/master/docs/Ionic-Vue-Usage.md)
@@ -352,6 +356,7 @@ Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/d
352356
<a href="https://github.com/folsze" title="folsze"><img src="https://github.com/folsze.png?size=100" width="50" height="50" /></a>
353357
<a href="https://github.com/pranav-singhal" title="pranav-singhal"><img src="https://github.com/pranav-singhal.png?size=100" width="50" height="50" /></a>
354358
<a href="https://github.com/beligatclement" title="beligatclement"><img src="https://github.com/beligatclement.png?size=100" width="50" height="50" /></a>
359+
<a href="https://github.com/cosentino" title="cosentino"><img src="https://avatars.githubusercontent.com/u/376903?s=48&v=4" width="50" height="50" /></a>
355360
</p>
356361

357362

docs/TypeORM-Usage.md

Lines changed: 115 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ module.exports = {
7272

7373
## Ionic/Angular app
7474

75-
An example of a Ionic/Vue app has been developed https://github.com/jepiqueau/ionic-sqlite-typeorm-app demonstrating the use of TypeORM with migrations. The migration files have been created manually, not using the TypeORM cli ( generate, create ). If one find the way of using the TypeORM cli he or she will be welcome to make a PR to this documentation and make a PR to the application.
75+
An example of a Ionic/Angular app has been developed https://github.com/jepiqueau/ionic-sqlite-typeorm-app demonstrating the use of TypeORM with migrations. The migration files have been created manually, not using the TypeORM cli ( generate, create ). If one find the way of using the TypeORM cli he or she will be welcome to make a PR to this documentation and make a PR to the application.
7676

7777
### Requirements
7878

@@ -153,4 +153,118 @@ An example of a Ionic/Vue app has been developed https://github.com/jepiqueau/io
153153
},
154154
```
155155

156+
## Ionic/React app
157+
158+
An example of a Ionic/React app has been developed https://github.com/cosentino/capacitor-sqlite-react-typeorm-app demonstrating the use of TypeORM with migrations. The migration files have been created manually, not using the TypeORM cli ( generate, create ). If one find the way of using the TypeORM cli he or she will be welcome to make a PR to this documentation and make a PR to the application.
159+
160+
### Requirements
161+
162+
Since Create React App does not allow to tweek the react project compilation as required by TypeORM.
163+
We thus need to replace react-script with a tool such as [CRACO](https://craco.js.org/) and then override the default configurations (note: ejecting CRA is a viable solution).
164+
CRACO will allow us to customize the default CRA Webpack configuration.
165+
166+
#### 1. Install the latest version of the package from npm as a dev dependency
167+
168+
```bash
169+
npm i -D @craco/craco craco-swc
170+
```
171+
172+
#### 2. Create a CRACO configuration file in your project's root directory and configure
173+
174+
Create the files craco.config.js and .swcrc the files in the project root.
175+
The following will tell CRACO to:
176+
177+
- force the minimizer (Terser) settings "keep_classnames" and "keep_fnames" to true
178+
- use SWC instead of Babel as transpiler
179+
- configure SWC parser and tranformer so that uses "legacyDecorator" and "decoratorMetadata"
180+
181+
craco.config.js:
182+
183+
```js
184+
const CracoSwcPlugin = require('craco-swc');
185+
186+
module.exports = {
187+
plugins: [
188+
{
189+
plugin: CracoSwcPlugin, // see .swcrc for SWC configuration (that will replace Babel)
190+
},
191+
{
192+
plugin: {
193+
overrideWebpackConfig: ({ webpackConfig }) => {
194+
const terser = webpackConfig?.optimization?.minimizer?.find(x => x.options.minimizer);
195+
if (terser) {
196+
terser.options.minimizer.options = {
197+
...terser.options.minimizer.options,
198+
keep_classnames: true,
199+
keep_fnames: true,
200+
}
201+
}
202+
return webpackConfig;
203+
}
204+
}
205+
}
206+
],
207+
};
208+
```
209+
210+
.swcrc:
211+
212+
```json
213+
{
214+
"$schema": "https://json.schemastore.org/swcrc",
215+
"jsc": {
216+
"externalHelpers": true,
217+
"target": "es5",
218+
"preserveAllComments": true,
219+
"parser": {
220+
"syntax": "typescript",
221+
"tsx": true,
222+
"dynamicImport": true,
223+
"decorators": true
224+
},
225+
"transform": {
226+
"legacyDecorator": true,
227+
"decoratorMetadata": true
228+
}
229+
}
230+
}
231+
```
232+
233+
#### 3. Update build commands to use craco CLI
234+
235+
In particular in the :
236+
237+
- Update existing calls to react-scripts in the scripts section of your package.json to use the craco CLI.
238+
- Override the ionic build​ command to use craco cli (see [https://ionicframework.com/docs/cli/configuration#overriding-the-build]).
239+
240+
```json
241+
"scripts": {
242+
"start": "npm run copysqlwasm && craco start",
243+
"build": "npm run copysqlwasm && craco build",
244+
"copysqlwasm": "copyfiles -u 3 node_modules/sql.js/dist/sql-wasm.wasm public/assets",
245+
"ionic:build": "npm run build",
246+
"ionic:serve": "npm run start"
247+
},
248+
```
249+
250+
#### 4. Add the following to tsconfig.json
251+
252+
```json
253+
"strictPropertyInitialization": false,
254+
"experimentalDecorators": true,
255+
"emitDecoratorMetadata": true,
256+
```
257+
258+
#### 5. Install reflect-metadata package
259+
260+
```bash
261+
npm i reflect-metadata
262+
```
263+
264+
#### 6. import 'reflect-metadata' once, before any typeorm entity import, for example add the following to the ./src/index.tsx
265+
266+
```ts
267+
import "reflect-metadata";
268+
import React from 'react';
269+
```
156270

0 commit comments

Comments
 (0)