rado 0.3.2 → 0.3.3

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.
Files changed (68) hide show
  1. package/README.md +277 -275
  2. package/dist/define/Expr.d.ts +2 -2
  3. package/dist/define/Expr.js +4 -1
  4. package/dist/define/Ops.d.ts +1 -6
  5. package/dist/define/Ops.js +10 -7
  6. package/dist/define/Query.d.ts +117 -2
  7. package/dist/define/Query.js +472 -17
  8. package/dist/define/Selection.d.ts +1 -1
  9. package/dist/define/Table.d.ts +1 -2
  10. package/dist/define/Table.js +1 -1
  11. package/dist/define/VirtualTable.d.ts +1 -1
  12. package/dist/driver/bun-sqlite.js +1 -2
  13. package/dist/index.d.ts +0 -8
  14. package/dist/index.js +0 -8
  15. package/dist/lib/Driver.d.ts +1 -2
  16. package/package.json +74 -74
  17. package/dist/define/Cursor.d.ts +0 -85
  18. package/dist/define/Cursor.js +0 -256
  19. package/dist/define/Update.d.ts +0 -4
  20. package/dist/define/Update.js +0 -0
  21. package/dist/define/query/Batch.d.ts +0 -7
  22. package/dist/define/query/Batch.js +0 -17
  23. package/dist/define/query/CreateTable.d.ts +0 -6
  24. package/dist/define/query/CreateTable.js +0 -11
  25. package/dist/define/query/Delete.d.ts +0 -13
  26. package/dist/define/query/Delete.js +0 -21
  27. package/dist/define/query/Insert.d.ts +0 -22
  28. package/dist/define/query/Insert.js +0 -47
  29. package/dist/define/query/Select.d.ts +0 -41
  30. package/dist/define/query/Select.js +0 -183
  31. package/dist/define/query/TableSelect.d.ts +0 -28
  32. package/dist/define/query/TableSelect.js +0 -79
  33. package/dist/define/query/Union.d.ts +0 -17
  34. package/dist/define/query/Union.js +0 -116
  35. package/dist/define/query/Update.d.ts +0 -12
  36. package/dist/define/query/Update.js +0 -18
  37. package/dist/lib/Column.d.ts +0 -57
  38. package/dist/lib/Column.js +0 -68
  39. package/dist/lib/Cursor.d.ts +0 -85
  40. package/dist/lib/Cursor.js +0 -256
  41. package/dist/lib/Expr.d.ts +0 -154
  42. package/dist/lib/Expr.js +0 -284
  43. package/dist/lib/Fields.d.ts +0 -13
  44. package/dist/lib/Fields.js +0 -0
  45. package/dist/lib/Functions.d.ts +0 -5
  46. package/dist/lib/Functions.js +0 -11
  47. package/dist/lib/Id.d.ts +0 -3
  48. package/dist/lib/Id.js +0 -0
  49. package/dist/lib/Index.d.ts +0 -17
  50. package/dist/lib/Index.js +0 -20
  51. package/dist/lib/Ops.d.ts +0 -7
  52. package/dist/lib/Ops.js +0 -36
  53. package/dist/lib/OrderBy.d.ts +0 -9
  54. package/dist/lib/OrderBy.js +0 -9
  55. package/dist/lib/Param.d.ts +0 -20
  56. package/dist/lib/Param.js +0 -27
  57. package/dist/lib/Query.d.ts +0 -109
  58. package/dist/lib/Query.js +0 -74
  59. package/dist/lib/Schema.d.ts +0 -18
  60. package/dist/lib/Schema.js +0 -67
  61. package/dist/lib/Selection.d.ts +0 -20
  62. package/dist/lib/Selection.js +0 -7
  63. package/dist/lib/Table.d.ts +0 -62
  64. package/dist/lib/Table.js +0 -130
  65. package/dist/lib/Target.d.ts +0 -38
  66. package/dist/lib/Target.js +0 -42
  67. package/dist/lib/Update.d.ts +0 -4
  68. package/dist/lib/Update.js +0 -0
