Skip to content
This repository was archived by the owner on May 22, 2024. It is now read-only.

Repository files navigation

npmlast buildPRs Welcome

@egomobile/orm

A simple and generic ORM mapper.

Install

Execute the following command from your project folder, where your package.json file is stored:

npm install --save @egomobile/orm

Usage

import{createDataContext,DbNullable,NULLasDbNull}from"@egomobile/orm";import{PostgreSQLDataAdapter}from"@egomobile/orm-pg";// npm i @egomobile/orm-pg// the reason, why non-nullable fields// can have (null) values is, that (null)// indicates, that the underlying fields// should not be defined / updated in the// database//// to set a value explicitly (null) in database// use the value of DbNull insteadclassUser{// non-nullable fieldspublicid: number|null=null;publicfirst_name: string|null=null;publiclast_name: string|null=null;// nullable fieldspublicemail: DbNullable<string|null>=null;}asyncfunctionmain(){constcontext=awaitcreateDataContext({adapter: newPostgreSQLDataAdapter(),entities: {// name of the entity / tableusers: {ids: ["id"],// list of column(s) which represent the IDtype: User,// the class / type to use to create objects from},},});constlistOfUsers=awaitcontext.find(User,{// the following both settings depend on the underlying// data adapterwhere: "is_active=$1 AND is_deleted=$2",params: [true,false],// $1, $2offset: 1,// skip the firstlimit: 100,// only return 100 rows});// return a user with ID 5979constspecificUser=awaitcontext.findOne(User,{where: "id=$1",params: [5979],// $1});if(specificUser!==null){// update with new dataspecificUser.last_name="Doe";specificUser.first_name="Jane";specificUser.email=DbNull;awaitcontext.update(specificUser);// remove from databaseawaitcontext.remove(specificUser);}else{console.log("User not found");}}// create new POCOconstnewUser=newUser();newUser.first_name="John";newUser.last_name="Doe";// ... and add it to databaseawaitcontext.insert(newUser);// depending on data adapter, it might be// possible to do raw queriesconstresult: any=awaitcontext.query("SELECT * FROM users WHERE id=$1 AND is_active=$2;",23979,true);console.log(result);main().catch(console.error);

Documentation

The API documentation can be found here.

Releases

Packages

Used by

Contributors

Languages