Skip to content

Commit

Permalink
feat: move to @akylas/nativescript-sqlite
Browse files Browse the repository at this point in the history
use our own custom typeorm for N 7.0
  • Loading branch information
farfromrefug committed Aug 14, 2020
1 parent 15fa0f5 commit 71ef0c1
Show file tree
Hide file tree
Showing 36 changed files with 9,928 additions and 3,507 deletions.
27 changes: 0 additions & 27 deletions demo/.migration_backup/package.json

This file was deleted.

7 changes: 0 additions & 7 deletions demo/.migration_backup/tsconfig.tns.json

This file was deleted.

6,825 changes: 6,825 additions & 0 deletions demo/.tsbuildinfo

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion demo/app/app.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
import * as application from '@nativescript/core/application';
application.run({ moduleName: "main-page" });
application.run({ moduleName: 'main-page' });
Binary file added demo/app/assets/maltobarbar.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions demo/app/main-page.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import * as observable from '@nativescript/core/data/observable';
import * as pages from '@nativescript/core/ui/page';
import {HelloWorldModel} from './main-view-model';
import { HelloWorldModel } from './main-view-model';

// Event handler for Page 'loaded' event attached in main-page.xml
export function pageLoaded(args: observable.EventData) {
// Get the event sender
let page = <pages.Page>args.object;
const page = args.object as pages.Page;
page.bindingContext = new HelloWorldModel();
}
5 changes: 4 additions & 1 deletion demo/app/main-page.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@
xmlns:ui="nativescript-akylas-sqlite">
<StackLayout class="p-20">
<Label text="{{ message }}" class="t-20 text-center c-black" textWrap="true"/>
<ActivityIndicator busy="true" verticalAlignment="center" class="m-20"></ActivityIndicator>
<!-- <ActivityIndicator busy="true" verticalAlignment="center" class="m-20"></ActivityIndicator> -->
<Button text="Generate and Insert Data!" tap="{{ onInsert }}" class="m-20"></Button>
<Button text="Generate and Insert Data (with transaction)!" tap="{{ onInsertWithTrans }}"
class="m-20"></Button>
<Button text="Select Data!" tap="{{ onSelect }}" class="m-20"></Button>
<Button text="Reset Data!" tap="{{ onReset }}" class="m-20"></Button>
<Button text="blobl test!" tap="{{ blobTest }}" class="m-20"></Button>
<Button text="typeorm test!" tap="{{ typeORMTest }}" class="m-20"></Button>
<Image imageSource="{{imageSource}}" class="m-20" width="100%" backgroundColor="red"></Image>
</StackLayout>
</Page>
135 changes: 104 additions & 31 deletions demo/app/main-view-model.ts
Original file line number Diff line number Diff line change
@@ -1,78 +1,151 @@
import { Observable } from "@nativescript/core/data/observable";
import {
openOrCreate,
SQLiteDatabase,
deleteDatabase,
} from "nativescript-akylas-sqlite";
import { path, knownFolders } from "@nativescript/core/file-system";
import { Observable } from '@nativescript/core/data/observable';
import { SQLiteDatabase, deleteDatabase, openOrCreate } from '@akylas/nativescript-sqlite';
import { knownFolders, path } from '@nativescript/core/file-system';
import { ImageSource } from '@nativescript/core';
import { BaseEntity, Column, Connection, Entity, PrimaryGeneratedColumn, createConnection } from '@akylas/typeorm/browser';
import { installMixins } from '@akylas/nativescript-sqlite/typeorm';

type DataExample = { id: number; name: string };
interface DataExample {
id: number;
name: string;
}

@Entity()
export default class ImageTest extends BaseEntity {
@PrimaryGeneratedColumn()
id: number;
@Column('blob', { nullable: false })
data: any;

@Column('int', { nullable: false })
width: number;

@Column('int', { nullable: false })
height: number;
}

