core-nails 1.0.11 → 1.0.13

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.
@@ -0,0 +1,32 @@
1
+ name: Publish to npm
2
+
3
+ on:
4
+ push:
5
+ branches:
6
+ - main
7
+ paths:
8
+ - 'package.json'
9
+
10
+ jobs:
11
+ build-and-publish:
12
+ runs-on: ubuntu-latest
13
+
14
+ steps:
15
+ - uses: actions/checkout@v3
16
+
17
+ - name: Setup Node.js
18
+ uses: actions/setup-node@v3
19
+ with:
20
+ node-version: 20
21
+ registry-url: https://registry.npmjs.org/
22
+
23
+ - name: Install dependencies
24
+ run: npm install
25
+
26
+ - name: Build dist
27
+ run: npm run build
28
+
29
+ - name: Publish to npm
30
+ run: npm publish
31
+ env:
32
+ NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
@@ -1,10 +1,5 @@
1
1
  import { z, ZodObject, ZodRawShape } from "zod";
2
- type WhereOperator = "==" | "!=" | "<" | "<=" | ">" | ">=" | "array-contains" | "in" | "not-in" | "array-contains-any";
3
- type WhereCondition<T> = {
4
- field: keyof T;
5
- operator: WhereOperator;
6
- value: any;
7
- };
2
+ type WhereCondition<T> = Partial<Record<keyof T, any>>;
8
3
  declare function InitializeModel<TSchema extends ZodObject<ZodRawShape>>(opts: {
9
4
  collection: string;
10
5
  schema: TSchema;
@@ -21,10 +16,10 @@ declare function InitializeModel<TSchema extends ZodObject<ZodRawShape>>(opts: {
21
16
  }) | null>;
22
17
  update(data: Partial<z.core.output<TSchema>>, id: string): Promise<void>;
23
18
  destroy(id: string): Promise<void>;
24
- where(conditions: WhereCondition<z.core.output<TSchema>>[]): Promise<(z.core.output<TSchema> & {
19
+ where(conditions: WhereCondition<z.core.output<TSchema>>): Promise<(z.core.output<TSchema> & {
25
20
  id: string;
26
21
  })[]>;
27
- find_by(conditions: WhereCondition<z.core.output<TSchema>>[]): Promise<(z.core.output<TSchema> & {
22
+ find_by(conditions: WhereCondition<z.core.output<TSchema>>): Promise<(z.core.output<TSchema> & {
28
23
  id: string;
29
24
  }) | null>;
30
25
  };
@@ -74,16 +74,16 @@ function InitializeModel(opts) {
74
74
  },
75
75
  async where(conditions) {
76
76
  let query = collection;
77
- for (const cond of conditions) {
78
- query = query.where(cond.field, cond.operator, cond.value);
77
+ for (const [field, value] of Object.entries(conditions)) {
78
+ query = query.where(field, "==", value);
79
79
  }
80
80
  const snapshot = await query.get();
81
81
  return parseDocs(snapshot);
82
82
  },
83
83
  async find_by(conditions) {
84
84
  let query = collection;
85
- for (const cond of conditions) {
86
- query = query.where(cond.field, cond.operator, cond.value);
85
+ for (const [field, value] of Object.entries(conditions)) {
86
+ query = query.where(field, "==", value);
87
87
  }
88
88
  const snapshot = await query.limit(1).get();
89
89
  if (snapshot.empty)
package/package.json CHANGED
@@ -1,8 +1,9 @@
1
1
  {
2
2
  "name": "core-nails",
3
- "version": "1.0.11",
3
+ "version": "1.0.13",
4
4
  "scripts": {
5
- "build": "tsc"
5
+ "build": "tsc",
6
+ "bump": "npm version patch"
6
7
  },
7
8
  "bin": {
8
9
  "nails": "dist/cli/index.js"
@@ -1,22 +1,6 @@
1
1
  import { z, ZodObject, ZodRawShape } from "zod";
2
2
 
3
- type WhereOperator =
4
- | "=="
5
- | "!="
6
- | "<"
7
- | "<="
8
- | ">"
9
- | ">="
10
- | "array-contains"
11
- | "in"
12
- | "not-in"
13
- | "array-contains-any";
14
-
15
- type WhereCondition<T> = {
16
- field: keyof T;
17
- operator: WhereOperator;
18
- value: any;
19
- };
3
+ type WhereCondition<T> = Partial<Record<keyof T, any>>;
20
4
 
21
5
  function InitializeModel<TSchema extends ZodObject<ZodRawShape>>(opts: {
22
6
  collection: string;
@@ -119,34 +103,22 @@ function InitializeModel<TSchema extends ZodObject<ZodRawShape>>(opts: {
119
103
  await collection.doc(id).delete();
120
104
  },
121
105
 
122
- async where(
123
- conditions: WhereCondition<T>[]
124
- ): Promise<WithId[]> {
106
+ async where(conditions: WhereCondition<T>): Promise<WithId[]> {
125
107
  let query: any = collection;
126
108
 
127
- for (const cond of conditions) {
128
- query = query.where(
129
- cond.field as string,
130
- cond.operator,
131
- cond.value
132
- );
109
+ for (const [field, value] of Object.entries(conditions)) {
110
+ query = query.where(field, "==", value);
133
111
  }
134
112
 
135
113
  const snapshot = await query.get();
136
114
  return parseDocs(snapshot);
137
115
  },
138
116
 
139
- async find_by(
140
- conditions: WhereCondition<T>[]
141
- ): Promise<WithId | null> {
117
+ async find_by(conditions: WhereCondition<T>): Promise<WithId | null> {
142
118
  let query: any = collection;
143
119
 
144
- for (const cond of conditions) {
145
- query = query.where(
146
- cond.field as string,
147
- cond.operator,
148
- cond.value
149
- );
120
+ for (const [field, value] of Object.entries(conditions)) {
121
+ query = query.where(field, "==", value);
150
122
  }
151
123
 
152
124
  const snapshot = await query.limit(1).get();