Skip to content

Instantly share code, notes, and snippets.

@maksimr
Last active June 24, 2023 16:01
Show Gist options
  • Save maksimr/6d17b861a53a7cf3e7415892dcaa5ca2 to your computer and use it in GitHub Desktop.
Save maksimr/6d17b861a53a7cf3e7415892dcaa5ca2 to your computer and use it in GitHub Desktop.
typeorm
async function main() {
const pg = require('pg');
const client = new pg.Client({
user: 'admin',
password: 'admin',
database: 'test',
host: '127.0.0.1',
port: 5432
});
await client.connect();
await client.query(`
DROP TABLE IF EXISTS foo;
CREATE TABLE foo (
id serial primary key,
vars jsonb NOT NULL DEFAULT '{}'::jsonb
);
`);
const typeorm = require('typeorm');
const FooEntity = new typeorm.EntitySchema({
name: 'Foo',
database: 'foo',
columns: {
id: { primary: true, type: 'int', generated: true },
vars: { type: 'jsonb' }
}
});
const ds = new typeorm.DataSource({
type: 'postgres',
username: 'admin',
host: '127.0.0.1',
password: 'admin',
database: 'test',
entities: [FooEntity]
});
await ds.initialize();
await ds.getRepository(FooEntity)
.createQueryBuilder()
.insert()
.values({ vars: { foo: 'foo' } })
.execute();
const x1 = await ds.manager.find(FooEntity)
console.log(x1);
console.log(
await ds.getRepository(FooEntity)
.createQueryBuilder()
.update()
.where({ id: x1[0].id })
.set({
vars: () => `vars || :vars::jsonb`
})
.setParameters({ vars: { bar: 'bar' } })
.execute()
);
const x2 = await ds.manager.find(FooEntity)
console.log(x2);
await client.end()
await ds.destroy();
console.log('Ok');
}
main();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment