Configuration

To get your application up and running, you need to set some things up first. In this section we are going to give you an example of how to create a simple todo application using wetland. We already made an example of a todo application, feel free to take a look at the code or try it yourself.

Creating a schema

The schema will define the fields, properties and relationships of the entity. Here are the two entities we are going to use in this example. You can find all mapping options described on the Mapping chapter.

List schema

List:

  • name: VARCHAR(24)
  • todos: collection Todo
// ./entities/list
let Todo = require('./todo');

class List {
  static setMapping(mapping) {
    mapping.forProperty('id').primary().increments();
    mapping.field('name', {type: 'string', size: 24});
    mapping.forProperty('todos')
      .oneToMany({targetEntity: Todo, mappedBy: 'list'})
      .cascade(['persist']);
  }
}

module.exports = List;
import {Todo} from './todo';

export class List {
  static setMapping(mapping) {
    mapping.forProperty('id').primary().increments();ls 
    mapping.field('name', {type: 'string'});
    mapping.forProperty('todos')
      .oneToMany({targetEntity: Todo, mappedBy: 'list')
      .cascade(['persist', 'remove']);
  }
}
Todo schema

Todo:

  • task: VARCHAR
  • done: BOOLEAN
  • list_id: INT
// ./entities/todo
let List = require('./list');

class Todo {
  static setMapping(mapping) {
    mapping.forProperty('id').primary().increments();
    mapping.field('task', {type: 'string'});
    mapping.field('done', {type: 'boolean', defaultTo: false});
    mapping.forProperty('list')
      .manyToOne({targetEntity: List, inversedBy: 'todos'})
      .joinColumn({onDelete: 'cascade'});
  }
}

module.exports = Todo;
import {List} from './list';

export class Todo {
  static setMapping(mapping) {
    mapping.forProperty('id').primary().increments();
    mapping.field('task', {type: 'string'});
    mapping.field('done', {type: 'boolean', defaultTo: false});
    mapping.forProperty('list')
      .manyToOne({targetEntity: Todo, inversedBy: 'todos'})
      .joinColumn({onDelete: 'cascade'});
  }
}

Implementing wetland

Implementation and usage of wetland is quite simple. First, you must require wetland and then create an instance. You can register both your stores and your entities upon creating a new instance, as we are demonstrating in this section. Other possibilities will be described on Chapter @. For demonstrational purposes, we are going to use MySQL as our database.

Database configuration

You can use multiple databases to store you entities. If you choose not to set a default database, do not forget to specify which database you want to use for each of your entities @CHANGE THIS. If no stores are given, it defaults to an empty object.

const Wetland = require('wetland');

let wetland = new Wetland({
  stores: {
    defaultStore: {
      client: 'mysql',
      connection: {
        user    : 'your-username',
        database: 'your-database'
      }
    }
  }
});
import {Wetland} from 'wetland';

const wetland = new Wetland({
  stores: {
    defaultStore: {
      client: 'mysql',
      connection: {
        user    : 'your-username',
        database: 'your-database'
      }
    }
  }
});

Registering entities

In this example we are registering our entities upon creating the wetland's instance, but there are many other convenient methods to register your entities and we are going to demonstrate how to use each one of them on Chapter @.

Using an entity array
const Wetland = require('wetland');
const List    = require('./entities/list');
const Todo    = require('./entities/todo');

let wetland = new Wetland({
  stores  : {...},
  entities: [List, Todo]
});
import {Wetland} from 'wetland';
import {List} from './list';
import {Todo} from './todo';

const wetland = new Wetland({
  stores  : {...},
  entities: [List, Todo]
});
Using entities path
const Wetland = require('wetland');

let wetland = new Wetland({
  stores : {...},
  entityPath: __dirname + './entities'
});
import {Wetland} from 'wetland';

const wetland = new Wetland({
  stores : {...},
  entityPath: __dirname + './entities'
});

results matching ""

    No results matching ""