Skip to content

Commit

Permalink
Updated code to typescript.
Browse files Browse the repository at this point in the history
  • Loading branch information
diosney committed Jun 27, 2024
1 parent 17b3eee commit 85f9aee
Show file tree
Hide file tree
Showing 19 changed files with 3,480 additions and 1,798 deletions.
4 changes: 3 additions & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ end_of_line = lf
indent_size = 2
indent_style = space
insert_final_newline = false
max_line_length = 80
max_line_length = 120
trim_trailing_whitespace = true

[*.md]
trim_trailing_whitespace = false
indent_size = 4
indent_style = space
35 changes: 35 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
name: tests

on:
push:
branches:
- master

pull_request:
branches:
- master

jobs:
build:
runs-on: ubuntu-latest

strategy:
matrix:
node-version:
- 10.x
- 12.x
- 14.x
- 16.x
- 18.x

steps:
- name: Checkout
uses: actions/checkout@v3

- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}

- run: npm clean-install
- run: npm run test
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,6 @@ node_modules

# OS specific ignores
.directory
.idea
.idea

/lib
40 changes: 0 additions & 40 deletions .jshintrc

This file was deleted.

10 changes: 10 additions & 0 deletions .mocharc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"timeout" : 15000,
"recursive": true,
"require" : [
"ts-node/register",
"source-map-support/register"
],
"reporter" : "progress",
"sort" : true
}
28 changes: 17 additions & 11 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,22 +1,28 @@
## Release notes
### Release Notes

### 1.0.0
#### 2.0.0

* As module was very stable, bumped version to `1.0.0` so is marked as
- (@DanielOverdevestPZH) Make plugin compatible with latest Sequelize v6.
- (@honzahommer) Added `readOnly` shortcut as alternative for `noUpdate.readOnly`.
- Updated code to typescript.

#### 1.0.0

- As module was very stable, bumped version to `1.0.0` so is marked as
**stable** for registries.

### 0.2.0
#### 0.2.0

* Added `readOnly` option.
- Added `readOnly` option.

### 0.1.2
#### 0.1.2

* Removed strong tag from package name.
- Removed strong tag from package name.

### 0.1.1
#### 0.1.1

* Added keywords to `package.json`.
- Added keywords to `package.json`.

### 0.1.0
#### 0.1.0

* Initial release.
- Initial release.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License (MIT)

Copyright (c) Diosney Sarmiento
Copyright (c) 2016-Present Diosney Sarmiento

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
88 changes: 61 additions & 27 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,42 +1,61 @@
# node-sequelize-noupdate-attributes

A very simple [Sequelize](https://github.com/sequelize/sequelize) plugin which adds **no update** and **readonly** attributes support.
A [Sequelize](https://sequelize.org) plugin which adds **no update** and **readonly** attributes support to models.

## Prerequisites
[![donate](https://shields.io/badge/ko--fi-donate-ff5f5f?logo=ko-fi&style=for-the-badgeKo-fi)](https://ko-fi.com/diosney)
[![tests](https://github.com/diosney/node-sequelize-noupdate-attributes/actions/workflows/tests.yml/badge.svg?branch=master)](https://github.com/diosney/node-sequelize-noupdate-attributes/actions/workflows/tests.yml)
[![npm](https://img.shields.io/npm/v/sequelize-noupdate-attributes)](https://www.npmjs.com/package/sequelize-noupdate-attributes)

Have previously installed the `sequelize` package in the project.
> **Note:** Starting from version `2.0.0`, the plugin supports Sequelize `v6`.
> <br>Ensure to have at least the `v6.16.0` since there are some typings and functions, like `.getAttributes()`, that
> weren't available from `v6.1.0`.
## Install
## Installation

Ensure the `sequelize` package has been previously installed in the project.

```sh
npm install --save sequelize-noupdate-attributes
```

## Usage

Add the plugin to the sequelize instance and then set the noupdate
properties in your models, as shown in the basic example below:
### How to Import

If you are using Javascript, you can import using the regular CommonJS `require` as follows:

const sequelizeNoUpdateAttributes = require('sequelize-noupdate-attributes');

If you are using Typescript, you can use the `require` calls, or just the Ecmascript modules import:

import sequelizeNoUpdateAttributes from 'sequelize-noupdate-attributes';

### How to Use

Add the plugin to the `sequelize` instance, then set the `noUpdate` or `readOnly` properties in your models
as demonstrated in the examples below:

```js
var sequelizeNoUpdateAttributes = require('sequelize-noupdate-attributes');
const sequelizeNoUpdateAttributes = require('sequelize-noupdate-attributes');

var sequelize = new Sequelize('database', 'user', 'password');
sequelizeNoUpdateAttributes(sequelize); // Note that is the `sequelize` instance the passed reference.
const sequelize = new Sequelize('sqlite::memory:');
// Note that the passed reference is the `sequelize` instance.
sequelizeNoUpdateAttributes(sequelize);

var Post = sequelize.define('Post', {
const Post = sequelize.define('Post', {
content: {
type : Sequelize.STRING,
type : DataTypes.STRING,
noUpdate: true
}
});
```

What this do is to mark the `content` attribute so if an update is done, then:
What this does is to mark the `content` attribute so if an update is done, then:

* if the field previous value is null, it will accept the change
* if the field previous value is not null, it will trigger a `SequelizeValidationErrorItem` error.
* if the field's previous value is `null`, it will accept the change
* if the field's previous value is not `null`, it will trigger a `ValidationErrorItem` error.

so, the `content` field becomes a **readonly** after its becomes not null.
so, the `content` field becomes **readonly** after its becomes not `null`.

Works too with foreign key fields in associations:

Expand All @@ -45,27 +64,28 @@ models.Post.belongsTo(models.Person, {
as : 'Creator',
foreignKey: {
allowNull: false,
noUpdate : true // Will mark the `CreatorId` field to be `noUpdate`d.
noUpdate : true // It will mark the `CreatorId` field to be `noUpdate`.
}
});
```

### Readonly attributes
### Readonly Attributes

If you do want truly **readonly** attributes with no modifications at all
If you want truly **readonly** attributes with no modifications at all
being allowed, you can use the `readonly` option as shown below:

```js
var Post = sequelize.define('Post', {
const Post = sequelize.define('Post', {
content: {
type : Sequelize.STRING,
type : DataTypes.STRING,
readOnly: true
}
});

var Post = sequelize.define('Post', {
// Or
const Post = sequelize.define('Post', {
content: {
type : Sequelize.STRING,
type : DataTypes.STRING,
noUpdate: {
readOnly: true
}
Expand All @@ -74,23 +94,37 @@ var Post = sequelize.define('Post', {
```

which effectively disables any modification on the `content` attribute,
no matter if the previous value was null or not, though exception is
no matter if the previous value was `null` or not, though the exception is
when the record is new.

If by any chance both `readonly` and `noUpdate` are present, `readOnly` takes precedence if is truthy.

## Notes

- The plugin does its work by registering a custom `beforeValidate` hook named `noUpdateAttributes`.

- If the plugin triggers a validation error, it will be a Sequelize `ValidationError` instance containing an `errors`
property of type `ValidationErrorItem[]`.

Each `ValidationErrorItem` instance will have an attribute called `validatorKey`, which, in the case of a plugin
validation error, will be` 'noUpdateAttribute'` (`NoUpdateAttributesValidatorKeys.NoUpdateAttribute`).

## Issues

The source is available for download from [GitHub](https://github.com/diosney/node-sequelize-noupdate-attributes)
and there is a [issue tracker](https://github.com/diosney/node-sequelize-noupdate-attributes/issues) so you can report bugs there.
The source code can be accessed on [GitHub](https://github.com/diosney/node-sequelize-noupdate-attributes).
<br>
If you encounter any bugs or need a new feature, please report them on the
[issue tracker](https://github.com/diosney/node-sequelize-noupdate-attributes/issues).

## Tests

To run the tests just execute `npm test`.
Just run `npm test`.

## License

The MIT License (MIT)

Copyright (c) Diosney Sarmiento
Copyright (c) 2016-Present Diosney Sarmiento

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
Loading

0 comments on commit 85f9aee

Please sign in to comment.