Security News
tea.xyz Spam Plagues npm and RubyGems Package Registries
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
typeodm.io
Advanced tools
Readme
Improve typescript experience using mongoose with decorators.
typescript
first class support.npm install typeodm.io --save
Dependencies
npm install metadata mongoose @types/mongoose --save
Remember enable decorators in tsconfig.json
"experimentalDecorators": true,
"emitDecoratorMetadata": true
defining simple 'User' document and export model
/* user.document.ts */
@document()
export class User {
@required() username: string;
@required() password: string;
@optional() rol: string;
}
export const UserModel = new getModel<User>(User);
using UserModel
/* Somewhere in your code, basic CRUD operations */
// Get List
UserModel.find();
// Get one
UserModel.findOne({ username: 'charlie' });
UserModel.findById(id);
// Save
const user = new UserModel({ username: 'charlie', password: 'asd!@#$' });
user.save();
// Update
UserModel.findByIdAndUpdate(id, { username: 'charlie 2' });
// Remove
UserModel.findByIdAndRemove(id);
You can find more useful functions for 'UserModel' here
@objectId
@required
@optional
@defaultValue
(alias for default
)@validate
@alias
@unique
@index
@spare
@lowercase
@uppercase
@trim
@match
@isIn
(alias for enum
)@length
(includes minlength
and maxlength
ex. @length(3, 50)
)@range
(includes min
and max
)@range
(includes min
and max
)@methods
@virtuals
@query
(Not have type support yet, but works fine)@statics
(Not have type support yet, but works fine)A comment only exists and belong to one post, therefore it must be saved as a subdocument, hasn't sense to create a collection to store comments. Example:
@document()
export class Comment {
// define 'message' property required and set rule minlength(2)
@required()
@length(2)
message: string;
// define 'author' property optional
@optional()
author: string;
// define 'status' propery to be 'pending' or 'approved'
@isIn(['pending', 'approved'])
status: string;
}
@document()
export class Post {
// define 'title' as a required property
@required()
title: string;
// define 'content' as property, and set minlength(5) and maxlength(2000) values
@length(5, 2000)
content: string;
// define 'rating' property, with range [1-5]
@range(1, 5)
rating: number;
// define 'comments' property to be a subdocument array of Comment
@arrayOf(Comment)
comments: Comment[];
}
In almost all applications the resource are store by user, in this case we'll see how to create documents 'User' and 'Todo' where each 'Todo' belong to a user.
@document()
export class User {
@required() username: string;
@required() password: string;
}
@document()
export class Todo {
@required()
title: string;
@defaultValue(false)
isComplete: boolean;
@ref(User)
user: Ref<User>;
}
export const TodoModel = getModel<Todo>(Todo);
Getting a Todo
list with the user data populated.
Using populate query
to property 'Todo.user' and projection username
to return only User._id
and User.username
const todos = await TodoModel.find().populate('user', 'username');
Now will see how to save a Todo
// getting user by creating a new one
const userToSave = new UserModel({ username: 'charlie', password: 'asd!@#$' });
const user = await userToSave.save();
// getting user by data base query
const user = await UserModel.find({ username: 'charlie' });
// getting user from request after jwt put it there
const user = req.user;
// after getting the `user._id` no matter what source you can save a `Todo` for this user in the following way:
const todoToSave = new TodoModel({ title: 'Go to market for some oranges' });
todoToSave.user = user._id;
const todo = await todoToSave.save();
// in some case you don't now if user._id if an instance of ObjectId, to be sure do:
const userId = new ObjectId(user._id);
You must annotate a class with @document
in order to use is as mongoose schema and to can extract a model from it.
@document(config: SchemaOptions)
you can pass a SchemaOptions object as parameter.
@query
and @statics
@plugin
decoratorFAQs
Improve typescript experience using mongoose with decorators.
The npm package typeodm.io receives a total of 3 weekly downloads. As such, typeodm.io popularity was classified as not popular.
We found that typeodm.io demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
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.
Security News
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
Security News
As cyber threats become more autonomous, AI-powered defenses are crucial for businesses to stay ahead of attackers who can exploit software vulnerabilities at scale.
Security News
UnitedHealth Group disclosed that the ransomware attack on Change Healthcare compromised protected health information for millions in the U.S., with estimated costs to the company expected to reach $1 billion.