@themost/data
MOST Web Framework Codename Blueshift - Data module
Last updated 19 days ago by themost .
BSD-3-Clause · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install @themost/data 
SYNC missed versions from official npm registry.

npm GitHub top language License GitHub last commit GitHub Release Date npm Snyk Vulnerabilities for npm package

@themost/data

MOST Web Framework 2.0 Codename Blueshift Data Module

License: BSD-3-Clause

Install

npm i @themost/data

Development

This repo is a submodule of MOST Web Framework

git clone http://github.com/themost-framework/themost.git

Features

JSON schemas

Each data model is represented by a JSON schema which contains the complete model definition including fields, constraints, triggers, associations, privileges, data views etc:

{
	"name": "User", "id": 90, "title": "Users", "inherits": "Account", "hidden": false, "sealed": false, "abstract": false, "version": "1.4",
	"fields": [
		{
			"name": "id", "title": "Id", "description": "The identifier of the item.",
			"type": "Integer",
			"nullable": false,
			"primary": true
		},
        {
            "name": "accountType",  "title": "Account Type", "description": "Contains a set of flags that define the type and scope of an account object.",
            "type": "Integer",
            "readonly":true,
            "value":"javascript:return 0;"
        },
		{
			"name": "lockoutTime", "title": "Lockout Time", "description": "The date and time that this account was locked out.",
			"type": "DateTime",
			"readonly": true
		},
		{
			"name": "logonCount", "title": "Logon Count", "description": "The number of times the account has successfully logged on.",
			"type": "Integer",
			"value": "javascript:return 0;",
			"readonly": true
		},
		{
			"name": "enabled", "title": "Enabled", "description": "Indicates whether a user is enabled or not.",
			"type": "Boolean",
			"nullable": false,
			"value": "javascript:return true;"
		},
		{
			"name": "lastLogon", "title": "Last Logon", "description": "The last time and date the user logged on.",
			"type": "DateTime",
			"readonly": true
		},
		{
			"name": "groups", "title": "User Groups", "description": "A collection of groups where user belongs.",
			"type": "Group",
			"expandable": true,
			"mapping": {
				"associationAdapter": "GroupMembers", 
        "parentModel": "Group",
				"parentField": "id", 
        "childModel": "User", 
        "childField": "id",
				"associationType": "junction", 
        "cascade": "delete"
			}
		},
		{
			"name": "additionalType",
            "value":"javascript:return this.model.name;",
            "readonly":true
		},
		{
			"name": "accountType",
			"value": "javascript:return 0;"
		}
	], 
  "privileges":
  [
        { "mask":1, 
          "type":"self", 
          "filter":"id eq me()" 
        },
        { "mask":15, 
          "type":"global", 
        }
    ],
    "constraints":[
        {
            "type":"unique",
            "fields": [ "name" ]
        }
    ],
	  "eventListeners": [
		{ "name":"New User Credentials Provider", "type":"/app/controllers/user-credentials-listener" }
    ],
    "seed":[
        {
            "name":"anonymous",
            "description":"Anonymous User",
            "groups":[
                { "name":"Guests" }
            ]
        },
		{
			"name":"admin@example.com",
			"description":"Site Administrator",
			"groups":[
				{ "name":"Administrators" }
			]
		}
    ]
}

Automatic migration

Each data model will be automatically migrated after its first use. DataModel.version property enables data model versioning:

{
    "name": "User", "id": 90, "title": "Users", "inherits": "Account", "hidden": false,
    "sealed": false, "abstract": false, "version": "1.4",
    "fields": [ .. ]
    ...
}

Data model inheritance

MOST data models support model inheritance. DataModel.inherits property defines the inherited model of data model.

//User model inherits Account model
{
    "name": "User", "id": 90, "title": "Users", "inherits": "Account", "hidden": false,
    "sealed": false, "abstract": false, "version": "1.4",
    "fields": [ .. ]
    ...
}

Constraint validations

Each data model may have one or more constraints for validating data object state:

//User model has a unique constraint based on user name.
...
"constraints":[
    {
        "description": "User name must be unique across different records.",
        "type":"unique",
        "fields": [ "name" ]
    }
]
...

Validation

Data field definition allows developers to specify validation rules (nullable, readonly etc):

...
{
    "name": "enabled", "title": "Enabled", "description": "Indicates whether a user is enabled or not.",
    "type": "Boolean",
    "nullable": false,
    "value": "javascript:return true;"
},
{
    "name": "dateCreated",
    "title": "Date Created",
    "description": "The date on which this item was created.",
    "type": "Date",
    "value":"javascript:return (new Date());",
    "readonly":true
}
...

Calculated values

Data field definition allows developers to specify default and calculated values:

...
{
    "name": "dateModified",
    "title": "Date Modified",
    "description": "The date on which this item was most recently modified.",
    "type": "Date",
    "readonly":true,
    "value":"javascript:return (new Date());",
    "calculation":"javascript:return (new Date());"
}
...

