Skip to content

Commit

Permalink
@airswap/stores: export createIndex from redis store
Browse files Browse the repository at this point in the history
  • Loading branch information
dmosites committed Feb 22, 2024
1 parent 1c3637b commit b5bf09a
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 72 deletions.
2 changes: 1 addition & 1 deletion tools/stores/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@airswap/stores",
"version": "4.2.4",
"version": "4.2.5",
"description": "AirSwap: Storage for Indexing",
"repository": {
"type": "git",
Expand Down
2 changes: 1 addition & 1 deletion tools/stores/redis/redis.create.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require('dotenv').config({ path: '../../.env' })

const { createClient } = require('redis')
const { createIndex } = require('../build/redis/redis.index.js')
const { createIndex } = require('../build/redis/redis.js')

async function main() {
if (!process.env.REDISCLOUD_URL) {
Expand Down
56 changes: 0 additions & 56 deletions tools/stores/redis/redis.index.ts

This file was deleted.

70 changes: 58 additions & 12 deletions tools/stores/redis/redis.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import { createClient } from 'redis'
import { createClient, SchemaFieldTypes } from 'redis'
import {
FullOrder,
OrderFilter,
Indexes,
Direction,
THIRTY_DAYS,
} from '@airswap/utils'
import { createIndex } from './redis.index'

function tagsKey(token: string) {
return `tags:${token.toLowerCase()}`
Expand All @@ -32,9 +31,9 @@ function cleanTags(tags: string[]) {
}

export class Redis {
private client: any
private ttl: number
private defaultReadLimit: number
public client: any
public ttl: number
public defaultReadLimit: number

public constructor(
connectionUrl: any,
Expand Down Expand Up @@ -172,13 +171,6 @@ export class Redis {
return false
}

public async setup() {
if (!this.client.isOpen) {
await this.client.connect()
}
await createIndex(this.client)
}

public async flush() {
if (!this.client.isOpen) {
await this.client.connect()
Expand All @@ -192,3 +184,57 @@ export class Redis {
}
}
}

export async function createIndex(client) {
try {
await client.ft.dropIndex('index:ordersBySigner')
} catch (e) {}
await client.ft.create(
'index:ordersBySigner',
{
'$.chainId': {
type: SchemaFieldTypes.NUMERIC,
AS: 'chainId',
},
'$.nonce': {
type: SchemaFieldTypes.TEXT,
AS: 'nonce',
},
'$.expiry': {
type: SchemaFieldTypes.TEXT,
AS: 'expiry',
},
'$.signer.wallet': {
type: SchemaFieldTypes.TEXT,
AS: 'signerWallet',
},
'$.signer.token': {
type: SchemaFieldTypes.TEXT,
AS: 'signerToken',
},
'$.signer.amount': {
type: SchemaFieldTypes.TEXT,
AS: 'signerAmount',
},
'$.signer.id': {
type: SchemaFieldTypes.TEXT,
AS: 'signerId',
},
'$.sender.amount': {
type: SchemaFieldTypes.TEXT,
AS: 'senderAmount',
},
'$.sender.token': {
type: SchemaFieldTypes.TEXT,
AS: 'senderToken',
},
'$.tags.*': {
type: SchemaFieldTypes.TAG,
AS: 'tags',
},
},
{
ON: 'JSON',
}
)
}
4 changes: 2 additions & 2 deletions tools/stores/test/redis.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { expect } from 'chai'
import { Redis } from '../redis/redis'
import { Redis, createIndex } from '../redis/redis'
import { ChainIds } from '@airswap/utils'

import {
Expand Down Expand Up @@ -46,7 +46,7 @@ async function createSignedOrder(
describe('Redis', async () => {
beforeEach(async () => {
await store.flush()
await store.setup()
await createIndex(store.client)
orderOne = await createSignedOrder('1', signerOne)
orderTwo = await createSignedOrder('2', signerTwo)
})
Expand Down

0 comments on commit b5bf09a

Please sign in to comment.