# tsgeneric-dao-api

> Generic DAO and Api NodeJs, Express, Mongoose

Latest version **0.0.2** (published 2016-09-01) · ISC license · 0 weekly downloads

## Install

```sh
npm install tsgeneric-dao-api
pnpm add tsgeneric-dao-api
yarn add tsgeneric-dao-api
bun add tsgeneric-dao-api
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.2 |
| Published | 2016-09-01 |
| First published | 2016-08-31 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 6 |
| Known vulnerabilities | 0 (+11 in 1 direct dependencies) |
| Install scripts | no |
| Author | Eslem |
| Maintainers | eslem |

## Links

- npm: https://www.npmjs.com/package/tsgeneric-dao-api
- npm.io page: https://npm.io/package/tsgeneric-dao-api

## Dependencies (6)

- [lodash](https://npm.io/package/lodash.md) ^4.15.0
- [express](https://npm.io/package/express.md) ^4.14.0
- [bluebird](https://npm.io/package/bluebird.md) ^3.4.5
- [mongoose](https://npm.io/package/mongoose.md) ^4.4.7
- [typescript](https://npm.io/package/typescript.md) ^1.8.10
- [es6-promise](https://npm.io/package/es6-promise.md) ^3.2.1

## Recent versions

- 0.0.2 (latest) — 2016-09-01
- 0.0.1 — 2016-08-31

## README

# TSGeneric-DAO-API

[![Build Status](https://travis-ci.org/Eslem/TSGeneric-DAO-API.svg?branch=master)](https://travis-ci.org/Eslem/TSGeneric-DAO-API)

Generics Classes for DAO and API with Typescript (Mongoose, Express)

## Classes

- GenericDAO: DAO functions (CRUD).
- GenericDAOImplMongoose: implementation of GenericDAO for Mongoose.

- GenericAPI: Route for DAO functions (CRUD).

- GenericDAOImplExpress: implementation of GenericAPI for Express.

--------------------------------------------------------------------------------

### DAO

```typescript
  interface GenericDAO <T>{
    create(model:T):Promise<T>;
    get(id:number|string): Promise<T|any>;
    getAll():Promise<[T]>;
    update(model:any):Promise<T>;
    delete(id:number|string):Promise<any>;
  }
```
[Example Note](test/common/NoteDB.ts)

### API

```typescript
interface GenericAPI {
  DAO:GenericDAO<any>; // DAO to use
  route:String; //route name
}

abstract class GenericAPIImplExpress implements GenericAPI {
  constructor(protected route: String, public DAO: GenericDAO<any>) { }

  init(router: express.Router) {
    this.baseRoute(router);
    this.idRoute(router);
    this.moreRoutes(router);
  }
```
[Example Note](test/common/NoteApi.ts)

### Implemetation

```typescript

//Interface for model
export interface Note {
    title: String;
    message: String;
}

//Document
export interface NoteDocument extends Note, mongoose.Document {
}

//DAO
export class NoteDAOImplMongoose extends GenericDAOImplMongoose<Note, NoteDocument>{
    constructor() {
        super(
            "Note", //Name of the model
            new mongoose.Schema({
                title: String,
                message: String
            }) //Mongoose Schema
        );
    }
}

//API
export class NoteApi extends GenericAPIImplExpress {
    constructor() {
        super(
          'notes',//url /api/notes
           new NoteDAOImplMongoose());
    }
}
/****** Endpoints
get      /api/notes      get all
get      /api/notes/:id  get note with id
post     /api/notes/     create note
put      /api/notes/:id  update note with id
delete   /api/notes/:id  delete note with id
***/


//// Example :

//Express app and router
let noteApi = new NoteApi();
noteApi.init(router);


let NoteDAO = new NoteDAOImplMongoose();

NoteDAO.create({
    title: 'Nain Note',
    message: 'This is a message'
})
.then(
    note => {
        //Model created
    }
).catch(
    err=>{
      //OnError
    }
);

```

---
_Source: https://npm.io/package/tsgeneric-dao-api · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
