rado 0.0.0 → 0.1.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.
@@ -1,35 +1,33 @@
1
1
  // src/driver/better-sqlite3.ts
2
2
  import { Driver } from "../Driver.js";
3
- import { QueryType } from "../Query.js";
4
3
  import { SqliteFormatter } from "../sqlite/SqliteFormatter.js";
4
+ import { SqliteSchema } from "../sqlite/SqliteSchema.js";
5
5
  var BetterSqlite3Driver = class extends Driver.Sync {
6
6
  constructor(db) {
7
- super();
7
+ super(new SqliteFormatter());
8
8
  this.db = db;
9
9
  }
10
- formatter = new SqliteFormatter();
11
- execute(query) {
12
- const [sql, params] = this.formatter.compile(query);
13
- const stmt = this.db.prepare(sql);
14
- if ("selection" in query) {
15
- const res = stmt.pluck().all(...params).map((item) => JSON.parse(item).result);
16
- if (query.singleResult)
17
- return res[0];
18
- return res;
19
- } else if (query.type === QueryType.Raw) {
20
- switch (query.expectedReturn) {
21
- case "row":
22
- return stmt.get(...params);
23
- case "rows":
24
- return stmt.all(...params);
25
- default:
26
- stmt.run(...params);
27
- return void 0;
28
- }
29
- } else {
30
- const { changes } = stmt.run(...params);
31
- return { rowsAffected: changes };
32
- }
10
+ rows([sql, params]) {
11
+ return this.db.prepare(sql).all(...params);
12
+ }
13
+ values([sql, params]) {
14
+ return this.db.prepare(sql).raw().all(...params);
15
+ }
16
+ execute([sql, params]) {
17
+ this.db.prepare(sql).run(...params);
18
+ }
19
+ mutate([sql, params]) {
20
+ const { changes } = this.db.prepare(sql).run(...params);
21
+ return { rowsAffected: changes };
22
+ }
23
+ schemaInstructions(tableName) {
24
+ const columnData = this.rows(
25
+ SqliteSchema.tableData(tableName).compile(this.formatter)
26
+ );
27
+ const indexData = this.rows(
28
+ SqliteSchema.indexData(tableName).compile(this.formatter)
29
+ );
30
+ return SqliteSchema.createInstructions(columnData, indexData);
33
31
  }
34
32
  export() {
35
33
  return this.db.serialize();
@@ -1,12 +1,17 @@
1
1
  import type { Database } from 'sql.js';
2
2
  import { Driver } from '../Driver';
3
- import { Query } from '../Query';
4
- import { SqliteFormatter } from '../sqlite/SqliteFormatter';
3
+ import { SchemaInstructions } from '../Schema';
4
+ import { Statement } from '../Statement';
5
5
  export declare class SqlJsDriver extends Driver.Sync {
6
- private db;
7
- formatter: SqliteFormatter;
6
+ db: Database;
8
7
  constructor(db: Database);
9
- execute(query: Query): any;
8
+ rows<T extends object = object>([sql, params]: Statement.Compiled): Array<T>;
9
+ values([sql, params]: Statement.Compiled): Array<Array<any>>;
10
+ execute([sql, params]: Statement.Compiled): void;
11
+ mutate([sql, params]: Statement.Compiled): {
12
+ rowsAffected: number;
13
+ };
14
+ schemaInstructions(tableName: string): SchemaInstructions | undefined;
10
15
  export(): Uint8Array;
11
16
  }
12
17
  export declare function connect(db: Database): SqlJsDriver;
@@ -1,44 +1,43 @@
1
1
  // src/driver/sql.js.ts
2
2
  import { Driver } from "../Driver.js";
3
- import { QueryType } from "../Query.js";
4
3
  import { SqliteFormatter } from "../sqlite/SqliteFormatter.js";
4
+ import { SqliteSchema } from "../sqlite/SqliteSchema.js";
5
5
  var SqlJsDriver = class extends Driver.Sync {
6
6
  constructor(db) {
7
- super();
7
+ super(new SqliteFormatter());
8
8
  this.db = db;
9
9
  }
10
- formatter = new SqliteFormatter();
11
- execute(query) {
12
- const [sql, params] = this.formatter.compile(query);
10
+ rows([sql, params]) {
13
11
  const stmt = this.db.prepare(sql);
14
- if ("selection" in query) {
15
- stmt.bind(params);
16
- const res = [];
17
- while (stmt.step()) {
18
- const row = stmt.get()[0];
19
- res.push(JSON.parse(row).result);
20
- }
21
- if (query.singleResult)
22
- return res[0];
23
- return res;
24
- } else if (query.type === QueryType.Raw) {
25
- switch (query.expectedReturn) {
26
- case "row":
27
- return stmt.getAsObject(params);
28
- case "rows":
29
- stmt.bind(params);
30
- const res = [];
31
- while (stmt.step())
32
- res.push(stmt.getAsObject());
33
- return res;
34
- default:
35
- stmt.run(params);
36
- return void 0;
37
- }
38
- } else {
39
- stmt.run(params);
40
- return { rowsAffected: this.db.getRowsModified() };
41
- }
12
+ stmt.bind(params);
13
+ const res = [];
14
+ while (stmt.step())
15
+ res.push(stmt.getAsObject());
16
+ return res;
17
+ }
18
+ values([sql, params]) {
19
+ const stmt = this.db.prepare(sql);
20
+ stmt.bind(params);
21
+ const res = [];
22
+ while (stmt.step())
23
+ res.push(stmt.get());
24
+ return res;
25
+ }
26
+ execute([sql, params]) {
27
+ this.db.prepare(sql).run(params);
28
+ }
29
+ mutate([sql, params]) {
30
+ this.db.prepare(sql).run(params);
31
+ return { rowsAffected: this.db.getRowsModified() };
32
+ }
33
+ schemaInstructions(tableName) {
34
+ const columnData = this.rows(
35
+ SqliteSchema.tableData(tableName).compile(this.formatter)
36
+ );
37
+ const indexData = this.rows(
38
+ SqliteSchema.indexData(tableName).compile(this.formatter)
39
+ );
40
+ return SqliteSchema.createInstructions(columnData, indexData);
42
41
  }
43
42
  export() {
44
43
  return this.db.export();
@@ -1,13 +1,20 @@
1
1
  import type { Database } from 'sqlite3';
2
2
  import { Driver } from '../Driver';
3
3
  import { Query } from '../Query';
4
- import { SqliteFormatter } from '../sqlite/SqliteFormatter';
4
+ import { SchemaInstructions } from '../Schema';
5
+ import { Statement } from '../Statement';
5
6
  export declare class Sqlite3Driver extends Driver.Async {
6
7
  private db;
7
- formatter: SqliteFormatter;
8
8
  lock: Promise<void> | undefined;
9
9
  constructor(db: Database);
10
- execute(query: Query): Promise<unknown>;
10
+ executeQuery<T>(query: Query<T>): Promise<T>;
11
+ rows<T extends object = object>([sql, params]: Statement.Compiled): Promise<Array<T>>;
12
+ values(stmt: Statement.Compiled): Promise<Array<Array<any>>>;
13
+ execute([sql, params]: Statement.Compiled): Promise<void>;
14
+ mutate([sql, params]: Statement.Compiled): Promise<{
15
+ rowsAffected: number;
16
+ }>;
17
+ schemaInstructions(tableName: string): Promise<SchemaInstructions | undefined>;
11
18
  isolate(): [connection: Driver.Async, release: () => Promise<void>];
12
19
  }
13
20
  export declare function connect(db: Database): Sqlite3Driver;
@@ -1,62 +1,63 @@
1
1
  // src/driver/sqlite3.ts
2
2
  import { Driver } from "../Driver.js";
3
- import { QueryType } from "../Query.js";
4
3
  import { SqliteFormatter } from "../sqlite/SqliteFormatter.js";
4
+ import { SqliteSchema } from "../sqlite/SqliteSchema.js";
5
5
  var Sqlite3Driver = class extends Driver.Async {
6
6
  constructor(db) {
7
- super();
7
+ super(new SqliteFormatter());
8
8
  this.db = db;
9
9
  }
10
- formatter = new SqliteFormatter();
11
10
  lock;
12
- async execute(query) {
11
+ async executeQuery(query) {
13
12
  await this.lock;
13
+ return super.executeQuery(query);
14
+ }
15
+ rows([sql, params]) {
16
+ return new Promise((resolve, reject) => {
17
+ const stmt = this.db.prepare(sql);
18
+ stmt.all(params, (err, rows) => {
19
+ if (err)
20
+ reject(err);
21
+ else
22
+ resolve(rows);
23
+ });
24
+ });
25
+ }
26
+ async values(stmt) {
27
+ const rows = await this.rows(stmt);
28
+ return rows.map(Object.values);
29
+ }
30
+ execute([sql, params]) {
14
31
  return new Promise((resolve, reject) => {
15
- const [sql, params] = this.formatter.compile(query);
16
32
  const stmt = this.db.prepare(sql);
17
- if ("selection" in query) {
18
- stmt.all(params, (err, rows) => {
19
- const res = rows.map((row) => JSON.parse(row.result).result);
20
- if (err)
21
- reject(err);
22
- else
23
- resolve(query.singleResult ? res[0] : res);
24
- });
25
- } else if (query.type === QueryType.Raw) {
26
- switch (query.expectedReturn) {
27
- case "row":
28
- return stmt.get(params, (err, row) => {
29
- if (err)
30
- reject(err);
31
- else
32
- resolve(row);
33
- });
34
- case "rows":
35
- return stmt.all(params, (err, rows) => {
36
- if (err)
37
- reject(err);
38
- else
39
- resolve(rows);
40
- });
41
- default:
42
- stmt.run(params, (err) => {
43
- if (err)
44
- reject(err);
45
- else
46
- resolve(void 0);
47
- });
48
- return void 0;
49
- }
50
- } else {
51
- stmt.run(params, function(err) {
52
- if (err)
53
- reject(err);
54
- else
55
- resolve({ rowsAffected: this.changes });
56
- });
57
- }
33
+ stmt.run(params, (err) => {
34
+ if (err)
35
+ reject(err);
36
+ else
37
+ resolve();
38
+ });
58
39
  });
59
40
  }
41
+ mutate([sql, params]) {
42
+ return new Promise((resolve, reject) => {
43
+ const stmt = this.db.prepare(sql);
44
+ stmt.run(params, function(err) {
45
+ if (err)
46
+ reject(err);
47
+ else
48
+ resolve({ rowsAffected: this.changes });
49
+ });
50
+ });
51
+ }
52
+ async schemaInstructions(tableName) {
53
+ const columnData = await this.rows(
54
+ SqliteSchema.tableData(tableName).compile(this.formatter)
55
+ );
56
+ const indexData = await this.rows(
57
+ SqliteSchema.indexData(tableName).compile(this.formatter)
58
+ );
59
+ return SqliteSchema.createInstructions(columnData, indexData);
60
+ }
60
61
  isolate() {
61
62
  const connection = new Sqlite3Driver(this.db);
62
63
  let release, trigger = new Promise((resolve) => {
package/dist/index.d.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  export * from './Column';
2
2
  export * from './Cursor';
3
+ export * from './Driver';
3
4
  export * from './Expr';
4
5
  export * from './Fields';
5
6
  export * from './Formatter';
@@ -9,6 +10,7 @@ export * from './OrderBy';
9
10
  export * from './Param';
10
11
  export * from './Query';
11
12
  export * from './Sanitizer';
13
+ export * from './Schema';
12
14
  export * from './Selection';
13
15
  export * from './Statement';
14
16
  export * from './Table';
package/dist/index.js CHANGED
@@ -1,6 +1,7 @@
1
1
  // src/index.ts
2
2
  export * from "./Column.js";
3
3
  export * from "./Cursor.js";
4
+ export * from "./Driver.js";
4
5
  export * from "./Expr.js";
5
6
  export * from "./Fields.js";
6
7
  export * from "./Formatter.js";
@@ -10,6 +11,7 @@ export * from "./OrderBy.js";
10
11
  export * from "./Param.js";
11
12
  export * from "./Query.js";
12
13
  export * from "./Sanitizer.js";
14
+ export * from "./Schema.js";
13
15
  export * from "./Selection.js";
14
16
  export * from "./Statement.js";
15
17
  export * from "./Table.js";
@@ -2,7 +2,7 @@ import { Formatter } from '../Formatter';
2
2
  import { Statement } from '../Statement';
3
3
  export declare class SqliteFormatter extends Formatter {
4
4
  escapeValue(value: any): string;
5
- escapeIdent(input: string): string;
5
+ escapeIdentifier(input: string): string;
6
6
  escapeString(input: string): string;
7
7
  formatSqlAccess(on: Statement, field: string): Statement;
8
8
  formatJsonAccess(on: Statement, field: string): Statement;
@@ -23,7 +23,7 @@ var SqliteFormatter = class extends Formatter {
23
23
  return this.escapeString(value);
24
24
  return "json(" + this.escapeString(JSON.stringify(value)) + ")";
25
25
  }
26
- escapeIdent(input) {
26
+ escapeIdentifier(input) {
27
27
  return escapeWithin(input, "`");
28
28
  }
29
29
  escapeString(input) {
@@ -0,0 +1,24 @@
1
+ import { SchemaInstructions } from '../Schema';
2
+ import { Statement } from '../Statement';
3
+ export declare namespace SqliteSchema {
4
+ type Column = {
5
+ cid: number;
6
+ name: string;
7
+ type: string;
8
+ notnull: number;
9
+ dflt_value: string | null;
10
+ pk: number;
11
+ };
12
+ interface Index {
13
+ type: 'index';
14
+ name: string;
15
+ tbl_name: string;
16
+ rootpage: number;
17
+ sql: string;
18
+ }
19
+ function tableData(tableName: string): Statement;
20
+ function indexData(tableName: string): Statement;
21
+ function createInstructions(columnData: Array<Column>, indexData: Array<Index>): SchemaInstructions | undefined;
22
+ function columnInstruction(column: Column): [string, string];
23
+ function indexInstruction(index: Index): [string, string];
24
+ }
@@ -0,0 +1,40 @@
1
+ // src/sqlite/SqliteSchema.ts
2
+ import { Statement, identifier } from "../Statement.js";
3
+ import { SqliteFormatter } from "./SqliteFormatter.js";
4
+ var SqliteSchema;
5
+ ((SqliteSchema2) => {
6
+ const formatter = new SqliteFormatter();
7
+ function tableData(tableName) {
8
+ return Statement.tag`select * from pragma_table_info(${tableName}) order by cid`;
9
+ }
10
+ SqliteSchema2.tableData = tableData;
11
+ function indexData(tableName) {
12
+ return Statement.tag`select * from sqlite_master where type='index' and tbl_name=${tableName}`;
13
+ }
14
+ SqliteSchema2.indexData = indexData;
15
+ function createInstructions(columnData, indexData2) {
16
+ if (columnData.length === 0 && indexData2.length === 0)
17
+ return void 0;
18
+ const columns = columnData.map(columnInstruction);
19
+ const indexes = indexData2.map(indexInstruction);
20
+ return {
21
+ columns: Object.fromEntries(columns),
22
+ indexes: Object.fromEntries(indexes)
23
+ };
24
+ }
25
+ SqliteSchema2.createInstructions = createInstructions;
26
+ function columnInstruction(column) {
27
+ return [
28
+ column.name,
29
+ identifier(column.name).add(column.type.toLowerCase()).addIf(column.pk === 1, "primary key").addIf(column.notnull === 1, "not null").addIf(column.dflt_value !== null, column.dflt_value).compile(formatter)[0]
30
+ ];
31
+ }
32
+ SqliteSchema2.columnInstruction = columnInstruction;
33
+ function indexInstruction(index) {
34
+ return [index.name, index.sql];
35
+ }
36
+ SqliteSchema2.indexInstruction = indexInstruction;
37
+ })(SqliteSchema || (SqliteSchema = {}));
38
+ export {
39
+ SqliteSchema
40
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "rado",
3
- "version": "0.0.0",
3
+ "version": "0.1.1",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "exports": {