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

feat(core): add support for indexes on JSON properties #4735

Merged
merged 1 commit into from
Sep 23, 2023
Merged

Conversation

B4nan
Copy link
Member

@B4nan B4nan commented Sep 23, 2023

To create an index on a JSON property, use an entity-level @Index() decorator with a dot path:

@Entity()
@Index({ properties: 'metaData.foo' })
@Index({ properties: ['metaData.foo', 'metaData.bar'] }) // compound index
export class Book {

  @Property({ type: 'json', nullable: true })
  metaData?: { foo: string; bar: number };

}

In PostgreSQL, this will generate a query like the following:

create index "book_meta_data_foo_index" on "book" (("meta_data"->>'foo'));

To create a unique index, use the @Unique() decorator:

@Entity()
@Unique({ properties: 'metaData.foo' })
@Unique({ properties: ['metaData.foo', 'metaData.bar'] }) // compound unique index
export class Book {

  @Property({ type: 'json', nullable: true })
  metaData?: { foo: string; bar: number };

}

In MySQL, you can also set the type explicitly:

@Entity()
@Index({ properties: 'metaData.foo', options: { returning: 'char(200)' } })
export class Book {

  @Property({ type: 'json', nullable: true })
  metaData?: { foo: string; bar: number };

}

This will generate a query like the following:

alter table `book`
  add index `book_meta_data_foo_index`((json_value(`meta_data`, '$.foo' returning char(200))));

MariaDB driver does not support this feature.

Closes #1230

To create an index on a JSON property, use an entity-level `@Index()` decorator with a dot path:

```ts
@entity()
@Index({ properties: 'metaData.foo' })
@Index({ properties: ['metaData.foo', 'metaData.bar'] }) // compound index
export class Book {

  @Property({ type: 'json', nullable: true })
  metaData?: { foo: string; bar: number };

}
```

In PostgreSQL, this will generate a query like the following:

```sql
create index "book_meta_data_foo_index" on "book" (("meta_data"->>'foo'));
```

To create a unique index, use the `@Unique()` decorator:

```ts
@entity()
@unique({ properties: 'metaData.foo' })
@unique({ properties: ['metaData.foo', 'metaData.bar'] }) // compound unique index
export class Book {

  @Property({ type: 'json', nullable: true })
  metaData?: { foo: string; bar: number };

}
```

In MySQL, you can also set the type explicitly:

```ts
@entity()
@Index({ properties: 'metaData.foo', options: { returning: 'char(200)' } })
export class Book {

  @Property({ type: 'json', nullable: true })
  metaData?: { foo: string; bar: number };

}
```

This will generate a query like the following:

```sql
alter table `book`
  add index `book_meta_data_foo_index`((json_value(`meta_data`, '$.foo' returning char(200))));
```

> MariaDB driver does not support this feature.

Closes #1230
@B4nan B4nan merged commit 6c09d9f into v6 Sep 23, 2023
6 of 8 checks passed
@B4nan B4nan deleted the json-index branch September 23, 2023 16:16
@B4nan B4nan mentioned this pull request Sep 23, 2023
B4nan added a commit that referenced this pull request Sep 24, 2023
To create an index on a JSON property, use an entity-level `@Index()`
decorator with a dot path:

```ts
@entity()
@Index({ properties: 'metaData.foo' })
@Index({ properties: ['metaData.foo', 'metaData.bar'] }) // compound index
export class Book {

  @Property({ type: 'json', nullable: true })
  metaData?: { foo: string; bar: number };

}
```

In PostgreSQL, this will generate a query like the following:

```sql
create index "book_meta_data_foo_index" on "book" (("meta_data"->>'foo'));
```

To create a unique index, use the `@Unique()` decorator:

```ts
@entity()
@unique({ properties: 'metaData.foo' })
@unique({ properties: ['metaData.foo', 'metaData.bar'] }) // compound unique index
export class Book {

  @Property({ type: 'json', nullable: true })
  metaData?: { foo: string; bar: number };

}
```

In MySQL, you can also set the type explicitly:

```ts
@entity()
@Index({ properties: 'metaData.foo', options: { returning: 'char(200)' } })
export class Book {

  @Property({ type: 'json', nullable: true })
  metaData?: { foo: string; bar: number };

}
```

This will generate a query like the following:

```sql
alter table `book`
  add index `book_meta_data_foo_index`((json_value(`meta_data`, '$.foo' returning char(200))));
```

> MariaDB driver does not support this feature.

Closes #1230
B4nan added a commit that referenced this pull request Sep 30, 2023
To create an index on a JSON property, use an entity-level `@Index()`
decorator with a dot path:

```ts
@entity()
@Index({ properties: 'metaData.foo' })
@Index({ properties: ['metaData.foo', 'metaData.bar'] }) // compound index
export class Book {

  @Property({ type: 'json', nullable: true })
  metaData?: { foo: string; bar: number };

}
```

