Skip to content
Azure Database (Table Storage and more) module for Nest framework (node.js) ☁️
TypeScript JavaScript
Branch: master
Clone or download
kamilmysliwiec Merge pull request #6 from nestjs/renovate/tslint-5.x
chore(deps): update dependency tslint to v5.20.0
Latest commit f6ab648 Sep 13, 2019
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.github chore: initial commit Sep 3, 2019
lib feat: add querying implementation Sep 13, 2019
.gitignore chore: initial commit Sep 3, 2019
.npmignore chore: initial commit Sep 3, 2019
.prettierrc style: update prettier config (printWidth: 120) Sep 6, 2019
CONTRINBUTING.md chore: initial commit Sep 3, 2019
LICENSE chore: initial commit Sep 3, 2019
README.md docs: fix typo Sep 12, 2019
env.sample chore: initial commit Sep 3, 2019
index.ts chore: initial commit Sep 3, 2019
jest.config.js chore: initial commit Sep 3, 2019
nest-cli.json chore: initial commit Sep 3, 2019
package.json
renovate.json chore: initial commit Sep 3, 2019
tsconfig.json chore: initial commit Sep 3, 2019
tslint.json chore: initial commit Sep 3, 2019

README.md

Nest Logo

A progressive Node.js framework for building efficient and scalable server-side applications, heavily inspired by Angular.

NPM Version Package License NPM Downloads Travis Linux Coverage Gitter Backers on Open Collective Sponsors on Open Collective

Description

Azure Database (Table Storage and more) module for Nest framework (node.js)

Before Installation

  1. Create a Storage account and resource (read more)
  2. For Table Storage, In the Azure Portal, go to Dashboard > Storage > your-storage-account.
  3. Note down the "Storage account name" and "Connection string" obtained at Access keys under Settings tab.

Installation

$ npm i --save @nestjs/azure-database

Usage

For Azure Table Storage support

  1. Create or update your existing .env file with the following content:
AZURE_STORAGE_CONNECTION_STRING=
  1. IMPORTANT: Make sure to add your .env file to your .gitignore! The .env file MUST NOT be versionned on Git.

  2. Make sure to include the following call to your main file:

if (process.env.NODE_ENV !== 'production') require('dotenv').config();

This line must be added before any other imports!

Example

Prepare your entity

  1. Create a new feature module, eg. with the nest CLI:
$ nest generate module contact
  1. Create a Data Transfert Object (DTO) inside a file named contact.dto.ts:
export class ContactDTO {
  name: string;
  message: string;
}
  1. Create a file called contact.entity.ts and describe the entity model using the provided decorators:
  • @EntityPartitionKey(value: string): Represents the PartitionKey of the entity (required).

  • @EntityRowKey(value: string): Represents the RowKey of the entity (required).

  • @EntityInt32(value?: string): For signed 32-bit integer values.

  • @EntityInt64(value?: string): For signed 64-bit integer values.

  • @EntityBinary(value?: string): For binary (blob) data.

  • @EntityBoolean(value?: string): For true or falsevalues.

  • @EntityString(value?: string): For character data.

  • @EntityDouble(value?: string): For floating point numbers with 15 digit precision.

  • @EntityDateTime(value?: string): For time of day.

For instance, the shape of the following entity:

import { EntityPartitionKey, EntityRowKey, EntityString } from '@nestjs/azure-database';

@EntityPartitionKey('ContactID')
@EntityRowKey('ContactName')
export class Contact {
  @EntityString() name: string;
  @EntityString() message: string;
}

Will be automatically converted to:

{
  "PartitionKey": { "_": "ContactID", "$": "Edm.String" },
  "RowKey": { "_": "ContactName", "$": "Edm.String" },
  "name": { "_": undefined, "$": "Edm.String" },
  "message": { "_": undefined, "$": "Edm.String" }
}

Note: The provided entity type annotations represent the Entity Data Model types.

  1. Import the AzureTableStorageModule inside your Nest feature module contact.module.ts:
import { Module } from '@nestjs/common';
import { AzureTableStorageModule } from '@nestjs/azure-database';
import { ContactController } from './contact.controller';
import { ContactService } from './contact.service';
import { Contact } from './contact.entity';

@Module({
  imports: [AzureTableStorageModule.forFeature(Contact)],
  providers: [ContactService],
  controllers: [ContactController],
})
export class ContactModule {}

You can optionnaly pass in the following arguments:

AzureTableStorageModule.forFeature(Contact, {
  table: 'AnotherTableName',
  createTableIfNotExists: true,
})
  • table: string: The name of the table. If not provided, the name of the Contact entity will be used as a table name
  • createTableIfNotExists: boolean: Whether to automatically create the table if it doesn't exists or not:
    • If true the table will be created during the startup of the app.
    • If false the table will not be created. You will have to create the table by yoursel before querying it!

CRUD operations

  1. Create a service that will abstract the CRUD operations:
$ nest generate service contact
  1. Use the @InjectRepository(Contact) to get an instance of the Azure Repository for the entity definition created earlier:
import { Injectable } from '@nestjs/common';
import { Repository, InjectRepository } from '@nestjs/azure-database';
import { Contact } from './contact.entity';

@Injectable()
export class ContactService {
  constructor(
    @InjectRepository(Contact)
    private readonly contactRepository: Repository<Contact>) {}
}

The AzureTableStorageRepository provides a couple of public APIs and Interfaces for managing various CRUD operations:

CREATE

create(entity: T): Promise<T>: creates a new entity.

  @Get()
  async getAllContacts() {
    return await this.contactService.findAll();
  }
READ

find(rowKey: string, entity: Partial<T>): Promise<T>: finds one entity using its RowKey.

  @Get(':rowKey')
  async getContact(@Param('rowKey') rowKey) {
    try {
      return await this.contactService.find(rowKey, new ContactEntity());
    } catch (error) {
      // Entity not found
      throw new UnprocessableEntityException(error);
    }
  }

findAll(tableQuery?: azure.TableQuery, currentToken?: azure.TableService.TableContinuationToken): Promise<AzureTableStorageResultList<T>>: finds all entities that matche the given query (return all entities if no query provided).

  @Get()
  async getAllContacts() {
    return await this.contactService.findAll();
  }
UPDATE

update(rowKey: string, entity: Partial<T>): Promise<T>: Updates an entity. It does a partial update.

  @Put(':rowKey')
  async saveContact(@Param('rowKey') rowKey, @Body() contactData: ContactDTO) {
    try {
      const contactEntity = new ContactEntity();
      // Disclaimer: Assign only the properties you are expecting!
      Object.assign(contactEntity, contactData);

      return await this.contactService.update(rowKey, contactEntity);
    } catch (error) {
      throw new UnprocessableEntityException(error);
    }
  }
  @Patch(':rowKey')
  async updateContactDetails(@Param('rowKey') rowKey, @Body() contactData: Partial<ContactDTO>) {
    try {
      const contactEntity = new ContactEntity();
      // Disclaimer: Assign only the properties you are expecting!
      Object.assign(contactEntity, contactData);

      return await this.contactService.update(rowKey, contactEntity);
    } catch (error) {
      throw new UnprocessableEntityException(error);
    }
  }
DELETE

delete(rowKey: string, entity: T): Promise<AzureTableStorageResponse>: Removes an entity from the database.

  @Delete(':rowKey')
  async deleteDelete(@Param('rowKey') rowKey) {
    try {
      const response = await this.contactService.delete(rowKey, new ContactEntity());

      if (response.statusCode === 204) {
        return null;
      } else {
        throw new UnprocessableEntityException(response);
      }
    } catch (error) {
      throw new UnprocessableEntityException(error);
    }
  }

Support

Nest is an MIT-licensed open source project. It can grow thanks to the sponsors and support by the amazing backers. If you'd like to join them, please read more here.

Stay in touch

License

Nest is MIT licensed.

You can’t perform that action at this time.