@onyx.dev/onyx-database 0.2.10 → 1.0.0
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 +168 -3
- package/dist/gen/cli/generate.cjs +1367 -112
- package/dist/gen/cli/generate.cjs.map +1 -1
- package/dist/index.cjs +207 -16
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +144 -3
- package/dist/index.d.ts +144 -3
- package/dist/index.js +206 -17
- package/dist/index.js.map +1 -1
- package/dist/schema/cli/schema.cjs +1991 -0
- package/dist/schema/cli/schema.cjs.map +1 -0
- package/package.json +4 -2
package/README.md
CHANGED
|
@@ -122,6 +122,9 @@ Generate directly from the API (using the same credential resolver as `init()`):
|
|
|
122
122
|
npx onyx-gen --source api --out ./src/onyx/types.ts --name OnyxSchema
|
|
123
123
|
```
|
|
124
124
|
|
|
125
|
+
With `--source api`, `onyx-gen` calls the Schema API (same as `onyx-schema get`) using the
|
|
126
|
+
standard config chain (env, project file, home profile).
|
|
127
|
+
|
|
125
128
|
Timestamp attributes are emitted as `Date` fields by default. When saving,
|
|
126
129
|
`Date` values are automatically serialized to ISO timestamp strings. Pass
|
|
127
130
|
`--timestamps string` to keep timestamps as ISO strings in generated types.
|
|
@@ -134,6 +137,63 @@ npx onyx-gen --source file --schema ./onyx.schema.json --out ./src/onyx/types.ts
|
|
|
134
137
|
|
|
135
138
|
Run it with no flags to use the defaults: `onyx-gen` reads `./onyx.schema.json` and writes to `./onyx/types.ts`.
|
|
136
139
|
|
|
140
|
+
### Manage schemas from the CLI
|
|
141
|
+
|
|
142
|
+
Publish or download schema JSON directly via API using the `onyx-schema` helper:
|
|
143
|
+
|
|
144
|
+
```bash
|
|
145
|
+
# Publish ./onyx.schema.json with publish=true by default
|
|
146
|
+
onyx-schema publish
|
|
147
|
+
|
|
148
|
+
# Overwrite ./onyx.schema.json with the remote schema
|
|
149
|
+
onyx-schema get
|
|
150
|
+
|
|
151
|
+
# Fetch only selected tables (prints to stdout; does not overwrite files)
|
|
152
|
+
onyx-schema get --tables=User,Profile
|
|
153
|
+
|
|
154
|
+
# Example subset output
|
|
155
|
+
onyx-schema get --tables=User,Profile
|
|
156
|
+
# {
|
|
157
|
+
# "tables": [
|
|
158
|
+
# {
|
|
159
|
+
# "name": "User",
|
|
160
|
+
# "attributes": [
|
|
161
|
+
# { "name": "id", "type": "string", "required": true },
|
|
162
|
+
# { "name": "email", "type": "string", "required": true }
|
|
163
|
+
# ]
|
|
164
|
+
# },
|
|
165
|
+
# {
|
|
166
|
+
# "name": "Profile",
|
|
167
|
+
# "attributes": [
|
|
168
|
+
# { "name": "id", "type": "string", "required": true },
|
|
169
|
+
# { "name": "userId", "type": "string", "required": true }
|
|
170
|
+
# ]
|
|
171
|
+
# }
|
|
172
|
+
# ]
|
|
173
|
+
# }
|
|
174
|
+
|
|
175
|
+
# Validate a schema file without publishing
|
|
176
|
+
onyx-schema validate ./onyx.schema.json
|
|
177
|
+
|
|
178
|
+
# Diff local schema vs API
|
|
179
|
+
onyx-schema diff ./onyx.schema.json
|
|
180
|
+
# Prints added/removed/changed tables and attribute differences between the API schema and your local file.
|
|
181
|
+
```
|
|
182
|
+
|
|
183
|
+
When `--tables` is provided, the subset is printed to stdout instead of writing a
|
|
184
|
+
file. Otherwise, the CLI writes to `./onyx.schema.json` by default.
|
|
185
|
+
|
|
186
|
+
In this repo's `examples/` workspace, the following scripts wrap the same commands:
|
|
187
|
+
|
|
188
|
+
```bash
|
|
189
|
+
npm run schema:get # fetch remote schema into ./onyx.schema.json
|
|
190
|
+
npm run schema:validate # validate the local schema file
|
|
191
|
+
npm run schema:publish # validate then publish the local schema
|
|
192
|
+
```
|
|
193
|
+
|
|
194
|
+
The CLI reuses the same configuration resolution as `onyx.init()` (env vars,
|
|
195
|
+
project config, and home profile files).
|
|
196
|
+
|
|
137
197
|
You can also emit to multiple paths in one run (comma-separated or by repeating `--out`):
|
|
138
198
|
|
|
139
199
|
```bash
|
|
@@ -266,7 +326,9 @@ Importable helpers for conditions and sort:
|
|
|
266
326
|
|
|
267
327
|
```ts
|
|
268
328
|
import {
|
|
269
|
-
eq, neq,
|
|
329
|
+
eq, neq, within, notWithin, // preferred aliases for IN/NOT IN
|
|
330
|
+
inOp, notIn,
|
|
331
|
+
between,
|
|
270
332
|
gt, gte, lt, lte,
|
|
271
333
|
like, notLike, contains, notContains,
|
|
272
334
|
startsWith, notStartsWith, matches, notMatches,
|
|
@@ -275,6 +337,41 @@ import {
|
|
|
275
337
|
} from '@onyx.dev/onyx-database';
|
|
276
338
|
```
|
|
277
339
|
|
|
340
|
+
- Prefer `within`/`notWithin` for inclusion checks (supports arrays, comma-separated strings, or inner queries).
|
|
341
|
+
- `inOp`/`notIn` remain available for backward compatibility and are exact aliases.
|
|
342
|
+
|
|
343
|
+
### Inner queries (IN/NOT IN with sub-selects)
|
|
344
|
+
|
|
345
|
+
You can pass another query builder to `within` or `notWithin` to create nested filters. The SDK serializes the inner query (including its table) before sending the request.
|
|
346
|
+
|
|
347
|
+
```ts
|
|
348
|
+
import { onyx, within, notWithin, eq, tables, Schema } from '@onyx.dev/onyx-database';
|
|
349
|
+
|
|
350
|
+
const db = onyx.init<Schema>();
|
|
351
|
+
|
|
352
|
+
// Users that HAVE the admin role
|
|
353
|
+
const usersWithAdmin = await db
|
|
354
|
+
.from(tables.User)
|
|
355
|
+
.where(
|
|
356
|
+
within(
|
|
357
|
+
'id',
|
|
358
|
+
db.select('userId').from(tables.UserRole).where(eq('roleId', 'role-admin')),
|
|
359
|
+
),
|
|
360
|
+
)
|
|
361
|
+
.list();
|
|
362
|
+
|
|
363
|
+
// Roles that DO NOT include a specific permission
|
|
364
|
+
const rolesMissingPermission = await db
|
|
365
|
+
.from(tables.Role)
|
|
366
|
+
.where(
|
|
367
|
+
notWithin(
|
|
368
|
+
'id',
|
|
369
|
+
db.from(tables.RolePermission).where(eq('permissionId', 'perm-manage-users')),
|
|
370
|
+
),
|
|
371
|
+
)
|
|
372
|
+
.list();
|
|
373
|
+
```
|
|
374
|
+
|
|
278
375
|
---
|
|
279
376
|
|
|
280
377
|
## Usage examples with `User`, `Role`, `Permission`
|
|
@@ -406,7 +503,75 @@ const delCount = await db
|
|
|
406
503
|
|
|
407
504
|
```
|
|
408
505
|
|
|
409
|
-
### 5)
|
|
506
|
+
### 5) Schema API
|
|
507
|
+
|
|
508
|
+
```ts
|
|
509
|
+
import { onyx } from '@onyx.dev/onyx-database';
|
|
510
|
+
const db = onyx.init();
|
|
511
|
+
|
|
512
|
+
// Fetch current schema (optionally filter by tables)
|
|
513
|
+
const schema = await db.getSchema({ tables: ['User', 'Profile'] });
|
|
514
|
+
|
|
515
|
+
// Review history
|
|
516
|
+
const history = await db.getSchemaHistory();
|
|
517
|
+
|
|
518
|
+
// Validate changes without applying
|
|
519
|
+
await db.validateSchema({
|
|
520
|
+
revisionDescription: 'Add profile triggers',
|
|
521
|
+
entities: [
|
|
522
|
+
{
|
|
523
|
+
name: 'Profile',
|
|
524
|
+
identifier: { name: 'id', generator: 'UUID' },
|
|
525
|
+
attributes: [
|
|
526
|
+
{ name: 'id', type: 'String', isNullable: false },
|
|
527
|
+
{ name: 'userId', type: 'String', isNullable: false },
|
|
528
|
+
],
|
|
529
|
+
},
|
|
530
|
+
],
|
|
531
|
+
});
|
|
532
|
+
|
|
533
|
+
// Update and optionally publish
|
|
534
|
+
await db.updateSchema(
|
|
535
|
+
{
|
|
536
|
+
revisionDescription: 'Publish profile changes',
|
|
537
|
+
entities: [
|
|
538
|
+
{
|
|
539
|
+
name: 'Profile',
|
|
540
|
+
identifier: { name: 'id', generator: 'UUID' },
|
|
541
|
+
attributes: [
|
|
542
|
+
{ name: 'id', type: 'String', isNullable: false },
|
|
543
|
+
{ name: 'userId', type: 'String', isNullable: false },
|
|
544
|
+
],
|
|
545
|
+
},
|
|
546
|
+
],
|
|
547
|
+
},
|
|
548
|
+
{ publish: true },
|
|
549
|
+
);
|
|
550
|
+
```
|
|
551
|
+
|
|
552
|
+
### 6) Secrets API
|
|
553
|
+
|
|
554
|
+
```ts
|
|
555
|
+
import { onyx } from '@onyx.dev/onyx-database';
|
|
556
|
+
const db = onyx.init();
|
|
557
|
+
|
|
558
|
+
// List secret metadata
|
|
559
|
+
const list = await db.listSecrets();
|
|
560
|
+
|
|
561
|
+
// Read a decrypted secret value
|
|
562
|
+
const secret = await db.getSecret('api-key');
|
|
563
|
+
|
|
564
|
+
// Create or update a secret
|
|
565
|
+
await db.putSecret('api-key', {
|
|
566
|
+
value: 'super-secret',
|
|
567
|
+
purpose: 'Access to external API',
|
|
568
|
+
});
|
|
569
|
+
|
|
570
|
+
// Delete a secret
|
|
571
|
+
await db.deleteSecret('api-key');
|
|
572
|
+
```
|
|
573
|
+
|
|
574
|
+
### 7) Documents API (binary assets)
|
|
410
575
|
|
|
411
576
|
```ts
|
|
412
577
|
import { onyx, type OnyxDocument } from '@onyx.dev/onyx-database';
|
|
@@ -429,7 +594,7 @@ const image = await db.getDocument('logo.png', { width: 128, height: 128 });
|
|
|
429
594
|
await db.deleteDocument('logo.png');
|
|
430
595
|
```
|
|
431
596
|
|
|
432
|
-
###
|
|
597
|
+
### 8) Streaming (live changes)
|
|
433
598
|
|
|
434
599
|
```ts
|
|
435
600
|
import { onyx, eq } from '@onyx.dev/onyx-database';
|