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

hibernatets

v2.0.105

Published

hibernate clone for typescript

Downloads

4,932

Readme

hibernateTS

typescript clone for hiberante/persistance API

Install

  npm i hibernatets

Setup

currently support for mariadb databases

set up by setting these environment variables

const db = process.env.DB_NAME;
const port = +process.env.DB_PORT;
const user = process.env.DB_USER;
const url = process.env.DB_URL;
const password = process.env.DB_PASSWORD;

for postgres

const url =process.env.PSQL_URL
const port =process.env.PSQL_PORT
const password =process.env.PSQL_PWD
const user = process.env.PSQL_USER
const db =process.env.PSQL_DB

import {PsqlBase} from "hibernatets/dbs/psql-base"

const pool=new PsqlBase()
load(type,{
  ...
  options:{
    db:pool
  }
})

save(obj,{
  db:pool
})

experimentalDecorators needs to be enabled

Api

Models

configure database with annotations

import { table, primary, column, mapping, Mappings } from 'hibernatets';
import { OtherModel } from './otherModel';

@table({
  // can be omitted defaults to ClassName toLowercase
  name: 'testmodel',
})
class TestModel {
  // { strategy: 'custom'|'auto-increment' }
  // custom for mapping to non auto-increment tables
  @primary()
  id: number;

  @column()
  randomcolumn: string;

  // key 'reverseforeignkey' in OtherModel references primary key of current table(TestModel)
  //alternative @mapping(Mappings.OneToMany,OtherModel,o=>o.reverseforeignkey) for autocompletion
  @mapping(Mappings.OneToMany, OtherModel, 'reverseforeignkey')
  othermodels: Array<OtherModel>;

  // key 'othermodel' in current table(TestModel) references primary key of OtherModel
  @mapping(Mappings.OneToOne, OtherModel)
  othermodel: OtherModel;
  //...
}

Functions

Load

objects can be loaded with

const obj: TestModel = await load(TestModel, 1); // primary key

or

import { load } from 'hibernatets';

const obj: Array<TestModel> = await load(
  TestModel,
  (t) => (t.randomcolumn = 'test')
); //assignment here
const obj: TestModel = await load(
  TestModel,
  (t) => (t.randomcolumn = 'test'),
  [],
  { first: true }
); //assignment here

or

also see SqlCondition

const obj: Array<TestModel> = await load(
  TestModel,
  new SqlCondition().column('randomcolumn').equals('test')
);
const obj: TestModel = await load(
  TestModel,
  new SqlCondition().column('randomcolumn').equals('test'),
  [],
  { first: true }
);

or

//!!!careful of sql injection with this approach @Deprecated in favor of SqlCondition
const obj: Array<TestModel> = await load(TestModel, 'randomcolumn = ?', [
  'test',
]);
//!!!careful of sql injection with this approach @Deprecated in favor of SqlCondition
const obj: TestModel = await load(TestModel, 'randomcolumn = ?', ['test'], {
  first: true,
});

for mappings the default is to not load nested mappings this can be enabled by adding the optional "deep" parameter

const obj: Array<TestModel> = await load(
  TestModel,
  (t) => (t.randomcolumn = 'test'),
  [],
  { deep: true }
);
const obj: TestModel = await load(
  TestModel,
  (t) => (t.randomcolumn = 'test'),
  [],
  { first: true, deep: true }
);

const obj: TestModel = await load(
  TestModel,
  (t) => (t.randomcolumn = 'test'),
  [],
  { first: true, deep: ['othermodels'] }
); //only loads othermodels mappings
const obj: TestModel = await load(
  TestModel,
  (t) => (t.randomcolumn = 'test'),
  [],
  {
    first: true,
    deep: {
      othermodels: " othermodelatt = 'test'  ",
    },
  }
); //only loads othermodels with query !!!careful of sql injection

alternatively all options can be passed in this format

const obj: Array<TestModel> = await load(TestModel, {
  filter: 'randomcolumn = ?', //for loading all just leave away
  params: ['test'],
  options: {
    deep: true,
  },
});
Updates

see Timing if you want to await finishing of request if and object is loaded with

  • Array.push will automatically store pushed Elements
  • Array.filter will remove filtered out Elements !!

(setters are always intercepted unless dontInterceptSetters:true is passed to load)

// with @column() attribute
const obj: TestModel = await load(TestModel, 1, [], {
  interceptArrayFunctions: true,
});

// automatically gets persisted to database
obj.attribute = 'test';

// with @mapping(Mappings.OneToMany) attributes
const obj: TestModel = await load(TestModel, 1);

const newObject = new NewObject();
// automatically gets added and persisted
obj.attributes.push(newObject);
Timing

assignments to loaded objects get automatically persisted and can be awaited with

const obj = await load(TestModel, 0);
obj.attribute = 'test';
//sql request not finished yet
await database.queries(obj);
//sql requests all finished
Delete
//alternative delete(Class,primary)
database.delete(obj);
Save
// for new Objects - loaded objects autoupdate on attribute change
database.save(obj);

Database Updates

when @column() annotation is used to further specify database fields updateDatabase can be used to automatically update the database for certain changes

  • database types: 'text' 'number'
  • database sizes: 'small' 'medium' 'large'
  • nullable
  • default
import { updateDatabase,table,column } from "hibernatets"

@table()
export class TestModel {

    // creates database column with type BIGINT
    @column({
      type:"number"
      size:"large"
    })
    example

}
// creates missing tables
// adds missing columns
// increases column size
// takes argument for folder with model files
updateDatabase(`${__dirname}/testmodels`);

Transformations

if a certain runtime type is too unique for this library you can specify transformations that are applied before saving or loading

import { updateDatabase,table,column } from "hibernatets"

@table()
export class TestModel {

    // creates database column with type BIGINT
    @column({
      type:"number"
      size:"large",
      transformations:{
        loadFromDbToProperty: (dbData: U) => Promise<T>;
	      saveFromPropertyToDb: (obj: T) => Promise<U>
      }
    })
    example

}

custom constraints

custom constraints can be added on a per table basic liek this

@table({
    constraints: [{
        type: "unique",
        columns: ["test1", "test2"]
    }]
})

npm at https://www.npmjs.com/package/hibernatets