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

data-mapper-core

v1.0.6

Published

A small typescript library for implementing patterns "domain driven design" and "data mapper".

Downloads

16

Readme

A small typescript library for implementing patterns "domain driven design" and "data mapper"

A small TypeScript library with great features:

  1. Placing the project's primary focus on the core domain and domain logic
  2. Basing complex designs on a model of the domain
  3. The interface of an object conforming to this pattern would include functions such as Create, Read, Update, and Delete, that operate on objects that represent domain entity types in a data store.
  4. A Data Mapper is a Data Access Layer that performs bidirectional transfer of data between a persistent data store (often a REST API) and an in-memory data representation (the domain layer). The goal of the pattern is to keep the in-memory representation and the persistent data store independent of each other and the data mapper itself.
  5. The layer is composed of one or more mappers (or Data Access Objects), performing the data transfer. Mapper implementations vary in scope. Generic mappers will handle many different domain entity types, dedicated mappers will handle one or a few.
  6. Initiating a creative collaboration between technical and domain experts to iteratively refine a conceptual model that addresses particular domain problems

Install

package.json

{
  "dependencies": {
    "data-mapper-core": "^1.0.3"
  }
}

npm

npm i data-mapper-core

Import

import JsonRemoteDataMapper from "data-mapper-core/dist/json_remote_data_mapper";

Global settings

RemoteDataMapper.baseURL = "http://localhost";

For example, in Vue project

import Vue from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'
import {RemoteDataMapper} from 'data-mapper-core';

const baseURL = process.env.VUE_APP_BASE_URL;
RemoteDataMapper.baseURL = baseURL;

new Vue({
  router,
  store,
  render: h => h(App)
}).$mount('#app');

Example

See tests for examples.

For example you have REST API endpoint

GET /users

Code 200

Response:

{
    "results": [
        {
            "username": "GWashington",
            "first_name": "Washington",
            "last_name": "George"
        },
        {
            "username": "JAdams",
            "first_name": "Adams",
            "last_name": "John"
        }
    ],
    "count": 2
}

GET /users/GWashington

Code 200

Response:

{
    "username": "GWashington",
    "first_name": "Washington",
    "last_name": "George"
}

POST /users/GWashington

Code 201

Response:

{
    "username": "GWashington",
    "first_name": "Washington",
    "last_name": "George"
}

PUT /users/GWashington

Code 200

Response:

{ }

DELETE /users/GWashington

Code 200

Response:

{ }

OK! You can make a domain model for a user like this:

import DataRow from "data-mapper-core/dist/data_row";

export default class UserInfo  extends DataRow{
    public login?: string;
    public firstName?: string;
    public lastName?: string;
    public fullName?: string;
}

And then you can make a data mapper class. It will help you extract data from the API into memory and abstract from the data store that you don't control:

import JsonRemoteDataMapper from "data-mapper-core/dist/json_remote_data_mapper";
import UserInfo from "./user_info";

export default class UserDataMapper extends JsonRemoteDataMapper<UserInfo> {

    protected factory(key: string): UserInfo {
        return new UserInfo(key);
    }

    protected get fieldMap(): Map<string, any> {
        const map = new Map<string, any>();
        map.set('login', 'username');
        map.set('firstName', 'first_name');
        map.set('lastName', 'last_name');
        map.set('fullName', function (payload: any, internalKey: string) {
            return `${payload["first_name"]} ${payload["last_name"]}`
        });
        return map;
    }

    protected get url(): string {
        return "/users";
    }

    get KEY_FIELD_NAME(): string {
        return "username";
    }
}

Test GET web method for resources

const dataMapper = new TestUserDataMapper();
const items: GetItemsResult<TestUserInfo> = await dataMapper.getItems();

Test GET web method for single resource

const dataMapper = new TestUserDataMapper();
const item: TestUserInfo | null = await dataMapper.getItem('GWashington');

Test DELETE web method

const dataMapper = new TestUserDataMapper();
const status: boolean = await dataMapper.delete('GWashington');

Test POST web method for new resource

const dataMapper = new TestUserDataMapper();
const item: TestUserInfo = await dataMapper.insert({data: data});

It's very simple! I am waiting for your feedback on the mail [email protected] ;)