Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
NotificationsYou must be signed in to change notification settings

typeorm/nativescript-vue-typeorm-sample

Repository files navigation

Okay let's break this down

  • NativeScript : Runs Javascript as Native apps on Android and iOS
  • VueJS: A frontend framework that can be used in NativeScript (Angular is possible too)
  • nativescript-sqlite: A Sqlite library for {N}, used here as the Sqlite driver
  • TypeORM: A TypeScript/ES7 based ORM that works on NodeJS as well as browsers

Demo

As you can see the task list is persisted even when the appis killed and reopened.

demo

Setting this up

Okay so the TL;DR version is this -

  • Setup a NativeScript project

    This creates a NativeScript-Vue app

    tns create nativescript-sample --template nativescript-vue-template

    Feel free to use NativeScript without any frontend framework, or withAngular. All the same. TypeORM will work everywhere

  • Make sure you arewebpack-ing your project.

    TypeORM's browser library is in ES7 (withimport/export statements)This is something that cannot be executed directly in NativeScript(At least not untill we have a JavascriptCore or V8 that start supporting it)

    So please make sure you are using {N} in bundle mode

    tns install webpack

    When running the project, always use bundle mode

    tns run ios --bundle
  • Install thenativescript-sqlite plugin

    Please make sure you install it as a tns plugin and notjust npm install

    tns plugin add nativescript-sqlite
  • Install TypeORM

    npm install typeorm

At this stage you are ready to use TypeORM.Please read the documentation onhttp://typeorm.io

TypeORM can be used with/without a repository and it can be usedinDataMapper fashion orActiveRecord fashion. Please do whateverfeels comfortable. Everything is supported.

Trying things out quickly

1. Setup TypeORM somewhere your app starts

I have done this in theapp/main.ts file. You should ideally do thiswherever your app is initialised.

import{createConnection,getManager}from"typeorm/browser";importTodofrom'./models/Todo';(async()=>{try{constconnection=awaitcreateConnection({database:'test.db',type:'nativescript',entities:[Todo],logging:true})console.log("Connection Created")// setting true will drop tables and recreateawaitconnection.synchronize(false)console.log("Synchronized")}catch(err){console.error(err)}})();

2. Define your model

I am using ActiveRecord. If you want to use DataMapper, you shouldnot extend from BaseEntitiy

import{BaseEntity,Entity,PrimaryGeneratedColumn,Column}from"typeorm/browser";@Entity()exportdefaultclassTodoextendsBaseEntity{    @PrimaryGeneratedColumn()id?:number;    @Column()task:string;    @Column()done:boolean;constructor(task:string,done:boolean){super();this.task=task;this.done=done;}}

3. Use the entities in your app

You can check outapp/components/App.ts

importTodofrom"./models/Todo";functionrefreshTodos(){Todo.find().then((todos)=>{console.log(todos)this.todos=todos}).catch(console.error)}functionaddTodo(){consttodo=newTodo(this.newtask,false);todo.save().then(()=>this.refreshTodos()).catch(console.error)}

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

[8]ページ先頭

©2009-2025 Movatter.jp