pecunia-cli 0.1.0 → 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/api.mjs CHANGED
@@ -1,3 +1,3 @@
1
- import { a as generateKyselySchema, n as generateSchema, o as generateDrizzleSchema, r as generatePrismaSchema, t as adapters } from "./generators-a19-M_dj.mjs";
1
+ import { a as generateKyselySchema, n as generateSchema, o as generateDrizzleSchema, r as generatePrismaSchema, t as adapters } from "./generators-97RierQY.mjs";
2
2
 
3
3
  export { adapters, generateDrizzleSchema, generateKyselySchema, generatePrismaSchema, generateSchema };
@@ -13,9 +13,9 @@ function convertToSnakeCase(str, camelCase) {
13
13
  }
14
14
  const generateDrizzleSchema = async ({ options, file, adapter }) => {
15
15
  const tables = getPaymentTables(options);
16
- const filePath = file || "./auth-schema.ts";
16
+ const filePath = file || "./payment-schema.ts";
17
17
  const databaseType = adapter.options?.provider;
18
- if (!databaseType) throw new Error(`Database provider type is undefined during Drizzle schema generation. Please define a \`provider\` in the Drizzle adapter config. Read more at https://better-auth.com/docs/adapters/drizzle`);
18
+ if (!databaseType) throw new Error("Database provider type is undefined during Drizzle schema generation. Please define a `provider` in the Drizzle adapter config.");
19
19
  const fileExist = existsSync(filePath);
20
20
  let code = generateImport({
21
21
  databaseType,
@@ -35,21 +35,20 @@ const generateDrizzleSchema = async ({ options, file, adapter }) => {
35
35
  const modelName = getModelName(tableKey);
36
36
  const fields = table.fields;
37
37
  function getType(name, field) {
38
- if (!databaseType) throw new Error(`Database provider type is undefined during Drizzle schema generation. Please define a \`provider\` in the Drizzle adapter config. Read more at https://better-auth.com/docs/adapters/drizzle`);
39
38
  name = convertToSnakeCase(name, adapter.options?.camelCase);
40
39
  if (field.references?.field === "id") {
41
- if (field.references.field) {
42
- if (databaseType === "mysql") return `varchar('${name}', { length: 36 })`;
43
- }
40
+ if (databaseType === "mysql") return `varchar('${name}', { length: 36 })`;
44
41
  return `text('${name}')`;
45
42
  }
46
43
  const type = field.type;
47
- if (typeof type !== "string") if (Array.isArray(type) && type.every((x) => typeof x === "string")) return {
48
- sqlite: `text({ enum: [${type.map((x) => `'${x}'`).join(", ")}] })`,
49
- pg: `text('${name}', { enum: [${type.map((x) => `'${x}'`).join(", ")}] })`,
50
- mysql: `mysqlEnum([${type.map((x) => `'${x}'`).join(", ")}])`
51
- }[databaseType];
52
- else throw new TypeError(`Invalid field type for field ${name} in model ${modelName}`);
44
+ if (typeof type !== "string") {
45
+ if (Array.isArray(type) && type.every((x) => typeof x === "string")) return {
46
+ sqlite: `text({ enum: [${type.map((x) => `'${x}'`).join(", ")}] })`,
47
+ pg: `text('${name}', { enum: [${type.map((x) => `'${x}'`).join(", ")}] })`,
48
+ mysql: `mysqlEnum([${type.map((x) => `'${x}'`).join(", ")}])`
49
+ }[databaseType];
50
+ throw new TypeError(`Invalid field type for field ${name} in model ${modelName}`);
51
+ }
53
52
  const dbTypeMap = {
54
53
  string: {
55
54
  sqlite: `text('${name}')`,
@@ -85,22 +84,28 @@ const generateDrizzleSchema = async ({ options, file, adapter }) => {
85
84
  sqlite: `text('${name}', { mode: "json" })`,
86
85
  pg: `jsonb('${name}')`,
87
86
  mysql: `json('${name}', { mode: "json" })`
87
+ },
88
+ uuid: {
89
+ sqlite: `text('${name}')`,
90
+ pg: `uuid('${name}')`,
91
+ mysql: `varchar('${name}', { length: 36 })`
88
92
  }
89
93
  }[type];
90
94
  if (!dbTypeMap) throw new Error(`Unsupported field type '${field.type}' for field '${name}'.`);
91
95
  return dbTypeMap[databaseType];
92
96
  }
93
- let id = "";
94
- if (databaseType === "mysql") id = `varchar('id', { length: 36 }).primaryKey()`;
95
- else if (databaseType === "pg") id = `text('id').primaryKey()`;
97
+ const idFieldType = table.fields.id?.type;
98
+ let id;
99
+ if (databaseType === "pg" && idFieldType === "uuid") id = `uuid('id').primaryKey()`;
100
+ else if (databaseType === "mysql") id = `varchar('id', { length: 36 }).primaryKey()`;
96
101
  else id = `text('id').primaryKey()`;
97
- let indexes = [];
98
- const assignIndexes = (indexes$1) => {
99
- if (!indexes$1.length) return "";
100
- let code$1 = [`, (table) => [`];
101
- for (const index of indexes$1) code$1.push(` ${index.type}("${index.name}").on(table.${index.on}),`);
102
- code$1.push(`]`);
103
- return code$1.join("\n");
102
+ const indexes = [];
103
+ const assignIndexes = (indexesToAssign) => {
104
+ if (!indexesToAssign.length) return "";
105
+ const parts = [`, (table) => [`];
106
+ for (const index of indexesToAssign) parts.push(` ${index.type}("${index.name}").on(table.${index.on}),`);
107
+ parts.push(`]`);
108
+ return parts.join("\n");
104
109
  };
105
110
  const schema = `export const ${modelName} = ${databaseType}Table("${convertToSnakeCase(modelName, adapter.options?.camelCase)}", {
106
111
  id: ${id},
@@ -164,43 +169,45 @@ const generateDrizzleSchema = async ({ options, file, adapter }) => {
164
169
  }
165
170
  });
166
171
  }
167
- const otherModels = Object.entries(tables).filter(([modelName$1]) => modelName$1 !== tableKey);
172
+ const otherModels = Object.entries(tables).filter(([otherName]) => otherName !== tableKey);
168
173
  const modelRelationsMap = /* @__PURE__ */ new Map();
169
- for (const [modelName$1, otherTable] of otherModels) {
174
+ for (const [otherModelName, otherTable] of otherModels) {
170
175
  const foreignKeysPointingHere = Object.entries(otherTable.fields).filter(([_, field]) => field.references?.model === tableKey || field.references?.model === getModelName(tableKey));
171
176
  if (foreignKeysPointingHere.length === 0) continue;
172
177
  const hasUnique = foreignKeysPointingHere.some(([_, field]) => !!field.unique);
173
178
  const hasMany$1 = foreignKeysPointingHere.some(([_, field]) => !field.unique);
174
- modelRelationsMap.set(modelName$1, {
175
- modelName: modelName$1,
179
+ modelRelationsMap.set(otherModelName, {
180
+ modelName: otherModelName,
176
181
  hasUnique,
177
182
  hasMany: hasMany$1
178
183
  });
179
184
  }
180
- for (const { modelName: modelName$1, hasMany: hasMany$1 } of modelRelationsMap.values()) {
185
+ for (const { modelName: otherModelName, hasMany: hasMany$1 } of modelRelationsMap.values()) {
181
186
  const relationType = hasMany$1 ? "many" : "one";
182
- let relationKey = getModelName(modelName$1);
187
+ let relationKey = getModelName(otherModelName);
183
188
  if (!adapter.options?.adapterConfig?.usePlural && relationType === "many") relationKey = `${relationKey}s`;
184
189
  if (!manyRelationsSet.has(relationKey)) {
185
190
  manyRelationsSet.add(relationKey);
186
191
  manyRelations.push({
187
192
  key: relationKey,
188
- model: getModelName(modelName$1),
193
+ model: getModelName(otherModelName),
189
194
  type: relationType
190
195
  });
191
196
  }
192
197
  }
193
198
  const relationsByModel = /* @__PURE__ */ new Map();
194
- for (const relation of oneRelations) if (relation.reference) {
199
+ for (const relation of oneRelations) {
200
+ if (!relation.reference) continue;
195
201
  const modelKey = relation.key;
196
202
  if (!relationsByModel.has(modelKey)) relationsByModel.set(modelKey, []);
197
203
  relationsByModel.get(modelKey).push(relation);
198
204
  }
199
205
  const duplicateRelations = [];
200
206
  const singleRelations = [];
201
- for (const [_modelKey, relations] of relationsByModel.entries()) if (relations.length > 1) duplicateRelations.push(...relations);
202
- else singleRelations.push(relations[0]);
203
- for (const relation of duplicateRelations) if (relation.reference) {
207
+ for (const [_modelKey, rels] of relationsByModel.entries()) if (rels.length > 1) duplicateRelations.push(...rels);
208
+ else singleRelations.push(rels[0]);
209
+ for (const relation of duplicateRelations) {
210
+ if (!relation.reference) continue;
204
211
  const fieldName = relation.reference.fieldName;
205
212
  const tableRelation = `export const ${`${modelName}${fieldName.charAt(0).toUpperCase() + fieldName.slice(1)}Relations`} = relations(${getModelName(table.modelName)}, ({ one }) => ({
206
213
  ${relation.key}: one(${relation.model}, {
@@ -243,17 +250,19 @@ const generateDrizzleSchema = async ({ options, file, adapter }) => {
243
250
  overwrite: fileExist
244
251
  };
245
252
  };
246
- function generateImport({ databaseType, tables, options }) {
253
+ function generateImport({ databaseType, tables }) {
247
254
  const rootImports = ["relations"];
248
255
  const coreImports = [];
249
256
  let hasBigint = false;
250
257
  let hasJson = false;
258
+ let hasUuid = false;
251
259
  for (const table of Object.values(tables)) {
252
260
  for (const field of Object.values(table.fields)) {
253
261
  if (field.bigint) hasBigint = true;
254
262
  if (field.type === "json") hasJson = true;
263
+ if (field.type === "uuid") hasUuid = true;
255
264
  }
256
- if (hasJson && hasBigint) break;
265
+ if (hasJson && hasBigint && hasUuid) break;
257
266
  }
258
267
  coreImports.push(`${databaseType}Table`);
259
268
  coreImports.push(databaseType === "mysql" ? "varchar, text" : databaseType === "pg" ? "text" : "text");
@@ -263,9 +272,8 @@ function generateImport({ databaseType, tables, options }) {
263
272
  if (Object.values(tables).some((table) => Object.values(table.fields).some((field) => (field.type === "number" || field.type === "number[]") && !field.bigint))) coreImports.push("int");
264
273
  if (Object.values(tables).some((table) => Object.values(table.fields).some((field) => typeof field.type !== "string" && Array.isArray(field.type) && field.type.every((x) => typeof x === "string")))) coreImports.push("mysqlEnum");
265
274
  } else if (databaseType === "pg") {
266
- const hasNonBigintNumber = Object.values(tables).some((table) => Object.values(table.fields).some((field) => (field.type === "number" || field.type === "number[]") && !field.bigint));
267
- Object.values(tables).some((table) => Object.values(table.fields).some((field) => field.references?.field === "id"));
268
- if (hasNonBigintNumber) coreImports.push("integer");
275
+ if (Object.values(tables).some((table) => Object.values(table.fields).some((field) => (field.type === "number" || field.type === "number[]") && !field.bigint))) coreImports.push("integer");
276
+ if (hasUuid) coreImports.push("uuid");
269
277
  } else coreImports.push("integer");
270
278
  if (hasJson) {
271
279
  if (databaseType === "pg") coreImports.push("jsonb");
package/dist/index.mjs CHANGED
@@ -1,5 +1,5 @@
1
1
  #!/usr/bin/env node
2
- import { i as getPackageInfo, n as generateSchema } from "./generators-a19-M_dj.mjs";
2
+ import { i as getPackageInfo, n as generateSchema } from "./generators-97RierQY.mjs";
3
3
  import { Command } from "commander";
4
4
  import fs, { existsSync, readFileSync } from "node:fs";
5
5
  import fs$1 from "node:fs/promises";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pecunia-cli",
3
- "version": "0.1.0",
3
+ "version": "0.1.2",
4
4
  "type": "module",
5
5
  "module": "dist/index.mjs",
6
6
  "main": "./dist/index.mjs",
@@ -64,8 +64,8 @@
64
64
  "dotenv": "^17.2.2",
65
65
  "drizzle-orm": "^0.33.0",
66
66
  "open": "^10.2.0",
67
- "pecunia-core": "^0.0.4",
68
- "pecunia-root": "^0.1.2",
67
+ "pecunia-core": "^0.0.5",
68
+ "pecunia-root": "^0.1.3",
69
69
  "pg": "^8.16.3",
70
70
  "prettier": "^3.6.2",
71
71
  "prompts": "^2.4.2",