storemw-core-api 1.0.4 → 1.0.6

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.
@@ -42,9 +42,7 @@ const ModelFactory = ({ debug = false, prisma, modelName, primaryKey = "id", acc
42
42
  const _data = { ...data, ...(0, default_1.getDefaultCreateFields)(actionUserId, accountId) };
43
43
  if (debug) {
44
44
  console.log(`[ModelFactory(data)]: ${JSON.stringify(_data)}`);
45
- console.log("[ModelFactory:init]", { modelName, primaryKey });
46
45
  console.log("[ModelFactory:delegate]", JSON.stringify(Object.keys(prisma)));
47
- console.log("[ModelFactory:model]", Object.keys(delegate));
48
46
  }
49
47
  return delegate.create({
50
48
  data: _data,
@@ -53,12 +51,18 @@ const ModelFactory = ({ debug = false, prisma, modelName, primaryKey = "id", acc
53
51
  const get = async ({ id, where, include, }) => {
54
52
  const condition = id ? { [primaryKey]: id } : where ?? {};
55
53
  const condition2 = { accountid: accountId };
54
+ if (debug) {
55
+ console.log("[ModelFactory:delegate]", JSON.stringify(Object.keys(prisma)));
56
+ }
56
57
  return delegate.findFirst({
57
58
  where: { ...condition, ...condition2, ...default_1.getDefaultGetWhere },
58
59
  include,
59
60
  });
60
61
  };
61
62
  const list = async ({ where, orderBy, offset, limit, include } = {}) => {
63
+ if (debug) {
64
+ console.log("[ModelFactory:delegate]", JSON.stringify(Object.keys(prisma)));
65
+ }
62
66
  return delegate.findMany({
63
67
  where,
64
68
  orderBy,
@@ -68,6 +72,10 @@ const ModelFactory = ({ debug = false, prisma, modelName, primaryKey = "id", acc
68
72
  });
69
73
  };
70
74
  const update = async ({ id, where, data }) => {
75
+ if (debug) {
76
+ console.log(`[ModelFactory(data)]: ${JSON.stringify(data)}`);
77
+ console.log("[ModelFactory:delegate]", JSON.stringify(Object.keys(prisma)));
78
+ }
71
79
  return prisma.$transaction(async (tx) => {
72
80
  const modelTx = tx[modelName];
73
81
  // prefer primary key if provided, otherwise fall back to custom where
@@ -96,6 +104,9 @@ const ModelFactory = ({ debug = false, prisma, modelName, primaryKey = "id", acc
96
104
  });
97
105
  };
98
106
  const trash = async ({ ids }) => {
107
+ if (debug) {
108
+ console.log("[ModelFactory:delegate]", JSON.stringify(Object.keys(prisma)));
109
+ }
99
110
  return prisma.$transaction(async (tx) => {
100
111
  const modelTx = tx[modelName];
101
112
  const result = await modelTx.updateMany({
@@ -111,6 +122,9 @@ const ModelFactory = ({ debug = false, prisma, modelName, primaryKey = "id", acc
111
122
  });
112
123
  };
113
124
  const remove = async ({ ids, where }) => {
125
+ if (debug) {
126
+ console.log("[ModelFactory:delegate]", JSON.stringify(Object.keys(prisma)));
127
+ }
114
128
  return prisma.$transaction(async (tx) => {
115
129
  const modelTx = tx[modelName];
116
130
  // prefer primary ids if provided, otherwise fall back to custom where
@@ -139,26 +153,20 @@ const ModelFactory = ({ debug = false, prisma, modelName, primaryKey = "id", acc
139
153
  return updated;
140
154
  });
141
155
  };
142
- // function convertBigIntToNumber(obj: any): any {
143
- // if (typeof obj === "bigint") {
144
- // return Number(obj);
145
- // }
146
- // if (Array.isArray(obj)) {
147
- // return obj.map((v) => convertBigIntToNumber(v));
148
- // }
149
- // if (obj !== null && typeof obj === "object") {
150
- // return Object.fromEntries(
151
- // Object.entries(obj).map(([k, v]) => [k, convertBigIntToNumber(v)])
152
- // );
153
- // }
154
- // return obj;
155
- // }
156
+ // Used for SELECT queries (read operations)
156
157
  const raw = async (query, params = []) => {
158
+ if (debug) {
159
+ console.log("[ModelFactory:delegate]", JSON.stringify(Object.keys(prisma)));
160
+ }
157
161
  const client = prisma;
158
162
  // return prisma.$queryRawUnsafe<T[]>(query, ...params);
159
163
  return client.$queryRawUnsafe(query, ...params);
160
164
  };
165
+ // Used for mutations (INSERT, UPDATE, DELETE, DDL like CREATE TABLE etc.)
161
166
  const rawExec = async (query, params = []) => {
167
+ if (debug) {
168
+ console.log("[ModelFactory:delegate]", JSON.stringify(Object.keys(prisma)));
169
+ }
162
170
  return prisma.$executeRawUnsafe(query, ...params); // returns number of affected rows
163
171
  };
164
172
  return { primaryKey, getFields, create, get, list, update, trash, remove, raw, rawExec };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "storemw-core-api",
3
- "version": "1.0.4",
3
+ "version": "1.0.6",
4
4
  "description": "STOREMW Core API",
5
5
  "main": "dist/exports.js",
6
6
  "types": "dist/exports.d.ts",