@cheetah.js/orm 0.1.146 → 0.1.147
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/decorators/unique.decorator.d.ts +9 -0
- package/dist/decorators/unique.decorator.js +44 -0
- package/dist/domain/entities.d.ts +2 -1
- package/dist/domain/entities.js +28 -0
- package/dist/driver/bun-mysql.driver.d.ts +7 -0
- package/dist/driver/bun-mysql.driver.js +11 -0
- package/dist/driver/bun-pg.driver.d.ts +7 -0
- package/dist/driver/bun-pg.driver.js +11 -0
- package/dist/driver/driver.interface.d.ts +12 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/package.json +2 -2
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export type UniqueDefinition = {
|
|
2
|
+
name: string;
|
|
3
|
+
properties: string[];
|
|
4
|
+
};
|
|
5
|
+
type UniqueOptions<T> = {
|
|
6
|
+
properties: (keyof T)[];
|
|
7
|
+
} | (keyof T)[] | undefined;
|
|
8
|
+
export declare function Unique<T>(options?: UniqueOptions<T>): ClassDecorator & PropertyDecorator;
|
|
9
|
+
export {};
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Unique = Unique;
|
|
4
|
+
const core_1 = require("@cheetah.js/core");
|
|
5
|
+
function getCtor(target) {
|
|
6
|
+
return typeof target === "function" ? target : target.constructor;
|
|
7
|
+
}
|
|
8
|
+
function buildFromOptions(options) {
|
|
9
|
+
const props = Array.isArray(options) ? options : options?.properties;
|
|
10
|
+
if (!props || props.length === 0) {
|
|
11
|
+
return null;
|
|
12
|
+
}
|
|
13
|
+
const keys = props;
|
|
14
|
+
return {
|
|
15
|
+
name: `${keys.join('_')}_unique`,
|
|
16
|
+
properties: keys,
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
function buildFromProperty(propertyKey) {
|
|
20
|
+
const name = String(propertyKey);
|
|
21
|
+
return {
|
|
22
|
+
name: `${name}_unique`,
|
|
23
|
+
properties: [name],
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
function resolveUnique(options, propertyKey) {
|
|
27
|
+
const fromOptions = buildFromOptions(options);
|
|
28
|
+
if (fromOptions) {
|
|
29
|
+
return fromOptions;
|
|
30
|
+
}
|
|
31
|
+
if (!propertyKey) {
|
|
32
|
+
throw new Error("@Unique on class requires properties option");
|
|
33
|
+
}
|
|
34
|
+
return buildFromProperty(propertyKey);
|
|
35
|
+
}
|
|
36
|
+
function Unique(options) {
|
|
37
|
+
return (target, propertyKey) => {
|
|
38
|
+
const ctor = getCtor(target);
|
|
39
|
+
const uniques = [...(core_1.Metadata.get("uniques", ctor) || [])];
|
|
40
|
+
const unique = resolveUnique(options, propertyKey);
|
|
41
|
+
uniques.push(unique);
|
|
42
|
+
core_1.Metadata.set("uniques", uniques, ctor);
|
|
43
|
+
};
|
|
44
|
+
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { PropertyOptions } from "../decorators/property.decorator";
|
|
2
|
-
import { Relationship, SnapshotIndexInfo, SnapshotTable } from "../driver/driver.interface";
|
|
2
|
+
import { Relationship, SnapshotIndexInfo, SnapshotTable, SnapshotUniqueInfo } from "../driver/driver.interface";
|
|
3
3
|
export type Property = {
|
|
4
4
|
options: PropertyOptions;
|
|
5
5
|
type: Function;
|
|
@@ -10,6 +10,7 @@ export type Options = {
|
|
|
10
10
|
};
|
|
11
11
|
hideProperties: string[];
|
|
12
12
|
indexes?: SnapshotIndexInfo[];
|
|
13
|
+
uniques?: SnapshotUniqueInfo[];
|
|
13
14
|
relations: Relationship<any>[];
|
|
14
15
|
tableName: string;
|
|
15
16
|
hooks?: {
|
package/dist/domain/entities.js
CHANGED
|
@@ -77,6 +77,31 @@ function buildIndexWhere(where, columnMap) {
|
|
|
77
77
|
const builder = new index_condition_builder_1.IndexConditionBuilder(columnMap);
|
|
78
78
|
return builder.build(where);
|
|
79
79
|
}
|
|
80
|
+
function mapUniqueDefinitions(uniques, entityName, columnMap) {
|
|
81
|
+
return uniques.map((unique) => toSnapshotUnique(unique, entityName, columnMap));
|
|
82
|
+
}
|
|
83
|
+
function toSnapshotUnique(unique, entityName, columnMap) {
|
|
84
|
+
const columns = resolveUniqueColumns(unique, columnMap);
|
|
85
|
+
const uniqueName = resolveUniqueName(unique.name, entityName, columns);
|
|
86
|
+
return {
|
|
87
|
+
table: entityName,
|
|
88
|
+
uniqueName,
|
|
89
|
+
columnName: columns.join(","),
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
function resolveUniqueColumns(unique, columnMap) {
|
|
93
|
+
return unique.properties.map((propName) => resolveUniqueColumn(propName, columnMap));
|
|
94
|
+
}
|
|
95
|
+
function resolveUniqueColumn(propName, columnMap) {
|
|
96
|
+
const mapped = columnMap[propName];
|
|
97
|
+
if (mapped) {
|
|
98
|
+
return mapped;
|
|
99
|
+
}
|
|
100
|
+
return (0, utils_1.toSnakeCase)(propName);
|
|
101
|
+
}
|
|
102
|
+
function resolveUniqueName(name, entityName, columns) {
|
|
103
|
+
return `${columns.join("_")}_unique`;
|
|
104
|
+
}
|
|
80
105
|
let EntityStorage = EntityStorage_1 = class EntityStorage {
|
|
81
106
|
constructor() {
|
|
82
107
|
this.entities = new Map();
|
|
@@ -85,6 +110,7 @@ let EntityStorage = EntityStorage_1 = class EntityStorage {
|
|
|
85
110
|
add(entity, properties, relations, hooks) {
|
|
86
111
|
const entityName = entity.options?.tableName || (0, utils_1.toSnakeCase)(entity.target.name);
|
|
87
112
|
const indexes = core_1.Metadata.get("indexes", entity.target) || [];
|
|
113
|
+
const uniques = core_1.Metadata.get("uniques", entity.target) || [];
|
|
88
114
|
const columnMap = buildIndexColumnMap(properties, relations);
|
|
89
115
|
this.entities.set(entity.target, {
|
|
90
116
|
properties: properties,
|
|
@@ -93,6 +119,7 @@ let EntityStorage = EntityStorage_1 = class EntityStorage {
|
|
|
93
119
|
.map(([key]) => key),
|
|
94
120
|
relations,
|
|
95
121
|
indexes: mapIndexDefinitions(indexes, entityName, columnMap),
|
|
122
|
+
uniques: mapUniqueDefinitions(uniques, entityName, columnMap),
|
|
96
123
|
hooks,
|
|
97
124
|
tableName: entityName,
|
|
98
125
|
...entity.options,
|
|
@@ -116,6 +143,7 @@ let EntityStorage = EntityStorage_1 = class EntityStorage {
|
|
|
116
143
|
tableName: values.tableName,
|
|
117
144
|
schema: values.schema || "public",
|
|
118
145
|
indexes: values.indexes || [],
|
|
146
|
+
uniques: values.uniques || [],
|
|
119
147
|
columns: this.snapshotColumns(values),
|
|
120
148
|
};
|
|
121
149
|
}
|
|
@@ -26,6 +26,13 @@ export declare class BunMysqlDriver extends BunDriverBase implements DriverInter
|
|
|
26
26
|
name: string;
|
|
27
27
|
properties?: string[];
|
|
28
28
|
}, schema: string | undefined, tableName: string): string;
|
|
29
|
+
getCreateUniqueConstraint(unique: {
|
|
30
|
+
name: string;
|
|
31
|
+
properties?: string[];
|
|
32
|
+
}, schema: string | undefined, tableName: string): string;
|
|
33
|
+
getDropUniqueConstraint(unique: {
|
|
34
|
+
name: string;
|
|
35
|
+
}, schema: string | undefined, tableName: string): string;
|
|
29
36
|
getAlterTableType(schema: string | undefined, tableName: string, colName: string, colDiff: ColDiff): string;
|
|
30
37
|
getAlterTableDefaultInstruction(schema: string | undefined, tableName: string, colName: string, colDiff: ColDiff): string;
|
|
31
38
|
getAlterTablePrimaryKeyInstruction(schema: string | undefined, tableName: string, colName: string, colDiff: ColDiff): string;
|
|
@@ -141,6 +141,17 @@ class BunMysqlDriver extends bun_driver_base_1.BunDriverBase {
|
|
|
141
141
|
getDropIndex(index, schema, tableName) {
|
|
142
142
|
return `ALTER TABLE \`${schema}\`.\`${tableName}\` DROP INDEX \`${index.name}\`;`;
|
|
143
143
|
}
|
|
144
|
+
getCreateUniqueConstraint(unique, schema, tableName) {
|
|
145
|
+
const properties = unique.properties || [];
|
|
146
|
+
if (properties.length === 0) {
|
|
147
|
+
throw new Error("Unique properties are required.");
|
|
148
|
+
}
|
|
149
|
+
const columns = properties.map((prop) => `\`${prop}\``).join(', ');
|
|
150
|
+
return `ALTER TABLE \`${schema}\`.\`${tableName}\` ADD CONSTRAINT \`${unique.name}\` UNIQUE (${columns});`;
|
|
151
|
+
}
|
|
152
|
+
getDropUniqueConstraint(unique, schema, tableName) {
|
|
153
|
+
return `ALTER TABLE \`${schema}\`.\`${tableName}\` DROP INDEX \`${unique.name}\`;`;
|
|
154
|
+
}
|
|
144
155
|
getAlterTableType(schema, tableName, colName, colDiff) {
|
|
145
156
|
return `ALTER TABLE \`${schema}\`.\`${tableName}\` MODIFY COLUMN \`${colName}\` ${colDiff.colType}${colDiff.colLength ? `(${colDiff.colLength})` : ''};`;
|
|
146
157
|
}
|
|
@@ -25,6 +25,13 @@ export declare class BunPgDriver extends BunDriverBase implements DriverInterfac
|
|
|
25
25
|
name: string;
|
|
26
26
|
properties?: string[];
|
|
27
27
|
}, schema: string | undefined, tableName: string): string;
|
|
28
|
+
getCreateUniqueConstraint(unique: {
|
|
29
|
+
name: string;
|
|
30
|
+
properties?: string[];
|
|
31
|
+
}, schema: string | undefined, tableName: string): string;
|
|
32
|
+
getDropUniqueConstraint(unique: {
|
|
33
|
+
name: string;
|
|
34
|
+
}, schema: string | undefined, tableName: string): string;
|
|
28
35
|
getAlterTableType(schema: string | undefined, tableName: string, colName: string, colDiff: ColDiff): string;
|
|
29
36
|
getAlterTableDefaultInstruction(schema: string | undefined, tableName: string, colName: string, colDiff: ColDiff): string;
|
|
30
37
|
getAlterTablePrimaryKeyInstruction(schema: string | undefined, tableName: string, colName: string, colDiff: ColDiff): string;
|
|
@@ -114,6 +114,17 @@ class BunPgDriver extends bun_driver_base_1.BunDriverBase {
|
|
|
114
114
|
getDropIndex(index, schema, tableName) {
|
|
115
115
|
return this.getDropConstraint(index, schema, tableName);
|
|
116
116
|
}
|
|
117
|
+
getCreateUniqueConstraint(unique, schema, tableName) {
|
|
118
|
+
const properties = unique.properties || [];
|
|
119
|
+
if (properties.length === 0) {
|
|
120
|
+
throw new Error("Unique properties are required.");
|
|
121
|
+
}
|
|
122
|
+
const columns = properties.map((prop) => `"${prop}"`).join(', ');
|
|
123
|
+
return `ALTER TABLE "${schema}"."${tableName}" ADD CONSTRAINT "${unique.name}" UNIQUE (${columns});`;
|
|
124
|
+
}
|
|
125
|
+
getDropUniqueConstraint(unique, schema, tableName) {
|
|
126
|
+
return this.getDropConstraint(unique, schema, tableName);
|
|
127
|
+
}
|
|
117
128
|
getAlterTableType(schema, tableName, colName, colDiff) {
|
|
118
129
|
return `ALTER TABLE "${schema}"."${tableName}" ALTER COLUMN "${colName}" TYPE ${colDiff.colType}${colDiff.colLength ? `(${colDiff.colLength})` : ''};`;
|
|
119
130
|
}
|
|
@@ -20,6 +20,8 @@ export interface DriverInterface {
|
|
|
20
20
|
getAddColumn(schema: string | undefined, tableName: string, colName: string, colDiff: ColDiff, colDiffInstructions: string[]): void;
|
|
21
21
|
getDropColumn(colDiffInstructions: string[], schema: string | undefined, tableName: string, colName: string): void;
|
|
22
22
|
getDropIndex(index: IndexStatement, schema: string | undefined, tableName: string): string;
|
|
23
|
+
getCreateUniqueConstraint(unique: UniqueStatement, schema: string | undefined, tableName: string): string;
|
|
24
|
+
getDropUniqueConstraint(unique: UniqueStatement, schema: string | undefined, tableName: string): string;
|
|
23
25
|
getAlterTableType(schema: string | undefined, tableName: string, colName: string, colDiff: ColDiff): string;
|
|
24
26
|
getAlterTableDefaultInstruction(schema: string | undefined, tableName: string, colName: string, colDiff: ColDiff): string;
|
|
25
27
|
getAlterTablePrimaryKeyInstruction(schema: string | undefined, tableName: string, colName: string, colDiff: ColDiff): string;
|
|
@@ -136,6 +138,7 @@ export type SnapshotTable = {
|
|
|
136
138
|
schema?: string;
|
|
137
139
|
columns: ColumnsInfo[];
|
|
138
140
|
indexes: SnapshotIndexInfo[];
|
|
141
|
+
uniques?: SnapshotUniqueInfo[];
|
|
139
142
|
foreignKeys?: ForeignKeyInfo[];
|
|
140
143
|
};
|
|
141
144
|
export type SnapshotIndexInfo = {
|
|
@@ -144,11 +147,20 @@ export type SnapshotIndexInfo = {
|
|
|
144
147
|
columnName: string;
|
|
145
148
|
where?: string;
|
|
146
149
|
};
|
|
150
|
+
export type SnapshotUniqueInfo = {
|
|
151
|
+
table: string;
|
|
152
|
+
uniqueName: string;
|
|
153
|
+
columnName: string;
|
|
154
|
+
};
|
|
147
155
|
export type IndexStatement = {
|
|
148
156
|
name: string;
|
|
149
157
|
properties?: string[];
|
|
150
158
|
where?: string;
|
|
151
159
|
};
|
|
160
|
+
export type UniqueStatement = {
|
|
161
|
+
name: string;
|
|
162
|
+
properties?: string[];
|
|
163
|
+
};
|
|
152
164
|
export type ForeignKeyInfo = {
|
|
153
165
|
referencedTableName: string;
|
|
154
166
|
referencedColumnName: string;
|
package/dist/index.d.ts
CHANGED
|
@@ -3,6 +3,7 @@ export * from './decorators/property.decorator';
|
|
|
3
3
|
export * from './decorators/primary-key.decorator';
|
|
4
4
|
export * from './decorators/one-many.decorator';
|
|
5
5
|
export * from './decorators/index.decorator';
|
|
6
|
+
export * from './decorators/unique.decorator';
|
|
6
7
|
export * from './decorators/event-hook.decorator';
|
|
7
8
|
export * from './decorators/enum.decorator';
|
|
8
9
|
export * from './decorators/computed.decorator';
|
package/dist/index.js
CHANGED
|
@@ -20,6 +20,7 @@ __exportStar(require("./decorators/property.decorator"), exports);
|
|
|
20
20
|
__exportStar(require("./decorators/primary-key.decorator"), exports);
|
|
21
21
|
__exportStar(require("./decorators/one-many.decorator"), exports);
|
|
22
22
|
__exportStar(require("./decorators/index.decorator"), exports);
|
|
23
|
+
__exportStar(require("./decorators/unique.decorator"), exports);
|
|
23
24
|
__exportStar(require("./decorators/event-hook.decorator"), exports);
|
|
24
25
|
__exportStar(require("./decorators/enum.decorator"), exports);
|
|
25
26
|
__exportStar(require("./decorators/computed.decorator"), exports);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cheetah.js/orm",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.147",
|
|
4
4
|
"description": "A simple ORM for Cheetah.js.",
|
|
5
5
|
"type": "commonjs",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -55,5 +55,5 @@
|
|
|
55
55
|
"bun",
|
|
56
56
|
"value-object"
|
|
57
57
|
],
|
|
58
|
-
"gitHead": "
|
|
58
|
+
"gitHead": "9efbea28c5bb4bdc91e8aa6ba03a634f0a4ab00f"
|
|
59
59
|
}
|