Skip to content
This repository has been archived by the owner on Mar 9, 2022. It is now read-only.

Add Encryption #48

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 12 additions & 2 deletions couchbase.android.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,21 @@ export class Couchbase {
private manager: any;
private database: any;

public constructor(databaseName: string) {

public constructor(databaseName: string, encryptionKey?:string) {
this.context = utils.ad.getApplicationContext();
try {
this.manager = new com.couchbase.lite.Manager(new com.couchbase.lite.android.AndroidContext(this.context), null);
this.database = this.manager.getDatabase(databaseName);
if(!encryptionKey){
this.database = this.manager.getDatabase(databaseName);
}else{
let databaseOptions:any = new com.couchbase.lite.DatabaseOptions();
databaseOptions.setCreate(true);
databaseOptions.setEncryptionKey(encryptionKey);
this.database = this.manager.openDatabase(databaseName, databaseOptions);
}


} catch (exception) {
console.error("MANAGER ERROR:", exception.message);
}
Expand Down
4 changes: 3 additions & 1 deletion couchbase.d.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
declare module "nativescript-couchbase" {

export class Couchbase {
constructor(databaseName: string);
constructor(databaseName: string, encryptionKey?: string);
createDocument(data: Object, documentId?: string);
getDocument(documentId: string);
updateDocument(documentId: string, data: any);
deleteDocument(documentId: string);
destroyDatabase();
closeDatabase():boolean;
openDatabase(name: string, key?: string);
createView(viewName: string, viewRevision: string, callback: any);
executeQuery(viewName: string, options?: any);
createPullReplication(remoteUrl: string);
Expand Down
15 changes: 12 additions & 3 deletions couchbase.ios.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,19 @@ export class Couchbase {
private manager: any;
private database: any;

constructor(databaseName: String){
constructor(databaseName: String, encryptionKey?:string){
this.manager = CBLManager.sharedInstance();
if (!this.manager){
console.log("MANAGER ERROR:Can not create share instance of CBLManager");
}
var errorRef = new interop.Reference();

this.database = this.manager.databaseNamedError(databaseName, errorRef);
if(!encryptionKey){
this.database = this.manager.databaseNamedError(databaseName, errorRef);
} else {
this.database = this.manager.registerEncryptionKeyForDatabaseNamed(databaseName, encryptionKey);
this.database = this.manager.databaseNamedError(databaseName, errorRef);
}


if (!this.database){
console.log(errorRef.value);
Expand Down Expand Up @@ -190,6 +195,10 @@ export class Couchbase {
}
}

closeDatabase(){
return this.database.close();
}

private mapToJson(properties: Object){
var errorRef = new interop.Reference();
var result = "";
Expand Down
7 changes: 5 additions & 2 deletions ng-demo/app/app.routing.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import { Routes } from "@angular/router";
import { ListComponent } from "./components/list/list.component";
import { CreateComponent } from "./components/create/create.component";
import { EncryptionComponent } from "./components/encryption/encryption.component";

export const appRoutes: Routes = [
{ path: '', component: ListComponent },
{ path: "create", component: CreateComponent }
{ path: "create", component: CreateComponent },
{ path: "encryption", component: EncryptionComponent }
];

export const appComponents: any = [
ListComponent,
CreateComponent
CreateComponent,
EncryptionComponent
];
19 changes: 19 additions & 0 deletions ng-demo/app/components/encryption/encryption.component.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<ActionBar title="Encription">
<NavigationButton text="Back" ios.position="left"></NavigationButton>
<ActionItem text="Save" (tap)="save()" ios.position="right"></ActionItem>
</ActionBar>
<StackLayout>

<Label text="State: {{state}}" textWrap="true"></Label>

<TextField #databasename hint="Database Name"></TextField>
<TextField #databasepassword hint="Database Password"></TextField>
<Button text="Open" (tap)="open(databasename.text, databasepassword.text)"></Button>
<Button text="Close" (tap)="close()"></Button>
<ScrollView height="300">
<StackLayout>
<Label *ngFor="let log of logs" text="{{log}}" textWrap="true"></Label>
</StackLayout>
</ScrollView>

</StackLayout>
47 changes: 47 additions & 0 deletions ng-demo/app/components/encryption/encryption.component.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import {Component} from "@angular/core";
import {Location} from "@angular/common";
import {CouchbaseInstance} from "../../couchbaseinstance";

@Component({
selector: "encryption",
templateUrl: "./components/encryption/encryption.component.html"
})
export class EncryptionComponent {

//private couchbaseInstance: CouchbaseInstance;
private database: any;
logs: Array<string>;
state: string;

constructor(location: Location,private couchbaseInstance: CouchbaseInstance) {

this.logs= new Array<string>();
//if(this.couchbaseInstance && th)
}

save() {
// if(this.firstname != "" && this.lastname != "") {
// this.database.createDocument({
// "firstname": this.firstname,
// "lastname": this.lastname
// });
// this.location.back();
// }
}

open(name:string, key: string){
this.database = this.couchbaseInstance.openDatabase(name, key);
if(!this.database){
this.logs.push("Error on open database")
}
}

close(){
if(this.couchbaseInstance.closeDatabase()){
this.logs.push("closed database")
}else {
this.logs.push("Error closing database");
}
}

}
13 changes: 12 additions & 1 deletion ng-demo/app/components/list/list.component.html
Original file line number Diff line number Diff line change
@@ -1,10 +1,21 @@
<ActionBar title="Person List">
<ActionItem text="Create" (tap)="create()" ios.position="right"></ActionItem>
</ActionBar>
<GridLayout>
<StackLayout>
<TextField #asd hint="asd" text="asd"></TextField>

<Button text="Encription" (tap)="encryption()"></Button>



<GridLayout>


<ListView [items]="personList">
<template let-item="item">
<Label [text]="item.firstname + ' ' + item.lastname"></Label>
</template>
</ListView>
</GridLayout>
</StackLayout>

44 changes: 33 additions & 11 deletions ng-demo/app/components/list/list.component.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {Component, NgZone} from "@angular/core";
import { Component, NgZone, OnInit } from '@angular/core';
import {Router} from "@angular/router";
import {Location} from "@angular/common";
import {CouchbaseInstance} from "../../couchbaseinstance";
Expand All @@ -7,20 +7,31 @@ import {CouchbaseInstance} from "../../couchbaseinstance";
selector: "my-app",
templateUrl: "./components/list/list.component.html",
})
export class ListComponent {
export class ListComponent implements OnInit{

private database: any;
private router: Router;
private ngZone: NgZone;
public personList: Array<Object>;

constructor(router: Router, location: Location, ngZone: NgZone, couchbaseInstance: CouchbaseInstance) {
constructor(router: Router, location: Location, ngZone: NgZone,public couchbaseInstance: CouchbaseInstance) {
this.router = router;
this.ngZone = ngZone;
this.database = couchbaseInstance.getDatabase();
// this.database = couchbaseInstance.getDatabase();
this.personList = [];
location.subscribe((path) => {
this.refresh();
});

}

couchbaseInstance.startSync(true);
ngOnInit(){
if(!this.couchbaseInstance.getDatabase()){
this.database = this.couchbaseInstance.openDatabase("mypassword");
}else{
this.database = this.couchbaseInstance.getDatabase();
}


this.database.addDatabaseChangeListener((changes) => {
let changeIndex;
Expand All @@ -38,9 +49,7 @@ export class ListComponent {
}
});

location.subscribe((path) => {
this.refresh();
});


this.refresh();
}
Expand All @@ -49,12 +58,25 @@ export class ListComponent {
this.router.navigate(["create"]);
}

encryption(){
this.router.navigate(["encryption"]);
}





private refresh() {
this.personList = [];
let rows = this.database.executeQuery("people");
for(let i = 0; i < rows.length; i++) {
this.personList.push(rows[i]);
if(this.database){
let rows = this.database.executeQuery("people");
for(let i = 0; i < rows.length; i++) {
this.personList.push(rows[i]);
}
}else{
console.log("cant run database query on refresh, database is null");
}

}

private indexOfObjectId(needle: string, haystack: any) {
Expand Down
39 changes: 23 additions & 16 deletions ng-demo/app/couchbaseinstance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,33 +10,40 @@ export class CouchbaseInstance {
private push: any;

constructor() {
if(!this.isInstantiated) {
this.database = new Couchbase("test-database");
this.database.createView("people", "1", (document, emitter) => {
emitter.emit(document._id, document);
});
this.isInstantiated = true;
}

}

getDatabase() {
closeDatabase(){
return this.database.closeDatabase();
}

openDatabase(name: string, key?: string){

this.database = new Couchbase(name,key);
this.database.createView("people", "1", (document, emitter) => {
emitter.emit(document._id, document);
});
return this.database;
}

getDatabase(): Couchbase {
return this.database;
}

startSync(continuous: boolean) {
this.push = this.database.createPushReplication("http://192.168.57.1:4984/test-database");
this.pull = this.database.createPullReplication("http://192.168.57.1:4984/test-database");
// this.push = this.database.createPushReplication("http://192.168.57.1:4984/test-database");
// this.pull = this.database.createPullReplication("http://192.168.57.1:4984/test-database");

this.push.setContinuous(continuous);
this.pull.setContinuous(continuous);
// this.push.setContinuous(continuous);
// this.pull.setContinuous(continuous);

this.push.start();
this.pull.start();
// this.push.start();
// this.pull.start();
}

stopSync() {
this.push.stop();
this.pull.stop();
// this.push.stop();
// this.pull.stop();
}

}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"android-angular": "npm run prepare-angular; cd ng-demo; tns run android",
"android-vanilla": "npm run prepare-vanilla; cd demo; tns run android",
"ios-angular": "npm run prepare-angular; cd ng-demo; tns emulate ios",
"ios-angular-deploy": "npm run prepare-angular; cd ng-demo; tns deploy ios",
"ios-vanilla": "npm run prepare-vanilla; cd demo; tns emulate ios"
},
"repository": {
Expand Down
1 change: 1 addition & 0 deletions platforms/android/include.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ repositories {

dependencies {
compile 'com.couchbase.lite:couchbase-lite-android:1.3.1'
compile 'com.couchbase.lite:couchbase-lite-android-sqlcipher:+'
compile 'com.google.code.gson:gson:2.6.2'
}

Expand Down
Binary file removed platforms/ios/CouchbaseLite.framework/CouchbaseLite
Binary file not shown.