🚀 Socket Launch Week Day 5:Introducing Repository Access Permissions and Custom Roles.Learn more
Sign In

js-data-decorators

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

js-data-decorators

Addon to js-data that facilitates creation of schemas through the use of @Decorators

latest
Source
npmnpm
Version
0.1.0
Version published
Weekly downloads
4
-66.67%
Maintainers
1
Weekly downloads
 
Created
Source

js-data-decorators

This package provides a set of decorators that facilitate the creation of js-data mappers and json schema.

Example

// models/user.js
import { Resource, Id, Property, Required, HasMany } from 'js-data-decorators';

@Resource()
class User {
	@Id()
	@Property({ type: 'integer' })
	id;

	@Property({ type: 'string' })
	@Required()
	name;

	@HasMany({ foreignKey: 'authorId' })
	articles;
}

export default User;
// models/article.js
import { Resource, Id, Property, BelongsTo } from 'js-data-decorators';

@Resource({ description: 'A article written to a blog.' })
class Article {
	@Id()
	@Property({ type: 'integer' })
	id;

	@Property({ type: ['string', 'null'] })
	@Required()
	authorId;

	@BelongsTo({ resource: 'user' })
	author;

	@Property({ type: 'boolean' })
	@Required()
	isPublished;

	@Property({ type: 'number', minimum: 1, maximum: 5 })
	starRating;

	@Property({ type: 'array', items: { type: 'string' } })
	tags;
}

export default Article;
import { DataStore } from 'js-data';
import { HttpAdapter } from 'js-data-http';
import User from 'models/user';
import Article from 'models/article';
import { defineMapper } from 'js-data-decortors';

const adapter = new HttpAdapter();
const store = new DataStore();

store.registerAdapter('http', adapter, { 'default': true });

defineMapper(store, User, { endpoint: '/users' });
defineMapper(store, Article, { endpoint: '/articles' });

The calls to defineMapper in the last two lines of the example above are equivalent to this:

store.defineMapper('user', {
	name: 'user',
	schema: {
		type: 'object',
		properties: {
			id: { type: 'integer' },
			name: { type: 'string' },
		},
	},
	relations: {
		hasMany: {
			article: { foreignKey: 'authorId', localField: 'articles' },
		},
	},
	idAttribute: 'id',
	required: ['name'],
	recordClass: User,
	endpoint: '/users',
});

store.defineMapper('article', {
	name: 'article',
	schema: {
		description: 'A article written to a blog.',
		type: 'object',
		properties: {
			id: { type: 'integer' },
			authorId: { type: ['string', 'null'] },
			isPublished: { type: 'boolean' },
			starRating: { type: 'number', minimum: 0, maximum: 5 },
			tags: { type: 'array', items: { type: 'string' } },
		},
	},
	relations: {
		belongsTo: {
			user: { foreignKey: 'authorId', localField: 'author' },
		},
	},
	idAttribute: 'id',
	recordClass: Article,
	endpoint: '/articles',
});

Keywords

js-data

FAQs

Package last updated on 06 Apr 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts