361 lines
13 KiB
JavaScript
Executable File
361 lines
13 KiB
JavaScript
Executable File
#!/usr/bin/env -S node
|
|
import type { Contract as End } from '../../snapshots/a6d994c8ca49497bbabf294eb5fe9e8feb2cdafb62dfaf7c9a5b2c2e9135a15f/contract';
|
|
import endContract from '../../snapshots/a6d994c8ca49497bbabf294eb5fe9e8feb2cdafb62dfaf7c9a5b2c2e9135a15f/contract.json' with { type: 'json' };
|
|
import { Migration, MigrationCLI, col, fn, lit, primaryKey } from '@prisma/orm-postgres/migration';
|
|
|
|
export default class M extends Migration<never, End> {
|
|
override readonly endContractJson = endContract;
|
|
|
|
override get operations() {
|
|
return [
|
|
this.createSchema({ schema: 'public' }),
|
|
this.createTable({
|
|
schema: 'public',
|
|
table: 'cart_products',
|
|
columns: [
|
|
col('cartId', 'int4', { notNull: true, codecRef: { codecId: 'pg/int4@1' } }),
|
|
col('historyId', 'int4', { notNull: true, codecRef: { codecId: 'pg/int4@1' } }),
|
|
col('quantity', 'int4', { notNull: true, codecRef: { codecId: 'pg/int4@1' } }),
|
|
],
|
|
constraints: [primaryKey(['cartId', 'historyId'])],
|
|
}),
|
|
this.createTable({
|
|
schema: 'public',
|
|
table: 'category',
|
|
columns: [
|
|
col('createdAt', 'timestamptz', {
|
|
notNull: true,
|
|
default: fn('now()'),
|
|
codecRef: { codecId: 'pg/timestamptz-string@1' },
|
|
}),
|
|
col('id', 'SERIAL', { notNull: true, codecRef: { codecId: 'pg/int4@1' } }),
|
|
col('name', 'text', { notNull: true, codecRef: { codecId: 'pg/text@1' } }),
|
|
],
|
|
constraints: [primaryKey(['id'])],
|
|
}),
|
|
this.createTable({
|
|
schema: 'public',
|
|
table: 'inventory',
|
|
columns: [
|
|
col('id', 'SERIAL', { notNull: true, codecRef: { codecId: 'pg/int4@1' } }),
|
|
col('name', 'text', { notNull: true, codecRef: { codecId: 'pg/text@1' } }),
|
|
],
|
|
constraints: [primaryKey(['id'])],
|
|
}),
|
|
this.createTable({
|
|
schema: 'public',
|
|
table: 'payment',
|
|
columns: [
|
|
col('cartId', 'int4', { notNull: true, codecRef: { codecId: 'pg/int4@1' } }),
|
|
col('createdAt', 'timestamptz', {
|
|
notNull: true,
|
|
default: fn('now()'),
|
|
codecRef: { codecId: 'pg/timestamptz-string@1' },
|
|
}),
|
|
col('id', 'SERIAL', { notNull: true, codecRef: { codecId: 'pg/int4@1' } }),
|
|
col('orderId', 'int4', { codecRef: { codecId: 'pg/int4@1' } }),
|
|
col('paid', 'bool', {
|
|
notNull: true,
|
|
default: lit(false),
|
|
codecRef: { codecId: 'pg/bool@1' },
|
|
}),
|
|
col('paidAt', 'timestamptz', { codecRef: { codecId: 'pg/timestamptz-string@1' } }),
|
|
col('paymentMethod', 'text', { notNull: true, codecRef: { codecId: 'pg/text@1' } }),
|
|
col('status', 'text', { notNull: true, codecRef: { codecId: 'pg/text@1' } }),
|
|
col('terminalId', 'int4', { codecRef: { codecId: 'pg/int4@1' } }),
|
|
col('userId', 'int4', { notNull: true, codecRef: { codecId: 'pg/int4@1' } }),
|
|
],
|
|
constraints: [primaryKey(['id'])],
|
|
}),
|
|
this.createTable({
|
|
schema: 'public',
|
|
table: 'product',
|
|
columns: [
|
|
col('createdAt', 'timestamptz', {
|
|
notNull: true,
|
|
default: fn('now()'),
|
|
codecRef: { codecId: 'pg/timestamptz-string@1' },
|
|
}),
|
|
col('description', 'text', { codecRef: { codecId: 'pg/text@1' } }),
|
|
col('id', 'SERIAL', { notNull: true, codecRef: { codecId: 'pg/int4@1' } }),
|
|
col('name', 'text', { notNull: true, codecRef: { codecId: 'pg/text@1' } }),
|
|
col('sku', 'text', { notNull: true, codecRef: { codecId: 'pg/text@1' } }),
|
|
col('supplierId', 'int4', { codecRef: { codecId: 'pg/int4@1' } }),
|
|
],
|
|
constraints: [primaryKey(['id'])],
|
|
}),
|
|
this.createTable({
|
|
schema: 'public',
|
|
table: 'product_category',
|
|
columns: [
|
|
col('categoryId', 'int4', { notNull: true, codecRef: { codecId: 'pg/int4@1' } }),
|
|
col('productId', 'int4', { notNull: true, codecRef: { codecId: 'pg/int4@1' } }),
|
|
],
|
|
constraints: [primaryKey(['productId', 'categoryId'])],
|
|
}),
|
|
this.createTable({
|
|
schema: 'public',
|
|
table: 'product_history',
|
|
columns: [
|
|
col('costUnit', 'numeric', { notNull: true, codecRef: { codecId: 'pg/numeric@1' } }),
|
|
col('createdAt', 'timestamptz', {
|
|
notNull: true,
|
|
default: fn('now()'),
|
|
codecRef: { codecId: 'pg/timestamptz-string@1' },
|
|
}),
|
|
col('id', 'SERIAL', { notNull: true, codecRef: { codecId: 'pg/int4@1' } }),
|
|
col('productId', 'int4', { notNull: true, codecRef: { codecId: 'pg/int4@1' } }),
|
|
col('soldAt', 'timestamptz', { codecRef: { codecId: 'pg/timestamptz-string@1' } }),
|
|
col('status', 'text', { notNull: true, codecRef: { codecId: 'pg/text@1' } }),
|
|
col('unitPrice', 'numeric', { notNull: true, codecRef: { codecId: 'pg/numeric@1' } }),
|
|
],
|
|
constraints: [primaryKey(['id'])],
|
|
}),
|
|
this.createTable({
|
|
schema: 'public',
|
|
table: 'shop_cart',
|
|
columns: [
|
|
col('createdAt', 'timestamptz', {
|
|
notNull: true,
|
|
default: fn('now()'),
|
|
codecRef: { codecId: 'pg/timestamptz-string@1' },
|
|
}),
|
|
col('id', 'SERIAL', { notNull: true, codecRef: { codecId: 'pg/int4@1' } }),
|
|
col('totalPrice', 'numeric', { notNull: true, codecRef: { codecId: 'pg/numeric@1' } }),
|
|
col('userId', 'int4', { codecRef: { codecId: 'pg/int4@1' } }),
|
|
],
|
|
constraints: [primaryKey(['id'])],
|
|
}),
|
|
this.createTable({
|
|
schema: 'public',
|
|
table: 'supplier',
|
|
columns: [
|
|
col('createdAt', 'timestamptz', {
|
|
notNull: true,
|
|
default: fn('now()'),
|
|
codecRef: { codecId: 'pg/timestamptz-string@1' },
|
|
}),
|
|
col('id', 'SERIAL', { notNull: true, codecRef: { codecId: 'pg/int4@1' } }),
|
|
col('lastSupply', 'timestamptz', { codecRef: { codecId: 'pg/timestamptz-string@1' } }),
|
|
col('name', 'text', { notNull: true, codecRef: { codecId: 'pg/text@1' } }),
|
|
],
|
|
constraints: [primaryKey(['id'])],
|
|
}),
|
|
this.createTable({
|
|
schema: 'public',
|
|
table: 'user',
|
|
columns: [
|
|
col('createdAt', 'timestamptz', {
|
|
notNull: true,
|
|
default: fn('now()'),
|
|
codecRef: { codecId: 'pg/timestamptz-string@1' },
|
|
}),
|
|
col('email', 'text', { notNull: true, codecRef: { codecId: 'pg/text@1' } }),
|
|
col('id', 'SERIAL', { notNull: true, codecRef: { codecId: 'pg/int4@1' } }),
|
|
col('name', 'text', { notNull: true, codecRef: { codecId: 'pg/text@1' } }),
|
|
col('password', 'text', { notNull: true, codecRef: { codecId: 'pg/text@1' } }),
|
|
col('role', 'text', { notNull: true, codecRef: { codecId: 'pg/text@1' } }),
|
|
col('updatedAt', 'timestamptz', {
|
|
notNull: true,
|
|
codecRef: { codecId: 'pg/timestamptz-string@1' },
|
|
}),
|
|
],
|
|
constraints: [primaryKey(['id'])],
|
|
}),
|
|
this.createTable({
|
|
schema: 'public',
|
|
table: 'warehouse',
|
|
columns: [
|
|
col('inventoryId', 'int4', { notNull: true, codecRef: { codecId: 'pg/int4@1' } }),
|
|
col('productId', 'int4', { notNull: true, codecRef: { codecId: 'pg/int4@1' } }),
|
|
col('stock', 'int4', {
|
|
notNull: true,
|
|
default: lit(0),
|
|
codecRef: { codecId: 'pg/int4@1' },
|
|
}),
|
|
],
|
|
constraints: [primaryKey(['inventoryId', 'productId'])],
|
|
}),
|
|
this.addUnique({
|
|
schema: 'public',
|
|
table: 'product',
|
|
constraint: 'product_sku_key',
|
|
columns: ['sku'],
|
|
}),
|
|
this.addUnique({
|
|
schema: 'public',
|
|
table: 'user',
|
|
constraint: 'user_email_key',
|
|
columns: ['email'],
|
|
}),
|
|
this.createIndex({
|
|
schema: 'public',
|
|
table: 'cart_products',
|
|
index: 'cart_products_cartId_idx_79939295',
|
|
columns: ['cartId'],
|
|
}),
|
|
this.createIndex({
|
|
schema: 'public',
|
|
table: 'cart_products',
|
|
index: 'cart_products_historyId_idx_dfaba3ef',
|
|
columns: ['historyId'],
|
|
}),
|
|
this.createIndex({
|
|
schema: 'public',
|
|
table: 'payment',
|
|
index: 'payment_cartId_idx_79939295',
|
|
columns: ['cartId'],
|
|
}),
|
|
this.createIndex({
|
|
schema: 'public',
|
|
table: 'payment',
|
|
index: 'payment_userId_idx_a489d58a',
|
|
columns: ['userId'],
|
|
}),
|
|
this.createIndex({
|
|
schema: 'public',
|
|
table: 'product',
|
|
index: 'product_supplierId_idx_c4d9a8b9',
|
|
columns: ['supplierId'],
|
|
}),
|
|
this.createIndex({
|
|
schema: 'public',
|
|
table: 'product_category',
|
|
index: 'product_category_categoryId_idx_15c304f2',
|
|
columns: ['categoryId'],
|
|
}),
|
|
this.createIndex({
|
|
schema: 'public',
|
|
table: 'product_category',
|
|
index: 'product_category_productId_idx_5858600a',
|
|
columns: ['productId'],
|
|
}),
|
|
this.createIndex({
|
|
schema: 'public',
|
|
table: 'product_history',
|
|
index: 'product_history_productId_idx_5858600a',
|
|
columns: ['productId'],
|
|
}),
|
|
this.createIndex({
|
|
schema: 'public',
|
|
table: 'shop_cart',
|
|
index: 'shop_cart_userId_idx_a489d58a',
|
|
columns: ['userId'],
|
|
}),
|
|
this.createIndex({
|
|
schema: 'public',
|
|
table: 'warehouse',
|
|
index: 'warehouse_inventoryId_idx_367f33d0',
|
|
columns: ['inventoryId'],
|
|
}),
|
|
this.createIndex({
|
|
schema: 'public',
|
|
table: 'warehouse',
|
|
index: 'warehouse_productId_idx_5858600a',
|
|
columns: ['productId'],
|
|
}),
|
|
this.addForeignKey({
|
|
schema: 'public',
|
|
table: 'cart_products',
|
|
foreignKey: {
|
|
name: 'cart_products_cartId_fkey',
|
|
columns: ['cartId'],
|
|
references: { schema: 'public', table: 'shop_cart', columns: ['id'] },
|
|
},
|
|
}),
|
|
this.addForeignKey({
|
|
schema: 'public',
|
|
table: 'cart_products',
|
|
foreignKey: {
|
|
name: 'cart_products_historyId_fkey',
|
|
columns: ['historyId'],
|
|
references: { schema: 'public', table: 'product_history', columns: ['id'] },
|
|
},
|
|
}),
|
|
this.addForeignKey({
|
|
schema: 'public',
|
|
table: 'payment',
|
|
foreignKey: {
|
|
name: 'payment_cartId_fkey',
|
|
columns: ['cartId'],
|
|
references: { schema: 'public', table: 'shop_cart', columns: ['id'] },
|
|
},
|
|
}),
|
|
this.addForeignKey({
|
|
schema: 'public',
|
|
table: 'payment',
|
|
foreignKey: {
|
|
name: 'payment_userId_fkey',
|
|
columns: ['userId'],
|
|
references: { schema: 'public', table: 'user', columns: ['id'] },
|
|
},
|
|
}),
|
|
this.addForeignKey({
|
|
schema: 'public',
|
|
table: 'product',
|
|
foreignKey: {
|
|
name: 'product_supplierId_fkey',
|
|
columns: ['supplierId'],
|
|
references: { schema: 'public', table: 'supplier', columns: ['id'] },
|
|
},
|
|
}),
|
|
this.addForeignKey({
|
|
schema: 'public',
|
|
table: 'product_category',
|
|
foreignKey: {
|
|
name: 'product_category_productId_fkey',
|
|
columns: ['productId'],
|
|
references: { schema: 'public', table: 'product', columns: ['id'] },
|
|
},
|
|
}),
|
|
this.addForeignKey({
|
|
schema: 'public',
|
|
table: 'product_category',
|
|
foreignKey: {
|
|
name: 'product_category_categoryId_fkey',
|
|
columns: ['categoryId'],
|
|
references: { schema: 'public', table: 'category', columns: ['id'] },
|
|
},
|
|
}),
|
|
this.addForeignKey({
|
|
schema: 'public',
|
|
table: 'product_history',
|
|
foreignKey: {
|
|
name: 'product_history_productId_fkey',
|
|
columns: ['productId'],
|
|
references: { schema: 'public', table: 'product', columns: ['id'] },
|
|
},
|
|
}),
|
|
this.addForeignKey({
|
|
schema: 'public',
|
|
table: 'shop_cart',
|
|
foreignKey: {
|
|
name: 'shop_cart_userId_fkey',
|
|
columns: ['userId'],
|
|
references: { schema: 'public', table: 'user', columns: ['id'] },
|
|
},
|
|
}),
|
|
this.addForeignKey({
|
|
schema: 'public',
|
|
table: 'warehouse',
|
|
foreignKey: {
|
|
name: 'warehouse_inventoryId_fkey',
|
|
columns: ['inventoryId'],
|
|
references: { schema: 'public', table: 'inventory', columns: ['id'] },
|
|
},
|
|
}),
|
|
this.addForeignKey({
|
|
schema: 'public',
|
|
table: 'warehouse',
|
|
foreignKey: {
|
|
name: 'warehouse_productId_fkey',
|
|
columns: ['productId'],
|
|
references: { schema: 'public', table: 'product', columns: ['id'] },
|
|
},
|
|
}),
|
|
];
|
|
}
|
|
}
|
|
|
|
MigrationCLI.run(import.meta.url, M);
|