https://github.com/techquery/datascheme
Data Model framework based on ECMAScript Decorator proposal
https://github.com/techquery/datascheme
Last synced: 11 months ago
JSON representation
Data Model framework based on ECMAScript Decorator proposal
- Host: GitHub
- URL: https://github.com/techquery/datascheme
- Owner: TechQuery
- License: lgpl-3.0
- Created: 2018-09-28T06:46:47.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2019-03-13T15:45:26.000Z (about 7 years ago)
- Last Synced: 2024-04-24T16:09:58.106Z (almost 2 years ago)
- Language: JavaScript
- Homepage: https://tech-query.me/DataScheme/
- Size: 522 KB
- Stars: 9
- Watchers: 4
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: ReadMe.md
- License: LICENSE
Awesome Lists containing this project
README
# Data Scheme
**Data Model** framework based on [ECMAScript Decorator proposal](https://github.com/tc39/proposal-decorators)
[](https://david-dm.org/TechQuery/DataScheme)
[](https://travis-ci.com/TechQuery/DataScheme)
[](https://www.jsdelivr.com/package/npm/data-scheme)
[](https://nodei.co/npm/data-scheme/)
## Basic Example
[`User.js`](https://tech-query.me/DataScheme/test-file/test/source/User.js.html)
```JavaScript
import Model, { mapGetter, is, Range, Email, Phone, URL } from 'data-scheme';
@mapGetter
export default class User extends Model {
@is(/^[\w-]{3,20}$/, '')
set name(value) { this.set('name', value); }
@is(Email, '')
set email(value) { this.set('email', value); }
@is( Phone )
set phone(value) { this.set('phone', value); }
@is([0, 1, 2], 2)
set gender(value) { this.set('gender', value); }
@is(Range( 1900 ))
set birthYear(value) { this.set('birthYear', value); }
@is(URL, 'http://example.com/test.jpg')
set avatar(value) { this.set('avatar', value); }
@is( URL )
set URL(value) { this.set('URL', value); }
@is( String )
set description(value) { this.set('description', value); }
}
```
`index.js`
```JavaScript
import User from './User';
const user = new User({
name: 'test',
email: 'test@example.com'
});
user.phone = '+86-028-88888888';
console.log( user.valueOf() );
```
**Console output**
```JSON
{
"name": "test",
"email": "test@example.com",
"gender": 2,
"avatar": "http://example.com/test.jpg",
"phone": "+86-028-88888888"
}
```
## Installation
```Shell
npm install data-scheme @babel/polyfill
npm install -D \
@babel/cli \
@babel/core \
@babel/preset-env \
@babel/plugin-proposal-decorators
```
`.babelrc`
```JSON
{
"presets": [
"@babel/preset-env"
],
"plugins": [
[
"@babel/plugin-proposal-decorators",
{
"decoratorsBeforeExport": true
}
]
]
}
```
## Advanced usage
- [Scheme Helper](https://tech-query.me/DataScheme/file/source/scheme.js.html)
- [Inherited & Nested Model](https://tech-query.me/DataScheme/test-file/test/source/User.js.html#lineNumber33)
### Observe Values changing
```JavaScript
import User from './User';
const user = new User({
name: 'test',
email: 'test@example.com'
});
user.observe({
name(value, oldValue) {
console.log(value, oldValue); // 'example' 'test'
}
});
user.name = 'example';
```
### Decorator hook
`hook.js`
```JavaScript
import { listen } from 'data-scheme';
listen('HTTP', ({Class, method, path, contentType}) =>
console.log(Class, method, path, contentType)
);
```
`User.js`
```JavaScript
import Model, { mapGetter, HTTP } from 'data-scheme';
@HTTP('POST', '/user')
@HTTP('GET', '/user')
@mapGetter
export default class User extends Model {
// Field defination
}
```
`index.js`
```JavaScript
import './hook';
import User from './User';
```
## Learn more
1. [Meta programming](https://github.com/tc39/proposal-decorators/blob/master/METAPROGRAMMING.md)
2. [Decorator API (Stage-2)](https://github.com/tc39/proposal-decorators/blob/master/TAXONOMY.md)