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

@contember/schema-utils

Package Overview
Dependencies
Maintainers
5
Versions
259
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@contember/schema-utils - npm Package Compare versions

Comparing version 1.3.0-alpha.7 to 1.3.0-alpha.8

dist/src/SchemaDatabaseMetadata.d.ts

16

dist/src/definition-generator/DefinitionCodeGenerator.js

@@ -41,16 +41,8 @@ "use strict";

generateUniqueConstraint({ entity, constraint }) {
const defaultName = model_1.NamingHelper.createUniqueConstraintName(entity.name, constraint.fields);
if (defaultName === constraint.name) {
const fieldsList = `${constraint.fields.map(it => (0, printJsValue_1.printJsValue)(it)).join(', ')}`;
return `@def.Unique(${fieldsList})`;
}
return `@def.Unique(${(0, printJsValue_1.printJsValue)(constraint)})`;
const fieldsList = `${constraint.fields.map(it => (0, printJsValue_1.printJsValue)(it)).join(', ')}`;
return `@def.Unique(${fieldsList})`;
}
generateIndex({ entity, index }) {
const defaultName = model_1.NamingHelper.createIndexName(entity.name, index.fields);
if (defaultName === index.name) {
const fieldsList = `${index.fields.map(it => (0, printJsValue_1.printJsValue)(it)).join(', ')}`;
return `@def.Index(${fieldsList})`;
}
return `@def.Index(${(0, printJsValue_1.printJsValue)(index)})`;
const fieldsList = `${index.fields.map(it => (0, printJsValue_1.printJsValue)(it)).join(', ')}`;
return `@def.Index(${fieldsList})`;
}

@@ -57,0 +49,0 @@ generateView({ entity }) {

import { Schema } from '@contember/schema';
import * as Typesafe from '@contember/typesafe';
export * from './definition-generator';
export * from './SchemaDatabaseMetadata';
export * from './lax';

@@ -5,0 +6,0 @@ export * from './model';

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

__exportStar(require("./definition-generator"), exports);
__exportStar(require("./SchemaDatabaseMetadata"), exports);
__exportStar(require("./lax"), exports);

@@ -37,0 +38,0 @@ __exportStar(require("./model"), exports);

@@ -185,4 +185,4 @@ "use strict";

name: entityName,
unique: {},
indexes: {},
unique: [],
indexes: [],
primary: 'id',

@@ -189,0 +189,0 @@ primaryColumn: 'id',

export * from './modelUtils';
export * from './NamingHelper';
export * from './resolveDefaultColumnType';
export * from './NamingConventions';
//# sourceMappingURL=index.d.ts.map

@@ -18,5 +18,4 @@ "use strict";

__exportStar(require("./modelUtils"), exports);
__exportStar(require("./NamingHelper"), exports);
__exportStar(require("./resolveDefaultColumnType"), exports);
__exportStar(require("./NamingConventions"), exports);
//# sourceMappingURL=index.js.map

@@ -13,8 +13,6 @@ import * as Typesafe from '@contember/typesafe';

};
readonly unique: {
readonly [x: string]: {
readonly fields: readonly string[];
readonly name: string;
};
};
readonly unique: readonly {
readonly fields: readonly string[];
readonly name?: string | undefined;
}[];
readonly indexes: Model.Indexes;

@@ -21,0 +19,0 @@ readonly eventLog: {

@@ -128,6 +128,8 @@ "use strict";

const viewSchema = viewSchemaInner;
const indexesSchema = Typesafe.coalesce(Typesafe.record(Typesafe.string, Typesafe.object({
const indexLike = Typesafe.intersection(Typesafe.object({
fields: Typesafe.array(Typesafe.string),
}), Typesafe.partial({
name: Typesafe.string,
})), {});
}));
const indexesSchema = Typesafe.coalesce(Typesafe.preprocess(Typesafe.array(indexLike), it => (it === null || it === void 0 ? void 0 : it.constructor) === Object ? Object.values(it) : it), []);
const entitySchema = Typesafe.intersection(Typesafe.object({

@@ -139,6 +141,3 @@ name: Typesafe.string,

fields: Typesafe.record(Typesafe.string, fieldSchema),
unique: Typesafe.record(Typesafe.string, Typesafe.object({
fields: Typesafe.array(Typesafe.string),
name: Typesafe.string,
})),
unique: Typesafe.preprocess(Typesafe.array(indexLike), it => (it === null || it === void 0 ? void 0 : it.constructor) === Object ? Object.values(it) : it),
indexes: indexesSchema,

@@ -145,0 +144,0 @@ eventLog: eventLogSchema,

@@ -49,11 +49,6 @@ "use strict";

validateUniqueConstraints(uniqueConstraints, fields, errors) {
for (const [constraintName, constraint] of Object.entries(uniqueConstraints)) {
const uniqueErrors = errors.for(constraintName);
if (constraint.name !== constraintName) {
uniqueErrors.add('MODEL_NAME_MISMATCH', `Constraint name ${constraint.name} does not match the name in a map "${constraintName}"`);
continue;
}
for (const constraint of uniqueConstraints) {
for (const field of constraint.fields) {
if (!fields.has(field)) {
uniqueErrors.add('MODEL_UNDEFINED_FIELD', `Referenced field ${field} in a constraint does not exists`);
errors.add('MODEL_UNDEFINED_FIELD', `Referenced field ${field} in a constraint does not exists`);
}

@@ -254,23 +249,2 @@ }

}
for (const entity of entities) {
const entityErrorBuilder = errorBuilder.for(entity.name);
for (const index of Object.values(entity.indexes)) {
const description = `index name ${index.name} of entity ${entity.name}`;
if (relationNames[index.name]) {
entityErrorBuilder.add('MODEL_NAME_COLLISION', `${description} collides with ${relationNames[index.name]}`);
}
else {
relationNames[index.name] = description;
}
}
for (const unique of Object.values(entity.unique)) {
const description = `unique index name ${unique.name} of entity ${entity.name}`;
if (relationNames[unique.name]) {
entityErrorBuilder.add('MODEL_NAME_COLLISION', `${description} collides with ${relationNames[unique.name]}`);
}
else {
relationNames[unique.name] = description;
}
}
}
}

@@ -277,0 +251,0 @@ }

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

const src_1 = require("../../../src");
(0, vitest_1.test)('index name collision', () => {
const model = {
enums: {},
entities: {
Foo: {
fields: {
id: {
columnName: 'id',
name: 'id',
nullable: false,
type: schema_1.Model.ColumnType.Uuid,
columnType: 'uuid',
},
},
name: 'Foo',
primary: 'id',
primaryColumn: 'id',
tableName: 'foo',
unique: {},
eventLog: {
enabled: true,
},
indexes: {
test: { fields: ['id'], name: 'test' },
},
},
Bar: {
fields: {
id: {
columnName: 'id',
name: 'id',
nullable: false,
type: schema_1.Model.ColumnType.Uuid,
columnType: 'uuid',
},
},
name: 'Bar',
primary: 'id',
primaryColumn: 'id',
tableName: 'bar',
unique: {
test: { fields: ['id'], name: 'test' },
},
eventLog: {
enabled: true,
},
indexes: {
foo: { fields: ['id'], name: 'foo' },
},
},
},
};
const validator = new src_1.ModelValidator(model);
vitest_1.assert.deepStrictEqual(validator.validate(), [
{
code: 'MODEL_NAME_COLLISION',
message: 'index name foo of entity Bar collides with table name foo of entity Foo',
path: ['entities', 'Bar'],
},
{
code: 'MODEL_NAME_COLLISION',
message: 'unique index name test of entity Bar collides with index name test of entity Foo',
path: ['entities', 'Bar'],
},
]);
});
(0, vitest_1.test)('"meta" collision', () => {

@@ -91,5 +25,5 @@ const model = {

tableName: 'foo',
unique: {},
unique: [],
eventLog: { enabled: true },
indexes: {},
indexes: [],
},

@@ -110,5 +44,5 @@ FooMeta: {

tableName: 'foo_meta',
unique: {},
unique: [],
eventLog: { enabled: true },
indexes: {},
indexes: [],
},

@@ -129,5 +63,5 @@ BarMeta: {

tableName: 'bar',
unique: {},
unique: [],
eventLog: { enabled: true },
indexes: {},
indexes: [],
},

@@ -134,0 +68,0 @@ },

{
"name": "@contember/schema-utils",
"version": "1.3.0-alpha.7",
"version": "1.3.0-alpha.8",
"license": "Apache-2.0",

@@ -15,11 +15,9 @@ "main": "dist/src/index.js",

"dependencies": {
"@contember/schema": "^1.3.0-alpha.7",
"@contember/typesafe": "^1.3.0-alpha.7",
"crypto-js": "^4.1.1"
"@contember/schema": "^1.3.0-alpha.8",
"@contember/typesafe": "^1.3.0-alpha.8"
},
"devDependencies": {
"@contember/schema-definition": "^1.3.0-alpha.7",
"@types/crypto-js": "^4.1.1",
"@contember/schema-definition": "^1.3.0-alpha.8",
"@types/node": "^18"
}
}

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

import { Acl, Model, Schema, Writable } from '@contember/schema'
import { Model, Schema, Writable } from '@contember/schema'
import {

@@ -7,3 +7,2 @@ acceptFieldVisitor,

NamingConventions,
NamingHelper,
resolveDefaultColumnType,

@@ -61,17 +60,9 @@ } from '../model'

}): string {
const defaultName = NamingHelper.createUniqueConstraintName(entity.name, constraint.fields)
if (defaultName === constraint.name) {
const fieldsList = `${constraint.fields.map(it => printJsValue(it)).join(', ')}`
return `@def.Unique(${fieldsList})`
}
return `@def.Unique(${printJsValue(constraint)})`
const fieldsList = `${constraint.fields.map(it => printJsValue(it)).join(', ')}`
return `@def.Unique(${fieldsList})`
}
private generateIndex({ entity, index }: { entity: Model.Entity; index: Model.Index }): string {
const defaultName = NamingHelper.createIndexName(entity.name, index.fields)
if (defaultName === index.name) {
const fieldsList = `${index.fields.map(it => printJsValue(it)).join(', ')}`
return `@def.Index(${fieldsList})`
}
return `@def.Index(${printJsValue(index)})`
const fieldsList = `${index.fields.map(it => printJsValue(it)).join(', ')}`
return `@def.Index(${fieldsList})`
}

@@ -78,0 +69,0 @@

@@ -8,2 +8,3 @@ import { Schema } from '@contember/schema'

export * from './definition-generator'
export * from './SchemaDatabaseMetadata'
export * from './lax'

@@ -10,0 +11,0 @@ export * from './model'

@@ -195,4 +195,4 @@ import { Model, Schema, Writable } from '@contember/schema'

name: entityName,
unique: {},
indexes: {},
unique: [],
indexes: [],
primary: 'id',

@@ -199,0 +199,0 @@ primaryColumn: 'id',

export * from './modelUtils'
export * from './NamingHelper'
export * from './resolveDefaultColumnType'
export * from './NamingConventions'

@@ -151,7 +151,19 @@ import * as Typesafe from '@contember/typesafe'

const indexesSchema = Typesafe.coalesce<Model.Indexes, Model.Indexes>(Typesafe.record(Typesafe.string, Typesafe.object({
fields: Typesafe.array(Typesafe.string),
name: Typesafe.string,
})), { })
const indexLike: Typesafe.Type<{readonly fields: readonly string[]; readonly name?: string | undefined}> = Typesafe.intersection(
Typesafe.object({
fields: Typesafe.array(Typesafe.string),
}),
Typesafe.partial({
name: Typesafe.string,
}),
)
const indexesSchema = Typesafe.coalesce<Model.Indexes, Model.Indexes>(
Typesafe.preprocess(
Typesafe.array(indexLike),
it => it?.constructor === Object ? Object.values(it) : it,
),
[],
)
const entitySchema = Typesafe.intersection(

@@ -164,6 +176,6 @@ Typesafe.object({

fields: Typesafe.record(Typesafe.string, fieldSchema),
unique: Typesafe.record(Typesafe.string, Typesafe.object({
fields: Typesafe.array(Typesafe.string),
name: Typesafe.string,
})),
unique: Typesafe.preprocess(
Typesafe.array(indexLike),
it => it?.constructor === Object ? Object.values(it) : it,
),
indexes: indexesSchema,

@@ -170,0 +182,0 @@ eventLog: eventLogSchema,

@@ -57,11 +57,6 @@ import { Model } from '@contember/schema'

private validateUniqueConstraints(uniqueConstraints: Model.Entity['unique'], fields: Set<string>, errors: ErrorBuilder): void {
for (const [constraintName, constraint] of Object.entries(uniqueConstraints)) {
const uniqueErrors = errors.for(constraintName)
if (constraint.name !== constraintName) {
uniqueErrors.add('MODEL_NAME_MISMATCH', `Constraint name ${constraint.name} does not match the name in a map "${constraintName}"`)
continue
}
for (const constraint of uniqueConstraints) {
for (const field of constraint.fields) {
if (!fields.has(field)) {
uniqueErrors.add('MODEL_UNDEFINED_FIELD', `Referenced field ${field} in a constraint does not exists`)
errors.add('MODEL_UNDEFINED_FIELD', `Referenced field ${field} in a constraint does not exists`)
}

@@ -270,21 +265,2 @@ }

}
for (const entity of entities) {
const entityErrorBuilder = errorBuilder.for(entity.name)
for (const index of Object.values(entity.indexes)) {
const description = `index name ${index.name} of entity ${entity.name}`
if (relationNames[index.name]) {
entityErrorBuilder.add('MODEL_NAME_COLLISION', `${description} collides with ${relationNames[index.name]}`)
} else {
relationNames[index.name] = description
}
}
for (const unique of Object.values(entity.unique)) {
const description = `unique index name ${unique.name} of entity ${entity.name}`
if (relationNames[unique.name]) {
entityErrorBuilder.add('MODEL_NAME_COLLISION', `${description} collides with ${relationNames[unique.name]}`)
} else {
relationNames[unique.name] = description
}
}
}
}

@@ -291,0 +267,0 @@ }

@@ -7,70 +7,3 @@ import { assert, test } from 'vitest'

test('index name collision', () => {
const model: Model.Schema = {
enums: {},
entities: {
Foo: {
fields: {
id: {
columnName: 'id',
name: 'id',
nullable: false,
type: Model.ColumnType.Uuid,
columnType: 'uuid',
},
},
name: 'Foo',
primary: 'id',
primaryColumn: 'id',
tableName: 'foo',
unique: {},
eventLog: {
enabled: true,
},
indexes: {
test: { fields: ['id'], name: 'test' },
},
},
Bar: {
fields: {
id: {
columnName: 'id',
name: 'id',
nullable: false,
type: Model.ColumnType.Uuid,
columnType: 'uuid',
},
},
name: 'Bar',
primary: 'id',
primaryColumn: 'id',
tableName: 'bar',
unique: {
test: { fields: ['id'], name: 'test' },
},
eventLog: {
enabled: true,
},
indexes: {
foo: { fields: ['id'], name: 'foo' },
},
},
},
}
const validator = new ModelValidator(model)
assert.deepStrictEqual(validator.validate(), [
{
code: 'MODEL_NAME_COLLISION',
message: 'index name foo of entity Bar collides with table name foo of entity Foo',
path: ['entities', 'Bar'],
},
{
code: 'MODEL_NAME_COLLISION',
message: 'unique index name test of entity Bar collides with index name test of entity Foo',
path: ['entities', 'Bar'],
},
])
})
test('"meta" collision', () => {

@@ -94,5 +27,5 @@ const model: Model.Schema = {

tableName: 'foo',
unique: {},
unique: [],
eventLog: { enabled: true },
indexes: {},
indexes: [],
},

@@ -113,5 +46,5 @@ FooMeta: {

tableName: 'foo_meta',
unique: {},
unique: [],
eventLog: { enabled: true },
indexes: {},
indexes: [],
},

@@ -132,5 +65,5 @@ BarMeta: {

tableName: 'bar',
unique: {},
unique: [],
eventLog: { enabled: true },
indexes: {},
indexes: [],
},

@@ -137,0 +70,0 @@ },

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

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