Skip to content

Commit

Permalink
feat(typeorm): update driver to work with latest typeorm version
Browse files Browse the repository at this point in the history
  • Loading branch information
mukaschultze committed Nov 22, 2023
1 parent a4fd527 commit ee32114
Show file tree
Hide file tree
Showing 9 changed files with 4,684 additions and 3,247 deletions.
2 changes: 1 addition & 1 deletion demo/app/main-view-model.ts
Expand Up @@ -2,7 +2,7 @@ import { Observable } from '@nativescript/core/data/observable';
import { SQLiteDatabase, deleteDatabase, openOrCreate } from '@nativescript-community/sqlite';
import { knownFolders, path } from '@nativescript/core/file-system';
import { ImageSource } from '@nativescript/core';
import { BaseEntity, Column, Connection, Entity, PrimaryGeneratedColumn, createConnection } from '@nativescript-community/typeorm/browser';
import { BaseEntity, Column, Connection, Entity, PrimaryGeneratedColumn, createConnection } from 'typeorm/browser';
import { installMixins } from '@nativescript-community/sqlite/typeorm';

interface DataExample {
Expand Down
4 changes: 2 additions & 2 deletions demo/package.json
@@ -1,9 +1,9 @@
{
"dependencies": {
"@nativescript-community/sqlite": "file:../plugin",
"@nativescript-community/typeorm": "0.2.2-5.1",
"@nativescript/core": "8.1.3",
"nativescript-theme-core": "^2.0.24"
"nativescript-theme-core": "^2.0.24",
"typeorm": "0.3.17"
},
"devDependencies": {
"@nativescript/ios": "8.1.0",
Expand Down
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -54,7 +54,7 @@
"@nano-sql/core": "^2.3.7",
"@nativescript-community/plugin-seed-tools": "file:tools",
"@nativescript-community/template-snippet": "file:demo-snippets",
"@nativescript-community/typeorm": "0.2.28-1"
"typeorm": "0.3.17"
},
"bootstrapper": "nativescript-plugin-seed",
"commitlint": {
Expand Down
2 changes: 1 addition & 1 deletion packages/sqlite/package.json
Expand Up @@ -38,7 +38,7 @@
"repository": "nativescript-community/sqlite",
"peerDependencies": {
"@nano-sql/core": "^2.3.7",
"@nativescript-community/typeorm": "0.2.28-1"
"typeorm": "^0.3.17"
},
"dependencies": {
"@nativescript/hook": "~2.0.0"
Expand Down
2 changes: 1 addition & 1 deletion src/sqlite/sqlitedatabase.android.ts
Expand Up @@ -146,7 +146,7 @@ const transactionRaw = async <T = any>(db: Db, action: (cancel?: () => void) =>
}
};

const messagePromises: { [key: string]: { resolve: Function; reject: Function; timeoutTimer: NodeJS.Timer }[] } = {};
const messagePromises: { [key: string]: { resolve: Function; reject: Function; timeoutTimer: ReturnType<typeof setTimeout> }[] } = {};

export class SQLiteDatabaseBase {
db: android.database.sqlite.SQLiteDatabase;
Expand Down
12 changes: 4 additions & 8 deletions src/sqlite/typeorm/NativescriptDriver.ts
@@ -1,12 +1,8 @@
import { Connection } from '@nativescript-community/typeorm/browser/connection/Connection';
import { AbstractSqliteDriver } from '@nativescript-community/typeorm/browser/driver/sqlite-abstract/AbstractSqliteDriver';
import { ColumnType } from '@nativescript-community/typeorm/browser/driver/types/ColumnTypes';
import { DriverOptionNotSetError } from '@nativescript-community/typeorm/browser/error/DriverOptionNotSetError';
import { DriverPackageNotInstalledError } from '@nativescript-community/typeorm/browser/error/DriverPackageNotInstalledError';
import { QueryRunner } from '@nativescript-community/typeorm/browser/query-runner/QueryRunner';
import { ColumnType, DataSource, DriverOptionNotSetError, DriverPackageNotInstalledError, QueryRunner } from 'typeorm/browser';
import { AbstractSqliteDriver } from 'typeorm/browser/driver/sqlite-abstract/AbstractSqliteDriver';
import * as NSQlite from '../sqlite';
import { NativescriptConnectionOptions } from './index';
import { NativescriptQueryRunner } from './NativescriptQueryRunner';
import { NativescriptConnectionOptions } from './index';
/**
* Organizes communication with sqlite DBMS within Nativescript.
*/
Expand All @@ -27,7 +23,7 @@ export class NativescriptDriver extends AbstractSqliteDriver {
// -------------------------------------------------------------------------
// Constructor
// -------------------------------------------------------------------------
constructor(connection: Connection) {
constructor(connection: DataSource) {
super(connection);
this.connection = connection;
this.options = connection.options as NativescriptConnectionOptions;
Expand Down
21 changes: 13 additions & 8 deletions src/sqlite/typeorm/NativescriptQueryRunner.ts
@@ -1,15 +1,13 @@
import { ObjectLiteral } from '@nativescript-community/typeorm/browser/common/ObjectLiteral';
import { QueryRunnerAlreadyReleasedError } from '@nativescript-community/typeorm/browser/error/QueryRunnerAlreadyReleasedError';
import { QueryFailedError } from '@nativescript-community/typeorm/browser/error/QueryFailedError';
import { AbstractSqliteQueryRunner } from '@nativescript-community/typeorm/browser/driver/sqlite-abstract/AbstractSqliteQueryRunner';
import { Broadcaster } from '@nativescript-community/typeorm/browser/subscriber/Broadcaster';
import { ObjectLiteral, QueryFailedError, QueryResult, QueryRunner, QueryRunnerAlreadyReleasedError } from 'typeorm/browser';
import { AbstractSqliteQueryRunner } from 'typeorm/browser/driver/sqlite-abstract/AbstractSqliteQueryRunner';
import { Broadcaster } from 'typeorm/browser/subscriber/Broadcaster';
import { NativescriptDriver } from './NativescriptDriver';
import * as NSQlite from '../sqlite';

/**
* Runs queries on a single sqlite database connection.
*/
export class NativescriptQueryRunner extends AbstractSqliteQueryRunner {
export class NativescriptQueryRunner extends AbstractSqliteQueryRunner implements QueryRunner {
/**
* Database driver used by connection.
*/
Expand All @@ -26,13 +24,15 @@ export class NativescriptQueryRunner extends AbstractSqliteQueryRunner {
/**
* Executes a given SQL query.
*/
async query(query: string, parameters?: any[]) {
async query(query: string, parameters?: any[], useStructuredResult?: boolean): Promise<any> {
if (this.isReleased) {
throw new QueryRunnerAlreadyReleasedError();
}

const connection = this.driver.connection;
const isInsertQuery = query.startsWith('INSERT INTO') || query.startsWith('UPDATE');
connection.logger.logQuery(query, parameters, this);

try {
const db: NSQlite.SQLiteDatabase = await this.connect();
const queryStartTime = +new Date();
Expand All @@ -46,7 +46,12 @@ export class NativescriptQueryRunner extends AbstractSqliteQueryRunner {
if (maxQueryExecutionTime && queryExecutionTime > maxQueryExecutionTime) {
connection.logger.logQuerySlow(queryExecutionTime, query, parameters, this);
}
return result;

if (useStructuredResult) {
return { records: result, raw: result } as QueryResult;
} else {
return result;
}
} catch (err) {
connection.logger.logQueryError(err, query, parameters, this);
throw new QueryFailedError(query, parameters, err);
Expand Down
22 changes: 15 additions & 7 deletions src/sqlite/typeorm/index.ts
@@ -1,21 +1,29 @@
import { Connection } from '@nativescript-community/typeorm/browser/connection/Connection';
import { BaseConnectionOptions } from '@nativescript-community/typeorm/browser/connection/BaseConnectionOptions';

import { DataSource } from 'typeorm/browser';
import { BaseDataSourceOptions } from 'typeorm/browser/data-source/BaseDataSourceOptions';
import { NativescriptDriver } from './NativescriptDriver';

export * from './NativescriptDriver';
export * from './NativescriptQueryRunner';

let installed = false;
export function installMixins() {
if (installed) {
return;
}
installed = true;
const DriverFactory = require('@nativescript-community/typeorm/browser/driver/DriverFactory').DriverFactory;
// eslint-disable-next-line @typescript-eslint/no-var-requires
const DriverFactory = require('typeorm/browser/driver/DriverFactory').DriverFactory;
const oldFunc = DriverFactory.prototype.create;

DriverFactory.prototype.create = function (connection: Connection) {
DriverFactory.prototype.create = function (connection: DataSource) {
const { type } = connection.options;

if (type === ('@nativescript-community/sqlite' as any)) {
console.warn('"@nativescript-community/sqlite" is not recognized as a valid sqlite driver by typeorm and will break some SQL queries. Please use "nativescript" instead.');
}

switch (type) {
case 'nativescript' as any:
case 'nativescript':
case '@nativescript-community/sqlite' as any:
return new NativescriptDriver(connection);
default:
Expand All @@ -27,7 +35,7 @@ export function installMixins() {
/**
* NativeScript-specific connection options.
*/
export interface NativescriptConnectionOptions extends BaseConnectionOptions {
export interface NativescriptConnectionOptions extends BaseDataSourceOptions {
/**
* Database type.
*/
Expand Down

0 comments on commit ee32114

Please sign in to comment.