sqlmorpheus 0.1.1 → 0.1.2

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/index.d.mts CHANGED
@@ -98,9 +98,9 @@ interface DatabaseInstructions {
98
98
  postMigration?(fromVersion: number, toVersion: number): SqlChanges;
99
99
  }
100
100
 
101
- declare function DbTable<T extends BackendTable>(tableName: string, primaryKey?: keyof T): (table: Class<T>, context: any) => void;
101
+ declare function DbTable<T extends BackendTable>(tableName: string, primaryKey?: keyof T): (table: Class<T>, context?: any) => void;
102
102
 
103
- declare function ForeignKey<TOther extends BackendTable>(toTable: Class<TOther>, toColumn: keyof TOther, onDelete?: ForeignKeyActions, onUpdate?: ForeignKeyActions): (_: undefined, context: any) => void;
103
+ declare function ForeignKey<TOther extends BackendTable>(toTable: Class<TOther>, toColumn: keyof TOther, onDelete?: ForeignKeyActions, onUpdate?: ForeignKeyActions): (table: any, context: any) => void;
104
104
 
105
105
  declare function prepareMigration(db: DatabaseAccess, instructions: DatabaseInstructions, overwriteExisting?: boolean): Promise<void>;
106
106
  declare function runPreparedMigrations(db: DatabaseAccess, instructions: DatabaseInstructions): Promise<void>;
package/dist/index.mjs CHANGED
@@ -24,9 +24,12 @@ var Logger = new LoggerClass();
24
24
  // src/lib/tableInfo/TableInfo.ts
25
25
  var TABLE_INFO_PROPERTY_NAME = Symbol("tableInfo");
26
26
  function getTableInfo(table) {
27
- if (!table.hasOwnProperty(Symbol.metadata))
28
- table[Symbol.metadata] = {};
29
- return getTableInfoFromMetadata(table[Symbol.metadata]);
27
+ let metadata = table[Symbol.metadata] ?? table.prototype?.[Symbol.metadata];
28
+ if (!metadata) {
29
+ metadata = {};
30
+ table[Symbol.metadata] = metadata;
31
+ }
32
+ return getTableInfoFromMetadata(metadata);
30
33
  }
