You can use TypeORM in connection with the cordova-sqlite-storage
plugin in your Ionic app.
This project demonstrates how that would work.
To support webpack builds outside of Ionic we had to remove the automatic selection of the correct TypeORM version (the typeorm
package comes with a Node and a browser version). In order to keep using TypeORM with Ionic you have to create a custom webpack.config.js
file. This example contains one that is identical to the one Ionic uses when no config file is specified but adds the NormalModuleReplacementPlugin
to select the correct version.
If you already have a custom webpack config file you have to add these lines to your plugins (for both development and production):
plugins: [
...,
new webpack.NormalModuleReplacementPlugin(/typeorm$/, function (result) {
result.request = result.request.replace(/typeorm/, "typeorm/browser");
})
]
If you don't use a custom wepack config, copy the one from this example and add it to your package.json
under config
:
"config": {
"ionic_webpack": "./config/webpack.config.js"
}
- Install the ionic and cordova cli:
npm install -g cordova ionic
- Install all dependencies:
npm install
- Add a platform:
ionic cordova platform add <ios | android>
- Run the app:
ionic cordova run <ios | android>
. If you need help, you can read ionic's guide for running an app on your device
- Install the plugin:
ionic cordova plugin add cordova-sqlite-storage --save
- Install TypeORM:
npm install typeorm --save
- Install node.js-Types:
npm install @types/node --save-dev
- Add
"typeRoots": ["node_modules/@types"]
to yourtsconfig.json
undercompilerOptions
- Create a custom webpack config file like the one included in this project to use the correct TypeORM version and add the config file to your
package.json
(Required with TypeORM >= 0.1.8)
Since Ionic make a lot of optimizations when building for productions, the following limitations occur
- Entities have to be marked with the table name (eg
@Entity('table_name')
) getRepository()
has to be called with the name of the entity instead of the class (eggetRepository('post') as Repository<Post>
)- Date fields aren't supported
@Column()
birthdate: Date;