Skip to content

Instantly share code, notes, and snippets.

@TPXP
Created August 16, 2021 15:16
Show Gist options
  • Save TPXP/ac29ad0fbf126b8470858ede6d8b6cdb to your computer and use it in GitHub Desktop.
Save TPXP/ac29ad0fbf126b8470858ede6d8b6cdb to your computer and use it in GitHub Desktop.
AdminJS Typeorm Active Record support
diff --git a/node_modules/@adminjs/typeorm/lib/Database.js b/node_modules/@adminjs/typeorm/lib/Database.js
index 01d0fcc..3ad852d 100644
--- a/node_modules/@adminjs/typeorm/lib/Database.js
+++ b/node_modules/@adminjs/typeorm/lib/Database.js
@@ -12,7 +12,9 @@ class Database extends adminjs_1.BaseDatabase {
const resources = [];
// eslint-disable-next-line no-restricted-syntax
for (const entityMetadata of this.connection.entityMetadatas) {
- resources.push(new Resource_1.Resource(entityMetadata.target));
+ resources.push(new Resource_1.Resource(
+ this.connection.getRepository(entityMetadata.target)
+ ));
}
return resources;
}
diff --git a/node_modules/@adminjs/typeorm/lib/Resource.js b/node_modules/@adminjs/typeorm/lib/Resource.js
index 5e36fc5..586c1ff 100644
--- a/node_modules/@adminjs/typeorm/lib/Resource.js
+++ b/node_modules/@adminjs/typeorm/lib/Resource.js
@@ -25,16 +25,16 @@ class Resource extends adminjs_1.BaseResource {
this.propsObject = this.prepareProps();
}
databaseName() {
- return this.model.getRepository().metadata.connection.options.database || 'typeorm';
+ return this.model.metadata.connection.options.database || 'typeorm';
}
databaseType() {
- return this.model.getRepository().metadata.connection.options.type || 'typeorm';
+ return this.model.metadata.connection.options.type || 'typeorm';
}
name() {
- return this.model.name;
+ return this.model.metadata.name;
}
id() {
- return this.model.name;
+ return this.model.metadata.name;
}
properties() {
return [...Object.values(this.propsObject)];
@@ -117,7 +117,7 @@ class Resource extends adminjs_1.BaseResource {
});
}
prepareProps() {
- const { columns } = this.model.getRepository().metadata;
+ const { columns } = this.model.metadata;
return columns.reduce((memo, col, index) => {
const property = new Property_1.Property(col, index);
return Object.assign(Object.assign({}, memo), { [property.path()]: property });
@@ -190,7 +190,7 @@ class Resource extends adminjs_1.BaseResource {
}
static isAdapterFor(rawResource) {
try {
- return !!rawResource.getRepository().metadata;
+ return !!rawResource.metadata;
}
catch (e) {
return false;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment