npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@aspectjs/persistence

v0.5.3

Published

ORM annotations

Downloads

15

Readme


icon: database

@aspectjs/persistence

Why ?

Inspired by the Java Persistence API, @aspectjs/persistence is an attempt to bring standardized persistence annotations to javascript. At the moment, different ORM libraries libraries like typeorm or mikro orm bring their own ES decorators to the Object-Relationnal mapping. Altough the decorators are very similar, you cannot switch from one implementation to another without reworking your codebase.

With @aspectjs/persistence, persistence libraries could rely on a standardized API that makes it possible to seamlessly build code on top of it, regardless of the implementation.

At the moment though, the project only offers a @Transactional() annotation, that integrates with typeorm.

Installation

npm i @aspectjs/persistence

@Transactional()

  • Enable the TransactionalAspect

aop.ts

import { getWeaver } from '@aspectjs/core';
import { TypeOrmTransactionalAspect } from '@aspectjs/persistence/typeorm';
import { DATASOURCE } from './datasource.ts';

getWeaver().enable(new TypeOrmTransactionalAspect().configure(DATASOURCE));

datasource.ts

import { DataSource } from 'typeorm';

export const DATASOURCE = new DataSource({
  type: 'postgres',
  host: 'localhost',
  port: 5432,
  entities: [User, Post],
  synchronize: true,
  ...options,
});

//   DATASOURCE.initialize();
  • Declare a transactional method
import { DATASOURCE } from './datasource.ts';

export class UsersService {
  em = DATASOURCE.manager;
  findAll(): Promise<User[]> {
    return this.usersRepository.find();
  }

  @Transactional()
  async save(user: User) {
    await this.em.save(user);
    for (const post of user.posts) {
      await this.em.save(post);
    }
  }
}

Usage with NestJS

app.module.ts

import { getWeaver } from '@aspectjs/core';
import { TypeOrmTransactionalAspect } from '@aspectjs/persistence/typeorm';
import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { DataSource } from 'typeorm';
import { Post } from './post.entity';
import { User } from './user.entity';
import { UsersModule } from './users/users.module';
@Module({
  imports: [
    TypeOrmModule.forRootAsync({
      useFactory() {
        return {
          type: 'postgres',
          host: 'localhost',
          port: 5432,
          username: 'user',
          password: 'password',
          database: 'database',
          entities: [
            /* ... */
          ],
        };
      },
      async dataSourceFactory(options) {
        const ds = new DataSource(options!);
        getWeaver().enable(new TypeOrmTransactionalAspect().configure(ds));
        return ds;
      },
    }),
    UsersModule,
  ],
  controllers: [],
  providers: [],
})
export class AppModule {}

users.service.ts

import { Transactional } from '@aspectjs/persistence';
import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { User } from '../user.entity';

@Injectable()
export class UsersService {
  constructor(
    @InjectRepository(User)
    private usersRepository: Repository<User>,
    @InjectRepository(User)
    private postsRepository: Repository<Post>,
  ) {}

  @Transactional()
  async save(user: User) {
    await this.usersRepository.save(u);
    for (const post of user.posts) {
      await this.postsRepository.save(post);
    }
  }
}