One-to-many associations

Each data model may have one-to-many associations with other models.

{
    "name": "Order", "id": 449, "title": "Order", "hidden": false, ...
    "fields": [
        ...
        {
            "name": "customer",
            "title": "Customer",
            "description": "Party placing the order.",
            "type": "Party",
            "expandable":true
        }
        ...
    ]
}

Many-to-many associations

A data model may have many-to-many assocations with other models.

...
{
    "name": "groups", "title": "User Groups", "description": "A collection of groups where user belongs.",
    "type": "Group",
    "expandable": true,
    "mapping": {
        "associationAdapter": "GroupMembers", "parentModel": "Group",
        "parentField": "id", "childModel": "User", "childField": "id",
        "associationType": "junction", "cascade": "delete"
    }
}
...

Filtering

Use advanced data object filtering:

    //get customers orders with status delivered
    context.model('Order').where('orderStatus/alternateName').equal('OrderedDelivered')
    .and("customer").equal(257)
     .list().then(function(result) {
            done(null, result);
        }).catch(function(err) {
            done(err);
        });

DataQueryable class offers a wide set of methods for filtering, paging, ordering and grouping data objects.

Ordering

Use data object ordering:

//get orders order by order date
context.model('Order').where('orderStatus/alternateName').equal('OrderProcessing')
.orderBy('orderDate').thenBy('paymentMethod/name')
 .list().then(function(result) {
        done(null, result);
    }).catch(function(err) {
        done(err);
    });

Paging

Use paging parameters for retrieved a paged resultset.

//skip 15 orders and take next 15
context.model('Order').where('orderStatus/alternateName').equal('OrderProcessing')
.take(15).skip(15)
 .list().then(function(result) {
        done(null, result);
    }).catch(function(err) {
        done(err);
    });

The result of a paged query is an instance of DataResultSet class.

Data query field selection

DataQueryable class allows developers to define a collection of selected fields which are going to be included in the result. This collection may contain fields which belong to the current model and others which belong to models associated with the current.

context.model('Order').where('orderStatus/alternateName').equal('OrderProcessing')
.select('id','orderDate','orderedItem/name as productName', 'customer/description as customerName')
.take(15)
 .list().then(function(result) {
        done(null, result);
    }).catch(function(err) {
        done(err);
    });

Grouping

Use data object grouping and produce on-the-fly statistical results based on the given parameters:

//count orders by order status
context.model('Order')
.select('count(id) as orderCount','orderStatus/name as orderStatusName')
.groupBy('orderStatus/name')
.where('orderDate').getMonth().equal(10).and('orderDate').getFullYear().equal(2015)
 .all().then(function(result) {
        done(null, result);
    }).catch(function(err) {
        done(err);
    });

Event listeners

Data model definition may contain a collection of event listeners which represents a set of procedures that are going to be executed before and after inserting, updating or deleting a data object.

//Order.json schema
...
"eventListeners": [
    { "name":"New Order Notification", "type":"/app/controllers/new-order-listener" }
]
...

//new-order-listener.js listener
...
exports.afterSave = function(event, callback) {
    if (event.state!=1) { return callback(); }
    //use most web mailer module
    var mm = require("most-web-mailer");
     mm.mailer(context).subject("New Order Notification")
     .subject("New Order")
     .template("new-order-notification")
     .to("employee1@example.com")
     .test()
     .send(event.target, function(err, res) {
        if (err) { return done(err); }
        return done();
    });
}

Privileges

Data model definition may contain a collection of privileges which should be given in users or groups. User access rights will be validated during read, insert, update or delete data operations.

...
"privileges":[
    { "mask":1, "type":"self", "filter":"id eq me()" },
    { "mask":15, "type":"global", "account":"*" }
]
...

Caching

The @themost/data module allows developers to use data caching mechanisms while getting data. DataModel.caching property indicates whether data will be cached or not.

{
    "name": "OrderStatus", "id": 9507079, "title": "Order Status", "hidden": false, "sealed": false,
    "abstract": false, "version": "1.1", "caching":"always"
}

Seeding

Data model definition may contain a seed property which is a collection of data items to be inserted when model will be migrated for the first time.

{
    "name": "OrderStatus", "id": 9507079, "title": "Order Status", "hidden": false,
    "sealed": false, "abstract": false, "version": "1.1",
    ...
    "fields": [
    ...
    ],
    ..
    "seed": [
        {  "id":1, "name":"Delivered", "alternateName":"OrderDelivered", "description":"Representing the successful delivery of an order."
        }, {
            "id":2, "name":"Cancelled", "alternateName":"OrderCancelled", "description":"Representing the cancellation of an order."
        }, {
            "id":3, "name":"In Transit", "alternateName":"OrderInTransit", "description":"Representing that an order is in transit."
        }, {
            "id":4, "name":"Payment Due", "alternateName":"OrderPaymentDue", "description":"Representing that payment is due on an order."
        }, {
            "id":5, "name":"Pickup", "alternateName":"OrderPickup", "description":"Representing availability of an order for pickup."
        }, {
            "id":6, "name":"Processing", "alternateName":"OrderProcessing", "description":"Representing that an order is being processed."
        }, {
            "id":7, "name":"Problem", "alternateName":"OrderProblem", "description":"Representing that there is a problem with the order."
        }, {
            "id":8, "name":"Returned", "alternateName":"OrderReturned", "description":"Representing that an order has been returned."
        }
    ]
}

