@tailor-platform/function-typeorm-tailordb
v0.1.1
Published
TypeORM helpers for TailorDB
Readme
@tailor-platform/function-typeorm-tailordb
TypeORM integration helpers for TailorDB.
This package provides minimal components to execute SQL and manage transactions against TailorDB with a Postgres-like shape, intended to be used alongside TypeORM. It exposes a lightweight query runner and an experimental DataSource integration.
Important: Schema sync and migrations are not supported. TailorDB is the source of truth for schema. Generate code from the live schema using the companion codegen package.
Note: TypeORM does not offer a public, stable plugin API for custom drivers. The DataSource integration below is experimental and aims to support basic CRUD through Repository/QueryBuilder. Complex features are out of scope.
Install
# npm
npm install @tailor-platform/function-typeorm-tailordb
# pnpm
pnpm add @tailor-platform/function-typeorm-tailordbWe recommend installing @tailor-platform/function-types for TailorDB typings when working in the Function service.
Using @tailor-platform/function-types
@tailor-platform/function-types provides ambient type declarations for Tailor Platform globals such as tailordb, tailor.secretmanager, etc.
- Install as a dev dependency
# npm
npm install -D @tailor-platform/function-types
# pnpm
pnpm add -D @tailor-platform/function-types- Add to your
tsconfig.jsonso TypeScript picks up the globals (recommended config)
{
"extends": "@tsconfig/recommended/tsconfig.json",
"compilerOptions": {
"target": "ESNext",
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"sourceMap": true,
"types": ["@tailor-platform/function-types"]
}
}After this, you can use the global tailordb.Client with proper types without importing it:
const client = new tailordb.Client({ namespace: "<namespace>" });
await client.connect();
const result = await client.queryObject<{ now: string }>("select now() as now");Minimal SQL usage
import { TailordbClientQueryRunner } from '@tailor-platform/function-typeorm-tailordb';
const client = new tailordb.Client({ namespace: '<tailordb namespace>' });
await client.connect();
const qr = new TailordbClientQueryRunner(client);
// Execute raw SQL
await qr.query('select 1');
// Transactions
await qr.startTransaction();
try {
await qr.query('insert into foo(bar) values($1)', ['baz']);
await qr.commitTransaction();
} catch (e) {
await qr.rollbackTransaction();
throw e;
}
await client.end();This query runner is intentionally minimal and designed for raw queries or integration scenarios where you manage SQL directly.
Experimental: TypeORM DataSource integration (Tailor Platform Function)
This package includes an experimental integration intended for the Tailor Platform Function runtime (which does not provide Node built‑ins). It patches TypeORM’s browser DriverFactory at runtime and returns a DataSource configured for TailorDB. Advanced features are not supported.
Recommended usage:
import { createDatasource } from '@tailor-platform/function-typeorm-tailordb';
import { App_Users } from './entities'; // generated by codegen
const ds = createDatasource('<tailordb namespace>', [App_Users]);
await ds.initialize();
// Minimal query
const rows = await ds.query('select 1 as one');
// Transaction
const qr = ds.createQueryRunner();
await qr.startTransaction();
try {
await qr.query('insert into app.sample(col) values($1)', ['val']);
await qr.commitTransaction();
} catch (e) {
await qr.rollbackTransaction();
throw e;
} finally {
await qr.release();
}
await ds.destroy();Repository/QueryBuilder basic CRUD (limited)
Register generated entities in the DataSource to use simple CRUD. Complex queries or relations are not guaranteed.
import { createDatasource } from '@tailor-platform/function-typeorm-tailordb';
import { App_Users } from './entities';
const ds = createDatasource('<namespace>', [App_Users]);
await ds.initialize();
const repo = ds.getRepository(App_Users);
// Create
const created = await repo.save({ name: 'Alice', email: '[email protected]' });
// Read
const found = await repo.findOne({ where: { id: created.id } });
// Update
await repo.update({ id: created.id }, { name: 'Alice Updated' });
// Delete
await repo.delete({ id: created.id });
await ds.destroy();Limitations (prototype)
- No schema sync or migrations. Use the codegen package to keep entities in sync with TailorDB.
- Focused on basic CRUD via Repository/QueryBuilder. Advanced relations, cascades, partial update return shapes, etc., are not guaranteed.
- Parameters: Postgres-style positional parameters are supported. Named parameters like
:nameare minimally converted to$n.