31
34
  function getTableInfoFromMetadata(metadata) {
32
35
  if (!metadata[TABLE_INFO_PROPERTY_NAME])
@@ -840,7 +843,7 @@ ${foreignKeyOnQuery}`;
840
843
  function DbTable(tableName, primaryKey) {
841
844
  return (table, context) => {
842
845
  Object.defineProperty(table, "name", { value: tableName, writable: false });
843
- const tableInfo = getTableInfoFromMetadata(context.metadata);
846
+ const tableInfo = context?.metadata ? getTableInfoFromMetadata(context.metadata) : getTableInfo(table);
844
847
  tableInfo.primaryKey = primaryKey?.toString();
845
848
  if (tableInfo.foreignKeys) {
846
849
  for (const foreignKey of tableInfo.foreignKeys) {
@@ -853,13 +856,14 @@ function DbTable(tableName, primaryKey) {
853
856
  // src/lib/tableInfo/decorators/ForeignKey.ts
854
857
  import "polyfill-symbol-metadata";
855
858
  function ForeignKey(toTable, toColumn, onDelete, onUpdate) {
856
- return (_, context) => {
857
- const metadata = getTableInfoFromMetadata(context.metadata);
859
+ return (table, context) => {
860
+ const metadata = context?.metadata ? getTableInfoFromMetadata(context.metadata) : getTableInfo(table);
858
861
  if (!metadata.foreignKeys)
859
862
  metadata.foreignKeys = [];
860
863
  metadata.foreignKeys.push({
861
864
  fromTable: "",
862
- fromColumn: context.name,
865
+ fromColumn: context.name ?? context,
866
+ //to stay compatible with old decorator structure (see https://www.typescriptlang.org/docs/handbook/release-notes/typescript-5-2.html#decorator-metadata)
863
867
  toTable: toTable.name,
864
868
  toColumn: toColumn.toString(),
865
869
  onDelete,
@@ -5,9 +5,13 @@ exports.getTableInfo = getTableInfo;
5
5
  exports.getTableInfoFromMetadata = getTableInfoFromMetadata;
6
6
  exports.TABLE_INFO_PROPERTY_NAME = Symbol("tableInfo");
7
7
  function getTableInfo(table) {
8
- if (!table.hasOwnProperty(Symbol.metadata))
9
- table[Symbol.metadata] = {};
10
- return getTableInfoFromMetadata(table[Symbol.metadata]);
8
+ //to stay compatible with old decorator structure (see https://www.typescriptlang.org/docs/handbook/release-notes/typescript-5-2.html#decorator-metadata):
9
+ let metadata = table[Symbol.metadata] ?? table.prototype?.[Symbol.metadata];
10
+ if (!metadata) {
11
+ metadata = {};
12
+ table[Symbol.metadata] = metadata;
13
+ }
14
+ return getTableInfoFromMetadata(metadata);
11
15
  }
12
16
  function getTableInfoFromMetadata(metadata) {
13
17
  if (!metadata[exports.TABLE_INFO_PROPERTY_NAME])
@@ -1,2 +1,2 @@
1
1
  import { BackendTable, Class } from "../../typings/BackendTable";
2
- export default function DbTable<T extends BackendTable>(tableName: string, primaryKey?: keyof T): (table: Class<T>, context: any) => void;
2
+ export default function DbTable<T extends BackendTable>(tableName: string, primaryKey?: keyof T): (table: Class<T>, context?: any) => void;
@@ -5,7 +5,7 @@ const TableInfo_1 = require("../TableInfo");
5
5
  function DbTable(tableName, primaryKey) {
6
6
  return (table, context) => {
7
7
  Object.defineProperty(table, "name", { value: tableName, writable: false });
8
- const tableInfo = (0, TableInfo_1.getTableInfoFromMetadata)(context.metadata);
8
+ const tableInfo = context?.metadata ? (0, TableInfo_1.getTableInfoFromMetadata)(context.metadata) : (0, TableInfo_1.getTableInfo)(table);
9
9
  tableInfo.primaryKey = primaryKey?.toString();
10
10
  if (tableInfo.foreignKeys) {
11
11
  for (const foreignKey of tableInfo.foreignKeys) {
@@ -1,4 +1,4 @@
1
1
  import { BackendTable, Class } from "../../typings/BackendTable";
2
2
  import "polyfill-symbol-metadata";
3
3
  import { ForeignKeyActions } from "../../typings/ForeignKeyInfo";
4
- export default function ForeignKey<TOther extends BackendTable>(toTable: Class<TOther>, toColumn: keyof TOther, onDelete?: ForeignKeyActions, onUpdate?: ForeignKeyActions): (_: undefined, context: any) => void;
4
+ export default function ForeignKey<TOther extends BackendTable>(toTable: Class<TOther>, toColumn: keyof TOther, onDelete?: ForeignKeyActions, onUpdate?: ForeignKeyActions): (table: any, context: any) => void;
@@ -4,13 +4,13 @@ exports.default = ForeignKey;
4
4
  const TableInfo_1 = require("../TableInfo");
5
5
  require("polyfill-symbol-metadata");
6
6
  function ForeignKey(toTable, toColumn, onDelete, onUpdate) {
7
- return (_, context) => {
8
- const metadata = (0, TableInfo_1.getTableInfoFromMetadata)(context.metadata);
7
+ return (table, context) => {
8
+ const metadata = context?.metadata ? (0, TableInfo_1.getTableInfoFromMetadata)(context.metadata) : (0, TableInfo_1.getTableInfo)(table);
9
9
  if (!metadata.foreignKeys)
10
10
  metadata.foreignKeys = [];
11
11
  metadata.foreignKeys.push({
12
12
  fromTable: "",
13
- fromColumn: context.name,
13
+ fromColumn: context.name ?? context, //to stay compatible with old decorator structure (see https://www.typescriptlang.org/docs/handbook/release-notes/typescript-5-2.html#decorator-metadata)
14
14
  toTable: toTable.name,
15
15
  toColumn: toColumn.toString(),
16
16
  onDelete: onDelete,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "sqlmorpheus",
3
- "version": "0.1.1",
3
+ "version": "0.1.2",
4
4
  "author": "JodliDev",
5
5
  "keywords": [],
6
6
  "license": "MIT",