Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@knorm/knorm

Package Overview
Dependencies
Maintainers
1
Versions
32
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@knorm/knorm - npm Package Compare versions

Comparing version 1.1.0 to 1.2.0

36

docs/guides/models.md

@@ -16,3 +16,4 @@ # Models

| `Model.virtuals` | object | none | Configures the model's virtual fields. See the [virtuals guide](guides/virtuals.md#virtuals) for more info |
| `Model.Query` | [Query](api/query.md#query) | [Query](api/query.md#query) | The `Query` class that the model uses to perform database operations. This allows [customizing queries](#customizing-queries) per model. |
| `Model.options` | object | none | Configures the model's default query and plugin options (for some plugins). See [customizing queries per model](#customizing-queries-per-model) for more info |
| `Model.Query` | [Query](api/query.md#query) | [Query](api/query.md#query) | The `Query` class that the model uses to perform database operations. This allows [customizing queries per model](#customizing-queries-per-model). |
| `Model.Field` | [Field](api/field.md#field) | [Field](api/field.md#field) | The `Field` class that the model uses to create field instances. Also allows customizing fields per model. |

@@ -180,6 +181,8 @@

## Customizing queries
## Customizing queries per model
You can set default query options per model via the `Model.options` setter.
For example, if your users table has some system users that should not be
fetched/updated/deleted, you can override `User.Query` and add default filters:
fetched/updated/deleted, you can add a default `where` option:

@@ -194,10 +197,29 @@ ```js

User.options: {
query: { where: { type: 'system' } }
};
User.fetch().then(console.log); // will not contain system users
```
> These options will also be inherited when the model is inherited. <br />
> Read more on [setting query options](guides/queries.md#setting-options)
For more fine-grained control, you can also override the `Query` class:
```js
class User extends Model {}
User.Query = class UserQuery extends User.Query {
constructor(...args) {
super(...args);
this.where({ type: 'system' });
// add an `onlySystemUsers` query option only for the `User` model
onlySystemUsers() {
return this.where({ type: 'system });
}
};
User.fetch().then(console.log); // will not contain system users
User.fetch({
onlySystemUsers: true
}).then(console.log); // will not contain system users
```
Note that this is not a good example

@@ -140,4 +140,31 @@ # Queries

!> Setting the same option twice overwrites the previously set value
For most query options, calling the same option does not overwrite the previous
value but instead appends to it. However, for boolean-value options, setting the
same option overwrites the previous value:
```js
User.query
.where({ id: 1 })
.fields(['id'])
.require(false)
.setOptions({
where: { names: 'foo' }, // `where` is now `{ id: 1, names: 'foo' }`
fields: ['name'], // `fields` are now `[ 'id', 'name' ]`
require: true // `require` is now `true`
})
.fetch({
require: false // `require` will eventually be `false`
});
```
You can set default query options per model via the `Model.options` setter:
```js
User.options = {
query: { fields: ['id'] }
};
User.fetch(); // instances returned will only contain the `id` field
```
## Where expressions

@@ -144,0 +171,0 @@

@@ -1,2 +0,2 @@

const { upperFirst } = require('lodash');
const { upperFirst, merge } = require('lodash');

@@ -403,2 +403,3 @@ class Model {

_virtuals: {},
_options: {},
fieldsToColumns: {},

@@ -448,2 +449,10 @@ unique: [],

}
},
options: {
get() {
return config._options;
},
set(options) {
config._options = merge(config._options, options);
}
}

@@ -455,3 +464,3 @@ });

static set config({ table, fields, virtuals }) {
static set config({ table, fields, virtuals, options }) {
if (!this._config) {

@@ -466,2 +475,3 @@ const value = this.createConfig();

this._config.virtuals = parentConfig._virtuals;
this._config.options = parentConfig._options;
}

@@ -485,2 +495,6 @@

}
if (options) {
this._config.options = options;
}
}

@@ -516,4 +530,16 @@

static set options(options) {
this.config = { options };
}
static get options() {
return this.config.options;
}
static get query() {
return new this.Query(this);
const query = new this.Query(this);
if (this._config && this._config._options && this._config._options.query) {
query.setOptions(this._config._options.query);
}
return query;
}

@@ -520,0 +546,0 @@

1

lib/Query.js

@@ -356,3 +356,2 @@ const { difference } = require('lodash');

case 'where':
case 'and':
return this.prepareWhere(sql, value);

@@ -359,0 +358,0 @@

{
"name": "@knorm/knorm",
"version": "1.1.0",
"version": "1.2.0",
"description": "A purely ES6 class-based ORM for Node.js",

@@ -5,0 +5,0 @@ "main": "index.js",

Sorry, the diff of this file is too big to display

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc