0.0.4 • Published 4 years ago

@veglogic/ngx-typeorm v0.0.4

Weekly downloads
-
License
MIT
Repository
-
Last release
4 years ago

ngx-typeorm

This library was generated with Angular CLI version 11.2.5.

This module is a third party module and is not offically supported by typeorm.

Installation

Table of Contents

  1. Install the peer dependencies
  2. Add/Update webpack.config.js
  3. Update angular.json
  4. Install package
  5. Import TypeORMModule into application

Install the peer dependencies

This package requires typeorm and sql.js to be available in your project for this module to work.

NPM:

$ npm install --save typeorm sql.js

YARN:

$ yarn add typeorm sql.js

Optionally install localforage

NPM:

$ npm install --save localforage

YARN:

$ yarn add localforage

Add/Update webpack.config.js

Because of how typeorm is bundled we need to use webpack to swap out the package for the browser version. This is done with the NormalModuleReplacementPlugin as shown bellow.

webpack.config.json

const webpack = require("webpack");

module.exports = {
  plugins: [
    new webpack.NormalModuleReplacementPlugin(/typeorm$/, function (result) {
      result.request = result.request.replace(/typeorm/, "typeorm/browser");
    }),
    new webpack.ProvidePlugin({
      "window.SQL": "sql.js/dist/sql-wasm.js",
    }),
  ],
};

Update angular.json

To get angular's cli to build using webpack next the builder needs to be swapped out with the webpack builder for the build command. While we are doing that also add "node_modules/sql.js/dist/sql-wasm.js" to your build angular.json config file for your application's build step.

angular.json

{
    ...
    "build": {
        "builder": "@angular-builders/custom-webpack:browser",
        "options": {
            "customWebpackConfig": {
                "path": "./webpack.config.js"
            },
            ...
            "scripts": [
                "node_modules/sql.js/dist/sql-wasm.js",
                "node_modules/localforage/dist/localforage.min.js"
            ]
        },
        ...
    }
    ...
}

Install package

NPM:

$ npm install --save @veglogic/ngx-typeorm

YARN:

$ yarn add @veglogic/ngx-typeorm

Import TypeORMModule into application

In your root module's folder you will need to import the TypeORMModule using the forRoot static method. This method expects the same options you would pass to typeorm's createConnection method.

import { NgModel } from '@angular/core';
import { TypeORMModule } from '@veglogic/ngx-typeorm';

@NgModule({
    ...
    imports: [
        ...
        TypeORMModule.forRoot({
            entities: [],
            synchronize: true,
            autoSave: true,
            location: 'v1',
            useLocalForage: true,
        })
        ...
    ]
    ...
})
export class AppModule {}

This will bootstrap the database connection on application initalization and make it available throughout the application at run time. Given that this lives exclusively in the frontend the single connection is maintained and carried for the life of the application's runtime.

General Usage

Creating entities

TypeORM goes into extensive detail on how to create entities in their documentation.

Example

import { Entity, PrimaryGeneratedColumn } from "typeorm/browser";

@Entity()
export class Photo {
  @PrimaryGeneratedColumn("uuid")
  public id: string;
}

Injecting the connection

To use the database within your project you simply need to import Connection from @veglogic/ngx-typeorm and angular's DI will pass it to you. From the connection object you are able to create repositories among other things.

import { Connection } from "@veglogic/ngx-typeorm";

@Injectable()
export class PhotoService {
  public readonly photoRepository = this.connection.getRepository(Photo);

  constructor(private connection: Connection) {}
}

License

The MIT License (MIT) Copyright © 2021 Brandon Bleau (bmbleau@gmail.com)

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

0.0.4

4 years ago

0.0.3

4 years ago

0.0.2

4 years ago

0.0.1

4 years ago