feat(order): module foundation (#6399)
What: Initial Order module foundation. Basic models and methods to manage entites like in Cart Module
This commit is contained in:
@@ -9,11 +9,23 @@ describe("createPsqlIndexStatementHelper", function () {
|
||||
}
|
||||
|
||||
const indexStatement = createPsqlIndexStatementHelper(options)
|
||||
expect(indexStatement).toEqual(
|
||||
expect(indexStatement + "").toEqual(
|
||||
`CREATE INDEX IF NOT EXISTS "${options.name}" ON "${options.tableName}" (${options.columns})`
|
||||
)
|
||||
})
|
||||
|
||||
it("should generate a simple index and auto compose its name", function () {
|
||||
const options = {
|
||||
tableName: "table_name",
|
||||
columns: "column_name",
|
||||
}
|
||||
|
||||
const indexStatement = createPsqlIndexStatementHelper(options)
|
||||
expect(indexStatement + "").toEqual(
|
||||
`CREATE INDEX IF NOT EXISTS "IDX_table_name_column_name" ON "${options.tableName}" (${options.columns})`
|
||||
)
|
||||
})
|
||||
|
||||
it("should generate a composite index", function () {
|
||||
const options = {
|
||||
name: "index_name",
|
||||
@@ -22,7 +34,7 @@ describe("createPsqlIndexStatementHelper", function () {
|
||||
}
|
||||
|
||||
const indexStatement = createPsqlIndexStatementHelper(options)
|
||||
expect(indexStatement).toEqual(
|
||||
expect(indexStatement.expression).toEqual(
|
||||
`CREATE INDEX IF NOT EXISTS "${options.name}" ON "${
|
||||
options.tableName
|
||||
}" (${options.columns.join(", ")})`
|
||||
@@ -38,7 +50,7 @@ describe("createPsqlIndexStatementHelper", function () {
|
||||
}
|
||||
|
||||
const indexStatement = createPsqlIndexStatementHelper(options)
|
||||
expect(indexStatement).toEqual(
|
||||
expect(indexStatement.expression).toEqual(
|
||||
`CREATE INDEX IF NOT EXISTS "${options.name}" ON "${
|
||||
options.tableName
|
||||
}" (${options.columns.join(", ")}) WHERE ${options.where}`
|
||||
@@ -55,7 +67,7 @@ describe("createPsqlIndexStatementHelper", function () {
|
||||
}
|
||||
|
||||
const indexStatement = createPsqlIndexStatementHelper(options)
|
||||
expect(indexStatement).toEqual(
|
||||
expect(indexStatement.toString()).toEqual(
|
||||
`CREATE INDEX IF NOT EXISTS "${options.name}" ON "${
|
||||
options.tableName
|
||||
}" USING GIN (${options.columns.join(", ")}) WHERE ${options.where}`
|
||||
@@ -64,7 +76,6 @@ describe("createPsqlIndexStatementHelper", function () {
|
||||
|
||||
it("should generate unique constraint", function () {
|
||||
const options = {
|
||||
name: "index_name",
|
||||
tableName: "table_name",
|
||||
columns: ["column_name_1", "column_name_2"],
|
||||
unique: true,
|
||||
@@ -72,8 +83,8 @@ describe("createPsqlIndexStatementHelper", function () {
|
||||
}
|
||||
|
||||
const indexStatement = createPsqlIndexStatementHelper(options)
|
||||
expect(indexStatement).toEqual(
|
||||
`CREATE UNIQUE INDEX IF NOT EXISTS "${options.name}" ON "${
|
||||
expect(indexStatement.expression).toEqual(
|
||||
`CREATE UNIQUE INDEX IF NOT EXISTS "IDX_table_name_column_name_1_column_name_2" ON "${
|
||||
options.tableName
|
||||
}" (${options.columns.join(", ")}) WHERE ${options.where}`
|
||||
)
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
import { Index } from "@mikro-orm/core"
|
||||
|
||||
/**
|
||||
* Create a PSQL index statement
|
||||
* @param name The name of the index
|
||||
* @param name The name of the index, if not provided it will be generated in the format IDX_tableName_columnName
|
||||
* @param tableName The name of the table
|
||||
* @param columns The columns to index
|
||||
* @param type The type of index (e.g GIN, GIST, BTREE, etc)
|
||||
@@ -16,7 +18,7 @@
|
||||
* where: "email IS NOT NULL"
|
||||
* });
|
||||
*
|
||||
* // CREATE INDEX IF NOT EXISTS idx_user_email ON user USING btree (email) WHERE email IS NOT NULL;
|
||||
* // expression: CREATE INDEX IF NOT EXISTS idx_user_email ON user USING btree (email) WHERE email IS NOT NULL;
|
||||
*
|
||||
* createPsqlIndexStatementHelper({
|
||||
* name: "idx_user_email",
|
||||
@@ -24,7 +26,7 @@
|
||||
* columns: "email"
|
||||
* });
|
||||
*
|
||||
* // CREATE INDEX IF NOT EXISTS idx_user_email ON user (email);
|
||||
* // expression: CREATE INDEX IF NOT EXISTS idx_user_email ON user (email);
|
||||
*
|
||||
*/
|
||||
export function createPsqlIndexStatementHelper({
|
||||
@@ -35,17 +37,38 @@ export function createPsqlIndexStatementHelper({
|
||||
where,
|
||||
unique,
|
||||
}: {
|
||||
name: string
|
||||
name?: string
|
||||
tableName: string
|
||||
columns: string | string[]
|
||||
type?: string
|
||||
where?: string
|
||||
unique?: boolean
|
||||
}) {
|
||||
const columnsName = Array.isArray(columns) ? columns.join("_") : columns
|
||||
|
||||
columns = Array.isArray(columns) ? columns.join(", ") : columns
|
||||
name = name || `IDX_${tableName}_${columnsName}`
|
||||
|
||||
const typeStr = type ? ` USING ${type}` : ""
|
||||
const optionsStr = where ? ` WHERE ${where}` : ""
|
||||
const uniqueStr = unique ? "UNIQUE " : ""
|
||||
|
||||
return `CREATE ${uniqueStr}INDEX IF NOT EXISTS "${name}" ON "${tableName}"${typeStr} (${columns})${optionsStr}`
|
||||
const expression = `CREATE ${uniqueStr}INDEX IF NOT EXISTS "${name}" ON "${tableName}"${typeStr} (${columns})${optionsStr}`
|
||||
return {
|
||||
toString: () => {
|
||||
return expression
|
||||
},
|
||||
valueOf: () => {
|
||||
return expression
|
||||
},
|
||||
name,
|
||||
expression,
|
||||
MikroORMIndex: (options = {}) => {
|
||||
return Index({
|
||||
name,
|
||||
expression,
|
||||
...options,
|
||||
})
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user