In PostgreSQL, this will generate a query like the following:

```sql
create index "book_meta_data_foo_index" on "book" (("meta_data"->>'foo'));
```

To create a unique index, use the `@Unique()` decorator:

```ts
@entity()
@unique({ properties: 'metaData.foo' })
@unique({ properties: ['metaData.foo', 'metaData.bar'] }) // compound unique index
export class Book {

  @Property({ type: 'json', nullable: true })
  metaData?: { foo: string; bar: number };

}
```

In MySQL, you can also set the type explicitly:

```ts
@entity()
@Index({ properties: 'metaData.foo', options: { returning: 'char(200)' } })
export class Book {

  @Property({ type: 'json', nullable: true })
  metaData?: { foo: string; bar: number };

}
```

This will generate a query like the following:

```sql
alter table `book`
  add index `book_meta_data_foo_index`((json_value(`meta_data`, '$.foo' returning char(200))));
```

> MariaDB driver does not support this feature.

Closes #1230
@B4nan B4nan mentioned this pull request Oct 2, 2023
22 tasks
B4nan added a commit that referenced this pull request Oct 2, 2023
To create an index on a JSON property, use an entity-level `@Index()`
decorator with a dot path:

```ts
@entity()
@Index({ properties: 'metaData.foo' })
@Index({ properties: ['metaData.foo', 'metaData.bar'] }) // compound index
export class Book {

  @Property({ type: 'json', nullable: true })
  metaData?: { foo: string; bar: number };

}
```

In PostgreSQL, this will generate a query like the following:

```sql
create index "book_meta_data_foo_index" on "book" (("meta_data"->>'foo'));
```

To create a unique index, use the `@Unique()` decorator:

```ts
@entity()
@unique({ properties: 'metaData.foo' })
@unique({ properties: ['metaData.foo', 'metaData.bar'] }) // compound unique index
export class Book {

  @Property({ type: 'json', nullable: true })
  metaData?: { foo: string; bar: number };

}
```

In MySQL, you can also set the type explicitly:

```ts
@entity()
@Index({ properties: 'metaData.foo', options: { returning: 'char(200)' } })
export class Book {

  @Property({ type: 'json', nullable: true })
  metaData?: { foo: string; bar: number };

}
```

This will generate a query like the following:

```sql
alter table `book`
  add index `book_meta_data_foo_index`((json_value(`meta_data`, '$.foo' returning char(200))));
```

> MariaDB driver does not support this feature.

Closes #1230
B4nan added a commit that referenced this pull request Oct 17, 2023
To create an index on a JSON property, use an entity-level `@Index()`
decorator with a dot path:

```ts
@entity()
@Index({ properties: 'metaData.foo' })
@Index({ properties: ['metaData.foo', 'metaData.bar'] }) // compound index
export class Book {

  @Property({ type: 'json', nullable: true })
  metaData?: { foo: string; bar: number };

}
```

In PostgreSQL, this will generate a query like the following:

```sql
create index "book_meta_data_foo_index" on "book" (("meta_data"->>'foo'));
```

To create a unique index, use the `@Unique()` decorator:

```ts
@entity()
@unique({ properties: 'metaData.foo' })
@unique({ properties: ['metaData.foo', 'metaData.bar'] }) // compound unique index
export class Book {

  @Property({ type: 'json', nullable: true })
  metaData?: { foo: string; bar: number };

}
```

In MySQL, you can also set the type explicitly:

```ts
@entity()
@Index({ properties: 'metaData.foo', options: { returning: 'char(200)' } })
export class Book {

  @Property({ type: 'json', nullable: true })
  metaData?: { foo: string; bar: number };

}
```

This will generate a query like the following:

```sql
alter table `book`
  add index `book_meta_data_foo_index`((json_value(`meta_data`, '$.foo' returning char(200))));
```

> MariaDB driver does not support this feature.

Closes #1230
B4nan added a commit that referenced this pull request Oct 21, 2023
To create an index on a JSON property, use an entity-level `@Index()`
decorator with a dot path:

```ts
@entity()
@Index({ properties: 'metaData.foo' })
@Index({ properties: ['metaData.foo', 'metaData.bar'] }) // compound index
export class Book {

  @Property({ type: 'json', nullable: true })
  metaData?: { foo: string; bar: number };

}
```

In PostgreSQL, this will generate a query like the following:

```sql
create index "book_meta_data_foo_index" on "book" (("meta_data"->>'foo'));
```

To create a unique index, use the `@Unique()` decorator:

```ts
@entity()
@unique({ properties: 'metaData.foo' })
@unique({ properties: ['metaData.foo', 'metaData.bar'] }) // compound unique index
export class Book {

  @Property({ type: 'json', nullable: true })
  metaData?: { foo: string; bar: number };

}
```