Data Adapters

There are different data connectors for the most popular database engines.

  • SQLite
  • MySQL
  • MSSQL
  • Oracle
  • PostgreSQL
  • H2

See further instructions here for using one of the available data adapters according to your development or production environment.

Current Tags

  • 2.5.15                                ...           latest (19 days ago)
  • 2.5.15-next.1                                ...           next (a month ago)

80 Versions

  • 2.5.15                                ...           19 days ago
  • 2.5.15-next.1                                ...           a month ago
  • 2.5.14                                ...           a month ago
  • 2.5.12                                ...           a month ago
  • 2.5.11                                ...           a month ago
  • 2.5.10                                ...           a month ago
  • 2.5.9                                ...           2 months ago
  • 2.5.9-dev.2                                ...           2 months ago
  • 2.5.8                                ...           2 months ago
  • 2.5.7                                ...           2 months ago
  • 2.5.6                                ...           2 months ago
  • 2.5.5                                ...           3 months ago
  • 2.5.4                                ...           3 months ago
  • 2.5.3                                ...           5 months ago
  • 2.5.2                                ...           6 months ago
  • 2.5.0                                ...           6 months ago
  • 2.4.9                                ...           6 months ago
  • 2.4.8                                ...           6 months ago
  • 2.4.7                                ...           7 months ago
  • 2.4.5                                ...           a year ago
  • 2.4.4                                ...           a year ago
  • 2.4.3                                ...           a year ago
  • 2.4.3-dev.2 [deprecated]           ...           a year ago
  • 2.4.3-dev.1                                ...           a year ago
  • 2.4.2                                ...           a year ago
  • 2.4.2-dev.1                                ...           a year ago
  • 2.4.1-dev.1 [deprecated]           ...           a year ago
  • 2.4.1                                ...           a year ago
  • 2.4.0-dev.2 [deprecated]           ...           a year ago
  • 2.4.0-dev.1 [deprecated]           ...           a year ago
  • 2.3.6                                ...           a year ago
  • 2.3.5                                ...           a year ago
  • 2.3.4                                ...           a year ago
  • 2.3.3                                ...           a year ago
  • 2.3.2                                ...           a year ago
  • 2.3.2-dev.3                                ...           a year ago
  • 2.3.2-dev.1                                ...           a year ago
  • 2.3.1                                ...           a year ago
  • 2.3.1-dev.2                                ...           a year ago
  • 2.3.1-dev.1                                ...           a year ago
  • 2.2.4                                ...           a year ago
  • 2.2.2                                ...           a year ago
  • 2.2.1                                ...           a year ago
  • 2.1.20                                ...           2 years ago
  • 2.1.19                                ...           2 years ago
  • 2.1.18                                ...           2 years ago
  • 2.1.17                                ...           2 years ago
  • 2.1.16                                ...           2 years ago
  • 2.1.15                                ...           2 years ago
  • 2.1.14                                ...           2 years ago
  • 2.1.12                                ...           2 years ago
  • 2.1.11                                ...           2 years ago
  • 2.1.10                                ...           2 years ago
  • 2.1.9                                ...           2 years ago
  • 2.1.5                                ...           2 years ago
  • 2.1.4                                ...           2 years ago
  • 2.1.3                                ...           2 years ago
  • 2.1.2                                ...           2 years ago
  • 2.1.1                                ...           2 years ago
  • 2.0.33                                ...           2 years ago
  • 2.0.32                                ...           2 years ago
  • 2.0.29                                ...           2 years ago
  • 2.0.28                                ...           2 years ago
  • 2.0.27                                ...           2 years ago
  • 2.0.26                                ...           2 years ago
  • 2.0.25                                ...           2 years ago
  • 2.0.24                                ...           2 years ago
  • 2.0.22                                ...           2 years ago
  • 2.0.21                                ...           2 years ago
  • 2.0.20                                ...           2 years ago
  • 2.0.19                                ...           2 years ago
  • 2.0.18                                ...           2 years ago
  • 2.0.17                                ...           2 years ago
  • 2.0.16                                ...           2 years ago
  • 2.0.15                                ...           2 years ago
  • 2.0.14                                ...           2 years ago
  • 2.0.7                                ...           2 years ago
  • 2.0.5                                ...           2 years ago
  • 2.0.0-alpha.1                                ...           3 years ago
  • 2.0.0-alpha                                ...           3 years ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 41
Dependencies (9)
Dev Dependencies (5)

Copyright 2014 - 2016 © taobao.org |