1.0.2 • Published 5 years ago

ngpa-repository v1.0.2

Weekly downloads
8
License
MIT
Repository
github
Last release
5 years ago

Angular Persistent Api Repository (ngpa-repository) Electron+Angular App

Angular Persistent API ngpa-repository is similar to
Springboot JPA repository where we can define our custom
repo with entity class as generic type and all of the
default available crud functions can be directly used.

1. Package dependencies which needs to be installed

npm install nedb

npm install ngx-electron

2. Electron Configuration main.js

electron = require("electron");
const { app, BrowserWindow } = electron;
let nedbDatabase = require("nedb");

global.ngpa_provider = {
  nedb: nedbDatabase
};

Above settings are required in main.js else neDb will be creating the tables in the in-memory-database instead of file based database.

This issue is with Angular CLI as it doesn't allow runtime creation of files using nedb, hence above configuration needs to be done.

3. Create a model class with a decorator Database

import { Database } from "ngpa-repository";

@Database("employee")
export class Employee {
  _id: string; // need to give _id as the neDb expects an id as _id
  firstName: string;
  lastName: string;
  designation: string;
}

4. Create a repo for above model class extending NeDBRepository and implement the returnEntityInstance method like below

import { NeDBRepository } from "ngpa-repository";
import { Employee } from "../model/employee.model";

export class EmployeeRepo extends NeDBRepository<Employee> {
  // this is an abstract implementation which needs to be given
  returnEntityInstance(): Employee {
    return new Employee();
  }
}

5. Import NgpaRepositoryModule in app.module

import { NgModule } from "@angular/core";
import { AppComponent } from "./app.component";
import { NgpaRepositoryModule } from "ngpa-repository";

@NgModule({
  declarations: [AppComponent],
  imports: [NgpaRepositoryModule],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule {}

6. How to use it in any angular component or service

import { Component, OnInit } from '@angular/core';
import { EmployeeRepo } from './employee/repo/employee.repo';
import { Employee } from './employee/model/employee.model';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {
  constructor(private employeeRepo:EmployeeRepo){}

  ngOnInit(){
    let employee:Employee = new Employee();
    employee.firstName = "John";
    employee.lastName = "Doe";
    employee.designation = "CEO";
    this.employeeRepo.save(employee);
  }
}

when the above ngOnInit code executes a folder named ngpa-data will be created at the root path.

which will have a subfolder named nedb which in turn will have two subfolders named config and database.

config folder contains nedb.config.json which is generated as default config.

database folder is where the real data recides post save with file named as whatever given in the entity Database decorators value.

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago

0.0.1

5 years ago