In MySQL, you can also set the type explicitly:

```ts
@entity()
@Index({ properties: 'metaData.foo', options: { returning: 'char(200)' } })
export class Book {

  @Property({ type: 'json', nullable: true })
  metaData?: { foo: string; bar: number };

}
```

This will generate a query like the following:

```sql
alter table `book`
  add index `book_meta_data_foo_index`((json_value(`meta_data`, '$.foo' returning char(200))));
```

> MariaDB driver does not support this feature.

Closes #1230
B4nan added a commit that referenced this pull request Oct 25, 2023
To create an index on a JSON property, use an entity-level `@Index()`
decorator with a dot path:

```ts
@entity()
@Index({ properties: 'metaData.foo' })
@Index({ properties: ['metaData.foo', 'metaData.bar'] }) // compound index
export class Book {

  @Property({ type: 'json', nullable: true })
  metaData?: { foo: string; bar: number };

}
```

In PostgreSQL, this will generate a query like the following:

```sql
create index "book_meta_data_foo_index" on "book" (("meta_data"->>'foo'));
```

To create a unique index, use the `@Unique()` decorator:

```ts
@entity()
@unique({ properties: 'metaData.foo' })
@unique({ properties: ['metaData.foo', 'metaData.bar'] }) // compound unique index
export class Book {

  @Property({ type: 'json', nullable: true })
  metaData?: { foo: string; bar: number };

}
```

In MySQL, you can also set the type explicitly:

```ts
@entity()
@Index({ properties: 'metaData.foo', options: { returning: 'char(200)' } })
export class Book {

  @Property({ type: 'json', nullable: true })
  metaData?: { foo: string; bar: number };

}
```

This will generate a query like the following:

```sql
alter table `book`
  add index `book_meta_data_foo_index`((json_value(`meta_data`, '$.foo' returning char(200))));
```

> MariaDB driver does not support this feature.

Closes #1230
B4nan added a commit that referenced this pull request Nov 2, 2023
To create an index on a JSON property, use an entity-level `@Index()`
decorator with a dot path:

```ts
@entity()
@Index({ properties: 'metaData.foo' })
@Index({ properties: ['metaData.foo', 'metaData.bar'] }) // compound index
export class Book {

  @Property({ type: 'json', nullable: true })
  metaData?: { foo: string; bar: number };

}
```

In PostgreSQL, this will generate a query like the following:

```sql
create index "book_meta_data_foo_index" on "book" (("meta_data"->>'foo'));
```

To create a unique index, use the `@Unique()` decorator:

```ts
@entity()
@unique({ properties: 'metaData.foo' })
@unique({ properties: ['metaData.foo', 'metaData.bar'] }) // compound unique index
export class Book {

  @Property({ type: 'json', nullable: true })
  metaData?: { foo: string; bar: number };

}
```

In MySQL, you can also set the type explicitly:

```ts
@entity()
@Index({ properties: 'metaData.foo', options: { returning: 'char(200)' } })
export class Book {

  @Property({ type: 'json', nullable: true })
  metaData?: { foo: string; bar: number };

}
```

This will generate a query like the following:

```sql
alter table `book`
  add index `book_meta_data_foo_index`((json_value(`meta_data`, '$.foo' returning char(200))));
```

> MariaDB driver does not support this feature.

Closes #1230
B4nan added a commit that referenced this pull request Nov 5, 2023
To create an index on a JSON property, use an entity-level `@Index()`
decorator with a dot path:

```ts
@entity()
@Index({ properties: 'metaData.foo' })
@Index({ properties: ['metaData.foo', 'metaData.bar'] }) // compound index
export class Book {

  @Property({ type: 'json', nullable: true })
  metaData?: { foo: string; bar: number };

}
```

In PostgreSQL, this will generate a query like the following:

```sql
create index "book_meta_data_foo_index" on "book" (("meta_data"->>'foo'));
```

To create a unique index, use the `@Unique()` decorator:

```ts
@entity()
@unique({ properties: 'metaData.foo' })
@unique({ properties: ['metaData.foo', 'metaData.bar'] }) // compound unique index
export class Book {

  @Property({ type: 'json', nullable: true })
  metaData?: { foo: string; bar: number };

}
```

In MySQL, you can also set the type explicitly:

```ts
@entity()
@Index({ properties: 'metaData.foo', options: { returning: 'char(200)' } })
export class Book {

  @Property({ type: 'json', nullable: true })
  metaData?: { foo: string; bar: number };

}
```

This will generate a query like the following:

```sql
alter table `book`
  add index `book_meta_data_foo_index`((json_value(`meta_data`, '$.foo' returning char(200))));
```

> MariaDB driver does not support this feature.

Closes #1230
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant