@mikro-orm/better-sqlite 6.0.0-dev.99 → 6.0.0-rc.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.
@@ -132,7 +132,7 @@ class BetterSqliteConnection extends knex_1.AbstractSqlConnection {
132
132
  }
133
133
  isRunQuery(query) {
134
134
  query = query.trim().toLowerCase();
135
- if (query.startsWith('insert into') && query.includes(' returning ')) {
135
+ if ((query.startsWith('insert into') || query.startsWith('update ')) && query.includes(' returning ')) {
136
136
  return false;
137
137
  }
138
138
  return query.startsWith('insert into') ||
@@ -143,7 +143,7 @@ class BetterSqliteConnection extends knex_1.AbstractSqlConnection {
143
143
  getCallMethod(obj) {
144
144
  if (obj.method === 'raw') {
145
145
  const query = obj.sql.trim().toLowerCase();
146
- if (query.startsWith('insert into') && query.includes(' returning ')) {
146
+ if ((query.startsWith('insert into') || query.startsWith('update ')) && query.includes(' returning ')) {
147
147
  return 'all';
148
148
  }
149
149
  if (this.isRunQuery(query)) {
@@ -5,9 +5,10 @@ export declare class BetterSqliteSchemaHelper extends SchemaHelper {
5
5
  enableForeignKeysSQL(): string;
6
6
  supportsSchemaConstraints(): boolean;
7
7
  getListTablesSQL(): string;
8
+ private parseTableDefinition;
8
9
  getColumns(connection: AbstractSqlConnection, tableName: string, schemaName?: string): Promise<any[]>;
9
10
  getEnumDefinitions(connection: AbstractSqlConnection, checks: CheckDef[], tableName: string, schemaName: string): Promise<Dictionary<string[]>>;
10
- getPrimaryKeys(connection: AbstractSqlConnection, indexes: IndexDef[] | undefined, tableName: string, schemaName?: string): Promise<string[]>;
11
+ getPrimaryKeys(connection: AbstractSqlConnection, indexes: IndexDef[], tableName: string, schemaName?: string): Promise<string[]>;
11
12
  getIndexes(connection: AbstractSqlConnection, tableName: string, schemaName?: string): Promise<IndexDef[]>;
12
13
  getChecks(connection: AbstractSqlConnection, tableName: string, schemaName?: string): Promise<CheckDef[]>;
13
14
  getForeignKeysSQL(tableName: string): string;
@@ -16,15 +16,44 @@ class BetterSqliteSchemaHelper extends knex_1.SchemaHelper {
16
16
  return `select name as table_name from sqlite_master where type = 'table' and name != 'sqlite_sequence' and name != 'geometry_columns' and name != 'spatial_ref_sys' `
17
17
  + `union all select name as table_name from sqlite_temp_master where type = 'table' order by name`;
18
18
  }
19
+ parseTableDefinition(sql, cols) {
20
+ const columns = {};
21
+ // extract all columns definitions
22
+ let columnsDef = sql.replaceAll('\n', '').match(new RegExp(`create table [\`"']?.*?[\`"']? \\((.*)\\)`, 'i'))?.[1];
23
+ /* istanbul ignore else */
24
+ if (columnsDef) {
25
+ for (let i = cols.length - 1; i >= 0; i--) {
26
+ const col = cols[i];
27
+ const re = ` *, *[\`"']?${col.name}[\`"']? (.*)`;
28
+ const columnDef = columnsDef.match(new RegExp(re, 'i'));
29
+ /* istanbul ignore else */
30
+ if (columnDef) {
31
+ columns[col.name] = { name: col.name, definition: columnDef[1] };
32
+ columnsDef = columnsDef.substring(0, columnDef.index);
33
+ }
34
+ }
35
+ }
36
+ return columns;
37
+ }
19
38
  async getColumns(connection, tableName, schemaName) {
20
- const columns = await connection.execute(`pragma table_info('${tableName}')`);
39
+ const columns = await connection.execute(`pragma table_xinfo('${tableName}')`);
21
40
  const sql = `select sql from sqlite_master where type = ? and name = ?`;
22
41
  const tableDefinition = await connection.execute(sql, ['table', tableName], 'get');
23
42
  const composite = columns.reduce((count, col) => count + (col.pk ? 1 : 0), 0) > 1;
24
43
  // there can be only one, so naive check like this should be enough
25
44
  const hasAutoincrement = tableDefinition.sql.toLowerCase().includes('autoincrement');
45
+ const columnDefinitions = this.parseTableDefinition(tableDefinition.sql, columns);
26
46
  return columns.map(col => {
27
47
  const mappedType = connection.getPlatform().getMappedType(col.type);
48
+ let generated;
49
+ if (col.hidden > 1) {
50
+ const storage = col.hidden === 2 ? 'virtual' : 'stored';
51
+ const re = `(generated always)? as \\((.*)\\)( ${storage})?$`;
52
+ const match = columnDefinitions[col.name].definition.match(re);
53
+ if (match) {
54
+ generated = `${match[2]} ${storage}`;
55
+ }
56
+ }
28
57
  return {
29
58
  name: col.name,
30
59
  type: col.type,
@@ -34,6 +63,7 @@ class BetterSqliteSchemaHelper extends knex_1.SchemaHelper {
34
63
  mappedType,
35
64
  unsigned: false,
36
65
  autoincrement: !composite && col.pk && this.platform.isNumericColumn(mappedType) && hasAutoincrement,
66
+ generated,
37
67
  };
38
68
  });
39
69
  }
@@ -52,7 +82,7 @@ class BetterSqliteSchemaHelper extends knex_1.SchemaHelper {
52
82
  return o;
53
83
  }, {});
54
84
  }
55
- async getPrimaryKeys(connection, indexes = [], tableName, schemaName) {
85
+ async getPrimaryKeys(connection, indexes, tableName, schemaName) {
56
86
  const sql = `pragma table_info(\`${tableName}\`)`;
57
87
  const cols = await connection.execute(sql);
58
88
  return cols.filter(col => !!col.pk).map(col => col.name);
@@ -66,6 +96,7 @@ class BetterSqliteSchemaHelper extends knex_1.SchemaHelper {
66
96
  ret.push({
67
97
  columnNames: [col.name],
68
98
  keyName: 'primary',
99
+ constraint: true,
69
100
  unique: true,
70
101
  primary: true,
71
102
  });
@@ -76,6 +107,7 @@ class BetterSqliteSchemaHelper extends knex_1.SchemaHelper {
76
107
  columnNames: [row.name],
77
108
  keyName: index.name,
78
109
  unique: !!index.unique,
110
+ constraint: !!index.unique,
79
111
  primary: false,
80
112
  })));
81
113
  }
package/index.mjs CHANGED
@@ -52,6 +52,8 @@ export const DatabaseObjectExistsException = mod.DatabaseObjectExistsException;
52
52
  export const DatabaseObjectNotFoundException = mod.DatabaseObjectNotFoundException;
53
53
  export const DatabaseSchema = mod.DatabaseSchema;
54
54
  export const DatabaseTable = mod.DatabaseTable;
55
+ export const Dataloader = mod.Dataloader;
56
+ export const DataloaderUtils = mod.DataloaderUtils;
55
57
  export const DateTimeType = mod.DateTimeType;
56
58
  export const DateType = mod.DateType;
57
59
  export const DeadlockException = mod.DeadlockException;
@@ -102,6 +104,7 @@ export const Index = mod.Index;
102
104
  export const IntegerType = mod.IntegerType;
103
105
  export const InvalidFieldNameException = mod.InvalidFieldNameException;
104
106
  export const IsolationLevel = mod.IsolationLevel;
107
+ export const JoinType = mod.JoinType;
105
108
  export const JsonProperty = mod.JsonProperty;
106
109
  export const JsonType = mod.JsonType;
107
110
  export const Knex = mod.Knex;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mikro-orm/better-sqlite",
3
- "version": "6.0.0-dev.99",
3
+ "version": "6.0.0-rc.1",
4
4
  "description": "TypeScript ORM for Node.js based on Data Mapper, Unit of Work and Identity Map patterns. Supports MongoDB, MySQL, PostgreSQL and SQLite databases as well as usage with vanilla JavaScript.",
5
5
  "main": "index.js",
6
6
  "module": "index.mjs",
@@ -58,15 +58,15 @@
58
58
  "access": "public"
59
59
  },
60
60
  "dependencies": {
61
- "@mikro-orm/knex": "~6.0.0-dev.99",
62
- "better-sqlite3": "8.6.0",
61
+ "@mikro-orm/knex": "6.0.0-rc.1",
62
+ "better-sqlite3": "8.7.0",
63
63
  "fs-extra": "11.1.1",
64
64
  "sqlstring-sqlite": "0.1.1"
65
65
  },
66
66
  "devDependencies": {
67
- "@mikro-orm/core": "^5.8.1"
67
+ "@mikro-orm/core": "^5.9.2"
68
68
  },
69
69
  "peerDependencies": {
70
- "@mikro-orm/core": "~6.0.0-dev.99"
70
+ "@mikro-orm/core": "6.0.0-rc.1"
71
71
  }
72
72
  }