
Security News
Meet Socket at Black Hat and DEF CON 2025 in Las Vegas
Meet Socket at Black Hat & DEF CON 2025 for 1:1s, insider security talks at Allegiant Stadium, and a private dinner with top minds in software supply chain security.
immutable-models
Advanced tools
Create immutable models driven by Immutable.js iterables.
Immutable Models requires Immutable.js 3.8.0 or later.
npm install --save immutable-models
This assumes that you’re using npm package manager with a module bundler like Webpack or Browserify to consume CommonJS modules.
Immutable.js is a great library for dealing with immutable data. However, it doesn't provide the easy way for hermetization.
Because of that, we can't create simple and defined model interfaces - everything is public and (excluding Records
) there
is no way to put additional logic in these models.
Immutable Models wraps immutable structures and provides interfaces defined by a developer.
Let's see some example:
import { Model } from 'immutable-models';
import { Permission } from './Permission';
import { Set } from 'immutable';
interface UserShape {
userName: string
createdBy?: User;
permissions?: Set<Permission>;
}
export class User extends Model<UserShape> {
getUserName(): string {
return this.get('userName');
}
getCreatedBy(): User {
return this.get('createdBy');
}
isAdmin(): boolean {
return this.hasPermission(Permission.ADMIN);
}
isCreatedByAdmin(): boolean {
return this.getCreatedBy() ? this.getCreatedBy().isAdmin() : false;
}
getPermissions(): Set<Permission> {
return this.get('permissions', Set<Permission>());
}
hasPermission(permission: Permission): boolean {
return this.getPermissions().contains(permission);
}
addPermission(permission: Permission): this {
return this.update('permissions', permissions => permissions.add(permission));
}
}
// example usage
const user = new User({
userName: 'piotr',
permissions: Set([Permission.DEVELOPER])
});
user.getUserName(); // > piotr
user.isAdmin(); // false
// create admin user based on user - immutable data
const adminUser = user.addPermission(Permission.ADMIN); // make me an admin!
adminUser.getUserName(); // > piotr
adminUser.isAdmin(); // > true
user.isAdmin(); // > false
// create another user
const anotherUser = new User({
userName: 'lukasz',
createdBy: adminUser
});
anotherUser.isAdmin(); // > false
anotherUser.isCreatedByAdmin(); // > true
Like you see, User
class hides complexity of Immutable.js structures and contains business logic.
It's not completed but we are working on it:
If you are using TypeScript, you don't have to install typings - they are provided in npm package.
MIT
v0.1.2
update
and has
methods to Model
has
method to ReadonlyModel
notSetValue
argument to get
method in ReadonlyModel
FAQs
Create immutable models driven by Immutable.js iterables.
The npm package immutable-models receives a total of 3 weekly downloads. As such, immutable-models popularity was classified as not popular.
We found that immutable-models 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
Meet Socket at Black Hat & DEF CON 2025 for 1:1s, insider security talks at Allegiant Stadium, and a private dinner with top minds in software supply chain security.
Security News
CAI is a new open source AI framework that automates penetration testing tasks like scanning and exploitation up to 3,600× faster than humans.
Security News
Deno 2.4 brings back bundling, improves dependency updates and telemetry, and makes the runtime more practical for real-world JavaScript projects.