File size: 4,020 Bytes
6a30288
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
import {
  buyerAccount,
  sellerPasswordHash,
  stores,
} from "./catalog-config.mjs";

const escapeSql = (value) => String(value).replace(/'/g, "''");

const stringLiteral = (value) => `'${escapeSql(value)}'`;

const imageIdFor = (assetPath) =>
  assetPath
    .split("/")
    .pop()
    .replace(/\.[^.]+$/, "")
    .replace(/[^a-z0-9-]/gi, "-")
    .toLowerCase();

const productRow = (product, storeSlug) => {
  const imagesJson = JSON.stringify([
    {
      id: imageIdFor(product.assetPath),
      url: product.assetPath,
      alt: product.name,
    },
  ]);

  return `  (${stringLiteral(product.name)}, ${product.price.toFixed(2)}, ${stringLiteral(
    product.description
  )}, ${product.inventory}, ${stringLiteral(imagesJson)}, (SELECT id FROM stores WHERE slug = ${stringLiteral(
    storeSlug
  )}))`;
};

const storeRows = stores.map(
  (store) =>
    `  (${stringLiteral(store.name)}, ${stringLiteral(
      store.industry
    )}, ${stringLiteral(store.description)}, ${stringLiteral(store.slug)})`
);

const productRows = stores.flatMap((store) =>
  store.products.map((product) => productRow(product, store.slug))
);

const userRows = [
  `  (${stringLiteral(buyerAccount.name)}, ${stringLiteral(
    buyerAccount.email
  )}, ${stringLiteral(
    buyerAccount.passwordHash
  )}, NULL, UNIX_TIMESTAMP())`,
  ...stores.map(
    (store) =>
      `  (${stringLiteral(store.name + " Seller")}, ${stringLiteral(
        store.email
      )}, ${stringLiteral(
        sellerPasswordHash
      )}, (SELECT id FROM stores WHERE slug = ${stringLiteral(
        store.slug
      )}), UNIX_TIMESTAMP())`
  ),
];

const sql = `USE onestopshop;

DROP TABLE IF EXISTS sessions;
DROP TABLE IF EXISTS users;
DROP TABLE IF EXISTS orders;
DROP TABLE IF EXISTS addresses;
DROP TABLE IF EXISTS payments;
DROP TABLE IF EXISTS carts;
DROP TABLE IF EXISTS products;
DROP TABLE IF EXISTS stores;

CREATE TABLE stores (
  id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
  store_name VARCHAR(40),
  industry TEXT,
  description TEXT,
  slug VARCHAR(50),
  UNIQUE KEY store_name_index (store_name),
  UNIQUE KEY store_slug_index (slug)
);

CREATE TABLE users (
  id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
  name VARCHAR(120),
  email VARCHAR(191),
  password_hash TEXT,
  store_id INT NULL,
  created_at INT,
  UNIQUE KEY user_email_index (email)
);

CREATE TABLE sessions (
  id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
  session_token VARCHAR(191),
  user_id INT,
  expires_at INT,
  UNIQUE KEY session_token_index (session_token)
);

CREATE TABLE products (
  id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
  name TEXT,
  price DECIMAL(10, 2) DEFAULT 0,
  description TEXT,
  inventory DECIMAL(10, 0) DEFAULT 0,
  images JSON,
  store_id INT
);

CREATE TABLE carts (
  id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
  items JSON,
  payment_intent_id TEXT,
  client_secret TEXT,
  is_closed BOOLEAN DEFAULT FALSE
);

CREATE TABLE payments (
  id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
  store_id INT,
  stripe_account_id TEXT,
  stripe_account_created_at INT,
  stripe_account_expires_at INT,
  details_submitted BOOLEAN DEFAULT FALSE
);

CREATE TABLE addresses (
  id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
  line1 TEXT,
  line2 TEXT,
  city TEXT,
  state TEXT,
  postal_code TEXT,
  country TEXT
);

CREATE TABLE orders (
  id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
  pretty_order_id INT,
  store_id INT,
  user_id INT NULL,
  items JSON,
  total DECIMAL(10, 2) DEFAULT 0,
  stripe_payment_intent_id VARCHAR(256),
  stripe_payment_intent_status TEXT,
  name TEXT,
  email TEXT,
  created_at INT,
  address INT,
  UNIQUE KEY stripe_payment_intent_id_index (stripe_payment_intent_id)
);

INSERT INTO stores (store_name, industry, description, slug)
VALUES
${storeRows.join(",\n")};

INSERT INTO products (name, price, description, inventory, images, store_id)
VALUES
${productRows.join(",\n")};

INSERT INTO users (name, email, password_hash, store_id, created_at)
VALUES
${userRows.join(",\n")};
`;

process.stdout.write(sql);