2.0.105 • Published 8 months ago

hibernatets v2.0.105

Weekly downloads
1
License
ISC
Repository
github
Last release
8 months ago

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

2.0.105

8 months ago

2.0.104

8 months ago

2.0.103

8 months ago

2.0.88

9 months ago

2.0.89

9 months ago

2.0.102

8 months ago

2.0.86

9 months ago

2.0.101

8 months ago

2.0.87

9 months ago

2.0.100

8 months ago

2.0.84

9 months ago

2.0.85

9 months ago

2.0.83

9 months ago

2.0.99

8 months ago

2.0.97

8 months ago

2.0.98

8 months ago

2.0.95

8 months ago

2.0.96

8 months ago

2.0.93

9 months ago

2.0.94

9 months ago

2.0.91

9 months ago

2.0.92

9 months ago

2.0.90

9 months ago

2.0.82

1 year ago

2.0.81

1 year ago

2.0.80

1 year ago

1.0.79

1 year ago

1.0.78

2 years ago

1.0.77

3 years ago

1.0.76

3 years ago

1.0.73

3 years ago

1.0.75

3 years ago

1.0.74

3 years ago

1.0.72

4 years ago

1.0.71

4 years ago

1.0.70

4 years ago

1.0.69

4 years ago

1.0.68

4 years ago

1.0.62

4 years ago

1.0.61

4 years ago

1.0.60

4 years ago

1.0.66

4 years ago

1.0.65

4 years ago

1.0.64

4 years ago

1.0.63

4 years ago

1.0.67

4 years ago

1.0.51

4 years ago

1.0.50

4 years ago

1.0.55

4 years ago

1.0.54

4 years ago

1.0.53

4 years ago

1.0.52

4 years ago

1.0.59

4 years ago

1.0.58

4 years ago

1.0.57

4 years ago

1.0.56

4 years ago

1.0.48

4 years ago

1.0.49

4 years ago

1.0.47

5 years ago

1.0.46

5 years ago

1.0.45

5 years ago

1.0.44

5 years ago

1.0.43

5 years ago

1.0.42

5 years ago

1.0.41

5 years ago

1.0.40

5 years ago

1.0.39

5 years ago

1.0.38

5 years ago

1.0.37

5 years ago

1.0.33

5 years ago

1.0.36

5 years ago

1.0.35

5 years ago

1.0.34

5 years ago

1.0.32

5 years ago

1.0.31

5 years ago

1.0.30

5 years ago

1.0.29

5 years ago

1.0.28

5 years ago

1.0.27

5 years ago

1.0.26

5 years ago

1.0.25

5 years ago

1.0.24

5 years ago

1.0.23

5 years ago

1.0.22

5 years ago

1.0.21

5 years ago

1.0.20

5 years ago

1.0.19

5 years ago

1.0.18

5 years ago

1.0.17

5 years ago

1.0.16

5 years ago

1.0.15

5 years ago

1.0.14

5 years ago

1.0.13

5 years ago

1.0.12

5 years ago

1.0.11

5 years ago

1.0.9

5 years ago

1.0.10

5 years ago

1.0.8

5 years ago

1.0.7

5 years ago

1.0.6

5 years ago

1.0.5

5 years ago

1.0.4

5 years ago

1.0.3

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago