@shadow-dev/orm 1.0.2 → 1.0.3

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.
@@ -2,13 +2,14 @@ import { Model } from "./Model";
2
2
  export declare class Repository<T extends object> {
3
3
  readonly model: Model<T>;
4
4
  constructor(model: Model<T>);
5
- create(data: T): Promise<void>;
5
+ create(data: T): Promise<T>;
6
6
  find(where?: Partial<T>): Promise<T[]>;
7
7
  findOne(where: Partial<T>): Promise<T | null>;
8
- update(where: Partial<T>, data: Partial<T>): Promise<void>;
8
+ update(where: Partial<T>, data: Partial<T>): Promise<T | null>;
9
9
  delete(where: Partial<T>): Promise<void>;
10
10
  count(where?: Partial<T>): Promise<number>;
11
11
  exists(where: Partial<T>): Promise<boolean>;
12
12
  private normalizeValue;
13
13
  private buildWhereClause;
14
+ private getPrimaryKeyField;
14
15
  }
@@ -6,12 +6,38 @@ const Database_1 = require("./Database");
6
6
  class Repository {
7
7
  constructor(model) {
8
8
  this.model = model;
9
+ // Validate PK at construction
10
+ const pk = this.getPrimaryKeyField();
11
+ if (!pk) {
12
+ throw new Error(`Model "${model.name}" has no primary key defined (pk: true)`);
13
+ }
9
14
  }
10
15
  async create(data) {
11
16
  const keys = Object.keys(data);
12
- const sql = `INSERT INTO \`${this.model.name}\` (${keys.join(",")}) VALUES (${keys.map(() => "?").join(",")})`;
17
+ if (keys.length === 0)
18
+ throw new Error("create(): empty data");
19
+ const sql = `INSERT INTO \`${this.model.name}\` (${keys.map(k => `\`${k}\``).join(",")})
20
+ VALUES (${keys.map(() => "?").join(",")})`;
13
21
  const values = keys.map((key) => this.normalizeValue(data[key]));
14
- await (0, Database_1.getPool)().execute(sql, values);
22
+ const [res] = await (0, Database_1.getPool)().execute(sql, values);
23
+ const pk = this.getPrimaryKeyField();
24
+ // If PK exists in data, refetch using it
25
+ if (pk && data[pk] != null) {
26
+ const row = await this.findOne({ [pk]: data[pk] });
27
+ if (row)
28
+ return row;
29
+ }
30
+ // If PK is auto-increment and insertId is present
31
+ if (pk && res.insertId && res.insertId !== 0) {
32
+ const row = await this.findOne({ [pk]: res.insertId });
33
+ if (row)
34
+ return row;
35
+ }
36
+ // Fallback — return data + insertId if available
37
+ if (res.insertId && res.insertId !== 0) {
38
+ return { ...data, id: res.insertId };
39
+ }
40
+ return data;
15
41
  }
16
42
  async find(where = {}) {
17
43
  const { sql, params } = this.buildWhereClause(where);
@@ -27,12 +53,18 @@ class Repository {
27
53
  return results.length > 0 ? results[0] : null;
28
54
  }
29
55
  async update(where, data) {
56
+ if (!where || Object.keys(where).length === 0) {
57
+ throw new Error("update(): missing WHERE");
58
+ }
30
59
  const setKeys = Object.keys(data);
31
- const setClause = setKeys.map(k => `${k} = ?`).join(", ");
60
+ if (setKeys.length === 0)
61
+ return this.findOne(where);
62
+ const setClause = setKeys.map(k => `\`${k}\` = ?`).join(", ");
32
63
  const setValues = setKeys.map(k => this.normalizeValue(data[k]));
33
64
  const { sql: whereClause, params: whereValues } = this.buildWhereClause(where);
34
65
  const query = `UPDATE \`${this.model.name}\` SET ${setClause} ${whereClause}`;
35
66
  await (0, Database_1.getPool)().execute(query, [...setValues, ...whereValues.map(this.normalizeValue)]);
67
+ return this.findOne(where);
36
68
  }
37
69
  async delete(where) {
38
70
  const { sql, params } = this.buildWhereClause(where);
@@ -69,5 +101,13 @@ class Repository {
69
101
  params: values,
70
102
  };
71
103
  }
104
+ getPrimaryKeyField() {
105
+ for (const key of Object.keys(this.model.schema)) {
106
+ if (this.model.schema[key]?.pk) {
107
+ return key;
108
+ }
109
+ }
110
+ return undefined;
111
+ }
72
112
  }
73
113
  exports.Repository = Repository;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@shadow-dev/orm",
3
- "version": "1.0.2",
3
+ "version": "1.0.3",
4
4
  "description": "Lightweight dynamic MySQL ORM designed for ShadowCore and modular apps.",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",