1.1.1 • Published 6 years ago

eventstore-objectmapper v1.1.1

Weekly downloads
1
License
MIT
Repository
github
Last release
6 years ago

Eventstore Object Mapper

This maps events to specific objects in an orm like fashon

npm i eventstore-objectmapper

Required Dependencies

Need Node 8 for async await syntax

Also, uses the eventstore module. One must know how to set this up to use this module. https://www.npmjs.com/package/eventstore

npm i eventstore

Doc Menu

  1. Getting Started
  2. Example Model
  3. Instance Properties
    1. dataValues
    2. latestDataValues
    3. selectedRevision
    4. latestRevision
    5. primaryValue
    6. getHistory
  4. Instance Methods
    1. saveData
    2. saveWhole
    3. takeSnapshot
    4. saveCurrentValues
    5. goToRevision
    6. goToLatestRevision
  5. Static Methods
    1. create
    2. findById
    3. createOrFind
    4. createOrUpdate
    5. findOneAndUpdate
    6. getHistory
  6. Static Getters
    1. schema
    2. snapshotFrequency

Getting Started

make a directory called models, or eventmodels.

const eventstore = require('eventstore');
const {Model} = require('eventstore-objectmapper');

let es = eventstore();

Model.init(es).then(()=>{
    //ready to use
}

Example Model

make file called user.js in models directory

const {Model} = require('eventstore-objectmapper');

class User extends Model {
    constructor(id){
        super(id);
    }

    static get schema(){
        return {
            id:{type:"Number", primary:true}
        }
    }
}

module.exports = User;

Instance Properties

dataValues

let user = await User.create({id:1, first:'Brian', last:'Alois'});

console.log('output:', user.dataValues);

//output: {id:1, first:'Brian', last:'Alois'}

latestDataValues

let user = await User.create({id:1, first:'Brian', last:'Alois'});

await user.saveData({first:'John'});

await user.goToRevision(0);

console.log('selected revision: ', user.dataValues, '\n, latest: ',user.latestDataValues)
//selected revision:  {id:1, first:'Brian', last:'Alois'}, 
//latest: {id:1, first:'John', last:'Alois'}

Instance Methods

saveData

The saveData method adds and updates data with what is given.

const User = require('./models/user');

let user = await User.create({id:1, first:'Brian', last:'Alois'});

await user.saveData({info:'software', last:null});

console.log(user.dataValues);
//{id:1, first:'Brian', last:null, info:'software'}

saveWhole

This is a little different from saveData in that the most updated revision becomes entirely the input

const User = require('./models/user');

let user = await User.create({id:1, first:'Brian', last:'Alois'});

await user.saveWhole({id: 1, info:'software'});

console.log(user.dataValues);
//{id: 1, first:null, last: null, info:'software'}

takeSnapshot

takes snapshot of latest values that were saved.

await user.takeSnapshot();

goToRevision

await user = await User.create({id:1, first:'brian', last:'alois'});
console.log(user.selectedRevision, user.first);
//0 brian

await user.saveData({first:'John'});
console.log(user.selectedRevision, user.first);
//1 John

await user.goToRevision(0);
console.log(user.selectedRevision, user.first);
//0 brian

getHistory

returns history of all the events of the instance

let history = await user.getHistory();

Static Methods

create

const User = require('./models/user');

let user = await User.create({id:1, firstName:'Brian', lastName:' Doe});

findById

const User = require('./models/user');

let user = await User.findById(1);

createOrFind

Method either finds and returns instance or creates it if it doesn't exist

const User = require('./models/user');

let user = await User.createOrFind({id:1, firstName:'Brian'});

createOrUpdate

Method either updates existing instance based on primary key, or if one does not exist with that primary it creates it

const User = require('./models/user');

let user = await User.createOrUpdate({id:1, firstName:'Brian'});

findOneAndUpdate

finds one based on primary value, in this case is id, and updates it.

let user = await User.findOneAndUpdate({id:1, firstName:'Brian'});

if upsert option is set to true it will create it if it is not found

let user = await User.findOneAndUpdate({id:1, firstName:'Brian'}, {upsert:true});

the defualt update is a saveData. If you want to do a saveWhole pass in the option whole=true

let user = await User.findOneAndUpdate({id:1, firstName:'Brian'}, {upsert:true, whole:true});

getHistory

get all the events and dates that events were added

let user_id = 2
let history = await User.getHisory(user_id);

Static Getters

schema

This is necessary, particularly the part where there is a key that is primary;

class User extends Model {
    constructor(id){
        super(id);
    }

    static get schema(){
        return {
            id:{type:"Number", primary:true},
            name:{type:"String"}
        }
    }
}

snapshotFrequency

by defualt it is set to take a snapshot at every 25 events, or saves/changes to the data. However, this can be changed.

class User extends Model {
    constructor(id){
        super(id);
    }

    static get schema(){
        return {
            id:{type:"Number", primary:true},
            name:{type:"String"}
        }
    }
    
    static get snapshotFrequency(){
        return 10;
    }
}
1.1.1

6 years ago

1.1.0

6 years ago

1.0.3

6 years ago

1.0.1

6 years ago

1.0.0

6 years ago