Data Model / Architecture with database

I have a programming issue. I have an app where every page presents somehow a list of students.

The page where the students are defined looks like this:

@Component({
  selector: 'page-students',
  templateUrl: 'students.html'
})
export class StudentsPage {
  studentNames: Array<{ id: string, inClass: string, name: string }>;

  constructor(public navCtrl: NavController, public uuid: IDGenerator, public popoverCtrl: PopoverController/*public storage: Storage*/) {
    // in the end, this can be done by a new class Student / StudentList and JSON Stringify
    this.studentNames = [];
  }

  public addStudent(event) {
    this.studentNames.push({ id: this.uuid.generate(), inClass: '8c', name: 'My Name' });
  }

  public changeStudent(name: string, arrayIndex: number) {
    let id = this.studentNames[arrayIndex].id;
  }

  public removeStudent(event, arrayIndex: number) {
    let id = this.studentNames[arrayIndex].id
    this.studentNames.splice(arrayIndex, 1);
  }

  public presentPopover(myEvent, studentName, index) {
    let popover = this.popoverCtrl.create(PopoverStudents, { studentName, index });
    popover.present({
      ev: myEvent
    });

    /*popover.onDidDismiss((popoverData) => {
      let selectedStudent = popoverData;
      let selectedIndex = 1;
      this.changeStudent(selectedStudent.name, selectedIndex)
    })*/
  }
}

On every page I define a list studentNames which I will load from the sqlite database (to be done).

Is this good style or would it be better to define a studentList list and parse this then in this class to the database if something is changed?

If that is so, how can I provide the same class to all pages? Thanks for help!

In my opinion, pages should never directly interact with either storage or http. That is the domain of service providers.

1 Like

Ok but how can I do this then? Do you have an example?

storage is a kind of provider in ma case. It’s stored under providers/db-provider.ts and instanciates an sqlite object that do the sql queries. Is this kind of what you meant or am I doing it wrong?

You can see the code on github if you like: https://github.com/anderl80/ionic-teacherapp please see the develop branch.

Have an injectable provider for the students with the various calls to the backend whether that be SQLite or whatever.

1 Like

I would recommend using the uuid package for generating UUIDs instead of that homebrew you have.

I would also centralize the opening of the database, instead of doing it in every single access method.

1 Like

Thanks for the recommendation!

You mean an object that has methods like new studentList.add(Student)? That would imply a class students and a class studentContaier or studentList.

Or should this injectable object be a student?

The student itself is not injectable, but the service of StudentProvider.add() should be.

Hi. I’ve downloaded your version from GitHub.

Give me a little while and I’ll do a mini framework version with the providers.

1 Like

Hi Anderl80,

As promised - here’s a quick project showing the use of layers where you have a couple of different entity types using the same underlying code - and shared across several pages.
It clearly shows you what you should expect to be injectable and what shouldn’t be - along with how to store and retrieve.

Let me know how you get on.

2 Likes

Awesome! I’ll have a look at it, many thanks!

You just blew my mind!

Cheers man :slight_smile:

100% forgot I wrote that!!