@@ -1,67 +0,0 @@
1
- // src/lib/Schema.ts
2
- import { Query } from "./Query.js";
3
- import { Statement } from "./Statement.js";
4
- var Schema;
5
- ((Schema2) => {
6
- function create(schema) {
7
- const queries = [];
8
- queries.push(Query.CreateTable({ table: schema, ifNotExists: true }));
9
- for (const index of Object.values(schema.indexes))
10
- queries.push(Query.CreateIndex({ table: schema, index, ifNotExists: true }));
11
- return Query.Batch({ queries });
12
- }
13
- Schema2.create = create;
14
- function removeLeadingWhitespace(str) {
15
- return str.replace(/\n\s+/g, "\n");
16
- }
17
- function upgrade(formatter, local, schema) {
18
- const columnNames = /* @__PURE__ */ new Set([
19
- ...Object.keys(local.columns),
20
- ...Object.keys(schema.columns)
21
- ]);
22
- const res = [];
23
- for (const columnName of columnNames) {
24
- const localInstruction = local.columns[columnName];
25
- const schemaCol = schema.columns[columnName];
26
- if (!localInstruction) {
27
- res.push(Query.AlterTable({ table: schema, addColumn: schemaCol }));
28
- } else if (!schemaCol) {
29
- res.push(Query.AlterTable({ table: schema, dropColumn: columnName }));
30
- } else {
31
- const { sql: instruction } = formatter.formatColumn(
32
- { stmt: new Statement(formatter) },
33
- { ...schemaCol, references: void 0 }
34
- );
35
- if (removeLeadingWhitespace(localInstruction) !== removeLeadingWhitespace(instruction)) {
36
- res.push(Query.AlterTable({ table: schema, alterColumn: schemaCol }));
37
- }
38
- }
39
- }
40
- const indexNames = /* @__PURE__ */ new Set([
41
- ...Object.keys(local.indexes),
42
- ...Object.keys(schema.indexes)
43
- ]);
44
- for (const indexName of indexNames) {
45
- const localInstruction = local.indexes[indexName];
46
- const schemaIndex = schema.indexes[indexName];
47
- if (!localInstruction) {
48
- res.push(Query.CreateIndex({ table: schema, index: schemaIndex }));
49
- } else if (!schemaIndex) {
50
- res.unshift(Query.DropIndex({ table: schema, name: indexName }));
51
- } else {
52
- const { sql: instruction } = formatter.compile(
53
- Query.CreateIndex({ table: schema, index: schemaIndex })
54
- );
55
- if (removeLeadingWhitespace(localInstruction) !== removeLeadingWhitespace(instruction)) {
56
- res.unshift(Query.DropIndex({ table: schema, name: indexName }));
57
- res.push(Query.CreateIndex({ table: schema, index: schemaIndex }));
58
- }
59
- }
60
- }
61
- return res;
62
- }
63
- Schema2.upgrade = upgrade;
64
- })(Schema || (Schema = {}));
65
- export {
66
- Schema
67
- };
@@ -1,20 +0,0 @@
1
- import type { Cursor } from './Cursor';
2
- import type { Expr } from './Expr';
3
- type SelectionBase = (() => any) | Expr<any> | Cursor.SelectMultiple<any> | Cursor.SelectSingle<any>;
4
- interface SelectionRecord extends Record<string, Selection> {
5
- }
6
- export type Selection = SelectionBase | SelectionRecord;
7
- export declare namespace Selection {
8
- const __tableType: unique symbol;
9
- const __cursorType: unique symbol;
10
- type Infer<T> = T extends {
11
- [__tableType](): infer K;
12
- } ? K : T extends {
13
- [__cursorType](): infer K;
14
- } ? K : T extends Expr<infer K> ? K : T extends Record<string, Selection> ? {
15
- [K in keyof T as T[K] extends () => any ? never : K]: Infer<T[K]>;
16
- } : T extends () => any ? never : T;
17
- type With<A, B> = Expr<Combine<A, B>>;
18
- type Combine<A, B> = Omit<A, keyof Infer<B>> & Infer<B>;
19
- }
20
- export {};
@@ -1,7 +0,0 @@
1
- // src/lib/Selection.ts
2
- var Selection;
3
- ((Selection2) => {
4
- })(Selection || (Selection = {}));
5
- export {
6
- Selection
7
- };
@@ -1,62 +0,0 @@
1
- import { Column, PrimaryKey } from './Column';
2
- import { Cursor } from './Cursor';
3
- import { EV, Expr } from './Expr';
4
- import { Fields } from './Fields';
5
- import { Index } from './Index';
6
- import { Schema } from './Schema';
7
- import { Selection } from './Selection';
8
- import { Update } from './Update';
9
- export declare class Table<T> extends Cursor.SelectMultiple<Table.Normalize<T>> {
10
- [Selection.__tableType](): Table.Normalize<T>;
11
- constructor(schema: Schema);
12
- fetch(id: EV<T extends {
13
- id: Column.IsPrimary<infer V, any>;
14
- } ? V : never>): Cursor.SelectSingle<Table.Normalize<T> | undefined>;
15
- insertOne(record: Table.Insert<T>): Cursor<Table.Normalize<T>>;
16
- insertAll(data: Array<Table.Insert<T>>): Cursor.InsertValues;
17
- set(data: Update<Table.Normalize<T>>): Cursor.Update<Table.Normalize<T>>;
18
- delete(): Cursor.Delete;
19
- createTable(): Cursor.CreateTable;
20
- as(alias: string): this;
21
- alias(): Record<string, this>;
22
- get(name: string): Expr<any>;
23
- toExpr(): Expr<Table.Normalize<T>>;
24
- schema(): Schema;
25
- }
26
- export declare namespace Table {
27
- type Intersection<A, B> = A & B extends infer U ? {
28
- [P in keyof U]: EV<U[P]>;
29
- } : never;
30
- type OptionalKeys<T> = {
31
- [K in keyof T]: null extends T[K] ? K : T[K] extends Column.IsPrimary<any, any> ? K : T[K] extends Column.IsOptional<any> ? K : never;
32
- }[keyof T];
33
- type RequiredKeys<T> = {
34
- [K in keyof T]: null extends T[K] ? never : T[K] extends Column.IsPrimary<any, any> ? never : T[K] extends Column.IsOptional<any> ? never : K;
35
- }[keyof T];
36
- type Optionals<T> = {
37
- [K in keyof T]?: T[K] extends Column.IsOptional<infer V> ? V : T[K] extends Column.IsPrimary<infer V, infer K> ? PrimaryKey<V, K> : T[K];
38
- };
39
- export type Insert<T> = Intersection<Optionals<Pick<T, OptionalKeys<T>>>, Pick<T, RequiredKeys<T>>>;
40
- export type Normalize<T> = {
41
- [K in keyof T]: T[K] extends Column.IsOptional<infer V> ? V : T[K] extends Column.IsPrimary<infer V, infer K> ? PrimaryKey<V, K> : T[K];
42
- };
43
- export type Infer<T> = T extends Table<infer U> ? Normalize<U> : never;
44
- export {};
45
- }
46
- export type TableOptions<T, R> = {
47
- name: string;
48
- alias?: string;
49
- columns: {
50
- [K in keyof T]: Column<T[K]>;
51
- };
52
- indexes?: (this: Fields<T>) => Record<string, Index>;
53
- };
54
- export declare function table<T extends {}, R>(options: TableOptions<T, R>): Table<T> & Fields<T>;
55
- export declare namespace table {
56
- export type infer<T> = Table.Infer<T>;
57
- type Extensions<T> = {
58
- [key: string]: (this: T, ...args: any[]) => any;
59
- };
60
- export function extend<T extends Table<any>, E extends Extensions<T>>(target: T, extensions: E): T & E;
61
- export {};
62
- }
package/dist/lib/Table.js DELETED
@@ -1,130 +0,0 @@
1
- // src/lib/Table.ts
2
- import { Cursor } from "./Cursor.js";
3
- import { Expr, ExprData } from "./Expr.js";
4
- import { Query } from "./Query.js";
5
- import { Selection } from "./Selection.js";
6
- import { Target } from "./Target.js";
7
- var Table = class extends Cursor.SelectMultiple {
8
- [Selection.__tableType]() {
9
- throw "assert";
10
- }
11
- constructor(schema) {
12
- super(
13
- Query.Select({
14
- from: Target.Table(schema),
15
- selection: ExprData.Row(Target.Table(schema))
16
- })
17
- );
18
- Object.defineProperty(this, "schema", {
19
- enumerable: false,
20
- value: () => schema
21
- });
22
- if (schema.columns)
23
- for (const column of Object.keys(schema.columns)) {
24
- Object.defineProperty(this, column, {
25
- enumerable: true,
26
- get: () => this.get(column)
27
- });
28
- }
29
- return new Proxy(this, {
30
- get(target, key) {
31
- return key in target ? target[key] : target.get(key);
32
- }
33
- });
34
- }
35
- fetch(id) {
36
- return this.where(this.get("id").is(id)).first();
37
- }
38
- insertOne(record) {
39
- return new Cursor(
40
- Query.Insert({
41
- into: this.schema(),
42
- data: [record],
43
- selection: ExprData.Row(Target.Table(this.schema())),
44
- singleResult: true
45
- })
46
- );
47
- }
48
- insertAll(data) {
49
- return new Cursor.Insert(this.schema()).values(...data);
50
- }
51
- set(data) {
52
- return new Cursor.Update(
53
- Query.Update({
54
- table: this.schema()
55
- })
56
- ).set(data);
57
- }
58
- delete() {
59
- return new Cursor.Delete(Query.Delete({ table: this.schema() }));
60
- }
61
- createTable() {
62
- return new Cursor.CreateTable(this.schema());
63
- }
64
- as(alias) {
65
- return new Table({ ...this.schema(), alias });
66
- }
67
- alias() {
68
- return new Proxy(
69
- {},
70
- {
71
- get: (_, key) => {
72
- return this.as(key);
73
- }
74
- }
75
- );
76
- }
77
- get(name) {
78
- return new Expr(ExprData.Field(this.toExpr().expr, name));
79
- }
80
- toExpr() {
81
- return new Expr(
82
- ExprData.Row(Target.Table(this.schema()))
83
- );
84
- }
85
- schema() {
86
- throw new Error("Not implemented");
87
- }
88
- };
89
- function table(options) {
90
- const schema = {
91
- ...options,
92
- columns: Object.fromEntries(
93
- Object.entries(options.columns).map(([key, column]) => {
94
- const { data } = column;
95
- return [key, { ...data, name: data.name || key }];
96
- })
97
- ),
98
- indexes: {}
99
- };
100
- const indexes = Object.fromEntries(
101
- Object.entries(
102
- options.indexes ? options.indexes.call(
103
- new Expr(ExprData.Row(Target.Table(schema)))
104
- ) : {}
105
- ).map(([key, index]) => {
106
- const indexName = `${schema.name}.${key}`;
107
- return [indexName, { name: indexName, ...index.data }];
108
- })
109
- );
110
- return new Table({
111
- ...schema,
112
- indexes
113
- });
114
- }
115
- ((table2) => {
116
- function extend(target, extensions) {
117
- return new Proxy(target, {
118
- get(target2, key) {
119
- if (key === "as")
120
- return (...args) => extend(target2.as(...args), extensions);
121
- return key in extensions ? extensions[key].bind(target2) : target2[key];
122
- }
123
- });
124
- }
125
- table2.extend = extend;
126
- })(table || (table = {}));
127
- export {
128
- Table,
129
- table
130
- };
@@ -1,38 +0,0 @@
1
- import { ExprData } from './Expr';
2
- import { Query as QueryData } from './Query';
3
- import { Schema } from './Schema';
4
- export declare enum TargetType {
5
- Expr = "Expr",
6
- Query = "Query",
7
- Table = "Table",
8
- Join = "Join"
9
- }
10
- export type Target = Target.Expr | Target.Query | Target.Table | Target.Join;
11
- export declare namespace Target {
12
- interface Expr {
13
- type: TargetType.Expr;
14
- expr: ExprData;
15
- alias?: string;
16
- }
17
- function Expr(expr: ExprData, alias?: string): Expr;
18
- interface Query {
19
- type: TargetType.Query;
20
- query: QueryData;
21
- alias?: string;
22
- }
23
- function Query(query: QueryData, alias?: string): Query;
24
- interface Table {
25
- type: TargetType.Table;
26
- table: Schema;
27
- }
28
- function Table(table: Schema): Table;
29
- interface Join {
30
- type: TargetType.Join;
31
- left: Target;
32
- right: Target;
33
- joinType: 'left' | 'inner';
34
- on: ExprData;
35
- }
36
- function Join(left: Target, right: Target, joinType: 'left' | 'inner', on: ExprData): Join;
37
- function source(from: Target): Schema | undefined;
38
- }
@@ -1,42 +0,0 @@
1
- // src/lib/Target.ts
2
- var TargetType = /* @__PURE__ */ ((TargetType2) => {
3
- TargetType2["Expr"] = "Expr";
4
- TargetType2["Query"] = "Query";
5
- TargetType2["Table"] = "Table";
6
- TargetType2["Join"] = "Join";
7
- return TargetType2;
8
- })(TargetType || {});
9
- var Target;
10
- ((Target2) => {
11
- function Expr(expr, alias) {
12
- return { type: "Expr" /* Expr */, expr, alias };
13
- }
14
- Target2.Expr = Expr;
15
- function Query(query, alias) {
16
- return { type: "Query" /* Query */, query, alias };
17
- }
18
- Target2.Query = Query;
19
- function Table(table) {
20
- return { type: "Table" /* Table */, table };
21
- }
22
- Target2.Table = Table;
23
- function Join(left, right, joinType, on) {
24
- return { type: "Join" /* Join */, left, right, joinType, on };
25
- }
26
- Target2.Join = Join;
27
- function source(from) {
28
- switch (from.type) {
29
- case "Table" /* Table */:
30
- return from.table;
31
- case "Join" /* Join */:
32
- return source(from.left);
33
- default:
34
- return void 0;
35
- }
36
- }
37
- Target2.source = source;
38
- })(Target || (Target = {}));
39
- export {
40
- Target,
41
- TargetType
42
- };
@@ -1,4 +0,0 @@
1
- import type { EV } from './Expr';
2
- export type Update<Row> = Partial<{
3
- [K in keyof Row]: EV<Row[K]>;
4
- }>;
File without changes