@stonyx/orm 0.2.5-alpha.0 → 0.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +482 -15
- package/config/environment.js +63 -6
- package/dist/aggregates.d.ts +21 -0
- package/dist/aggregates.js +93 -0
- package/dist/attr.d.ts +2 -0
- package/dist/attr.js +22 -0
- package/dist/belongs-to.d.ts +11 -0
- package/dist/belongs-to.js +59 -0
- package/dist/cli.d.ts +22 -0
- package/dist/cli.js +148 -0
- package/dist/commands.d.ts +7 -0
- package/dist/commands.js +146 -0
- package/dist/db.d.ts +21 -0
- package/dist/db.js +180 -0
- package/dist/exports/db.d.ts +7 -0
- package/{src → dist}/exports/db.js +2 -4
- package/dist/has-many.d.ts +11 -0
- package/dist/has-many.js +58 -0
- package/dist/hooks.d.ts +75 -0
- package/dist/hooks.js +110 -0
- package/dist/index.d.ts +14 -0
- package/dist/index.js +34 -0
- package/dist/main.d.ts +46 -0
- package/dist/main.js +181 -0
- package/dist/manage-record.d.ts +13 -0
- package/dist/manage-record.js +123 -0
- package/dist/meta-request.d.ts +6 -0
- package/dist/meta-request.js +52 -0
- package/dist/migrate.d.ts +2 -0
- package/dist/migrate.js +57 -0
- package/dist/model-property.d.ts +9 -0
- package/dist/model-property.js +29 -0
- package/dist/model.d.ts +15 -0
- package/dist/model.js +18 -0
- package/dist/mysql/connection.d.ts +14 -0
- package/dist/mysql/connection.js +24 -0
- package/dist/mysql/migration-generator.d.ts +45 -0
- package/dist/mysql/migration-generator.js +254 -0
- package/dist/mysql/migration-runner.d.ts +12 -0
- package/dist/mysql/migration-runner.js +88 -0
- package/dist/mysql/mysql-db.d.ts +100 -0
- package/dist/mysql/mysql-db.js +425 -0
- package/dist/mysql/query-builder.d.ts +10 -0
- package/dist/mysql/query-builder.js +44 -0
- package/dist/mysql/schema-introspector.d.ts +19 -0
- package/dist/mysql/schema-introspector.js +257 -0
- package/dist/mysql/type-map.d.ts +21 -0
- package/dist/mysql/type-map.js +36 -0
- package/dist/orm-request.d.ts +38 -0
- package/dist/orm-request.js +475 -0
- package/dist/plural-registry.d.ts +4 -0
- package/dist/plural-registry.js +9 -0
- package/dist/postgres/connection.d.ts +15 -0
- package/dist/postgres/connection.js +32 -0
- package/dist/postgres/migration-generator.d.ts +45 -0
- package/dist/postgres/migration-generator.js +280 -0
- package/dist/postgres/migration-runner.d.ts +10 -0
- package/dist/postgres/migration-runner.js +87 -0
- package/dist/postgres/postgres-db.d.ts +119 -0
- package/dist/postgres/postgres-db.js +477 -0
- package/dist/postgres/query-builder.d.ts +27 -0
- package/dist/postgres/query-builder.js +98 -0
- package/dist/postgres/schema-introspector.d.ts +29 -0
- package/dist/postgres/schema-introspector.js +296 -0
- package/dist/postgres/type-map.d.ts +23 -0
- package/dist/postgres/type-map.js +56 -0
- package/dist/record.d.ts +75 -0
- package/dist/record.js +129 -0
- package/dist/relationships.d.ts +10 -0
- package/dist/relationships.js +41 -0
- package/dist/schema-helpers.d.ts +20 -0
- package/dist/schema-helpers.js +48 -0
- package/dist/serializer.d.ts +17 -0
- package/dist/serializer.js +136 -0
- package/dist/setup-rest-server.d.ts +1 -0
- package/dist/setup-rest-server.js +52 -0
- package/dist/standalone-db.d.ts +58 -0
- package/dist/standalone-db.js +142 -0
- package/dist/store.d.ts +62 -0
- package/dist/store.js +286 -0
- package/dist/timescale/query-builder.d.ts +43 -0
- package/dist/timescale/query-builder.js +115 -0
- package/dist/timescale/timescale-db.d.ts +45 -0
- package/dist/timescale/timescale-db.js +84 -0
- package/dist/transforms.d.ts +2 -0
- package/dist/transforms.js +17 -0
- package/dist/types/orm-types.d.ts +153 -0
- package/dist/types/orm-types.js +1 -0
- package/dist/utils.d.ts +7 -0
- package/dist/utils.js +17 -0
- package/dist/view-resolver.d.ts +8 -0
- package/dist/view-resolver.js +171 -0
- package/dist/view.d.ts +11 -0
- package/dist/view.js +18 -0
- package/package.json +64 -11
- package/src/aggregates.ts +109 -0
- package/src/{attr.js → attr.ts} +2 -2
- package/src/belongs-to.ts +90 -0
- package/src/cli.ts +183 -0
- package/src/commands.ts +179 -0
- package/src/db.ts +232 -0
- package/src/exports/db.ts +7 -0
- package/src/has-many.ts +92 -0
- package/src/hooks.ts +151 -0
- package/src/{index.js → index.ts} +12 -2
- package/src/main.ts +229 -0
- package/src/manage-record.ts +161 -0
- package/src/{meta-request.js → meta-request.ts} +17 -14
- package/src/migrate.ts +72 -0
- package/src/model-property.ts +35 -0
- package/src/model.ts +21 -0
- package/src/mysql/connection.ts +43 -0
- package/src/mysql/migration-generator.ts +337 -0
- package/src/mysql/migration-runner.ts +121 -0
- package/src/mysql/mysql-db.ts +543 -0
- package/src/mysql/query-builder.ts +69 -0
- package/src/mysql/schema-introspector.ts +310 -0
- package/src/mysql/type-map.ts +42 -0
- package/src/orm-request.ts +582 -0
- package/src/plural-registry.ts +12 -0
- package/src/postgres/connection.ts +48 -0
- package/src/postgres/migration-generator.ts +370 -0
- package/src/postgres/migration-runner.ts +115 -0
- package/src/postgres/postgres-db.ts +616 -0
- package/src/postgres/query-builder.ts +148 -0
- package/src/postgres/schema-introspector.ts +360 -0
- package/src/postgres/type-map.ts +61 -0
- package/src/record.ts +186 -0
- package/src/relationships.ts +54 -0
- package/src/schema-helpers.ts +59 -0
- package/src/serializer.ts +161 -0
- package/src/setup-rest-server.ts +62 -0
- package/src/standalone-db.ts +185 -0
- package/src/store.ts +373 -0
- package/src/timescale/query-builder.ts +174 -0
- package/src/timescale/timescale-db.ts +119 -0
- package/src/transforms.ts +20 -0
- package/src/types/mysql2.d.ts +49 -0
- package/src/types/orm-types.ts +158 -0
- package/src/types/pg.d.ts +32 -0
- package/src/types/stonyx-cron.d.ts +5 -0
- package/src/types/stonyx-events.d.ts +4 -0
- package/src/types/stonyx-rest-server.d.ts +16 -0
- package/src/types/stonyx-utils.d.ts +33 -0
- package/src/types/stonyx.d.ts +21 -0
- package/src/utils.ts +22 -0
- package/src/view-resolver.ts +211 -0
- package/src/view.ts +22 -0
- package/.claude/project-structure.md +0 -578
- package/.github/workflows/ci.yml +0 -36
- package/.github/workflows/publish.yml +0 -143
- package/src/belongs-to.js +0 -63
- package/src/db.js +0 -80
- package/src/has-many.js +0 -61
- package/src/main.js +0 -119
- package/src/manage-record.js +0 -103
- package/src/model-property.js +0 -29
- package/src/model.js +0 -9
- package/src/orm-request.js +0 -249
- package/src/record.js +0 -100
- package/src/relationships.js +0 -43
- package/src/serializer.js +0 -138
- package/src/setup-rest-server.js +0 -57
- package/src/store.js +0 -211
- package/src/transforms.js +0 -20
- package/stonyx-bootstrap.cjs +0 -30
|
@@ -0,0 +1,477 @@
|
|
|
1
|
+
import { getPool, closePool } from './connection.js';
|
|
2
|
+
import { ensureMigrationsTable, getAppliedMigrations, getMigrationFiles, applyMigration, parseMigrationFile } from './migration-runner.js';
|
|
3
|
+
import { introspectModels, introspectViews, getTopologicalOrder, schemasToSnapshot } from './schema-introspector.js';
|
|
4
|
+
import { loadLatestSnapshot, detectSchemaDrift } from './migration-generator.js';
|
|
5
|
+
import { buildInsert, buildUpdate, buildDelete, buildSelect, buildVectorSearch, buildHybridSearch } from './query-builder.js';
|
|
6
|
+
import { store } from '@stonyx/orm';
|
|
7
|
+
import { createRecord } from '../manage-record.js';
|
|
8
|
+
import { confirm } from '@stonyx/utils/prompt';
|
|
9
|
+
import { readFile } from '@stonyx/utils/file';
|
|
10
|
+
import { getPluralName } from '../plural-registry.js';
|
|
11
|
+
import { isDbError } from '../utils.js';
|
|
12
|
+
import config from 'stonyx/config';
|
|
13
|
+
import log from 'stonyx/log';
|
|
14
|
+
import path from 'path';
|
|
15
|
+
const defaultDeps = {
|
|
16
|
+
getPool, closePool, ensureMigrationsTable, getAppliedMigrations,
|
|
17
|
+
getMigrationFiles, applyMigration, parseMigrationFile,
|
|
18
|
+
introspectModels, introspectViews, getTopologicalOrder, schemasToSnapshot,
|
|
19
|
+
loadLatestSnapshot, detectSchemaDrift,
|
|
20
|
+
buildInsert, buildUpdate, buildDelete, buildSelect, buildVectorSearch, buildHybridSearch,
|
|
21
|
+
createRecord, store, confirm, readFile, getPluralName, config, log, path
|
|
22
|
+
};
|
|
23
|
+
export default class PostgresDB {
|
|
24
|
+
/** PostgreSQL extensions to enable on pool init. Subclasses can override. */
|
|
25
|
+
static extensions = ['vector'];
|
|
26
|
+
/** Config key under config.orm for this adapter. Subclasses can override. */
|
|
27
|
+
static configKey = 'postgres';
|
|
28
|
+
/** Singleton instance, set by subclass constructor name. */
|
|
29
|
+
static instance;
|
|
30
|
+
deps;
|
|
31
|
+
pool;
|
|
32
|
+
pgConfig;
|
|
33
|
+
constructor(deps = {}) {
|
|
34
|
+
const Ctor = this.constructor;
|
|
35
|
+
if (Ctor.instance)
|
|
36
|
+
return Ctor.instance;
|
|
37
|
+
Ctor.instance = this;
|
|
38
|
+
this.deps = { ...defaultDeps, ...deps };
|
|
39
|
+
this.pool = null;
|
|
40
|
+
this.pgConfig = this.deps.config.orm[Ctor.configKey];
|
|
41
|
+
}
|
|
42
|
+
requirePool() {
|
|
43
|
+
if (!this.pool)
|
|
44
|
+
throw new Error('PostgresDB pool not initialized — call init() first');
|
|
45
|
+
return this.pool;
|
|
46
|
+
}
|
|
47
|
+
async init() {
|
|
48
|
+
this.pool = await this.deps.getPool(this.pgConfig, this.constructor.extensions);
|
|
49
|
+
await this.deps.ensureMigrationsTable(this.pool, this.pgConfig.migrationsTable);
|
|
50
|
+
await this.loadMemoryRecords();
|
|
51
|
+
}
|
|
52
|
+
async startup() {
|
|
53
|
+
const migrationsPath = this.deps.path.resolve(this.deps.config.rootPath, this.pgConfig.migrationsDir);
|
|
54
|
+
// Check for pending migrations
|
|
55
|
+
const applied = await this.deps.getAppliedMigrations(this.requirePool(), this.pgConfig.migrationsTable);
|
|
56
|
+
const files = await this.deps.getMigrationFiles(migrationsPath);
|
|
57
|
+
const pending = files.filter(f => !applied.includes(f));
|
|
58
|
+
if (pending.length > 0) {
|
|
59
|
+
this.deps.log.db?.(`${pending.length} pending migration(s) found.`);
|
|
60
|
+
const shouldApply = await this.deps.confirm(`${pending.length} pending migration(s) found. Apply now?`);
|
|
61
|
+
if (shouldApply) {
|
|
62
|
+
for (const filename of pending) {
|
|
63
|
+
const content = await this.deps.readFile(this.deps.path.join(migrationsPath, filename));
|
|
64
|
+
const { up } = this.deps.parseMigrationFile(content);
|
|
65
|
+
await this.deps.applyMigration(this.requirePool(), filename, up, this.pgConfig.migrationsTable);
|
|
66
|
+
this.deps.log.db?.(`Applied migration: ${filename}`);
|
|
67
|
+
}
|
|
68
|
+
// Reload records after applying migrations
|
|
69
|
+
await this.loadMemoryRecords();
|
|
70
|
+
}
|
|
71
|
+
else {
|
|
72
|
+
this.deps.log.warn?.('Skipping pending migrations. Schema may be outdated.');
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
else if (files.length === 0) {
|
|
76
|
+
const schemas = this.deps.introspectModels();
|
|
77
|
+
const modelCount = Object.keys(schemas).length;
|
|
78
|
+
if (modelCount > 0) {
|
|
79
|
+
const shouldGenerate = await this.deps.confirm(`No migrations found but ${modelCount} model(s) detected. Generate and apply initial migration?`);
|
|
80
|
+
if (shouldGenerate) {
|
|
81
|
+
const { generateMigration } = await import('./migration-generator.js');
|
|
82
|
+
const result = await generateMigration('initial_setup');
|
|
83
|
+
if (result) {
|
|
84
|
+
const { up } = this.deps.parseMigrationFile(result.content);
|
|
85
|
+
await this.deps.applyMigration(this.requirePool(), result.filename, up, this.pgConfig.migrationsTable);
|
|
86
|
+
this.deps.log.db?.(`Applied migration: ${result.filename}`);
|
|
87
|
+
await this.loadMemoryRecords();
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
else {
|
|
91
|
+
this.deps.log.warn?.('Skipping initial migration. Tables may not exist.');
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
// Check for schema drift
|
|
96
|
+
const schemas = this.deps.introspectModels();
|
|
97
|
+
const snapshot = await this.deps.loadLatestSnapshot(this.deps.path.resolve(this.deps.config.rootPath, this.pgConfig.migrationsDir));
|
|
98
|
+
if (Object.keys(snapshot).length > 0) {
|
|
99
|
+
const drift = this.deps.detectSchemaDrift(schemas, snapshot);
|
|
100
|
+
if (drift.hasChanges) {
|
|
101
|
+
this.deps.log.warn?.('Schema drift detected: models have changed since the last migration.');
|
|
102
|
+
this.deps.log.warn?.('Run `stonyx db:generate-migration` to create a new migration.');
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
async shutdown() {
|
|
107
|
+
await this.deps.closePool();
|
|
108
|
+
this.pool = null;
|
|
109
|
+
}
|
|
110
|
+
async save() {
|
|
111
|
+
// No-op: PostgreSQL persists data immediately via persist()
|
|
112
|
+
}
|
|
113
|
+
/**
|
|
114
|
+
* Loads only models with memory: true into the in-memory store on startup.
|
|
115
|
+
* Models with memory: false are skipped -- accessed on-demand via find()/findAll().
|
|
116
|
+
*/
|
|
117
|
+
async loadMemoryRecords() {
|
|
118
|
+
const schemas = this.deps.introspectModels();
|
|
119
|
+
const order = this.deps.getTopologicalOrder(schemas);
|
|
120
|
+
const Orm = (await import('@stonyx/orm')).default;
|
|
121
|
+
for (const modelName of order) {
|
|
122
|
+
const { modelClass } = Orm.instance.getRecordClasses(modelName);
|
|
123
|
+
if (modelClass?.memory === false) {
|
|
124
|
+
this.deps.log.db?.(`Skipping memory load for '${modelName}' (memory: false)`);
|
|
125
|
+
continue;
|
|
126
|
+
}
|
|
127
|
+
const schema = schemas[modelName];
|
|
128
|
+
const { sql, values } = this.deps.buildSelect(schema.table);
|
|
129
|
+
try {
|
|
130
|
+
const result = await this.requirePool().query(sql, values);
|
|
131
|
+
for (const row of result.rows) {
|
|
132
|
+
const rawData = this._rowToRawData(row, schema);
|
|
133
|
+
this.deps.createRecord(modelName, rawData, { isDbRecord: true, serialize: false, transform: false });
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
catch (error) {
|
|
137
|
+
// 42P01 = undefined_table (PG equivalent of ER_NO_SUCH_TABLE)
|
|
138
|
+
if (isDbError(error) && error.code === '42P01') {
|
|
139
|
+
this.deps.log.db?.(`Table '${schema.table}' does not exist yet. Skipping load for '${modelName}'.`);
|
|
140
|
+
continue;
|
|
141
|
+
}
|
|
142
|
+
throw error;
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
// Load views with memory: true
|
|
146
|
+
const viewSchemas = this.deps.introspectViews();
|
|
147
|
+
for (const [viewName, viewSchema] of Object.entries(viewSchemas)) {
|
|
148
|
+
const { modelClass: viewClass } = Orm.instance.getRecordClasses(viewName);
|
|
149
|
+
if (viewClass?.memory !== true) {
|
|
150
|
+
this.deps.log.db?.(`Skipping memory load for view '${viewName}' (memory: false)`);
|
|
151
|
+
continue;
|
|
152
|
+
}
|
|
153
|
+
const sourceIdType = schemas[viewSchema.source]?.idType || 'number';
|
|
154
|
+
const schema = {
|
|
155
|
+
table: viewSchema.viewName,
|
|
156
|
+
idType: sourceIdType,
|
|
157
|
+
columns: viewSchema.columns || {},
|
|
158
|
+
foreignKeys: (viewSchema.foreignKeys || {}),
|
|
159
|
+
relationships: { belongsTo: {}, hasMany: {} },
|
|
160
|
+
vectorColumns: {},
|
|
161
|
+
memory: false,
|
|
162
|
+
};
|
|
163
|
+
const { sql, values } = this.deps.buildSelect(schema.table);
|
|
164
|
+
try {
|
|
165
|
+
const result = await this.requirePool().query(sql, values);
|
|
166
|
+
for (const row of result.rows) {
|
|
167
|
+
const rawData = this._rowToRawData(row, schema);
|
|
168
|
+
this.deps.createRecord(viewName, rawData, { isDbRecord: true, serialize: false, transform: false });
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
catch (error) {
|
|
172
|
+
if (isDbError(error) && error.code === '42P01') {
|
|
173
|
+
this.deps.log.db?.(`View '${viewSchema.viewName}' does not exist yet. Skipping load for '${viewName}'.`);
|
|
174
|
+
continue;
|
|
175
|
+
}
|
|
176
|
+
throw error;
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
/**
|
|
181
|
+
* @deprecated Use loadMemoryRecords() instead. Kept for backward compatibility.
|
|
182
|
+
*/
|
|
183
|
+
async loadAllRecords() {
|
|
184
|
+
return this.loadMemoryRecords();
|
|
185
|
+
}
|
|
186
|
+
/**
|
|
187
|
+
* Find a single record by ID from PostgreSQL.
|
|
188
|
+
* Does NOT cache the result in the store for memory: false models.
|
|
189
|
+
*/
|
|
190
|
+
async findRecord(modelName, id) {
|
|
191
|
+
const schemas = this.deps.introspectModels();
|
|
192
|
+
let schema = schemas[modelName];
|
|
193
|
+
// Check views if not found in models
|
|
194
|
+
if (!schema) {
|
|
195
|
+
const viewSchemas = this.deps.introspectViews();
|
|
196
|
+
const viewSchema = viewSchemas[modelName];
|
|
197
|
+
if (viewSchema) {
|
|
198
|
+
const sourceIdType = schemas[viewSchema.source]?.idType || 'number';
|
|
199
|
+
schema = {
|
|
200
|
+
table: viewSchema.viewName,
|
|
201
|
+
idType: sourceIdType,
|
|
202
|
+
columns: viewSchema.columns || {},
|
|
203
|
+
foreignKeys: (viewSchema.foreignKeys || {}),
|
|
204
|
+
relationships: { belongsTo: {}, hasMany: {} },
|
|
205
|
+
vectorColumns: {},
|
|
206
|
+
memory: false,
|
|
207
|
+
};
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
if (!schema)
|
|
211
|
+
return undefined;
|
|
212
|
+
const { sql, values } = this.deps.buildSelect(schema.table, { id });
|
|
213
|
+
try {
|
|
214
|
+
const result = await this.requirePool().query(sql, values);
|
|
215
|
+
if (result.rows.length === 0)
|
|
216
|
+
return undefined;
|
|
217
|
+
const rawData = this._rowToRawData(result.rows[0], schema);
|
|
218
|
+
const record = this.deps.createRecord(modelName, rawData, { isDbRecord: true, serialize: false, transform: false });
|
|
219
|
+
this._evictIfNotMemory(modelName, record);
|
|
220
|
+
return record;
|
|
221
|
+
}
|
|
222
|
+
catch (error) {
|
|
223
|
+
if (isDbError(error) && error.code === '42P01')
|
|
224
|
+
return undefined;
|
|
225
|
+
throw error;
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
/**
|
|
229
|
+
* Find all records of a model from PostgreSQL, with optional conditions.
|
|
230
|
+
*/
|
|
231
|
+
async findAll(modelName, conditions) {
|
|
232
|
+
const schemas = this.deps.introspectModels();
|
|
233
|
+
let schema = schemas[modelName];
|
|
234
|
+
// Check views if not found in models
|
|
235
|
+
if (!schema) {
|
|
236
|
+
const viewSchemas = this.deps.introspectViews();
|
|
237
|
+
const viewSchema = viewSchemas[modelName];
|
|
238
|
+
if (viewSchema) {
|
|
239
|
+
const sourceIdType = schemas[viewSchema.source]?.idType || 'number';
|
|
240
|
+
schema = {
|
|
241
|
+
table: viewSchema.viewName,
|
|
242
|
+
idType: sourceIdType,
|
|
243
|
+
columns: viewSchema.columns || {},
|
|
244
|
+
foreignKeys: (viewSchema.foreignKeys || {}),
|
|
245
|
+
relationships: { belongsTo: {}, hasMany: {} },
|
|
246
|
+
vectorColumns: {},
|
|
247
|
+
memory: false,
|
|
248
|
+
};
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
if (!schema)
|
|
252
|
+
return [];
|
|
253
|
+
const resolvedSchema = schema;
|
|
254
|
+
const { sql, values } = this.deps.buildSelect(resolvedSchema.table, conditions);
|
|
255
|
+
try {
|
|
256
|
+
const result = await this.requirePool().query(sql, values);
|
|
257
|
+
const records = result.rows.map(row => {
|
|
258
|
+
const rawData = this._rowToRawData(row, resolvedSchema);
|
|
259
|
+
return this.deps.createRecord(modelName, rawData, { isDbRecord: true, serialize: false, transform: false });
|
|
260
|
+
});
|
|
261
|
+
for (const record of records) {
|
|
262
|
+
this._evictIfNotMemory(modelName, record);
|
|
263
|
+
}
|
|
264
|
+
return records;
|
|
265
|
+
}
|
|
266
|
+
catch (error) {
|
|
267
|
+
if (isDbError(error) && error.code === '42P01')
|
|
268
|
+
return [];
|
|
269
|
+
throw error;
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
/**
|
|
273
|
+
* Perform a vector similarity search using cosine distance.
|
|
274
|
+
*/
|
|
275
|
+
async vectorSearch(modelName, vectorColumn, queryVector, options = {}) {
|
|
276
|
+
const schemas = this.deps.introspectModels();
|
|
277
|
+
const schema = schemas[modelName];
|
|
278
|
+
if (!schema)
|
|
279
|
+
return [];
|
|
280
|
+
const { sql, values } = this.deps.buildVectorSearch(schema.table, vectorColumn, queryVector, options);
|
|
281
|
+
try {
|
|
282
|
+
const result = await this.requirePool().query(sql, values);
|
|
283
|
+
return result.rows.map(row => {
|
|
284
|
+
const distance = row.distance;
|
|
285
|
+
delete row.distance;
|
|
286
|
+
const rawData = this._rowToRawData(row, schema);
|
|
287
|
+
const record = this.deps.createRecord(modelName, rawData, { isDbRecord: true, serialize: false, transform: false });
|
|
288
|
+
this._evictIfNotMemory(modelName, record);
|
|
289
|
+
return { record, distance };
|
|
290
|
+
});
|
|
291
|
+
}
|
|
292
|
+
catch (error) {
|
|
293
|
+
if (isDbError(error) && error.code === '42P01')
|
|
294
|
+
return [];
|
|
295
|
+
throw error;
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
/**
|
|
299
|
+
* Perform a hybrid search combining vector similarity with text filtering.
|
|
300
|
+
*/
|
|
301
|
+
async hybridSearch(modelName, vectorColumn, queryVector, textColumn, textQuery, options = {}) {
|
|
302
|
+
const schemas = this.deps.introspectModels();
|
|
303
|
+
const schema = schemas[modelName];
|
|
304
|
+
if (!schema)
|
|
305
|
+
return [];
|
|
306
|
+
const { sql, values } = this.deps.buildHybridSearch(schema.table, vectorColumn, queryVector, textColumn, textQuery, options);
|
|
307
|
+
try {
|
|
308
|
+
const result = await this.requirePool().query(sql, values);
|
|
309
|
+
return result.rows.map(row => {
|
|
310
|
+
const distance = row.distance;
|
|
311
|
+
delete row.distance;
|
|
312
|
+
const rawData = this._rowToRawData(row, schema);
|
|
313
|
+
const record = this.deps.createRecord(modelName, rawData, { isDbRecord: true, serialize: false, transform: false });
|
|
314
|
+
this._evictIfNotMemory(modelName, record);
|
|
315
|
+
return { record, distance };
|
|
316
|
+
});
|
|
317
|
+
}
|
|
318
|
+
catch (error) {
|
|
319
|
+
if (isDbError(error) && error.code === '42P01')
|
|
320
|
+
return [];
|
|
321
|
+
throw error;
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
/**
|
|
325
|
+
* Remove a record from the in-memory store if its model has memory: false.
|
|
326
|
+
* The record object itself survives -- the caller retains the reference.
|
|
327
|
+
* @private
|
|
328
|
+
*/
|
|
329
|
+
_evictIfNotMemory(modelName, record) {
|
|
330
|
+
const storeRef = this.deps.store;
|
|
331
|
+
if (storeRef._memoryResolver && !storeRef._memoryResolver(modelName)) {
|
|
332
|
+
const modelStore = storeRef.get?.(modelName) ?? storeRef.data?.get(modelName);
|
|
333
|
+
if (modelStore)
|
|
334
|
+
modelStore.delete(record.id);
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
_rowToRawData(row, schema) {
|
|
338
|
+
const rawData = { ...row };
|
|
339
|
+
// PostgreSQL returns native booleans and parsed JSONB -- no manual conversion needed.
|
|
340
|
+
// Only FK remapping and timestamp stripping required.
|
|
341
|
+
// Map FK columns back to relationship keys
|
|
342
|
+
for (const [fkCol] of Object.entries(schema.foreignKeys)) {
|
|
343
|
+
const relName = fkCol.replace(/_id$/, '');
|
|
344
|
+
if (rawData[fkCol] !== undefined) {
|
|
345
|
+
rawData[relName] = rawData[fkCol];
|
|
346
|
+
delete rawData[fkCol];
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
// Remove timestamp columns -- managed by PostgreSQL
|
|
350
|
+
delete rawData.created_at;
|
|
351
|
+
delete rawData.updated_at;
|
|
352
|
+
return rawData;
|
|
353
|
+
}
|
|
354
|
+
async persist(operation, modelName, context, response) {
|
|
355
|
+
// Views are read-only -- no-op for all write operations
|
|
356
|
+
const Orm = (await import('@stonyx/orm')).default;
|
|
357
|
+
if (Orm.instance?.isView?.(modelName))
|
|
358
|
+
return;
|
|
359
|
+
switch (operation) {
|
|
360
|
+
case 'create':
|
|
361
|
+
return this._persistCreate(modelName, context, response);
|
|
362
|
+
case 'update':
|
|
363
|
+
return this._persistUpdate(modelName, context, response);
|
|
364
|
+
case 'delete':
|
|
365
|
+
return this._persistDelete(modelName, context);
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
async _persistCreate(modelName, _context, response) {
|
|
369
|
+
const schemas = this.deps.introspectModels();
|
|
370
|
+
const schema = schemas[modelName];
|
|
371
|
+
if (!schema)
|
|
372
|
+
return;
|
|
373
|
+
const recordId = response?.data?.id;
|
|
374
|
+
const storeRef = this.deps.store;
|
|
375
|
+
const record = recordId != null ? storeRef.get(modelName, isNaN(recordId) ? recordId : parseInt(recordId)) : null;
|
|
376
|
+
if (!record)
|
|
377
|
+
return;
|
|
378
|
+
const insertData = this._recordToRow(record, schema);
|
|
379
|
+
// For auto-increment models, remove the pending ID
|
|
380
|
+
const isPendingId = record.__data.__pendingSqlId;
|
|
381
|
+
if (isPendingId) {
|
|
382
|
+
delete insertData.id;
|
|
383
|
+
}
|
|
384
|
+
const { sql, values } = this.deps.buildInsert(schema.table, insertData);
|
|
385
|
+
const result = await this.requirePool().query(sql, values);
|
|
386
|
+
// Re-key the record in the store if PostgreSQL generated the ID (via RETURNING)
|
|
387
|
+
if (isPendingId && result.rows.length > 0) {
|
|
388
|
+
const pendingId = record.id;
|
|
389
|
+
const realId = result.rows[0].id;
|
|
390
|
+
const modelStore = this.deps.store.get(modelName);
|
|
391
|
+
modelStore.delete(pendingId);
|
|
392
|
+
record.__data.id = realId;
|
|
393
|
+
record.id = realId;
|
|
394
|
+
modelStore.set(realId, record);
|
|
395
|
+
// Update the response data with the real ID
|
|
396
|
+
if (response?.data) {
|
|
397
|
+
response.data.id = realId;
|
|
398
|
+
}
|
|
399
|
+
delete record.__data.__pendingSqlId;
|
|
400
|
+
}
|
|
401
|
+
}
|
|
402
|
+
async _persistUpdate(modelName, context, _response) {
|
|
403
|
+
const schemas = this.deps.introspectModels();
|
|
404
|
+
const schema = schemas[modelName];
|
|
405
|
+
if (!schema)
|
|
406
|
+
return;
|
|
407
|
+
const record = context.record;
|
|
408
|
+
if (!record)
|
|
409
|
+
return;
|
|
410
|
+
const id = record.id;
|
|
411
|
+
const oldState = context.oldState || {};
|
|
412
|
+
const currentData = record.__data;
|
|
413
|
+
// Build a diff of changed columns
|
|
414
|
+
const changedData = {};
|
|
415
|
+
for (const [col] of Object.entries(schema.columns)) {
|
|
416
|
+
if (currentData[col] !== oldState[col]) {
|
|
417
|
+
changedData[col] = currentData[col] ?? null;
|
|
418
|
+
}
|
|
419
|
+
}
|
|
420
|
+
// Check FK changes too
|
|
421
|
+
for (const fkCol of Object.keys(schema.foreignKeys)) {
|
|
422
|
+
const relName = fkCol.replace(/_id$/, '');
|
|
423
|
+
const currentFkValue = record.__relationships[relName]?.id ?? null;
|
|
424
|
+
const oldFkValue = oldState[relName] ?? null;
|
|
425
|
+
if (currentFkValue !== oldFkValue) {
|
|
426
|
+
changedData[fkCol] = currentFkValue;
|
|
427
|
+
}
|
|
428
|
+
}
|
|
429
|
+
if (Object.keys(changedData).length === 0)
|
|
430
|
+
return;
|
|
431
|
+
// PostgreSQL doesn't have ON UPDATE CURRENT_TIMESTAMP -- set updated_at manually
|
|
432
|
+
changedData.updated_at = new Date();
|
|
433
|
+
const { sql, values } = this.deps.buildUpdate(schema.table, id, changedData);
|
|
434
|
+
await this.requirePool().query(sql, values);
|
|
435
|
+
}
|
|
436
|
+
async _persistDelete(modelName, context) {
|
|
437
|
+
const schemas = this.deps.introspectModels();
|
|
438
|
+
const schema = schemas[modelName];
|
|
439
|
+
if (!schema)
|
|
440
|
+
return;
|
|
441
|
+
const id = context.recordId;
|
|
442
|
+
if (id == null)
|
|
443
|
+
return;
|
|
444
|
+
const { sql, values } = this.deps.buildDelete(schema.table, id);
|
|
445
|
+
await this.requirePool().query(sql, values);
|
|
446
|
+
}
|
|
447
|
+
_recordToRow(record, schema) {
|
|
448
|
+
const row = {};
|
|
449
|
+
const data = record.__data;
|
|
450
|
+
// ID
|
|
451
|
+
if (data.id !== undefined) {
|
|
452
|
+
row.id = data.id;
|
|
453
|
+
}
|
|
454
|
+
// Attribute columns
|
|
455
|
+
for (const [col, pgType] of Object.entries(schema.columns)) {
|
|
456
|
+
if (data[col] !== undefined) {
|
|
457
|
+
// JSONB columns: stringify non-string values for PostgreSQL JSONB storage
|
|
458
|
+
row[col] = pgType === 'JSONB' && typeof data[col] !== 'string'
|
|
459
|
+
? JSON.stringify(data[col])
|
|
460
|
+
: data[col];
|
|
461
|
+
}
|
|
462
|
+
}
|
|
463
|
+
// FK columns from relationships
|
|
464
|
+
for (const fkCol of Object.keys(schema.foreignKeys)) {
|
|
465
|
+
const relName = fkCol.replace(/_id$/, '');
|
|
466
|
+
const related = record.__relationships[relName];
|
|
467
|
+
if (related) {
|
|
468
|
+
row[fkCol] = related.id;
|
|
469
|
+
}
|
|
470
|
+
else if (data[relName] !== undefined) {
|
|
471
|
+
// Raw FK value (e.g., from create payload)
|
|
472
|
+
row[fkCol] = data[relName];
|
|
473
|
+
}
|
|
474
|
+
}
|
|
475
|
+
return row;
|
|
476
|
+
}
|
|
477
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
interface QueryResult {
|
|
2
|
+
sql: string;
|
|
3
|
+
values: unknown[];
|
|
4
|
+
}
|
|
5
|
+
interface VectorSearchOptions {
|
|
6
|
+
limit?: number;
|
|
7
|
+
where?: Record<string, unknown>;
|
|
8
|
+
}
|
|
9
|
+
interface HybridSearchOptions {
|
|
10
|
+
limit?: number;
|
|
11
|
+
where?: Record<string, unknown>;
|
|
12
|
+
}
|
|
13
|
+
export declare function validateIdentifier(name: string, context?: string): string;
|
|
14
|
+
export declare function buildInsert(table: string, data: Record<string, unknown>): QueryResult;
|
|
15
|
+
export declare function buildUpdate(table: string, id: unknown, data: Record<string, unknown>): QueryResult;
|
|
16
|
+
export declare function buildDelete(table: string, id: unknown): QueryResult;
|
|
17
|
+
export declare function buildSelect(table: string, conditions?: Record<string, unknown>): QueryResult;
|
|
18
|
+
/**
|
|
19
|
+
* Build a vector similarity search query using cosine distance (<=>).
|
|
20
|
+
*/
|
|
21
|
+
export declare function buildVectorSearch(table: string, vectorColumn: string, queryVector: number[], options?: VectorSearchOptions): QueryResult;
|
|
22
|
+
/**
|
|
23
|
+
* Build a hybrid search query combining vector similarity with text filtering.
|
|
24
|
+
* Uses cosine distance for vector ranking and ILIKE for text matching.
|
|
25
|
+
*/
|
|
26
|
+
export declare function buildHybridSearch(table: string, vectorColumn: string, queryVector: number[], textColumn: string, textQuery: string, options?: HybridSearchOptions): QueryResult;
|
|
27
|
+
export {};
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
const SAFE_IDENTIFIER = /^[a-zA-Z_][a-zA-Z0-9_-]*$/;
|
|
2
|
+
export function validateIdentifier(name, context = 'identifier') {
|
|
3
|
+
if (!name || typeof name !== 'string' || !SAFE_IDENTIFIER.test(name)) {
|
|
4
|
+
throw new Error(`Invalid SQL ${context}: "${name}". Identifiers must match ${SAFE_IDENTIFIER}`);
|
|
5
|
+
}
|
|
6
|
+
return name;
|
|
7
|
+
}
|
|
8
|
+
export function buildInsert(table, data) {
|
|
9
|
+
validateIdentifier(table, 'table name');
|
|
10
|
+
const keys = Object.keys(data);
|
|
11
|
+
keys.forEach(k => validateIdentifier(k, 'column name'));
|
|
12
|
+
const placeholders = keys.map((_, i) => `$${i + 1}`);
|
|
13
|
+
const values = keys.map(k => data[k]);
|
|
14
|
+
const sql = `INSERT INTO "${table}" (${keys.map(k => `"${k}"`).join(', ')}) VALUES (${placeholders.join(', ')}) RETURNING "id"`;
|
|
15
|
+
return { sql, values };
|
|
16
|
+
}
|
|
17
|
+
export function buildUpdate(table, id, data) {
|
|
18
|
+
validateIdentifier(table, 'table name');
|
|
19
|
+
const keys = Object.keys(data);
|
|
20
|
+
keys.forEach(k => validateIdentifier(k, 'column name'));
|
|
21
|
+
const setClauses = keys.map((k, i) => `"${k}" = $${i + 1}`);
|
|
22
|
+
const values = [...keys.map(k => data[k]), id];
|
|
23
|
+
const sql = `UPDATE "${table}" SET ${setClauses.join(', ')} WHERE "id" = $${keys.length + 1}`;
|
|
24
|
+
return { sql, values };
|
|
25
|
+
}
|
|
26
|
+
export function buildDelete(table, id) {
|
|
27
|
+
validateIdentifier(table, 'table name');
|
|
28
|
+
return {
|
|
29
|
+
sql: `DELETE FROM "${table}" WHERE "id" = $1`,
|
|
30
|
+
values: [id],
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
export function buildSelect(table, conditions) {
|
|
34
|
+
validateIdentifier(table, 'table name');
|
|
35
|
+
if (!conditions || Object.keys(conditions).length === 0) {
|
|
36
|
+
return { sql: `SELECT * FROM "${table}"`, values: [] };
|
|
37
|
+
}
|
|
38
|
+
const keys = Object.keys(conditions);
|
|
39
|
+
keys.forEach(k => validateIdentifier(k, 'column name'));
|
|
40
|
+
const whereClauses = keys.map((k, i) => `"${k}" = $${i + 1}`);
|
|
41
|
+
const values = keys.map(k => conditions[k]);
|
|
42
|
+
const sql = `SELECT * FROM "${table}" WHERE ${whereClauses.join(' AND ')}`;
|
|
43
|
+
return { sql, values };
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Build a vector similarity search query using cosine distance (<=>).
|
|
47
|
+
*/
|
|
48
|
+
export function buildVectorSearch(table, vectorColumn, queryVector, options = {}) {
|
|
49
|
+
validateIdentifier(table, 'table name');
|
|
50
|
+
validateIdentifier(vectorColumn, 'column name');
|
|
51
|
+
const { limit = 10, where } = options;
|
|
52
|
+
const values = [];
|
|
53
|
+
let paramIndex = 1;
|
|
54
|
+
// Vector parameter as a formatted string for pgvector
|
|
55
|
+
const vectorStr = `[${queryVector.join(',')}]`;
|
|
56
|
+
values.push(vectorStr);
|
|
57
|
+
const vectorParam = `$${paramIndex++}`;
|
|
58
|
+
const whereClauses = [];
|
|
59
|
+
if (where) {
|
|
60
|
+
for (const [k, v] of Object.entries(where)) {
|
|
61
|
+
validateIdentifier(k, 'column name');
|
|
62
|
+
whereClauses.push(`"${k}" = $${paramIndex++}`);
|
|
63
|
+
values.push(v);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
const whereStr = whereClauses.length > 0 ? ` WHERE ${whereClauses.join(' AND ')}` : '';
|
|
67
|
+
values.push(limit);
|
|
68
|
+
const sql = `SELECT *, ("${vectorColumn}" <=> ${vectorParam}::vector) AS distance FROM "${table}"${whereStr} ORDER BY "${vectorColumn}" <=> ${vectorParam}::vector LIMIT $${paramIndex}`;
|
|
69
|
+
return { sql, values };
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Build a hybrid search query combining vector similarity with text filtering.
|
|
73
|
+
* Uses cosine distance for vector ranking and ILIKE for text matching.
|
|
74
|
+
*/
|
|
75
|
+
export function buildHybridSearch(table, vectorColumn, queryVector, textColumn, textQuery, options = {}) {
|
|
76
|
+
validateIdentifier(table, 'table name');
|
|
77
|
+
validateIdentifier(vectorColumn, 'column name');
|
|
78
|
+
validateIdentifier(textColumn, 'column name');
|
|
79
|
+
const { limit = 10, where } = options;
|
|
80
|
+
const values = [];
|
|
81
|
+
let paramIndex = 1;
|
|
82
|
+
const vectorStr = `[${queryVector.join(',')}]`;
|
|
83
|
+
values.push(vectorStr);
|
|
84
|
+
const vectorParam = `$${paramIndex++}`;
|
|
85
|
+
values.push(`%${textQuery}%`);
|
|
86
|
+
const textParam = `$${paramIndex++}`;
|
|
87
|
+
const whereClauses = [`"${textColumn}" ILIKE ${textParam}`];
|
|
88
|
+
if (where) {
|
|
89
|
+
for (const [k, v] of Object.entries(where)) {
|
|
90
|
+
validateIdentifier(k, 'column name');
|
|
91
|
+
whereClauses.push(`"${k}" = $${paramIndex++}`);
|
|
92
|
+
values.push(v);
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
values.push(limit);
|
|
96
|
+
const sql = `SELECT *, ("${vectorColumn}" <=> ${vectorParam}::vector) AS distance FROM "${table}" WHERE ${whereClauses.join(' AND ')} ORDER BY "${vectorColumn}" <=> ${vectorParam}::vector LIMIT $${paramIndex}`;
|
|
97
|
+
return { sql, values };
|
|
98
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import type { ForeignKeyDef, HypertableConfig, ModelSchema, ViewSchema } from '../types/orm-types.js';
|
|
2
|
+
interface ViewSnapshotEntry {
|
|
3
|
+
viewName: string;
|
|
4
|
+
source: string;
|
|
5
|
+
groupBy?: string;
|
|
6
|
+
columns: Record<string, string>;
|
|
7
|
+
foreignKeys: Record<string, ForeignKeyDef>;
|
|
8
|
+
isView: boolean;
|
|
9
|
+
viewQuery: string;
|
|
10
|
+
}
|
|
11
|
+
interface ModelSnapshotEntry {
|
|
12
|
+
table: string;
|
|
13
|
+
idType: string;
|
|
14
|
+
columns: Record<string, string>;
|
|
15
|
+
foreignKeys: Record<string, ForeignKeyDef>;
|
|
16
|
+
vectorColumns?: Record<string, number>;
|
|
17
|
+
hypertable?: HypertableConfig;
|
|
18
|
+
}
|
|
19
|
+
export declare function introspectModels(): Record<string, ModelSchema>;
|
|
20
|
+
export declare function buildTableDDL(name: string, schema: ModelSchema, allSchemas?: Record<string, ModelSchema>): string;
|
|
21
|
+
/**
|
|
22
|
+
* Build HNSW index DDL for vector columns on a model.
|
|
23
|
+
*/
|
|
24
|
+
export declare function buildVectorIndexDDL(name: string, schema: ModelSchema): string[];
|
|
25
|
+
export { getTopologicalOrder } from '../schema-helpers.js';
|
|
26
|
+
export declare function introspectViews(): Record<string, ViewSchema>;
|
|
27
|
+
export declare function buildViewDDL(name: string, viewSchema: ViewSchema, modelSchemas?: Record<string, ModelSchema>): string;
|
|
28
|
+
export declare function viewSchemasToSnapshot(viewSchemas: Record<string, ViewSchema>): Record<string, ViewSnapshotEntry>;
|
|
29
|
+
export declare function schemasToSnapshot(schemas: Record<string, ModelSchema>): Record<string, ModelSnapshotEntry>;
|