export class HelloWorldModel extends Observable {
public message: string;
public message: string = '';
public imageSource: ImageSource = null;
sqlite: SQLiteDatabase;

constructor() {
super();
this.resetDb();
this.sqlite.setVersion(1);
this.message = `version = ${this.sqlite.getVersion()}`;
this.resetDb().then(() => {
this.sqlite.setVersion(1).then(() => {
this.sqlite.getVersion().then((r) => {
console.log('got version', r);
this.message = `version = ${r}`;
});
});
});
}

resetDb() {
const filePath = path.join(
knownFolders.documents().getFolder("db").path,
"dbname.sqlite"
);
const filePath = path.join(knownFolders.documents().getFolder('db').path, 'dbname.sqlite');
deleteDatabase(filePath);
this.sqlite = openOrCreate(filePath);
const createCmd =
"CREATE TABLE names (id INT, name TEXT, json TEXT, PRIMARY KEY (id))";
this.sqlite.execute(createCmd);
const createCmd = 'CREATE TABLE names (id INT, name TEXT, json TEXT, PRIMARY KEY (id))';
return this.sqlite.execute(createCmd);
}

insert(data: DataExample[]) {
data.map((data, i) => {
const insert = `INSERT INTO names (id, name, json) VALUES (?, ?, ?)`;
const insert = 'INSERT INTO names (id, name, json) VALUES (?, ?, ?)';
// Uncomment to crash it
if (i === 1000) {
console.log("About to crash!");
console.log('About to crash!');
data.id = 0;
}
this.sqlite.execute(insert, [
data.id,
data.name,
JSON.stringify(data),
]);
this.sqlite.execute(insert, [data.id, data.name, JSON.stringify(data)]);
});
}

onInsert() {
try {
this.insert(generateData(10000));
} catch (error) {
alert("Error onInsert: " + error);
alert('Error onInsert: ' + error);
}
}

onInsertWithTrans() {
try {
this.sqlite.transaction(() => this.insert(generateData(10000)));
} catch (error) {
alert("Error onInsertWithTrans: " + error);
alert('Error onInsertWithTrans: ' + error);
}
}

onSelect() {
const select = "SELECT * FROM names WHERE id < 20";
const select = 'SELECT * FROM names WHERE id < 20';
const data = this.sqlite.select(select);
alert(`Received data: ${JSON.stringify(data)}`);
}

onReset() {
const reset = "DELETE FROM names";
const reset = 'DELETE FROM names';
this.sqlite.execute(reset);
}
async blobTest() {
const createCmd = 'CREATE TABLE blobs (id INT, name TEXT, data blob, PRIMARY KEY (id))';
await this.sqlite.execute(createCmd);
const insert = 'INSERT INTO blobs (id, name, data) VALUES (?, ?, ?)';
const imageSource = ImageSource.fromFileOrResourceSync('~/assets/maltobarbar.jpg');
console.log('imageSource', imageSource);
const byteArrayOutputStream = new java.io.ByteArrayOutputStream();
imageSource.android.compress(android.graphics.Bitmap.CompressFormat.JPEG, 100, byteArrayOutputStream);
await this.sqlite.execute(insert, [0, 'test', byteArrayOutputStream]);
console.log('done writing blob!', byteArrayOutputStream);
const blob = this.sqlite.each(
'select * from blobs',
null,
(err, result) => {
console.log('read', result);
const data = result.data;
const bmp = android.graphics.BitmapFactory.decodeByteArray(data, 0, data.length);
console.log('decoded image', bmp.getWidth(), bmp.getHeight());
this.imageSource = new ImageSource(bmp);
},
null
);
}
async typeORMTest() {
const filePath = path.join(knownFolders.documents().path, 'db.sqlite');
deleteDatabase(filePath);
console.log('typeORMTest');
installMixins();
const connection = await createConnection({
database: filePath,
type: 'nativescript' as any,
entities: [ImageTest],
logging: true,
});
await connection.synchronize(false);
const imageSource = ImageSource.fromFileOrResourceSync('~/assets/maltobarbar.jpg');
console.log('imageSource', imageSource);
const image = new ImageTest();
image.width = imageSource.width;
image.height = imageSource.height;
if (global.isAndroid) {
const byteArrayOutputStream = new java.io.ByteArrayOutputStream();
imageSource.android.compress(android.graphics.Bitmap.CompressFormat.JPEG, 100, byteArrayOutputStream);
image.data = byteArrayOutputStream.toByteArray();
} else {
const data = UIImageJPEGRepresentation(imageSource.ios, 1);
image.data = data;
}
console.log('saving image');
await image.save();
console.log('saving image done');
const results = await ImageTest.find({
order: {
id: 'DESC',
},
take: 10,
});
console.log('images', results);
}
}

const generateData = (length: number) => {
Expand All @@ -82,6 +155,6 @@ const generateData = (length: number) => {
data.push({ id: i, name: `${Math.random() + i} Test data` });
++i;
}
console.log("Generated " + i + " data items");
console.log('Generated ' + i + ' data items');
return data;
};
5 changes: 4 additions & 1 deletion demo/app/package.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
{
"name": "nativescript.sqlite.demo",
"main": "app.js"
"main": "app.js",
"android": {
"markingMode": "none"
}
}
19 changes: 11 additions & 8 deletions demo/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,27 @@
"id": "org.nativescript.demo",
"tns-ios": {
"version": "6.2.0"
},
"tns-android": {
"version": "6.5.3"
}
},
"dependencies": {
"@nativescript/core": "rc",
"@akylas/nativescript-sqlite": "file:../plugin",
"nativescript-theme-core": "^2.0.24",
"nativescript-unit-test-runner": "^0.7.0",
"nativescript-akylas-sqlite": "file:../plugin",
"@nativescript/core": "^6.2.3"
"nativescript-unit-test-runner": "0.7.1-next-2019-07-19-122824-01",
"@akylas/typeorm": "latest"
},
"devDependencies": {
"@nativescript/types": "rc",
"@nativescript/webpack": "rc",
"jasmine-core": "^3.5.0",
"loader-utils": "1.2.3",
"karma": "^4.4.1",
"karma-jasmine": "^2.0.1",
"karma-nativescript-launcher": "^0.4.0",
"nativescript-dev-webpack": "next",
"tns-platform-declarations": "^6.2.3",
"tslint": "~5.20.1",
"typescript": "3.4.5"
"loader-utils": "1.2.3",
"typescript": "3.9.7"
},
"scripts": {
"build.plugin": "cd ../src && npm run build",
Expand Down
Loading

0 comments on commit 71ef0c1

Please sign in to comment.