Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to use h2 in Nodejs #1

Open
damasofc opened this issue Jul 25, 2018 · 1 comment
Open

How to use h2 in Nodejs #1

damasofc opened this issue Jul 25, 2018 · 1 comment

Comments

@damasofc
Copy link

damasofc commented Jul 25, 2018

Hi, I want to know how I call the functions that you've made, in the case when I use MongoDb I do something like this:
`var mongoose = require('mongoose');

mongoose.connect('mongodb://user:passwordDB@ds253821.mlab.com:53821/addressbook')

var Schema = mongoose.Schema;

var personSchema = new Schema({
firstname: String,
lastname: String,
addres: String
});

var Person = mongoose.model('Person',personSchema);

var john = Person({
firstname: 'Damaso',
lastname: 'Fernandez',
addres: '55 Main st.'
});

john.save((err) => {
if(err) throw err;
console.log('Person Saved!!');
});`

could you please guide me how can I do something like this with the code that you made,

Thank you in advance for your answer.

@kbarbounakis
Copy link
Collaborator

Hello

MOST Data H2 Adapter (@themost/h2) is one of the available adapters of @themost/data which of MOST Web Framework

You can use @themost/cli tool in order to create a new data project (you can read more about this in the previous link)

@themost/data is a fully configuration ORM with several features that helps developers to build scalable applications. Now @themost/data is in version 2.x focused on ES7 and Typescript. You can read more about MOST Data Module features in the previous version of most-data.

@themost/data is an ORM based on configurable data schemas. So first of all you need to create a basic application structure:

+ config
     + models
           + Person.json
  app.json

config/models/Person.json schema holds Person schema e.g.

{
   "name": "Person",
   "version": "1.0",
   "fields": [
       {
           "name": "id",
           "type": "Counter",
           "primary": true
       },
       {
           "name": "givenName",
           "type": "Text"
       },
       {
           "name": "familyName",
           "type": "Text"
       },
       {
           "name": "email",
           "type": "Text"
       },
       {
           "name": "address",
           "type": "Text"
       }
       ]
}

config/models/app.json is the application configuration file. This file contains data adapters configuration and othe configuration settings. The following configuration file holds information about a local H2 database:

{
    "adapterTypes": [
        { "name":"H2 Data Adapter", "invariantName": "h2", "type":"@themost/h2" }
    ],
    "adapters": [
        { "name":"development_h2", "invariantName":"h2", "default":true,
            "options": {
               "path":"./test",
               "user":"SA",
               "password":""
           }
        }
    ]
}

Install core modules:

npm install @themost/data
npm install @themost/2

and create a test script which is equivalent with your script:

const path = require("path");
const util = require("util");
const DataConfiguration = require("@themost/data").DataConfiguration;
const DefaultDataContext = require("@themost/data").DefaultDataContext;

//load application configuration
const config = new DataConfiguration(path.resolve(process.cwd(), __dirname, 'config'));
/**
 * @description Test data context
 * @class
 */ 
function TestDataContext(configuration) {
    TestDataContext.super_.bind(this)();
    this.getConfiguration = function() {
        return configuration;
    };
}
util.inherits(TestDataContext, DefaultDataContext);

//initialize test data context
let context = new TestDataContext(config);

//try to find person
context.model('Person').where('email').equal('john.barnes@example.com').getItem().then(function(res) {
    if (typeof res === 'undefined') {
        //if person cannot be found
        let person = {
            givenName: 'John',
            familyName: 'Barnes',
            email: 'john.barnes@example.com',
            address: '23 Narrow Round'
        };
        //save new person
        return context.model('Person').save(person).then(function() {
            //and finalize context
            context.finalize(function() {
                console.log('INFO', 'NEW PERSON', person);      
            });
        });
    }
    else {
        context.finalize(function() {
                console.log('INFO', 'PERSON', res);    
            });
    }
    
}).catch(function(err) {
    context.finalize(function() {
        console.error(err);
    });
});

Of course the previous example is a basic test script. If you want to create a data application based on @themost/data ORM please read the instructions provided in:

MOST Web Framework (v2.x)
https://github.com/themost-framework/themost

MOST Data ORM Module (v 1.x):
https://github.com/kbarbounakis/most-data

I hope these instructions will help you to start using @themost/data ORM and its powerful features.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants