pecunia-cli 0.3.7 → 0.3.9

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-CWgRQHR-.mjs";
1
+ import { a as generateKyselySchema, n as generateSchema, o as generateDrizzleSchema, r as generatePrismaSchema, t as adapters } from "./generators-27dPVtx_.mjs";
2
2
 
3
3
  export { adapters, generateDrizzleSchema, generateKyselySchema, generatePrismaSchema, generateSchema };
@@ -30,7 +30,18 @@ const generateDrizzleSchema = async ({ options, file, adapter }) => {
30
30
  schema: tables,
31
31
  usePlural: adapter.options?.adapterConfig?.usePlural
32
32
  });
33
- for (const tableKey in tables) {
33
+ const circularReferenceFields = new Set(["defaultPaymentMethodId", "defaultPriceId"]);
34
+ const sortedTableKeys = Object.keys(tables).sort((a, b) => {
35
+ const tableA = tables[a];
36
+ const tableB = tables[b];
37
+ const orderA = tableA.order ?? 0;
38
+ const orderB = tableB.order ?? 0;
39
+ if (orderA !== orderB) return orderA - orderB;
40
+ if (Object.values(tableA.fields).some((field) => field.references?.model === b || field.references?.model === getModelName(b))) return 1;
41
+ if (Object.values(tableB.fields).some((field) => field.references?.model === a || field.references?.model === getModelName(a))) return -1;
42
+ return 0;
43
+ });
44
+ for (const tableKey of sortedTableKeys) {
34
45
  const table = tables[tableKey];
35
46
  const modelName = getModelName(tableKey);
36
47
  const fields = table.fields;
@@ -103,7 +114,7 @@ const generateDrizzleSchema = async ({ options, file, adapter }) => {
103
114
  };
104
115
  const schema = `export const ${modelName} = ${databaseType}Table("${convertToSnakeCase(modelName, adapter.options?.camelCase)}", {
105
116
  id: ${id},
106
- ${Object.keys(fields).map((field) => {
117
+ ${Object.keys(fields).filter((field) => field !== "id").map((field) => {
107
118
  const attr = fields[field];
108
119
  const fieldName = attr.fieldName || field;
109
120
  let type = getType(fieldName, attr);
@@ -112,11 +123,6 @@ const generateDrizzleSchema = async ({ options, file, adapter }) => {
112
123
  name: `${modelName}_${fieldName}_idx`,
113
124
  on: fieldName
114
125
  });
115
- else if (attr.index && attr.unique) indexes.push({
116
- type: "uniqueIndex",
117
- name: `${modelName}_${fieldName}_uidx`,
118
- on: fieldName
119
- });
120
126
  if (attr.defaultValue !== null && typeof attr.defaultValue !== "undefined") if (typeof attr.defaultValue === "function") {
121
127
  if (attr.type === "date" && attr.defaultValue.toString().includes("new Date()")) if (databaseType === "sqlite") type += `.default(sql\`(cast(unixepoch('subsecond') * 1000 as integer))\`)`;
122
128
  else type += `.defaultNow()`;
@@ -125,7 +131,14 @@ const generateDrizzleSchema = async ({ options, file, adapter }) => {
125
131
  if (attr.onUpdate && attr.type === "date") {
126
132
  if (typeof attr.onUpdate === "function") type += `.$onUpdate(${attr.onUpdate})`;
127
133
  }
128
- return `${fieldName}: ${type}${attr.required ? ".notNull()" : ""}${attr.unique ? ".unique()" : ""}${attr.references ? `.references(()=> ${getModelName(attr.references.model)}.${getFieldName({
134
+ const isCircularReference = circularReferenceFields.has(field);
135
+ const shouldAddReference = attr.references && !isCircularReference;
136
+ if (isCircularReference && attr.index) indexes.push({
137
+ type: "index",
138
+ name: `${modelName}_${fieldName}_idx`,
139
+ on: fieldName
140
+ });
141
+ return `${fieldName}: ${type}${attr.required ? ".notNull()" : ""}${attr.unique ? ".unique()" : ""}${shouldAddReference ? `.references(()=> ${getModelName(attr.references.model)}.${getFieldName({
129
142
  model: attr.references.model,
130
143
  field: attr.references.field
131
144
  })}, { onDelete: '${attr.references.onDelete || "cascade"}' })` : ""}`;
@@ -134,26 +147,37 @@ const generateDrizzleSchema = async ({ options, file, adapter }) => {
134
147
  code += `\n${schema}\n`;
135
148
  }
136
149
  let relationsString = "";
137
- for (const tableKey in tables) {
150
+ for (const tableKey of sortedTableKeys) {
138
151
  const table = tables[tableKey];
139
152
  const modelName = getModelName(tableKey);
140
153
  const oneRelations = [];
141
154
  const manyRelations = [];
142
155
  const manyRelationsSet = /* @__PURE__ */ new Set();
143
- const foreignFields = Object.entries(table.fields).filter(([_, field]) => field.references);
156
+ const foreignFields = Object.entries(table.fields).filter(([fieldName, field]) => field.references || circularReferenceFields.has(fieldName) && field.type === "string");
144
157
  for (const [fieldName, field] of foreignFields) {
145
- const referencedModel = field.references.model;
158
+ let referencedModel;
159
+ let referenceField = "id";
160
+ if (field.references) {
161
+ referencedModel = field.references.model;
162
+ referenceField = field.references.field || "id";
163
+ } else if (circularReferenceFields.has(fieldName)) if (fieldName === "defaultPaymentMethodId") referencedModel = "payment_method";
164
+ else if (fieldName === "defaultPriceId") referencedModel = "prices";
165
+ else continue;
166
+ else continue;
146
167
  const relationKey = getModelName(referencedModel);
168
+ let relationName;
169
+ if (circularReferenceFields.has(fieldName)) relationName = fieldName.replace(/Id$/, "");
170
+ else relationName = relationKey;
147
171
  const fieldRef = `${getModelName(tableKey)}.${getFieldName({
148
172
  model: tableKey,
149
173
  field: fieldName
150
174
  })}`;
151
175
  const referenceRef = `${getModelName(referencedModel)}.${getFieldName({
152
176
  model: referencedModel,
153
- field: field.references.field || "id"
177
+ field: referenceField
154
178
  })}`;
155
179
  oneRelations.push({
156
- key: relationKey,
180
+ key: relationName,
157
181
  model: getModelName(referencedModel),
158
182
  type: "one",
159
183
  reference: {
@@ -163,7 +187,7 @@ const generateDrizzleSchema = async ({ options, file, adapter }) => {
163
187
  }
164
188
  });
165
189
  }
166
- const otherModels = Object.entries(tables).filter(([modelName$1]) => modelName$1 !== tableKey);
190
+ const otherModels = sortedTableKeys.filter((modelName$1) => modelName$1 !== tableKey).map((modelName$1) => [modelName$1, tables[modelName$1]]);
167
191
  const modelRelationsMap = /* @__PURE__ */ new Map();
168
192
  for (const [modelName$1, otherTable] of otherModels) {
169
193
  const foreignKeysPointingHere = Object.entries(otherTable.fields).filter(([_, field]) => field.references?.model === tableKey || field.references?.model === getModelName(tableKey));
@@ -179,7 +203,16 @@ const generateDrizzleSchema = async ({ options, file, adapter }) => {
179
203
  for (const { modelName: modelName$1, hasMany: hasMany$1 } of modelRelationsMap.values()) {
180
204
  const relationType = hasMany$1 ? "many" : "one";
181
205
  let relationKey = getModelName(modelName$1);
182
- if (!adapter.options?.adapterConfig?.usePlural && relationType === "many") relationKey = `${relationKey}s`;
206
+ if (!adapter.options?.adapterConfig?.usePlural && relationType === "many") {
207
+ if (!relationKey.endsWith("s")) relationKey = `${relationKey}s`;
208
+ }
209
+ const pluralFixes = {
210
+ "productss": "products",
211
+ "pricess": "prices",
212
+ "webhook_deliverys": "webhookDeliveries",
213
+ "webhookDeliverys": "webhookDeliveries"
214
+ };
215
+ if (pluralFixes[relationKey]) relationKey = pluralFixes[relationKey];
183
216
  if (!manyRelationsSet.has(relationKey)) {
184
217
  manyRelationsSet.add(relationKey);
185
218
  manyRelations.push({
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-CWgRQHR-.mjs";
2
+ import { i as getPackageInfo, n as generateSchema } from "./generators-27dPVtx_.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.3.7",
3
+ "version": "0.3.9",
4
4
  "type": "module",
5
5
  "module": "dist/index.mjs",
6
6
  "main": "./dist/index.mjs",