Model
Javascript object model.
Works on modern browsers.
Check if tests pass on your browser.
Install
NPM
npm install @jaysalvat/smart-model
import SmartModel from '@jaysalvat/smart-model'
Module from CDN
import SmartModel from 'https://unpkg.com/@jaysalvat/smart-model@latest/build/smart-model.esm.min.js'
The old way
<script src="https://unpkg.com/@jaysalvat/smart-model@latest/build/smart-model.min.js"></script>
Usage
Better documentation soon...
function readingtime(text) { }
const Post = SmartModel.create('Post', {
title: {
required: true,
type: String,
},
body: {
required: true,
type: String,
rule: {
'tooShort': (calue) => value.length < 100,
'tooLong': (calue) => value.length > 1000,
}
},
createdAt: {
type: Date,
default: new Date(),
transform: (value) => new Date(value),
format: (value) => value.toLocaleString()
},
updatedAt: {
type: Date,
default: new Date(),
transform: (value) => new Date(value),
format: (value) => value.toLocaleString()
},
author: {
required: true,
type: {
firstname: {
required: true,
type: String
},
lastname: {
type: String
}
}
},
bodyLength: (post) => post.body.length,
readingTime: (post) => readingTime(post.body)
},
{
strict: false,
exceptions: true
}
{
onUpdate() {
this.updatedAt = new Date()
}
}
)
const post = new Post({
title: 'my new post',
body: 'lorem ipsum...'
})
Dev
Dev mode
npm run dev
Build
npm run build
Lint
npm run lint
Fix lint errors
npm run lint:fix
Tests
npm run test
npm run test:watch
npm run test:browser
Bump version and publish to NPM
npm run release
npm run release:patch
npm run release:minor
npm run release:major