-
I'm trying to make TypeORM work with SvelteKit and following the Getting Started page of their documentation (https://typeorm.io/) but encontered some difficulties to make it work: the js anotation (@entity) added with the library first error:
second error (when explicitely specifying a type):
What I tried:
There is already a discussion but with a beta version and thought it would be better to open a fresh one: #3334 Anyone figured out how to make it work or willing to help me? :) Thanks ;) |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 6 replies
-
First of all ensure you have emitDecoratorMetadata set in "compilerOptions": {
...
// needed for typeORM
"emitDecoratorMetadata": true,
"experimentalDecorators": true
},` And include reflect-metadata in export default defineConfig({
...
ssr: {
external: ['reflect-metadata'],
},
}); Despite all this effort with metadata reflection, there have been previous bugs with it, so ensure that every column in your entities has a labelled @Entity({ name: 'test' })
export class Test extends BaseEntity {
...
@Column({ type: 'varchar'})
name!: string | null;
} I personally have had some issues with import { Test } from './entities/Test';
class TypeOrm {
private static instance: Promise<DataSource> | null = null;
private constructor() {
// Private constructor to prevent external instantiation
}
public static getDb(): Promise<DataSource> {
if (!TypeOrm.instance) {
TypeOrm.instance = new DataSource({
type: 'postgres',
host: 'localhost',
port: 5432,
username: 'svelte', // make these .env
password: 'sveltepassword',
database: 'sveltetypeorm',
synchronize: true,
entities: [Test],
migrations: [],
subscribers: [],
logging: true,
})
.initialize()
.then((fulfilled) => {
console.info('Data Source has been initialized!');
return fulfilled;
})
.catch((err) => {
console.error('Error during Data Source initialization', err);
return null;
});
}
return TypeOrm.instance;
}
}
export default TypeOrm; Then, at the top level of import 'reflect-metadata';
...
await TypeOrm.getDb(); This appears to fire only once on startup, and rarely when hot reloading. In my server load functions I then use the active record pattern: export const load: PageLoad = async () => {
const test = new Test();
test.name = 'test name';
await test.save();
const tests = await Test.find();
const json = JSON.stringify(tests);
return { json };
}; To use either entity manager or repository, bring in the singleton: ...
const db = await TypeOrm.getDb();
const test = new Test();
test.name = 'test name';
db.manager.save(test) However I'll note that the repository pattern doesn't appear to be creating new entities for me, but haven't put much effort into fixing it. |
Beta Was this translation helpful? Give feedback.
First of all ensure you have emitDecoratorMetadata set in
tsconfig.json
as per the docs:And include reflect-metadata in
vite.config.js
as you mentioned:Despite all this effort with metadata reflection, there have been previous bugs with it, so ensure that every column in your entities has a labelled
type
:I personally have had some issues w…