metal-orm 1.0.33 → 1.0.34
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/README.md +7 -5
- package/dist/index.cjs +295 -0
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +2585 -98
- package/dist/index.d.ts +2585 -98
- package/dist/index.js +285 -0
- package/dist/index.js.map +1 -1
- package/package.json +1 -6
- package/scripts/generate-entities.mjs +1 -1
- package/src/index.ts +1 -0
- package/src/orm/entity.ts +5 -5
- package/src/orm/execute.ts +4 -4
- package/src/orm/orm-session.ts +229 -229
- package/src/query-builder/select.ts +885 -886
- package/src/schema/types.ts +39 -39
- package/dist/decorators/index.cjs +0 -4968
- package/dist/decorators/index.cjs.map +0 -1
- package/dist/decorators/index.d.cts +0 -70
- package/dist/decorators/index.d.ts +0 -70
- package/dist/decorators/index.js +0 -4933
- package/dist/decorators/index.js.map +0 -1
- package/dist/select-BuMpVcVt.d.cts +0 -2424
- package/dist/select-BuMpVcVt.d.ts +0 -2424
package/dist/index.js
CHANGED
|
@@ -4968,12 +4968,42 @@ var createLiteral = (val) => ({ type: "Literal", value: val });
|
|
|
4968
4968
|
|
|
4969
4969
|
// src/orm/entity-metadata.ts
|
|
4970
4970
|
var metadataMap = /* @__PURE__ */ new Map();
|
|
4971
|
+
var ensureEntityMetadata = (target) => {
|
|
4972
|
+
let meta = metadataMap.get(target);
|
|
4973
|
+
if (!meta) {
|
|
4974
|
+
meta = {
|
|
4975
|
+
target,
|
|
4976
|
+
tableName: target.name || "unknown",
|
|
4977
|
+
columns: {},
|
|
4978
|
+
relations: {}
|
|
4979
|
+
};
|
|
4980
|
+
metadataMap.set(target, meta);
|
|
4981
|
+
}
|
|
4982
|
+
return meta;
|
|
4983
|
+
};
|
|
4971
4984
|
var getEntityMetadata = (target) => {
|
|
4972
4985
|
return metadataMap.get(target);
|
|
4973
4986
|
};
|
|
4974
4987
|
var getAllEntityMetadata = () => {
|
|
4975
4988
|
return Array.from(metadataMap.values());
|
|
4976
4989
|
};
|
|
4990
|
+
var addColumnMetadata = (target, propertyKey, column) => {
|
|
4991
|
+
const meta = ensureEntityMetadata(target);
|
|
4992
|
+
meta.columns[propertyKey] = { ...column };
|
|
4993
|
+
};
|
|
4994
|
+
var addRelationMetadata = (target, propertyKey, relation) => {
|
|
4995
|
+
const meta = ensureEntityMetadata(target);
|
|
4996
|
+
meta.relations[propertyKey] = relation;
|
|
4997
|
+
};
|
|
4998
|
+
var setEntityTableName = (target, tableName, hooks) => {
|
|
4999
|
+
const meta = ensureEntityMetadata(target);
|
|
5000
|
+
if (tableName && tableName.length > 0) {
|
|
5001
|
+
meta.tableName = tableName;
|
|
5002
|
+
}
|
|
5003
|
+
if (hooks) {
|
|
5004
|
+
meta.hooks = hooks;
|
|
5005
|
+
}
|
|
5006
|
+
};
|
|
4977
5007
|
var buildTableDef = (meta) => {
|
|
4978
5008
|
if (meta.table) {
|
|
4979
5009
|
return meta.table;
|
|
@@ -7471,6 +7501,251 @@ var Orm = class {
|
|
|
7471
7501
|
}
|
|
7472
7502
|
};
|
|
7473
7503
|
|
|
7504
|
+
// src/decorators/decorator-metadata.ts
|
|
7505
|
+
var METADATA_KEY = "metal-orm:decorators";
|
|
7506
|
+
var isStandardDecoratorContext = (value) => {
|
|
7507
|
+
return typeof value === "object" && value !== null && "kind" in value;
|
|
7508
|
+
};
|
|
7509
|
+
var getOrCreateMetadataBag = (context) => {
|
|
7510
|
+
const metadata = context.metadata || (context.metadata = {});
|
|
7511
|
+
const existing = metadata[METADATA_KEY];
|
|
7512
|
+
if (existing) {
|
|
7513
|
+
return existing;
|
|
7514
|
+
}
|
|
7515
|
+
const bag = { columns: [], relations: [] };
|
|
7516
|
+
metadata[METADATA_KEY] = bag;
|
|
7517
|
+
return bag;
|
|
7518
|
+
};
|
|
7519
|
+
var readMetadataBag = (context) => {
|
|
7520
|
+
return context.metadata?.[METADATA_KEY];
|
|
7521
|
+
};
|
|
7522
|
+
var registerInitializer = (context, initializer) => {
|
|
7523
|
+
context.addInitializer?.(initializer);
|
|
7524
|
+
};
|
|
7525
|
+
|
|
7526
|
+
// src/decorators/entity.ts
|
|
7527
|
+
var toSnakeCase = (value) => {
|
|
7528
|
+
return value.replace(/([a-z0-9])([A-Z])/g, "$1_$2").replace(/[^a-z0-9_]+/gi, "_").replace(/__+/g, "_").replace(/^_|_$/g, "").toLowerCase();
|
|
7529
|
+
};
|
|
7530
|
+
var deriveTableNameFromConstructor = (ctor) => {
|
|
7531
|
+
const fallback = "unknown";
|
|
7532
|
+
const rawName = ctor.name || fallback;
|
|
7533
|
+
const strippedName = rawName.replace(/Entity$/i, "");
|
|
7534
|
+
const normalized = toSnakeCase(strippedName || rawName);
|
|
7535
|
+
if (!normalized) {
|
|
7536
|
+
return fallback;
|
|
7537
|
+
}
|
|
7538
|
+
return normalized.endsWith("s") ? normalized : `${normalized}s`;
|
|
7539
|
+
};
|
|
7540
|
+
function Entity(options = {}) {
|
|
7541
|
+
const decorator = (value) => {
|
|
7542
|
+
const tableName = options.tableName ?? deriveTableNameFromConstructor(value);
|
|
7543
|
+
setEntityTableName(value, tableName, options.hooks);
|
|
7544
|
+
return value;
|
|
7545
|
+
};
|
|
7546
|
+
const decoratorWithContext = (value, context) => {
|
|
7547
|
+
const ctor = value;
|
|
7548
|
+
decorator(ctor);
|
|
7549
|
+
if (context && isStandardDecoratorContext(context)) {
|
|
7550
|
+
const bag = readMetadataBag(context);
|
|
7551
|
+
if (bag) {
|
|
7552
|
+
const meta = ensureEntityMetadata(ctor);
|
|
7553
|
+
for (const entry of bag.columns) {
|
|
7554
|
+
if (!meta.columns[entry.propertyName]) {
|
|
7555
|
+
addColumnMetadata(ctor, entry.propertyName, { ...entry.column });
|
|
7556
|
+
}
|
|
7557
|
+
}
|
|
7558
|
+
for (const entry of bag.relations) {
|
|
7559
|
+
if (!meta.relations[entry.propertyName]) {
|
|
7560
|
+
addRelationMetadata(ctor, entry.propertyName, entry.relation);
|
|
7561
|
+
}
|
|
7562
|
+
}
|
|
7563
|
+
}
|
|
7564
|
+
}
|
|
7565
|
+
return ctor;
|
|
7566
|
+
};
|
|
7567
|
+
return decoratorWithContext;
|
|
7568
|
+
}
|
|
7569
|
+
|
|
7570
|
+
// src/decorators/column.ts
|
|
7571
|
+
var normalizeColumnInput = (input) => {
|
|
7572
|
+
const asOptions = input;
|
|
7573
|
+
const asDefinition = input;
|
|
7574
|
+
const column = {
|
|
7575
|
+
type: asOptions.type ?? asDefinition.type,
|
|
7576
|
+
args: asOptions.args ?? asDefinition.args,
|
|
7577
|
+
notNull: asOptions.notNull ?? asDefinition.notNull,
|
|
7578
|
+
primary: asOptions.primary ?? asDefinition.primary,
|
|
7579
|
+
unique: asDefinition.unique,
|
|
7580
|
+
default: asDefinition.default,
|
|
7581
|
+
autoIncrement: asDefinition.autoIncrement,
|
|
7582
|
+
generated: asDefinition.generated,
|
|
7583
|
+
check: asDefinition.check,
|
|
7584
|
+
references: asDefinition.references,
|
|
7585
|
+
comment: asDefinition.comment
|
|
7586
|
+
};
|
|
7587
|
+
if (!column.type) {
|
|
7588
|
+
throw new Error("Column decorator requires a column type");
|
|
7589
|
+
}
|
|
7590
|
+
return column;
|
|
7591
|
+
};
|
|
7592
|
+
var normalizePropertyName = (name) => {
|
|
7593
|
+
if (typeof name === "symbol") {
|
|
7594
|
+
return name.description ?? name.toString();
|
|
7595
|
+
}
|
|
7596
|
+
return name;
|
|
7597
|
+
};
|
|
7598
|
+
var resolveConstructor = (target) => {
|
|
7599
|
+
if (typeof target === "function") {
|
|
7600
|
+
return target;
|
|
7601
|
+
}
|
|
7602
|
+
if (target && typeof target.constructor === "function") {
|
|
7603
|
+
return target.constructor;
|
|
7604
|
+
}
|
|
7605
|
+
return void 0;
|
|
7606
|
+
};
|
|
7607
|
+
var registerColumn = (ctor, propertyName, column) => {
|
|
7608
|
+
const meta = ensureEntityMetadata(ctor);
|
|
7609
|
+
if (meta.columns[propertyName]) {
|
|
7610
|
+
return;
|
|
7611
|
+
}
|
|
7612
|
+
addColumnMetadata(ctor, propertyName, column);
|
|
7613
|
+
};
|
|
7614
|
+
var registerColumnFromContext = (context, column) => {
|
|
7615
|
+
if (!context.name) {
|
|
7616
|
+
throw new Error("Column decorator requires a property name");
|
|
7617
|
+
}
|
|
7618
|
+
const propertyName = normalizePropertyName(context.name);
|
|
7619
|
+
const bag = getOrCreateMetadataBag(context);
|
|
7620
|
+
if (!bag.columns.some((entry) => entry.propertyName === propertyName)) {
|
|
7621
|
+
bag.columns.push({ propertyName, column: { ...column } });
|
|
7622
|
+
}
|
|
7623
|
+
registerInitializer(context, function() {
|
|
7624
|
+
const ctor = resolveConstructor(this);
|
|
7625
|
+
if (!ctor) {
|
|
7626
|
+
return;
|
|
7627
|
+
}
|
|
7628
|
+
registerColumn(ctor, propertyName, column);
|
|
7629
|
+
});
|
|
7630
|
+
};
|
|
7631
|
+
function Column(definition) {
|
|
7632
|
+
const normalized = normalizeColumnInput(definition);
|
|
7633
|
+
const decorator = (targetOrValue, propertyKeyOrContext) => {
|
|
7634
|
+
if (isStandardDecoratorContext(propertyKeyOrContext)) {
|
|
7635
|
+
registerColumnFromContext(propertyKeyOrContext, normalized);
|
|
7636
|
+
return;
|
|
7637
|
+
}
|
|
7638
|
+
const propertyName = normalizePropertyName(propertyKeyOrContext);
|
|
7639
|
+
const ctor = resolveConstructor(targetOrValue);
|
|
7640
|
+
if (!ctor) {
|
|
7641
|
+
throw new Error("Unable to resolve constructor when registering column metadata");
|
|
7642
|
+
}
|
|
7643
|
+
registerColumn(ctor, propertyName, { ...normalized });
|
|
7644
|
+
};
|
|
7645
|
+
return decorator;
|
|
7646
|
+
}
|
|
7647
|
+
function PrimaryKey(definition) {
|
|
7648
|
+
const normalized = normalizeColumnInput(definition);
|
|
7649
|
+
normalized.primary = true;
|
|
7650
|
+
return Column(normalized);
|
|
7651
|
+
}
|
|
7652
|
+
|
|
7653
|
+
// src/decorators/relations.ts
|
|
7654
|
+
var normalizePropertyName2 = (name) => {
|
|
7655
|
+
if (typeof name === "symbol") {
|
|
7656
|
+
return name.description ?? name.toString();
|
|
7657
|
+
}
|
|
7658
|
+
return name;
|
|
7659
|
+
};
|
|
7660
|
+
var resolveConstructor2 = (instanceOrCtor) => {
|
|
7661
|
+
if (typeof instanceOrCtor === "function") {
|
|
7662
|
+
return instanceOrCtor;
|
|
7663
|
+
}
|
|
7664
|
+
if (instanceOrCtor && typeof instanceOrCtor.constructor === "function") {
|
|
7665
|
+
return instanceOrCtor.constructor;
|
|
7666
|
+
}
|
|
7667
|
+
return void 0;
|
|
7668
|
+
};
|
|
7669
|
+
var registerRelation = (ctor, propertyName, metadata) => {
|
|
7670
|
+
addRelationMetadata(ctor, propertyName, metadata);
|
|
7671
|
+
};
|
|
7672
|
+
var createFieldDecorator = (metadataFactory) => {
|
|
7673
|
+
const decorator = (targetOrValue, propertyKeyOrContext) => {
|
|
7674
|
+
if (isStandardDecoratorContext(propertyKeyOrContext)) {
|
|
7675
|
+
const ctx = propertyKeyOrContext;
|
|
7676
|
+
if (!ctx.name) {
|
|
7677
|
+
throw new Error("Relation decorator requires a property name");
|
|
7678
|
+
}
|
|
7679
|
+
const propertyName2 = normalizePropertyName2(ctx.name);
|
|
7680
|
+
const bag = getOrCreateMetadataBag(ctx);
|
|
7681
|
+
const relationMetadata = metadataFactory(propertyName2);
|
|
7682
|
+
if (!bag.relations.some((entry) => entry.propertyName === propertyName2)) {
|
|
7683
|
+
bag.relations.push({ propertyName: propertyName2, relation: relationMetadata });
|
|
7684
|
+
}
|
|
7685
|
+
registerInitializer(ctx, function() {
|
|
7686
|
+
const ctor2 = resolveConstructor2(this);
|
|
7687
|
+
if (!ctor2) {
|
|
7688
|
+
return;
|
|
7689
|
+
}
|
|
7690
|
+
registerRelation(ctor2, propertyName2, relationMetadata);
|
|
7691
|
+
});
|
|
7692
|
+
return;
|
|
7693
|
+
}
|
|
7694
|
+
const propertyName = normalizePropertyName2(propertyKeyOrContext);
|
|
7695
|
+
const ctor = resolveConstructor2(targetOrValue);
|
|
7696
|
+
if (!ctor) {
|
|
7697
|
+
throw new Error("Unable to resolve constructor when registering relation metadata");
|
|
7698
|
+
}
|
|
7699
|
+
registerRelation(ctor, propertyName, metadataFactory(propertyName));
|
|
7700
|
+
};
|
|
7701
|
+
return decorator;
|
|
7702
|
+
};
|
|
7703
|
+
function HasMany(options) {
|
|
7704
|
+
return createFieldDecorator((propertyName) => ({
|
|
7705
|
+
kind: RelationKinds.HasMany,
|
|
7706
|
+
propertyKey: propertyName,
|
|
7707
|
+
target: options.target,
|
|
7708
|
+
foreignKey: options.foreignKey,
|
|
7709
|
+
localKey: options.localKey,
|
|
7710
|
+
cascade: options.cascade
|
|
7711
|
+
}));
|
|
7712
|
+
}
|
|
7713
|
+
function HasOne(options) {
|
|
7714
|
+
return createFieldDecorator((propertyName) => ({
|
|
7715
|
+
kind: RelationKinds.HasOne,
|
|
7716
|
+
propertyKey: propertyName,
|
|
7717
|
+
target: options.target,
|
|
7718
|
+
foreignKey: options.foreignKey,
|
|
7719
|
+
localKey: options.localKey,
|
|
7720
|
+
cascade: options.cascade
|
|
7721
|
+
}));
|
|
7722
|
+
}
|
|
7723
|
+
function BelongsTo(options) {
|
|
7724
|
+
return createFieldDecorator((propertyName) => ({
|
|
7725
|
+
kind: RelationKinds.BelongsTo,
|
|
7726
|
+
propertyKey: propertyName,
|
|
7727
|
+
target: options.target,
|
|
7728
|
+
foreignKey: options.foreignKey,
|
|
7729
|
+
localKey: options.localKey,
|
|
7730
|
+
cascade: options.cascade
|
|
7731
|
+
}));
|
|
7732
|
+
}
|
|
7733
|
+
function BelongsToMany(options) {
|
|
7734
|
+
return createFieldDecorator((propertyName) => ({
|
|
7735
|
+
kind: RelationKinds.BelongsToMany,
|
|
7736
|
+
propertyKey: propertyName,
|
|
7737
|
+
target: options.target,
|
|
7738
|
+
pivotTable: options.pivotTable,
|
|
7739
|
+
pivotForeignKeyToRoot: options.pivotForeignKeyToRoot,
|
|
7740
|
+
pivotForeignKeyToTarget: options.pivotForeignKeyToTarget,
|
|
7741
|
+
localKey: options.localKey,
|
|
7742
|
+
targetKey: options.targetKey,
|
|
7743
|
+
pivotPrimaryKey: options.pivotPrimaryKey,
|
|
7744
|
+
defaultPivotColumns: options.defaultPivotColumns,
|
|
7745
|
+
cascade: options.cascade
|
|
7746
|
+
}));
|
|
7747
|
+
}
|
|
7748
|
+
|
|
7474
7749
|
// src/core/execution/db-executor.ts
|
|
7475
7750
|
function rowsToQueryResult(rows) {
|
|
7476
7751
|
if (rows.length === 0) {
|
|
@@ -7643,17 +7918,24 @@ function createTediousExecutor(connection, module, options) {
|
|
|
7643
7918
|
}
|
|
7644
7919
|
export {
|
|
7645
7920
|
AsyncLocalStorage,
|
|
7921
|
+
BelongsTo,
|
|
7922
|
+
BelongsToMany,
|
|
7923
|
+
Column,
|
|
7646
7924
|
DefaultBelongsToReference,
|
|
7647
7925
|
DefaultHasManyCollection,
|
|
7648
7926
|
DefaultManyToManyCollection,
|
|
7649
7927
|
DeleteQueryBuilder,
|
|
7650
7928
|
DomainEventBus,
|
|
7929
|
+
Entity,
|
|
7651
7930
|
EntityStatus,
|
|
7931
|
+
HasMany,
|
|
7932
|
+
HasOne,
|
|
7652
7933
|
InsertQueryBuilder,
|
|
7653
7934
|
MySqlDialect,
|
|
7654
7935
|
Orm,
|
|
7655
7936
|
OrmSession,
|
|
7656
7937
|
PostgresDialect,
|
|
7938
|
+
PrimaryKey,
|
|
7657
7939
|
RelationKinds,
|
|
7658
7940
|
SelectQueryBuilder,
|
|
7659
7941
|
SqlServerDialect,
|
|
@@ -7672,6 +7954,7 @@ export {
|
|
|
7672
7954
|
belongsTo,
|
|
7673
7955
|
belongsToMany,
|
|
7674
7956
|
between,
|
|
7957
|
+
bootstrapEntities,
|
|
7675
7958
|
caseWhen,
|
|
7676
7959
|
ceil,
|
|
7677
7960
|
ceiling,
|
|
@@ -7726,6 +8009,7 @@ export {
|
|
|
7726
8009
|
generateCreateTableSql,
|
|
7727
8010
|
generateSchemaSql,
|
|
7728
8011
|
getSchemaIntrospector,
|
|
8012
|
+
getTableDefFromEntity,
|
|
7729
8013
|
groupConcat,
|
|
7730
8014
|
gt,
|
|
7731
8015
|
gte,
|
|
@@ -7798,6 +8082,7 @@ export {
|
|
|
7798
8082
|
rpad,
|
|
7799
8083
|
rtrim,
|
|
7800
8084
|
sel,
|
|
8085
|
+
selectFromEntity,
|
|
7801
8086
|
sign,
|
|
7802
8087
|
sin,
|
|
7803
8088
|
space,
|