Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update deprecated test connectors, Add whereNull, whereNotNull functionality #327

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions lib/connectors/mongodb-connector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,19 @@ export class MongoDBConnector implements Connector {
}, {});
}

if (queryDescription.whereNulls) {
wheres = queryDescription.whereNulls.reduce((prev, curr) => {
const mongoOperator = "$exists";

return {
...prev,
[curr.field]: {
[mongoOperator]: curr.notNull,
},
};
}, {});
}

let results: any[] = [];

switch (queryDescription.type) {
Expand Down
26 changes: 26 additions & 0 deletions lib/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -635,6 +635,32 @@ export class Model {

return this;
}

/** Add a `where` clause to your query that gets a record if field is null
*
* await Flight.whereNull("id").get();
*
*/
static whereNull<T extends ModelSchema>(
this: T,
field: string
) {
this._currentQuery.whereNull(this.formatFieldToDatabase(field) as string);
return this;
}

/** Add a `where` clause to your query that gets a record if field is not null
*
* await Flight.whereNotNull("id").get();
*
*/
static whereNotNull<T extends ModelSchema>(
this: T,
field: string
) {
this._currentQuery.whereNotNull(this.formatFieldToDatabase(field) as string);
return this;
}

/** Update one or multiple records. Also update `updated_at` if `timestamps` is `true`.
*
Expand Down
38 changes: 38 additions & 0 deletions lib/query-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ export type WhereInClause = {
possibleValues: FieldValue[];
};

export type WhereNullClause = {
field: string;
notNull: boolean;
}

export type OrderByClauses = {
[field: string]: OrderDirection;
};
Expand All @@ -48,6 +53,7 @@ export type QueryDescription = {
orderBy?: OrderByClauses;
groupBy?: string;
wheres?: WhereClause[];
whereNulls?: WhereNullClause[];
whereIn?: WhereInClause;
joins?: JoinClause[];
leftOuterJoins?: JoinClause[];
Expand Down Expand Up @@ -202,6 +208,38 @@ export class QueryBuilder {
return this;
}

whereNull(
field: string,
notNull = false,
) {
if (!this._query.whereNulls) {
this._query.whereNulls = [];
}

const existingWhereForFieldIndex = this._query.whereNulls.findIndex((where) =>
where.field === field
);

const whereNullClause: WhereNullClause = {
field,
notNull: notNull,
}

if (existingWhereForFieldIndex === -1) {
this._query.whereNulls.push(whereNullClause);
} else {
this._query.whereNulls[existingWhereForFieldIndex] = whereNullClause;
}

return this;
}

whereNotNull(
field: string
) {
return this.whereNull(field, true);
}

update(values: Values) {
this._query.type = "update";
this._query.values = values;
Expand Down
10 changes: 10 additions & 0 deletions lib/translators/sql-translator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,16 @@ export class SQLTranslator implements Translator {
});
}

if (query.whereNulls) {
query.whereNulls.forEach((whereNull) => {
if (whereNull.notNull) {
queryBuilder = queryBuilder.whereNotNull(whereNull.field);
} else {
queryBuilder = queryBuilder.whereNull(whereNull.field);
}
});
}

if (query.joins) {
query.joins.forEach((join) => {
queryBuilder = queryBuilder.join(
Expand Down
26 changes: 11 additions & 15 deletions tests/connection.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { config } from "https://deno.land/x/dotenv/mod.ts";
import { Database } from "../mod.ts";
import { Database, MySQLConnector, SQLite3Connector } from "../mod.ts";

const env = config();

Expand All @@ -16,25 +16,21 @@ const defaultSQLiteOptions = {
};

const getMySQLConnection = (options = {}, debug = true): Database => {
const connection: Database = new Database(
{ dialect: "mysql", debug },
{
...defaultMySQLOptions,
...options,
},
);
const connector = new MySQLConnector({
...defaultMySQLOptions,
...options
});
const connection: Database = new Database({ connector, debug })

return connection;
};

const getSQLiteConnection = (options = {}, debug = true): Database => {
const connection: Database = new Database(
{ dialect: "sqlite3", debug },
{
...defaultSQLiteOptions,
...options,
},
);
const connector = new SQLite3Connector({
...defaultSQLiteOptions,
...options
});
const connection: Database = new Database({ connector, debug });

return connection;
};
Expand Down
42 changes: 42 additions & 0 deletions tests/units/queries/sqlite/response.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,3 +107,45 @@ Deno.test("SQLite: Response model", async () => {

await connection.close();
});

Deno.test("SQLite: Response model, Query Null Fields", async () => {
const connection = getSQLiteConnection();
connection.link([Article]);
await connection.sync({ drop: true });

await Article.create([
{ title: "hola" },
{ title: "hola mundo!", content: "not the first article!" },
]);

const selectNullFieldResponse = await Article.whereNull('content').all();

assertEquals(
selectNullFieldResponse.length,
1,
"Select expected only one record"
);

const selectNullFieldResponseChain = await Article.where('title', 'hola').whereNull('content').all();

assertEquals(
selectNullFieldResponseChain.length,
1,
"Select expected only one record"
);

assertEquals(
selectNullFieldResponse[0].title,
'hola',
"Select expected record with null content"
)

const selectNotNullFieldResponseChain = await Article.where('title', 'hola').whereNotNull('content').all();
assertEquals(
selectNotNullFieldResponseChain.length,
0,
"Select expected no record"
);

await connection.close();
});