Quick start

Here we will show you how to use basic methods to apply changes and fetch items from your database. More detailed examples can be found in our Cookbook.

Prerequisites

For this guide, we will assume you have both node and npm installed. Your node version must be 6.0 or above to be compatible with all ES6 features. We will also assume you have one of the databases supported by wetland.

Creating an entity

One way of creating your entities is creating a file for each of them on the same directory. In this example, we will store them in ./entities. Mapping methods are used to configure your entity's schema. Here is what an entity file looks like:

class User {
  static setMapping(mapping) {
    mapping.forProperty('id').primary().increments();
    mapping.field('name', {type: 'string', size: 40});
    mapping.field('phone', {type: 'integer'});
  }
}

module.exports.User = User;
export class User {
  static setMapping(mapping) {
    mapping.forProperty('id').primary().increments();
    mapping.field('name', {type: 'string', size: 40});
    mapping.field('phone', {type: 'integer'});
  }
}

Implementing wetland

In this quick start, we will register both our entities and our default store upon wetland's instantiation. Wetland will fetch all entity files on the wetland.entityPath directory and register each one of them. There are many ways to register entities and stores, detailed examples can be found in our Cookbook.

const Wetland = require('wetland').Wetland;
const wetland = new Wetland({
  stores: {
    defaultStore: {
      client    : 'mysql',
      connection: {
        host    : '127.0.0.1',
        username: 'root',
        database: 'my_database'
      }
    }
  },
  entityPath: __dirname + './entities' 
});
import {Wetland} from 'wetland';

const wetland = new Wetland({
  stores: {
    defaultStore: {
      client    : 'mysql',
      connection: {
        host    : '127.0.0.1',
        username: 'root',
        database: 'my_database'
      }
    }
  },
  entityPath: __dirname + './entities' 
});

"I'd like to speak to the manager!"

To instantiate the entity manager all you need to do is call .getManager() on your wetland instance. The entity manager distributes scopes and supplies some core methods to be used in your application.

let manager = wetland.getManager();
let manager = wetland.getManager();

Creating new rows

Creating a new row is very easy. All you have to do is create a new instance of your entity and add its properties like with any object.

let newUser  = new User();
newUser.name = 'Wesley';

Persisting changes

To persist changes into your database, call .persist() to stage this entity to be persisted and .flush() to apply those changes.

manager.persist(newUser).flush().then(() => console.log('New user created'));

Fetching from the database

To fetch rows from your database, call .getRepository() to specify which table you are fetching from and .find() to fetch based on your criteria.

manager.getRepository(User).find({name: 'Wesley'}).then();

results matching ""

    No results matching ""