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

cormo

Package Overview
Dependencies
Maintainers
8
Versions
173
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

cormo - npm Package Compare versions

Comparing version 0.16.13 to 0.17.0

8

lib/adapters/base.d.ts

@@ -39,2 +39,10 @@ import { ColumnPropertyInternal } from '../model';

group_by?: string[];
joins: Array<{
model_name: string;
type: string;
alias: string;
base_column: string;
join_column: string;
}>;
distinct?: boolean;
limit?: number;

@@ -41,0 +49,0 @@ skip?: number;

4

lib/adapters/base.js

@@ -46,2 +46,6 @@ "use strict";

/** @internal */
this.support_join = false;
/** @internal */
this.support_distinct = false;
/** @internal */
this.native_integrity = false;

@@ -48,0 +52,0 @@ }

22

lib/adapters/mongodb.js

@@ -235,3 +235,3 @@ "use strict";

const before_count = lodash_1.default.reduce(subs, (memo, sub) => {
return memo + Object.keys(sub).length;
return memo + Object.keys(sub || {}).length;
}, 0);

@@ -472,5 +472,5 @@ const obj = lodash_1.default.extend({}, ...subs);

/** @internal */
async updatePartial(model, data, conditions, options) {
async updatePartial(model, data, conditions_arg, options) {
const schema = this._connection.models[model]._schema;
conditions = _buildWhere(schema, conditions);
let conditions = _buildWhere(schema, conditions_arg);
if (!conditions) {

@@ -503,5 +503,5 @@ conditions = {};

/** @internal */
async upsert(model, data, conditions, options) {
async upsert(model, data, conditions_arg, options) {
const schema = this._connection.models[model]._schema;
conditions = _buildWhere(schema, conditions);
let conditions = _buildWhere(schema, conditions_arg);
if (!conditions) {

@@ -675,5 +675,5 @@ conditions = {};

/** @internal */
async count(model_name, conditions, options) {
async count(model_name, conditions_arg, options) {
const model_class = this._connection.models[model_name];
conditions = _buildWhere(model_class._schema, conditions);
const conditions = _buildWhere(model_class._schema, conditions_arg);
// console.log(JSON.stringify(conditions))

@@ -714,5 +714,5 @@ if (options.group_by || options.group_fields) {

/** @internal */
async delete(model, conditions, options) {
async delete(model, conditions_arg, options) {
const model_class = this._connection.models[model];
conditions = _buildWhere(model_class._schema, conditions);
const conditions = _buildWhere(model_class._schema, conditions_arg);
try {

@@ -871,6 +871,6 @@ // console.log(JSON.stringify(conditions))

/** @internal */
_buildConditionsForFind(model, conditions, options) {
_buildConditionsForFind(model, conditions_arg, options) {
const fields = this._buildSelect(options.select);
let orders;
conditions = _buildWhere(this._connection.models[model]._schema, conditions);
let conditions = _buildWhere(this._connection.models[model]._schema, conditions_arg);
if (options.near != null && Object.keys(options.near)[0]) {

@@ -877,0 +877,0 @@ const field = Object.keys(options.near)[0];

@@ -131,2 +131,6 @@ "use strict";

/** @internal */
this.support_join = true;
/** @internal */
this.support_distinct = true;
/** @internal */
this.native_integrity = true;

@@ -431,3 +435,3 @@ /** @internal */

if (conditions.length > 0) {
sql += ' WHERE ' + this._buildWhere(this._connection.models[model]._schema, conditions, values);
sql += ' WHERE ' + this._buildWhere(this._connection.models[model]._schema, '', {}, conditions, values);
}

@@ -496,3 +500,3 @@ let result;

const table_name = this._connection.models[model].table_name;
const sql = `SELECT ${select} FROM \`${table_name}\` WHERE id=? LIMIT 1`;
const sql = `SELECT ${select} FROM \`${table_name}\` AS _Base WHERE id=? LIMIT 1`;
if (options.explain) {

@@ -578,3 +582,3 @@ return await this.query(`EXPLAIN ${sql}`, id, { transaction: options.transaction, node: options.node });

if (conditions.length > 0) {
sql += ' WHERE ' + this._buildWhere(this._connection.models[model]._schema, conditions, params);
sql += ' WHERE ' + this._buildWhere(this._connection.models[model]._schema, '', {}, conditions, params);
}

@@ -585,3 +589,3 @@ if (options.group_by) {

if (options.conditions_of_group.length > 0) {
sql += ' HAVING ' + this._buildWhere(options.group_fields, options.conditions_of_group, params);
sql += ' HAVING ' + this._buildWhere(options.group_fields, '', {}, options.conditions_of_group, params);
}

@@ -608,3 +612,3 @@ sql = `SELECT COUNT(*) AS count FROM (${sql}) _sub`;

if (conditions.length > 0) {
sql += ' WHERE ' + this._buildWhere(this._connection.models[model]._schema, conditions, params);
sql += ' WHERE ' + this._buildWhere(this._connection.models[model]._schema, '', {}, conditions, params);
}

@@ -1006,8 +1010,17 @@ let result;

const table_name = model_class.table_name;
let sql = `SELECT ${select} FROM \`${table_name}\``;
const join_schemas = {};
let sql = `SELECT ${options.distinct ? 'DISTINCT' : ''} ${select} FROM \`${table_name}\` AS _Base`;
if (options.index_hint) {
sql += ` ${options.index_hint}`;
}
if (options.joins.length > 0) {
const escape_ch = this._escape_ch;
for (const join of options.joins) {
sql += ` ${join.type} ${this._connection.models[join.model_name].table_name} AS _${join.alias}`;
sql += ` ON _Base.${escape_ch}${join.base_column}${escape_ch} = _${join.alias}.${escape_ch}${join.join_column}${escape_ch}`;
join_schemas[join.alias] = this._connection.models[join.model_name]._schema;
}
}
if (conditions.length > 0) {
sql += ' WHERE ' + this._buildWhere(model_class._schema, conditions, params);
sql += ' WHERE ' + this._buildWhere(model_class._schema, '_Base', join_schemas, conditions, params);
}

@@ -1019,3 +1032,3 @@ if (options.group_by) {

if (options.conditions_of_group.length > 0) {
sql += ' HAVING ' + this._buildWhere(options.group_fields, options.conditions_of_group, params);
sql += ' HAVING ' + this._buildWhere(options.group_fields, '_Base', {}, options.conditions_of_group, params);
}

@@ -1022,0 +1035,0 @@ if ((options && options.orders.length > 0) || order_by) {

@@ -116,2 +116,6 @@ "use strict";

/** @internal */
this.support_join = true;
/** @internal */
this.support_distinct = true;
/** @internal */
this.native_integrity = true;

@@ -317,3 +321,3 @@ /** @internal */

if (conditions.length > 0) {
sql += ' WHERE ' + this._buildWhere(this._connection.models[model]._schema, conditions, values);
sql += ' WHERE ' + this._buildWhere(this._connection.models[model]._schema, '', {}, conditions, values);
}

@@ -333,3 +337,3 @@ let result;

const table_name = this._connection.models[model].table_name;
const sql = `SELECT ${select} FROM "${table_name}" WHERE id=$1 LIMIT 1`;
const sql = `SELECT ${select} FROM "${table_name}" AS _Base WHERE id=$1 LIMIT 1`;
if (options.explain) {

@@ -421,3 +425,3 @@ return await this.query(`EXPLAIN ${sql}`, [id], options.transaction);

if (conditions.length > 0) {
sql += ' WHERE ' + this._buildWhere(this._connection.models[model]._schema, conditions, params);
sql += ' WHERE ' + this._buildWhere(this._connection.models[model]._schema, '', {}, conditions, params);
}

@@ -428,3 +432,3 @@ if (options.group_by) {

if (options.conditions_of_group.length > 0) {
sql += ' HAVING ' + this._buildWhere(options.group_fields, options.conditions_of_group, params);
sql += ' HAVING ' + this._buildWhere(options.group_fields, '', {}, options.conditions_of_group, params);
}

@@ -452,3 +456,3 @@ sql = `SELECT COUNT(*) AS count FROM (${sql}) _sub`;

if (conditions.length > 0) {
sql += ' WHERE ' + this._buildWhere(this._connection.models[model]._schema, conditions, params);
sql += ' WHERE ' + this._buildWhere(this._connection.models[model]._schema, '', {}, conditions, params);
}

@@ -568,3 +572,3 @@ let result;

else {
return column;
return '_Base.' + column;
}

@@ -747,5 +751,14 @@ });

const table_name = model_class.table_name;
let sql = `SELECT ${select} FROM "${table_name}"`;
const join_schemas = {};
let sql = `SELECT ${options.distinct ? 'DISTINCT' : ''} ${select} FROM "${table_name}" as _Base`;
if (options.joins.length > 0) {
const escape_ch = this._escape_ch;
for (const join of options.joins) {
sql += ` ${join.type} ${this._connection.models[join.model_name].table_name} AS _${join.alias}`;
sql += ` ON _Base.${escape_ch}${join.base_column}${escape_ch} = _${join.alias}.${escape_ch}${join.join_column}${escape_ch}`;
join_schemas[join.alias] = this._connection.models[join.model_name]._schema;
}
}
if (conditions.length > 0) {
sql += ' WHERE ' + this._buildWhere(model_class._schema, conditions, params);
sql += ' WHERE ' + this._buildWhere(model_class._schema, '_Base', join_schemas, conditions, params);
}

@@ -757,3 +770,3 @@ if (options.group_by) {

if (options.conditions_of_group.length > 0) {
sql += ' HAVING ' + this._buildWhere(options.group_fields, options.conditions_of_group, params);
sql += ' HAVING ' + this._buildWhere(options.group_fields, '_Base', {}, options.conditions_of_group, params);
}

@@ -760,0 +773,0 @@ if ((options && options.orders.length > 0) || order_by) {

@@ -119,3 +119,3 @@ "use strict";

/** @internal */
_buildWhereSingle(schema, property, key, value, params) {
_buildWhereSingle(schema, property, key, key_prefix, value, params) {
let property_type_class;

@@ -137,3 +137,4 @@ if (key === 'id') {

else {
column = this._escape_ch + (property ? property._dbname_us : key.replace(/\./g, '_')) + this._escape_ch;
column =
key_prefix + this._escape_ch + (property ? property._dbname_us : key.replace(/\./g, '_')) + this._escape_ch;
}

@@ -160,3 +161,3 @@ let op = '=';

else {
const sub_expr = this._buildWhereSingle(schema, property, key, value[sub_key], params);
const sub_expr = this._buildWhereSingle(schema, property, key, key_prefix, value[sub_key], params);
return `(NOT (${sub_expr}) OR ${column} IS NULL)`;

@@ -263,3 +264,14 @@ }

/** @internal */
_buildWhere(schema, conditions, params, conjunction = 'AND') {
_buildWhereSingleJoin(schema, base_alias, join_schemas, key, value, params) {
const model = key.split('.', 1)[0];
if (join_schemas[model]) {
// if key is 'JoinModel.column'
const model_class = join_schemas[model];
const property = model_class[key.substring(model.length + 1)];
return this._buildWhereSingle(model_class, property, key, `_${model}.`, value, params);
}
return this._buildWhereSingle(schema, schema[key], key, base_alias ? base_alias + '.' : '', value, params);
}
/** @internal */
_buildWhere(schema, base_alias, join_schemas, conditions, params, conjunction = 'AND') {
let subs = [];

@@ -269,3 +281,3 @@ let keys;

subs = conditions.map((condition) => {
return this._buildWhere(schema, condition, params);
return this._buildWhere(schema, base_alias, join_schemas, condition, params);
});

@@ -280,12 +292,12 @@ }

const key = keys[0];
if (key.substr(0, 1) === '$') {
if (key.substring(0, 1) === '$') {
switch (key) {
case '$and':
return this._buildWhere(schema, conditions[key], params, 'AND');
return this._buildWhere(schema, base_alias, join_schemas, conditions[key], params, 'AND');
case '$or':
return this._buildWhere(schema, conditions[key], params, 'OR');
return this._buildWhere(schema, base_alias, join_schemas, conditions[key], params, 'OR');
}
}
else {
return this._buildWhereSingle(schema, schema[key], key, conditions[key], params);
return this._buildWhereSingleJoin(schema, base_alias, join_schemas, key, conditions[key], params);
}

@@ -295,3 +307,3 @@ }

subs = keys.map((key) => {
return this._buildWhereSingle(schema, schema[key], key, conditions[key], params);
return this._buildWhereSingleJoin(schema, base_alias, join_schemas, key, conditions[key], params);
});

@@ -398,7 +410,7 @@ }

const escape_ch = this._escape_ch;
select = select.map((column) => `${escape_ch}${schema[column]._dbname_us}${escape_ch}`);
select = select.map((column) => `_Base.${escape_ch}${schema[column]._dbname_us}${escape_ch}`);
return select.join(',');
}
else {
return '*';
return `_Base.*`;
}

@@ -405,0 +417,0 @@ }

@@ -92,2 +92,6 @@ "use strict";

/** @internal */
this.support_join = true;
/** @internal */
this.support_distinct = true;
/** @internal */
this.key_type = types.Integer;

@@ -309,3 +313,3 @@ /** @internal */

if (conditions.length > 0) {
sql += ' WHERE ' + this._buildWhere(this._connection.models[model]._schema, conditions, values);
sql += ' WHERE ' + this._buildWhere(this._connection.models[model]._schema, '', {}, conditions, values);
}

@@ -332,3 +336,3 @@ try {

const table_name = this._connection.models[model].table_name;
const sql = `SELECT ${select} FROM "${table_name}" WHERE id=? LIMIT 1`;
const sql = `SELECT ${select} FROM "${table_name}" AS _Base WHERE id=? LIMIT 1`;
if (options.explain) {

@@ -411,3 +415,3 @@ return await this._client.allAsync(`EXPLAIN QUERY PLAN ${sql}`, id);

if (conditions.length > 0) {
sql += ' WHERE ' + this._buildWhere(this._connection.models[model]._schema, conditions, params);
sql += ' WHERE ' + this._buildWhere(this._connection.models[model]._schema, '', {}, conditions, params);
}

@@ -418,3 +422,3 @@ if (options.group_by) {

if (options.conditions_of_group.length > 0) {
sql += ' HAVING ' + this._buildWhere(options.group_fields, options.conditions_of_group, params);
sql += ' HAVING ' + this._buildWhere(options.group_fields, '', {}, options.conditions_of_group, params);
}

@@ -441,3 +445,3 @@ sql = `SELECT COUNT(*) AS count FROM (${sql})`;

if (conditions.length > 0) {
sql += ' WHERE ' + this._buildWhere(this._connection.models[model]._schema, conditions, params);
sql += ' WHERE ' + this._buildWhere(this._connection.models[model]._schema, '', {}, conditions, params);
}

@@ -680,7 +684,16 @@ try {

}
const params = [];
const table_name = model_class.table_name;
const params = [];
let sql = `SELECT ${select} FROM "${table_name}"`;
const join_schemas = {};
let sql = `SELECT ${options.distinct ? 'DISTINCT' : ''} ${select} FROM "${table_name}" AS _Base`;
if (options.joins.length > 0) {
const escape_ch = this._escape_ch;
for (const join of options.joins) {
sql += ` ${join.type} ${this._connection.models[join.model_name].table_name} AS _${join.alias}`;
sql += ` ON _Base.${escape_ch}${join.base_column}${escape_ch} = _${join.alias}.${escape_ch}${join.join_column}${escape_ch}`;
join_schemas[join.alias] = this._connection.models[join.model_name]._schema;
}
}
if (conditions.length > 0) {
sql += ' WHERE ' + this._buildWhere(model_class._schema, conditions, params);
sql += ' WHERE ' + this._buildWhere(model_class._schema, '_Base', join_schemas, conditions, params);
}

@@ -692,3 +705,3 @@ if (options.group_by) {

if (options.conditions_of_group.length > 0) {
sql += ' HAVING ' + this._buildWhere(options.group_fields, options.conditions_of_group, params);
sql += ' HAVING ' + this._buildWhere(options.group_fields, '_Base', {}, options.conditions_of_group, params);
}

@@ -695,0 +708,0 @@ if (options && options.orders.length > 0) {

@@ -74,3 +74,8 @@ /// <reference types="node" />

}>;
static _integrities: any[];
static _integrities: Array<{
type: string;
column: string;
child?: typeof BaseModel;
parent?: typeof BaseModel;
}>;
static _associations: {

@@ -77,0 +82,0 @@ [column: string]: any;

@@ -15,2 +15,10 @@ /// <reference types="node" />

group_by?: string[];
joins: Array<{
model_class: typeof BaseModel;
type: string;
alias?: string;
base_column?: string;
join_column?: string;
}>;
distinct?: boolean;
limit?: number;

@@ -47,2 +55,13 @@ skip?: number;

group<U>(group_by: string | null, fields?: object): QuerySingle<M, U>;
join(model: typeof BaseModel, options?: {
alias?: string;
base_column?: string;
join_column?: string;
}): QuerySingle<M, T>;
left_outer_join(model: typeof BaseModel, options?: {
alias?: string;
base_column?: string;
join_column?: string;
}): QuerySingle<M, T>;
distinct(): QuerySingle<M, T>;
one(): QuerySingleNull<M, T>;

@@ -89,2 +108,13 @@ limit(limit?: number): QuerySingle<M, T>;

group<U>(group_by: string | null, fields?: object): QuerySingleNull<M, U>;
join(model: typeof BaseModel, options?: {
alias?: string;
base_column?: string;
join_column?: string;
}): QuerySingleNull<M, T>;
left_outer_join(model: typeof BaseModel, options?: {
alias?: string;
base_column?: string;
join_column?: string;
}): QuerySingleNull<M, T>;
distinct(): QuerySingleNull<M, T>;
one(): QuerySingleNull<M, T>;

@@ -131,2 +161,13 @@ limit(limit?: number): QuerySingleNull<M, T>;

group<U>(group_by: string | null, fields?: object): QueryArray<M, U>;
join(model: typeof BaseModel, options?: {
alias?: string;
base_column?: string;
join_column?: string;
}): QueryArray<M, T>;
left_outer_join(model: typeof BaseModel, options?: {
alias?: string;
base_column?: string;
join_column?: string;
}): QueryArray<M, T>;
distinct(): QueryArray<M, T>;
one(): QuerySingleNull<M, T>;

@@ -211,2 +252,22 @@ limit(limit?: number): QueryArray<M, T>;

/**
* (inner) join
*/
join(model_class: typeof BaseModel, options?: {
alias?: string;
base_column?: string;
join_column?: string;
}): this;
/**
* left outer join
*/
left_outer_join(model_class: typeof BaseModel, options?: {
alias?: string;
base_column?: string;
join_column?: string;
}): this;
/**
* Returns distinct records
*/
distinct(): this;
/**
* Returns only one record (or null if does not exists).

@@ -213,0 +274,0 @@ *

@@ -32,2 +32,3 @@ "use strict";

select_single: false,
joins: [],
};

@@ -149,2 +150,44 @@ }

/**
* (inner) join
*/
join(model_class, options) {
if (!this._adapter.support_join) {
throw new Error('this adapter does not support join');
}
this._options.joins.push({
model_class,
type: 'INNER JOIN',
alias: options === null || options === void 0 ? void 0 : options.alias,
base_column: options === null || options === void 0 ? void 0 : options.base_column,
join_column: options === null || options === void 0 ? void 0 : options.join_column,
});
return this;
}
/**
* left outer join
*/
left_outer_join(model_class, options) {
if (!this._adapter.support_join) {
throw new Error('this adapter does not support join');
}
this._options.joins.push({
model_class,
type: 'LEFT OUTER JOIN',
alias: options === null || options === void 0 ? void 0 : options.alias,
base_column: options === null || options === void 0 ? void 0 : options.base_column,
join_column: options === null || options === void 0 ? void 0 : options.join_column,
});
return this;
}
/**
* Returns distinct records
*/
distinct() {
if (!this._adapter.support_join) {
throw new Error('this adapter does not support distinct');
}
this._options.distinct = true;
return this;
}
/**
* Returns only one record (or null if does not exists).

@@ -500,3 +543,48 @@ *

}
return Object.assign({ conditions_of_group: this._options.conditions_of_group, explain: this._options.explain, group_by, group_fields: this._options.group_fields, lean: this._options.lean, limit: this._options.limit, near: this._options.near, node: this._options.node, index_hint: this._options.index_hint, orders, skip: this._options.skip, transaction: this._options.transaction }, (select_raw.length > 0 && { select, select_raw }));
const joins = [];
for (const join of this._options.joins) {
if (join.base_column) {
joins.push({
model_name: join.model_class._name,
type: join.type,
alias: join.alias || join.model_class._name,
base_column: join.base_column,
join_column: join.join_column || 'id',
});
}
else if (join.join_column) {
joins.push({
model_name: join.model_class._name,
type: join.type,
alias: join.alias || join.model_class._name,
base_column: 'id',
join_column: join.join_column,
});
}
else {
const child_integrities = this._model._integrities.filter((item) => item.child === join.model_class);
if (child_integrities.length === 1) {
joins.push({
model_name: join.model_class._name,
type: join.type,
alias: join.alias || join.model_class._name,
base_column: 'id',
join_column: child_integrities[0].column,
});
}
else {
const parent_integrities = this._model._integrities.filter((item) => item.parent === join.model_class);
if (parent_integrities.length === 1) {
joins.push({
model_name: join.model_class._name,
type: join.type,
alias: join.alias || join.model_class._name,
base_column: parent_integrities[0].column,
join_column: 'id',
});
}
}
}
}
return Object.assign({ conditions_of_group: this._options.conditions_of_group, explain: this._options.explain, group_by, group_fields: this._options.group_fields, joins, lean: this._options.lean, limit: this._options.limit, near: this._options.near, node: this._options.node, index_hint: this._options.index_hint, orders, skip: this._options.skip, transaction: this._options.transaction, distinct: this._options.distinct }, (select_raw.length > 0 && { select, select_raw }));
}

@@ -503,0 +591,0 @@ async _execAndInclude(options) {

{
"name": "cormo",
"description": "ORM framework for Node.js",
"version": "0.16.13",
"version": "0.17.0",
"keywords": [

@@ -70,3 +70,3 @@ "orm",

},
"gitHead": "1ff8c0c42d91179954061a916377ea6a107e8279"
"gitHead": "872e1e3c54a371ba6e51a3e26f5fd9b0439e05ed"
}
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