How to use a database


so I read quit a lot but I just don’t get how I use a database properly.

I know there’s storage and SQLite, but I can’t find anything about it except the rather poorly documentation.

I also think, a lot of people just copy and paste code around, I’m interested in an articel which explains what design pattern we are using to implement ist etc. etc. and make a nice tutorial for ionic 3. Most of what I find seems outdated.

Are there any good sources on how to use an database like SQLite with Ionic 3?

In general just use the ionic storage plugin. It uses sqlite on phones and local storage in a browser. This means you get dagger data storage on a phone while also letting the app still work in the browser. It stores everything in json, which is almost certainly what you want anyway.

so are you suggesting I should fake a relational database using e.g. JSON.stringify + ionic storage? That just sounds bad. I’m just used to the nice absraction of ORMs and what not. Isn’t there something like that?Building my database using what I said above will work but it might just suck.

The storage from Ionic is just stupid key value right? So no “real” relations and joins and stuff, right?

Is there some simple ORM for this?

I don’t need browser support.

ANd how would I make it persistent? I need persistent data storage. Just a normal simple relational database would be fine.Sure I can fake it with the “normal” storage but still: how do I make it persistent? Would I have to even do the saving myself? Like safe it to a file and read it again? That just does sound like an horrible solution if there are databases already made for all these things.

There must be a proper source on how to just use sqlite.

Yep, so that’s a good point, if you are doing a lot of relational data it’s probably best to use SQLite. And if you don’t care about testing in a browser then even better, because that won’t be a factor for you.

As far as persistence, I was only bringing it up because localstorage is not safe on mobile, as the OS may delete it when it detects the users phone storage is getting full, yet it won’t delete SQLite. This makes the ionic storage module really handy for simpler storage because it handles all that for you and you just save and load json. In your case though as you say probably not a good idea.

So then yes, just use the SQLite plugin with Ionic Native and you’ll be good to go! The regular ionic native docs explain how to use it, there’s not much to it, just write your sql queries.

Unfortunately things like creating and keeping the database updated is up to you. In the past I created a database creation service that handles initialization and modification of all tables and had it run on app startup. I don’t know of any migration frameworks for sqlite, so I believe you just have to handle it yourself. Generally I just don’t store that much data on the device, instead it’s mostly on a server, so my SQLite stuff has been lightweight enough to just handle it myself.

Regarding an ORM, regardless of what tool you use you will get javascript objects back…it’s javascript after all. But, the good new is it’s also TypeScript :slight_smile: Meaning when you load your data you can give it a type of whatever your data model is. So, definitely not a full ORM, but, between setting models and having a good service layer it should be close enough :slight_smile:

I have a few relations, so fakign them would be okay. But still, persistency is very important. So I guess I’ll go with sqlite. Is it normal, that it doesn’t work on the emulator? I know it doesnt in the browser, but what about the emulator?

Yeah I might look into

It works fine in the emulator yes. Everything works fine in the emulator, since it emulates the full OS. The only limitations of the emulator are interactions (i.e., camera, gps location), but even those can be faked for testing purposes.

I would be way too scared to use that package in a production app. If you only have a few relations like you say I think you’d be much better off handling it yourself with a service. I just wouldn’t trust my project to that, as they even say in the description:

Ionic-ORM & TypeORM is in active development. Don’t be frustrated with bugs if you face them. If you notice bug or have something not working please report an issue, we’ll try to fix it as soon as possible.

But, that’s your call of course :slight_smile:

EDIT: Regarding your comment on persistence being important, that is a non-factor in this choice. Both Ionic Storage and SQLite both use SQLite and are both equally safe for persistence. The difference is Ionic Storage automatically switches between localstorage on a browser and SQLite on a phone, and doesn’t allow relations since the SQLite is abstracted away.