`find` stripping off where options in node.js shell
See original GitHub issueIssue type:
[x] question [ ] bug report [ ] feature request [ ] documentation issue
Database system/driver:
[ ] cordova
[ ] mongodb
[ ] mssql
[ ] mysql
/ mariadb
[ ] oracle
[x] postgres
[ ] cockroachdb
[ ] sqlite
[ ] sqljs
[ ] react-native
[ ] expo
TypeORM version:
[ ] latest
[ ] @next
[x] ^0.2.20
Steps to reproduce or a small repository showing the problem:
Given the shell.js
file below, a database (defined by databaseUrl) and a simple model (e.g. Person) run the following:
node shell
> db.persons.findOne(123)
Promise { <pending> }
> query: SELECT "Person"."id" AS "Person_id", "Person"."name" AS "Person_name" FROM "main_person" "Person" WHERE "Person"."id" IN ($1) LIMIT 1 -- PARAMETERS: [123]
> db.persons.find({ id: 123 })
Promise { <pending> }
> query: SELECT "Person"."id" AS "Person_id", "Person"."name" AS "Person_name" FROM "main_person" "Person"
Look at the logged queries. Notice that the first findOne
example works properly, but in the second case Typeorm is stripping out any find
arguments (in this case { id: 123 }
).
Also note that the query
function also succeeds for some reason (e.g. > query(123)
).
// File: shell.js
// begin model Person.ts
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
Object.defineProperty(exports, "__esModule", { value: true });
var typeorm_1 = require("typeorm");
var Person = /** @class */ (function () {
function Person() {
}
__decorate([
typeorm_1.PrimaryColumn('bigint'),
__metadata("design:type", String)
], Person.prototype, "id", void 0);
__decorate([
typeorm_1.Column({ length: 50 }),
__metadata("design:type", String)
], Person.prototype, "name", void 0);
Person = __decorate([
typeorm_1.Entity('person')
], Person);
return Person;
}());
// end Person.ts
// begin shell
const repl = require('repl');
const { createConnection } = require('typeorm');
const m = {
Person
};
const databaseUrl = '';
const connect = () => createConnection({
type: 'postgres',
url: databaseUrl,
entities: Object.values(m),
synchronize: false,
logging: true,
extra: {
ssl: false,
},
});
const shell = repl.start('> ');
connect().then(db => {
db.persons = db.getRepository(m.Person);
shell.context.query = (id) => db.persons.find({ id });
shell.context.db = db;
shell.context.m = m;
});
I have no idea why this isn’t working… any help would be much appreciated!
Issue Analytics
- State:
- Created 4 years ago
- Reactions:4
- Comments:5 (1 by maintainers)
Hopes this helps someone in the future. I was having this same issue. Tracked it down to these lines in EntityManager.ts.
I’m very new to javascript/typescript so I’m not sure why this was happening but
idOrOptionsOrConditions instanceof Object
was returning false here in the repl. This wasn’t happening when running the server.I fixed it by setting
useGlobal: true
in my repl optionsNot sure why it is not working for you. I’m guessing, but could it be because of the pending promise? Then, consider using
--experimental-repl-await
.I’ll just post what is working for me since this issue is the only thing I could find about Typeorm repl.
package.json
src/chat/chat.entity.ts
repl.ts
I’m on
node@12.14.0
mysql2@2.1.0
typeorm@0.2.24
Hope this helps.