Skip to content

Commit

Permalink
fix: adapt facebook hermes or metro bundler for loop (#374)
Browse files Browse the repository at this point in the history
* fix: adapt facebook hermes or metro bundler for loop

* fix: type definitions fix

Co-authored-by: JimmyDaddy <[email protected]>
  • Loading branch information
JimmyDaddy and JimmyDaddy authored Dec 27, 2022
1 parent ea005ba commit 6ec38b2
Show file tree
Hide file tree
Showing 9 changed files with 120 additions and 40 deletions.
12 changes: 5 additions & 7 deletions src/adapters/sequelize.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { AbstractBone } from '../types/abstract_bone';
import { Spell } from '../spell';

type WhereConditions<T extends typeof SequelizeBone> = {
[Property in keyof Extract<InstanceType<T>, Literal>]?: Literal | Literal[] | OperatorCondition;
[Property in BoneColumns<T>]?: Literal | Literal[] | OperatorCondition;
} | {
[key in '$and' | '$or']?: WhereConditions<T>[];
}
Expand Down Expand Up @@ -41,9 +41,7 @@ interface SequelizeConditions<T extends typeof SequelizeBone> extends BaseSequel
}

interface FindOrCreateOptions<T extends typeof SequelizeBone> extends BaseSequelizeConditions<T> {
defaults?: {
[Property in keyof Extract<InstanceType<T>, Literal>]?: Literal
}
defaults?: BoneCreateValues<T>;
}

interface FindOrBuildOptions<T extends typeof SequelizeBone> extends FindOrCreateOptions<T> {
Expand Down Expand Up @@ -130,19 +128,19 @@ export class SequelizeBone extends AbstractBone {
*/
static bulkBuild<T extends typeof SequelizeBone>(this:T, valueSets: Array<BoneCreateValues<T>>, options?: BoneOptions): Array<InstanceType<T>>;

static count<T extends typeof SequelizeBone>(this: T, conditions?: SequelizeConditions<T>): Spell<T, ResultSet<T> | number>;
static count<T extends typeof SequelizeBone>(this: T, name?: BoneColumns<T>): Spell<T, ResultSet<T> | number>;
static count<T extends typeof SequelizeBone>(this: T, name?: Raw | '*'): Spell<T, ResultSet<T> | number>;
static count<T extends typeof SequelizeBone>(this: T, conditions?: SequelizeConditions<T>): Spell<T, ResultSet<T> | number>;

static decrement<T extends typeof SequelizeBone>(
this: T,
fields: { [Property in keyof Extract<InstanceType<T>, Literal>]?: number } | string | Array<BoneColumns<T> | string> ,
fields: { [Property in BoneColumns<T>]?: number } | string | Array<BoneColumns<T> | string> ,
options?: SequelizeConditions<T>
): Spell<T, QueryResult>;

static increment<T extends typeof SequelizeBone>(
this: T,
fields: { [Property in keyof Extract<InstanceType<T>, Literal>]?: number } | string | Array<BoneColumns<T> | string> ,
fields: { [Property in BoneColumns<T>]?: number } | string | Array<BoneColumns<T> | string> ,
options?: SequelizeConditions<T>
): Spell<T, QueryResult>;

Expand Down
12 changes: 7 additions & 5 deletions src/collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,16 +98,18 @@ function dispatch(spell, rows, fields) {

const results = new Collection();
for (const row of rows) {
const result = {};
for (const prop in row) {
const result = Object.keys(row).reduce((res, prop) => {
const data = row[prop];
const qualifier = prop === table ? tableAlias : prop;
if (qualifier === '' || qualifier === tableAlias) {
Object.assign(result, data);
Object.assign(res, data);
} else {
if (Object.values(data).some(value => value != null)) result[prop] = data;
if (Object.values(data).some(value => value != null)) {
res[prop] = data;
}
}
}
return res;
}, {});
let current;
if (joined && result[primaryColumn] != null) {
current = results.find(r => r[primaryKey] == result[primaryColumn]);
Expand Down
13 changes: 8 additions & 5 deletions src/spell.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {
Literal, command, Raw, Connection,
ResultSet, QueryResult,
QueryOptions, SetOptions, WithOptions,
Collection, WhereConditions, OrderOptions, BoneColumns,
Collection, WhereConditions, OrderOptions, BoneColumns, OnConditions,
} from './types/common';
import { AbstractBone } from './types/abstract_bone';
import { Hint, IndexHint, CommonHintsArgs, HintInterface } from './hint';
Expand Down Expand Up @@ -92,6 +92,9 @@ export class Spellbook {
export class Spell<T extends typeof AbstractBone, U = InstanceType<T> | Collection<InstanceType<T>> | ResultSet<T> | number | null> extends Promise<U> {
constructor(Model: T, opts: SpellOptions);

Model: T;
connection: Connection;

command: string;
scopes: Function[];

Expand All @@ -107,7 +110,7 @@ export class Spell<T extends typeof AbstractBone, U = InstanceType<T> | Collecti
with(...qualifiers: string[]): Spell<T, U>;

join<V extends typeof AbstractBone>(Model: V, onConditions: string, ...values: Literal[]): Spell<T, U>;
join<V extends typeof AbstractBone>(Model: V, onConditions: WhereConditions<T>): Spell<T, U>;
join<V extends typeof AbstractBone>(Model: V, onConditions: OnConditions<T>): Spell<T, U>;

$where(conditions: WhereConditions<T>): this;
$where(conditions: string, ...values: Literal[]): this;
Expand Down Expand Up @@ -165,9 +168,9 @@ export class Spell<T extends typeof AbstractBone, U = InstanceType<T> | Collecti
toString(): string;

all: Spell<T, U>;
first: Spell<T, InstanceType<T>>;
last: Spell<T, InstanceType<T>>;
get(index: number): Spell<T, InstanceType<T>>;
first: Spell<T, InstanceType<T> | null>;
last: Spell<T, InstanceType<T> | null>;
get(index: number): Spell<T, InstanceType<T> | null>;

unscoped: Spell<T, U>;
unparanoid: Spell<T, U>;
Expand Down
10 changes: 5 additions & 5 deletions src/types/abstract_bone.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
Pool, Literal, WhereConditions,
Collection, ResultSet, OrderOptions,
QueryOptions, AttributeMeta, AssociateOptions, Values, Connection, BulkCreateOptions, BoneCreateValues,
GeneratorReturnType, BoneColumns, InstanceColumns, Raw,
GeneratorReturnType, BoneColumns, InstanceColumns, Raw, OnConditions,
} from './common';
import { AbstractDriver } from '../drivers';
import { Spell } from '../spell';
Expand Down Expand Up @@ -110,7 +110,7 @@ export class AbstractBone {
static renameAttribute<T extends typeof AbstractBone>(this: T, originalName: string, newName: string): void;

static alias<T extends typeof AbstractBone>(this: T, name: BoneColumns<T>): string;
static alias<T extends typeof AbstractBone>(this: T, data: Record<BoneColumns<T>, Literal>): Record<string, Literal>;
static alias<T extends typeof AbstractBone>(this: T, data: { [key in BoneColumns<T>]: Literal }): Record<string, Literal>;
static unalias<T extends typeof AbstractBone>(this: T, name: BoneColumns<T>): string;

// after alias/unalias
Expand All @@ -127,7 +127,7 @@ export class AbstractBone {
* @example
* Bone.create({ foo: 1, bar: 'baz' })
*/
static create<T extends typeof AbstractBone>(this: T, values: BoneCreateValues<T> & Partial<Record<BoneColumns<T>, Literal>>, options?: QueryOptions): Promise<InstanceType<T>>;
static create<T extends typeof AbstractBone>(this: T, values: BoneCreateValues<T>, options?: QueryOptions): Promise<InstanceType<T>>;

/**
* INSERT or UPDATE rows
Expand Down Expand Up @@ -192,8 +192,8 @@ export class AbstractBone {
* @example
* Bone.join(Muscle, 'bones.id == muscles.boneId')
*/
static join<T extends typeof AbstractBone>(this: T, Model: AbstractBone, onConditions: WhereConditions<T>): Spell<T, Collection<InstanceType<T>>>;
static join<T extends typeof AbstractBone>(this: T, Model: AbstractBone, onConditions: string, ...values: Literal[]): Spell<T, Collection<InstanceType<T>>>;
static join<T extends typeof AbstractBone, U extends typeof AbstractBone>(this: T, Model: U, onConditions: OnConditions<T>): Spell<T, Collection<InstanceType<T>>>;
static join<T extends typeof AbstractBone, U extends typeof AbstractBone>(this: T, Model: U, onConditions: string, ...values: Literal[]): Spell<T, Collection<InstanceType<T>>>;

/**
* Set WHERE conditions
Expand Down
31 changes: 25 additions & 6 deletions src/types/common.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ export interface QueryResult {
fields?: Array<{ table: string, name: string }>,
}

interface TransactionMethodOptions {
Model: typeof AbstractBone;
}

export interface Connection {
/**
* MySQL
Expand All @@ -44,6 +48,13 @@ export interface Connection {
values?: Array<Literal | Literal[]>,
callback?: (err: Error, result: QueryResult) => void,
): void

begin(opts: TransactionMethodOptions): Promise<void>;

commit(opts: TransactionMethodOptions): Promise<void>;

rollback(opts: TransactionMethodOptions): Promise<void>;

}

export declare class Pool {
Expand All @@ -65,7 +76,7 @@ export interface QueryOptions {
}

export type BulkCreateOptions = QueryOptions & {
updateOnDuplicate?: string[];
updateOnDuplicate?: string[] | true;
fields?: string[];
}

Expand Down Expand Up @@ -147,7 +158,7 @@ export class Raw {
}

export type SetOptions<T extends typeof AbstractBone> = {
[Property in keyof Extract<InstanceType<T>, Literal>]: Literal
[Property in BoneColumns<T>]: Literal
} | {
[key: string]: Literal
};
Expand All @@ -156,10 +167,12 @@ export type WithOptions = {
[qualifier: string]: { select: string | string[], throughRelation?: string }
}

type OrderSortType = 'desc' | 'asc' | Uppercase<'desc' | 'asc'>;

type OrderOptions<T extends typeof AbstractBone> = {
[key in keyof Extract<InstanceType<T>, Literal>]?: 'desc' | 'asc'
} | [ BoneColumns<T>, 'desc' | 'asc' ]
| Array<BoneColumns<T> | [ BoneColumns<T>, 'desc' | 'asc' ] | Raw | string | Array<Raw | string>>
[Property in Extract<BoneColumns<T>, Literal>]?: OrderSortType
} | [ BoneColumns<T>, OrderSortType ]
| Array<BoneColumns<T> | [ BoneColumns<T>, OrderSortType ] | Raw | string | Array<Raw | string>>
| string | Raw;

export class Collection<T extends AbstractBone> extends Array<T> {
Expand All @@ -169,7 +182,13 @@ export class Collection<T extends AbstractBone> extends Array<T> {
}

export type WhereConditions<T extends typeof AbstractBone> = {
[Property in keyof Extract<InstanceType<T>, Literal>]?: Literal | Literal[] | OperatorCondition;
[Property in BoneColumns<T>]?: Literal | Literal[] | OperatorCondition;
} | {
[key in '$and' | '$or']?: WhereConditions<T>[];
}

export type OnConditions<T extends typeof AbstractBone> = {
[Property in BoneColumns<T>]?: Literal | Literal[] | OperatorCondition ;
} | {
[key in '$and' | '$or']?: WhereConditions<T>[];
}
Expand Down
6 changes: 3 additions & 3 deletions test/types/basics.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -341,17 +341,17 @@ describe('=> Basics (TypeScript)', function() {

it('first', async () => {
const post1 = await Post.find().first;
assert.equal(post1.title, 'Leah');
assert.equal(post1?.title, 'Leah');
});

it('last', async () => {
const post1 = await Post.find().last;
assert.equal(post1.title, 'Nephalem');
assert.equal(post1?.title, 'Nephalem');
});

it('get(index)', async () => {
const post1 = await Post.find().get(0);
assert.equal(post1.title, 'Leah');
assert.equal(post1?.title, 'Leah');
});

it('all', async () => {
Expand Down
14 changes: 7 additions & 7 deletions test/types/sequelize.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ describe('=> sequelize (TypeScript)', function() {
class Post extends SequelizeBone {
static table = 'articles';

@Column()
id: bigint;
@Column(DataTypes.BIGINT)
id: number;

@Column({ name: 'gmt_create' })
createdAt: Date;
Expand Down Expand Up @@ -269,8 +269,8 @@ describe('=> sequelize (TypeScript)', function() {
});

it('bone.save()', async function() {
await Post.create({ id: BigInt(1), title: 'Leah' });
const post = new Post({ id: BigInt(1), title: 'Diablo' });
await Post.create({ id: 1, title: 'Leah' });
const post = new Post({ id: 1, title: 'Diablo' });
await post.save();

const posts = await Post.all;
Expand Down Expand Up @@ -713,7 +713,7 @@ describe('=> sequelize (TypeScript)', function() {
const { id } = await Post.create({ title: 'Leah' });
const [ post, isNewRecord ] = await Post.findOrBuild({
where: { title: 'Tyrael' },
defaults: { isPrivate: 1 },
defaults: { isPrivate: true },
});
assert.notEqual(post.id, id);
assert.equal(post.id, null);
Expand Down Expand Up @@ -856,7 +856,7 @@ describe('=> sequelize (TypeScript)', function() {
title: 'Yhorm2',
},
order: {
id: 'asc',
id: 'ASC' as 'ASC',
},
}));

Expand Down Expand Up @@ -905,7 +905,7 @@ describe('=> sequelize (TypeScript)', function() {
title: 'Yhorm2',
},
order: {
id: 'asc',
id: 'ASC' as 'ASC',
},
}));

Expand Down
60 changes: 59 additions & 1 deletion test/types/spell.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,34 @@ import {
connect, INDEX_HINT_SCOPE_TYPE,
INDEX_HINT_TYPE, INDEX_HINT_SCOPE, Hint, IndexHint, Raw
} from '../..';
import { HasOne } from '../../src/decorators';

describe('=> Spell (TypeScript)', function() {
const { STRING, TEXT, TINYINT } = DataTypes;

class Attachment extends Bone {
@Column()
id: number;

@Column()
articleId: number;

@Column()
url: string;

@Column()
width: number;

@Column()
height: number;

@Column({
name: 'gmt_deleted',
})
deletedAt: Date;

}

class Post extends Bone {
static table = 'articles'

Expand Down Expand Up @@ -61,6 +86,23 @@ describe('=> Spell (TypeScript)', function() {

@Column(TEXT)
settings: string;

@HasOne({
foreignKey: 'articleId',
})
attachment: Attachment;
}

class Comment extends Bone {
@Column()
id: number;

@Column()
articleId: number;

@Column()
content: string;

}

before(async function() {
Expand All @@ -70,7 +112,7 @@ describe('=> Spell (TypeScript)', function() {
port: process.env.MYSQL_PORT,
user: 'root',
database: 'leoric',
models: [ Post ],
models: [ Post, Comment, Attachment ],
});
});

Expand Down Expand Up @@ -239,4 +281,20 @@ describe('=> Spell (TypeScript)', function() {
assert.equal(Post.find().where({ id: 1 }).orWhere(new Raw('id = 3')).toSqlString(), 'SELECT * FROM `articles` WHERE (`id` = 1 OR id = 3) AND `gmt_deleted` IS NULL');
});
});

describe('associations', () => {
it('predefined hasOne join', function() {
assert.equal(
Post.select('title', 'createdAt').with('attachment').toString(),
'SELECT `posts`.`title`, `posts`.`gmt_create`, `attachment`.* FROM `articles` AS `posts` LEFT JOIN `attachments` AS `attachment` ON `posts`.`id` = `attachment`.`article_id` AND `attachment`.`gmt_deleted` IS NULL WHERE `posts`.`gmt_deleted` IS NULL'
);
});

it('arbitrary join', function() {
assert.equal(
Post.join(Comment, 'comments.articleId = posts.id').toString(),
'SELECT `posts`.*, `comments`.* FROM `articles` AS `posts` LEFT JOIN `comments` AS `comments` ON `comments`.`article_id` = `posts`.`id` WHERE `posts`.`gmt_deleted` IS NULL'
);
});
})
});
2 changes: 1 addition & 1 deletion test/unit/bone.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const { Bone, DataTypes, connect, default: Realm } = require('../..');
const expect = require('expect.js');

const {
TINYINT, MEDIUMINT, BIGINT, INTEGER,
TINYINT, MEDIUMINT, BIGINT,
STRING,
DATE, VIRTUAL,
} = DataTypes;
Expand Down

0 comments on commit 6ec38b2

Please sign in to comment.