1.3.2 • Published 5 months ago

backend-js v1.3.2

Weekly downloads
9
License
MIT
Repository
github
Last release
5 months ago

backend-js Codacy Badge

0_00

Backend-js is a layer built above expressjs and socket.io to enable behaviours framework for nodejs applications.

Installation

npm install backend-js

Usage

backend

var backend = require('backend-js');
var App = backend.app(__dirname + '/behaviours', {
    path: '/api/v1',
    parser: 'json',
    port: 8383,
    origins: '*'
});
var App = app(path, options)
parametertypedescription
pathstringpath of behaviours directory.
optionsobjectapp configurations object.
options.pathstringprefix path appended to the beginning of routes.
options.parserstringif json, text, raw or urlencoded is used, the body of the request will be parse accordingly also the body of the response will be serialized accordingly.
options.parserOptionsobjectoptions for parser.
options.portnumberport of server.
options.originsstringcomma separated domains allowed to send ajax requests to this server or "*" to allow any.
options.staticobjectoptions object to define static served files.
options.static.routestringvirtual path/route for static served files.
options.static.pathstringrelative path of the directory of static served files.
returntypedescription
Appfunctionfunction conventionally denotes the Express application.

model

var backend = require('backend-js');
var model = backend.model();
var User = model({
  name: 'User'
}, {
  username: String,
  password: String
});
var ModelEntity = model(options, attributes, plugins)
parametertypedescription
optionsstring | objecteither model name for lazy loading or object for model configuration.
options.namestringmodel name.
options.featuresobjectobject contains special functionalities of the model. It is passed to data access layer.
options.queryarrayarray of QueryExpression repressing the query to be executed by default.
attributesobjectobject describes the model schema. it contains key-value pairs where the key is a model attribute/field name and the value is the data type of this attribute/field. Data types are native javascript data types String, Number and Date. Data type could be javascript array of single object annotation [{}] or just an object annotation {} containing other key-value pairs expressing nested model schema.
pluginsarrayarray of mongoose plugins to define additional functionalities to the model.
returntypedescription
ModelEntityfunctionmodel constructor function prototyped as ModelEntity.

query

var QueryExpression = backend.QueryExpression;
var ComparisonOperators = {
    EQUAL: '=',
    NE: '$ne'
};
var LogicalOperators = {
    AND: '$and',
    OR: '$or',
    NOT: '$not'
};
backend.setComparisonOperators(ComparisonOperators);
backend.setLogicalOperators(LogicalOperators);
var query = [new QueryExpression({
    fieldName: 'username',
    comparisonOperator: ComparisonOperators.EQUAL,
    fieldValue: 'name'
}),new QueryExpression({    
    fieldName: 'password',
    comparisonOperator: ComparisonOperators.EQUAL,
    fieldValue: 'pass',
    logicalOperator: LogicalOperators.AND,
    contextualLevel: 0
})]
setComparisonOperators(operators)
setLogicalOperators(operators)
parametertypedescription
operatorsobjectobject contains key-value pairs where the key is a unique id for an operator and the value is a corresponding database engine operator. It is passed to data access layer.
var expression = new QueryExpression(options)
parametertypedescription
optionsobjectobject describes a condition in a where clause of a query.
options.fieldNamestringattribute/field name of the model to be part of the condition.
options.comparisonOperatorstringa value represents comparison operation to be manipulated by database engine.
options.fieldValueanythe value to be compared to the attribute/field of the model.
options.logicalOperatorstringa value represents logical operation to be manipulated by database to combine multiple conditions.
options.contextualLevelnumberstarts with 0 represents the depth of the logical operation in the conditions tree. It is used to indicate brackets.
returntypedescription
expressionobjectobject represents a condition expression combined with other expressions to represent a query. It is adapted by data access layer..

entity

var ModelEntity = backend.ModelEntity;
var entity = new ModelEntity({});
var model = entity.getObjectConstructor();
var schema = entity.getObjectAttributes();
var features = entity.getObjectFeatures();
var query = entity.getObjectQuery();
var entity = new ModelEntity(features)
parametertypedescription
featuresobjectobject contains special functionalities of the model. It is passed to data access layer.
returntypedescription
entityobjectobject contains all specifications and meta data of the model.
entity.getObjectConstructorfunctionfunction returns the model constructor depending on the data access layer.
entity.getObjectAttributesfunctionfunction returns the model schema key-value pairs.
entity.getObjectFeaturesfunctionfunction returns the model features.
entity.getObjectQueryfunctionfunction returns the model query an array of QueryExpression to be executed by default.

behaviour (API / functional code unit)

var getUsers = behaviour({
  name: 'GetUsers',
  version: '1',
  path: '/users',
  method: 'GET'
}, function(init) {
  return function() {
    var self = init.apply(this, arguments).self();
    self.begin('Query', function(key, businessController, operation) {
        operation
          .entity(new User())
          .append(true)
          .apply();
      });
  };
});
var Behavior = behaviour(option, constructor);
parametertypedescription
optionsobjectapi configuration (name, version, path, method, parameters, returns)
constructorfunctionlogic function works by registering on methods to do functions regardless its orders, like (database processor query, insert, delete or update), data mapping to map returns of data to specific format or server error handling

data access

you should define your own data access layer like following

var backend = require('backend-js');

var ModelController = function () {
    self.removeObjects = function (queryExprs, entity, callback) {
        // do remove
    };
    self.addObjects = function (objsAttributes, entity, callback) {
        // do add new
    };
    self.getObjects = function (queryExprs, entity, callback) {
        // do select
    };
    self.save = function (callback, oldSession) {
        // do select
    };
};

ModelController.defineEntity = function (name, attributes) {
    // define entity
    return entity;
};

ModelController.prototype.constructor = ModelController;

backend.setModelController(new ModelController());

Starter project

A sample project that you can learn from examples how to use Backend-JS.

https://github.com/QuaNode/BeamJS-Start

1.3.2

5 months ago

1.3.1

7 months ago

1.3.0

8 months ago

1.2.10

12 months ago

1.2.9

12 months ago

1.2.8

12 months ago

1.2.6

12 months ago

1.2.5

1 year ago

1.2.4

1 year ago

1.2.3

1 year ago

1.2.2

1 year ago

1.2.0

2 years ago

1.2.1

2 years ago

1.1.8

2 years ago

1.1.7

2 years ago

1.1.6

2 years ago

1.1.5

2 years ago

1.1.4

2 years ago

1.1.3

2 years ago

1.1.2

2 years ago

1.1.1

3 years ago

1.1.0

3 years ago

1.0.9

3 years ago

1.0.8

3 years ago

1.0.7

3 years ago

1.0.1

3 years ago

1.0.6

3 years ago

1.0.5

3 years ago

1.0.4

3 years ago

1.0.3

3 years ago

1.0.0

3 years ago

0.9.9-79

4 years ago

0.9.9-77

4 years ago

0.9.9-78

4 years ago

0.9.9-76

4 years ago

0.9.9-75

4 years ago

0.9.9-74

4 years ago

0.9.9-73

4 years ago

0.9.9-71

4 years ago

0.9.9-72

4 years ago

0.9.9-70

4 years ago

0.9.9-69

4 years ago

0.9.9-68

5 years ago

0.9.9-67

5 years ago

0.9.9-66

5 years ago

0.9.9-65

5 years ago

0.9.9-64

5 years ago

0.9.9-63

5 years ago

0.9.9-62

5 years ago

0.9.9-61

5 years ago

0.9.9-60

5 years ago

0.9.9-59

5 years ago

0.9.9-58

5 years ago

0.9.9-57

5 years ago

0.9.9-56

5 years ago

0.9.9-55

5 years ago

0.9.9-54

5 years ago

0.9.9-53

5 years ago

0.9.9-52

6 years ago

0.9.9-51

6 years ago

0.9.9-50

6 years ago

0.9.9-49

6 years ago

0.9.9-48

6 years ago

0.9.9-47

6 years ago

0.9.9-46

6 years ago

0.9.9-45

6 years ago

0.9.9-44

6 years ago

0.9.9-43

6 years ago

0.9.9-42

6 years ago

0.9.9-41

6 years ago

0.9.9-40

6 years ago

0.9.9-39

6 years ago

0.9.9-37

6 years ago

0.9.9-36

6 years ago

0.9.9-35

6 years ago

0.9.9-34

6 years ago

0.9.9-33

6 years ago

0.9.9-32

6 years ago

0.9.9-31

6 years ago

0.9.9-30

7 years ago

0.9.9-29

7 years ago

0.9.9-28

7 years ago

0.9.9-27

7 years ago

0.9.9-26

7 years ago

0.9.9-25

7 years ago

0.9.9-24

7 years ago

0.9.9-23

7 years ago

0.9.9-22

7 years ago

0.9.9-21

7 years ago

0.9.9-20

7 years ago

0.9.9-19

7 years ago

0.9.9-18

7 years ago

0.9.9-17

7 years ago

0.9.9-16

7 years ago

0.9.9-15

7 years ago

0.9.9-14

7 years ago

0.9.9-13

7 years ago

0.9.9-12

7 years ago

0.9.9-11

7 years ago

0.9.9-10

7 years ago

0.9.9-9

7 years ago

0.9.9-8

7 years ago

0.9.9-7

7 years ago

0.9.9-6

7 years ago

0.9.9-5

7 years ago

0.9.9-4

7 years ago

0.9.9-3

7 years ago

0.9.9-2

8 years ago

0.9.9-1

8 years ago

0.9.9

8 years ago

0.9.8

8 years ago

0.9.7

8 years ago

0.9.6

8 years ago

0.9.5

8 years ago

0.9.4

8 years ago

0.9.3

8 years ago

0.9.2

8 years ago

0.9.1

8 years ago

0.9.0

8 years ago