Git Product home page Git Product logo

persistor's Introduction

CircleCI npm version

Persistor

Description

Persistor is a subclass of SuperType that serializes to and reconstitutes from mongoDB or PostgreSQL.

Installation

It is automatically installed with Amorphic

Example

First create some object templates (many-to-many example):

var ObjectTemplate = require('@havenlife/supertype').default;
var PersistObjectTemplate = require('persistor')(ObjectTemplate, null, ObjectTemplate);

var Customer = PersistObjectTemplate.create("customer:Customer", {
    email:		{type: String},
    firstName:  {type: String},
    middleName: {type: String},
    lastName:	{type: String}
});

var Account = PersistObjectTemplate.create("account:Account", {
    init:       function (number, title, customer) {
        this.number = number;
        this.title = title;
        if (customer)
            this.addCustomer(customer);
    },
    addCustomer: function(customer, relationship) {
        var role = new Role(customer, this, relationship);
        this.roles.push(role);
        customer.roles.push(role);
    },
    number:     {type: Number},
    title:      {type: Array, of: String, max: 4}
});

var Role = PersistObjectTemplate.create("role:Role", {
    init:       function (customer, account, relationship) {
        this.customer = customer;
        this.account = account;
        if (relationship)
            this.relationship = relationship;
    },
    relationship: {type: String, value: "primary"},
    customer:     {type: Customer}
});

Customer.mixin({
    roles:      {type: Array, of: Role, value: [], fetch: true}
});

Account.mixin({
    roles:      {type: Array, of: Role, value: [], fetch: true}
});

Next you need a schema but this schema only defines the foreign key relationships

var collections = {
    customer: {
        children: {
            roles: {template: Role, id:"customer_id"}
        }
    },
    account: {
        children: {
            roles: {id:"account_id"}
        }
    },
    role: {
        parents: {
            customer: {id: 'customer_id'},
            account: {id: 'account_id'}
        }
    },
};

In the schema, the high level properties, customer, account and role are the names of the documents and must match the names of the documents in the template definition which is always :

var Role = PersistObjectTemplate.create("role:Role", {

Then open your database and set the schema

return Q.ninvoke(MongoClient, "connect", "mongodb://localhost:27017/testpersist").then(function (db) {
    PersistObjectTemplate.setDB(db);
    PersistObjectTemplate.setSchema(collections);
    return Q(db);
}).then ....;

Create some objects

var sam = new Customer("Sam", "M", "Elsamman");
var karen = new Customer("Karen", "", "Burke");
var account = new Account(123, ['Sam Elsamman', 'Karen Burke'], sam);
account.addCustomer(karen, "joint");

Persist the top level and any connected dirty objects will automatically persist as well, managing all foreign keys needed to maintain the relationships.

sam.persistSave().then(function() {
    // id inserted after saving
    return Q(sam._id);
}).then ....

And retrieve stuff back by id, cascading down to fetch the roles as well.

Customer.getFromPersistWithId(customer_id, {roles: true}).then (function (customer) {
    // Customer object complete with collection of role objects fetched and instantiated
});

Or fetch with a query

Customer.getFromPersistWithQuery({lastName: "Elsamman"}, {roles: true}).then (function (customer) {
    // Customer object complete with collection of role objects fetched and instantiated
});

The account object connected to the fetched role is also automatically when a role is fetched because the template specified {fetch: true}. Had it not been specified you would manually fetch the related account object like this

Customer.getFromPersistWithId(customer_id, {roles: true}).then (function (customer) {
    customer.role[0].fetch({account: true}).then(function() {
        // Now you can reference customer.role[0].account
    });
});

Debugging the container

1. Remove local node_modules folder and delete package-lock file and run the following commnd to launch the container in debug mode.
    npm run test:docker:debug

    If you are using Visual Studio Code, you can use the following setting to attach to the debugging container.
        {
            "type": "node",
            "request": "attach",
            "name": "persistor_debug",
            "port": 5858,
            "localRoot": "${workspaceFolder}",
            "remoteRoot": "/app"
        }

Debugging in the local environment.

    2. Make sure to install the mongodb and postgres locally and set the environment variables from the test.local.env file.
        If you are using Visual Studio Code, you can use the following setting to debug.
             {
                "type": "node",
                "request": "launch",
                "name": "Mocha Tests",
                "program": "${workspaceFolder}/node_modules/mocha/bin/_mocha",
                "args": [
                    "-u",
                    "tdd",
                    "--timeout",
                    "999999",
                    "--colors",
                    "${workspaceFolder}/test"
                ],
                "envFile": "${workspaceFolder}/test.local.env",
                "internalConsoleOptions": "openOnSessionStart"
            }

License

Persistor is licensed under the MIT license

persistor's People

Contributors

csmastermind avatar hackerrdave avatar kumar29 avatar nikmash avatar nsaenz7 avatar selsamman avatar srksag avatar sshankar19 avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

persistor's Issues

handle null in getTemplateFromKnexPOJO

in PersistObjectTemplate.getTemplateFromKnexPOJO, if value=null for isRemoteDoc, it logs:
ERROR: query[getTemplateFromKnexPOJO]: (__amorphicContext={} component="persistor" module="query" activity="getTemplateFromKnexPOJO" data="there was a problem. remote object key must be a string")

it shouldn't log out the Error.

Re-add Persistor type to static create()

/**
 * @TODO: was typed `Persistor` but that's weird? doesn't work need to figure out what's going on.
 */
static create(): any | undefined {return undefined}; 

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.